PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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