New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment with registered settings #13

Merged
merged 16 commits into from Sep 22, 2016

Conversation

Projects
None yet
3 participants
@joehoyle
Collaborator

joehoyle commented Aug 30, 2016

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.

joehoyle added some commits Aug 30, 2016

Experiment with registered settings
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.
Register default settings
this is until core implements is, hopefully!
@joehoyle

This comment has been minimized.

Show comment
Hide comment
@joehoyle

joehoyle Sep 14, 2016

Collaborator

Another round of changes:

  1. Renamed to WP_REST_Settings_Controller along with the route moving to /settings per #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": "joe@hmn.md",
    "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.

Collaborator

joehoyle commented Sep 14, 2016

Another round of changes:

  1. Renamed to WP_REST_Settings_Controller along with the route moving to /settings per #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": "joe@hmn.md",
    "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

This comment has been minimized.

Show comment
Hide comment
@joehoyle

joehoyle Sep 14, 2016

Collaborator

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.

Collaborator

joehoyle commented Sep 14, 2016

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

This comment has been minimized.

Show comment
Hide comment
@joehoyle

joehoyle Sep 14, 2016

Collaborator

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

Collaborator

joehoyle commented Sep 14, 2016

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

@rmccue

Seems good to me, apart from some stylistic points here.

Show outdated Hide outdated lib/class-wp-rest-settings-controller.php Outdated
Show outdated Hide outdated lib/class-wp-rest-settings-controller.php Outdated
Show outdated Hide outdated lib/class-wp-rest-settings-controller.php Outdated
Show outdated Hide outdated lib/class-wp-rest-settings-controller.php Outdated
Show outdated Hide outdated lib/class-wp-rest-settings-controller.php Outdated
Show outdated Hide outdated plugin.php Outdated
Show outdated Hide outdated plugin.php Outdated
Show outdated Hide outdated plugin.php Outdated
Show outdated Hide outdated plugin.php Outdated
Show outdated Hide outdated plugin.php Outdated

@kadamwhite kadamwhite referenced this pull request Sep 15, 2016

Closed

Options Endpoint #2673

Show outdated Hide outdated lib/class-wp-rest-settings-controller.php Outdated
@joehoyle

This comment has been minimized.

Show comment
Hide comment
@joehoyle

joehoyle Sep 19, 2016

Collaborator

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.

Collaborator

joehoyle commented Sep 19, 2016

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

This comment has been minimized.

Show comment
Hide comment
@joehoyle

joehoyle Sep 22, 2016

Collaborator

@rmccue ok patched up those and also added unit tests here.

Collaborator

joehoyle commented Sep 22, 2016

@rmccue ok patched up those and also added unit tests here.

Show outdated Hide outdated lib/class-wp-rest-settings-controller.php Outdated
Show outdated Hide outdated lib/class-wp-rest-settings-controller.php Outdated
@joehoyle

This comment has been minimized.

Show comment
Hide comment
@joehoyle

joehoyle Sep 22, 2016

Collaborator

Think we are looking good here @rmccue

Collaborator

joehoyle commented Sep 22, 2016

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 ) {

This comment has been minimized.

@danielbachhuber

danielbachhuber Sep 22, 2016

Member

Would this be a useful utility function somewhere else?

@danielbachhuber

danielbachhuber Sep 22, 2016

Member

Would this be a useful utility function somewhere else?

register_setting( 'general', 'WPLANG', array(
'show_in_rest' => array(
'name' => 'language',

This comment has been minimized.

@danielbachhuber

danielbachhuber Sep 22, 2016

Member

Is a locale always a language?

@danielbachhuber

danielbachhuber Sep 22, 2016

Member

Is a locale always a language?

@danielbachhuber

This comment has been minimized.

Show comment
Hide comment
@danielbachhuber

danielbachhuber Sep 22, 2016

Member

This reads pretty well 👍 Left a couple small comments.

Member

danielbachhuber commented Sep 22, 2016

This reads pretty well 👍 Left a couple small comments.

@rmccue

rmccue approved these changes Sep 22, 2016

@rmccue rmccue merged commit 18c14e4 into master Sep 22, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@rmccue rmccue deleted the register-setting-experiment branch Sep 22, 2016

@rmccue

This comment has been minimized.

Show comment
Hide comment
@rmccue

rmccue Sep 22, 2016

Member

Would this be a useful utility function somewhere else?

Let's split this into a new ticket.

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 join this conversation on GitHub. Already have an account? Sign in to comment