From bbd2cf8786b784292f534c45124db478b944c02c Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 27 Nov 2026 11:09:50 +0000 Subject: [PATCH] Abilities API: Allow marking a `show_in_abilities` object as read only. Post types accept `array( 'readonly' => true )` for `show_in_abilities`, which keeps them readable through abilities and marks them read only. Settings accept the same `readonly` key in their `show_in_abilities` array. The new `wp_is_writable_through_abilities()` function tells write abilities whether a `show_in_abilities` value allows writes. See #66844. --- src/wp-includes/abilities.php | 30 +++ src/wp-includes/class-wp-post-type.php | 17 +- src/wp-includes/option.php | 8 +- src/wp-includes/post.php | 6 +- .../wpIsWritableThroughAbilities.php | 175 ++++++++++++++++++ .../wpRegisterCoreContentAbility.php | 103 +++++++++-- .../wpRegisterCoreSettingsGetAbility.php | 35 ++++ 7 files changed, 355 insertions(+), 19 deletions(-) create mode 100644 tests/phpunit/tests/abilities-api/wpIsWritableThroughAbilities.php diff --git a/src/wp-includes/abilities.php b/src/wp-includes/abilities.php index 835c19e04f949..a3afc97b32d4d 100644 --- a/src/wp-includes/abilities.php +++ b/src/wp-includes/abilities.php @@ -380,3 +380,33 @@ function wp_register_core_abilities(): void { // Register the content abilities (currently the read-only `core/content-query`). ( new WP_Abilities_Content() )->register(); } + +/** + * Determines whether a `show_in_abilities` value lets abilities write the object it is set on. + * + * Write abilities check the `show_in_abilities` value of each post type, setting, or other + * object they change, so that an object marked read only can be read through abilities but + * not created, updated, or deleted. + * + * `true` and an array without a truthy `readonly` key allow writes. An array with a truthy + * `readonly` key, an empty array, and any other value do not. + * + * Example: + * + * $post_type_object = get_post_type_object( 'book' ); + * if ( $post_type_object && wp_is_writable_through_abilities( $post_type_object->show_in_abilities ) ) { + * // Books can be created, updated, and deleted through abilities. + * } + * + * @since 7.3.0 + * + * @param mixed $show_in_abilities The `show_in_abilities` value of a post type, a setting, or another object. + * @return bool Whether abilities may write the object. + */ +function wp_is_writable_through_abilities( $show_in_abilities ): bool { + if ( is_array( $show_in_abilities ) ) { + return ! empty( $show_in_abilities ) && empty( $show_in_abilities['readonly'] ); + } + + return true === $show_in_abilities; +} diff --git a/src/wp-includes/class-wp-post-type.php b/src/wp-includes/class-wp-post-type.php index 0767deeb34162..838a0ef70e7c8 100644 --- a/src/wp-includes/class-wp-post-type.php +++ b/src/wp-includes/class-wp-post-type.php @@ -390,10 +390,12 @@ final class WP_Post_Type { * Whether this post type should be exposed through the Abilities API. * * Default false. If true, the core content abilities can access the post type's - * posts, subject to the current user's capabilities. + * posts, subject to the current user's capabilities. If an array with `readonly` + * set to true, the posts can be read through abilities but not written. * * @since 7.2.0 - * @var bool $show_in_abilities + * @since 7.3.0 Can be an array with a `readonly` key. + * @var bool|array{readonly: bool} $show_in_abilities */ public $show_in_abilities; @@ -612,8 +614,15 @@ public function set_props( $args ) { $args['rest_namespace'] = 'wp/v2'; } - // Only `true` exposes the post type, so other values, such as arrays, can be given a meaning later. - $args['show_in_abilities'] = true === $args['show_in_abilities']; + /* + * Only `true` and an array with a boolean `readonly` key expose the post type. Other + * keys of the array are dropped, so other values can be given a meaning later. + */ + if ( is_array( $args['show_in_abilities'] ) && is_bool( $args['show_in_abilities']['readonly'] ?? null ) ) { + $args['show_in_abilities'] = array( 'readonly' => $args['show_in_abilities']['readonly'] ); + } else { + $args['show_in_abilities'] = true === $args['show_in_abilities']; + } // If not set, default to the setting for 'show_ui'. if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) { diff --git a/src/wp-includes/option.php b/src/wp-includes/option.php index e53aedeb47ba7..a1ab39179be31 100644 --- a/src/wp-includes/option.php +++ b/src/wp-includes/option.php @@ -3039,6 +3039,7 @@ function _wp_register_initial_settings_for_abilities(): void { * Please consider writing more inclusive code. * @since 6.6.0 Added the `label` argument. * @since 7.2.0 Added the `show_in_abilities` argument. + * @since 7.3.0 The `show_in_abilities` array accepts a `readonly` key. * * @global array $new_allowed_options * @global array $wp_registered_settings @@ -3064,7 +3065,8 @@ function _wp_register_initial_settings_for_abilities(): void { * @type bool|array $show_in_abilities Whether this setting should be exposed through the Abilities API. * When true, it uses the same name and schema as `$show_in_rest`. * It may also be an array with 'name' and 'schema' keys, used instead of - * `$show_in_rest` rather than merged with it. + * `$show_in_rest` rather than merged with it, and a 'readonly' key + * that, when true, lets abilities read the setting but not write it. * Settings registered after abilities initialize are not exposed. * @type mixed $default Default value when calling `get_option()`. * } @@ -3294,7 +3296,9 @@ function unregister_setting( $option_group, $option_name, $deprecated = '' ) { * @type bool|array $show_in_abilities Whether this setting should be exposed through the Abilities API. * When true, it uses the same name and schema as `$show_in_rest`. * It may also be an array with 'name' and 'schema' keys, used - * instead of `$show_in_rest` rather than merged with it. + * instead of `$show_in_rest` rather than merged with it, and a + * 'readonly' key that, when true, lets abilities read the setting + * but not write it. * @type mixed $default Default value when calling `get_option()`. Only present when the * setting was registered with a default. * } diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 6d42c97836dea..fbd4d82858d57 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -1706,6 +1706,7 @@ function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) * @since 5.3.0 The `supports` argument will now accept an array of arguments for a feature. * @since 5.9.0 The `rest_namespace` argument was added. * @since 7.2.0 The `show_in_abilities` argument was added. + * @since 7.3.0 The `show_in_abilities` argument accepts an array with a `readonly` key. * * @global array $wp_post_types List of post types. * @@ -1754,9 +1755,10 @@ function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) * of $show_in_menu. * @type bool $show_in_rest Whether to include the post type in the REST API. Set this to true * for the post type to be available in the block editor. - * @type bool $show_in_abilities Whether to expose this post type through the Abilities API, so the + * @type bool|array $show_in_abilities Whether to expose this post type through the Abilities API, so the * core content abilities can access its posts, subject to the current - * user's capabilities. Default false. + * user's capabilities. Pass `array( 'readonly' => true )` to let + * abilities read the posts but not write them. Default false. * @type string $rest_base To change the base URL of REST API route. Default is $post_type. * @type string $rest_namespace To change the namespace URL of REST API route. Default is wp/v2. * @type string $rest_controller_class REST API controller class name. Default is 'WP_REST_Posts_Controller'. diff --git a/tests/phpunit/tests/abilities-api/wpIsWritableThroughAbilities.php b/tests/phpunit/tests/abilities-api/wpIsWritableThroughAbilities.php new file mode 100644 index 0000000000000..1eb01d30cc1a8 --- /dev/null +++ b/tests/phpunit/tests/abilities-api/wpIsWritableThroughAbilities.php @@ -0,0 +1,175 @@ + Writable test cases. + */ + public static function data_writable_values(): array { + return array( + 'true' => array( + 'show_in_abilities' => true, + ), + 'readonly false' => array( + 'show_in_abilities' => array( 'readonly' => false ), + ), + 'setting array with a name and schema' => array( + 'show_in_abilities' => array( + 'name' => 'custom_name', + 'schema' => array( 'type' => 'string' ), + ), + ), + 'setting array with readonly null' => array( + 'show_in_abilities' => array( + 'name' => 'custom_name', + 'readonly' => null, + ), + ), + 'setting array with an empty readonly' => array( + 'show_in_abilities' => array( + 'schema' => array( 'type' => 'string' ), + 'readonly' => '', + ), + ), + ); + } + + /** + * Tests that `true` and arrays without a truthy `readonly` key allow writes. + * + * @ticket 66844 + * @dataProvider data_writable_values + * @since 7.3.0 + * + * @param mixed $show_in_abilities The `show_in_abilities` value. + */ + public function test_allows_writes( $show_in_abilities ): void { + $this->assertTrue( wp_is_writable_through_abilities( $show_in_abilities ) ); + } + + /** + * Returns `show_in_abilities` values that do not allow writes. + * + * @return array Read only and unexposed test cases. + */ + public static function data_values_that_are_not_writable(): array { + return array( + 'readonly true' => array( + 'show_in_abilities' => array( 'readonly' => true ), + ), + 'setting array with readonly true' => array( + 'show_in_abilities' => array( + 'name' => 'custom_name', + 'schema' => array( 'type' => 'string' ), + 'readonly' => true, + ), + ), + 'readonly as the string "false"' => array( + 'show_in_abilities' => array( 'readonly' => 'false' ), + ), + 'readonly as integer 1' => array( + 'show_in_abilities' => array( 'readonly' => 1 ), + ), + 'false' => array( + 'show_in_abilities' => false, + ), + 'null' => array( + 'show_in_abilities' => null, + ), + 'empty array' => array( + 'show_in_abilities' => array(), + ), + 'integer 1' => array( + 'show_in_abilities' => 1, + ), + 'string "true"' => array( + 'show_in_abilities' => 'true', + ), + 'object with readonly false' => array( + 'show_in_abilities' => (object) array( 'readonly' => false ), + ), + ); + } + + /** + * Tests that arrays with a truthy `readonly` key and values that do not expose an object + * do not allow writes. + * + * @ticket 66844 + * @dataProvider data_values_that_are_not_writable + * @since 7.3.0 + * + * @param mixed $show_in_abilities The `show_in_abilities` value. + */ + public function test_does_not_allow_writes( $show_in_abilities ): void { + $this->assertFalse( wp_is_writable_through_abilities( $show_in_abilities ) ); + } + + /** + * Returns `show_in_abilities` values for a post type, and whether abilities may write + * its posts. + * + * @return array Post type test cases. + */ + public static function data_post_type_values(): array { + return array( + 'true' => array( + 'show_in_abilities' => true, + 'expected' => true, + ), + 'readonly false' => array( + 'show_in_abilities' => array( 'readonly' => false ), + 'expected' => true, + ), + 'readonly true' => array( + 'show_in_abilities' => array( 'readonly' => true ), + 'expected' => false, + ), + 'non-boolean readonly' => array( + 'show_in_abilities' => array( 'readonly' => 0 ), + 'expected' => false, + ), + 'array without readonly' => array( + 'show_in_abilities' => array( 'create' ), + 'expected' => false, + ), + 'false' => array( + 'show_in_abilities' => false, + 'expected' => false, + ), + ); + } + + /** + * Tests the helper with the value a registered post type holds. + * + * @ticket 66844 + * @dataProvider data_post_type_values + * @since 7.3.0 + * + * @param mixed $show_in_abilities The `show_in_abilities` value to register the post type with. + * @param bool $expected Whether abilities may write the post type's posts. + */ + public function test_post_type_value( $show_in_abilities, bool $expected ): void { + register_post_type( + 'writable_cpt', + array( + 'public' => true, + 'show_in_abilities' => $show_in_abilities, + ) + ); + + $this->assertSame( $expected, wp_is_writable_through_abilities( get_post_type_object( 'writable_cpt' )->show_in_abilities ) ); + } +} diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php index cf537f5cf0911..808c5c041e19a 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php @@ -938,38 +938,119 @@ public function test_exposes_a_post_type_registered_by_another_plugin(): void { } /** - * Returns `show_in_abilities` values other than `true`. + * Returns `show_in_abilities` arrays that expose a post type, and the value the post type + * object holds. * - * @return array Non-boolean `show_in_abilities` test cases. + * @return array, expected: array{readonly: bool}}> `show_in_abilities` array test cases. */ - public static function data_show_in_abilities_values_other_than_true(): array { + public static function data_show_in_abilities_arrays(): array { return array( - 'false' => array( + 'read only' => array( + 'value' => array( 'readonly' => true ), + 'expected' => array( 'readonly' => true ), + ), + 'not read only' => array( + 'value' => array( 'readonly' => false ), + 'expected' => array( 'readonly' => false ), + ), + 'read only with other keys' => array( + 'value' => array( + 'readonly' => true, + 'create' => true, + ), + 'expected' => array( 'readonly' => true ), + ), + ); + } + + /** + * A post type exposed with a `show_in_abilities` array, read only or not, can be queried. + * + * @ticket 66844 + * @dataProvider data_show_in_abilities_arrays + * @since 7.3.0 + * + * @param array $value The `show_in_abilities` value to register the post type with. + * @param array{readonly: bool} $expected The value the post type object should hold. + */ + public function test_exposes_a_post_type_with_a_show_in_abilities_array( array $value, array $expected ): void { + register_post_type( + 'content_cpt', + array( + 'public' => true, + 'show_in_abilities' => $value, + ) + ); + + $this->assertSame( $expected, get_post_type_object( 'content_cpt' )->show_in_abilities, 'The post type object should hold only the readonly key.' ); + + $post_id = self::factory()->post->create( + array( + 'post_type' => 'content_cpt', + 'post_status' => 'publish', + ) + ); + + $this->login_as( 'administrator' ); + $this->register_ability(); + + // The `type` enum of `where` lists the exposed post types. + $enum = wp_get_ability( 'core/content-query' )->get_input_schema()['properties']['where']['properties']['type']['enum']; + $this->assertContains( 'content_cpt', $enum, 'The post type should appear in the post type enum.' ); + + $result = wp_get_ability( 'core/content-query' )->execute( + array( + 'where' => array( + 'type' => 'content_cpt', + 'ids' => array( $post_id ), + ), + ) + ); + $this->assertNotWPError( $result, 'A query of the post type should succeed.' ); + $this->assertSame( array( $post_id ), wp_list_pluck( $result['items'], 'id' ), 'The post should be readable by ID.' ); + } + + /** + * Returns `show_in_abilities` values that do not expose a post type. + * + * @return array Unsupported `show_in_abilities` test cases. + */ + public static function data_unsupported_show_in_abilities_values(): array { + return array( + 'false' => array( 'value' => false, ), - 'array of operations' => array( + 'array of operations' => array( 'value' => array( 'create' ), ), - 'string "false"' => array( + 'non-boolean readonly' => array( + 'value' => array( 'readonly' => 'true' ), + ), + 'empty array' => array( + 'value' => array(), + ), + 'string "false"' => array( 'value' => 'false', ), - 'integer 1' => array( + 'integer 1' => array( 'value' => 1, ), ); } /** - * Only a `show_in_abilities` value of `true` exposes a post type, so other values, such as - * arrays, can be given a meaning later. + * Only `true` and an array with a boolean `readonly` key expose a post type, so other + * values can be given a meaning later. * * @ticket 66268 - * @dataProvider data_show_in_abilities_values_other_than_true + * @ticket 66844 + * @dataProvider data_unsupported_show_in_abilities_values * @since 7.2.0 + * @since 7.3.0 An array with a boolean `readonly` key exposes the post type. * * @param mixed $value The `show_in_abilities` value to register the post type with. */ - public function test_does_not_expose_a_post_type_with_a_show_in_abilities_value_other_than_true( $value ): void { + public function test_does_not_expose_a_post_type_with_an_unsupported_show_in_abilities_value( $value ): void { register_post_type( 'content_cpt', array( diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsGetAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsGetAbility.php index 016bca1b7a756..bc5fdd78cfcb7 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsGetAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsGetAbility.php @@ -464,6 +464,41 @@ public function test_core_settings_get_exposes_a_custom_registered_setting(): vo $this->assertSame( array( 'core_settings_get_ability_test_option' => 7 ), $result, 'A custom setting should be returned with its typed value.' ); } + /** + * A setting marked read only in its `show_in_abilities` array can still be read. + * + * @ticket 66844 + * @since 7.3.0 + */ + public function test_core_settings_get_exposes_a_read_only_setting(): void { + $option = 'core_settings_get_ability_read_only_test_option'; + + register_setting( + 'general', + $option, + array( + 'type' => 'string', + 'show_in_abilities' => array( + 'name' => 'read_only_test_setting', + 'readonly' => true, + ), + ) + ); + update_option( $option, 'value' ); + + try { + $this->register_ability(); + $this->become_admin(); + + $result = wp_get_ability( 'core/settings-get' )->execute( array( 'fields' => array( 'read_only_test_setting' ) ) ); + } finally { + unregister_setting( 'general', $option ); + $this->register_ability(); + } + + $this->assertSame( array( 'read_only_test_setting' => 'value' ), $result, 'A read only setting should be returned under its exposed name.' ); + } + /** * A setting shown in the REST API without `show_in_abilities` is not exposed. *