3 * Send XML response back to AJAX request.
8 class WP_Ajax_Response {
10 * Store XML responses to send.
16 private $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 public function __construct( $args = '' ) {
33 * Make private properties readable for backwards compatibility.
38 * @param string $name Property to get.
39 * @return mixed Property.
41 public function __get( $name ) {
46 * Make private properties settable for backwards compatibility.
51 * @param string $name Property to set.
52 * @param mixed $value Property value.
53 * @return mixed Newly-set property.
55 public function __set( $name, $value ) {
56 return $this->$name = $value;
60 * Make private properties checkable for backwards compatibility.
65 * @param string $name Property to check if set.
66 * @return bool Whether the property is set.
68 public function __isset( $name ) {
69 return isset( $this->$name );
73 * Make private properties un-settable for backwards compatibility.
78 * @param string $name Property to unset.
80 public function __unset( $name ) {
81 unset( $this->$name );
85 * Append to XML response based on given arguments.
87 * The arguments that can be passed in the $args parameter are below. It is
88 * also possible to pass a WP_Error object in either the 'id' or 'data'
89 * argument. The parameter isn't actually optional, content should be given
90 * in order to send the correct response.
92 * 'what' argument is a string that is the XMLRPC response type.
93 * 'action' argument is a boolean or string that acts like a nonce.
94 * 'id' argument can be WP_Error or an integer.
95 * 'old_id' argument is false by default or an integer of the previous ID.
96 * 'position' argument is an integer or a string with -1 = top, 1 = bottom,
97 * html ID = after, -html ID = before.
98 * 'data' argument is a string with the content or message.
99 * 'supplemental' argument is an array of strings that will be children of
100 * the supplemental element.
104 * @param string|array $args Override defaults.
105 * @return string XML response.
107 public function add( $args = '' ) {
109 'what' => 'object', 'action' => false,
110 'id' => '0', 'old_id' => false,
112 'data' => '', 'supplemental' => array()
115 $r = wp_parse_args( $args, $defaults );
117 $position = preg_replace( '/[^a-z0-9:_-]/i', '', $r['position'] );
120 $action = $r['action'];
121 $old_id = $r['old_id'];
124 if ( is_wp_error( $id ) ) {
130 if ( is_wp_error( $data ) ) {
131 foreach ( (array) $data->get_error_codes() as $code ) {
132 $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . "]]></wp_error>";
133 if ( ! $error_data = $data->get_error_data( $code ) ) {
137 if ( is_object( $error_data ) ) {
138 $class = ' class="' . get_class( $error_data ) . '"';
139 $error_data = get_object_vars( $error_data );
142 $response .= "<wp_error_data code='$code'$class>";
144 if ( is_scalar( $error_data ) ) {
145 $response .= "<![CDATA[$error_data]]>";
146 } elseif ( is_array( $error_data ) ) {
147 foreach ( $error_data as $k => $v ) {
148 $response .= "<$k><![CDATA[$v]]></$k>";
152 $response .= "</wp_error_data>";
155 $response = "<response_data><![CDATA[$data]]></response_data>";
159 if ( is_array( $r['supplemental'] ) ) {
160 foreach ( $r['supplemental'] as $k => $v ) {
161 $s .= "<$k><![CDATA[$v]]></$k>";
163 $s = "<supplemental>$s</supplemental>";
166 if ( false === $action ) {
167 $action = $_POST['action'];
170 $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action
171 $x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>";
177 $this->responses[] = $x;
182 * Display XML formatted responses.
184 * Sets the content type header to text/xml.
188 public function send() {
189 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) );
190 echo "<?xml version='1.0' encoding='" . get_option( 'blog_charset' ) . "' standalone='yes'?><wp_ajax>";
191 foreach ( (array) $this->responses as $response )
194 if ( defined( 'DOING_AJAX' ) && DOING_AJAX )