@@ -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