]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-wp-ajax-response.php
WordPress 4.2
[autoinstalls/wordpress.git] / wp-includes / class-wp-ajax-response.php
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         /**
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          */
25         public function __construct( $args = '' ) {
26                 if ( !empty($args) )
27                         $this->add($args);
28         }
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         public 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
61                 $r = wp_parse_args( $args, $defaults );
62
63                 $position = preg_replace( '/[^a-z0-9:_-]/i', '', $r['position'] );
64                 $id = $r['id'];
65                 $what = $r['what'];
66                 $action = $r['action'];
67                 $old_id = $r['old_id'];
68                 $data = $r['data'];
69
70                 if ( is_wp_error( $id ) ) {
71                         $data = $id;
72                         $id = 0;
73                 }
74
75                 $response = '';
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 ) ) {
80                                         continue;
81                                 }
82                                 $class = '';
83                                 if ( is_object( $error_data ) ) {
84                                         $class = ' class="' . get_class( $error_data ) . '"';
85                                         $error_data = get_object_vars( $error_data );
86                                 }
87
88                                 $response .= "<wp_error_data code='$code'$class>";
89
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>";
95                                         }
96                                 }
97
98                                 $response .= "</wp_error_data>";
99                         }
100                 } else {
101                         $response = "<response_data><![CDATA[$data]]></response_data>";
102                 }
103
104                 $s = '';
105                 if ( is_array( $r['supplemental'] ) ) {
106                         foreach ( $r['supplemental'] as $k => $v ) {
107                                 $s .= "<$k><![CDATA[$v]]></$k>";
108                         }
109                         $s = "<supplemental>$s</supplemental>";
110                 }
111
112                 if ( false === $action ) {
113                         $action = $_POST['action'];
114                 }
115                 $x = '';
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'>";
118                 $x .=           $response;
119                 $x .=           $s;
120                 $x .=   "</$what>";
121                 $x .= "</response>";
122
123                 $this->responses[] = $x;
124                 return $x;
125         }
126
127         /**
128          * Display XML formatted responses.
129          *
130          * Sets the content type header to text/xml.
131          *
132          * @since 2.1.0
133          */
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 )
138                         echo $response;
139                 echo '</wp_ajax>';
140                 if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
141                         wp_die();
142                 else
143                         die();
144         }
145 }