Skip to content
This repository has been archived by the owner on Aug 14, 2019. It is now read-only.

Experiment with registered settings #13

Merged
merged 16 commits into from
Sep 22, 2016
Merged

Conversation

joehoyle
Copy link
Member

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.

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!
@joehoyle
Copy link
Member Author

joehoyle commented Sep 14, 2016

Another round of changes:

  1. Renamed to WP_REST_Settings_Controller along with the route moving to /settings per Maybe find a better name for /site/? #14.
  2. We now make use of register_setting( ..., ..., array( 'show_in_rest' => array( ...rest options ) ) ) to pass options for the setting in the rest api.
  3. We merge in values from the register_setting like description and type into the show_in_rest args.
  4. Registering a setting in the API now looks like this:
register_setting( 'general', 'blogname', array(
    'show_in_rest'   => array(
        'name'            => 'title',
    ),
    'type'           => 'string',
    'description'    => __( 'Site title.' ),
) );

name is the property name of the setting in the rest api.

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.

@joehoyle
Copy link
Member Author

Deleting Options

There'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 null.

@joehoyle
Copy link
Member Author

Aside: the WordPress.com mobile app makes use of Site Title, Description and Site URL.

Copy link
Member

@rmccue rmccue left a 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';
Copy link
Member

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' ),
Copy link
Member

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' ),
Copy link
Member

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 ) {
Copy link
Member

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"
Copy link
Member

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() {
Copy link
Member

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';
Copy link
Member

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.' ),
) );
Copy link
Member

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',
Copy link
Member

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.' ),
Copy link
Member

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,
Copy link
Member

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.

@joehoyle
Copy link
Member Author

Thanks for the review @rmccue, I'll wait on the outcome of register_setting on trac today before fixing this up as that might come with some extra work.

@joehoyle
Copy link
Member Author

@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 ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be flipped.

Copy link
Member Author

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spacing is off.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5008108

@joehoyle
Copy link
Member Author

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 ) {
Copy link
Member

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',
Copy link
Member

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?

@danielbachhuber
Copy link
Member

This reads pretty well 👍 Left a couple small comments.

@rmccue rmccue merged commit 18c14e4 into master Sep 22, 2016
@rmccue rmccue deleted the register-setting-experiment branch September 22, 2016 23:04
@rmccue
Copy link
Member

rmccue commented Sep 22, 2016

Would this be a useful utility function somewhere else?

Let's split this into a new ticket.

joehoyle added a commit to WP-API/WP-API that referenced this pull request Sep 28, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants