Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/wp-includes/abilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
17 changes: 13 additions & 4 deletions src/wp-includes/class-wp-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'] ) {
Expand Down
8 changes: 6 additions & 2 deletions src/wp-includes/option.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()`.
* }
Expand Down Expand Up @@ -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.
* }
Expand Down
6 changes: 4 additions & 2 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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'.
Expand Down
175 changes: 175 additions & 0 deletions tests/phpunit/tests/abilities-api/wpIsWritableThroughAbilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php

declare( strict_types=1 );

/**
* Tests for wp_is_writable_through_abilities().
*
* @covers ::wp_is_writable_through_abilities
*
* @group abilities-api
*/
class Tests_Abilities_API_WpIsWritableThroughAbilities extends WP_UnitTestCase {

/**
* Returns `show_in_abilities` values that allow writes.
*
* @return array<string, array{show_in_abilities: mixed}> 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<string, array{show_in_abilities: mixed}> 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<string, array{show_in_abilities: mixed, expected: bool}> 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 ) );
}
}
Loading
Loading