Make WordPress Core

Changeset 58227

Timestamp:
05/29/2024 07:19:56 AM (2 months ago)
Author:
youknowriad
Message:

REST API: Allow view access of template endpoint to anyone with the edit_post capability.

In order to render the block template in the locked template preview inside the post editor we need to be able to fetch the contents of any block templates/template parts for any user role that can edit a post.

Props fabiankaegy, youknowriad.
Fixes #61137.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

    r58079 r58227  
    237237     *
    238238     * @since 5.8.0
     239
    239240     *
    240241     * @param WP_REST_Request $request Full details about the request.
     
    242243     */
    243244    public function get_items_permissions_check( $request ) {
    244         return $this->permissions_check( $request );
     245        if ( current_user_can( 'edit_posts' ) ) {
     246            return true;
     247        }
     248        foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
     249            if ( current_user_can( $post_type->cap->edit_posts ) ) {
     250                return true;
     251            }
     252        }
     253
     254        return new WP_Error(
     255            'rest_cannot_manage_templates',
     256            __( 'Sorry, you are not allowed to access the templates on this site.', 'default' ),
     257            array(
     258                'status' => rest_authorization_required_code(),
     259            )
     260        );
    245261    }
    246262
     
    278294     *
    279295     * @since 5.8.0
     296
    280297     *
    281298     * @param WP_REST_Request $request Full details about the request.
     
    283300     */
    284301    public function get_item_permissions_check( $request ) {
    285         return $this->permissions_check( $request );
     302        if ( current_user_can( 'edit_posts' ) ) {
     303            return true;
     304        }
     305        foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
     306            if ( current_user_can( $post_type->cap->edit_posts ) ) {
     307                return true;
     308            }
     309        }
     310
     311        return new WP_Error(
     312            'rest_cannot_manage_templates',
     313            __( 'Sorry, you are not allowed to access the templates on this site.', 'default' ),
     314            array(
     315                'status' => rest_authorization_required_code(),
     316            )
     317        );
    286318    }
    287319
  • trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php

    r58079 r58227  
    1515     */
    1616    protected static $admin_id;
     17
     18
    1719    private static $template_post;
    1820    private static $template_part_post;
     
    2426     */
    2527    public static function wpSetupBeforeClass( $factory ) {
    26         self::$admin_id = $factory->user->create(
     28        self::$admin_id = $factory->user->create(
    2729            array(
    2830                'role' => 'administrator',
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
    2941            )
    3042        );
     
    167179     * @covers WP_REST_Templates_Controller::get_items
    168180     */
    169     public function test_get_items_no_permission() {
    170         wp_set_current_user( 0 );
     181    public function test_get_items_() {
     182        wp_set_current_user( );
    171183        $request  = new WP_REST_Request( 'GET', '/wp/v2/templates' );
    172184        $response = rest_get_server()->dispatch( $request );
    173         $this->assertErrorResponse( 'rest_cannot_manage_templates', $response, 401 );
    174     }
    175 
    176     /**
    177      * @covers WP_REST_Templates_Controller::get_item
    178      */
    179     public function test_get_item() {
    180         wp_set_current_user( self::$admin_id );
    181         $request  = new WP_REST_Request( 'GET', '/wp/v2/templates/default//my_template' );
    182         $response = rest_get_server()->dispatch( $request );
    183         $data     = $response->get_data();
    184         unset( $data['content'] );
    185         unset( $data['_links'] );
     185        $data     = $response->get_data();
    186186
    187187        $this->assertSame(
     
    207207                'original_source' => 'site',
    208208            ),
     209
     210
     211
     212
     213
     214
     215
     216
     217
     218
     219
     220
     221
     222
     223
     224
     225
     226
     227
     228
     229
     230
     231
     232
     233
     234
     235
     236
     237
     238
     239
     240
     241
     242
     243
     244
     245
     246
     247
     248
     249
     250
     251
     252
     253
     254
     255
     256
     257
     258
     259
     260
     261
     262
     263
     264
     265
    209266            $data
    210267        );
     268
     269
     270
     271
     272
     273
     274
     275
     276
     277
     278
     279
     280
     281
     282
     283
     284
     285
     286
     287
     288
     289
     290
     291
     292
     293
     294
     295
     296
     297
     298
     299
     300
     301
     302
     303
     304
     305
     306
     307
     308
     309
     310
     311
     312
     313
     314
     315
    211316    }
    212317
Note: See TracChangeset for help on using the changeset viewer.