You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

158 lines
4.9 KiB

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