Skip to content

Commit e9ae497

Browse files
Abilities API: Address further review feedback on the content query
- Return null instead of an empty string for a date that cannot be resolved, and declare the `date-time` format on the date fields, so callers can tell a usable date from a missing one through the schema. A malformed stored date that the format rejects is reported as null too, instead of failing output validation for the whole call. - Map the filter to the message under `params` in the `content_invalid_filter` error data, as the REST API's `rest_invalid_param` errors do, so callers can tell which filter failed without parsing the translated message. - Mark `id`, which is always returned, as required in the post output schema.
1 parent 12c5097 commit e9ae497

2 files changed

Lines changed: 148 additions & 26 deletions

File tree

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

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -438,19 +438,20 @@ public function execute_content_query( $input = array() ) {
438438
$parent = null;
439439
if ( isset( $input['parent'] ) ) {
440440
if ( ! is_post_type_hierarchical( $post_type ) ) {
441-
return $this->invalid_filter_error( __( 'The parent filter is only supported for hierarchical post types.' ) );
441+
return $this->invalid_filter_error( 'parent', __( 'The parent filter is only supported for hierarchical post types.' ) );
442442
}
443443

444444
$parent = $this->parse_filter_int( $input['parent'], 0 );
445445
if ( null === $parent ) {
446-
return $this->invalid_filter_error( __( 'The parent filter must be a non-negative integer.' ) );
446+
return $this->invalid_filter_error( 'parent', __( 'The parent filter must be a non-negative integer.' ) );
447447
}
448448
}
449449

450450
$author = null;
451451
if ( isset( $input['author_slug'] ) ) {
452452
if ( ! post_type_supports( $post_type, 'author' ) ) {
453453
return $this->invalid_filter_error(
454+
'author_slug',
454455
/* translators: %s: Parameter. */
455456
sprintf( __( 'The %s filter is only supported for post types that support authors.' ), 'author_slug' )
456457
);
@@ -459,6 +460,7 @@ public function execute_content_query( $input = array() ) {
459460
$author = $this->get_author_by_slug( $input['author_slug'], $post_type_object );
460461
if ( ! $author ) {
461462
return $this->invalid_filter_error(
463+
'author_slug',
462464
/* translators: %s: Parameter. */
463465
sprintf( __( 'The %s filter must be the slug of an existing user.' ), 'author_slug' )
464466
);
@@ -473,7 +475,7 @@ public function execute_content_query( $input = array() ) {
473475
* would return every post of the type — the opposite of the caller's intent.
474476
*/
475477
if ( isset( $input['include'] ) && array() === $include ) {
476-
return $this->invalid_filter_error( __( 'The include filter must list one or more valid post IDs.' ) );
478+
return $this->invalid_filter_error( 'include', __( 'The include filter must list one or more valid post IDs.' ) );
477479
}
478480

479481
$per_page = $this->normalize_per_page( $input, $include );
@@ -890,20 +892,24 @@ private function get_post_properties(): array {
890892
'description' => __( 'The post status.' ),
891893
),
892894
'date' => array(
893-
'type' => 'string',
894-
'description' => __( "The publication date, in ISO 8601 format using the site's timezone. Empty string when the date cannot be resolved." ),
895+
'type' => array( 'string', 'null' ),
896+
'format' => 'date-time',
897+
'description' => __( "The publication date, in ISO 8601 format using the site's timezone. Null when the date cannot be resolved." ),
895898
),
896899
'date_gmt' => array(
897-
'type' => 'string',
898-
'description' => __( 'The publication date, in ISO 8601 format as GMT. Empty string when the date cannot be resolved.' ),
900+
'type' => array( 'string', 'null' ),
901+
'format' => 'date-time',
902+
'description' => __( 'The publication date, in ISO 8601 format as GMT. Null when the date cannot be resolved.' ),
899903
),
900904
'modified' => array(
901-
'type' => 'string',
902-
'description' => __( "The last modified date, in ISO 8601 format using the site's timezone. Empty string when the date cannot be resolved." ),
905+
'type' => array( 'string', 'null' ),
906+
'format' => 'date-time',
907+
'description' => __( "The last modified date, in ISO 8601 format using the site's timezone. Null when the date cannot be resolved." ),
903908
),
904909
'modified_gmt' => array(
905-
'type' => 'string',
906-
'description' => __( 'The last modified date, in ISO 8601 format as GMT. Empty string when the date cannot be resolved.' ),
910+
'type' => array( 'string', 'null' ),
911+
'format' => 'date-time',
912+
'description' => __( 'The last modified date, in ISO 8601 format as GMT. Null when the date cannot be resolved.' ),
907913
),
908914
'slug' => array(
909915
'type' => 'string',
@@ -1102,8 +1108,9 @@ private function get_content_query_input_schema( array $post_types, array $statu
11021108
/**
11031109
* Builds the output schema of a single post, shared by all content abilities.
11041110
*
1105-
* No field is marked required because the `fields` input lets the caller request any
1106-
* subset, and a field is only present when its post type supports it.
1111+
* Only `id` is required, because it is always returned. The other fields are optional
1112+
* because the `fields` input lets the caller request any subset, and a field is only
1113+
* present when its post type supports it.
11071114
*
11081115
* @since 7.2.0
11091116
*
@@ -1113,16 +1120,18 @@ private function get_content_output_schema(): array {
11131120
return array(
11141121
'type' => 'object',
11151122
'additionalProperties' => false,
1123+
'required' => array( 'id' ),
11161124
'properties' => $this->get_post_properties(),
11171125
);
11181126
}
11191127

11201128
/**
11211129
* Builds the output schema for the `core/content-query` ability.
11221130
*
1123-
* No field is marked required because the `fields` input lets the caller request any
1124-
* subset, and a field is only present when its post type supports it. Single-post
1125-
* mode returns the post object directly, while query mode returns a paginated wrapper.
1131+
* Only `id` is required in a post, because it is always returned. The other fields are
1132+
* optional because the `fields` input lets the caller request any subset, and a field
1133+
* is only present when its post type supports it. Single-post mode returns the post
1134+
* object directly, while query mode returns a paginated wrapper.
11261135
*
11271136
* @since 7.2.0
11281137
*
@@ -1490,19 +1499,26 @@ private function restore_post_context( array $previous_context ): void {
14901499
* @param WP_Post $post The post object.
14911500
* @param string $field Either 'date' or 'modified'.
14921501
* @param bool $gmt Whether to format the date in GMT instead of the site's timezone.
1493-
* @return string The ISO 8601 date, or an empty string if unavailable.
1502+
* @return string|null The ISO 8601 date, or null if unavailable.
14941503
*/
1495-
private function format_date( WP_Post $post, string $field, bool $gmt ): string {
1504+
private function format_date( WP_Post $post, string $field, bool $gmt ): ?string {
14961505
$datetime = $gmt ? get_post_datetime( $post, $field, 'gmt' ) : false;
14971506
if ( ! $datetime ) {
14981507
$datetime = get_post_datetime( $post, $field );
14991508
}
15001509

15011510
if ( ! $datetime ) {
1502-
return '';
1511+
return null;
15031512
}
15041513

1505-
return ( $gmt ? $datetime->setTimezone( new DateTimeZone( 'UTC' ) ) : $datetime )->format( 'c' );
1514+
/*
1515+
* A malformed stored date can still parse: a zero month formats with a negative
1516+
* year. The `date-time` format rejects it, which would fail output validation for
1517+
* the whole call, so it is reported as null too.
1518+
*/
1519+
$date = ( $gmt ? $datetime->setTimezone( new DateTimeZone( 'UTC' ) ) : $datetime )->format( 'c' );
1520+
1521+
return rest_parse_date( $date ) ? $date : null;
15061522
}
15071523

15081524
/**
@@ -1533,12 +1549,24 @@ private function not_found_error(): WP_Error {
15331549
/**
15341550
* Builds the error for a query filter that cannot be honored.
15351551
*
1552+
* As in the REST API's `rest_invalid_param` errors, the error data maps the filter to
1553+
* the message under `params`, so callers can tell which filter failed without parsing
1554+
* the translated message.
1555+
*
15361556
* @since 7.2.0
15371557
*
1558+
* @param string $filter The filter's input name.
15381559
* @param string $message The error message.
15391560
* @return WP_Error The invalid filter error.
15401561
*/
1541-
private function invalid_filter_error( string $message ): WP_Error {
1542-
return new WP_Error( 'content_invalid_filter', $message, array( 'status' => 400 ) );
1562+
private function invalid_filter_error( string $filter, string $message ): WP_Error {
1563+
return new WP_Error(
1564+
'content_invalid_filter',
1565+
$message,
1566+
array(
1567+
'status' => 400,
1568+
'params' => array( $filter => $message ),
1569+
)
1570+
);
15431571
}
15441572
}

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

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ public function test_output_schema_describes_single_post_and_query_responses():
387387
$this->assertSame( 'object', $schema['type'], 'The output schema should describe object responses.' );
388388
$this->assertCount( 2, $schema['oneOf'], 'The output schema should describe single-post and query responses.' );
389389
$this->assertSame( 'object', $post_schema['type'], 'The single-post response should be described as an object.' );
390-
$this->assertArrayNotHasKey( 'required', $post_schema, 'Individual post fields should remain optional.' );
390+
$this->assertSame( array( 'id' ), $post_schema['required'], 'Only the always-returned id should be required in a post.' );
391391
$this->assertFalse( $post_schema['additionalProperties'], 'Returned posts should not allow unknown properties.' );
392392
$this->assertArrayHasKey( 'type', $post_schema['properties'], 'The post schema should describe the post type as type.' );
393393
$this->assertArrayNotHasKey( 'post_type', $post_schema['properties'], 'The post schema should not expose the post type as post_type.' );
@@ -1865,6 +1865,11 @@ public function test_query_mode_rejects_parent_filter_for_non_hierarchical_post_
18651865

18661866
$this->assertWPError( $result, 'The parent filter should be rejected for non-hierarchical post types.' );
18671867
$this->assertSame( 'content_invalid_filter', $result->get_error_code(), 'Unsupported parent filters should return a filter error.' );
1868+
$this->assertSame(
1869+
array( 'parent' => $result->get_error_message() ),
1870+
$result->get_error_data()['params'],
1871+
'The error data should map the unsupported parent filter to the error message.'
1872+
);
18681873
}
18691874

18701875
/**
@@ -1930,6 +1935,11 @@ public function test_query_mode_rejects_author_filter_for_post_type_without_auth
19301935

19311936
$this->assertWPError( $result, 'The author_slug filter should be rejected for post types without author support.' );
19321937
$this->assertSame( 'content_invalid_filter', $result->get_error_code(), 'Unsupported author filters should return a filter error.' );
1938+
$this->assertSame(
1939+
array( 'author_slug' => $result->get_error_message() ),
1940+
$result->get_error_data()['params'],
1941+
'The error data should map the unsupported author_slug filter to the error message.'
1942+
);
19331943
}
19341944

19351945
/**
@@ -3601,7 +3611,7 @@ public function test_gmt_date_recovers_from_a_null_stored_gmt_date( string $fiel
36013611
}
36023612

36033613
/**
3604-
* A post with no usable date at all reports the documented empty-string sentinel.
3614+
* A post with no usable date at all reports null.
36053615
*
36063616
* @ticket 66268
36073617
* @since 7.2.0
@@ -3614,7 +3624,7 @@ public function test_gmt_date_recovers_from_a_null_stored_gmt_date( string $fiel
36143624
* @param string $local_date Unused. Present to match the shared data provider shape.
36153625
* @param string $expected Unused. Present to match the shared data provider shape.
36163626
*/
3617-
public function test_gmt_date_is_empty_when_no_usable_date_exists( string $field, string $gmt_column, string $local_column, string $local_date, string $expected ): void {
3627+
public function test_gmt_date_is_null_when_no_usable_date_exists( string $field, string $gmt_column, string $local_column, string $local_date, string $expected ): void {
36183628
$this->login_as( 'administrator' );
36193629
$this->register_ability();
36203630

@@ -3635,7 +3645,71 @@ public function test_gmt_date_is_empty_when_no_usable_date_exists( string $field
36353645
)
36363646
);
36373647

3638-
$this->assertSame( '', $result[ $field ], 'An unresolvable GMT date should be the empty-string sentinel.' );
3648+
$this->assertIsArray( $result, 'An unresolvable GMT date should pass output validation.' );
3649+
$this->assertArrayHasKey( $field, $result, 'An unresolvable GMT date should still be returned.' );
3650+
$this->assertNull( $result[ $field ], 'An unresolvable GMT date should be null.' );
3651+
}
3652+
3653+
/**
3654+
* Data provider for date fields.
3655+
*
3656+
* @since 7.2.0
3657+
*
3658+
* @return array<string, array{field: string, column: string}>
3659+
*/
3660+
public function data_date_fields(): array {
3661+
return array(
3662+
'date' => array(
3663+
'field' => 'date',
3664+
'column' => 'post_date',
3665+
),
3666+
'date_gmt' => array(
3667+
'field' => 'date_gmt',
3668+
'column' => 'post_date_gmt',
3669+
),
3670+
'modified' => array(
3671+
'field' => 'modified',
3672+
'column' => 'post_modified',
3673+
),
3674+
'modified_gmt' => array(
3675+
'field' => 'modified_gmt',
3676+
'column' => 'post_modified_gmt',
3677+
),
3678+
);
3679+
}
3680+
3681+
/**
3682+
* A malformed stored date reports null instead of failing output validation.
3683+
*
3684+
* A zero month still parses, but formats with a negative year, which the `date-time`
3685+
* format of the output schema rejects.
3686+
*
3687+
* @ticket 66268
3688+
* @since 7.2.0
3689+
*
3690+
* @dataProvider data_date_fields
3691+
*
3692+
* @param string $field The ability output field to request.
3693+
* @param string $column The cached post column to corrupt.
3694+
*/
3695+
public function test_malformed_date_is_null( string $field, string $column ): void {
3696+
$this->login_as( 'administrator' );
3697+
$this->register_ability();
3698+
3699+
$post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) );
3700+
3701+
$this->replace_cached_post_date_columns( $post_id, array( $column => '0000-00-01 00:00:00' ) );
3702+
3703+
$result = wp_get_ability( 'core/content-query' )->execute(
3704+
array(
3705+
'id' => $post_id,
3706+
'fields' => array( 'id', $field ),
3707+
)
3708+
);
3709+
3710+
$this->assertIsArray( $result, 'A malformed date should pass output validation.' );
3711+
$this->assertArrayHasKey( $field, $result, 'A malformed date should still be returned.' );
3712+
$this->assertNull( $result[ $field ], 'A malformed date should be null.' );
36393713
}
36403714

36413715
/**
@@ -3717,6 +3791,11 @@ public function test_execute_callback_rejects_an_author_slug_that_names_no_user(
37173791

37183792
$this->assertWPError( $result, 'An author_slug that names no user must not silently widen the query to all authors.' );
37193793
$this->assertSame( 'content_invalid_filter', $result->get_error_code(), 'An unhonorable author_slug filter should fail closed as an invalid filter.' );
3794+
$this->assertSame(
3795+
array( 'author_slug' => $result->get_error_message() ),
3796+
$result->get_error_data()['params'],
3797+
'The error data should map the unmatched author_slug filter to the error message.'
3798+
);
37203799
}
37213800

37223801
/**
@@ -3789,6 +3868,11 @@ public function test_execute_callback_rejects_non_integer_parent_filter(): void
37893868

37903869
$this->assertWPError( $result, 'A non-integer parent filter must not silently coerce to a top-level (0) query.' );
37913870
$this->assertSame( 'content_invalid_filter', $result->get_error_code(), 'An unhonorable parent filter should fail closed as an invalid filter.' );
3871+
$this->assertSame(
3872+
array( 'parent' => $result->get_error_message() ),
3873+
$result->get_error_data()['params'],
3874+
'The error data should map the non-integer parent filter to the error message.'
3875+
);
37923876
}
37933877

37943878
/**
@@ -3817,6 +3901,11 @@ public function test_execute_callback_rejects_include_with_no_valid_ids(): void
38173901

38183902
$this->assertWPError( $result, 'An include filter with no valid IDs must not fall through to an unrestricted query.' );
38193903
$this->assertSame( 'content_invalid_filter', $result->get_error_code(), 'An empty-after-parsing include should fail closed as an invalid filter.' );
3904+
$this->assertSame(
3905+
array( 'include' => $result->get_error_message() ),
3906+
$result->get_error_data()['params'],
3907+
'The error data should map the include filter to the error message.'
3908+
);
38203909
}
38213910

38223911
/**
@@ -3855,6 +3944,11 @@ public function test_author_slug_filter_only_names_users_the_current_user_may_se
38553944
$hidden = $query( array( 'publish' ), self::$user_ids['author_secondary'] );
38563945
$this->assertWPError( $hidden, 'A subscriber should not learn that an author without published posts exists.' );
38573946
$this->assertSame( 'content_invalid_filter', $hidden->get_error_code(), 'A hidden author should be reported like a missing one.' );
3947+
$this->assertSame(
3948+
array( 'author_slug' => $hidden->get_error_message() ),
3949+
$hidden->get_error_data()['params'],
3950+
'The error data should map the hidden author_slug filter to the error message.'
3951+
);
38583952

38593953
$this->login_as( 'editor' );
38603954
$drafts = $query( array( 'draft' ), self::$user_ids['author_secondary'] );

0 commit comments

Comments
 (0)