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