Repository navigation
Abilities API: Add the core/settings-update ability - #10892
jorgefilipecosta wants to merge 1 commit into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
eb0ef04 to
e582d44
Compare
|
Thank you @westonruter for reviewing this PR, all your suggestions were applied. |
| * @return array<string, array<string, mixed>|object>|WP_Error Updated settings on success, WP_Error on failure. | ||
| */ | ||
| public static function execute_update_settings( $input = array() ) { | ||
| $input = is_array( $input ) ? $input : array(); |
There was a problem hiding this comment.
The @param for $input says it is an array. Is this case here in case an external request is submitting a non-array value? When would an non-array be passed? I realize this is the same as the execute_get_settings() method.
|
|
||
| if ( empty( $input['settings'] ) || ! is_array( $input['settings'] ) ) { | ||
| return array( | ||
| 'updated_settings' => (object) array(), |
There was a problem hiding this comment.
Why are we coercing an empty array into an object?
There was a problem hiding this comment.
To force the JSON encoding to be {} as opposed to [].
There was a problem hiding this comment.
Sorry I should have been clear I'm asking from an API perspective not the technical impl. I should have asked: why feed it an empty anything (I only saw that resolved thread afterwards 😅)
If there's isn't a strong reason, I'd suggest we just make $payload['updated_settings'] : array<string,mixed>|null . It's
- more self documenting (the possibility of no updated settings),
- more performant to check (a falsy check you don't need to unpack),
- more resilient to user/llm error (since they only need to do the falsely check to access the internals without erroring),
- lets us bypass all these ugly coercions from WP using multidimensional arrays by just setting these to
null.
(I understand none of these are big concerns individually, but papercuts add up in api design, why I'm asking if there's any downsides to consider)
| * @return array<string, array<string, mixed>|object>|WP_Error Updated settings on success, WP_Error on failure. | ||
| */ | ||
| public static function execute_update_settings( $input = array() ): array { |
There was a problem hiding this comment.
- Abilities API: Add the
core/settings-updateability #10892 (comment) - If you want to return a \WP_Error here, you need to remove the hardcoded
arrayfrom the method signature.
There was a problem hiding this comment.
Also, why is this entire class (including the properties) static?
It makes me really nervous that folks can (ab)use these public static functions anywhere and not just as part of the class instantiation lifecycle.
If it's just about caching the schemas, we can keep the props private static without it affecting our public API.
|
|
||
| $args = $allowed_settings[ $option_name ]; | ||
|
|
||
| $setting_group = $args['group'] ?? 'general'; |
There was a problem hiding this comment.
Is the concern her just that $args['group] might be unset or can it also contain an empty string we need to coerce to our general fallback?
|
|
||
| // Cast values for comparison (handles type mismatches from database and REST sanitization). | ||
| $current_value = self::cast_value( get_option( $option_name ), $setting_type ); | ||
| $sanitized_value = self::cast_value( $sanitized_value, $setting_type ); |
There was a problem hiding this comment.
Why do we need to cast $sanitized_value, and more importantly why would we cast it after we update the option?
|
Related to https://core.trac.wordpress.org/ticket/64605, but since that got merged I'll ask here. How exactly does Is |
6ef6754 to
d129749
Compare
There was a problem hiding this comment.
Was it intentional to include this auto-generated inspector controls functionality with this PR?
There was a problem hiding this comment.
Sorry that was for other from and the changes were included by mistake
Adds `core/settings-update`, which updates the settings exposed with the `show_in_abilities` argument and returns the saved values of the settings named in the call. A `null` value deletes the stored value of a setting, which then reads as its default when it has one. A setting whose schema sets `readonly` to true can be read with `core/settings-get`, but is left out of the update input. The ability requires the `manage_options` capability, and changing the privacy policy page also requires `manage_privacy_options`. See #64616.
d129749 to
c20d3ff
Compare
core/settings-update ability
Part of: WordPress/ai#40
Inspired by the work on https://github.com/galatanovidiu/mcp-adapter-implementation-example/tree/experiment/layerd-mcp-tools/includes/Abilities by @galatanovidiu.
Follow-up to #12141, with the shape from WordPress/abilities#22.
This PR adds the
core/settings-updateability to thesitecategory, for the settings registered withshow_in_abilities:{ data }, with one property per setting to change, named as incore/settings-get, and returns the saved values of those settings.nullvalue deletes the stored value of a setting, which then reads as its default when it has one.readonlytotruecan be read withcore/settings-get, but not updated.manage_optionscapability, and changing the privacy policy page also requiresmanage_privacy_options.Updating some settings:
{ "title": "Launch notes", "description": "News about the launch", "posts_per_page": 5 }Going back to the default with
null:{ "posts_per_page": 10 }Trac ticket: https://core.trac.wordpress.org/ticket/64616
Testing
Verify unit tests are passing:
Or run the examples above in the editor console as an administrator.
AI disclosure: the change and tests were drafted with AI assistance (Claude Code).