author | ymh <ymh.work@gmail.com> |
Tue, 15 Oct 2019 15:48:13 +0200 | |
changeset 13 | d255fe9cd479 |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* Send XML response back to Ajax request. |
0 | 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 |
*/ |
|
5 | 15 |
public $responses = array(); |
0 | 16 |
|
17 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* Constructor - Passes args to WP_Ajax_Response::add(). |
0 | 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 |
*/ |
|
5 | 25 |
public function __construct( $args = '' ) { |
9 | 26 |
if ( ! empty( $args ) ) { |
27 |
$this->add( $args ); |
|
28 |
} |
|
0 | 29 |
} |
30 |
||
31 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
32 |
* Appends data to an XML response based on given arguments. |
0 | 33 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
34 |
* With `$args` defaults, extra data output would be: |
0 | 35 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
* <response action='{$action}_$id'> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
37 |
* <$what id='$id' position='$position'> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
38 |
* <response_data><![CDATA[$data]]></response_data> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
* </$what> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
* </response> |
0 | 41 |
* |
42 |
* @since 2.1.0 |
|
43 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
44 |
* @param string|array $args { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
45 |
* Optional. An array or string of XML response arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
46 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
47 |
* @type string $what XML-RPC response type. Used as a child element of `<response>`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
* Default 'object' (`<object>`). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
* @type string|false $action Value to use for the `action` attribute in `<response>`. Will be |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
50 |
* appended with `_$id` on output. If false, `$action` will default to |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
51 |
* the value of `$_POST['action']`. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
52 |
* @type int|WP_Error $id The response ID, used as the response type `id` attribute. Also |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
53 |
* accepts a `WP_Error` object if the ID does not exist. Default 0. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
* @type int|false $old_id The previous response ID. Used as the value for the response type |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
* `old_id` attribute. False hides the attribute. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
* @type string $position Value of the response type `position` attribute. Accepts 1 (bottom), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
* -1 (top), html ID (after), or -html ID (before). Default 1 (bottom). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
* @type string|WP_Error $data The response content/message. Also accepts a WP_Error object if the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
* ID does not exist. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
60 |
* @type array $supplemental An array of extra strings that will be output within a `<supplemental>` |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
* element as CDATA. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
62 |
* } |
0 | 63 |
* @return string XML response. |
64 |
*/ |
|
5 | 65 |
public function add( $args = '' ) { |
0 | 66 |
$defaults = array( |
9 | 67 |
'what' => 'object', |
68 |
'action' => false, |
|
69 |
'id' => '0', |
|
70 |
'old_id' => false, |
|
71 |
'position' => 1, |
|
72 |
'data' => '', |
|
73 |
'supplemental' => array(), |
|
0 | 74 |
); |
75 |
||
76 |
$r = wp_parse_args( $args, $defaults ); |
|
77 |
||
5 | 78 |
$position = preg_replace( '/[^a-z0-9:_-]/i', '', $r['position'] ); |
9 | 79 |
$id = $r['id']; |
80 |
$what = $r['what']; |
|
81 |
$action = $r['action']; |
|
82 |
$old_id = $r['old_id']; |
|
83 |
$data = $r['data']; |
|
5 | 84 |
|
85 |
if ( is_wp_error( $id ) ) { |
|
0 | 86 |
$data = $id; |
9 | 87 |
$id = 0; |
0 | 88 |
} |
89 |
||
90 |
$response = ''; |
|
5 | 91 |
if ( is_wp_error( $data ) ) { |
0 | 92 |
foreach ( (array) $data->get_error_codes() as $code ) { |
9 | 93 |
$response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . ']]></wp_error>'; |
5 | 94 |
if ( ! $error_data = $data->get_error_data( $code ) ) { |
0 | 95 |
continue; |
5 | 96 |
} |
0 | 97 |
$class = ''; |
5 | 98 |
if ( is_object( $error_data ) ) { |
9 | 99 |
$class = ' class="' . get_class( $error_data ) . '"'; |
5 | 100 |
$error_data = get_object_vars( $error_data ); |
0 | 101 |
} |
102 |
||
103 |
$response .= "<wp_error_data code='$code'$class>"; |
|
104 |
||
5 | 105 |
if ( is_scalar( $error_data ) ) { |
0 | 106 |
$response .= "<![CDATA[$error_data]]>"; |
5 | 107 |
} elseif ( is_array( $error_data ) ) { |
108 |
foreach ( $error_data as $k => $v ) { |
|
0 | 109 |
$response .= "<$k><![CDATA[$v]]></$k>"; |
5 | 110 |
} |
0 | 111 |
} |
112 |
||
9 | 113 |
$response .= '</wp_error_data>'; |
0 | 114 |
} |
115 |
} else { |
|
116 |
$response = "<response_data><![CDATA[$data]]></response_data>"; |
|
117 |
} |
|
118 |
||
119 |
$s = ''; |
|
5 | 120 |
if ( is_array( $r['supplemental'] ) ) { |
121 |
foreach ( $r['supplemental'] as $k => $v ) { |
|
0 | 122 |
$s .= "<$k><![CDATA[$v]]></$k>"; |
5 | 123 |
} |
0 | 124 |
$s = "<supplemental>$s</supplemental>"; |
125 |
} |
|
126 |
||
5 | 127 |
if ( false === $action ) { |
0 | 128 |
$action = $_POST['action']; |
5 | 129 |
} |
9 | 130 |
$x = ''; |
0 | 131 |
$x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action |
9 | 132 |
$x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>"; |
133 |
$x .= $response; |
|
134 |
$x .= $s; |
|
135 |
$x .= "</$what>"; |
|
136 |
$x .= '</response>'; |
|
0 | 137 |
|
138 |
$this->responses[] = $x; |
|
139 |
return $x; |
|
140 |
} |
|
141 |
||
142 |
/** |
|
143 |
* Display XML formatted responses. |
|
144 |
* |
|
145 |
* Sets the content type header to text/xml. |
|
146 |
* |
|
147 |
* @since 2.1.0 |
|
148 |
*/ |
|
5 | 149 |
public function send() { |
0 | 150 |
header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) ); |
151 |
echo "<?xml version='1.0' encoding='" . get_option( 'blog_charset' ) . "' standalone='yes'?><wp_ajax>"; |
|
9 | 152 |
foreach ( (array) $this->responses as $response ) { |
0 | 153 |
echo $response; |
9 | 154 |
} |
0 | 155 |
echo '</wp_ajax>'; |
9 | 156 |
if ( wp_doing_ajax() ) { |
0 | 157 |
wp_die(); |
9 | 158 |
} else { |
0 | 159 |
die(); |
9 | 160 |
} |
0 | 161 |
} |
162 |
} |