1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @version 2.0-beta12 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
7
|
|
|
exit; |
8
|
|
|
} |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* core-integration.php |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
if ( ! function_exists( 'wp_parse_slug_list' ) ) { |
15
|
|
|
/** |
16
|
|
|
* Clean up an array, comma- or space-separated list of slugs. |
17
|
|
|
* |
18
|
|
|
* @since |
19
|
|
|
* |
20
|
|
|
* @param array|string $list List of slugs. |
21
|
|
|
* @return array Sanitized array of slugs. |
22
|
|
|
*/ |
23
|
|
|
function wp_parse_slug_list( $list ) { |
24
|
|
|
if ( ! is_array( $list ) ) { |
25
|
|
|
$list = preg_split( '/[\s,]+/', $list ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
foreach ( $list as $key => $value ) { |
29
|
|
|
$list[ $key ] = sanitize_title( $value ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return array_unique( $list ); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ( ! function_exists( 'rest_get_server' ) ) { |
37
|
|
|
/** |
38
|
|
|
* Retrieves the current REST server instance. |
39
|
|
|
* |
40
|
|
|
* Instantiates a new instance if none exists already. |
41
|
|
|
* |
42
|
|
|
* @since 4.5.0 |
43
|
|
|
* |
44
|
|
|
* @global WP_REST_Server $wp_rest_server REST server instance. |
45
|
|
|
* |
46
|
|
|
* @return WP_REST_Server REST server instance. |
47
|
|
|
*/ |
48
|
|
|
function rest_get_server() { |
49
|
|
|
/* @var WP_REST_Server $wp_rest_server */ |
50
|
|
|
global $wp_rest_server; |
51
|
|
|
|
52
|
|
|
if ( empty( $wp_rest_server ) ) { |
53
|
|
|
/** |
54
|
|
|
* Filter the REST Server Class. |
55
|
|
|
* |
56
|
|
|
* This filter allows you to adjust the server class used by the API, using a |
57
|
|
|
* different class to handle requests. |
58
|
|
|
* |
59
|
|
|
* @since 4.4.0 |
60
|
|
|
* |
61
|
|
|
* @param string $class_name The name of the server class. Default 'WP_REST_Server'. |
62
|
|
|
*/ |
63
|
|
|
$wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' ); |
64
|
|
|
$wp_rest_server = new $wp_rest_server_class; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Fires when preparing to serve an API request. |
68
|
|
|
* |
69
|
|
|
* Endpoint objects should be created and register their hooks on this action rather |
70
|
|
|
* than another action to ensure they're only loaded when needed. |
71
|
|
|
* |
72
|
|
|
* @since 4.4.0 |
73
|
|
|
* |
74
|
|
|
* @param WP_REST_Server $wp_rest_server Server object. |
75
|
|
|
*/ |
76
|
|
|
do_action( 'rest_api_init', $wp_rest_server ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $wp_rest_server; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* plugin.php |
85
|
|
|
*/ |
86
|
|
|
|
87
|
|
|
if ( ! function_exists( 'rest_authorization_required_code' ) ) { |
88
|
|
|
/** |
89
|
|
|
* Returns a contextual HTTP error code for authorization failure. |
90
|
|
|
* |
91
|
|
|
* @return integer |
92
|
|
|
*/ |
93
|
|
|
function rest_authorization_required_code() { |
|
|
|
|
94
|
|
|
return is_user_logged_in() ? 403 : 401; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
View Code Duplication |
if ( ! function_exists( 'register_rest_field' ) ) { |
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Registers a new field on an existing WordPress object type. |
101
|
|
|
* |
102
|
|
|
* @global array $wp_rest_additional_fields Holds registered fields, organized |
103
|
|
|
* by object type. |
104
|
|
|
* |
105
|
|
|
* @param string|array $object_type Object(s) the field is being registered |
106
|
|
|
* to, "post"|"term"|"comment" etc. |
107
|
|
|
* @param string $attribute The attribute name. |
108
|
|
|
* @param array $args { |
109
|
|
|
* Optional. An array of arguments used to handle the registered field. |
110
|
|
|
* |
111
|
|
|
* @type string|array|null $get_callback Optional. The callback function used to retrieve the field |
112
|
|
|
* value. Default is 'null', the field will not be returned in |
113
|
|
|
* the response. |
114
|
|
|
* @type string|array|null $update_callback Optional. The callback function used to set and update the |
115
|
|
|
* field value. Default is 'null', the value cannot be set or |
116
|
|
|
* updated. |
117
|
|
|
* @type string|array|null $schema Optional. The callback function used to create the schema for |
118
|
|
|
* this field. Default is 'null', no schema entry will be returned. |
119
|
|
|
* } |
120
|
|
|
*/ |
121
|
|
|
function register_rest_field( $object_type, $attribute, $args = array() ) { |
|
|
|
|
122
|
|
|
$defaults = array( |
123
|
|
|
'get_callback' => null, |
124
|
|
|
'update_callback' => null, |
125
|
|
|
'schema' => null, |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$args = wp_parse_args( $args, $defaults ); |
129
|
|
|
|
130
|
|
|
global $wp_rest_additional_fields; |
131
|
|
|
|
132
|
|
|
$object_types = (array) $object_type; |
133
|
|
|
|
134
|
|
|
foreach ( $object_types as $object_type ) { |
135
|
|
|
$wp_rest_additional_fields[ $object_type ][ $attribute ] = $args; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
View Code Duplication |
if ( ! function_exists( 'register_api_field' ) ) { |
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Backwards compat shim |
143
|
|
|
*/ |
144
|
|
|
function register_api_field( $object_type, $attributes, $args = array() ) { |
|
|
|
|
145
|
|
|
_deprecated_function( 'register_api_field', 'WPAPI-2.0', 'register_rest_field' ); |
146
|
|
|
register_rest_field( $object_type, $attributes, $args ); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if ( ! function_exists( 'rest_validate_request_arg' ) ) { |
151
|
|
|
/** |
152
|
|
|
* Validate a request argument based on details registered to the route. |
153
|
|
|
* |
154
|
|
|
* @param mixed $value |
155
|
|
|
* @param WP_REST_Request $request |
156
|
|
|
* @param string $param |
157
|
|
|
* @return WP_Error|boolean |
158
|
|
|
*/ |
159
|
|
|
function rest_validate_request_arg( $value, $request, $param ) { |
|
|
|
|
160
|
|
|
|
161
|
|
|
$attributes = $request->get_attributes(); |
162
|
|
|
if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) { |
163
|
|
|
return true; |
164
|
|
|
} |
165
|
|
|
$args = $attributes['args'][ $param ]; |
166
|
|
|
|
167
|
|
View Code Duplication |
if ( ! empty( $args['enum'] ) ) { |
|
|
|
|
168
|
|
|
if ( ! in_array( $value, $args['enum'] ) ) { |
169
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not one of %s', 'woocommerce' ), $param, implode( ', ', $args['enum'] ) ) ); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if ( 'integer' === $args['type'] && ! is_numeric( $value ) ) { |
174
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not of type %s', 'woocommerce' ), $param, 'integer' ) ); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
View Code Duplication |
if ( 'string' === $args['type'] && ! is_string( $value ) ) { |
|
|
|
|
178
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not of type %s', 'woocommerce' ), $param, 'string' ) ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
View Code Duplication |
if ( isset( $args['format'] ) ) { |
|
|
|
|
182
|
|
|
switch ( $args['format'] ) { |
183
|
|
|
case 'date-time' : |
184
|
|
|
if ( ! rest_parse_date( $value ) ) { |
185
|
|
|
return new WP_Error( 'rest_invalid_date', __( 'The date you provided is invalid.', 'woocommerce' ) ); |
186
|
|
|
} |
187
|
|
|
break; |
188
|
|
|
|
189
|
|
|
case 'email' : |
190
|
|
|
if ( ! is_email( $value ) ) { |
191
|
|
|
return new WP_Error( 'rest_invalid_email', __( 'The email address you provided is invalid.', 'woocommerce' ) ); |
192
|
|
|
} |
193
|
|
|
break; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if ( in_array( $args['type'], array( 'numeric', 'integer' ) ) && ( isset( $args['minimum'] ) || isset( $args['maximum'] ) ) ) { |
198
|
|
|
if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) { |
199
|
|
View Code Duplication |
if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) { |
|
|
|
|
200
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be greater than %d (exclusive)', 'woocommerce' ), $param, $args['minimum'] ) ); |
201
|
|
|
} else if ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) { |
202
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be greater than %d (inclusive)', 'woocommerce' ), $param, $args['minimum'] ) ); |
203
|
|
|
} |
204
|
|
|
} else if ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) { |
205
|
|
View Code Duplication |
if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) { |
|
|
|
|
206
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be less than %d (exclusive)', 'woocommerce' ), $param, $args['maximum'] ) ); |
207
|
|
|
} else if ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) { |
208
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be less than %d (inclusive)', 'woocommerce' ), $param, $args['maximum'] ) ); |
209
|
|
|
} |
210
|
|
|
} else if ( isset( $args['maximum'] ) && isset( $args['minimum'] ) ) { |
211
|
|
|
if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) { |
212
|
|
|
if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) { |
213
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (exclusive) and %d (exclusive)', 'woocommerce' ), $param, $args['minimum'], $args['maximum'] ) ); |
214
|
|
|
} |
215
|
|
View Code Duplication |
} else if ( empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) { |
|
|
|
|
216
|
|
|
if ( $value >= $args['maximum'] || $value < $args['minimum'] ) { |
217
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (inclusive) and %d (exclusive)', 'woocommerce' ), $param, $args['minimum'], $args['maximum'] ) ); |
218
|
|
|
} |
219
|
|
|
} else if ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) { |
220
|
|
|
if ( $value > $args['maximum'] || $value <= $args['minimum'] ) { |
221
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (exclusive) and %d (inclusive)', 'woocommerce' ), $param, $args['minimum'], $args['maximum'] ) ); |
222
|
|
|
} |
223
|
|
|
} else if ( empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) { |
224
|
|
|
if ( $value > $args['maximum'] || $value < $args['minimum'] ) { |
225
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (inclusive) and %d (inclusive)', 'woocommerce' ), $param, $args['minimum'], $args['maximum'] ) ); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return true; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
View Code Duplication |
if ( ! function_exists( 'rest_sanitize_request_arg' ) ) { |
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Sanitize a request argument based on details registered to the route. |
238
|
|
|
* |
239
|
|
|
* @param mixed $value |
240
|
|
|
* @param WP_REST_Request $request |
241
|
|
|
* @param string $param |
242
|
|
|
* @return mixed |
243
|
|
|
*/ |
244
|
|
|
function rest_sanitize_request_arg( $value, $request, $param ) { |
|
|
|
|
245
|
|
|
|
246
|
|
|
$attributes = $request->get_attributes(); |
247
|
|
|
if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) { |
248
|
|
|
return $value; |
249
|
|
|
} |
250
|
|
|
$args = $attributes['args'][ $param ]; |
251
|
|
|
|
252
|
|
|
if ( 'integer' === $args['type'] ) { |
253
|
|
|
return (int) $value; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
if ( isset( $args['format'] ) ) { |
257
|
|
|
switch ( $args['format'] ) { |
258
|
|
|
case 'date-time' : |
259
|
|
|
return sanitize_text_field( $value ); |
260
|
|
|
|
261
|
|
|
case 'email' : |
262
|
|
|
/* |
263
|
|
|
* sanitize_email() validates, which would be unexpected |
264
|
|
|
*/ |
265
|
|
|
return sanitize_text_field( $value ); |
266
|
|
|
|
267
|
|
|
case 'uri' : |
268
|
|
|
return esc_url_raw( $value ); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $value; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
} |
276
|
|
|
|
This check looks for functions that have already been defined in other files.
Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the
@ignore
annotation.See also the PhpDoc documentation for @ignore.