21 * @see WP_Ajax_Response::add() |
21 * @see WP_Ajax_Response::add() |
22 * |
22 * |
23 * @param string|array $args Optional. Will be passed to add() method. |
23 * @param string|array $args Optional. Will be passed to add() method. |
24 */ |
24 */ |
25 public function __construct( $args = '' ) { |
25 public function __construct( $args = '' ) { |
26 if ( !empty($args) ) |
26 if ( ! empty( $args ) ) { |
27 $this->add($args); |
27 $this->add( $args ); |
|
28 } |
28 } |
29 } |
29 |
30 |
30 /** |
31 /** |
31 * Appends data to an XML response based on given arguments. |
32 * Appends data to an XML response based on given arguments. |
32 * |
33 * |
61 * } |
62 * } |
62 * @return string XML response. |
63 * @return string XML response. |
63 */ |
64 */ |
64 public function add( $args = '' ) { |
65 public function add( $args = '' ) { |
65 $defaults = array( |
66 $defaults = array( |
66 'what' => 'object', 'action' => false, |
67 'what' => 'object', |
67 'id' => '0', 'old_id' => false, |
68 'action' => false, |
68 'position' => 1, |
69 'id' => '0', |
69 'data' => '', 'supplemental' => array() |
70 'old_id' => false, |
|
71 'position' => 1, |
|
72 'data' => '', |
|
73 'supplemental' => array(), |
70 ); |
74 ); |
71 |
75 |
72 $r = wp_parse_args( $args, $defaults ); |
76 $r = wp_parse_args( $args, $defaults ); |
73 |
77 |
74 $position = preg_replace( '/[^a-z0-9:_-]/i', '', $r['position'] ); |
78 $position = preg_replace( '/[^a-z0-9:_-]/i', '', $r['position'] ); |
75 $id = $r['id']; |
79 $id = $r['id']; |
76 $what = $r['what']; |
80 $what = $r['what']; |
77 $action = $r['action']; |
81 $action = $r['action']; |
78 $old_id = $r['old_id']; |
82 $old_id = $r['old_id']; |
79 $data = $r['data']; |
83 $data = $r['data']; |
80 |
84 |
81 if ( is_wp_error( $id ) ) { |
85 if ( is_wp_error( $id ) ) { |
82 $data = $id; |
86 $data = $id; |
83 $id = 0; |
87 $id = 0; |
84 } |
88 } |
85 |
89 |
86 $response = ''; |
90 $response = ''; |
87 if ( is_wp_error( $data ) ) { |
91 if ( is_wp_error( $data ) ) { |
88 foreach ( (array) $data->get_error_codes() as $code ) { |
92 foreach ( (array) $data->get_error_codes() as $code ) { |
89 $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . "]]></wp_error>"; |
93 $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . ']]></wp_error>'; |
90 if ( ! $error_data = $data->get_error_data( $code ) ) { |
94 if ( ! $error_data = $data->get_error_data( $code ) ) { |
91 continue; |
95 continue; |
92 } |
96 } |
93 $class = ''; |
97 $class = ''; |
94 if ( is_object( $error_data ) ) { |
98 if ( is_object( $error_data ) ) { |
95 $class = ' class="' . get_class( $error_data ) . '"'; |
99 $class = ' class="' . get_class( $error_data ) . '"'; |
96 $error_data = get_object_vars( $error_data ); |
100 $error_data = get_object_vars( $error_data ); |
97 } |
101 } |
98 |
102 |
99 $response .= "<wp_error_data code='$code'$class>"; |
103 $response .= "<wp_error_data code='$code'$class>"; |
100 |
104 |
121 } |
125 } |
122 |
126 |
123 if ( false === $action ) { |
127 if ( false === $action ) { |
124 $action = $_POST['action']; |
128 $action = $_POST['action']; |
125 } |
129 } |
126 $x = ''; |
130 $x = ''; |
127 $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action |
131 $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action |
128 $x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>"; |
132 $x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>"; |
129 $x .= $response; |
133 $x .= $response; |
130 $x .= $s; |
134 $x .= $s; |
131 $x .= "</$what>"; |
135 $x .= "</$what>"; |
132 $x .= "</response>"; |
136 $x .= '</response>'; |
133 |
137 |
134 $this->responses[] = $x; |
138 $this->responses[] = $x; |
135 return $x; |
139 return $x; |
136 } |
140 } |
137 |
141 |
143 * @since 2.1.0 |
147 * @since 2.1.0 |
144 */ |
148 */ |
145 public function send() { |
149 public function send() { |
146 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) ); |
150 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) ); |
147 echo "<?xml version='1.0' encoding='" . get_option( 'blog_charset' ) . "' standalone='yes'?><wp_ajax>"; |
151 echo "<?xml version='1.0' encoding='" . get_option( 'blog_charset' ) . "' standalone='yes'?><wp_ajax>"; |
148 foreach ( (array) $this->responses as $response ) |
152 foreach ( (array) $this->responses as $response ) { |
149 echo $response; |
153 echo $response; |
|
154 } |
150 echo '</wp_ajax>'; |
155 echo '</wp_ajax>'; |
151 if ( wp_doing_ajax() ) |
156 if ( wp_doing_ajax() ) { |
152 wp_die(); |
157 wp_die(); |
153 else |
158 } else { |
154 die(); |
159 die(); |
|
160 } |
155 } |
161 } |
156 } |
162 } |