1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @todo to be removed when the wp-api branch is merged. |
4
|
|
|
* @version 2.0-beta12 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
8
|
|
|
exit; |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
if ( ! function_exists( 'rest_authorization_required_code' ) ) { |
12
|
|
|
/** |
13
|
|
|
* Returns a contextual HTTP error code for authorization failure. |
14
|
|
|
* |
15
|
|
|
* @return integer |
16
|
|
|
*/ |
17
|
|
|
function rest_authorization_required_code() { |
18
|
|
|
return is_user_logged_in() ? 403 : 401; |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
View Code Duplication |
if ( ! function_exists( 'register_rest_field' ) ) { |
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Registers a new field on an existing WordPress object type. |
25
|
|
|
* |
26
|
|
|
* @global array $wp_rest_additional_fields Holds registered fields, organized |
27
|
|
|
* by object type. |
28
|
|
|
* |
29
|
|
|
* @param string|array $object_type Object(s) the field is being registered |
30
|
|
|
* to, "post"|"term"|"comment" etc. |
31
|
|
|
* @param string $attribute The attribute name. |
32
|
|
|
* @param array $args { |
33
|
|
|
* Optional. An array of arguments used to handle the registered field. |
34
|
|
|
* |
35
|
|
|
* @type string|array|null $get_callback Optional. The callback function used to retrieve the field |
36
|
|
|
* value. Default is 'null', the field will not be returned in |
37
|
|
|
* the response. |
38
|
|
|
* @type string|array|null $update_callback Optional. The callback function used to set and update the |
39
|
|
|
* field value. Default is 'null', the value cannot be set or |
40
|
|
|
* updated. |
41
|
|
|
* @type string|array|null $schema Optional. The callback function used to create the schema for |
42
|
|
|
* this field. Default is 'null', no schema entry will be returned. |
43
|
|
|
* } |
44
|
|
|
*/ |
45
|
|
|
function register_rest_field( $object_type, $attribute, $args = array() ) { |
46
|
|
|
$defaults = array( |
47
|
|
|
'get_callback' => null, |
48
|
|
|
'update_callback' => null, |
49
|
|
|
'schema' => null, |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$args = wp_parse_args( $args, $defaults ); |
53
|
|
|
|
54
|
|
|
global $wp_rest_additional_fields; |
55
|
|
|
|
56
|
|
|
$object_types = (array) $object_type; |
57
|
|
|
|
58
|
|
|
foreach ( $object_types as $object_type ) { |
59
|
|
|
$wp_rest_additional_fields[ $object_type ][ $attribute ] = $args; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
if ( ! function_exists( 'register_api_field' ) ) { |
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Backwards compat shim |
67
|
|
|
*/ |
68
|
|
|
function register_api_field( $object_type, $attributes, $args = array() ) { |
69
|
|
|
_deprecated_function( 'register_api_field', 'WPAPI-2.0', 'register_rest_field' ); |
70
|
|
|
register_rest_field( $object_type, $attributes, $args ); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ( ! function_exists( 'rest_validate_request_arg' ) ) { |
75
|
|
|
/** |
76
|
|
|
* Validate a request argument based on details registered to the route. |
77
|
|
|
* |
78
|
|
|
* @param mixed $value |
79
|
|
|
* @param WP_REST_Request $request |
80
|
|
|
* @param string $param |
81
|
|
|
* @return WP_Error|boolean |
82
|
|
|
*/ |
83
|
|
|
function rest_validate_request_arg( $value, $request, $param ) { |
84
|
|
|
|
85
|
|
|
$attributes = $request->get_attributes(); |
86
|
|
|
if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) { |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
$args = $attributes['args'][ $param ]; |
90
|
|
|
|
91
|
|
View Code Duplication |
if ( ! empty( $args['enum'] ) ) { |
|
|
|
|
92
|
|
|
if ( ! in_array( $value, $args['enum'] ) ) { |
93
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not one of %s' ), $param, implode( ', ', $args['enum'] ) ) ); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
View Code Duplication |
if ( 'integer' === $args['type'] && ! is_numeric( $value ) ) { |
|
|
|
|
98
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not of type %s' ), $param, 'integer' ) ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
View Code Duplication |
if ( 'string' === $args['type'] && ! is_string( $value ) ) { |
|
|
|
|
102
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not of type %s' ), $param, 'string' ) ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
View Code Duplication |
if ( isset( $args['format'] ) ) { |
|
|
|
|
106
|
|
|
switch ( $args['format'] ) { |
107
|
|
|
case 'date-time' : |
108
|
|
|
if ( ! rest_parse_date( $value ) ) { |
109
|
|
|
return new WP_Error( 'rest_invalid_date', __( 'The date you provided is invalid.' ) ); |
110
|
|
|
} |
111
|
|
|
break; |
112
|
|
|
|
113
|
|
|
case 'email' : |
114
|
|
|
if ( ! is_email( $value ) ) { |
115
|
|
|
return new WP_Error( 'rest_invalid_email', __( 'The email address you provided is invalid.' ) ); |
116
|
|
|
} |
117
|
|
|
break; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ( in_array( $args['type'], array( 'numeric', 'integer' ) ) && ( isset( $args['minimum'] ) || isset( $args['maximum'] ) ) ) { |
122
|
|
|
if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) { |
123
|
|
View Code Duplication |
if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) { |
|
|
|
|
124
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be greater than %d (exclusive)' ), $param, $args['minimum'] ) ); |
125
|
|
|
} else if ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) { |
126
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be greater than %d (inclusive)' ), $param, $args['minimum'] ) ); |
127
|
|
|
} |
128
|
|
|
} else if ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) { |
129
|
|
View Code Duplication |
if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) { |
|
|
|
|
130
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be less than %d (exclusive)' ), $param, $args['maximum'] ) ); |
131
|
|
|
} else if ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) { |
132
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be less than %d (inclusive)' ), $param, $args['maximum'] ) ); |
133
|
|
|
} |
134
|
|
|
} else if ( isset( $args['maximum'] ) && isset( $args['minimum'] ) ) { |
135
|
|
|
if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) { |
136
|
|
|
if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) { |
137
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (exclusive) and %d (exclusive)' ), $param, $args['minimum'], $args['maximum'] ) ); |
138
|
|
|
} |
139
|
|
View Code Duplication |
} else if ( empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) { |
|
|
|
|
140
|
|
|
if ( $value >= $args['maximum'] || $value < $args['minimum'] ) { |
141
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (inclusive) and %d (exclusive)' ), $param, $args['minimum'], $args['maximum'] ) ); |
142
|
|
|
} |
143
|
|
|
} else if ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) { |
144
|
|
|
if ( $value > $args['maximum'] || $value <= $args['minimum'] ) { |
145
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (exclusive) and %d (inclusive)' ), $param, $args['minimum'], $args['maximum'] ) ); |
146
|
|
|
} |
147
|
|
|
} else if ( empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) { |
148
|
|
|
if ( $value > $args['maximum'] || $value < $args['minimum'] ) { |
149
|
|
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (inclusive) and %d (inclusive)' ), $param, $args['minimum'], $args['maximum'] ) ); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return true; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
View Code Duplication |
if ( ! function_exists( 'rest_sanitize_request_arg' ) ) { |
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Sanitize a request argument based on details registered to the route. |
162
|
|
|
* |
163
|
|
|
* @param mixed $value |
164
|
|
|
* @param WP_REST_Request $request |
165
|
|
|
* @param string $param |
166
|
|
|
* @return mixed |
167
|
|
|
*/ |
168
|
|
|
function rest_sanitize_request_arg( $value, $request, $param ) { |
169
|
|
|
|
170
|
|
|
$attributes = $request->get_attributes(); |
171
|
|
|
if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) { |
172
|
|
|
return $value; |
173
|
|
|
} |
174
|
|
|
$args = $attributes['args'][ $param ]; |
175
|
|
|
|
176
|
|
|
if ( 'integer' === $args['type'] ) { |
177
|
|
|
return (int) $value; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ( isset( $args['format'] ) ) { |
181
|
|
|
switch ( $args['format'] ) { |
182
|
|
|
case 'date-time' : |
183
|
|
|
return sanitize_text_field( $value ); |
184
|
|
|
|
185
|
|
|
case 'email' : |
186
|
|
|
/* |
187
|
|
|
* sanitize_email() validates, which would be unexpected |
188
|
|
|
*/ |
189
|
|
|
return sanitize_text_field( $value ); |
190
|
|
|
|
191
|
|
|
case 'uri' : |
192
|
|
|
return esc_url_raw( $value ); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $value; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.