33 * |
33 * |
34 * @see register_rest_route() |
34 * @see register_rest_route() |
35 */ |
35 */ |
36 public function register_routes() { |
36 public function register_routes() { |
37 |
37 |
38 register_rest_route( $this->namespace, '/' . $this->rest_base, array( |
38 register_rest_route( |
|
39 $this->namespace, |
|
40 '/' . $this->rest_base, |
39 array( |
41 array( |
40 'methods' => WP_REST_Server::READABLE, |
42 array( |
41 'callback' => array( $this, 'get_item' ), |
43 'methods' => WP_REST_Server::READABLE, |
42 'args' => array(), |
44 'callback' => array( $this, 'get_item' ), |
43 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
45 'args' => array(), |
44 ), |
46 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
45 array( |
47 ), |
46 'methods' => WP_REST_Server::EDITABLE, |
48 array( |
47 'callback' => array( $this, 'update_item' ), |
49 'methods' => WP_REST_Server::EDITABLE, |
48 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
50 'callback' => array( $this, 'update_item' ), |
49 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
51 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
50 ), |
52 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
51 'schema' => array( $this, 'get_public_item_schema' ), |
53 ), |
52 ) ); |
54 'schema' => array( $this, 'get_public_item_schema' ), |
|
55 ) |
|
56 ); |
53 |
57 |
54 } |
58 } |
55 |
59 |
56 /** |
60 /** |
57 * Checks if a given request has access to read and manage settings. |
61 * Checks if a given request has access to read and manage settings. |
137 * @return array|WP_Error Array on success, or error object on failure. |
141 * @return array|WP_Error Array on success, or error object on failure. |
138 */ |
142 */ |
139 public function update_item( $request ) { |
143 public function update_item( $request ) { |
140 $options = $this->get_registered_options(); |
144 $options = $this->get_registered_options(); |
141 |
145 |
142 $params = $request->get_params(); |
146 $params = $request->get_params(); |
143 |
147 |
144 foreach ( $options as $name => $args ) { |
148 foreach ( $options as $name => $args ) { |
145 if ( ! array_key_exists( $name, $params ) ) { |
149 if ( ! array_key_exists( $name, $params ) ) { |
146 continue; |
150 continue; |
147 } |
151 } |
183 * delete all options that have invalid values from the |
187 * delete all options that have invalid values from the |
184 * database. |
188 * database. |
185 */ |
189 */ |
186 if ( is_wp_error( rest_validate_value_from_schema( get_option( $args['option_name'], false ), $args['schema'] ) ) ) { |
190 if ( is_wp_error( rest_validate_value_from_schema( get_option( $args['option_name'], false ), $args['schema'] ) ) ) { |
187 return new WP_Error( |
191 return new WP_Error( |
188 'rest_invalid_stored_value', sprintf( __( 'The %s property has an invalid stored value, and cannot be updated to null.' ), $name ), array( 'status' => 500 ) |
192 'rest_invalid_stored_value', |
|
193 sprintf( __( 'The %s property has an invalid stored value, and cannot be updated to null.' ), $name ), |
|
194 array( 'status' => 500 ) |
189 ); |
195 ); |
190 } |
196 } |
191 |
197 |
192 delete_option( $args['option_name'] ); |
198 delete_option( $args['option_name'] ); |
193 } else { |
199 } else { |
230 'type' => empty( $args['type'] ) ? null : $args['type'], |
236 'type' => empty( $args['type'] ) ? null : $args['type'], |
231 'description' => empty( $args['description'] ) ? '' : $args['description'], |
237 'description' => empty( $args['description'] ) ? '' : $args['description'], |
232 'default' => isset( $args['default'] ) ? $args['default'] : null, |
238 'default' => isset( $args['default'] ) ? $args['default'] : null, |
233 ); |
239 ); |
234 |
240 |
235 $rest_args['schema'] = array_merge( $default_schema, $rest_args['schema'] ); |
241 $rest_args['schema'] = array_merge( $default_schema, $rest_args['schema'] ); |
236 $rest_args['option_name'] = $name; |
242 $rest_args['option_name'] = $name; |
237 |
243 |
238 // Skip over settings that don't have a defined type in the schema. |
244 // Skip over settings that don't have a defined type in the schema. |
239 if ( empty( $rest_args['schema']['type'] ) ) { |
245 if ( empty( $rest_args['schema']['type'] ) ) { |
240 continue; |
246 continue; |
272 'type' => 'object', |
278 'type' => 'object', |
273 'properties' => array(), |
279 'properties' => array(), |
274 ); |
280 ); |
275 |
281 |
276 foreach ( $options as $option_name => $option ) { |
282 foreach ( $options as $option_name => $option ) { |
277 $schema['properties'][ $option_name ] = $option['schema']; |
283 $schema['properties'][ $option_name ] = $option['schema']; |
278 $schema['properties'][ $option_name ]['arg_options'] = array( |
284 $schema['properties'][ $option_name ]['arg_options'] = array( |
279 'sanitize_callback' => array( $this, 'sanitize_callback' ), |
285 'sanitize_callback' => array( $this, 'sanitize_callback' ), |
280 ); |
286 ); |
281 } |
287 } |
282 |
288 |