Keine Beschreibung

class-wp-ajax-response.php 5.1KB

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