author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
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 = '' ) { |
0 | 26 |
if ( !empty($args) ) |
27 |
$this->add($args); |
|
28 |
} |
|
29 |
||
30 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
* Appends data to an XML response based on given arguments. |
0 | 32 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
33 |
* With `$args` defaults, extra data output would be: |
0 | 34 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
35 |
* <response action='{$action}_$id'> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
* <$what id='$id' position='$position'> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
37 |
* <response_data><![CDATA[$data]]></response_data> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
38 |
* </$what> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
* </response> |
0 | 40 |
* |
41 |
* @since 2.1.0 |
|
42 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
43 |
* @param string|array $args { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
44 |
* Optional. An array or string of XML response arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
45 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
46 |
* @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
|
47 |
* Default 'object' (`<object>`). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
* @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
|
49 |
* 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
|
50 |
* the value of `$_POST['action']`. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
51 |
* @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
|
52 |
* 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
|
53 |
* @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
|
54 |
* `old_id` attribute. False hides the attribute. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
* @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
|
56 |
* -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
|
57 |
* @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
|
58 |
* ID does not exist. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
* @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
|
60 |
* element as CDATA. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
* } |
0 | 62 |
* @return string XML response. |
63 |
*/ |
|
5 | 64 |
public function add( $args = '' ) { |
0 | 65 |
$defaults = array( |
66 |
'what' => 'object', 'action' => false, |
|
67 |
'id' => '0', 'old_id' => false, |
|
68 |
'position' => 1, |
|
69 |
'data' => '', 'supplemental' => array() |
|
70 |
); |
|
71 |
||
72 |
$r = wp_parse_args( $args, $defaults ); |
|
73 |
||
5 | 74 |
$position = preg_replace( '/[^a-z0-9:_-]/i', '', $r['position'] ); |
75 |
$id = $r['id']; |
|
76 |
$what = $r['what']; |
|
77 |
$action = $r['action']; |
|
78 |
$old_id = $r['old_id']; |
|
79 |
$data = $r['data']; |
|
80 |
||
81 |
if ( is_wp_error( $id ) ) { |
|
0 | 82 |
$data = $id; |
83 |
$id = 0; |
|
84 |
} |
|
85 |
||
86 |
$response = ''; |
|
5 | 87 |
if ( is_wp_error( $data ) ) { |
0 | 88 |
foreach ( (array) $data->get_error_codes() as $code ) { |
5 | 89 |
$response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . "]]></wp_error>"; |
90 |
if ( ! $error_data = $data->get_error_data( $code ) ) { |
|
0 | 91 |
continue; |
5 | 92 |
} |
0 | 93 |
$class = ''; |
5 | 94 |
if ( is_object( $error_data ) ) { |
95 |
$class = ' class="' . get_class( $error_data ) . '"'; |
|
96 |
$error_data = get_object_vars( $error_data ); |
|
0 | 97 |
} |
98 |
||
99 |
$response .= "<wp_error_data code='$code'$class>"; |
|
100 |
||
5 | 101 |
if ( is_scalar( $error_data ) ) { |
0 | 102 |
$response .= "<![CDATA[$error_data]]>"; |
5 | 103 |
} elseif ( is_array( $error_data ) ) { |
104 |
foreach ( $error_data as $k => $v ) { |
|
0 | 105 |
$response .= "<$k><![CDATA[$v]]></$k>"; |
5 | 106 |
} |
0 | 107 |
} |
108 |
||
109 |
$response .= "</wp_error_data>"; |
|
110 |
} |
|
111 |
} else { |
|
112 |
$response = "<response_data><![CDATA[$data]]></response_data>"; |
|
113 |
} |
|
114 |
||
115 |
$s = ''; |
|
5 | 116 |
if ( is_array( $r['supplemental'] ) ) { |
117 |
foreach ( $r['supplemental'] as $k => $v ) { |
|
0 | 118 |
$s .= "<$k><![CDATA[$v]]></$k>"; |
5 | 119 |
} |
0 | 120 |
$s = "<supplemental>$s</supplemental>"; |
121 |
} |
|
122 |
||
5 | 123 |
if ( false === $action ) { |
0 | 124 |
$action = $_POST['action']; |
5 | 125 |
} |
0 | 126 |
$x = ''; |
127 |
$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'>"; |
|
129 |
$x .= $response; |
|
130 |
$x .= $s; |
|
131 |
$x .= "</$what>"; |
|
132 |
$x .= "</response>"; |
|
133 |
||
134 |
$this->responses[] = $x; |
|
135 |
return $x; |
|
136 |
} |
|
137 |
||
138 |
/** |
|
139 |
* Display XML formatted responses. |
|
140 |
* |
|
141 |
* Sets the content type header to text/xml. |
|
142 |
* |
|
143 |
* @since 2.1.0 |
|
144 |
*/ |
|
5 | 145 |
public function send() { |
0 | 146 |
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>"; |
|
148 |
foreach ( (array) $this->responses as $response ) |
|
149 |
echo $response; |
|
150 |
echo '</wp_ajax>'; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
151 |
if ( wp_doing_ajax() ) |
0 | 152 |
wp_die(); |
153 |
else |
|
154 |
die(); |
|
155 |
} |
|
156 |
} |