class-wp-ajax-response.php 4.9 KB

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