Skip to content

Commit c2efdbd

Browse files
Abilities API: Polish the content write abilities.
- Add `enum` to the `outcome` of the `core/content-delete` output schema. - Note in the changelog that the `where.status` filter of `core/content-query` accepts `trash`. - Say to set `permanent` to true in the message for posts that cannot be trashed. - Pass the full input path to `invalid_attribute_error()`, as `invalid_filter_error()` takes it. - Rename `save_post()` to `persist_post()`, so it does not read as the `save_post` hook. - Read the statuses that can be set directly, instead of removing `trash` from the queryable ones. - Remove the unused input defaults from the write ability callbacks. - Align comments and docblocks. See #66706.
1 parent 6386a17 commit c2efdbd

2 files changed

Lines changed: 39 additions & 40 deletions

File tree

‎src/wp-includes/abilities/class-wp-abilities-content.php‎

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ final class WP_Abilities_Content {
9898
/**
9999
* The `data` attributes the create and update abilities can write.
100100
*
101-
* Only these reach the save, each mapped to one post field. Other keys are never written.
101+
* An update must send at least one of them. {@see self::prepare_post_data()} maps each
102+
* one that is sent to a post field.
102103
*
103104
* @since 7.3.0
104105
* @var list<string>
@@ -119,7 +120,7 @@ final class WP_Abilities_Content {
119120
* Must run on the `wp_abilities_api_init` hook.
120121
*
121122
* @since 7.2.0
122-
* @since 7.3.0 Registers the `core/content-create`, `core/content-update`, and `core/content-delete` abilities.
123+
* @since 7.3.0 Added the `core/content-create`, `core/content-update`, and `core/content-delete` abilities.
123124
*/
124125
public function register(): void {
125126
/*
@@ -132,8 +133,8 @@ public function register(): void {
132133
return;
133134
}
134135

135-
// The statuses that can be set: the ones the query accepts, other than `trash`.
136-
$statuses = array_values( array_diff( $this->get_queryable_statuses(), array( 'trash' ) ) );
136+
// The statuses that can be set: the ones that are not internal.
137+
$statuses = array_values( get_post_stati( array( 'internal' => false ) ) );
137138

138139
$this->register_content_query( $post_types );
139140
$this->register_content_create( $post_types, $statuses );
@@ -145,7 +146,7 @@ public function register(): void {
145146
* Registers the read-only `core/content-query` ability.
146147
*
147148
* @since 7.2.0
148-
* @since 7.3.0 Added the `$post_types` parameter.
149+
* @since 7.3.0 Added the `$post_types` parameter and support for `trash` in the `where.status` filter.
149150
*
150151
* @param list<string> $post_types Exposed post type names.
151152
*/
@@ -201,10 +202,10 @@ private function register_content_create( array $post_types, array $statuses ):
201202
'category' => self::CATEGORY,
202203
'input_schema' => $this->get_content_create_input_schema( $post_types, $statuses ),
203204
'output_schema' => $this->get_content_output_schema(),
204-
'execute_callback' => function ( $input = array() ) {
205+
'execute_callback' => function ( $input ) {
205206
return $this->execute_content_create( $input );
206207
},
207-
'permission_callback' => function ( $input = array() ): bool {
208+
'permission_callback' => function ( $input ): bool {
208209
return $this->check_create_permission( $input );
209210
},
210211
'meta' => array(
@@ -236,10 +237,10 @@ private function register_content_update( array $post_types, array $statuses ):
236237
'category' => self::CATEGORY,
237238
'input_schema' => $this->get_content_update_input_schema( $post_types, $statuses ),
238239
'output_schema' => $this->get_content_output_schema(),
239-
'execute_callback' => function ( $input = array() ) {
240+
'execute_callback' => function ( $input ) {
240241
return $this->execute_content_update( $input );
241242
},
242-
'permission_callback' => function ( $input = array() ): bool {
243+
'permission_callback' => function ( $input ): bool {
243244
return $this->check_post_permission( $input, 'edit_post' );
244245
},
245246
'meta' => array(
@@ -270,10 +271,10 @@ private function register_content_delete( array $post_types ): void {
270271
'category' => self::CATEGORY,
271272
'input_schema' => $this->get_content_delete_input_schema( $post_types ),
272273
'output_schema' => $this->get_content_delete_output_schema(),
273-
'execute_callback' => function ( $input = array() ) {
274+
'execute_callback' => function ( $input ) {
274275
return $this->execute_content_delete( $input );
275276
},
276-
'permission_callback' => function ( $input = array() ): bool {
277+
'permission_callback' => function ( $input ): bool {
277278
return $this->check_post_permission( $input, 'delete_post' );
278279
},
279280
'meta' => array(
@@ -335,15 +336,14 @@ private function check_query_permission( $input = array() ): bool {
335336
*
336337
* @since 7.3.0
337338
*
338-
* @param mixed $input Optional. The ability input. Default empty array.
339+
* @param mixed $input The unprepared ability input.
339340
* @return bool True if the request may proceed, false otherwise.
340341
*/
341-
private function check_create_permission( $input = array() ): bool {
342+
private function check_create_permission( $input ): bool {
342343
if ( ! is_user_logged_in() ) {
343344
return false;
344345
}
345346

346-
// The input is read defensively: WP_Ability::check_permissions() does not prepare it.
347347
$data = $this->get_input_object( rest_sanitize_object( $input ), 'data' ) ?? array();
348348
$post_type_object = $this->get_exposed_post_type( $data['type'] ?? null );
349349

@@ -360,7 +360,7 @@ private function check_create_permission( $input = array() ): bool {
360360
*
361361
* @since 7.3.0
362362
*
363-
* @param mixed $input The ability input.
363+
* @param mixed $input The unprepared ability input.
364364
* @param string $capability The capability required on the post: `edit_post` or `delete_post`.
365365
* @return bool True if the request may proceed, false otherwise.
366366
*/
@@ -369,7 +369,6 @@ private function check_post_permission( $input, string $capability ): bool {
369369
return false;
370370
}
371371

372-
// The input is read defensively: WP_Ability::check_permissions() does not prepare it.
373372
$where = $this->get_input_object( rest_sanitize_object( $input ), 'where' ) ?? array();
374373
if ( ! $this->get_exposed_post_type( $where['type'] ?? null ) ) {
375374
return false;
@@ -892,6 +891,7 @@ private function execute_content_create( array $input ) {
892891
return $this->not_found_error();
893892
}
894893

894+
// Checked again after the permission callback, whose result a filter can change.
895895
if ( ! current_user_can( $post_type_object->cap->create_posts ) ) {
896896
return new WP_Error(
897897
'content_cannot_create',
@@ -905,7 +905,7 @@ private function execute_content_create( array $input ) {
905905
return $postarr;
906906
}
907907

908-
return $this->save_post( $postarr, $this->normalize_fields( $input ) );
908+
return $this->persist_post( $postarr, $this->normalize_fields( $input ) );
909909
}
910910

911911
/**
@@ -956,7 +956,7 @@ private function execute_content_update( array $input ) {
956956
return $postarr;
957957
}
958958

959-
return $this->save_post( $postarr, $this->normalize_fields( $input ) );
959+
return $this->persist_post( $postarr, $this->normalize_fields( $input ) );
960960
}
961961

962962
/**
@@ -999,7 +999,7 @@ private function execute_content_delete( array $input ) {
999999
if ( ! $this->can_trash( $post ) ) {
10001000
return new WP_Error(
10011001
'content_trash_not_supported',
1002-
__( 'The post does not support trashing. Set "permanent" to delete it.' ),
1002+
__( 'The post does not support trashing. Set `permanent` to true to delete it.' ),
10031003
array( 'status' => 501 )
10041004
);
10051005
}
@@ -1155,15 +1155,15 @@ private function check_supported_attributes( array $data, string $post_type ): ?
11551155
foreach ( $features as $attribute => $feature ) {
11561156
if ( array_key_exists( $attribute, $data ) && ! post_type_supports( $post_type, $feature ) ) {
11571157
return $this->invalid_attribute_error(
1158-
$attribute,
1158+
"data[{$attribute}]",
11591159
/* translators: 1: Attribute name, such as "content", 2: Post type feature, such as "editor". */
11601160
sprintf( __( 'The %1$s attribute is only supported for post types that support the %2$s feature.' ), $attribute, $feature )
11611161
);
11621162
}
11631163
}
11641164

11651165
if ( array_key_exists( 'parent', $data ) && ! is_post_type_hierarchical( $post_type ) ) {
1166-
return $this->invalid_attribute_error( 'parent', __( 'The parent attribute is only supported for hierarchical post types.' ) );
1166+
return $this->invalid_attribute_error( 'data[parent]', __( 'The parent attribute is only supported for hierarchical post types.' ) );
11671167
}
11681168

11691169
return null;
@@ -1193,7 +1193,7 @@ private function prepare_status( array $data, WP_Post_Type $post_type_object, ?W
11931193
* not even when the post already has them.
11941194
*/
11951195
if ( ! $status_object instanceof stdClass || ! empty( $status_object->internal ) ) {
1196-
return $this->invalid_attribute_error( 'status', __( 'The status must be a registered post status that is not internal.' ) );
1196+
return $this->invalid_attribute_error( 'data[status]', __( 'The status must be a registered post status that is not internal.' ) );
11971197
}
11981198

11991199
// Keeping the stored status changes nothing.
@@ -1239,7 +1239,7 @@ private function prepare_author( array $data, WP_Post_Type $post_type_object, ?W
12391239

12401240
$author_id = $this->parse_filter_int( $data['author'], 1 );
12411241
if ( null === $author_id ) {
1242-
return $this->invalid_attribute_error( 'author', __( 'The author must be a user ID.' ) );
1242+
return $this->invalid_attribute_error( 'data[author]', __( 'The author must be a user ID.' ) );
12431243
}
12441244

12451245
// Keeping the stored author changes nothing.
@@ -1263,7 +1263,7 @@ private function prepare_author( array $data, WP_Post_Type $post_type_object, ?W
12631263

12641264
// Intentionally strict: the author must be an existing user who belongs to this site.
12651265
if ( ! get_userdata( $author_id ) || ( is_multisite() && ! is_user_member_of_blog( $author_id ) ) ) {
1266-
return $this->invalid_attribute_error( 'author', __( 'The author must be an existing user of this site.' ) );
1266+
return $this->invalid_attribute_error( 'data[author]', __( 'The author must be an existing user of this site.' ) );
12671267
}
12681268

12691269
return $author_id;
@@ -1289,7 +1289,7 @@ private function prepare_parent( array $data, string $post_type, ?WP_Post $post
12891289
$reason = __( 'The parent must be 0, or a readable post of the same post type that is neither the post itself nor one of its descendants.' );
12901290
$parent_id = $this->parse_filter_int( $data['parent'], 0 );
12911291
if ( null === $parent_id ) {
1292-
return $this->invalid_attribute_error( 'parent', $reason );
1292+
return $this->invalid_attribute_error( 'data[parent]', $reason );
12931293
}
12941294

12951295
// Keeping the stored parent changes nothing.
@@ -1307,11 +1307,11 @@ private function prepare_parent( array $data, string $post_type, ?WP_Post $post
13071307
*/
13081308
$parent = get_post( $parent_id );
13091309
if ( ! $parent instanceof WP_Post || $post_type !== $parent->post_type || ! $this->check_read_permission( $parent ) ) {
1310-
return $this->invalid_attribute_error( 'parent', $reason );
1310+
return $this->invalid_attribute_error( 'data[parent]', $reason );
13111311
}
13121312

13131313
if ( $post && ( $post->ID === $parent->ID || in_array( $post->ID, get_post_ancestors( $parent ), true ) ) ) {
1314-
return $this->invalid_attribute_error( 'parent', $reason );
1314+
return $this->invalid_attribute_error( 'data[parent]', $reason );
13151315
}
13161316

13171317
return $parent_id;
@@ -1346,7 +1346,7 @@ private function prepare_date( array $data, ?WP_Post $post, ?string $status ) {
13461346
}
13471347

13481348
if ( null === $dates ) {
1349-
return $this->invalid_attribute_error( 'date', __( 'The date must be an object with a valid `site` or `gmt` date.' ) );
1349+
return $this->invalid_attribute_error( 'data[date]', __( 'The date must be an object with a valid `site` or `gmt` date.' ) );
13501350
}
13511351

13521352
$fields = array(
@@ -1412,15 +1412,15 @@ private function prepare_slug( array $data, WP_Post_Type $post_type_object, ?WP_
14121412
? current_user_can( 'publish_post', $post->ID )
14131413
: current_user_can( $post_type_object->cap->publish_posts );
14141414
if ( ! $can_publish ) {
1415-
return $this->invalid_attribute_error( 'slug', __( 'The slug of a pending post can only be set by a user who can publish the post.' ) );
1415+
return $this->invalid_attribute_error( 'data[slug]', __( 'The slug of a pending post can only be set by a user who can publish the post.' ) );
14161416
}
14171417
}
14181418

14191419
$slug = sanitize_title( $data['slug'] );
14201420

14211421
// Intentionally strict: a slug that sanitizes to nothing is an error.
14221422
if ( '' === $slug ) {
1423-
return $this->invalid_attribute_error( 'slug', __( 'The slug must contain characters that are valid in a slug.' ) );
1423+
return $this->invalid_attribute_error( 'data[slug]', __( 'The slug must contain characters that are valid in a slug.' ) );
14241424
}
14251425

14261426
/*
@@ -1450,7 +1450,7 @@ private function prepare_slug( array $data, WP_Post_Type $post_type_object, ?WP_
14501450
* @return array<string, mixed>|WP_Error The saved post, only its ID when the current user
14511451
* may not read it, or a WP_Error on failure.
14521452
*/
1453-
private function save_post( array $postarr, array $fields ) {
1453+
private function persist_post( array $postarr, array $fields ) {
14541454
$postarr = wp_slash( $postarr );
14551455
$post_id = isset( $postarr['ID'] ) ? wp_update_post( $postarr, true ) : wp_insert_post( $postarr, true );
14561456

@@ -2280,6 +2280,7 @@ private function get_content_delete_output_schema(): array {
22802280
'properties' => array(
22812281
'outcome' => array(
22822282
'type' => 'string',
2283+
'enum' => array( 'trashed', 'deleted' ),
22832284
'description' => __( 'What happened to the post: `trashed` when it was moved to the Trash, `deleted` when it was deleted permanently.' ),
22842285
),
22852286
),
@@ -2870,19 +2871,16 @@ private function not_found_error(): WP_Error {
28702871
/**
28712872
* Builds the error for a `data` attribute that cannot be applied.
28722873
*
2873-
* The error data maps the attribute's input path, such as `data[title]`, to the message
2874-
* under `params`, so callers can tell which attribute failed without parsing the
2875-
* translated message.
2874+
* The error data maps the attribute's input path to the message under `params`, so
2875+
* callers can tell which attribute failed without parsing the translated message.
28762876
*
28772877
* @since 7.3.0
28782878
*
2879-
* @param string $attribute The attribute name, such as `title`, or `data` for the whole object.
2880-
* @param string $message The error message.
2879+
* @param string $param The attribute's input path, such as `data[title]`.
2880+
* @param string $message The error message.
28812881
* @return WP_Error The invalid attribute error.
28822882
*/
2883-
private function invalid_attribute_error( string $attribute, string $message ): WP_Error {
2884-
$param = 'data' === $attribute ? 'data' : "data[{$attribute}]";
2885-
2883+
private function invalid_attribute_error( string $param, string $message ): WP_Error {
28862884
return new WP_Error(
28872885
'content_invalid_attribute',
28882886
$message,

‎tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ public function test_output_schema(): void {
263263
$this->assertSame( array( 'outcome' ), $schema['required'], 'The output should require the outcome.' );
264264
$this->assertSame( array( 'outcome' ), array_keys( $schema['properties'] ), 'The output should hold the outcome only.' );
265265
$this->assertSame( 'string', $schema['properties']['outcome']['type'], 'The outcome should be a string.' );
266+
$this->assertSame( array( 'trashed', 'deleted' ), $schema['properties']['outcome']['enum'], 'The outcome should be one of trashed and deleted.' );
266267
}
267268

268269
/**
@@ -879,7 +880,7 @@ public function test_post_without_trash_support_needs_permanent(): void {
879880
$result = $this->run_ability( $this->delete_input( $post_id ) );
880881

881882
$this->assert_error( $result, 'content_trash_not_supported', 501, 'Trashing a post without Trash support' );
882-
$this->assertSame( 'The post does not support trashing. Set "permanent" to delete it.', $result->get_error_message(), 'The message should ask for a permanent delete.' );
883+
$this->assertSame( 'The post does not support trashing. Set `permanent` to true to delete it.', $result->get_error_message(), 'The message should ask for a permanent delete.' );
883884
$this->assertSame( $before, $this->snapshot( $post_id ), 'A post without Trash support should be left as it was.' );
884885

885886
$this->assert_deleted( $this->run_ability( $this->delete_input( $post_id, true ) ), $post_id, 'Deleting a post without Trash support permanently' );

0 commit comments

Comments
 (0)