Experiment with registered settings #13
Changes from 4 commits
512d28c
b7640d7
c012917
002ff7f
0e6a79b
e87b072
4da393e
5183965
af6f211
a370913
36ec88e
2ff0242
913f71b
11598ec
5008108
dcacb38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<?php | ||
|
||
/** | ||
* Manage a WordPress site's settings. | ||
*/ | ||
|
||
class WP_REST_Settings_Controller extends WP_REST_Controller { | ||
public function __construct() { | ||
$this->namespace = 'wp/v2'; | ||
$this->rest_base = 'settings'; | ||
} | ||
|
||
/** | ||
* Register the routes for the objects of the controller. | ||
*/ | ||
public function register_routes() { | ||
register_rest_route( $this->namespace, '/' . $this->rest_base, array( | ||
array( | ||
'methods' => WP_REST_Server::READABLE, | ||
'callback' => array( $this, 'get_item' ), | ||
'args' => array(), | ||
'permission_callback' => array( $this, 'get_item_permissions_check' ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alignment is off |
||
), | ||
array( | ||
'methods' => WP_REST_Server::EDITABLE, | ||
'callback' => array( $this, 'update_item' ), | ||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), | ||
'permission_callback' => array( $this, 'get_item_permissions_check' ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alignment |
||
), | ||
'schema' => array( $this, 'get_public_item_schema' ), | ||
) ); | ||
} | ||
|
||
public function get_item_permissions_check( $request ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs phpDoc |
||
return current_user_can( 'manage_options' ); | ||
} | ||
|
||
/** | ||
* Get the settings. | ||
* | ||
* @param WP_REST_Request $request Full details about the request. | ||
* @return WP_Error|array | ||
*/ | ||
public function get_item( $request ) { | ||
$options = $this->get_registered_options(); | ||
$response = array(); | ||
|
||
foreach ( $options as $name => $args ) { | ||
// default to a null value as "null" in the response means "not set" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comments need to start with a capital and end with a period. |
||
$response[ $name ] = get_option( $args['option_name'], $args['schema']['default'] ); | ||
|
||
// because get_option() is lossy, we have to | ||
// cast values to the type they are registered with. | ||
switch ( $args['schema']['type'] ) { | ||
case 'string': | ||
$response[ $name ] = strval( $response[ $name ] ); | ||
break; | ||
case 'number': | ||
$response[ $name ] = floatval( $response[ $name ] ); | ||
break; | ||
case 'boolean': | ||
$response[ $name ] = (bool) $response[ $name ]; | ||
break; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The contents here should probably be split into a separate method for reusability. |
||
} | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Update settings for the settings object. | ||
* | ||
* @param WP_REST_Request $request Full detail about the request. | ||
* @return WP_Error|array | ||
*/ | ||
public function update_item( $request ) { | ||
$options = $this->get_registered_options(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spacing is off. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 5008108 |
||
$params = $request->get_params(); | ||
|
||
foreach ( $options as $name => $args ) { | ||
if ( array_key_exists( $name, $params ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be flipped. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in dcacb38 |
||
// a null value means reset the option, which is essentially deleting it | ||
// from the database and then relying on the default value. | ||
if ( is_null( $request[ $name ] ) ) { | ||
delete_option( $args['option_name'] ); | ||
} else { | ||
update_option( $args['option_name'], $request[ $name ] ); | ||
} | ||
} | ||
} | ||
|
||
return $this->get_item( $request ); | ||
} | ||
|
||
/** | ||
* Get all the registered options for the Settings API | ||
* | ||
* @return array | ||
*/ | ||
protected function get_registered_options() { | ||
require_once ABSPATH . 'wp-admin/includes/plugin.php'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💀 |
||
$rest_options = array(); | ||
|
||
foreach ( get_registered_settings() as $name => $args ) { | ||
if ( empty( $args['show_in_rest'] ) ) { | ||
continue; | ||
} | ||
|
||
$rest_args = array(); | ||
if ( is_array( $args['show_in_rest'] ) ) { | ||
$rest_args = $args['show_in_rest']; | ||
} | ||
|
||
$rest_args = array_merge( $rest_args, array( | ||
'option_name' => $name, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be overridable, since the data is contained in the registration for the option itself. |
||
'name' => ! empty( $rest_args['name'] ) ? $rest_args['name'] : $name, | ||
'schema' => array( | ||
'type' => empty( $args['type'] ) ? null : $args['type'], | ||
'description' => empty( $args['description'] ) ? '' : $args['description'], | ||
'default' => isset( $args['default'] ) ? $args['default'] : null, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alignment. |
||
), | ||
)); | ||
|
||
// skip over settings that don't have a defined type in the schema | ||
if ( empty( $rest_args['schema']['type'] ) ) { | ||
continue; | ||
} | ||
|
||
$rest_options[ $rest_args['name'] ] = $rest_args; | ||
} | ||
|
||
return $rest_options; | ||
} | ||
|
||
/** | ||
* Get the site setting schema, conforming to JSON Schema. | ||
* | ||
* @return array | ||
*/ | ||
public function get_item_schema() { | ||
$options = $this->get_registered_options(); | ||
|
||
$schema = array( | ||
'$schema' => 'http://json-schema.org/draft-04/schema#', | ||
'title' => 'settings', | ||
'type' => 'object', | ||
'properties' => array(), | ||
); | ||
|
||
foreach ( $options as $option_name => $option ) { | ||
$schema['properties'][ $option_name ] = $option['schema']; | ||
} | ||
|
||
return $this->add_additional_fields_schema( $schema ); | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be moved to
protected $namespace = '...'
instead.