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