-
Notifications
You must be signed in to change notification settings - Fork 8
Conversation
In https://core.trac.wordpress.org/ticket/37885 I have added a patch to make the registered settings be imbued with more data for inclusion in the REST API. This is a work in progress to see how the controller might look for that.
this is until core implements is, hopefully!
Another round of changes:
register_setting( 'general', 'blogname', array(
'show_in_rest' => array(
'name' => 'title',
),
'type' => 'string',
'description' => __( 'Site title.' ),
) );
The response so far looks like this: {
"date_format": "F j, Y",
"default_category": 1,
"default_post_format": "0",
"description": "Just another Fake Site site",
"email": "[email protected]",
"language": "en_US",
"posts_per_page": 10,
"start_of_week": 1,
"time_format": "g:i a",
"timezone": "",
"title": "",
"url": "http://wordpress.dev",
"use_smilies": true
} Updating a setting looks like this: http POST http://wordpress.dev/wp-json/wp/v2/settings title="This is the new title" Right now I'm registering core settings in this plugin, but ideally this would be moved to core. This all depends on buy-in from https://core.trac.wordpress.org/ticket/37885 of course. |
Deleting OptionsThere's no way to "DELETE" an option from a REST API perspective, as an setting is part of the item schema and fixed, you can however "null" out a setting value which will essentially reset it back to the default. To do that, the client just has to set a setting to |
Aside: the WordPress.com mobile app makes use of Site Title, Description and Site URL. |
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.
Seems good to me, apart from some stylistic points here.
class WP_REST_Settings_Controller extends WP_REST_Controller { | ||
public function __construct() { | ||
$this->namespace = 'wp/v2'; | ||
$this->rest_base = 'settings'; |
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.
'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 comment
The reason will be displayed to describe this comment to others. Learn more.
Alignment is off
'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 comment
The reason will be displayed to describe this comment to others. Learn more.
Alignment
) ); | ||
} | ||
|
||
public function get_item_permissions_check( $request ) { |
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.
Needs phpDoc
$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 comment
The reason will be displayed to describe this comment to others. Learn more.
Comments need to start with a capital and end with a period.
$site_route->register_routes(); | ||
} | ||
|
||
function rest_register_settings() { |
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.
Needs phpDoc
} | ||
|
||
function rest_register_settings() { | ||
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
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.
💀 💯
), | ||
'type' => 'string', | ||
'description' => __( 'Site title.' ), | ||
) ); |
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.
The space here isn't necessary.
|
||
register_setting( 'general', 'blogname', array( | ||
'show_in_rest' => array( | ||
'name' => 'title', |
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.
Alignment is off here (and below)
'name' => 'language', | ||
), | ||
'type' => 'string', | ||
'description' => __( 'IETF "like" WordPress locale code.' ), |
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.
IETF-like
should be hyphenated. I'd probably change this to WordPress locale code.
, no need to mention the spec, since it has to be from the defined list of WP-valid locales anyway.
} | ||
|
||
$rest_args = array_merge( $rest_args, array( | ||
'option_name' => $name, |
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.
This shouldn't be overridable, since the data is contained in the registration for the option itself.
Thanks for the review @rmccue, I'll wait on the outcome of |
@rmccue ok patched up those and also added unit tests here. |
$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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in dcacb38
* @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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 5008108
Think we are looking good here @rmccue |
* @param string $type Type that the data should be converted to. | ||
* @return mixed | ||
*/ | ||
protected function cast_value_to_type( $value, $type ) { |
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.
Would this be a useful utility function somewhere else?
|
||
register_setting( 'general', 'WPLANG', array( | ||
'show_in_rest' => array( | ||
'name' => 'language', |
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.
Is a locale always a language?
This reads pretty well 👍 Left a couple small comments. |
Let's split this into a new ticket. |
Merged in from https://github.com/WP-API/wp-api-site-endpoints. The bulk of this comes from WP-API/wp-api-site-endpoints#13
In https://core.trac.wordpress.org/ticket/37885 I have added a patch to
make the registered settings be imbued with more data for inclusion in
the REST API. This is a work in progress to see how the controller might
look for that.