WP_Ajax_Response

Send XML response back to Ajax request.

Defined (1)

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

/bp-forums/bbpress/bb-includes/backpress/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. * @access private 
  7. */ 
  8. var $responses = array(); 
  9.  
  10. /** 
  11. * PHP4 Constructor - Passes args to {@link WP_Ajax_Response::add()}. 
  12. * @since 2.1.0 
  13. * @see WP_Ajax_Response::add() 
  14. * @param string|array $args Optional. Will be passed to add() method. 
  15. * @return WP_Ajax_Response 
  16. */ 
  17. function __construct( $args = '' ) { 
  18. if ( !empty($args) ) 
  19. $this->add($args); 
  20.  
  21. function WP_Ajax_Response( $args = '' ) { 
  22. $this->__construct( $args ); 
  23.  
  24. /** 
  25. * Append to XML response based on given arguments. 
  26. * The arguments that can be passed in the $args parameter are below. It is 
  27. * also possible to pass a WP_Error object in either the 'id' or 'data' 
  28. * argument. The parameter isn't actually optional, content should be given 
  29. * in order to send the correct response. 
  30. * 'what' argument is a string that is the XMLRPC response type. 
  31. * 'action' argument is a boolean or string that acts like a nonce. 
  32. * 'id' argument can be WP_Error or an integer. 
  33. * 'old_id' argument is false by default or an integer of the previous ID. 
  34. * 'position' argument is an integer or a string with -1 = top, 1 = bottom,  
  35. * html ID = after, -html ID = before. 
  36. * 'data' argument is a string with the content or message. 
  37. * 'supplemental' argument is an array of strings that will be children of 
  38. * the supplemental element. 
  39. * @since 2.1.0 
  40. * @param string|array $args Override defaults. 
  41. * @return string XML response. 
  42. */ 
  43. function add( $args = '' ) { 
  44. $defaults = array( 
  45. 'what' => 'object', 'action' => false,  
  46. 'id' => '0', 'old_id' => false,  
  47. 'position' => 1,  
  48. 'data' => '', 'supplemental' => array() 
  49. ); 
  50.  
  51. $r = wp_parse_args( $args, $defaults ); 
  52. extract( $r, EXTR_SKIP ); 
  53. $position = preg_replace( '/[^a-z0-9:_-]/i', '', $position ); 
  54.  
  55. if ( is_wp_error($id) ) { 
  56. $data = $id; 
  57. $id = 0; 
  58.  
  59. $response = ''; 
  60. if ( is_wp_error($data) ) { 
  61. foreach ( (array) $data->get_error_codes() as $code ) { 
  62. $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message($code) . "]]></wp_error>"; 
  63. if ( !$error_data = $data->get_error_data($code) ) 
  64. continue; 
  65. $class = ''; 
  66. if ( is_object($error_data) ) { 
  67. $class = ' class="' . get_class($error_data) . '"'; 
  68. $error_data = get_object_vars($error_data); 
  69.  
  70. $response .= "<wp_error_data code='$code'$class>"; 
  71.  
  72. if ( is_scalar($error_data) ) { 
  73. $response .= "<![CDATA[$error_data]]>"; 
  74. } elseif ( is_array($error_data) ) { 
  75. foreach ( $error_data as $k => $v ) 
  76. $response .= "<$k><![CDATA[$v]]></$k>"; 
  77.  
  78. $response .= "</wp_error_data>"; 
  79. } else { 
  80. $response = "<response_data><![CDATA[$data]]></response_data>"; 
  81.  
  82. $s = ''; 
  83. if ( is_array($supplemental) ) { 
  84. foreach ( $supplemental as $k => $v ) 
  85. $s .= "<$k><![CDATA[$v]]></$k>"; 
  86. $s = "<supplemental>$s</supplemental>"; 
  87.  
  88. if ( false === $action ) 
  89. $action = $_POST['action']; 
  90.  
  91. $x = ''; 
  92. $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action 
  93. $x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>"; 
  94. $x .= $response; 
  95. $x .= $s; 
  96. $x .= "</$what>"; 
  97. $x .= "</response>"; 
  98.  
  99. $this->responses[] = $x; 
  100. return $x; 
  101.  
  102. /** 
  103. * Display XML formatted responses. 
  104. * Sets the content type header to text/xml. 
  105. * @since 2.1.0 
  106. */ 
  107. function send() { 
  108. header('Content-Type: text/xml'); 
  109. echo "<?xml version='1.0' standalone='yes'?><wp_ajax>"; 
  110. foreach ( (array) $this->responses as $response ) 
  111. echo $response; 
  112. echo '</wp_ajax>'; 
  113. die();