Skip to content

Commit 85f803c

Browse files
Abilities API: Keep the core/content-query callbacks private.
Only `register()` stays public. The permission and execute callbacks, and the `post_password_required` filter that `format_post()` adds, are now private methods, reached through closures defined in the class, so callers run the ability through the Abilities API, such as `wp_get_ability( 'core/content-query' )->execute()`, which validates the input and checks permissions first. This replaces the class docblock's note that the class is not part of the public API. Public methods are covered by Core's backward compatibility commitment whatever the docblock says. Tests that call the callbacks directly now capture them from the registration arguments, through the `wp_register_ability_args` filter. The password filter test now reads `post_password_required()` while an editable protected post renders, instead of calling the filter callback.
1 parent e9ae497 commit 85f803c

2 files changed

Lines changed: 100 additions & 41 deletions

File tree

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
* and field projection are built from the same field definitions. Future write-oriented
2626
* content abilities can reuse them as well.
2727
*
28-
* This class is part of WordPress' internal implementation of the core abilities and is
29-
* not part of the public API. It may be changed or removed at any time without notice.
30-
* Do not use it directly or rely on its existence.
28+
* Only register() is public. The ability callbacks are closures that call private
29+
* methods, so callers go through the Abilities API, such as
30+
* `wp_get_ability( 'core/content-query' )->execute()`, which validates the input and
31+
* checks permissions before running them.
3132
*
3233
* @since 7.2.0
3334
*
@@ -145,8 +146,12 @@ private function register_content_query(): void {
145146
'category' => self::CATEGORY,
146147
'input_schema' => $this->get_content_query_input_schema( $post_types, $statuses ),
147148
'output_schema' => $this->get_content_query_output_schema(),
148-
'execute_callback' => array( $this, 'execute_content_query' ),
149-
'permission_callback' => array( $this, 'check_permission' ),
149+
'execute_callback' => function ( $input = array() ) {
150+
return $this->execute_content_query( $input );
151+
},
152+
'permission_callback' => function ( $input = array() ): bool {
153+
return $this->check_permission( $input );
154+
},
150155
'meta' => array(
151156
'annotations' => array(
152157
'readonly' => true,
@@ -174,7 +179,7 @@ private function register_content_query(): void {
174179
* @param mixed $input Optional. The ability input. Default empty array.
175180
* @return bool True if the request may proceed, false otherwise.
176181
*/
177-
public function check_permission( $input = array() ): bool {
182+
private function check_permission( $input = array() ): bool {
178183
$input = rest_sanitize_object( $input );
179184

180185
if ( ! is_user_logged_in() ) {
@@ -407,7 +412,7 @@ private function check_read_permission( WP_Post $post, array $checked_post_ids =
407412
* @param mixed $input Optional. The ability input. Default empty array.
408413
* @return array<string, mixed>|WP_Error A single post, a `posts` list with totals in query mode, or a WP_Error.
409414
*/
410-
public function execute_content_query( $input = array() ) {
415+
private function execute_content_query( $input = array() ) {
411416
$input = rest_sanitize_object( $input );
412417
$fields = $this->normalize_fields( $input );
413418
$requires_edit = $this->has_explicit_edit_fields( $input );
@@ -1196,10 +1201,14 @@ private function format_post( WP_Post $post, array $fields ): array {
11961201
* The filter unlocks only posts the current user can edit, mirroring the REST posts
11971202
* controller's check_password_required(): an unconditional bypass (e.g. __return_false)
11981203
* would also expose other protected posts that the content filter may render, such as
1199-
* posts pulled in by a Query Loop block.
1204+
* posts pulled in by a Query Loop block. The closure is kept in a variable, so the same
1205+
* instance can be removed again.
12001206
*/
1207+
$allow_password_content = function ( $required, $checked_post ): bool {
1208+
return $this->allow_password_content( $required, $checked_post );
1209+
};
12011210
if ( $unlock_password ) {
1202-
add_filter( 'post_password_required', array( $this, 'allow_password_content' ), 10, 2 );
1211+
add_filter( 'post_password_required', $allow_password_content, 10, 2 );
12031212
}
12041213

12051214
/*
@@ -1210,7 +1219,7 @@ private function format_post( WP_Post $post, array $fields ): array {
12101219
return $this->build_post_fields( $post, $fields, $can_edit, $password_required && ! $can_edit );
12111220
} finally {
12121221
if ( $unlock_password ) {
1213-
remove_filter( 'post_password_required', array( $this, 'allow_password_content' ), 10 );
1222+
remove_filter( 'post_password_required', $allow_password_content, 10 );
12141223
}
12151224

12161225
$this->restore_post_context( $previous_context );
@@ -1330,7 +1339,7 @@ private function build_post_fields( WP_Post $post, array $fields, bool $can_edit
13301339
* @param mixed $post The post being checked; a WP_Post when invoked by the core filter.
13311340
* @return bool Whether the post still requires a password.
13321341
*/
1333-
public function allow_password_content( $required, $post ): bool {
1342+
private function allow_password_content( $required, $post ): bool {
13341343
if ( ! $required || ! $post instanceof WP_Post ) {
13351344
return (bool) $required;
13361345
}

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

Lines changed: 80 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,40 @@ private function register_ability(): void {
181181
( new WP_Abilities_Content() )->register();
182182
}
183183

184+
/**
185+
* Registers the core/content-query ability and returns the callbacks it was registered with.
186+
*
187+
* The callbacks call private methods, so tests that skip input validation or the
188+
* permission check capture them from the registration arguments.
189+
*
190+
* @since 7.2.0
191+
*
192+
* @return array<string, callable> The permission and execute callbacks, keyed by argument name.
193+
*/
194+
private function get_ability_callbacks(): array {
195+
$callbacks = array();
196+
197+
add_filter(
198+
'wp_register_ability_args',
199+
static function ( array $args, string $name ) use ( &$callbacks ): array {
200+
if ( 'core/content-query' === $name ) {
201+
$callbacks = array(
202+
'permission_callback' => $args['permission_callback'],
203+
'execute_callback' => $args['execute_callback'],
204+
);
205+
}
206+
207+
return $args;
208+
},
209+
10,
210+
2
211+
);
212+
213+
$this->register_ability();
214+
215+
return $callbacks;
216+
}
217+
184218
/**
185219
* Logs in as a user with the given role and returns the user ID.
186220
*
@@ -2538,24 +2572,37 @@ public function test_allow_password_content_only_unlocks_editable_posts(): void
25382572
'post_password' => 'secret',
25392573
)
25402574
);
2541-
$other_id = self::$post_ids['password_protected'];
25422575

25432576
$this->login_as( 'author' );
25442577

2545-
$ability = new WP_Abilities_Content();
2578+
// Read the password gate of each post while the editable protected post renders.
2579+
$gates = array();
2580+
add_filter(
2581+
'the_content',
2582+
static function ( $content ) use ( &$gates, $owned_id ) {
2583+
$gates = array(
2584+
'owned' => post_password_required( $owned_id ),
2585+
'other' => post_password_required( self::$post_ids['password_protected'] ),
2586+
'public' => post_password_required( self::$post_ids['published'] ),
2587+
);
25462588

2547-
$this->assertFalse(
2548-
$ability->allow_password_content( true, get_post( $owned_id ) ),
2549-
'The filter should unlock a protected post the current user can edit.'
2550-
);
2551-
$this->assertTrue(
2552-
$ability->allow_password_content( true, get_post( $other_id ) ),
2553-
'The filter should keep the gate on a protected post the current user cannot edit.'
2589+
return $content;
2590+
}
25542591
);
2555-
$this->assertFalse(
2556-
$ability->allow_password_content( false, get_post( $other_id ) ),
2557-
'The filter should leave posts that do not require a password ungated.'
2592+
2593+
$this->register_ability();
2594+
2595+
$result = wp_get_ability( 'core/content-query' )->execute(
2596+
array(
2597+
'id' => $owned_id,
2598+
'fields' => array( 'id', 'content_rendered' ),
2599+
)
25582600
);
2601+
2602+
$this->assertIsArray( $result, 'Precondition: the author should read their own protected post.' );
2603+
$this->assertFalse( $gates['owned'], 'The filter should unlock a protected post the current user can edit.' );
2604+
$this->assertTrue( $gates['other'], 'The filter should keep the gate on a protected post the current user cannot edit.' );
2605+
$this->assertFalse( $gates['public'], 'The filter should leave posts that do not require a password ungated.' );
25592606
}
25602607

25612608
/**
@@ -2898,11 +2945,11 @@ public function test_callbacks_reject_a_fractional_id( $id ): void {
28982945
$this->assertSame( self::IMPORTED_POST_ID, $post_id, 'Precondition: the post should have the ID the fraction truncates to.' );
28992946

29002947
$this->login_as( 'administrator' );
2901-
$content = new WP_Abilities_Content();
2948+
$callbacks = $this->get_ability_callbacks();
29022949

2903-
$this->assertFalse( $content->check_permission( array( 'id' => $id ) ), 'The permission callback should deny a fractional ID.' );
2950+
$this->assertFalse( $callbacks['permission_callback']( array( 'id' => $id ) ), 'The permission callback should deny a fractional ID.' );
29042951

2905-
$result = $content->execute_content_query( array( 'id' => $id ) );
2952+
$result = $callbacks['execute_callback']( array( 'id' => $id ) );
29062953

29072954
$this->assertWPError( $result, 'The execute callback should not resolve a fractional ID.' );
29082955
$this->assertSame( 'content_not_found', $result->get_error_code(), 'A fractional ID should fail the lookup.' );
@@ -3725,13 +3772,13 @@ public function test_malformed_date_is_null( string $field, string $column ): vo
37253772
public function test_execute_callback_returns_not_found_for_structural_lookup_failures(): void {
37263773
$this->login_as( 'administrator' );
37273774

3728-
$content = new WP_Abilities_Content();
3775+
$execute = $this->get_ability_callbacks()['execute_callback'];
37293776

3730-
$missing = $content->execute_content_query( array( 'id' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
3777+
$missing = $execute( array( 'id' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
37313778
$this->assertWPError( $missing, 'A nonexistent post ID should fail the lookup.' );
37323779
$this->assertSame( 'content_not_found', $missing->get_error_code(), 'Missing posts should map to the uniform not-found error.' );
37333780

3734-
$mismatched = $content->execute_content_query(
3781+
$mismatched = $execute(
37353782
array(
37363783
'id' => self::$post_ids['published'],
37373784
'type' => 'page',
@@ -3740,7 +3787,7 @@ public function test_execute_callback_returns_not_found_for_structural_lookup_fa
37403787
$this->assertWPError( $mismatched, 'A post type mismatch should fail the lookup.' );
37413788
$this->assertSame( 'content_not_found', $mismatched->get_error_code(), 'Mismatched post types should map to the uniform not-found error.' );
37423789

3743-
$missing_slug = $content->execute_content_query(
3790+
$missing_slug = $execute(
37443791
array(
37453792
'type' => 'post',
37463793
'slug' => 'no-such-slug',
@@ -3780,9 +3827,9 @@ public function test_execute_callback_rejects_an_author_slug_that_names_no_user(
37803827
);
37813828

37823829
$this->login_as( 'administrator' );
3783-
$content = new WP_Abilities_Content();
3830+
$execute = $this->get_ability_callbacks()['execute_callback'];
37843831

3785-
$result = $content->execute_content_query(
3832+
$result = $execute(
37863833
array(
37873834
'type' => 'post',
37883835
'author_slug' => $author_slug,
@@ -3857,9 +3904,9 @@ public function test_author_slug_filter_matches_a_slug_in_capitals(): void {
38573904
*/
38583905
public function test_execute_callback_rejects_non_integer_parent_filter(): void {
38593906
$this->login_as( 'administrator' );
3860-
$content = new WP_Abilities_Content();
3907+
$execute = $this->get_ability_callbacks()['execute_callback'];
38613908

3862-
$result = $content->execute_content_query(
3909+
$result = $execute(
38633910
array(
38643911
'type' => 'page',
38653912
'parent' => 'not-a-number',
@@ -3890,9 +3937,9 @@ public function test_execute_callback_rejects_include_with_no_valid_ids(): void
38903937
$this->login_as( 'administrator' );
38913938

38923939
self::factory()->post->create( array( 'post_status' => 'publish' ) );
3893-
$content = new WP_Abilities_Content();
3940+
$execute = $this->get_ability_callbacks()['execute_callback'];
38943941

3895-
$result = $content->execute_content_query(
3942+
$result = $execute(
38963943
array(
38973944
'type' => 'post',
38983945
'include' => array( 0 ),
@@ -3925,8 +3972,9 @@ public function test_author_slug_filter_only_names_users_the_current_user_may_se
39253972
)
39263973
);
39273974

3928-
$query = static function ( array $statuses, int $user_id ) {
3929-
return ( new WP_Abilities_Content() )->execute_content_query(
3975+
$execute = $this->get_ability_callbacks()['execute_callback'];
3976+
$query = static function ( array $statuses, int $user_id ) use ( $execute ) {
3977+
return $execute(
39303978
array(
39313979
'type' => 'post',
39323980
'status' => $statuses,
@@ -3971,8 +4019,9 @@ public function test_author_slug_filter_hides_users_of_other_sites(): void {
39714019
switch_to_blog( $site_id );
39724020
wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
39734021

3974-
$query = static function ( string $author_slug ) {
3975-
return ( new WP_Abilities_Content() )->execute_content_query(
4022+
$execute = $this->get_ability_callbacks()['execute_callback'];
4023+
$query = static function ( string $author_slug ) use ( $execute ) {
4024+
return $execute(
39764025
array(
39774026
'type' => 'post',
39784027
'author_slug' => $author_slug,
@@ -4004,13 +4053,14 @@ public function test_author_slug_filter_names_authors_who_are_not_site_members()
40044053
$super_admin_id = self::factory()->user->create( array( 'user_nicename' => 'network-author' ) );
40054054
grant_super_admin( $super_admin_id );
40064055
$site_id = self::factory()->blog->create();
4056+
$execute = $this->get_ability_callbacks()['execute_callback'];
40074057

40084058
switch_to_blog( $site_id );
40094059
$post_id = self::factory()->post->create( array( 'post_author' => $super_admin_id ) );
40104060
wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
40114061

40124062
$is_member = is_user_member_of_blog( $super_admin_id, $site_id );
4013-
$result = ( new WP_Abilities_Content() )->execute_content_query(
4063+
$result = $execute(
40144064
array(
40154065
'type' => 'post',
40164066
'author_slug' => 'network-author',

0 commit comments

Comments
 (0)