PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-wp-ajax-response.php

http://github.com/wordpress/wordpress
PHP | 163 lines | 89 code | 13 blank | 61 comment | 11 complexity | 5493514a74ba19ffd4e4c47200b08fdc MD5 | raw file
Possible License(s): 0BSD
  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. /**
  30. * Appends data to an XML response based on given arguments.
  31. *
  32. * With `$args` defaults, extra data output would be:
  33. *
  34. * <response action='{$action}_$id'>
  35. * <$what id='$id' position='$position'>
  36. * <response_data><![CDATA[$data]]></response_data>
  37. * </$what>
  38. * </response>
  39. *
  40. * @since 2.1.0
  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',
  66. 'action' => false,
  67. 'id' => '0',
  68. 'old_id' => false,
  69. 'position' => 1,
  70. 'data' => '',
  71. 'supplemental' => array(),
  72. );
  73. $parsed_args = wp_parse_args( $args, $defaults );
  74. $position = preg_replace( '/[^a-z0-9:_-]/i', '', $parsed_args['position'] );
  75. $id = $parsed_args['id'];
  76. $what = $parsed_args['what'];
  77. $action = $parsed_args['action'];
  78. $old_id = $parsed_args['old_id'];
  79. $data = $parsed_args['data'];
  80. if ( is_wp_error( $id ) ) {
  81. $data = $id;
  82. $id = 0;
  83. }
  84. $response = '';
  85. if ( is_wp_error( $data ) ) {
  86. foreach ( (array) $data->get_error_codes() as $code ) {
  87. $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . ']]></wp_error>';
  88. $error_data = $data->get_error_data( $code );
  89. if ( ! $error_data ) {
  90. continue;
  91. }
  92. $class = '';
  93. if ( is_object( $error_data ) ) {
  94. $class = ' class="' . get_class( $error_data ) . '"';
  95. $error_data = get_object_vars( $error_data );
  96. }
  97. $response .= "<wp_error_data code='$code'$class>";
  98. if ( is_scalar( $error_data ) ) {
  99. $response .= "<![CDATA[$error_data]]>";
  100. } elseif ( is_array( $error_data ) ) {
  101. foreach ( $error_data as $k => $v ) {
  102. $response .= "<$k><![CDATA[$v]]></$k>";
  103. }
  104. }
  105. $response .= '</wp_error_data>';
  106. }
  107. } else {
  108. $response = "<response_data><![CDATA[$data]]></response_data>";
  109. }
  110. $s = '';
  111. if ( is_array( $parsed_args['supplemental'] ) ) {
  112. foreach ( $parsed_args['supplemental'] as $k => $v ) {
  113. $s .= "<$k><![CDATA[$v]]></$k>";
  114. }
  115. $s = "<supplemental>$s</supplemental>";
  116. }
  117. if ( false === $action ) {
  118. $action = $_POST['action'];
  119. }
  120. $x = '';
  121. $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action.
  122. $x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>";
  123. $x .= $response;
  124. $x .= $s;
  125. $x .= "</$what>";
  126. $x .= '</response>';
  127. $this->responses[] = $x;
  128. return $x;
  129. }
  130. /**
  131. * Display XML formatted responses.
  132. *
  133. * Sets the content type header to text/xml.
  134. *
  135. * @since 2.1.0
  136. */
  137. public function send() {
  138. header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) );
  139. echo "<?xml version='1.0' encoding='" . get_option( 'blog_charset' ) . "' standalone='yes'?><wp_ajax>";
  140. foreach ( (array) $this->responses as $response ) {
  141. echo $response;
  142. }
  143. echo '</wp_ajax>';
  144. if ( wp_doing_ajax() ) {
  145. wp_die();
  146. } else {
  147. die();
  148. }
  149. }
  150. }