3 * Send XML response back to AJAX request.
8 class WP_Ajax_Response {
10 * Store XML responses to send.
16 var $responses = array();
19 * Constructor - Passes args to {@link WP_Ajax_Response::add()}.
22 * @see WP_Ajax_Response::add()
24 * @param string|array $args Optional. Will be passed to add() method.
25 * @return WP_Ajax_Response
27 function __construct( $args = '' ) {
33 * Append to XML response based on given arguments.
35 * The arguments that can be passed in the $args parameter are below. It is
36 * also possible to pass a WP_Error object in either the 'id' or 'data'
37 * argument. The parameter isn't actually optional, content should be given
38 * in order to send the correct response.
40 * 'what' argument is a string that is the XMLRPC response type.
41 * 'action' argument is a boolean or string that acts like a nonce.
42 * 'id' argument can be WP_Error or an integer.
43 * 'old_id' argument is false by default or an integer of the previous ID.
44 * 'position' argument is an integer or a string with -1 = top, 1 = bottom,
45 * html ID = after, -html ID = before.
46 * 'data' argument is a string with the content or message.
47 * 'supplemental' argument is an array of strings that will be children of
48 * the supplemental element.
52 * @param string|array $args Override defaults.
53 * @return string XML response.
55 function add( $args = '' ) {
57 'what' => 'object', 'action' => false,
58 'id' => '0', 'old_id' => false,
60 'data' => '', 'supplemental' => array()
63 $r = wp_parse_args( $args, $defaults );
64 extract( $r, EXTR_SKIP );
65 $position = preg_replace( '/[^a-z0-9:_-]/i', '', $position );
67 if ( is_wp_error($id) ) {
73 if ( is_wp_error($data) ) {
74 foreach ( (array) $data->get_error_codes() as $code ) {
75 $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message($code) . "]]></wp_error>";
76 if ( !$error_data = $data->get_error_data($code) )
79 if ( is_object($error_data) ) {
80 $class = ' class="' . get_class($error_data) . '"';
81 $error_data = get_object_vars($error_data);
84 $response .= "<wp_error_data code='$code'$class>";
86 if ( is_scalar($error_data) ) {
87 $response .= "<![CDATA[$error_data]]>";
88 } elseif ( is_array($error_data) ) {
89 foreach ( $error_data as $k => $v )
90 $response .= "<$k><![CDATA[$v]]></$k>";
93 $response .= "</wp_error_data>";
96 $response = "<response_data><![CDATA[$data]]></response_data>";
100 if ( is_array($supplemental) ) {
101 foreach ( $supplemental as $k => $v )
102 $s .= "<$k><![CDATA[$v]]></$k>";
103 $s = "<supplemental>$s</supplemental>";
106 if ( false === $action )
107 $action = $_POST['action'];
110 $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action
111 $x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>";
117 $this->responses[] = $x;
122 * Display XML formatted responses.
124 * Sets the content type header to text/xml.
129 header('Content-Type: text/xml');
130 echo "<?xml version='1.0' standalone='yes'?><wp_ajax>";
131 foreach ( (array) $this->responses as $response )