author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
<?php |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
* REST API functions. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
4 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
5 |
* @package WordPress |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
6 |
* @subpackage REST_API |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
7 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
8 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
9 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
10 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
11 |
* Version number for our API. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
12 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
* @var string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
15 |
define( 'REST_API_VERSION', '2.0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
16 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
17 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
18 |
* Registers a REST API route. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
19 |
* |
9 | 20 |
* Note: Do not use before the {@see 'rest_api_init'} hook. |
21 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
22 |
* @since 4.4.0 |
16 | 23 |
* @since 5.1.0 Added a `_doing_it_wrong()` notice when not called on or after the `rest_api_init` hook. |
24 |
* @since 5.5.0 Added a `_doing_it_wrong()` notice when the required `permission_callback` argument is not set. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
25 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
26 |
* @param string $route_namespace The first URL segment after core prefix. Should be unique to your package/plugin. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
27 |
* @param string $route The base URL for route you are adding. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
28 |
* @param array $args Optional. Either an array of options for the endpoint, or an array of arrays for |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
29 |
* multiple methods. Default empty array. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
30 |
* @param bool $override Optional. If the route already exists, should we override it? True overrides, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
31 |
* false merges (with newer overriding if duplicate keys exist). Default false. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
32 |
* @return bool True on success, false on error. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
33 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
34 |
function register_rest_route( $route_namespace, $route, $args = array(), $override = false ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
35 |
if ( empty( $route_namespace ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
36 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
37 |
* Non-namespaced routes are not allowed, with the exception of the main |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
38 |
* and namespace indexes. If you really need to register a |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
39 |
* non-namespaced route, call `WP_REST_Server::register_route` directly. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
40 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
41 |
_doing_it_wrong( 'register_rest_route', __( 'Routes must be namespaced with plugin or theme name and version.' ), '4.4.0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
42 |
return false; |
9 | 43 |
} elseif ( empty( $route ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
44 |
_doing_it_wrong( 'register_rest_route', __( 'Route must be specified.' ), '4.4.0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
45 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
46 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
47 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
48 |
$clean_namespace = trim( $route_namespace, '/' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
49 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
50 |
if ( $clean_namespace !== $route_namespace ) { |
16 | 51 |
_doing_it_wrong( __FUNCTION__, __( 'Namespace must not start or end with a slash.' ), '5.4.2' ); |
52 |
} |
|
53 |
||
9 | 54 |
if ( ! did_action( 'rest_api_init' ) ) { |
55 |
_doing_it_wrong( |
|
56 |
'register_rest_route', |
|
57 |
sprintf( |
|
58 |
/* translators: %s: rest_api_init */ |
|
59 |
__( 'REST API routes must be registered on the %s action.' ), |
|
60 |
'<code>rest_api_init</code>' |
|
61 |
), |
|
62 |
'5.1.0' |
|
63 |
); |
|
64 |
} |
|
65 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
66 |
if ( isset( $args['args'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
67 |
$common_args = $args['args']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
68 |
unset( $args['args'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
69 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
70 |
$common_args = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
71 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
72 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
73 |
if ( isset( $args['callback'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
74 |
// Upgrade a single set to multiple. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
75 |
$args = array( $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
76 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
77 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
78 |
$defaults = array( |
9 | 79 |
'methods' => 'GET', |
80 |
'callback' => null, |
|
81 |
'args' => array(), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
82 |
); |
16 | 83 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
84 |
foreach ( $args as $key => &$arg_group ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
85 |
if ( ! is_numeric( $key ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
86 |
// Route option, skip here. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
87 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
88 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
89 |
|
9 | 90 |
$arg_group = array_merge( $defaults, $arg_group ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
91 |
$arg_group['args'] = array_merge( $common_args, $arg_group['args'] ); |
16 | 92 |
|
93 |
if ( ! isset( $arg_group['permission_callback'] ) ) { |
|
94 |
_doing_it_wrong( |
|
95 |
__FUNCTION__, |
|
96 |
sprintf( |
|
18 | 97 |
/* translators: 1: The REST API route being registered, 2: The argument name, 3: The suggested function name. */ |
16 | 98 |
__( 'The REST API route definition for %1$s is missing the required %2$s argument. For REST API routes that are intended to be public, use %3$s as the permission callback.' ), |
99 |
'<code>' . $clean_namespace . '/' . trim( $route, '/' ) . '</code>', |
|
100 |
'<code>permission_callback</code>', |
|
101 |
'<code>__return_true</code>' |
|
102 |
), |
|
103 |
'5.5.0' |
|
104 |
); |
|
105 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
106 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
107 |
foreach ( $arg_group['args'] as $arg ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
108 |
if ( ! is_array( $arg ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
109 |
_doing_it_wrong( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
110 |
__FUNCTION__, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
111 |
sprintf( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
112 |
/* translators: 1: $args, 2: The REST API route being registered. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
113 |
__( 'REST API %1$s should be an array of arrays. Non-array value detected for %2$s.' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
114 |
'<code>$args</code>', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
115 |
'<code>' . $clean_namespace . '/' . trim( $route, '/' ) . '</code>' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
116 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
117 |
'6.1.0' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
118 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
119 |
break; // Leave the foreach loop once a non-array argument was found. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
120 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
121 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
122 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
123 |
|
16 | 124 |
$full_route = '/' . $clean_namespace . '/' . trim( $route, '/' ); |
125 |
rest_get_server()->register_route( $clean_namespace, $full_route, $args, $override ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
126 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
127 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
128 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
129 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
130 |
* Registers a new field on an existing WordPress object type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
131 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
132 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
133 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
134 |
* @global array $wp_rest_additional_fields Holds registered fields, organized |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
135 |
* by object type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
136 |
* |
19 | 137 |
* @param string|array $object_type Object(s) the field is being registered to, |
138 |
* "post"|"term"|"comment" etc. |
|
16 | 139 |
* @param string $attribute The attribute name. |
140 |
* @param array $args { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
141 |
* Optional. An array of arguments used to handle the registered field. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
142 |
* |
16 | 143 |
* @type callable|null $get_callback Optional. The callback function used to retrieve the field value. Default is |
144 |
* 'null', the field will not be returned in the response. The function will |
|
145 |
* be passed the prepared object data. |
|
146 |
* @type callable|null $update_callback Optional. The callback function used to set and update the field value. Default |
|
147 |
* is 'null', the value cannot be set or updated. The function will be passed |
|
148 |
* the model object, like WP_Post. |
|
18 | 149 |
* @type array|null $schema Optional. The schema for this field. |
16 | 150 |
* Default is 'null', no schema entry will be returned. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
151 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
152 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
153 |
function register_rest_field( $object_type, $attribute, $args = array() ) { |
19 | 154 |
global $wp_rest_additional_fields; |
155 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
156 |
$defaults = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
157 |
'get_callback' => null, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
158 |
'update_callback' => null, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
159 |
'schema' => null, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
160 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
161 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
162 |
$args = wp_parse_args( $args, $defaults ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
163 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
164 |
$object_types = (array) $object_type; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
165 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
166 |
foreach ( $object_types as $object_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
167 |
$wp_rest_additional_fields[ $object_type ][ $attribute ] = $args; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
168 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
169 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
170 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
171 |
/** |
18 | 172 |
* Registers rewrite rules for the REST API. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
173 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
174 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
175 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
176 |
* @see rest_api_register_rewrites() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
177 |
* @global WP $wp Current WordPress environment instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
178 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
179 |
function rest_api_init() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
180 |
rest_api_register_rewrites(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
181 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
182 |
global $wp; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
183 |
$wp->add_query_var( 'rest_route' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
184 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
185 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
186 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
187 |
* Adds REST rewrite rules. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
188 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
189 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
190 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
191 |
* @see add_rewrite_rule() |
16 | 192 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
193 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
194 |
function rest_api_register_rewrites() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
195 |
global $wp_rewrite; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
196 |
|
9 | 197 |
add_rewrite_rule( '^' . rest_get_url_prefix() . '/?$', 'index.php?rest_route=/', 'top' ); |
198 |
add_rewrite_rule( '^' . rest_get_url_prefix() . '/(.*)?', 'index.php?rest_route=/$matches[1]', 'top' ); |
|
199 |
add_rewrite_rule( '^' . $wp_rewrite->index . '/' . rest_get_url_prefix() . '/?$', 'index.php?rest_route=/', 'top' ); |
|
200 |
add_rewrite_rule( '^' . $wp_rewrite->index . '/' . rest_get_url_prefix() . '/(.*)?', 'index.php?rest_route=/$matches[1]', 'top' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
201 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
202 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
203 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
204 |
* Registers the default REST API filters. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
205 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
206 |
* Attached to the {@see 'rest_api_init'} action |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
207 |
* to make testing and disabling these filters easier. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
208 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
209 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
210 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
211 |
function rest_api_default_filters() { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
212 |
if ( wp_is_serving_rest_request() ) { |
16 | 213 |
// Deprecated reporting. |
214 |
add_action( 'deprecated_function_run', 'rest_handle_deprecated_function', 10, 3 ); |
|
215 |
add_filter( 'deprecated_function_trigger_error', '__return_false' ); |
|
216 |
add_action( 'deprecated_argument_run', 'rest_handle_deprecated_argument', 10, 3 ); |
|
217 |
add_filter( 'deprecated_argument_trigger_error', '__return_false' ); |
|
218 |
add_action( 'doing_it_wrong_run', 'rest_handle_doing_it_wrong', 10, 3 ); |
|
219 |
add_filter( 'doing_it_wrong_trigger_error', '__return_false' ); |
|
220 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
221 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
222 |
// Default serving. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
223 |
add_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
224 |
add_filter( 'rest_post_dispatch', 'rest_send_allow_header', 10, 3 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
225 |
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
226 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
227 |
add_filter( 'rest_pre_dispatch', 'rest_handle_options_request', 10, 3 ); |
18 | 228 |
add_filter( 'rest_index', 'rest_add_application_passwords_to_index' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
229 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
230 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
231 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
232 |
* Registers default REST API routes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
233 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
234 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
235 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
236 |
function create_initial_rest_routes() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
237 |
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) { |
16 | 238 |
$controller = $post_type->get_rest_controller(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
239 |
|
16 | 240 |
if ( ! $controller ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
241 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
242 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
243 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
244 |
if ( ! $post_type->late_route_registration ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
245 |
$controller->register_routes(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
246 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
247 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
248 |
$revisions_controller = $post_type->get_revisions_rest_controller(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
249 |
if ( $revisions_controller ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
250 |
$revisions_controller->register_routes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
251 |
} |
9 | 252 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
253 |
$autosaves_controller = $post_type->get_autosave_rest_controller(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
254 |
if ( $autosaves_controller ) { |
9 | 255 |
$autosaves_controller->register_routes(); |
256 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
257 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
258 |
if ( $post_type->late_route_registration ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
259 |
$controller->register_routes(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
260 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
261 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
262 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
263 |
// Post types. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
264 |
$controller = new WP_REST_Post_Types_Controller(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
265 |
$controller->register_routes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
266 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
267 |
// Post statuses. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
268 |
$controller = new WP_REST_Post_Statuses_Controller(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
269 |
$controller->register_routes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
270 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
271 |
// Taxonomies. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
272 |
$controller = new WP_REST_Taxonomies_Controller(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
273 |
$controller->register_routes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
274 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
275 |
// Terms. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
276 |
foreach ( get_taxonomies( array( 'show_in_rest' => true ), 'object' ) as $taxonomy ) { |
16 | 277 |
$controller = $taxonomy->get_rest_controller(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
278 |
|
16 | 279 |
if ( ! $controller ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
280 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
281 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
282 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
283 |
$controller->register_routes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
284 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
285 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
286 |
// Users. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
287 |
$controller = new WP_REST_Users_Controller(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
288 |
$controller->register_routes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
289 |
|
18 | 290 |
// Application Passwords |
291 |
$controller = new WP_REST_Application_Passwords_Controller(); |
|
292 |
$controller->register_routes(); |
|
293 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
294 |
// Comments. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
295 |
$controller = new WP_REST_Comments_Controller(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
296 |
$controller->register_routes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
297 |
|
18 | 298 |
$search_handlers = array( |
299 |
new WP_REST_Post_Search_Handler(), |
|
300 |
new WP_REST_Term_Search_Handler(), |
|
301 |
new WP_REST_Post_Format_Search_Handler(), |
|
302 |
); |
|
303 |
||
9 | 304 |
/** |
305 |
* Filters the search handlers to use in the REST search controller. |
|
306 |
* |
|
307 |
* @since 5.0.0 |
|
308 |
* |
|
309 |
* @param array $search_handlers List of search handlers to use in the controller. Each search |
|
310 |
* handler instance must extend the `WP_REST_Search_Handler` class. |
|
311 |
* Default is only a handler for posts. |
|
312 |
*/ |
|
18 | 313 |
$search_handlers = apply_filters( 'wp_rest_search_handlers', $search_handlers ); |
9 | 314 |
|
315 |
$controller = new WP_REST_Search_Controller( $search_handlers ); |
|
316 |
$controller->register_routes(); |
|
317 |
||
318 |
// Block Renderer. |
|
19 | 319 |
$controller = new WP_REST_Block_Renderer_Controller(); |
9 | 320 |
$controller->register_routes(); |
321 |
||
16 | 322 |
// Block Types. |
323 |
$controller = new WP_REST_Block_Types_Controller(); |
|
324 |
$controller->register_routes(); |
|
325 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
326 |
// Settings. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
327 |
$controller = new WP_REST_Settings_Controller(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
328 |
$controller->register_routes(); |
9 | 329 |
|
330 |
// Themes. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
331 |
$controller = new WP_REST_Themes_Controller(); |
9 | 332 |
$controller->register_routes(); |
333 |
||
16 | 334 |
// Plugins. |
335 |
$controller = new WP_REST_Plugins_Controller(); |
|
336 |
$controller->register_routes(); |
|
337 |
||
18 | 338 |
// Sidebars. |
339 |
$controller = new WP_REST_Sidebars_Controller(); |
|
340 |
$controller->register_routes(); |
|
341 |
||
342 |
// Widget Types. |
|
343 |
$controller = new WP_REST_Widget_Types_Controller(); |
|
344 |
$controller->register_routes(); |
|
345 |
||
346 |
// Widgets. |
|
347 |
$controller = new WP_REST_Widgets_Controller(); |
|
348 |
$controller->register_routes(); |
|
349 |
||
16 | 350 |
// Block Directory. |
351 |
$controller = new WP_REST_Block_Directory_Controller(); |
|
352 |
$controller->register_routes(); |
|
353 |
||
18 | 354 |
// Pattern Directory. |
355 |
$controller = new WP_REST_Pattern_Directory_Controller(); |
|
356 |
$controller->register_routes(); |
|
357 |
||
19 | 358 |
// Block Patterns. |
359 |
$controller = new WP_REST_Block_Patterns_Controller(); |
|
360 |
$controller->register_routes(); |
|
361 |
||
362 |
// Block Pattern Categories. |
|
363 |
$controller = new WP_REST_Block_Pattern_Categories_Controller(); |
|
364 |
$controller->register_routes(); |
|
365 |
||
18 | 366 |
// Site Health. |
367 |
$site_health = WP_Site_Health::get_instance(); |
|
368 |
$controller = new WP_REST_Site_Health_Controller( $site_health ); |
|
369 |
$controller->register_routes(); |
|
19 | 370 |
|
371 |
// URL Details. |
|
372 |
$controller = new WP_REST_URL_Details_Controller(); |
|
373 |
$controller->register_routes(); |
|
374 |
||
375 |
// Menu Locations. |
|
376 |
$controller = new WP_REST_Menu_Locations_Controller(); |
|
377 |
$controller->register_routes(); |
|
378 |
||
379 |
// Site Editor Export. |
|
380 |
$controller = new WP_REST_Edit_Site_Export_Controller(); |
|
381 |
$controller->register_routes(); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
382 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
383 |
// Navigation Fallback. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
384 |
$controller = new WP_REST_Navigation_Fallback_Controller(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
385 |
$controller->register_routes(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
386 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
387 |
// Font Collections. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
388 |
$font_collections_controller = new WP_REST_Font_Collections_Controller(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
389 |
$font_collections_controller->register_routes(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
390 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
391 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
392 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
393 |
* Loads the REST API. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
394 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
395 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
396 |
* |
16 | 397 |
* @global WP $wp Current WordPress environment instance. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
398 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
399 |
function rest_api_loaded() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
400 |
if ( empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
401 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
402 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
403 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
404 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
405 |
* Whether this is a REST Request. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
406 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
407 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
408 |
* @var bool |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
409 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
410 |
define( 'REST_REQUEST', true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
411 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
412 |
// Initialize the server. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
413 |
$server = rest_get_server(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
414 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
415 |
// Fire off the request. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
416 |
$route = untrailingslashit( $GLOBALS['wp']->query_vars['rest_route'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
417 |
if ( empty( $route ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
418 |
$route = '/'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
419 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
420 |
$server->serve_request( $route ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
421 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
422 |
// We're done. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
423 |
die(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
424 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
425 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
426 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
427 |
* Retrieves the URL prefix for any API resource. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
428 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
429 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
430 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
431 |
* @return string Prefix. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
432 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
433 |
function rest_get_url_prefix() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
434 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
435 |
* Filters the REST URL prefix. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
436 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
437 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
438 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
439 |
* @param string $prefix URL prefix. Default 'wp-json'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
440 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
441 |
return apply_filters( 'rest_url_prefix', 'wp-json' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
442 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
443 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
444 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
445 |
* Retrieves the URL to a REST endpoint on a site. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
446 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
447 |
* Note: The returned URL is NOT escaped. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
448 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
449 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
450 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
451 |
* @todo Check if this is even necessary |
16 | 452 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
453 |
* |
18 | 454 |
* @param int|null $blog_id Optional. Blog ID. Default of null returns URL for current blog. |
455 |
* @param string $path Optional. REST route. Default '/'. |
|
456 |
* @param string $scheme Optional. Sanitization scheme. Default 'rest'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
457 |
* @return string Full URL to the endpoint. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
458 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
459 |
function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
460 |
if ( empty( $path ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
461 |
$path = '/'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
462 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
463 |
|
9 | 464 |
$path = '/' . ltrim( $path, '/' ); |
465 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
466 |
if ( is_multisite() && get_blog_option( $blog_id, 'permalink_structure' ) || get_option( 'permalink_structure' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
467 |
global $wp_rewrite; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
468 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
469 |
if ( $wp_rewrite->using_index_permalinks() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
470 |
$url = get_home_url( $blog_id, $wp_rewrite->index . '/' . rest_get_url_prefix(), $scheme ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
471 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
472 |
$url = get_home_url( $blog_id, rest_get_url_prefix(), $scheme ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
473 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
474 |
|
9 | 475 |
$url .= $path; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
476 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
477 |
$url = trailingslashit( get_home_url( $blog_id, '', $scheme ) ); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
478 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
479 |
* nginx only allows HTTP/1.0 methods when redirecting from / to /index.php. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
480 |
* To work around this, we manually add index.php to the URL, avoiding the redirect. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
481 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
482 |
if ( ! str_ends_with( $url, 'index.php' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
483 |
$url .= 'index.php'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
484 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
485 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
486 |
$url = add_query_arg( 'rest_route', $path, $url ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
487 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
488 |
|
16 | 489 |
if ( is_ssl() && isset( $_SERVER['SERVER_NAME'] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
490 |
// If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS. |
16 | 491 |
if ( parse_url( get_home_url( $blog_id ), PHP_URL_HOST ) === $_SERVER['SERVER_NAME'] ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
492 |
$url = set_url_scheme( $url, 'https' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
493 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
494 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
495 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
496 |
if ( is_admin() && force_ssl_admin() ) { |
16 | 497 |
/* |
498 |
* In this situation the home URL may be http:, and `is_ssl()` may be false, |
|
499 |
* but the admin is served over https: (one way or another), so REST API usage |
|
500 |
* will be blocked by browsers unless it is also served over HTTPS. |
|
501 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
502 |
$url = set_url_scheme( $url, 'https' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
503 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
504 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
505 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
506 |
* Filters the REST URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
507 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
508 |
* Use this filter to adjust the url returned by the get_rest_url() function. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
509 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
510 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
511 |
* |
18 | 512 |
* @param string $url REST URL. |
513 |
* @param string $path REST route. |
|
514 |
* @param int|null $blog_id Blog ID. |
|
515 |
* @param string $scheme Sanitization scheme. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
516 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
517 |
return apply_filters( 'rest_url', $url, $path, $blog_id, $scheme ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
518 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
519 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
520 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
521 |
* Retrieves the URL to a REST endpoint. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
522 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
523 |
* Note: The returned URL is NOT escaped. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
524 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
525 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
526 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
527 |
* @param string $path Optional. REST route. Default empty. |
16 | 528 |
* @param string $scheme Optional. Sanitization scheme. Default 'rest'. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
529 |
* @return string Full URL to the endpoint. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
530 |
*/ |
16 | 531 |
function rest_url( $path = '', $scheme = 'rest' ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
532 |
return get_rest_url( null, $path, $scheme ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
533 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
534 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
535 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
536 |
* Do a REST request. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
537 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
538 |
* Used primarily to route internal requests through WP_REST_Server. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
539 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
540 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
541 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
542 |
* @param WP_REST_Request|string $request Request. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
543 |
* @return WP_REST_Response REST response. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
544 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
545 |
function rest_do_request( $request ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
546 |
$request = rest_ensure_request( $request ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
547 |
return rest_get_server()->dispatch( $request ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
548 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
549 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
550 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
551 |
* Retrieves the current REST server instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
552 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
553 |
* Instantiates a new instance if none exists already. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
554 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
555 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
556 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
557 |
* @global WP_REST_Server $wp_rest_server REST server instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
558 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
559 |
* @return WP_REST_Server REST server instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
560 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
561 |
function rest_get_server() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
562 |
/* @var WP_REST_Server $wp_rest_server */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
563 |
global $wp_rest_server; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
564 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
565 |
if ( empty( $wp_rest_server ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
566 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
567 |
* Filters the REST Server Class. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
568 |
* |
18 | 569 |
* This filter allows you to adjust the server class used by the REST API, using a |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
570 |
* different class to handle requests. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
571 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
572 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
573 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
574 |
* @param string $class_name The name of the server class. Default 'WP_REST_Server'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
575 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
576 |
$wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' ); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
577 |
$wp_rest_server = new $wp_rest_server_class(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
578 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
579 |
/** |
18 | 580 |
* Fires when preparing to serve a REST API request. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
581 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
582 |
* Endpoint objects should be created and register their hooks on this action rather |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
583 |
* than another action to ensure they're only loaded when needed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
584 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
585 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
586 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
587 |
* @param WP_REST_Server $wp_rest_server Server object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
588 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
589 |
do_action( 'rest_api_init', $wp_rest_server ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
590 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
591 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
592 |
return $wp_rest_server; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
593 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
594 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
595 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
596 |
* Ensures request arguments are a request object (for consistency). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
597 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
598 |
* @since 4.4.0 |
16 | 599 |
* @since 5.3.0 Accept string argument for the request path. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
600 |
* |
16 | 601 |
* @param array|string|WP_REST_Request $request Request to check. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
602 |
* @return WP_REST_Request REST request instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
603 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
604 |
function rest_ensure_request( $request ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
605 |
if ( $request instanceof WP_REST_Request ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
606 |
return $request; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
607 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
608 |
|
16 | 609 |
if ( is_string( $request ) ) { |
610 |
return new WP_REST_Request( 'GET', $request ); |
|
611 |
} |
|
612 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
613 |
return new WP_REST_Request( 'GET', '', $request ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
614 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
615 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
616 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
617 |
* Ensures a REST response is a response object (for consistency). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
618 |
* |
16 | 619 |
* This implements WP_REST_Response, allowing usage of `set_status`/`header`/etc |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
620 |
* without needing to double-check the object. Will also allow WP_Error to indicate error |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
621 |
* responses, so users should immediately check for this value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
622 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
623 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
624 |
* |
16 | 625 |
* @param WP_REST_Response|WP_Error|WP_HTTP_Response|mixed $response Response to check. |
626 |
* @return WP_REST_Response|WP_Error If response generated an error, WP_Error, if response |
|
627 |
* is already an instance, WP_REST_Response, otherwise |
|
628 |
* returns a new WP_REST_Response instance. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
629 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
630 |
function rest_ensure_response( $response ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
631 |
if ( is_wp_error( $response ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
632 |
return $response; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
633 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
634 |
|
16 | 635 |
if ( $response instanceof WP_REST_Response ) { |
636 |
return $response; |
|
637 |
} |
|
638 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
639 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
640 |
* While WP_HTTP_Response is the base class of WP_REST_Response, it doesn't provide |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
641 |
* all the required methods used in WP_REST_Server::dispatch(). |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
642 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
643 |
if ( $response instanceof WP_HTTP_Response ) { |
16 | 644 |
return new WP_REST_Response( |
645 |
$response->get_data(), |
|
646 |
$response->get_status(), |
|
647 |
$response->get_headers() |
|
648 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
649 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
650 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
651 |
return new WP_REST_Response( $response ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
652 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
653 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
654 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
655 |
* Handles _deprecated_function() errors. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
656 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
657 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
658 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
659 |
* @param string $function_name The function that was called. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
660 |
* @param string $replacement The function that should have been called. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
661 |
* @param string $version Version. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
662 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
663 |
function rest_handle_deprecated_function( $function_name, $replacement, $version ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
664 |
if ( ! WP_DEBUG || headers_sent() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
665 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
666 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
667 |
if ( ! empty( $replacement ) ) { |
16 | 668 |
/* translators: 1: Function name, 2: WordPress version number, 3: New function name. */ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
669 |
$string = sprintf( __( '%1$s (since %2$s; use %3$s instead)' ), $function_name, $version, $replacement ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
670 |
} else { |
16 | 671 |
/* translators: 1: Function name, 2: WordPress version number. */ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
672 |
$string = sprintf( __( '%1$s (since %2$s; no alternative available)' ), $function_name, $version ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
673 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
674 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
675 |
header( sprintf( 'X-WP-DeprecatedFunction: %s', $string ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
676 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
677 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
678 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
679 |
* Handles _deprecated_argument() errors. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
680 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
681 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
682 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
683 |
* @param string $function_name The function that was called. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
684 |
* @param string $message A message regarding the change. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
685 |
* @param string $version Version. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
686 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
687 |
function rest_handle_deprecated_argument( $function_name, $message, $version ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
688 |
if ( ! WP_DEBUG || headers_sent() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
689 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
690 |
} |
16 | 691 |
if ( $message ) { |
692 |
/* translators: 1: Function name, 2: WordPress version number, 3: Error message. */ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
693 |
$string = sprintf( __( '%1$s (since %2$s; %3$s)' ), $function_name, $version, $message ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
694 |
} else { |
16 | 695 |
/* translators: 1: Function name, 2: WordPress version number. */ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
696 |
$string = sprintf( __( '%1$s (since %2$s; no alternative available)' ), $function_name, $version ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
697 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
698 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
699 |
header( sprintf( 'X-WP-DeprecatedParam: %s', $string ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
700 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
701 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
702 |
/** |
16 | 703 |
* Handles _doing_it_wrong errors. |
704 |
* |
|
705 |
* @since 5.5.0 |
|
706 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
707 |
* @param string $function_name The function that was called. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
708 |
* @param string $message A message explaining what has been done incorrectly. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
709 |
* @param string|null $version The version of WordPress where the message was added. |
16 | 710 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
711 |
function rest_handle_doing_it_wrong( $function_name, $message, $version ) { |
16 | 712 |
if ( ! WP_DEBUG || headers_sent() ) { |
713 |
return; |
|
714 |
} |
|
715 |
||
716 |
if ( $version ) { |
|
717 |
/* translators: Developer debugging message. 1: PHP function name, 2: WordPress version number, 3: Explanatory message. */ |
|
718 |
$string = __( '%1$s (since %2$s; %3$s)' ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
719 |
$string = sprintf( $string, $function_name, $version, $message ); |
16 | 720 |
} else { |
721 |
/* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message. */ |
|
722 |
$string = __( '%1$s (%2$s)' ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
723 |
$string = sprintf( $string, $function_name, $message ); |
16 | 724 |
} |
725 |
||
726 |
header( sprintf( 'X-WP-DoingItWrong: %s', $string ) ); |
|
727 |
} |
|
728 |
||
729 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
730 |
* Sends Cross-Origin Resource Sharing headers with API requests. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
731 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
732 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
733 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
734 |
* @param mixed $value Response data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
735 |
* @return mixed Response data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
736 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
737 |
function rest_send_cors_headers( $value ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
738 |
$origin = get_http_origin(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
739 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
740 |
if ( $origin ) { |
16 | 741 |
// Requests from file:// and data: URLs send "Origin: null". |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
742 |
if ( 'null' !== $origin ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
743 |
$origin = sanitize_url( $origin ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
744 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
745 |
header( 'Access-Control-Allow-Origin: ' . $origin ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
746 |
header( 'Access-Control-Allow-Methods: OPTIONS, GET, POST, PUT, PATCH, DELETE' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
747 |
header( 'Access-Control-Allow-Credentials: true' ); |
13 | 748 |
header( 'Vary: Origin', false ); |
749 |
} elseif ( ! headers_sent() && 'GET' === $_SERVER['REQUEST_METHOD'] && ! is_user_logged_in() ) { |
|
16 | 750 |
header( 'Vary: Origin', false ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
751 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
752 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
753 |
return $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
754 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
755 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
756 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
757 |
* Handles OPTIONS requests for the server. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
758 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
759 |
* This is handled outside of the server code, as it doesn't obey normal route |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
760 |
* mapping. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
761 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
762 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
763 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
764 |
* @param mixed $response Current response, either response or `null` to indicate pass-through. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
765 |
* @param WP_REST_Server $handler ResponseHandler instance (usually WP_REST_Server). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
766 |
* @param WP_REST_Request $request The request that was used to make current response. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
767 |
* @return WP_REST_Response Modified response, either response or `null` to indicate pass-through. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
768 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
769 |
function rest_handle_options_request( $response, $handler, $request ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
770 |
if ( ! empty( $response ) || $request->get_method() !== 'OPTIONS' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
771 |
return $response; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
772 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
773 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
774 |
$response = new WP_REST_Response(); |
9 | 775 |
$data = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
776 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
777 |
foreach ( $handler->get_routes() as $route => $endpoints ) { |
9 | 778 |
$match = preg_match( '@^' . $route . '$@i', $request->get_route(), $matches ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
779 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
780 |
if ( ! $match ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
781 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
782 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
783 |
|
9 | 784 |
$args = array(); |
785 |
foreach ( $matches as $param => $value ) { |
|
786 |
if ( ! is_int( $param ) ) { |
|
787 |
$args[ $param ] = $value; |
|
788 |
} |
|
789 |
} |
|
790 |
||
791 |
foreach ( $endpoints as $endpoint ) { |
|
16 | 792 |
// Remove the redundant preg_match() argument. |
9 | 793 |
unset( $args[0] ); |
794 |
||
795 |
$request->set_url_params( $args ); |
|
796 |
$request->set_attributes( $endpoint ); |
|
797 |
} |
|
798 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
799 |
$data = $handler->get_data_for_route( $route, $endpoints, 'help' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
800 |
$response->set_matched_route( $route ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
801 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
802 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
803 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
804 |
$response->set_data( $data ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
805 |
return $response; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
806 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
807 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
808 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
809 |
* Sends the "Allow" header to state all methods that can be sent to the current route. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
810 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
811 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
812 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
813 |
* @param WP_REST_Response $response Current response being served. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
814 |
* @param WP_REST_Server $server ResponseHandler instance (usually WP_REST_Server). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
815 |
* @param WP_REST_Request $request The request that was used to make current response. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
816 |
* @return WP_REST_Response Response to be served, with "Allow" header if route has allowed methods. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
817 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
818 |
function rest_send_allow_header( $response, $server, $request ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
819 |
$matched_route = $response->get_matched_route(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
820 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
821 |
if ( ! $matched_route ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
822 |
return $response; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
823 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
824 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
825 |
$routes = $server->get_routes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
826 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
827 |
$allowed_methods = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
828 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
829 |
// Get the allowed methods across the routes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
830 |
foreach ( $routes[ $matched_route ] as $_handler ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
831 |
foreach ( $_handler['methods'] as $handler_method => $value ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
832 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
833 |
if ( ! empty( $_handler['permission_callback'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
834 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
835 |
$permission = call_user_func( $_handler['permission_callback'], $request ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
836 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
837 |
$allowed_methods[ $handler_method ] = true === $permission; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
838 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
839 |
$allowed_methods[ $handler_method ] = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
840 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
841 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
842 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
843 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
844 |
// Strip out all the methods that are not allowed (false values). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
845 |
$allowed_methods = array_filter( $allowed_methods ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
846 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
847 |
if ( $allowed_methods ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
848 |
$response->header( 'Allow', implode( ', ', array_map( 'strtoupper', array_keys( $allowed_methods ) ) ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
849 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
850 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
851 |
return $response; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
852 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
853 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
854 |
/** |
16 | 855 |
* Recursively computes the intersection of arrays using keys for comparison. |
856 |
* |
|
857 |
* @since 5.3.0 |
|
858 |
* |
|
859 |
* @param array $array1 The array with master keys to check. |
|
860 |
* @param array $array2 An array to compare keys against. |
|
861 |
* @return array An associative array containing all the entries of array1 which have keys |
|
862 |
* that are present in all arguments. |
|
863 |
*/ |
|
864 |
function _rest_array_intersect_key_recursive( $array1, $array2 ) { |
|
865 |
$array1 = array_intersect_key( $array1, $array2 ); |
|
866 |
foreach ( $array1 as $key => $value ) { |
|
867 |
if ( is_array( $value ) && is_array( $array2[ $key ] ) ) { |
|
868 |
$array1[ $key ] = _rest_array_intersect_key_recursive( $value, $array2[ $key ] ); |
|
869 |
} |
|
870 |
} |
|
871 |
return $array1; |
|
872 |
} |
|
873 |
||
874 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
875 |
* Filters the REST API response to include only an allow-listed set of response object fields. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
876 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
877 |
* @since 4.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
878 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
879 |
* @param WP_REST_Response $response Current response being served. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
880 |
* @param WP_REST_Server $server ResponseHandler instance (usually WP_REST_Server). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
881 |
* @param WP_REST_Request $request The request that was used to make current response. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
882 |
* @return WP_REST_Response Response to be served, trimmed down to contain a subset of fields. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
883 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
884 |
function rest_filter_response_fields( $response, $server, $request ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
885 |
if ( ! isset( $request['_fields'] ) || $response->is_error() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
886 |
return $response; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
887 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
888 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
889 |
$data = $response->get_data(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
890 |
|
9 | 891 |
$fields = wp_parse_list( $request['_fields'] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
892 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
893 |
if ( 0 === count( $fields ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
894 |
return $response; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
895 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
896 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
897 |
// Trim off outside whitespace from the comma delimited list. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
898 |
$fields = array_map( 'trim', $fields ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
899 |
|
16 | 900 |
// Create nested array of accepted field hierarchy. |
901 |
$fields_as_keyed = array(); |
|
902 |
foreach ( $fields as $field ) { |
|
903 |
$parts = explode( '.', $field ); |
|
904 |
$ref = &$fields_as_keyed; |
|
905 |
while ( count( $parts ) > 1 ) { |
|
906 |
$next = array_shift( $parts ); |
|
907 |
if ( isset( $ref[ $next ] ) && true === $ref[ $next ] ) { |
|
908 |
// Skip any sub-properties if their parent prop is already marked for inclusion. |
|
909 |
break 2; |
|
910 |
} |
|
911 |
$ref[ $next ] = isset( $ref[ $next ] ) ? $ref[ $next ] : array(); |
|
912 |
$ref = &$ref[ $next ]; |
|
913 |
} |
|
914 |
$last = array_shift( $parts ); |
|
915 |
$ref[ $last ] = true; |
|
916 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
917 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
918 |
if ( wp_is_numeric_array( $data ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
919 |
$new_data = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
920 |
foreach ( $data as $item ) { |
16 | 921 |
$new_data[] = _rest_array_intersect_key_recursive( $item, $fields_as_keyed ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
922 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
923 |
} else { |
16 | 924 |
$new_data = _rest_array_intersect_key_recursive( $data, $fields_as_keyed ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
925 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
926 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
927 |
$response->set_data( $new_data ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
928 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
929 |
return $response; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
930 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
931 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
932 |
/** |
16 | 933 |
* Given an array of fields to include in a response, some of which may be |
934 |
* `nested.fields`, determine whether the provided field should be included |
|
935 |
* in the response body. |
|
936 |
* |
|
937 |
* If a parent field is passed in, the presence of any nested field within |
|
938 |
* that parent will cause the method to return `true`. For example "title" |
|
939 |
* will return true if any of `title`, `title.raw` or `title.rendered` is |
|
940 |
* provided. |
|
941 |
* |
|
942 |
* @since 5.3.0 |
|
943 |
* |
|
944 |
* @param string $field A field to test for inclusion in the response body. |
|
945 |
* @param array $fields An array of string fields supported by the endpoint. |
|
946 |
* @return bool Whether to include the field or not. |
|
947 |
*/ |
|
948 |
function rest_is_field_included( $field, $fields ) { |
|
949 |
if ( in_array( $field, $fields, true ) ) { |
|
950 |
return true; |
|
951 |
} |
|
952 |
||
953 |
foreach ( $fields as $accepted_field ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
954 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
955 |
* Check to see if $field is the parent of any item in $fields. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
956 |
* A field "parent" should be accepted if "parent.child" is accepted. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
957 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
958 |
if ( str_starts_with( $accepted_field, "$field." ) ) { |
16 | 959 |
return true; |
960 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
961 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
962 |
* Conversely, if "parent" is accepted, all "parent.child" fields |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
963 |
* should also be accepted. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
964 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
965 |
if ( str_starts_with( $field, "$accepted_field." ) ) { |
16 | 966 |
return true; |
967 |
} |
|
968 |
} |
|
969 |
||
970 |
return false; |
|
971 |
} |
|
972 |
||
973 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
974 |
* Adds the REST API URL to the WP RSD endpoint. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
975 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
976 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
977 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
978 |
* @see get_rest_url() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
979 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
980 |
function rest_output_rsd() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
981 |
$api_root = get_rest_url(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
982 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
983 |
if ( empty( $api_root ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
984 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
985 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
986 |
?> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
987 |
<api name="WP-API" blogID="1" preferred="false" apiLink="<?php echo esc_url( $api_root ); ?>" /> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
988 |
<?php |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
989 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
990 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
991 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
992 |
* Outputs the REST API link tag into page header. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
993 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
994 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
995 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
996 |
* @see get_rest_url() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
997 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
998 |
function rest_output_link_wp_head() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
999 |
$api_root = get_rest_url(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1000 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1001 |
if ( empty( $api_root ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1002 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1003 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1004 |
|
16 | 1005 |
printf( '<link rel="https://api.w.org/" href="%s" />', esc_url( $api_root ) ); |
1006 |
||
1007 |
$resource = rest_get_queried_resource_route(); |
|
1008 |
||
1009 |
if ( $resource ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1010 |
printf( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1011 |
'<link rel="alternate" title="%1$s" type="application/json" href="%2$s" />', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1012 |
_x( 'JSON', 'REST API resource link name' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1013 |
esc_url( rest_url( $resource ) ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1014 |
); |
16 | 1015 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1016 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1017 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1018 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1019 |
* Sends a Link header for the REST API. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1020 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1021 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1022 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1023 |
function rest_output_link_header() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1024 |
if ( headers_sent() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1025 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1026 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1027 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1028 |
$api_root = get_rest_url(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1029 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1030 |
if ( empty( $api_root ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1031 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1032 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1033 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1034 |
header( sprintf( 'Link: <%s>; rel="https://api.w.org/"', sanitize_url( $api_root ) ), false ); |
16 | 1035 |
|
1036 |
$resource = rest_get_queried_resource_route(); |
|
1037 |
||
1038 |
if ( $resource ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1039 |
header( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1040 |
sprintf( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1041 |
'Link: <%1$s>; rel="alternate"; title="%2$s"; type="application/json"', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1042 |
sanitize_url( rest_url( $resource ) ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1043 |
_x( 'JSON', 'REST API resource link name' ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1044 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1045 |
false |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1046 |
); |
16 | 1047 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1048 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1049 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1050 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1051 |
* Checks for errors when using cookie-based authentication. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1052 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1053 |
* WordPress' built-in cookie authentication is always active |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1054 |
* for logged in users. However, the API has to check nonces |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1055 |
* for each request to ensure users are not vulnerable to CSRF. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1056 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1057 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1058 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1059 |
* @global mixed $wp_rest_auth_cookie |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1060 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1061 |
* @param WP_Error|mixed $result Error from another authentication handler, |
16 | 1062 |
* null if we should handle it, or another value if not. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1063 |
* @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1064 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1065 |
function rest_cookie_check_errors( $result ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1066 |
if ( ! empty( $result ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1067 |
return $result; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1068 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1069 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1070 |
global $wp_rest_auth_cookie; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1071 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1072 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1073 |
* Is cookie authentication being used? (If we get an auth |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1074 |
* error, but we're still logged in, another authentication |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1075 |
* must have been used). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1076 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1077 |
if ( true !== $wp_rest_auth_cookie && is_user_logged_in() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1078 |
return $result; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1079 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1080 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1081 |
// Determine if there is a nonce. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1082 |
$nonce = null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1083 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1084 |
if ( isset( $_REQUEST['_wpnonce'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1085 |
$nonce = $_REQUEST['_wpnonce']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1086 |
} elseif ( isset( $_SERVER['HTTP_X_WP_NONCE'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1087 |
$nonce = $_SERVER['HTTP_X_WP_NONCE']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1088 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1089 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1090 |
if ( null === $nonce ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1091 |
// No nonce at all, so act as if it's an unauthenticated request. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1092 |
wp_set_current_user( 0 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1093 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1094 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1095 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1096 |
// Check the nonce. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1097 |
$result = wp_verify_nonce( $nonce, 'wp_rest' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1098 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1099 |
if ( ! $result ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1100 |
add_filter( 'rest_send_nocache_headers', '__return_true', 20 ); |
18 | 1101 |
return new WP_Error( 'rest_cookie_invalid_nonce', __( 'Cookie check failed' ), array( 'status' => 403 ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1102 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1103 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1104 |
// Send a refreshed nonce in header. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1105 |
rest_get_server()->send_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1106 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1107 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1108 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1109 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1110 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1111 |
* Collects cookie authentication status. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1112 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1113 |
* Collects errors from wp_validate_auth_cookie for use by rest_cookie_check_errors. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1114 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1115 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1116 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1117 |
* @see current_action() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1118 |
* @global mixed $wp_rest_auth_cookie |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1119 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1120 |
function rest_cookie_collect_status() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1121 |
global $wp_rest_auth_cookie; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1122 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1123 |
$status_type = current_action(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1124 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1125 |
if ( 'auth_cookie_valid' !== $status_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1126 |
$wp_rest_auth_cookie = substr( $status_type, 12 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1127 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1128 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1129 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1130 |
$wp_rest_auth_cookie = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1131 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1132 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1133 |
/** |
18 | 1134 |
* Collects the status of authenticating with an application password. |
1135 |
* |
|
1136 |
* @since 5.6.0 |
|
1137 |
* @since 5.7.0 Added the `$app_password` parameter. |
|
1138 |
* |
|
1139 |
* @global WP_User|WP_Error|null $wp_rest_application_password_status |
|
1140 |
* @global string|null $wp_rest_application_password_uuid |
|
1141 |
* |
|
1142 |
* @param WP_Error $user_or_error The authenticated user or error instance. |
|
1143 |
* @param array $app_password The Application Password used to authenticate. |
|
1144 |
*/ |
|
1145 |
function rest_application_password_collect_status( $user_or_error, $app_password = array() ) { |
|
1146 |
global $wp_rest_application_password_status, $wp_rest_application_password_uuid; |
|
1147 |
||
1148 |
$wp_rest_application_password_status = $user_or_error; |
|
1149 |
||
1150 |
if ( empty( $app_password['uuid'] ) ) { |
|
1151 |
$wp_rest_application_password_uuid = null; |
|
1152 |
} else { |
|
1153 |
$wp_rest_application_password_uuid = $app_password['uuid']; |
|
1154 |
} |
|
1155 |
} |
|
1156 |
||
1157 |
/** |
|
1158 |
* Gets the Application Password used for authenticating the request. |
|
1159 |
* |
|
1160 |
* @since 5.7.0 |
|
1161 |
* |
|
1162 |
* @global string|null $wp_rest_application_password_uuid |
|
1163 |
* |
|
19 | 1164 |
* @return string|null The Application Password UUID, or null if Application Passwords was not used. |
18 | 1165 |
*/ |
1166 |
function rest_get_authenticated_app_password() { |
|
1167 |
global $wp_rest_application_password_uuid; |
|
1168 |
||
1169 |
return $wp_rest_application_password_uuid; |
|
1170 |
} |
|
1171 |
||
1172 |
/** |
|
1173 |
* Checks for errors when using application password-based authentication. |
|
1174 |
* |
|
1175 |
* @since 5.6.0 |
|
1176 |
* |
|
1177 |
* @global WP_User|WP_Error|null $wp_rest_application_password_status |
|
1178 |
* |
|
1179 |
* @param WP_Error|null|true $result Error from another authentication handler, |
|
1180 |
* null if we should handle it, or another value if not. |
|
1181 |
* @return WP_Error|null|true WP_Error if the application password is invalid, the $result, otherwise true. |
|
1182 |
*/ |
|
1183 |
function rest_application_password_check_errors( $result ) { |
|
1184 |
global $wp_rest_application_password_status; |
|
1185 |
||
1186 |
if ( ! empty( $result ) ) { |
|
1187 |
return $result; |
|
1188 |
} |
|
1189 |
||
1190 |
if ( is_wp_error( $wp_rest_application_password_status ) ) { |
|
1191 |
$data = $wp_rest_application_password_status->get_error_data(); |
|
1192 |
||
1193 |
if ( ! isset( $data['status'] ) ) { |
|
1194 |
$data['status'] = 401; |
|
1195 |
} |
|
1196 |
||
1197 |
$wp_rest_application_password_status->add_data( $data ); |
|
1198 |
||
1199 |
return $wp_rest_application_password_status; |
|
1200 |
} |
|
1201 |
||
1202 |
if ( $wp_rest_application_password_status instanceof WP_User ) { |
|
1203 |
return true; |
|
1204 |
} |
|
1205 |
||
1206 |
return $result; |
|
1207 |
} |
|
1208 |
||
1209 |
/** |
|
1210 |
* Adds Application Passwords info to the REST API index. |
|
1211 |
* |
|
1212 |
* @since 5.6.0 |
|
1213 |
* |
|
1214 |
* @param WP_REST_Response $response The index response object. |
|
1215 |
* @return WP_REST_Response |
|
1216 |
*/ |
|
1217 |
function rest_add_application_passwords_to_index( $response ) { |
|
1218 |
if ( ! wp_is_application_passwords_available() ) { |
|
1219 |
return $response; |
|
1220 |
} |
|
1221 |
||
1222 |
$response->data['authentication']['application-passwords'] = array( |
|
1223 |
'endpoints' => array( |
|
1224 |
'authorization' => admin_url( 'authorize-application.php' ), |
|
1225 |
), |
|
1226 |
); |
|
1227 |
||
1228 |
return $response; |
|
1229 |
} |
|
1230 |
||
1231 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1232 |
* Retrieves the avatar URLs in various sizes. |
16 | 1233 |
* |
1234 |
* @since 4.7.0 |
|
1235 |
* |
|
1236 |
* @see get_avatar_url() |
|
1237 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1238 |
* @param mixed $id_or_email The avatar to retrieve a URL for. Accepts a user ID, Gravatar MD5 hash, |
16 | 1239 |
* user email, WP_User object, WP_Post object, or WP_Comment object. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1240 |
* @return (string|false)[] Avatar URLs keyed by size. Each value can be a URL string or boolean false. |
16 | 1241 |
*/ |
1242 |
function rest_get_avatar_urls( $id_or_email ) { |
|
1243 |
$avatar_sizes = rest_get_avatar_sizes(); |
|
1244 |
||
1245 |
$urls = array(); |
|
1246 |
foreach ( $avatar_sizes as $size ) { |
|
1247 |
$urls[ $size ] = get_avatar_url( $id_or_email, array( 'size' => $size ) ); |
|
1248 |
} |
|
1249 |
||
1250 |
return $urls; |
|
1251 |
} |
|
1252 |
||
1253 |
/** |
|
1254 |
* Retrieves the pixel sizes for avatars. |
|
1255 |
* |
|
1256 |
* @since 4.7.0 |
|
1257 |
* |
|
1258 |
* @return int[] List of pixel sizes for avatars. Default `[ 24, 48, 96 ]`. |
|
1259 |
*/ |
|
1260 |
function rest_get_avatar_sizes() { |
|
1261 |
/** |
|
1262 |
* Filters the REST avatar sizes. |
|
1263 |
* |
|
1264 |
* Use this filter to adjust the array of sizes returned by the |
|
1265 |
* `rest_get_avatar_sizes` function. |
|
1266 |
* |
|
1267 |
* @since 4.4.0 |
|
1268 |
* |
|
1269 |
* @param int[] $sizes An array of int values that are the pixel sizes for avatars. |
|
1270 |
* Default `[ 24, 48, 96 ]`. |
|
1271 |
*/ |
|
1272 |
return apply_filters( 'rest_avatar_sizes', array( 24, 48, 96 ) ); |
|
1273 |
} |
|
1274 |
||
1275 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1276 |
* Parses an RFC3339 time into a Unix timestamp. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1277 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1278 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1279 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1280 |
* @param string $date RFC3339 timestamp. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1281 |
* @param bool $force_utc Optional. Whether to force UTC timezone instead of using |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1282 |
* the timestamp's timezone. Default false. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1283 |
* @return int|false Unix timestamp on success, false on failure. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1284 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1285 |
function rest_parse_date( $date, $force_utc = false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1286 |
if ( $force_utc ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1287 |
$date = preg_replace( '/[+-]\d+:?\d+$/', '+00:00', $date ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1288 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1289 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1290 |
$regex = '#^\d{4}-\d{2}-\d{2}[Tt ]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}(?::\d{2})?)?$#'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1291 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1292 |
if ( ! preg_match( $regex, $date, $matches ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1293 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1294 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1295 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1296 |
return strtotime( $date ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1297 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1298 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1299 |
/** |
16 | 1300 |
* Parses a 3 or 6 digit hex color (with #). |
1301 |
* |
|
1302 |
* @since 5.4.0 |
|
1303 |
* |
|
1304 |
* @param string $color 3 or 6 digit hex color (with #). |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1305 |
* @return string|false Color value on success, false on failure. |
16 | 1306 |
*/ |
1307 |
function rest_parse_hex_color( $color ) { |
|
1308 |
$regex = '|^#([A-Fa-f0-9]{3}){1,2}$|'; |
|
1309 |
if ( ! preg_match( $regex, $color, $matches ) ) { |
|
1310 |
return false; |
|
1311 |
} |
|
1312 |
||
1313 |
return $color; |
|
1314 |
} |
|
1315 |
||
1316 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1317 |
* Parses a date into both its local and UTC equivalent, in MySQL datetime format. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1318 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1319 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1320 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1321 |
* @see rest_parse_date() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1322 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1323 |
* @param string $date RFC3339 timestamp. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1324 |
* @param bool $is_utc Whether the provided date should be interpreted as UTC. Default false. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1325 |
* @return array|null { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1326 |
* Local and UTC datetime strings, in MySQL datetime format (Y-m-d H:i:s), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1327 |
* null on failure. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1328 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1329 |
* @type string $0 Local datetime string. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1330 |
* @type string $1 UTC datetime string. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1331 |
* } |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1332 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1333 |
function rest_get_date_with_gmt( $date, $is_utc = false ) { |
16 | 1334 |
/* |
1335 |
* Whether or not the original date actually has a timezone string |
|
1336 |
* changes the way we need to do timezone conversion. |
|
1337 |
* Store this info before parsing the date, and use it later. |
|
1338 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1339 |
$has_timezone = preg_match( '#(Z|[+-]\d{2}(:\d{2})?)$#', $date ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1340 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1341 |
$date = rest_parse_date( $date ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1342 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1343 |
if ( empty( $date ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1344 |
return null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1345 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1346 |
|
16 | 1347 |
/* |
1348 |
* At this point $date could either be a local date (if we were passed |
|
1349 |
* a *local* date without a timezone offset) or a UTC date (otherwise). |
|
1350 |
* Timezone conversion needs to be handled differently between these two cases. |
|
1351 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1352 |
if ( ! $is_utc && ! $has_timezone ) { |
16 | 1353 |
$local = gmdate( 'Y-m-d H:i:s', $date ); |
9 | 1354 |
$utc = get_gmt_from_date( $local ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1355 |
} else { |
16 | 1356 |
$utc = gmdate( 'Y-m-d H:i:s', $date ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1357 |
$local = get_date_from_gmt( $utc ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1358 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1359 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1360 |
return array( $local, $utc ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1361 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1362 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1363 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1364 |
* Returns a contextual HTTP error code for authorization failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1365 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1366 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1367 |
* |
18 | 1368 |
* @return int 401 if the user is not logged in, 403 if the user is logged in. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1369 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1370 |
function rest_authorization_required_code() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1371 |
return is_user_logged_in() ? 403 : 401; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1372 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1373 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1374 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1375 |
* Validate a request argument based on details registered to the route. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1376 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1377 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1378 |
* |
16 | 1379 |
* @param mixed $value |
1380 |
* @param WP_REST_Request $request |
|
1381 |
* @param string $param |
|
1382 |
* @return true|WP_Error |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1383 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1384 |
function rest_validate_request_arg( $value, $request, $param ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1385 |
$attributes = $request->get_attributes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1386 |
if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1387 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1388 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1389 |
$args = $attributes['args'][ $param ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1390 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1391 |
return rest_validate_value_from_schema( $value, $args, $param ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1392 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1393 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1394 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1395 |
* Sanitize a request argument based on details registered to the route. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1396 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1397 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1398 |
* |
16 | 1399 |
* @param mixed $value |
1400 |
* @param WP_REST_Request $request |
|
1401 |
* @param string $param |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1402 |
* @return mixed |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1403 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1404 |
function rest_sanitize_request_arg( $value, $request, $param ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1405 |
$attributes = $request->get_attributes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1406 |
if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1407 |
return $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1408 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1409 |
$args = $attributes['args'][ $param ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1410 |
|
16 | 1411 |
return rest_sanitize_value_from_schema( $value, $args, $param ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1412 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1413 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1414 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1415 |
* Parse a request argument based on details registered to the route. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1416 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1417 |
* Runs a validation check and sanitizes the value, primarily to be used via |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1418 |
* the `sanitize_callback` arguments in the endpoint args registration. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1419 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1420 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1421 |
* |
16 | 1422 |
* @param mixed $value |
1423 |
* @param WP_REST_Request $request |
|
1424 |
* @param string $param |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1425 |
* @return mixed |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1426 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1427 |
function rest_parse_request_arg( $value, $request, $param ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1428 |
$is_valid = rest_validate_request_arg( $value, $request, $param ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1429 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1430 |
if ( is_wp_error( $is_valid ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1431 |
return $is_valid; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1432 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1433 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1434 |
$value = rest_sanitize_request_arg( $value, $request, $param ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1435 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1436 |
return $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1437 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1438 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1439 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1440 |
* Determines if an IP address is valid. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1441 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1442 |
* Handles both IPv4 and IPv6 addresses. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1443 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1444 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1445 |
* |
16 | 1446 |
* @param string $ip IP address. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1447 |
* @return string|false The valid IP address, otherwise false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1448 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1449 |
function rest_is_ip_address( $ip ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1450 |
$ipv4_pattern = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1451 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1452 |
if ( ! preg_match( $ipv4_pattern, $ip ) && ! WpOrg\Requests\Ipv6::check_ipv6( $ip ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1453 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1454 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1455 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1456 |
return $ip; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1457 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1458 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1459 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1460 |
* Changes a boolean-like value into the proper boolean value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1461 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1462 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1463 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1464 |
* @param bool|string|int $value The value being evaluated. |
18 | 1465 |
* @return bool Returns the proper associated boolean value. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1466 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1467 |
function rest_sanitize_boolean( $value ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1468 |
// String values are translated to `true`; make sure 'false' is false. |
9 | 1469 |
if ( is_string( $value ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1470 |
$value = strtolower( $value ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1471 |
if ( in_array( $value, array( 'false', '0' ), true ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1472 |
$value = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1473 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1474 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1475 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1476 |
// Everything else will map nicely to boolean. |
9 | 1477 |
return (bool) $value; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1478 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1479 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1480 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1481 |
* Determines if a given value is boolean-like. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1482 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1483 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1484 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1485 |
* @param bool|string $maybe_bool The value being evaluated. |
18 | 1486 |
* @return bool True if a boolean, otherwise false. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1487 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1488 |
function rest_is_boolean( $maybe_bool ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1489 |
if ( is_bool( $maybe_bool ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1490 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1491 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1492 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1493 |
if ( is_string( $maybe_bool ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1494 |
$maybe_bool = strtolower( $maybe_bool ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1495 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1496 |
$valid_boolean_values = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1497 |
'false', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1498 |
'true', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1499 |
'0', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1500 |
'1', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1501 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1502 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1503 |
return in_array( $maybe_bool, $valid_boolean_values, true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1504 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1505 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1506 |
if ( is_int( $maybe_bool ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1507 |
return in_array( $maybe_bool, array( 0, 1 ), true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1508 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1509 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1510 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1511 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1512 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1513 |
/** |
16 | 1514 |
* Determines if a given value is integer-like. |
1515 |
* |
|
1516 |
* @since 5.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1517 |
* |
16 | 1518 |
* @param mixed $maybe_integer The value being evaluated. |
1519 |
* @return bool True if an integer, otherwise false. |
|
1520 |
*/ |
|
1521 |
function rest_is_integer( $maybe_integer ) { |
|
18 | 1522 |
return is_numeric( $maybe_integer ) && round( (float) $maybe_integer ) === (float) $maybe_integer; |
16 | 1523 |
} |
1524 |
||
1525 |
/** |
|
1526 |
* Determines if a given value is array-like. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1527 |
* |
16 | 1528 |
* @since 5.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1529 |
* |
16 | 1530 |
* @param mixed $maybe_array The value being evaluated. |
1531 |
* @return bool |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1532 |
*/ |
16 | 1533 |
function rest_is_array( $maybe_array ) { |
1534 |
if ( is_scalar( $maybe_array ) ) { |
|
1535 |
$maybe_array = wp_parse_list( $maybe_array ); |
|
1536 |
} |
|
1537 |
||
1538 |
return wp_is_numeric_array( $maybe_array ); |
|
1539 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1540 |
|
16 | 1541 |
/** |
1542 |
* Converts an array-like value to an array. |
|
1543 |
* |
|
1544 |
* @since 5.5.0 |
|
1545 |
* |
|
1546 |
* @param mixed $maybe_array The value being evaluated. |
|
1547 |
* @return array Returns the array extracted from the value. |
|
1548 |
*/ |
|
1549 |
function rest_sanitize_array( $maybe_array ) { |
|
1550 |
if ( is_scalar( $maybe_array ) ) { |
|
1551 |
return wp_parse_list( $maybe_array ); |
|
1552 |
} |
|
1553 |
||
1554 |
if ( ! is_array( $maybe_array ) ) { |
|
1555 |
return array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1556 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1557 |
|
16 | 1558 |
// Normalize to numeric array so nothing unexpected is in the keys. |
1559 |
return array_values( $maybe_array ); |
|
1560 |
} |
|
1561 |
||
1562 |
/** |
|
1563 |
* Determines if a given value is object-like. |
|
1564 |
* |
|
1565 |
* @since 5.5.0 |
|
1566 |
* |
|
1567 |
* @param mixed $maybe_object The value being evaluated. |
|
1568 |
* @return bool True if object like, otherwise false. |
|
1569 |
*/ |
|
1570 |
function rest_is_object( $maybe_object ) { |
|
1571 |
if ( '' === $maybe_object ) { |
|
1572 |
return true; |
|
1573 |
} |
|
1574 |
||
1575 |
if ( $maybe_object instanceof stdClass ) { |
|
1576 |
return true; |
|
1577 |
} |
|
1578 |
||
1579 |
if ( $maybe_object instanceof JsonSerializable ) { |
|
1580 |
$maybe_object = $maybe_object->jsonSerialize(); |
|
1581 |
} |
|
1582 |
||
1583 |
return is_array( $maybe_object ); |
|
1584 |
} |
|
1585 |
||
1586 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1587 |
* Converts an object-like value to an array. |
16 | 1588 |
* |
1589 |
* @since 5.5.0 |
|
1590 |
* |
|
1591 |
* @param mixed $maybe_object The value being evaluated. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1592 |
* @return array Returns the object extracted from the value as an associative array. |
16 | 1593 |
*/ |
1594 |
function rest_sanitize_object( $maybe_object ) { |
|
1595 |
if ( '' === $maybe_object ) { |
|
1596 |
return array(); |
|
1597 |
} |
|
1598 |
||
1599 |
if ( $maybe_object instanceof stdClass ) { |
|
1600 |
return (array) $maybe_object; |
|
1601 |
} |
|
1602 |
||
1603 |
if ( $maybe_object instanceof JsonSerializable ) { |
|
1604 |
$maybe_object = $maybe_object->jsonSerialize(); |
|
1605 |
} |
|
1606 |
||
1607 |
if ( ! is_array( $maybe_object ) ) { |
|
1608 |
return array(); |
|
1609 |
} |
|
1610 |
||
1611 |
return $maybe_object; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1612 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1613 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1614 |
/** |
16 | 1615 |
* Gets the best type for a value. |
1616 |
* |
|
1617 |
* @since 5.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1618 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1619 |
* @param mixed $value The value to check. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1620 |
* @param string[] $types The list of possible types. |
16 | 1621 |
* @return string The best matching type, an empty string if no types match. |
1622 |
*/ |
|
1623 |
function rest_get_best_type_for_value( $value, $types ) { |
|
1624 |
static $checks = array( |
|
1625 |
'array' => 'rest_is_array', |
|
1626 |
'object' => 'rest_is_object', |
|
1627 |
'integer' => 'rest_is_integer', |
|
1628 |
'number' => 'is_numeric', |
|
1629 |
'boolean' => 'rest_is_boolean', |
|
1630 |
'string' => 'is_string', |
|
1631 |
'null' => 'is_null', |
|
1632 |
); |
|
1633 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1634 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1635 |
* Both arrays and objects allow empty strings to be converted to their types. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1636 |
* But the best answer for this type is a string. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1637 |
*/ |
16 | 1638 |
if ( '' === $value && in_array( 'string', $types, true ) ) { |
1639 |
return 'string'; |
|
1640 |
} |
|
1641 |
||
1642 |
foreach ( $types as $type ) { |
|
1643 |
if ( isset( $checks[ $type ] ) && $checks[ $type ]( $value ) ) { |
|
1644 |
return $type; |
|
1645 |
} |
|
1646 |
} |
|
1647 |
||
1648 |
return ''; |
|
1649 |
} |
|
1650 |
||
1651 |
/** |
|
1652 |
* Handles getting the best type for a multi-type schema. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1653 |
* |
16 | 1654 |
* This is a wrapper for {@see rest_get_best_type_for_value()} that handles |
1655 |
* backward compatibility for schemas that use invalid types. |
|
1656 |
* |
|
1657 |
* @since 5.5.0 |
|
1658 |
* |
|
1659 |
* @param mixed $value The value to check. |
|
1660 |
* @param array $args The schema array to use. |
|
1661 |
* @param string $param The parameter name, used in error messages. |
|
1662 |
* @return string |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1663 |
*/ |
16 | 1664 |
function rest_handle_multi_type_schema( $value, $args, $param = '' ) { |
1665 |
$allowed_types = array( 'array', 'object', 'string', 'number', 'integer', 'boolean', 'null' ); |
|
1666 |
$invalid_types = array_diff( $args['type'], $allowed_types ); |
|
1667 |
||
1668 |
if ( $invalid_types ) { |
|
1669 |
_doing_it_wrong( |
|
1670 |
__FUNCTION__, |
|
18 | 1671 |
/* translators: 1: Parameter, 2: List of allowed types. */ |
16 | 1672 |
wp_sprintf( __( 'The "type" schema keyword for %1$s can only contain the built-in types: %2$l.' ), $param, $allowed_types ), |
1673 |
'5.5.0' |
|
1674 |
); |
|
1675 |
} |
|
1676 |
||
1677 |
$best_type = rest_get_best_type_for_value( $value, $args['type'] ); |
|
1678 |
||
1679 |
if ( ! $best_type ) { |
|
1680 |
if ( ! $invalid_types ) { |
|
1681 |
return ''; |
|
1682 |
} |
|
1683 |
||
1684 |
// Backward compatibility for previous behavior which allowed the value if there was an invalid type used. |
|
1685 |
$best_type = reset( $invalid_types ); |
|
1686 |
} |
|
1687 |
||
1688 |
return $best_type; |
|
1689 |
} |
|
1690 |
||
1691 |
/** |
|
1692 |
* Checks if an array is made up of unique items. |
|
1693 |
* |
|
1694 |
* @since 5.5.0 |
|
1695 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1696 |
* @param array $input_array The array to check. |
16 | 1697 |
* @return bool True if the array contains unique items, false otherwise. |
1698 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1699 |
function rest_validate_array_contains_unique_items( $input_array ) { |
16 | 1700 |
$seen = array(); |
1701 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1702 |
foreach ( $input_array as $item ) { |
16 | 1703 |
$stabilized = rest_stabilize_value( $item ); |
1704 |
$key = serialize( $stabilized ); |
|
1705 |
||
1706 |
if ( ! isset( $seen[ $key ] ) ) { |
|
1707 |
$seen[ $key ] = true; |
|
1708 |
||
1709 |
continue; |
|
1710 |
} |
|
1711 |
||
1712 |
return false; |
|
1713 |
} |
|
1714 |
||
1715 |
return true; |
|
1716 |
} |
|
1717 |
||
1718 |
/** |
|
1719 |
* Stabilizes a value following JSON Schema semantics. |
|
1720 |
* |
|
1721 |
* For lists, order is preserved. For objects, properties are reordered alphabetically. |
|
1722 |
* |
|
1723 |
* @since 5.5.0 |
|
1724 |
* |
|
1725 |
* @param mixed $value The value to stabilize. Must already be sanitized. Objects should have been converted to arrays. |
|
1726 |
* @return mixed The stabilized value. |
|
1727 |
*/ |
|
1728 |
function rest_stabilize_value( $value ) { |
|
1729 |
if ( is_scalar( $value ) || is_null( $value ) ) { |
|
1730 |
return $value; |
|
1731 |
} |
|
1732 |
||
1733 |
if ( is_object( $value ) ) { |
|
1734 |
_doing_it_wrong( __FUNCTION__, __( 'Cannot stabilize objects. Convert the object to an array first.' ), '5.5.0' ); |
|
1735 |
||
1736 |
return $value; |
|
1737 |
} |
|
1738 |
||
1739 |
ksort( $value ); |
|
1740 |
||
1741 |
foreach ( $value as $k => $v ) { |
|
1742 |
$value[ $k ] = rest_stabilize_value( $v ); |
|
1743 |
} |
|
1744 |
||
1745 |
return $value; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1746 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1747 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1748 |
/** |
18 | 1749 |
* Validates if the JSON Schema pattern matches a value. |
1750 |
* |
|
1751 |
* @since 5.6.0 |
|
1752 |
* |
|
1753 |
* @param string $pattern The pattern to match against. |
|
1754 |
* @param string $value The value to check. |
|
1755 |
* @return bool True if the pattern matches the given value, false otherwise. |
|
1756 |
*/ |
|
1757 |
function rest_validate_json_schema_pattern( $pattern, $value ) { |
|
1758 |
$escaped_pattern = str_replace( '#', '\\#', $pattern ); |
|
1759 |
||
1760 |
return 1 === preg_match( '#' . $escaped_pattern . '#u', $value ); |
|
1761 |
} |
|
1762 |
||
1763 |
/** |
|
1764 |
* Finds the schema for a property using the patternProperties keyword. |
|
1765 |
* |
|
1766 |
* @since 5.6.0 |
|
1767 |
* |
|
1768 |
* @param string $property The property name to check. |
|
1769 |
* @param array $args The schema array to use. |
|
1770 |
* @return array|null The schema of matching pattern property, or null if no patterns match. |
|
1771 |
*/ |
|
1772 |
function rest_find_matching_pattern_property_schema( $property, $args ) { |
|
1773 |
if ( isset( $args['patternProperties'] ) ) { |
|
1774 |
foreach ( $args['patternProperties'] as $pattern => $child_schema ) { |
|
1775 |
if ( rest_validate_json_schema_pattern( $pattern, $property ) ) { |
|
1776 |
return $child_schema; |
|
1777 |
} |
|
1778 |
} |
|
1779 |
} |
|
1780 |
||
1781 |
return null; |
|
1782 |
} |
|
1783 |
||
1784 |
/** |
|
1785 |
* Formats a combining operation error into a WP_Error object. |
|
1786 |
* |
|
1787 |
* @since 5.6.0 |
|
1788 |
* |
|
1789 |
* @param string $param The parameter name. |
|
1790 |
* @param array $error The error details. |
|
1791 |
* @return WP_Error |
|
1792 |
*/ |
|
1793 |
function rest_format_combining_operation_error( $param, $error ) { |
|
1794 |
$position = $error['index']; |
|
1795 |
$reason = $error['error_object']->get_error_message(); |
|
1796 |
||
1797 |
if ( isset( $error['schema']['title'] ) ) { |
|
1798 |
$title = $error['schema']['title']; |
|
1799 |
||
1800 |
return new WP_Error( |
|
1801 |
'rest_no_matching_schema', |
|
1802 |
/* translators: 1: Parameter, 2: Schema title, 3: Reason. */ |
|
1803 |
sprintf( __( '%1$s is not a valid %2$s. Reason: %3$s' ), $param, $title, $reason ), |
|
1804 |
array( 'position' => $position ) |
|
1805 |
); |
|
1806 |
} |
|
1807 |
||
1808 |
return new WP_Error( |
|
1809 |
'rest_no_matching_schema', |
|
1810 |
/* translators: 1: Parameter, 2: Reason. */ |
|
1811 |
sprintf( __( '%1$s does not match the expected format. Reason: %2$s' ), $param, $reason ), |
|
1812 |
array( 'position' => $position ) |
|
1813 |
); |
|
1814 |
} |
|
1815 |
||
1816 |
/** |
|
1817 |
* Gets the error of combining operation. |
|
1818 |
* |
|
1819 |
* @since 5.6.0 |
|
1820 |
* |
|
1821 |
* @param array $value The value to validate. |
|
1822 |
* @param string $param The parameter name, used in error messages. |
|
1823 |
* @param array $errors The errors array, to search for possible error. |
|
1824 |
* @return WP_Error The combining operation error. |
|
1825 |
*/ |
|
1826 |
function rest_get_combining_operation_error( $value, $param, $errors ) { |
|
1827 |
// If there is only one error, simply return it. |
|
1828 |
if ( 1 === count( $errors ) ) { |
|
1829 |
return rest_format_combining_operation_error( $param, $errors[0] ); |
|
1830 |
} |
|
1831 |
||
1832 |
// Filter out all errors related to type validation. |
|
1833 |
$filtered_errors = array(); |
|
1834 |
foreach ( $errors as $error ) { |
|
1835 |
$error_code = $error['error_object']->get_error_code(); |
|
1836 |
$error_data = $error['error_object']->get_error_data(); |
|
1837 |
||
1838 |
if ( 'rest_invalid_type' !== $error_code || ( isset( $error_data['param'] ) && $param !== $error_data['param'] ) ) { |
|
1839 |
$filtered_errors[] = $error; |
|
1840 |
} |
|
1841 |
} |
|
1842 |
||
1843 |
// If there is only one error left, simply return it. |
|
1844 |
if ( 1 === count( $filtered_errors ) ) { |
|
1845 |
return rest_format_combining_operation_error( $param, $filtered_errors[0] ); |
|
1846 |
} |
|
1847 |
||
1848 |
// If there are only errors related to object validation, try choosing the most appropriate one. |
|
1849 |
if ( count( $filtered_errors ) > 1 && 'object' === $filtered_errors[0]['schema']['type'] ) { |
|
1850 |
$result = null; |
|
1851 |
$number = 0; |
|
1852 |
||
1853 |
foreach ( $filtered_errors as $error ) { |
|
1854 |
if ( isset( $error['schema']['properties'] ) ) { |
|
1855 |
$n = count( array_intersect_key( $error['schema']['properties'], $value ) ); |
|
1856 |
if ( $n > $number ) { |
|
1857 |
$result = $error; |
|
1858 |
$number = $n; |
|
1859 |
} |
|
1860 |
} |
|
1861 |
} |
|
1862 |
||
1863 |
if ( null !== $result ) { |
|
1864 |
return rest_format_combining_operation_error( $param, $result ); |
|
1865 |
} |
|
1866 |
} |
|
1867 |
||
1868 |
// If each schema has a title, include those titles in the error message. |
|
1869 |
$schema_titles = array(); |
|
1870 |
foreach ( $errors as $error ) { |
|
1871 |
if ( isset( $error['schema']['title'] ) ) { |
|
1872 |
$schema_titles[] = $error['schema']['title']; |
|
1873 |
} |
|
1874 |
} |
|
1875 |
||
1876 |
if ( count( $schema_titles ) === count( $errors ) ) { |
|
1877 |
/* translators: 1: Parameter, 2: Schema titles. */ |
|
1878 |
return new WP_Error( 'rest_no_matching_schema', wp_sprintf( __( '%1$s is not a valid %2$l.' ), $param, $schema_titles ) ); |
|
1879 |
} |
|
1880 |
||
1881 |
/* translators: %s: Parameter. */ |
|
1882 |
return new WP_Error( 'rest_no_matching_schema', sprintf( __( '%s does not match any of the expected formats.' ), $param ) ); |
|
1883 |
} |
|
1884 |
||
1885 |
/** |
|
1886 |
* Finds the matching schema among the "anyOf" schemas. |
|
1887 |
* |
|
1888 |
* @since 5.6.0 |
|
1889 |
* |
|
1890 |
* @param mixed $value The value to validate. |
|
1891 |
* @param array $args The schema array to use. |
|
1892 |
* @param string $param The parameter name, used in error messages. |
|
1893 |
* @return array|WP_Error The matching schema or WP_Error instance if all schemas do not match. |
|
1894 |
*/ |
|
1895 |
function rest_find_any_matching_schema( $value, $args, $param ) { |
|
1896 |
$errors = array(); |
|
1897 |
||
1898 |
foreach ( $args['anyOf'] as $index => $schema ) { |
|
1899 |
if ( ! isset( $schema['type'] ) && isset( $args['type'] ) ) { |
|
1900 |
$schema['type'] = $args['type']; |
|
1901 |
} |
|
1902 |
||
1903 |
$is_valid = rest_validate_value_from_schema( $value, $schema, $param ); |
|
1904 |
if ( ! is_wp_error( $is_valid ) ) { |
|
1905 |
return $schema; |
|
1906 |
} |
|
1907 |
||
1908 |
$errors[] = array( |
|
1909 |
'error_object' => $is_valid, |
|
1910 |
'schema' => $schema, |
|
1911 |
'index' => $index, |
|
1912 |
); |
|
1913 |
} |
|
1914 |
||
1915 |
return rest_get_combining_operation_error( $value, $param, $errors ); |
|
1916 |
} |
|
1917 |
||
1918 |
/** |
|
1919 |
* Finds the matching schema among the "oneOf" schemas. |
|
1920 |
* |
|
1921 |
* @since 5.6.0 |
|
1922 |
* |
|
1923 |
* @param mixed $value The value to validate. |
|
1924 |
* @param array $args The schema array to use. |
|
1925 |
* @param string $param The parameter name, used in error messages. |
|
1926 |
* @param bool $stop_after_first_match Optional. Whether the process should stop after the first successful match. |
|
1927 |
* @return array|WP_Error The matching schema or WP_Error instance if the number of matching schemas is not equal to one. |
|
1928 |
*/ |
|
1929 |
function rest_find_one_matching_schema( $value, $args, $param, $stop_after_first_match = false ) { |
|
1930 |
$matching_schemas = array(); |
|
1931 |
$errors = array(); |
|
1932 |
||
1933 |
foreach ( $args['oneOf'] as $index => $schema ) { |
|
1934 |
if ( ! isset( $schema['type'] ) && isset( $args['type'] ) ) { |
|
1935 |
$schema['type'] = $args['type']; |
|
1936 |
} |
|
1937 |
||
1938 |
$is_valid = rest_validate_value_from_schema( $value, $schema, $param ); |
|
1939 |
if ( ! is_wp_error( $is_valid ) ) { |
|
1940 |
if ( $stop_after_first_match ) { |
|
1941 |
return $schema; |
|
1942 |
} |
|
1943 |
||
1944 |
$matching_schemas[] = array( |
|
1945 |
'schema_object' => $schema, |
|
1946 |
'index' => $index, |
|
1947 |
); |
|
1948 |
} else { |
|
1949 |
$errors[] = array( |
|
1950 |
'error_object' => $is_valid, |
|
1951 |
'schema' => $schema, |
|
1952 |
'index' => $index, |
|
1953 |
); |
|
1954 |
} |
|
1955 |
} |
|
1956 |
||
1957 |
if ( ! $matching_schemas ) { |
|
1958 |
return rest_get_combining_operation_error( $value, $param, $errors ); |
|
1959 |
} |
|
1960 |
||
1961 |
if ( count( $matching_schemas ) > 1 ) { |
|
1962 |
$schema_positions = array(); |
|
1963 |
$schema_titles = array(); |
|
1964 |
||
1965 |
foreach ( $matching_schemas as $schema ) { |
|
1966 |
$schema_positions[] = $schema['index']; |
|
1967 |
||
1968 |
if ( isset( $schema['schema_object']['title'] ) ) { |
|
1969 |
$schema_titles[] = $schema['schema_object']['title']; |
|
1970 |
} |
|
1971 |
} |
|
1972 |
||
1973 |
// If each schema has a title, include those titles in the error message. |
|
1974 |
if ( count( $schema_titles ) === count( $matching_schemas ) ) { |
|
1975 |
return new WP_Error( |
|
1976 |
'rest_one_of_multiple_matches', |
|
1977 |
/* translators: 1: Parameter, 2: Schema titles. */ |
|
1978 |
wp_sprintf( __( '%1$s matches %2$l, but should match only one.' ), $param, $schema_titles ), |
|
1979 |
array( 'positions' => $schema_positions ) |
|
1980 |
); |
|
1981 |
} |
|
1982 |
||
1983 |
return new WP_Error( |
|
1984 |
'rest_one_of_multiple_matches', |
|
1985 |
/* translators: %s: Parameter. */ |
|
1986 |
sprintf( __( '%s matches more than one of the expected formats.' ), $param ), |
|
1987 |
array( 'positions' => $schema_positions ) |
|
1988 |
); |
|
1989 |
} |
|
1990 |
||
1991 |
return $matching_schemas[0]['schema_object']; |
|
1992 |
} |
|
1993 |
||
1994 |
/** |
|
1995 |
* Checks the equality of two values, following JSON Schema semantics. |
|
1996 |
* |
|
1997 |
* Property order is ignored for objects. |
|
1998 |
* |
|
1999 |
* Values must have been previously sanitized/coerced to their native types. |
|
2000 |
* |
|
2001 |
* @since 5.7.0 |
|
2002 |
* |
|
2003 |
* @param mixed $value1 The first value to check. |
|
2004 |
* @param mixed $value2 The second value to check. |
|
2005 |
* @return bool True if the values are equal or false otherwise. |
|
2006 |
*/ |
|
2007 |
function rest_are_values_equal( $value1, $value2 ) { |
|
2008 |
if ( is_array( $value1 ) && is_array( $value2 ) ) { |
|
2009 |
if ( count( $value1 ) !== count( $value2 ) ) { |
|
2010 |
return false; |
|
2011 |
} |
|
2012 |
||
2013 |
foreach ( $value1 as $index => $value ) { |
|
2014 |
if ( ! array_key_exists( $index, $value2 ) || ! rest_are_values_equal( $value, $value2[ $index ] ) ) { |
|
2015 |
return false; |
|
2016 |
} |
|
2017 |
} |
|
2018 |
||
2019 |
return true; |
|
2020 |
} |
|
2021 |
||
2022 |
if ( is_int( $value1 ) && is_float( $value2 ) |
|
2023 |
|| is_float( $value1 ) && is_int( $value2 ) |
|
2024 |
) { |
|
2025 |
return (float) $value1 === (float) $value2; |
|
2026 |
} |
|
2027 |
||
2028 |
return $value1 === $value2; |
|
2029 |
} |
|
2030 |
||
2031 |
/** |
|
2032 |
* Validates that the given value is a member of the JSON Schema "enum". |
|
2033 |
* |
|
2034 |
* @since 5.7.0 |
|
2035 |
* |
|
2036 |
* @param mixed $value The value to validate. |
|
2037 |
* @param array $args The schema array to use. |
|
2038 |
* @param string $param The parameter name, used in error messages. |
|
2039 |
* @return true|WP_Error True if the "enum" contains the value or a WP_Error instance otherwise. |
|
2040 |
*/ |
|
2041 |
function rest_validate_enum( $value, $args, $param ) { |
|
2042 |
$sanitized_value = rest_sanitize_value_from_schema( $value, $args, $param ); |
|
2043 |
if ( is_wp_error( $sanitized_value ) ) { |
|
2044 |
return $sanitized_value; |
|
2045 |
} |
|
2046 |
||
2047 |
foreach ( $args['enum'] as $enum_value ) { |
|
2048 |
if ( rest_are_values_equal( $sanitized_value, $enum_value ) ) { |
|
2049 |
return true; |
|
2050 |
} |
|
2051 |
} |
|
2052 |
||
2053 |
$encoded_enum_values = array(); |
|
2054 |
foreach ( $args['enum'] as $enum_value ) { |
|
2055 |
$encoded_enum_values[] = is_scalar( $enum_value ) ? $enum_value : wp_json_encode( $enum_value ); |
|
2056 |
} |
|
2057 |
||
2058 |
if ( count( $encoded_enum_values ) === 1 ) { |
|
2059 |
/* translators: 1: Parameter, 2: Valid values. */ |
|
2060 |
return new WP_Error( 'rest_not_in_enum', wp_sprintf( __( '%1$s is not %2$s.' ), $param, $encoded_enum_values[0] ) ); |
|
2061 |
} |
|
2062 |
||
2063 |
/* translators: 1: Parameter, 2: List of valid values. */ |
|
2064 |
return new WP_Error( 'rest_not_in_enum', wp_sprintf( __( '%1$s is not one of %2$l.' ), $param, $encoded_enum_values ) ); |
|
2065 |
} |
|
2066 |
||
2067 |
/** |
|
2068 |
* Get all valid JSON schema properties. |
|
2069 |
* |
|
2070 |
* @since 5.6.0 |
|
2071 |
* |
|
2072 |
* @return string[] All valid JSON schema properties. |
|
2073 |
*/ |
|
2074 |
function rest_get_allowed_schema_keywords() { |
|
2075 |
return array( |
|
2076 |
'title', |
|
2077 |
'description', |
|
2078 |
'default', |
|
2079 |
'type', |
|
2080 |
'format', |
|
2081 |
'enum', |
|
2082 |
'items', |
|
2083 |
'properties', |
|
2084 |
'additionalProperties', |
|
2085 |
'patternProperties', |
|
2086 |
'minProperties', |
|
2087 |
'maxProperties', |
|
2088 |
'minimum', |
|
2089 |
'maximum', |
|
2090 |
'exclusiveMinimum', |
|
2091 |
'exclusiveMaximum', |
|
2092 |
'multipleOf', |
|
2093 |
'minLength', |
|
2094 |
'maxLength', |
|
2095 |
'pattern', |
|
2096 |
'minItems', |
|
2097 |
'maxItems', |
|
2098 |
'uniqueItems', |
|
2099 |
'anyOf', |
|
2100 |
'oneOf', |
|
2101 |
); |
|
2102 |
} |
|
2103 |
||
2104 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2105 |
* Validate a value based on a schema. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2106 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2107 |
* @since 4.7.0 |
16 | 2108 |
* @since 4.9.0 Support the "object" type. |
2109 |
* @since 5.2.0 Support validating "additionalProperties" against a schema. |
|
2110 |
* @since 5.3.0 Support multiple types. |
|
2111 |
* @since 5.4.0 Convert an empty string to an empty object. |
|
2112 |
* @since 5.5.0 Add the "uuid" and "hex-color" formats. |
|
2113 |
* Support the "minLength", "maxLength" and "pattern" keywords for strings. |
|
2114 |
* Support the "minItems", "maxItems" and "uniqueItems" keywords for arrays. |
|
2115 |
* Validate required properties. |
|
18 | 2116 |
* @since 5.6.0 Support the "minProperties" and "maxProperties" keywords for objects. |
2117 |
* Support the "multipleOf" keyword for numbers and integers. |
|
2118 |
* Support the "patternProperties" keyword for objects. |
|
2119 |
* Support the "anyOf" and "oneOf" keywords. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2120 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2121 |
* @param mixed $value The value to validate. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2122 |
* @param array $args Schema array to use for validation. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2123 |
* @param string $param The parameter name, used in error messages. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2124 |
* @return true|WP_Error |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2125 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2126 |
function rest_validate_value_from_schema( $value, $args, $param = '' ) { |
18 | 2127 |
if ( isset( $args['anyOf'] ) ) { |
2128 |
$matching_schema = rest_find_any_matching_schema( $value, $args, $param ); |
|
2129 |
if ( is_wp_error( $matching_schema ) ) { |
|
2130 |
return $matching_schema; |
|
2131 |
} |
|
2132 |
||
2133 |
if ( ! isset( $args['type'] ) && isset( $matching_schema['type'] ) ) { |
|
2134 |
$args['type'] = $matching_schema['type']; |
|
2135 |
} |
|
2136 |
} |
|
2137 |
||
2138 |
if ( isset( $args['oneOf'] ) ) { |
|
2139 |
$matching_schema = rest_find_one_matching_schema( $value, $args, $param ); |
|
2140 |
if ( is_wp_error( $matching_schema ) ) { |
|
2141 |
return $matching_schema; |
|
2142 |
} |
|
2143 |
||
2144 |
if ( ! isset( $args['type'] ) && isset( $matching_schema['type'] ) ) { |
|
2145 |
$args['type'] = $matching_schema['type']; |
|
2146 |
} |
|
2147 |
} |
|
2148 |
||
16 | 2149 |
$allowed_types = array( 'array', 'object', 'string', 'number', 'integer', 'boolean', 'null' ); |
2150 |
||
2151 |
if ( ! isset( $args['type'] ) ) { |
|
18 | 2152 |
/* translators: %s: Parameter. */ |
16 | 2153 |
_doing_it_wrong( __FUNCTION__, sprintf( __( 'The "type" schema keyword for %s is required.' ), $param ), '5.5.0' ); |
2154 |
} |
|
2155 |
||
2156 |
if ( is_array( $args['type'] ) ) { |
|
2157 |
$best_type = rest_handle_multi_type_schema( $value, $args, $param ); |
|
2158 |
||
2159 |
if ( ! $best_type ) { |
|
18 | 2160 |
return new WP_Error( |
2161 |
'rest_invalid_type', |
|
2162 |
/* translators: 1: Parameter, 2: List of types. */ |
|
2163 |
sprintf( __( '%1$s is not of type %2$s.' ), $param, implode( ',', $args['type'] ) ), |
|
2164 |
array( 'param' => $param ) |
|
2165 |
); |
|
16 | 2166 |
} |
2167 |
||
2168 |
$args['type'] = $best_type; |
|
2169 |
} |
|
2170 |
||
2171 |
if ( ! in_array( $args['type'], $allowed_types, true ) ) { |
|
2172 |
_doing_it_wrong( |
|
2173 |
__FUNCTION__, |
|
18 | 2174 |
/* translators: 1: Parameter, 2: The list of allowed types. */ |
16 | 2175 |
wp_sprintf( __( 'The "type" schema keyword for %1$s can only be one of the built-in types: %2$l.' ), $param, $allowed_types ), |
2176 |
'5.5.0' |
|
2177 |
); |
|
2178 |
} |
|
2179 |
||
18 | 2180 |
switch ( $args['type'] ) { |
2181 |
case 'null': |
|
2182 |
$is_valid = rest_validate_null_value_from_schema( $value, $param ); |
|
2183 |
break; |
|
2184 |
case 'boolean': |
|
2185 |
$is_valid = rest_validate_boolean_value_from_schema( $value, $param ); |
|
2186 |
break; |
|
2187 |
case 'object': |
|
2188 |
$is_valid = rest_validate_object_value_from_schema( $value, $args, $param ); |
|
2189 |
break; |
|
2190 |
case 'array': |
|
2191 |
$is_valid = rest_validate_array_value_from_schema( $value, $args, $param ); |
|
2192 |
break; |
|
2193 |
case 'number': |
|
2194 |
$is_valid = rest_validate_number_value_from_schema( $value, $args, $param ); |
|
2195 |
break; |
|
2196 |
case 'string': |
|
2197 |
$is_valid = rest_validate_string_value_from_schema( $value, $args, $param ); |
|
2198 |
break; |
|
2199 |
case 'integer': |
|
2200 |
$is_valid = rest_validate_integer_value_from_schema( $value, $args, $param ); |
|
2201 |
break; |
|
2202 |
default: |
|
2203 |
$is_valid = true; |
|
2204 |
break; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2205 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2206 |
|
18 | 2207 |
if ( is_wp_error( $is_valid ) ) { |
2208 |
return $is_valid; |
|
16 | 2209 |
} |
2210 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2211 |
if ( ! empty( $args['enum'] ) ) { |
18 | 2212 |
$enum_contains_value = rest_validate_enum( $value, $args, $param ); |
2213 |
if ( is_wp_error( $enum_contains_value ) ) { |
|
2214 |
return $enum_contains_value; |
|
16 | 2215 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2216 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2217 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2218 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2219 |
* The "format" keyword should only be applied to strings. However, for backward compatibility, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2220 |
* we allow the "format" keyword if the type keyword was not specified, or was set to an invalid value. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2221 |
*/ |
16 | 2222 |
if ( isset( $args['format'] ) |
2223 |
&& ( ! isset( $args['type'] ) || 'string' === $args['type'] || ! in_array( $args['type'], $allowed_types, true ) ) |
|
2224 |
) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2225 |
switch ( $args['format'] ) { |
16 | 2226 |
case 'hex-color': |
2227 |
if ( ! rest_parse_hex_color( $value ) ) { |
|
2228 |
return new WP_Error( 'rest_invalid_hex_color', __( 'Invalid hex color.' ) ); |
|
2229 |
} |
|
2230 |
break; |
|
2231 |
||
9 | 2232 |
case 'date-time': |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2233 |
if ( ! rest_parse_date( $value ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2234 |
return new WP_Error( 'rest_invalid_date', __( 'Invalid date.' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2235 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2236 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2237 |
|
9 | 2238 |
case 'email': |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2239 |
if ( ! is_email( $value ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2240 |
return new WP_Error( 'rest_invalid_email', __( 'Invalid email address.' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2241 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2242 |
break; |
9 | 2243 |
case 'ip': |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2244 |
if ( ! rest_is_ip_address( $value ) ) { |
16 | 2245 |
/* translators: %s: IP address. */ |
18 | 2246 |
return new WP_Error( 'rest_invalid_ip', sprintf( __( '%s is not a valid IP address.' ), $param ) ); |
16 | 2247 |
} |
2248 |
break; |
|
2249 |
case 'uuid': |
|
2250 |
if ( ! wp_is_uuid( $value ) ) { |
|
18 | 2251 |
/* translators: %s: The name of a JSON field expecting a valid UUID. */ |
16 | 2252 |
return new WP_Error( 'rest_invalid_uuid', sprintf( __( '%s is not a valid UUID.' ), $param ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2253 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2254 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2255 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2256 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2257 |
|
18 | 2258 |
return true; |
2259 |
} |
|
2260 |
||
2261 |
/** |
|
2262 |
* Validates a null value based on a schema. |
|
2263 |
* |
|
2264 |
* @since 5.7.0 |
|
2265 |
* |
|
2266 |
* @param mixed $value The value to validate. |
|
2267 |
* @param string $param The parameter name, used in error messages. |
|
2268 |
* @return true|WP_Error |
|
2269 |
*/ |
|
2270 |
function rest_validate_null_value_from_schema( $value, $param ) { |
|
2271 |
if ( null !== $value ) { |
|
2272 |
return new WP_Error( |
|
2273 |
'rest_invalid_type', |
|
2274 |
/* translators: 1: Parameter, 2: Type name. */ |
|
2275 |
sprintf( __( '%1$s is not of type %2$s.' ), $param, 'null' ), |
|
2276 |
array( 'param' => $param ) |
|
2277 |
); |
|
2278 |
} |
|
2279 |
||
2280 |
return true; |
|
2281 |
} |
|
2282 |
||
2283 |
/** |
|
2284 |
* Validates a boolean value based on a schema. |
|
2285 |
* |
|
2286 |
* @since 5.7.0 |
|
2287 |
* |
|
2288 |
* @param mixed $value The value to validate. |
|
2289 |
* @param string $param The parameter name, used in error messages. |
|
2290 |
* @return true|WP_Error |
|
2291 |
*/ |
|
2292 |
function rest_validate_boolean_value_from_schema( $value, $param ) { |
|
2293 |
if ( ! rest_is_boolean( $value ) ) { |
|
2294 |
return new WP_Error( |
|
2295 |
'rest_invalid_type', |
|
2296 |
/* translators: 1: Parameter, 2: Type name. */ |
|
2297 |
sprintf( __( '%1$s is not of type %2$s.' ), $param, 'boolean' ), |
|
2298 |
array( 'param' => $param ) |
|
2299 |
); |
|
2300 |
} |
|
2301 |
||
2302 |
return true; |
|
2303 |
} |
|
2304 |
||
2305 |
/** |
|
2306 |
* Validates an object value based on a schema. |
|
2307 |
* |
|
2308 |
* @since 5.7.0 |
|
2309 |
* |
|
2310 |
* @param mixed $value The value to validate. |
|
2311 |
* @param array $args Schema array to use for validation. |
|
2312 |
* @param string $param The parameter name, used in error messages. |
|
2313 |
* @return true|WP_Error |
|
2314 |
*/ |
|
2315 |
function rest_validate_object_value_from_schema( $value, $args, $param ) { |
|
2316 |
if ( ! rest_is_object( $value ) ) { |
|
2317 |
return new WP_Error( |
|
2318 |
'rest_invalid_type', |
|
2319 |
/* translators: 1: Parameter, 2: Type name. */ |
|
2320 |
sprintf( __( '%1$s is not of type %2$s.' ), $param, 'object' ), |
|
2321 |
array( 'param' => $param ) |
|
2322 |
); |
|
2323 |
} |
|
2324 |
||
2325 |
$value = rest_sanitize_object( $value ); |
|
2326 |
||
2327 |
if ( isset( $args['required'] ) && is_array( $args['required'] ) ) { // schema version 4 |
|
2328 |
foreach ( $args['required'] as $name ) { |
|
2329 |
if ( ! array_key_exists( $name, $value ) ) { |
|
2330 |
return new WP_Error( |
|
2331 |
'rest_property_required', |
|
2332 |
/* translators: 1: Property of an object, 2: Parameter. */ |
|
2333 |
sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param ) |
|
2334 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2335 |
} |
18 | 2336 |
} |
2337 |
} elseif ( isset( $args['properties'] ) ) { // schema version 3 |
|
2338 |
foreach ( $args['properties'] as $name => $property ) { |
|
2339 |
if ( isset( $property['required'] ) && true === $property['required'] && ! array_key_exists( $name, $value ) ) { |
|
2340 |
return new WP_Error( |
|
2341 |
'rest_property_required', |
|
2342 |
/* translators: 1: Property of an object, 2: Parameter. */ |
|
2343 |
sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param ) |
|
2344 |
); |
|
2345 |
} |
|
2346 |
} |
|
2347 |
} |
|
2348 |
||
2349 |
foreach ( $value as $property => $v ) { |
|
2350 |
if ( isset( $args['properties'][ $property ] ) ) { |
|
2351 |
$is_valid = rest_validate_value_from_schema( $v, $args['properties'][ $property ], $param . '[' . $property . ']' ); |
|
2352 |
if ( is_wp_error( $is_valid ) ) { |
|
2353 |
return $is_valid; |
|
2354 |
} |
|
2355 |
continue; |
|
2356 |
} |
|
2357 |
||
2358 |
$pattern_property_schema = rest_find_matching_pattern_property_schema( $property, $args ); |
|
2359 |
if ( null !== $pattern_property_schema ) { |
|
2360 |
$is_valid = rest_validate_value_from_schema( $v, $pattern_property_schema, $param . '[' . $property . ']' ); |
|
2361 |
if ( is_wp_error( $is_valid ) ) { |
|
2362 |
return $is_valid; |
|
2363 |
} |
|
2364 |
continue; |
|
2365 |
} |
|
2366 |
||
2367 |
if ( isset( $args['additionalProperties'] ) ) { |
|
2368 |
if ( false === $args['additionalProperties'] ) { |
|
2369 |
return new WP_Error( |
|
2370 |
'rest_additional_properties_forbidden', |
|
2371 |
/* translators: %s: Property of an object. */ |
|
2372 |
sprintf( __( '%1$s is not a valid property of Object.' ), $property ) |
|
2373 |
); |
|
2374 |
} |
|
2375 |
||
2376 |
if ( is_array( $args['additionalProperties'] ) ) { |
|
2377 |
$is_valid = rest_validate_value_from_schema( $v, $args['additionalProperties'], $param . '[' . $property . ']' ); |
|
2378 |
if ( is_wp_error( $is_valid ) ) { |
|
2379 |
return $is_valid; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2380 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2381 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2382 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2383 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2384 |
|
18 | 2385 |
if ( isset( $args['minProperties'] ) && count( $value ) < $args['minProperties'] ) { |
2386 |
return new WP_Error( |
|
2387 |
'rest_too_few_properties', |
|
2388 |
sprintf( |
|
2389 |
/* translators: 1: Parameter, 2: Number. */ |
|
2390 |
_n( |
|
2391 |
'%1$s must contain at least %2$s property.', |
|
2392 |
'%1$s must contain at least %2$s properties.', |
|
2393 |
$args['minProperties'] |
|
2394 |
), |
|
2395 |
$param, |
|
2396 |
number_format_i18n( $args['minProperties'] ) |
|
2397 |
) |
|
2398 |
); |
|
2399 |
} |
|
2400 |
||
2401 |
if ( isset( $args['maxProperties'] ) && count( $value ) > $args['maxProperties'] ) { |
|
2402 |
return new WP_Error( |
|
2403 |
'rest_too_many_properties', |
|
2404 |
sprintf( |
|
2405 |
/* translators: 1: Parameter, 2: Number. */ |
|
2406 |
_n( |
|
2407 |
'%1$s must contain at most %2$s property.', |
|
2408 |
'%1$s must contain at most %2$s properties.', |
|
2409 |
$args['maxProperties'] |
|
2410 |
), |
|
2411 |
$param, |
|
2412 |
number_format_i18n( $args['maxProperties'] ) |
|
2413 |
) |
|
2414 |
); |
|
2415 |
} |
|
2416 |
||
2417 |
return true; |
|
2418 |
} |
|
2419 |
||
2420 |
/** |
|
2421 |
* Validates an array value based on a schema. |
|
2422 |
* |
|
2423 |
* @since 5.7.0 |
|
2424 |
* |
|
2425 |
* @param mixed $value The value to validate. |
|
2426 |
* @param array $args Schema array to use for validation. |
|
2427 |
* @param string $param The parameter name, used in error messages. |
|
2428 |
* @return true|WP_Error |
|
2429 |
*/ |
|
2430 |
function rest_validate_array_value_from_schema( $value, $args, $param ) { |
|
2431 |
if ( ! rest_is_array( $value ) ) { |
|
2432 |
return new WP_Error( |
|
2433 |
'rest_invalid_type', |
|
2434 |
/* translators: 1: Parameter, 2: Type name. */ |
|
2435 |
sprintf( __( '%1$s is not of type %2$s.' ), $param, 'array' ), |
|
2436 |
array( 'param' => $param ) |
|
2437 |
); |
|
2438 |
} |
|
2439 |
||
2440 |
$value = rest_sanitize_array( $value ); |
|
2441 |
||
2442 |
if ( isset( $args['items'] ) ) { |
|
2443 |
foreach ( $value as $index => $v ) { |
|
2444 |
$is_valid = rest_validate_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' ); |
|
2445 |
if ( is_wp_error( $is_valid ) ) { |
|
2446 |
return $is_valid; |
|
2447 |
} |
|
2448 |
} |
|
2449 |
} |
|
2450 |
||
2451 |
if ( isset( $args['minItems'] ) && count( $value ) < $args['minItems'] ) { |
|
2452 |
return new WP_Error( |
|
2453 |
'rest_too_few_items', |
|
2454 |
sprintf( |
|
2455 |
/* translators: 1: Parameter, 2: Number. */ |
|
2456 |
_n( |
|
2457 |
'%1$s must contain at least %2$s item.', |
|
2458 |
'%1$s must contain at least %2$s items.', |
|
2459 |
$args['minItems'] |
|
2460 |
), |
|
2461 |
$param, |
|
2462 |
number_format_i18n( $args['minItems'] ) |
|
2463 |
) |
|
2464 |
); |
|
2465 |
} |
|
2466 |
||
2467 |
if ( isset( $args['maxItems'] ) && count( $value ) > $args['maxItems'] ) { |
|
2468 |
return new WP_Error( |
|
2469 |
'rest_too_many_items', |
|
2470 |
sprintf( |
|
2471 |
/* translators: 1: Parameter, 2: Number. */ |
|
2472 |
_n( |
|
2473 |
'%1$s must contain at most %2$s item.', |
|
2474 |
'%1$s must contain at most %2$s items.', |
|
2475 |
$args['maxItems'] |
|
2476 |
), |
|
2477 |
$param, |
|
2478 |
number_format_i18n( $args['maxItems'] ) |
|
2479 |
) |
|
2480 |
); |
|
2481 |
} |
|
2482 |
||
2483 |
if ( ! empty( $args['uniqueItems'] ) && ! rest_validate_array_contains_unique_items( $value ) ) { |
|
2484 |
/* translators: %s: Parameter. */ |
|
2485 |
return new WP_Error( 'rest_duplicate_items', sprintf( __( '%s has duplicate items.' ), $param ) ); |
|
2486 |
} |
|
2487 |
||
2488 |
return true; |
|
2489 |
} |
|
2490 |
||
2491 |
/** |
|
2492 |
* Validates a number value based on a schema. |
|
2493 |
* |
|
2494 |
* @since 5.7.0 |
|
2495 |
* |
|
2496 |
* @param mixed $value The value to validate. |
|
2497 |
* @param array $args Schema array to use for validation. |
|
2498 |
* @param string $param The parameter name, used in error messages. |
|
2499 |
* @return true|WP_Error |
|
2500 |
*/ |
|
2501 |
function rest_validate_number_value_from_schema( $value, $args, $param ) { |
|
2502 |
if ( ! is_numeric( $value ) ) { |
|
2503 |
return new WP_Error( |
|
2504 |
'rest_invalid_type', |
|
2505 |
/* translators: 1: Parameter, 2: Type name. */ |
|
2506 |
sprintf( __( '%1$s is not of type %2$s.' ), $param, $args['type'] ), |
|
2507 |
array( 'param' => $param ) |
|
2508 |
); |
|
2509 |
} |
|
2510 |
||
2511 |
if ( isset( $args['multipleOf'] ) && fmod( $value, $args['multipleOf'] ) !== 0.0 ) { |
|
2512 |
return new WP_Error( |
|
2513 |
'rest_invalid_multiple', |
|
2514 |
/* translators: 1: Parameter, 2: Multiplier. */ |
|
2515 |
sprintf( __( '%1$s must be a multiple of %2$s.' ), $param, $args['multipleOf'] ) |
|
2516 |
); |
|
2517 |
} |
|
2518 |
||
2519 |
if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) { |
|
2520 |
if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) { |
|
2521 |
return new WP_Error( |
|
2522 |
'rest_out_of_bounds', |
|
2523 |
/* translators: 1: Parameter, 2: Minimum number. */ |
|
2524 |
sprintf( __( '%1$s must be greater than %2$d' ), $param, $args['minimum'] ) |
|
2525 |
); |
|
2526 |
} |
|
2527 |
||
2528 |
if ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) { |
|
2529 |
return new WP_Error( |
|
2530 |
'rest_out_of_bounds', |
|
2531 |
/* translators: 1: Parameter, 2: Minimum number. */ |
|
2532 |
sprintf( __( '%1$s must be greater than or equal to %2$d' ), $param, $args['minimum'] ) |
|
2533 |
); |
|
2534 |
} |
|
2535 |
} |
|
2536 |
||
2537 |
if ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) { |
|
2538 |
if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) { |
|
2539 |
return new WP_Error( |
|
2540 |
'rest_out_of_bounds', |
|
2541 |
/* translators: 1: Parameter, 2: Maximum number. */ |
|
2542 |
sprintf( __( '%1$s must be less than %2$d' ), $param, $args['maximum'] ) |
|
2543 |
); |
|
2544 |
} |
|
2545 |
||
2546 |
if ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) { |
|
2547 |
return new WP_Error( |
|
2548 |
'rest_out_of_bounds', |
|
2549 |
/* translators: 1: Parameter, 2: Maximum number. */ |
|
2550 |
sprintf( __( '%1$s must be less than or equal to %2$d' ), $param, $args['maximum'] ) |
|
2551 |
); |
|
2552 |
} |
|
2553 |
} |
|
2554 |
||
2555 |
if ( isset( $args['minimum'], $args['maximum'] ) ) { |
|
2556 |
if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) { |
|
2557 |
if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) { |
|
2558 |
return new WP_Error( |
|
2559 |
'rest_out_of_bounds', |
|
2560 |
sprintf( |
|
2561 |
/* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */ |
|
2562 |
__( '%1$s must be between %2$d (exclusive) and %3$d (exclusive)' ), |
|
2563 |
$param, |
|
2564 |
$args['minimum'], |
|
2565 |
$args['maximum'] |
|
2566 |
) |
|
2567 |
); |
|
2568 |
} |
|
2569 |
} |
|
2570 |
||
2571 |
if ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) { |
|
2572 |
if ( $value > $args['maximum'] || $value <= $args['minimum'] ) { |
|
2573 |
return new WP_Error( |
|
2574 |
'rest_out_of_bounds', |
|
2575 |
sprintf( |
|
2576 |
/* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */ |
|
2577 |
__( '%1$s must be between %2$d (exclusive) and %3$d (inclusive)' ), |
|
2578 |
$param, |
|
2579 |
$args['minimum'], |
|
2580 |
$args['maximum'] |
|
2581 |
) |
|
2582 |
); |
|
2583 |
} |
|
2584 |
} |
|
2585 |
||
2586 |
if ( ! empty( $args['exclusiveMaximum'] ) && empty( $args['exclusiveMinimum'] ) ) { |
|
2587 |
if ( $value >= $args['maximum'] || $value < $args['minimum'] ) { |
|
2588 |
return new WP_Error( |
|
2589 |
'rest_out_of_bounds', |
|
2590 |
sprintf( |
|
2591 |
/* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */ |
|
2592 |
__( '%1$s must be between %2$d (inclusive) and %3$d (exclusive)' ), |
|
2593 |
$param, |
|
2594 |
$args['minimum'], |
|
2595 |
$args['maximum'] |
|
2596 |
) |
|
2597 |
); |
|
2598 |
} |
|
2599 |
} |
|
2600 |
||
2601 |
if ( empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) { |
|
2602 |
if ( $value > $args['maximum'] || $value < $args['minimum'] ) { |
|
2603 |
return new WP_Error( |
|
2604 |
'rest_out_of_bounds', |
|
2605 |
sprintf( |
|
2606 |
/* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */ |
|
2607 |
__( '%1$s must be between %2$d (inclusive) and %3$d (inclusive)' ), |
|
2608 |
$param, |
|
2609 |
$args['minimum'], |
|
2610 |
$args['maximum'] |
|
2611 |
) |
|
2612 |
); |
|
2613 |
} |
|
2614 |
} |
|
2615 |
} |
|
2616 |
||
2617 |
return true; |
|
2618 |
} |
|
2619 |
||
2620 |
/** |
|
2621 |
* Validates a string value based on a schema. |
|
2622 |
* |
|
2623 |
* @since 5.7.0 |
|
2624 |
* |
|
2625 |
* @param mixed $value The value to validate. |
|
2626 |
* @param array $args Schema array to use for validation. |
|
2627 |
* @param string $param The parameter name, used in error messages. |
|
2628 |
* @return true|WP_Error |
|
2629 |
*/ |
|
2630 |
function rest_validate_string_value_from_schema( $value, $args, $param ) { |
|
2631 |
if ( ! is_string( $value ) ) { |
|
2632 |
return new WP_Error( |
|
2633 |
'rest_invalid_type', |
|
2634 |
/* translators: 1: Parameter, 2: Type name. */ |
|
2635 |
sprintf( __( '%1$s is not of type %2$s.' ), $param, 'string' ), |
|
2636 |
array( 'param' => $param ) |
|
2637 |
); |
|
2638 |
} |
|
2639 |
||
2640 |
if ( isset( $args['minLength'] ) && mb_strlen( $value ) < $args['minLength'] ) { |
|
2641 |
return new WP_Error( |
|
2642 |
'rest_too_short', |
|
2643 |
sprintf( |
|
2644 |
/* translators: 1: Parameter, 2: Number of characters. */ |
|
2645 |
_n( |
|
2646 |
'%1$s must be at least %2$s character long.', |
|
2647 |
'%1$s must be at least %2$s characters long.', |
|
2648 |
$args['minLength'] |
|
2649 |
), |
|
2650 |
$param, |
|
2651 |
number_format_i18n( $args['minLength'] ) |
|
2652 |
) |
|
2653 |
); |
|
2654 |
} |
|
2655 |
||
2656 |
if ( isset( $args['maxLength'] ) && mb_strlen( $value ) > $args['maxLength'] ) { |
|
2657 |
return new WP_Error( |
|
2658 |
'rest_too_long', |
|
2659 |
sprintf( |
|
2660 |
/* translators: 1: Parameter, 2: Number of characters. */ |
|
2661 |
_n( |
|
2662 |
'%1$s must be at most %2$s character long.', |
|
2663 |
'%1$s must be at most %2$s characters long.', |
|
2664 |
$args['maxLength'] |
|
2665 |
), |
|
2666 |
$param, |
|
2667 |
number_format_i18n( $args['maxLength'] ) |
|
2668 |
) |
|
2669 |
); |
|
2670 |
} |
|
2671 |
||
2672 |
if ( isset( $args['pattern'] ) && ! rest_validate_json_schema_pattern( $args['pattern'], $value ) ) { |
|
2673 |
return new WP_Error( |
|
2674 |
'rest_invalid_pattern', |
|
2675 |
/* translators: 1: Parameter, 2: Pattern. */ |
|
2676 |
sprintf( __( '%1$s does not match pattern %2$s.' ), $param, $args['pattern'] ) |
|
2677 |
); |
|
2678 |
} |
|
2679 |
||
2680 |
return true; |
|
2681 |
} |
|
2682 |
||
2683 |
/** |
|
2684 |
* Validates an integer value based on a schema. |
|
2685 |
* |
|
2686 |
* @since 5.7.0 |
|
2687 |
* |
|
2688 |
* @param mixed $value The value to validate. |
|
2689 |
* @param array $args Schema array to use for validation. |
|
2690 |
* @param string $param The parameter name, used in error messages. |
|
2691 |
* @return true|WP_Error |
|
2692 |
*/ |
|
2693 |
function rest_validate_integer_value_from_schema( $value, $args, $param ) { |
|
2694 |
$is_valid_number = rest_validate_number_value_from_schema( $value, $args, $param ); |
|
2695 |
if ( is_wp_error( $is_valid_number ) ) { |
|
2696 |
return $is_valid_number; |
|
2697 |
} |
|
2698 |
||
2699 |
if ( ! rest_is_integer( $value ) ) { |
|
2700 |
return new WP_Error( |
|
2701 |
'rest_invalid_type', |
|
2702 |
/* translators: 1: Parameter, 2: Type name. */ |
|
2703 |
sprintf( __( '%1$s is not of type %2$s.' ), $param, 'integer' ), |
|
2704 |
array( 'param' => $param ) |
|
2705 |
); |
|
2706 |
} |
|
2707 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2708 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2709 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2710 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2711 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2712 |
* Sanitize a value based on a schema. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2713 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2714 |
* @since 4.7.0 |
16 | 2715 |
* @since 5.5.0 Added the `$param` parameter. |
18 | 2716 |
* @since 5.6.0 Support the "anyOf" and "oneOf" keywords. |
19 | 2717 |
* @since 5.9.0 Added `text-field` and `textarea-field` formats. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2718 |
* |
16 | 2719 |
* @param mixed $value The value to sanitize. |
2720 |
* @param array $args Schema array to use for sanitization. |
|
2721 |
* @param string $param The parameter name, used in error messages. |
|
2722 |
* @return mixed|WP_Error The sanitized value or a WP_Error instance if the value cannot be safely sanitized. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2723 |
*/ |
16 | 2724 |
function rest_sanitize_value_from_schema( $value, $args, $param = '' ) { |
18 | 2725 |
if ( isset( $args['anyOf'] ) ) { |
2726 |
$matching_schema = rest_find_any_matching_schema( $value, $args, $param ); |
|
2727 |
if ( is_wp_error( $matching_schema ) ) { |
|
2728 |
return $matching_schema; |
|
2729 |
} |
|
2730 |
||
2731 |
if ( ! isset( $args['type'] ) ) { |
|
2732 |
$args['type'] = $matching_schema['type']; |
|
2733 |
} |
|
2734 |
||
2735 |
$value = rest_sanitize_value_from_schema( $value, $matching_schema, $param ); |
|
2736 |
} |
|
2737 |
||
2738 |
if ( isset( $args['oneOf'] ) ) { |
|
2739 |
$matching_schema = rest_find_one_matching_schema( $value, $args, $param ); |
|
2740 |
if ( is_wp_error( $matching_schema ) ) { |
|
2741 |
return $matching_schema; |
|
2742 |
} |
|
2743 |
||
2744 |
if ( ! isset( $args['type'] ) ) { |
|
2745 |
$args['type'] = $matching_schema['type']; |
|
2746 |
} |
|
2747 |
||
2748 |
$value = rest_sanitize_value_from_schema( $value, $matching_schema, $param ); |
|
2749 |
} |
|
2750 |
||
16 | 2751 |
$allowed_types = array( 'array', 'object', 'string', 'number', 'integer', 'boolean', 'null' ); |
2752 |
||
2753 |
if ( ! isset( $args['type'] ) ) { |
|
18 | 2754 |
/* translators: %s: Parameter. */ |
16 | 2755 |
_doing_it_wrong( __FUNCTION__, sprintf( __( 'The "type" schema keyword for %s is required.' ), $param ), '5.5.0' ); |
2756 |
} |
|
2757 |
||
2758 |
if ( is_array( $args['type'] ) ) { |
|
2759 |
$best_type = rest_handle_multi_type_schema( $value, $args, $param ); |
|
2760 |
||
2761 |
if ( ! $best_type ) { |
|
2762 |
return null; |
|
2763 |
} |
|
2764 |
||
2765 |
$args['type'] = $best_type; |
|
2766 |
} |
|
2767 |
||
2768 |
if ( ! in_array( $args['type'], $allowed_types, true ) ) { |
|
2769 |
_doing_it_wrong( |
|
2770 |
__FUNCTION__, |
|
18 | 2771 |
/* translators: 1: Parameter, 2: The list of allowed types. */ |
16 | 2772 |
wp_sprintf( __( 'The "type" schema keyword for %1$s can only be one of the built-in types: %2$l.' ), $param, $allowed_types ), |
2773 |
'5.5.0' |
|
2774 |
); |
|
2775 |
} |
|
2776 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2777 |
if ( 'array' === $args['type'] ) { |
16 | 2778 |
$value = rest_sanitize_array( $value ); |
2779 |
||
2780 |
if ( ! empty( $args['items'] ) ) { |
|
2781 |
foreach ( $value as $index => $v ) { |
|
2782 |
$value[ $index ] = rest_sanitize_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' ); |
|
2783 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2784 |
} |
16 | 2785 |
|
2786 |
if ( ! empty( $args['uniqueItems'] ) && ! rest_validate_array_contains_unique_items( $value ) ) { |
|
18 | 2787 |
/* translators: %s: Parameter. */ |
2788 |
return new WP_Error( 'rest_duplicate_items', sprintf( __( '%s has duplicate items.' ), $param ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2789 |
} |
16 | 2790 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2791 |
return $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2792 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2793 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2794 |
if ( 'object' === $args['type'] ) { |
16 | 2795 |
$value = rest_sanitize_object( $value ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2796 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2797 |
foreach ( $value as $property => $v ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2798 |
if ( isset( $args['properties'][ $property ] ) ) { |
16 | 2799 |
$value[ $property ] = rest_sanitize_value_from_schema( $v, $args['properties'][ $property ], $param . '[' . $property . ']' ); |
18 | 2800 |
continue; |
2801 |
} |
|
2802 |
||
2803 |
$pattern_property_schema = rest_find_matching_pattern_property_schema( $property, $args ); |
|
2804 |
if ( null !== $pattern_property_schema ) { |
|
2805 |
$value[ $property ] = rest_sanitize_value_from_schema( $v, $pattern_property_schema, $param . '[' . $property . ']' ); |
|
2806 |
continue; |
|
2807 |
} |
|
2808 |
||
2809 |
if ( isset( $args['additionalProperties'] ) ) { |
|
16 | 2810 |
if ( false === $args['additionalProperties'] ) { |
2811 |
unset( $value[ $property ] ); |
|
2812 |
} elseif ( is_array( $args['additionalProperties'] ) ) { |
|
2813 |
$value[ $property ] = rest_sanitize_value_from_schema( $v, $args['additionalProperties'], $param . '[' . $property . ']' ); |
|
2814 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2815 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2816 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2817 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2818 |
return $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2819 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2820 |
|
16 | 2821 |
if ( 'null' === $args['type'] ) { |
2822 |
return null; |
|
2823 |
} |
|
2824 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2825 |
if ( 'integer' === $args['type'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2826 |
return (int) $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2827 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2828 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2829 |
if ( 'number' === $args['type'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2830 |
return (float) $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2831 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2832 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2833 |
if ( 'boolean' === $args['type'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2834 |
return rest_sanitize_boolean( $value ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2835 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2836 |
|
16 | 2837 |
// This behavior matches rest_validate_value_from_schema(). |
2838 |
if ( isset( $args['format'] ) |
|
2839 |
&& ( ! isset( $args['type'] ) || 'string' === $args['type'] || ! in_array( $args['type'], $allowed_types, true ) ) |
|
2840 |
) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2841 |
switch ( $args['format'] ) { |
16 | 2842 |
case 'hex-color': |
2843 |
return (string) sanitize_hex_color( $value ); |
|
2844 |
||
9 | 2845 |
case 'date-time': |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2846 |
return sanitize_text_field( $value ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2847 |
|
9 | 2848 |
case 'email': |
16 | 2849 |
// sanitize_email() validates, which would be unexpected. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2850 |
return sanitize_text_field( $value ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2851 |
|
9 | 2852 |
case 'uri': |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2853 |
return sanitize_url( $value ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2854 |
|
9 | 2855 |
case 'ip': |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2856 |
return sanitize_text_field( $value ); |
16 | 2857 |
|
2858 |
case 'uuid': |
|
2859 |
return sanitize_text_field( $value ); |
|
19 | 2860 |
|
2861 |
case 'text-field': |
|
2862 |
return sanitize_text_field( $value ); |
|
2863 |
||
2864 |
case 'textarea-field': |
|
2865 |
return sanitize_textarea_field( $value ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2866 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2867 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2868 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2869 |
if ( 'string' === $args['type'] ) { |
18 | 2870 |
return (string) $value; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2871 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2872 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2873 |
return $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2874 |
} |
9 | 2875 |
|
2876 |
/** |
|
2877 |
* Append result of internal request to REST API for purpose of preloading data to be attached to a page. |
|
2878 |
* Expected to be called in the context of `array_reduce`. |
|
2879 |
* |
|
2880 |
* @since 5.0.0 |
|
2881 |
* |
|
16 | 2882 |
* @param array $memo Reduce accumulator. |
2883 |
* @param string $path REST API path to preload. |
|
2884 |
* @return array Modified reduce accumulator. |
|
9 | 2885 |
*/ |
2886 |
function rest_preload_api_request( $memo, $path ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2887 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2888 |
* array_reduce() doesn't support passing an array in PHP 5.2, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2889 |
* so we need to make sure we start with one. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2890 |
*/ |
9 | 2891 |
if ( ! is_array( $memo ) ) { |
2892 |
$memo = array(); |
|
2893 |
} |
|
2894 |
||
2895 |
if ( empty( $path ) ) { |
|
2896 |
return $memo; |
|
2897 |
} |
|
2898 |
||
2899 |
$method = 'GET'; |
|
2900 |
if ( is_array( $path ) && 2 === count( $path ) ) { |
|
2901 |
$method = end( $path ); |
|
2902 |
$path = reset( $path ); |
|
2903 |
||
2904 |
if ( ! in_array( $method, array( 'GET', 'OPTIONS' ), true ) ) { |
|
2905 |
$method = 'GET'; |
|
2906 |
} |
|
2907 |
} |
|
2908 |
||
19 | 2909 |
$path = untrailingslashit( $path ); |
2910 |
if ( empty( $path ) ) { |
|
2911 |
$path = '/'; |
|
2912 |
} |
|
2913 |
||
9 | 2914 |
$path_parts = parse_url( $path ); |
2915 |
if ( false === $path_parts ) { |
|
2916 |
return $memo; |
|
2917 |
} |
|
2918 |
||
2919 |
$request = new WP_REST_Request( $method, $path_parts['path'] ); |
|
2920 |
if ( ! empty( $path_parts['query'] ) ) { |
|
2921 |
parse_str( $path_parts['query'], $query_params ); |
|
2922 |
$request->set_query_params( $query_params ); |
|
2923 |
} |
|
2924 |
||
2925 |
$response = rest_do_request( $request ); |
|
2926 |
if ( 200 === $response->status ) { |
|
2927 |
$server = rest_get_server(); |
|
19 | 2928 |
/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */ |
2929 |
$response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $server, $request ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2930 |
$embed = $request->has_param( '_embed' ) ? rest_parse_embed_param( $request['_embed'] ) : false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2931 |
$data = (array) $server->response_to_data( $response, $embed ); |
9 | 2932 |
|
2933 |
if ( 'OPTIONS' === $method ) { |
|
2934 |
$memo[ $method ][ $path ] = array( |
|
2935 |
'body' => $data, |
|
2936 |
'headers' => $response->headers, |
|
2937 |
); |
|
2938 |
} else { |
|
2939 |
$memo[ $path ] = array( |
|
2940 |
'body' => $data, |
|
2941 |
'headers' => $response->headers, |
|
2942 |
); |
|
2943 |
} |
|
2944 |
} |
|
2945 |
||
2946 |
return $memo; |
|
2947 |
} |
|
16 | 2948 |
|
2949 |
/** |
|
2950 |
* Parses the "_embed" parameter into the list of resources to embed. |
|
2951 |
* |
|
2952 |
* @since 5.4.0 |
|
2953 |
* |
|
2954 |
* @param string|array $embed Raw "_embed" parameter value. |
|
2955 |
* @return true|string[] Either true to embed all embeds, or a list of relations to embed. |
|
2956 |
*/ |
|
2957 |
function rest_parse_embed_param( $embed ) { |
|
2958 |
if ( ! $embed || 'true' === $embed || '1' === $embed ) { |
|
2959 |
return true; |
|
2960 |
} |
|
2961 |
||
2962 |
$rels = wp_parse_list( $embed ); |
|
2963 |
||
2964 |
if ( ! $rels ) { |
|
2965 |
return true; |
|
2966 |
} |
|
2967 |
||
2968 |
return $rels; |
|
2969 |
} |
|
2970 |
||
2971 |
/** |
|
2972 |
* Filters the response to remove any fields not available in the given context. |
|
2973 |
* |
|
2974 |
* @since 5.5.0 |
|
18 | 2975 |
* @since 5.6.0 Support the "patternProperties" keyword for objects. |
2976 |
* Support the "anyOf" and "oneOf" keywords. |
|
16 | 2977 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2978 |
* @param array|object $response_data The response data to modify. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2979 |
* @param array $schema The schema for the endpoint used to filter the response. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2980 |
* @param string $context The requested context. |
16 | 2981 |
* @return array|object The filtered response data. |
2982 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2983 |
function rest_filter_response_by_context( $response_data, $schema, $context ) { |
18 | 2984 |
if ( isset( $schema['anyOf'] ) ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2985 |
$matching_schema = rest_find_any_matching_schema( $response_data, $schema, '' ); |
18 | 2986 |
if ( ! is_wp_error( $matching_schema ) ) { |
2987 |
if ( ! isset( $schema['type'] ) ) { |
|
2988 |
$schema['type'] = $matching_schema['type']; |
|
2989 |
} |
|
2990 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2991 |
$response_data = rest_filter_response_by_context( $response_data, $matching_schema, $context ); |
18 | 2992 |
} |
2993 |
} |
|
2994 |
||
2995 |
if ( isset( $schema['oneOf'] ) ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2996 |
$matching_schema = rest_find_one_matching_schema( $response_data, $schema, '', true ); |
18 | 2997 |
if ( ! is_wp_error( $matching_schema ) ) { |
2998 |
if ( ! isset( $schema['type'] ) ) { |
|
2999 |
$schema['type'] = $matching_schema['type']; |
|
3000 |
} |
|
3001 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3002 |
$response_data = rest_filter_response_by_context( $response_data, $matching_schema, $context ); |
18 | 3003 |
} |
3004 |
} |
|
3005 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3006 |
if ( ! is_array( $response_data ) && ! is_object( $response_data ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3007 |
return $response_data; |
16 | 3008 |
} |
3009 |
||
3010 |
if ( isset( $schema['type'] ) ) { |
|
3011 |
$type = $schema['type']; |
|
3012 |
} elseif ( isset( $schema['properties'] ) ) { |
|
3013 |
$type = 'object'; // Back compat if a developer accidentally omitted the type. |
|
3014 |
} else { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3015 |
return $response_data; |
16 | 3016 |
} |
3017 |
||
3018 |
$is_array_type = 'array' === $type || ( is_array( $type ) && in_array( 'array', $type, true ) ); |
|
3019 |
$is_object_type = 'object' === $type || ( is_array( $type ) && in_array( 'object', $type, true ) ); |
|
3020 |
||
3021 |
if ( $is_array_type && $is_object_type ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3022 |
if ( rest_is_array( $response_data ) ) { |
16 | 3023 |
$is_object_type = false; |
3024 |
} else { |
|
3025 |
$is_array_type = false; |
|
3026 |
} |
|
3027 |
} |
|
3028 |
||
3029 |
$has_additional_properties = $is_object_type && isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ); |
|
3030 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3031 |
foreach ( $response_data as $key => $value ) { |
16 | 3032 |
$check = array(); |
3033 |
||
3034 |
if ( $is_array_type ) { |
|
3035 |
$check = isset( $schema['items'] ) ? $schema['items'] : array(); |
|
3036 |
} elseif ( $is_object_type ) { |
|
3037 |
if ( isset( $schema['properties'][ $key ] ) ) { |
|
3038 |
$check = $schema['properties'][ $key ]; |
|
18 | 3039 |
} else { |
3040 |
$pattern_property_schema = rest_find_matching_pattern_property_schema( $key, $schema ); |
|
3041 |
if ( null !== $pattern_property_schema ) { |
|
3042 |
$check = $pattern_property_schema; |
|
3043 |
} elseif ( $has_additional_properties ) { |
|
3044 |
$check = $schema['additionalProperties']; |
|
3045 |
} |
|
16 | 3046 |
} |
3047 |
} |
|
3048 |
||
3049 |
if ( ! isset( $check['context'] ) ) { |
|
3050 |
continue; |
|
3051 |
} |
|
3052 |
||
3053 |
if ( ! in_array( $context, $check['context'], true ) ) { |
|
3054 |
if ( $is_array_type ) { |
|
3055 |
// All array items share schema, so there's no need to check each one. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3056 |
$response_data = array(); |
16 | 3057 |
break; |
3058 |
} |
|
3059 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3060 |
if ( is_object( $response_data ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3061 |
unset( $response_data->$key ); |
16 | 3062 |
} else { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3063 |
unset( $response_data[ $key ] ); |
16 | 3064 |
} |
3065 |
} elseif ( is_array( $value ) || is_object( $value ) ) { |
|
3066 |
$new_value = rest_filter_response_by_context( $value, $check, $context ); |
|
3067 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3068 |
if ( is_object( $response_data ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3069 |
$response_data->$key = $new_value; |
16 | 3070 |
} else { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3071 |
$response_data[ $key ] = $new_value; |
16 | 3072 |
} |
3073 |
} |
|
3074 |
} |
|
3075 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3076 |
return $response_data; |
16 | 3077 |
} |
3078 |
||
3079 |
/** |
|
3080 |
* Sets the "additionalProperties" to false by default for all object definitions in the schema. |
|
3081 |
* |
|
3082 |
* @since 5.5.0 |
|
18 | 3083 |
* @since 5.6.0 Support the "patternProperties" keyword. |
16 | 3084 |
* |
3085 |
* @param array $schema The schema to modify. |
|
3086 |
* @return array The modified schema. |
|
3087 |
*/ |
|
3088 |
function rest_default_additional_properties_to_false( $schema ) { |
|
3089 |
$type = (array) $schema['type']; |
|
3090 |
||
3091 |
if ( in_array( 'object', $type, true ) ) { |
|
3092 |
if ( isset( $schema['properties'] ) ) { |
|
3093 |
foreach ( $schema['properties'] as $key => $child_schema ) { |
|
3094 |
$schema['properties'][ $key ] = rest_default_additional_properties_to_false( $child_schema ); |
|
3095 |
} |
|
3096 |
} |
|
3097 |
||
18 | 3098 |
if ( isset( $schema['patternProperties'] ) ) { |
3099 |
foreach ( $schema['patternProperties'] as $key => $child_schema ) { |
|
3100 |
$schema['patternProperties'][ $key ] = rest_default_additional_properties_to_false( $child_schema ); |
|
3101 |
} |
|
3102 |
} |
|
3103 |
||
16 | 3104 |
if ( ! isset( $schema['additionalProperties'] ) ) { |
3105 |
$schema['additionalProperties'] = false; |
|
3106 |
} |
|
3107 |
} |
|
3108 |
||
3109 |
if ( in_array( 'array', $type, true ) ) { |
|
3110 |
if ( isset( $schema['items'] ) ) { |
|
3111 |
$schema['items'] = rest_default_additional_properties_to_false( $schema['items'] ); |
|
3112 |
} |
|
3113 |
} |
|
3114 |
||
3115 |
return $schema; |
|
3116 |
} |
|
3117 |
||
3118 |
/** |
|
3119 |
* Gets the REST API route for a post. |
|
3120 |
* |
|
3121 |
* @since 5.5.0 |
|
3122 |
* |
|
3123 |
* @param int|WP_Post $post Post ID or post object. |
|
19 | 3124 |
* @return string The route path with a leading slash for the given post, |
3125 |
* or an empty string if there is not a route. |
|
16 | 3126 |
*/ |
3127 |
function rest_get_route_for_post( $post ) { |
|
3128 |
$post = get_post( $post ); |
|
3129 |
||
3130 |
if ( ! $post instanceof WP_Post ) { |
|
3131 |
return ''; |
|
3132 |
} |
|
3133 |
||
19 | 3134 |
$post_type_route = rest_get_route_for_post_type_items( $post->post_type ); |
3135 |
if ( ! $post_type_route ) { |
|
16 | 3136 |
return ''; |
3137 |
} |
|
3138 |
||
19 | 3139 |
$route = sprintf( '%s/%d', $post_type_route, $post->ID ); |
16 | 3140 |
|
3141 |
/** |
|
3142 |
* Filters the REST API route for a post. |
|
3143 |
* |
|
3144 |
* @since 5.5.0 |
|
3145 |
* |
|
3146 |
* @param string $route The route path. |
|
3147 |
* @param WP_Post $post The post object. |
|
3148 |
*/ |
|
3149 |
return apply_filters( 'rest_route_for_post', $route, $post ); |
|
3150 |
} |
|
3151 |
||
3152 |
/** |
|
19 | 3153 |
* Gets the REST API route for a post type. |
3154 |
* |
|
3155 |
* @since 5.9.0 |
|
3156 |
* |
|
3157 |
* @param string $post_type The name of a registered post type. |
|
3158 |
* @return string The route path with a leading slash for the given post type, |
|
3159 |
* or an empty string if there is not a route. |
|
3160 |
*/ |
|
3161 |
function rest_get_route_for_post_type_items( $post_type ) { |
|
3162 |
$post_type = get_post_type_object( $post_type ); |
|
3163 |
if ( ! $post_type ) { |
|
3164 |
return ''; |
|
3165 |
} |
|
3166 |
||
3167 |
if ( ! $post_type->show_in_rest ) { |
|
3168 |
return ''; |
|
3169 |
} |
|
3170 |
||
3171 |
$namespace = ! empty( $post_type->rest_namespace ) ? $post_type->rest_namespace : 'wp/v2'; |
|
3172 |
$rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name; |
|
3173 |
$route = sprintf( '/%s/%s', $namespace, $rest_base ); |
|
3174 |
||
3175 |
/** |
|
3176 |
* Filters the REST API route for a post type. |
|
3177 |
* |
|
3178 |
* @since 5.9.0 |
|
3179 |
* |
|
3180 |
* @param string $route The route path. |
|
3181 |
* @param WP_Post_Type $post_type The post type object. |
|
3182 |
*/ |
|
3183 |
return apply_filters( 'rest_route_for_post_type_items', $route, $post_type ); |
|
3184 |
} |
|
3185 |
||
3186 |
/** |
|
16 | 3187 |
* Gets the REST API route for a term. |
3188 |
* |
|
3189 |
* @since 5.5.0 |
|
3190 |
* |
|
3191 |
* @param int|WP_Term $term Term ID or term object. |
|
19 | 3192 |
* @return string The route path with a leading slash for the given term, |
3193 |
* or an empty string if there is not a route. |
|
16 | 3194 |
*/ |
3195 |
function rest_get_route_for_term( $term ) { |
|
3196 |
$term = get_term( $term ); |
|
3197 |
||
3198 |
if ( ! $term instanceof WP_Term ) { |
|
3199 |
return ''; |
|
3200 |
} |
|
3201 |
||
19 | 3202 |
$taxonomy_route = rest_get_route_for_taxonomy_items( $term->taxonomy ); |
3203 |
if ( ! $taxonomy_route ) { |
|
16 | 3204 |
return ''; |
3205 |
} |
|
3206 |
||
19 | 3207 |
$route = sprintf( '%s/%d', $taxonomy_route, $term->term_id ); |
16 | 3208 |
|
3209 |
/** |
|
3210 |
* Filters the REST API route for a term. |
|
3211 |
* |
|
3212 |
* @since 5.5.0 |
|
3213 |
* |
|
3214 |
* @param string $route The route path. |
|
3215 |
* @param WP_Term $term The term object. |
|
3216 |
*/ |
|
3217 |
return apply_filters( 'rest_route_for_term', $route, $term ); |
|
3218 |
} |
|
3219 |
||
3220 |
/** |
|
19 | 3221 |
* Gets the REST API route for a taxonomy. |
3222 |
* |
|
3223 |
* @since 5.9.0 |
|
3224 |
* |
|
3225 |
* @param string $taxonomy Name of taxonomy. |
|
3226 |
* @return string The route path with a leading slash for the given taxonomy. |
|
3227 |
*/ |
|
3228 |
function rest_get_route_for_taxonomy_items( $taxonomy ) { |
|
3229 |
$taxonomy = get_taxonomy( $taxonomy ); |
|
3230 |
if ( ! $taxonomy ) { |
|
3231 |
return ''; |
|
3232 |
} |
|
3233 |
||
3234 |
if ( ! $taxonomy->show_in_rest ) { |
|
3235 |
return ''; |
|
3236 |
} |
|
3237 |
||
3238 |
$namespace = ! empty( $taxonomy->rest_namespace ) ? $taxonomy->rest_namespace : 'wp/v2'; |
|
3239 |
$rest_base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; |
|
3240 |
$route = sprintf( '/%s/%s', $namespace, $rest_base ); |
|
3241 |
||
3242 |
/** |
|
3243 |
* Filters the REST API route for a taxonomy. |
|
3244 |
* |
|
3245 |
* @since 5.9.0 |
|
3246 |
* |
|
3247 |
* @param string $route The route path. |
|
3248 |
* @param WP_Taxonomy $taxonomy The taxonomy object. |
|
3249 |
*/ |
|
3250 |
return apply_filters( 'rest_route_for_taxonomy_items', $route, $taxonomy ); |
|
3251 |
} |
|
3252 |
||
3253 |
/** |
|
16 | 3254 |
* Gets the REST route for the currently queried object. |
3255 |
* |
|
3256 |
* @since 5.5.0 |
|
3257 |
* |
|
3258 |
* @return string The REST route of the resource, or an empty string if no resource identified. |
|
3259 |
*/ |
|
3260 |
function rest_get_queried_resource_route() { |
|
3261 |
if ( is_singular() ) { |
|
3262 |
$route = rest_get_route_for_post( get_queried_object() ); |
|
3263 |
} elseif ( is_category() || is_tag() || is_tax() ) { |
|
3264 |
$route = rest_get_route_for_term( get_queried_object() ); |
|
3265 |
} elseif ( is_author() ) { |
|
3266 |
$route = '/wp/v2/users/' . get_queried_object_id(); |
|
3267 |
} else { |
|
3268 |
$route = ''; |
|
3269 |
} |
|
3270 |
||
3271 |
/** |
|
3272 |
* Filters the REST route for the currently queried object. |
|
3273 |
* |
|
3274 |
* @since 5.5.0 |
|
3275 |
* |
|
3276 |
* @param string $link The route with a leading slash, or an empty string. |
|
3277 |
*/ |
|
3278 |
return apply_filters( 'rest_queried_resource_route', $route ); |
|
3279 |
} |
|
18 | 3280 |
|
3281 |
/** |
|
3282 |
* Retrieves an array of endpoint arguments from the item schema and endpoint method. |
|
3283 |
* |
|
3284 |
* @since 5.6.0 |
|
3285 |
* |
|
3286 |
* @param array $schema The full JSON schema for the endpoint. |
|
3287 |
* @param string $method Optional. HTTP method of the endpoint. The arguments for `CREATABLE` endpoints are |
|
3288 |
* checked for required values and may fall-back to a given default, this is not done |
|
3289 |
* on `EDITABLE` endpoints. Default WP_REST_Server::CREATABLE. |
|
3290 |
* @return array The endpoint arguments. |
|
3291 |
*/ |
|
3292 |
function rest_get_endpoint_args_for_schema( $schema, $method = WP_REST_Server::CREATABLE ) { |
|
3293 |
||
3294 |
$schema_properties = ! empty( $schema['properties'] ) ? $schema['properties'] : array(); |
|
3295 |
$endpoint_args = array(); |
|
3296 |
$valid_schema_properties = rest_get_allowed_schema_keywords(); |
|
3297 |
$valid_schema_properties = array_diff( $valid_schema_properties, array( 'default', 'required' ) ); |
|
3298 |
||
3299 |
foreach ( $schema_properties as $field_id => $params ) { |
|
3300 |
||
3301 |
// Arguments specified as `readonly` are not allowed to be set. |
|
3302 |
if ( ! empty( $params['readonly'] ) ) { |
|
3303 |
continue; |
|
3304 |
} |
|
3305 |
||
3306 |
$endpoint_args[ $field_id ] = array( |
|
3307 |
'validate_callback' => 'rest_validate_request_arg', |
|
3308 |
'sanitize_callback' => 'rest_sanitize_request_arg', |
|
3309 |
); |
|
3310 |
||
3311 |
if ( WP_REST_Server::CREATABLE === $method && isset( $params['default'] ) ) { |
|
3312 |
$endpoint_args[ $field_id ]['default'] = $params['default']; |
|
3313 |
} |
|
3314 |
||
3315 |
if ( WP_REST_Server::CREATABLE === $method && ! empty( $params['required'] ) ) { |
|
3316 |
$endpoint_args[ $field_id ]['required'] = true; |
|
3317 |
} |
|
3318 |
||
3319 |
foreach ( $valid_schema_properties as $schema_prop ) { |
|
3320 |
if ( isset( $params[ $schema_prop ] ) ) { |
|
3321 |
$endpoint_args[ $field_id ][ $schema_prop ] = $params[ $schema_prop ]; |
|
3322 |
} |
|
3323 |
} |
|
3324 |
||
3325 |
// Merge in any options provided by the schema property. |
|
3326 |
if ( isset( $params['arg_options'] ) ) { |
|
3327 |
||
3328 |
// Only use required / default from arg_options on CREATABLE endpoints. |
|
3329 |
if ( WP_REST_Server::CREATABLE !== $method ) { |
|
3330 |
$params['arg_options'] = array_diff_key( |
|
3331 |
$params['arg_options'], |
|
3332 |
array( |
|
3333 |
'required' => '', |
|
3334 |
'default' => '', |
|
3335 |
) |
|
3336 |
); |
|
3337 |
} |
|
3338 |
||
3339 |
$endpoint_args[ $field_id ] = array_merge( $endpoint_args[ $field_id ], $params['arg_options'] ); |
|
3340 |
} |
|
3341 |
} |
|
3342 |
||
3343 |
return $endpoint_args; |
|
3344 |
} |
|
3345 |
||
3346 |
||
3347 |
/** |
|
3348 |
* Converts an error to a response object. |
|
3349 |
* |
|
3350 |
* This iterates over all error codes and messages to change it into a flat |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3351 |
* array. This enables simpler client behavior, as it is represented as a |
18 | 3352 |
* list in JSON rather than an object/map. |
3353 |
* |
|
3354 |
* @since 5.7.0 |
|
3355 |
* |
|
3356 |
* @param WP_Error $error WP_Error instance. |
|
3357 |
* |
|
3358 |
* @return WP_REST_Response List of associative arrays with code and message keys. |
|
3359 |
*/ |
|
3360 |
function rest_convert_error_to_response( $error ) { |
|
3361 |
$status = array_reduce( |
|
3362 |
$error->get_all_error_data(), |
|
19 | 3363 |
static function ( $status, $error_data ) { |
18 | 3364 |
return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status; |
3365 |
}, |
|
3366 |
500 |
|
3367 |
); |
|
3368 |
||
3369 |
$errors = array(); |
|
3370 |
||
3371 |
foreach ( (array) $error->errors as $code => $messages ) { |
|
3372 |
$all_data = $error->get_all_error_data( $code ); |
|
3373 |
$last_data = array_pop( $all_data ); |
|
3374 |
||
3375 |
foreach ( (array) $messages as $message ) { |
|
3376 |
$formatted = array( |
|
3377 |
'code' => $code, |
|
3378 |
'message' => $message, |
|
3379 |
'data' => $last_data, |
|
3380 |
); |
|
3381 |
||
3382 |
if ( $all_data ) { |
|
3383 |
$formatted['additional_data'] = $all_data; |
|
3384 |
} |
|
3385 |
||
3386 |
$errors[] = $formatted; |
|
3387 |
} |
|
3388 |
} |
|
3389 |
||
3390 |
$data = $errors[0]; |
|
3391 |
if ( count( $errors ) > 1 ) { |
|
3392 |
// Remove the primary error. |
|
3393 |
array_shift( $errors ); |
|
3394 |
$data['additional_errors'] = $errors; |
|
3395 |
} |
|
3396 |
||
3397 |
return new WP_REST_Response( $data, $status ); |
|
3398 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3399 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3400 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3401 |
* Checks whether a REST API endpoint request is currently being handled. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3402 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3403 |
* This may be a standalone REST API request, or an internal request dispatched from within a regular page load. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3404 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3405 |
* @since 6.5.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3406 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3407 |
* @global WP_REST_Server $wp_rest_server REST server instance. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3408 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3409 |
* @return bool True if a REST endpoint request is currently being handled, false otherwise. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3410 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3411 |
function wp_is_rest_endpoint() { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3412 |
/* @var WP_REST_Server $wp_rest_server */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3413 |
global $wp_rest_server; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3414 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3415 |
// Check whether this is a standalone REST request. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3416 |
$is_rest_endpoint = wp_is_serving_rest_request(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3417 |
if ( ! $is_rest_endpoint ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3418 |
// Otherwise, check whether an internal REST request is currently being handled. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3419 |
$is_rest_endpoint = isset( $wp_rest_server ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3420 |
&& $wp_rest_server->is_dispatching(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3421 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3422 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3423 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3424 |
* Filters whether a REST endpoint request is currently being handled. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3425 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3426 |
* This may be a standalone REST API request, or an internal request dispatched from within a regular page load. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3427 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3428 |
* @since 6.5.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3429 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3430 |
* @param bool $is_request_endpoint Whether a REST endpoint request is currently being handled. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3431 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3432 |
return (bool) apply_filters( 'wp_is_rest_endpoint', $is_rest_endpoint ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3433 |
} |