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