3 * Send XML response back to AJAX request.
8 class WP_Ajax_Response {
10 * Store XML responses to send.
15 public $responses = array();
18 * Constructor - Passes args to {@link WP_Ajax_Response::add()}.
21 * @see WP_Ajax_Response::add()
23 * @param string|array $args Optional. Will be passed to add() method.
25 public function __construct( $args = '' ) {
31 * Append to XML response based on given arguments.
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.
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.
50 * @param string|array $args Override defaults.
51 * @return string XML response.
53 public function add( $args = '' ) {
55 'what' => 'object', 'action' => false,
56 'id' => '0', 'old_id' => false,
58 'data' => '', 'supplemental' => array()
61 $r = wp_parse_args( $args, $defaults );
63 $position = preg_replace( '/[^a-z0-9:_-]/i', '', $r['position'] );
66 $action = $r['action'];
67 $old_id = $r['old_id'];
70 if ( is_wp_error( $id ) ) {
76 if ( is_wp_error( $data ) ) {
77 foreach ( (array) $data->get_error_codes() as $code ) {
78 $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . "]]></wp_error>";
79 if ( ! $error_data = $data->get_error_data( $code ) ) {
83 if ( is_object( $error_data ) ) {
84 $class = ' class="' . get_class( $error_data ) . '"';
85 $error_data = get_object_vars( $error_data );
88 $response .= "<wp_error_data code='$code'$class>";
90 if ( is_scalar( $error_data ) ) {
91 $response .= "<![CDATA[$error_data]]>";
92 } elseif ( is_array( $error_data ) ) {
93 foreach ( $error_data as $k => $v ) {
94 $response .= "<$k><![CDATA[$v]]></$k>";
98 $response .= "</wp_error_data>";
101 $response = "<response_data><![CDATA[$data]]></response_data>";
105 if ( is_array( $r['supplemental'] ) ) {
106 foreach ( $r['supplemental'] as $k => $v ) {
107 $s .= "<$k><![CDATA[$v]]></$k>";
109 $s = "<supplemental>$s</supplemental>";
112 if ( false === $action ) {
113 $action = $_POST['action'];
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'>";
123 $this->responses[] = $x;
128 * Display XML formatted responses.
130 * Sets the content type header to text/xml.
134 public function send() {
135 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) );
136 echo "<?xml version='1.0' encoding='" . get_option( 'blog_charset' ) . "' standalone='yes'?><wp_ajax>";
137 foreach ( (array) $this->responses as $response )
140 if ( defined( 'DOING_AJAX' ) && DOING_AJAX )