Repository navigation
Abilities API: Add the core/settings-update ability - #10892
jorgefilipecosta wants to merge 22 commits 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. |
|
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
d129749 to
c20d3ff
Compare
core/settings-update ability
c20d3ff to
ad65fc0
Compare
…epeated capability check in `core/settings-update`. A false value written over a stored value no longer adds the option again, which could change its autoload value. A value equal to the default that get_option() reads is now added only when no value is stored. The execute callback no longer checks `manage_options` again. The required capability decides who can run the ability, so a `wp_ability_permission_result` filter that grants the call, or changed required capabilities, now also allow the update. Setting the privacy policy page still requires `manage_privacy_options`. See #64616.
…allback in `core/settings-update`. The privacy policy page check now runs as the permission callback of the ability, so `check_permissions()` reports it before the ability runs and the `wp_ability_permission_result` filter applies to it. A refused call now returns `ability_invalid_permissions`. A null value is now refused while the setting has a stored value that `core/settings-get` leaves out, also for a setting with a default. A schema with a top-level `anyOf` or `oneOf` now accepts null through an `anyOf` with a null schema, and a schema whose type is already a list gets null added to that list. The site address (`home`) is now read-only too, and the read-only and capability rules compare the option name trimmed, as the options API stores it. An empty `default_post_format` is now accepted as the standard format, as `0` is. Also addresses review feedback: * The execute callback sanitizes its input, as the permission callback does. * The value schema helper has PHPStan shapes. * The docblocks say that a callback that runs while a value is saved can still stop the call after earlier settings were saved, and that settings exposed before become writable unless their schema sets `readonly` to true. * A comment notes that a page in a status other than the trash or an auto-draft is still accepted. * New tests cover the permission filter, unprepared input, the read-only options and the combining keywords. See #64616.
…t and the default category in `core/settings-update`. These settings have no registered default, so a null value deleted them, and without a value dates and times show as empty strings and new posts get no category. Their input schema no longer accepts null, whatever name exposes them. They can still be updated with a value. See #64616.
…gs-update`. When no value is stored and the sanitized value matches the default that get_option() reads, update_option() stores nothing, so requests that do not register the setting read false. The default that reads is now stored whenever a write leaves no stored value, for example `-10` for the number of posts per page. Also addresses review feedback: * The input schema of a setting drops the default in one place. * The execute callback keeps the values to write apart from the input. * The `register_setting()` docblock no longer says "including settings exposed before", and names the settings that cannot be updated or reset. * The comment in wp_register_core_abilities() names both settings abilities. See #64616.
…ore/settings-update`. The read-only, reset and privacy policy page rules now compare the option name in lower case, the way the database matches it, to fix possible inconsistencies. Also addresses review feedback: * The ability description, the `register_setting()` docblock and a test docblock name the WordPress address as read-only. * An invalid stored value is reported with its own message instead of "Invalid parameter(s)". * The language and page value errors reuse existing translated strings. * The repeated paragraphs in register() are merged. * The value schema helpers drop the unused list type handling, and their PHPStan shapes say the type is a single type. * The settings tests share one administrator instead of creating one per call. See #64616.
… of `core/settings-update`. The value rules and the `en_US` language mapping now compare the option name in lower case, the way the read-only, reset and privacy policy page rules do, to fix possible inconsistencies. Also simplifies the tests: * The value rule test covers each option registered in another case. * The tests that store a core default are rows of the read-default data provider, which now also checks that no other option changes. See #64616.
…ngs-update`. The value schema helper now adds `null` to the type with `array_merge()`, so a type that is already a list stays a flat list instead of being nested. Its PHPStan shapes accept a single type or a list of types. See #64616.
…e settings abilities. The output schema helper of `WP_Abilities_Settings` is private, so no caller needs to know that it was renamed and got new parameters in 7.3.0. See #64616.
…ttings abilities. The array shape of an exposed setting was repeated in six `@var`, `@param` and `@return` tags of `WP_Abilities_Settings`. It is now declared once as a PHPStan type on the class. The standard tags describe a plain array, and `@phpstan-var`, `@phpstan-param` and `@phpstan-return` tags carry the exact shape. See #64616.
…s with a verb. Renames two private methods of `WP_Abilities_Settings`, so every method of the class starts with a verb: * `nullable_schema()` is now `get_nullable_schema()`. * `value_schema()` is now `get_value_schema()`. See #64616.
The docblocks of `get_update_value_schema()`, `get_nullable_schema()` and `get_invalid_value_message()` described each step of the code, so a change to the code also meant a change to the documentation. They now keep the summary, and the reasons that are not clear from the code are inline comments next to the lines they explain. See #64616.
…ettings-update`. `show_on_front`, `default_comment_status` and `default_ping_status` have no registered default, so a null value deleted them. Without a value, `is_front_page()` is false on the posts home page, and new posts get an empty comment and ping status. They are now in the list of options that cannot be reset, next to `date_format`, `time_format` and `default_category`. The description of the ability and of `data` no longer name the settings. They say that a setting whose schema does not accept null cannot be reset. In the tests, a test setting that lists its allowed values takes the place of `default_ping_status` where a setting that can be reset is needed. See #64616.
8e15447 to
99d4a08
Compare
…level. Adds `src/wp-includes/abilities` to the paths of `phpstan-strict.neon.dist`, so that new code in the core abilities is held to rule level 10. The 21 errors that the existing code reports at that level are added to the strict baselines with `composer phpstan:baselines`: 12 in `WP_Abilities_Content`, 4 in `WP_Abilities_Settings` and 5 in `WP_Abilities_Users`. See #64616.
|
Hi @westonruter, thank you for checking, I added src/wp-includes/abilities to the strict config and the 21 existing errors to the baselines. |
|
Hi @gziolo, thank you for testing everything again 😊 I agree with the final shape, each setting should declare its rules when it is registered. I applied the NON_RESETTABLE_OPTIONS note and the style comments. For now I would keep the lists in the ability, as they are private and can be removed later, and then we work on the long term solution, I added it to the follow-up work in the PR description. |
|
Hi @gziolo, thank you for the reviews, I applied all the feedback or tracked it as follow-up work in the PR description. |
|
@jorgefilipecosta, sounds good to me. I will do another pass tomorrow with the goal to approve with the marked follow-up task as discussed 👍🏻 |
|
Thanks for the updates! I tested changes locally and everything works as expected. That addresses the remaining issue from my review. I’m happy to approve with the follow-up work listed in the PR description. Before commit, could you copy that list to Trac #64616 and link any existing tickets there? That will give contributors one place to find the remaining work after this lands. We most likely want to keep the ticket open until all follow-up tasks are resolved. |
|
Thank you for the review @gziolo, the follow-up tasks are now listed at https://core.trac.wordpress.org/ticket/64616#comment:33. |
Add a `core/settings-update` ability that updates the settings registered with `show_in_abilities`, named as in `core/settings-get`, and returns their saved values. A `null` value resets a setting to its default. Values are validated before anything is saved, and the ability requires the `manage_options` capability. Developed in #10892. Props jorgefilipecosta, gziolo, westonruter, justlevine. See #64616. git-svn-id: https://develop.svn.wordpress.org/trunk@65861 602fd350-edb4-49c9-b593-d223f7449a82
Add a `core/settings-update` ability that updates the settings registered with `show_in_abilities`, named as in `core/settings-get`, and returns their saved values. A `null` value resets a setting to its default. Values are validated before anything is saved, and the ability requires the `manage_options` capability. Developed in WordPress/wordpress-develop#10892. Props jorgefilipecosta, gziolo, westonruter, justlevine. See #64616. Built from https://develop.svn.wordpress.org/trunk@65861 git-svn-id: http://core.svn.wordpress.org/trunk@64890 1a063a9b-81f0-0310-95a4-ce76da25c4cd
|
Closed committed at WordPress/WordPress@29b6043. |
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 resets a setting to its default, and stores the default so requests that do not register the setting read it too. A setting without a default has its stored value deleted, exceptdate_format,time_format,default_category,show_on_front,default_comment_statusanddefault_ping_status, which refusenullbecause Core needs their values.settings_invalid_value, and the error data maps each refused setting to its message underparams. This covers an unknowntimezone, alanguagethat is not installed, an unknowndefault_post_format, a page setting that is not 0 or the ID of a page, or is a page in the trash or an auto-draft, and adefault_categorythat is not the ID of a category.show_in_abilitiescan be updated, except the read-only ones below, including the settings a plugin exposed forcore/settings-getbefore this ability existed. This is intended, and no separate opt-in for writing is added.readonlytotruein its schema: it can be read withcore/settings-get, but not updated.url), the site address and the administration email address (email) are read-only for this ability, under whatever name a plugin exposes them. Settings > General only changes the administration email address after the new address confirms it.manage_optionscapability, and changing the privacy policy page also requiresmanage_privacy_options.src/wp-includes/abilitiesis added to the strict PHPStan configuration, with the existing errors in the baselines.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
Follow-up work
nulland that have value rules. The long-term solution is for each setting to declare its rules when it is registered. The lists are private, so they can be removed then:urlandemailsetreadonlywhere they are registered, inregister_initial_settings(), and the list of read-only options is removed from the ability. Abilities API: Allow marking ashow_in_abilitiesobject as read only #16288 adds areadonlymarker toshow_in_abilities, so this comes after it, with a single marker.null: avalidate_callbackargument ofregister_setting(), next tosanitize_callback, thatcore/settings-updatecalls before saving.date_format,time_format,default_category,show_on_front,default_comment_statusanddefault_ping_statushave no registered default, so they refusenullhere, while the settings REST route deletes them.start_of_weekhas no registered default either, andnulldeletes it here and in the settings REST route, so the week then starts on Sunday instead of the install default (Monday in English). Registering a reset value for each would let both reset them.timezone_stringis exposed (timezone). A site on a fixed offset, such as UTC+2, stores it ingmt_offsetand keepstimezone_stringempty, so the abilities show an empty timezone and cannot change the offset. Exposinggmt_offsetalone is not safe, because core calculates it fromtimezone_string, and only the Settings > General form keeps the two in sync.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).