You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.7 KiB

1 year ago
  1. /**
  2. * @output wp-includes/js/wp-ajax-response.js
  3. */
  4. /* global wpAjax */
  5. window.wpAjax = jQuery.extend( {
  6. unserialize: function( s ) {
  7. var r = {}, q, pp, i, p;
  8. if ( !s ) { return r; }
  9. q = s.split('?'); if ( q[1] ) { s = q[1]; }
  10. pp = s.split('&');
  11. for ( i in pp ) {
  12. if ( typeof pp.hasOwnProperty === 'function' && !pp.hasOwnProperty(i) ) { continue; }
  13. p = pp[i].split('=');
  14. r[p[0]] = p[1];
  15. }
  16. return r;
  17. },
  18. parseAjaxResponse: function( x, r, e ) { // 1 = good, 0 = strange (bad data?), -1 = you lack permission.
  19. var parsed = {}, re = jQuery('#' + r).empty(), err = '', noticeMessage = '';
  20. if ( x && typeof x === 'object' && x.getElementsByTagName('wp_ajax') ) {
  21. parsed.responses = [];
  22. parsed.errors = false;
  23. jQuery('response', x).each( function() {
  24. var th = jQuery(this), child = jQuery(this.firstChild), response;
  25. response = { action: th.attr('action'), what: child.get(0).nodeName, id: child.attr('id'), oldId: child.attr('old_id'), position: child.attr('position') };
  26. response.data = jQuery( 'response_data', child ).text();
  27. response.supplemental = {};
  28. if ( !jQuery( 'supplemental', child ).children().each( function() {
  29. if ( this.nodeName === 'notice' ) {
  30. noticeMessage += jQuery(this).text();
  31. return;
  32. }
  33. response.supplemental[this.nodeName] = jQuery(this).text();
  34. } ).length ) { response.supplemental = false; }
  35. response.errors = [];
  36. if ( !jQuery('wp_error', child).each( function() {
  37. var code = jQuery(this).attr('code'), anError, errorData, formField;
  38. anError = { code: code, message: this.firstChild.nodeValue, data: false };
  39. errorData = jQuery('wp_error_data[code="' + code + '"]', x);
  40. if ( errorData ) { anError.data = errorData.get(); }
  41. formField = jQuery( 'form-field', errorData ).text();
  42. if ( formField ) { code = formField; }
  43. if ( e ) { wpAjax.invalidateForm( jQuery('#' + e + ' :input[name="' + code + '"]' ).parents('.form-field:first') ); }
  44. err += '<p>' + anError.message + '</p>';
  45. response.errors.push( anError );
  46. parsed.errors = true;
  47. } ).length ) { response.errors = false; }
  48. parsed.responses.push( response );
  49. } );
  50. if ( err.length ) {
  51. re.html( '<div class="notice notice-error">' + err + '</div>' );
  52. wp.a11y.speak( err );
  53. } else if ( noticeMessage.length ) {
  54. re.html( '<div class="notice notice-success is-dismissible"><p>' + noticeMessage + '</p></div>');
  55. jQuery(document).trigger( 'wp-updates-notice-added' );
  56. wp.a11y.speak( noticeMessage );
  57. }
  58. return parsed;
  59. }
  60. if ( isNaN( x ) ) {
  61. wp.a11y.speak( x );
  62. return ! re.html( '<div class="notice notice-error"><p>' + x + '</p></div>' );
  63. }
  64. x = parseInt( x, 10 );
  65. if ( -1 === x ) {
  66. wp.a11y.speak( wpAjax.noPerm );
  67. return ! re.html( '<div class="notice notice-error"><p>' + wpAjax.noPerm + '</p></div>' );
  68. } else if ( 0 === x ) {
  69. wp.a11y.speak( wpAjax.broken );
  70. return ! re.html( '<div class="notice notice-error"><p>' + wpAjax.broken + '</p></div>' );
  71. }
  72. return true;
  73. },
  74. invalidateForm: function ( selector ) {
  75. return jQuery( selector ).addClass( 'form-invalid' ).find('input').one( 'change wp-check-valid-field', function() { jQuery(this).closest('.form-invalid').removeClass( 'form-invalid' ); } );
  76. },
  77. validateForm: function( selector ) {
  78. selector = jQuery( selector );
  79. return !wpAjax.invalidateForm( selector.find('.form-required').filter( function() { return jQuery('input:visible', this).val() === ''; } ) ).length;
  80. }
  81. }, wpAjax || { noPerm: 'Sorry, you are not allowed to do that.', broken: 'Something went wrong.' } );
  82. // Basic form validation.
  83. jQuery( function($){
  84. $('form.validate').on( 'submit', function() { return wpAjax.validateForm( $(this) ); } );
  85. });