16 r[p[0]] = p[1]; |
16 r[p[0]] = p[1]; |
17 } |
17 } |
18 return r; |
18 return r; |
19 }, |
19 }, |
20 parseAjaxResponse: function( x, r, e ) { // 1 = good, 0 = strange (bad data?), -1 = you lack permission. |
20 parseAjaxResponse: function( x, r, e ) { // 1 = good, 0 = strange (bad data?), -1 = you lack permission. |
21 var parsed = {}, re = jQuery('#' + r).empty(), err = ''; |
21 var parsed = {}, re = jQuery('#' + r).empty(), err = '', noticeMessage = ''; |
22 |
22 |
23 if ( x && typeof x === 'object' && x.getElementsByTagName('wp_ajax') ) { |
23 if ( x && typeof x === 'object' && x.getElementsByTagName('wp_ajax') ) { |
24 parsed.responses = []; |
24 parsed.responses = []; |
25 parsed.errors = false; |
25 parsed.errors = false; |
26 jQuery('response', x).each( function() { |
26 jQuery('response', x).each( function() { |
27 var th = jQuery(this), child = jQuery(this.firstChild), response; |
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') }; |
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(); |
29 response.data = jQuery( 'response_data', child ).text(); |
30 response.supplemental = {}; |
30 response.supplemental = {}; |
31 if ( !jQuery( 'supplemental', child ).children().each( function() { |
31 if ( !jQuery( 'supplemental', child ).children().each( function() { |
|
32 |
|
33 if ( this.nodeName === 'notice' ) { |
|
34 noticeMessage += jQuery(this).text(); |
|
35 return; |
|
36 } |
|
37 |
32 response.supplemental[this.nodeName] = jQuery(this).text(); |
38 response.supplemental[this.nodeName] = jQuery(this).text(); |
33 } ).length ) { response.supplemental = false; } |
39 } ).length ) { response.supplemental = false; } |
34 response.errors = []; |
40 response.errors = []; |
35 if ( !jQuery('wp_error', child).each( function() { |
41 if ( !jQuery('wp_error', child).each( function() { |
36 var code = jQuery(this).attr('code'), anError, errorData, formField; |
42 var code = jQuery(this).attr('code'), anError, errorData, formField; |
44 response.errors.push( anError ); |
50 response.errors.push( anError ); |
45 parsed.errors = true; |
51 parsed.errors = true; |
46 } ).length ) { response.errors = false; } |
52 } ).length ) { response.errors = false; } |
47 parsed.responses.push( response ); |
53 parsed.responses.push( response ); |
48 } ); |
54 } ); |
49 if ( err.length ) { re.html( '<div class="error">' + err + '</div>' ); } |
55 if ( err.length ) { |
|
56 re.html( '<div class="error">' + err + '</div>' ); |
|
57 wp.a11y.speak( err ); |
|
58 } else if ( noticeMessage.length ) { |
|
59 re.html( '<div class="updated notice is-dismissible"><p>' + noticeMessage + '</p></div>'); |
|
60 jQuery(document).trigger( 'wp-updates-notice-added' ); |
|
61 wp.a11y.speak( noticeMessage ); |
|
62 } |
50 return parsed; |
63 return parsed; |
51 } |
64 } |
52 if ( isNaN(x) ) { return !re.html('<div class="error"><p>' + x + '</p></div>'); } |
65 if ( isNaN(x) ) { return !re.html('<div class="error"><p>' + x + '</p></div>'); } |
53 x = parseInt(x,10); |
66 x = parseInt(x,10); |
54 if ( -1 === x ) { return !re.html('<div class="error"><p>' + wpAjax.noPerm + '</p></div>'); } |
67 if ( -1 === x ) { return !re.html('<div class="error"><p>' + wpAjax.noPerm + '</p></div>'); } |