WP_Ajax_Response

Send XML response back to Ajax request.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-ajax-response.php  
  1. class WP_Ajax_Response { 
  2. /** 
  3. * Store XML responses to send. 
  4. * @since 2.1.0 
  5. * @var array 
  6. */ 
  7. public $responses = array(); 
  8.  
  9. /** 
  10. * Constructor - Passes args to WP_Ajax_Response::add(). 
  11. * @since 2.1.0 
  12. * @see WP_Ajax_Response::add() 
  13. * @param string|array $args Optional. Will be passed to add() method. 
  14. */ 
  15. public function __construct( $args = '' ) { 
  16. if ( !empty($args) ) 
  17. $this->add($args); 
  18.  
  19. /** 
  20. * Appends data to an XML response based on given arguments. 
  21. * With `$args` defaults, extra data output would be: 
  22. * <response action='{$action}_$id'> 
  23. * <$what id='$id' position='$position'> 
  24. * <response_data><![CDATA[$data]]></response_data> 
  25. * </$what> 
  26. * </response> 
  27. * @since 2.1.0 
  28. * @access public 
  29. * @param string|array $args { 
  30. * Optional. An array or string of XML response arguments. 
  31. * @type string $what XML-RPC response type. Used as a child element of `<response>`. 
  32. * Default 'object' (`<object>`). 
  33. * @type string|false $action Value to use for the `action` attribute in `<response>`. Will be 
  34. * appended with `_$id` on output. If false, `$action` will default to 
  35. * the value of `$_POST['action']`. Default false. 
  36. * @type int|WP_Error $id The response ID, used as the response type `id` attribute. Also 
  37. * accepts a `WP_Error` object if the ID does not exist. Default 0. 
  38. * @type int|false $old_id The previous response ID. Used as the value for the response type 
  39. * `old_id` attribute. False hides the attribute. Default false. 
  40. * @type string $position Value of the response type `position` attribute. Accepts 1 (bottom),  
  41. * -1 (top), html ID (after), or -html ID (before). Default 1 (bottom). 
  42. * @type string|WP_Error $data The response content/message. Also accepts a WP_Error object if the 
  43. * ID does not exist. Default empty. 
  44. * @type array $supplemental An array of extra strings that will be output within a `<supplemental>` 
  45. * element as CDATA. Default empty array. 
  46. * } 
  47. * @return string XML response. 
  48. */ 
  49. public function add( $args = '' ) { 
  50. $defaults = array( 
  51. 'what' => 'object', 'action' => false,  
  52. 'id' => '0', 'old_id' => false,  
  53. 'position' => 1,  
  54. 'data' => '', 'supplemental' => array() 
  55. ); 
  56.  
  57. $r = wp_parse_args( $args, $defaults ); 
  58.  
  59. $position = preg_replace( '/[^a-z0-9:_-]/i', '', $r['position'] ); 
  60. $id = $r['id']; 
  61. $what = $r['what']; 
  62. $action = $r['action']; 
  63. $old_id = $r['old_id']; 
  64. $data = $r['data']; 
  65.  
  66. if ( is_wp_error( $id ) ) { 
  67. $data = $id; 
  68. $id = 0; 
  69.  
  70. $response = ''; 
  71. if ( is_wp_error( $data ) ) { 
  72. foreach ( (array) $data->get_error_codes() as $code ) { 
  73. $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . "]]></wp_error>"; 
  74. if ( ! $error_data = $data->get_error_data( $code ) ) { 
  75. continue; 
  76. $class = ''; 
  77. if ( is_object( $error_data ) ) { 
  78. $class = ' class="' . get_class( $error_data ) . '"'; 
  79. $error_data = get_object_vars( $error_data ); 
  80.  
  81. $response .= "<wp_error_data code='$code'$class>"; 
  82.  
  83. if ( is_scalar( $error_data ) ) { 
  84. $response .= "<![CDATA[$error_data]]>"; 
  85. } elseif ( is_array( $error_data ) ) { 
  86. foreach ( $error_data as $k => $v ) { 
  87. $response .= "<$k><![CDATA[$v]]></$k>"; 
  88.  
  89. $response .= "</wp_error_data>"; 
  90. } else { 
  91. $response = "<response_data><![CDATA[$data]]></response_data>"; 
  92.  
  93. $s = ''; 
  94. if ( is_array( $r['supplemental'] ) ) { 
  95. foreach ( $r['supplemental'] as $k => $v ) { 
  96. $s .= "<$k><![CDATA[$v]]></$k>"; 
  97. $s = "<supplemental>$s</supplemental>"; 
  98.  
  99. if ( false === $action ) { 
  100. $action = $_POST['action']; 
  101. $x = ''; 
  102. $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action 
  103. $x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>"; 
  104. $x .= $response; 
  105. $x .= $s; 
  106. $x .= "</$what>"; 
  107. $x .= "</response>"; 
  108.  
  109. $this->responses[] = $x; 
  110. return $x; 
  111.  
  112. /** 
  113. * Display XML formatted responses. 
  114. * Sets the content type header to text/xml. 
  115. * @since 2.1.0 
  116. */ 
  117. public function send() { 
  118. header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) ); 
  119. echo "<?xml version='1.0' encoding='" . get_option( 'blog_charset' ) . "' standalone='yes'?><wp_ajax>"; 
  120. foreach ( (array) $this->responses as $response ) 
  121. echo $response; 
  122. echo '</wp_ajax>'; 
  123. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) 
  124. wp_die(); 
  125. else 
  126. die();