]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-wp-ajax-response.php
Wordpress 3.1
[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          * @access private
15          */
16         var $responses = array();
17
18         /**
19          * PHP4 Constructor - Passes args to {@link WP_Ajax_Response::add()}.
20          *
21          * @since 2.1.0
22          * @see WP_Ajax_Response::add()
23          *
24          * @param string|array $args Optional. Will be passed to add() method.
25          * @return WP_Ajax_Response
26          */
27         function WP_Ajax_Response( $args = '' ) {
28                 if ( !empty($args) )
29                         $this->add($args);
30         }
31
32         /**
33          * Append to XML response based on given arguments.
34          *
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.
39          *
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.
49          *
50          * @since 2.1.0
51          *
52          * @param string|array $args Override defaults.
53          * @return string XML response.
54          */
55         function add( $args = '' ) {
56                 $defaults = array(
57                         'what' => 'object', 'action' => false,
58                         'id' => '0', 'old_id' => false,
59                         'position' => 1,
60                         'data' => '', 'supplemental' => array()
61                 );
62
63                 $r = wp_parse_args( $args, $defaults );
64                 extract( $r, EXTR_SKIP );
65                 $position = preg_replace( '/[^a-z0-9:_-]/i', '', $position );
66
67                 if ( is_wp_error($id) ) {
68                         $data = $id;
69                         $id = 0;
70                 }
71
72                 $response = '';
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) )
77                                         continue;
78                                 $class = '';
79                                 if ( is_object($error_data) ) {
80                                         $class = ' class="' . get_class($error_data) . '"';
81                                         $error_data = get_object_vars($error_data);
82                                 }
83
84                                 $response .= "<wp_error_data code='$code'$class>";
85
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>";
91                                 }
92
93                                 $response .= "</wp_error_data>";
94                         }
95                 } else {
96                         $response = "<response_data><![CDATA[$data]]></response_data>";
97                 }
98
99                 $s = '';
100                 if ( is_array($supplemental) ) {
101                         foreach ( $supplemental as $k => $v )
102                                 $s .= "<$k><![CDATA[$v]]></$k>";
103                         $s = "<supplemental>$s</supplemental>";
104                 }
105
106                 if ( false === $action )
107                         $action = $_POST['action'];
108
109                 $x = '';
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'>";
112                 $x .=           $response;
113                 $x .=           $s;
114                 $x .=   "</$what>";
115                 $x .= "</response>";
116
117                 $this->responses[] = $x;
118                 return $x;
119         }
120
121         /**
122          * Display XML formatted responses.
123          *
124          * Sets the content type header to text/xml.
125          *
126          * @since 2.1.0
127          */
128         function send() {
129                 header('Content-Type: text/xml');
130                 echo "<?xml version='1.0' standalone='yes'?><wp_ajax>";
131                 foreach ( (array) $this->responses as $response )
132                         echo $response;
133                 echo '</wp_ajax>';
134                 die();
135         }
136 }
137
138 ?>