WordPress.org

Make WordPress Core

Changeset 52374


Ignore:
Timestamp:
12/14/2021 04:29:03 PM (5 weeks ago)
Author:
spacedmonkey
Message:

REST API: Ensure that the get_theme_item method should respect fields param.

Fix the get_theme_item method in the WP_REST_Global_Styles_Controller class to respect the fields param context filtering and to return links.

Props spacedmonkey, hellofromtonya, peterwilsoncc, costdev.
Fixes #54595.

Location:
trunk
Files:
2 edited

Legend:

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

    r52342 r52374  
    378378
    379379        $links = array(
    380             'self'       => array(
     380            'self' => array(
    381381                'href' => rest_url( trailingslashit( $base ) . $id ),
    382             ),
    383             'collection' => array(
    384                 'href' => rest_url( $base ),
    385382            ),
    386383        );
     
    536533        }
    537534
    538         $theme    = WP_Theme_JSON_Resolver::get_merged_data( 'theme' );
    539         $styles   = $theme->get_raw_data()['styles'];
    540         $settings = $theme->get_settings();
    541         $result   = array(
    542             'settings' => $settings,
    543             'styles'   => $styles,
     535        $theme  = WP_Theme_JSON_Resolver::get_merged_data( 'theme' );
     536        $data   = array();
     537        $fields = $this->get_fields_for_response( $request );
     538
     539        if ( rest_is_field_included( 'settings', $fields ) ) {
     540            $data['settings'] = $theme->get_settings();
     541        }
     542
     543        if ( rest_is_field_included( 'styles', $fields ) ) {
     544            $data['styles'] = $theme->get_raw_data()['styles'];
     545        }
     546
     547        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
     548        $data    = $this->add_additional_fields_to_object( $data, $request );
     549        $data    = $this->filter_response_by_context( $data, $context );
     550
     551        $response = rest_ensure_response( $data );
     552
     553        $links = array(
     554            'self' => array(
     555                'href' => rest_url( sprintf( '%s/%s/themes/%s', $this->namespace, $this->rest_base, $request['stylesheet'] ) ),
     556            ),
    544557        );
    545         $response = rest_ensure_response( $result );
     558
     559        $response->add_links( $links );
    546560
    547561        return $response;
  • trunk/tests/phpunit/tests/rest-api/rest-global-styles-controller.php

    r52342 r52374  
    153153        $response = rest_get_server()->dispatch( $request );
    154154        $data     = $response->get_data();
    155         unset( $data['_links'] );
    156 
     155        $links    = $response->get_links();
    157156        $this->assertArrayHasKey( 'settings', $data );
    158157        $this->assertArrayHasKey( 'styles', $data );
     158        $this->assertArrayHasKey( 'self', $links );
     159        $this->assertStringContainsString( '/wp/v2/global-styles/themes/tt1-blocks', $links['self'][0]['href'] );
     160    }
     161
     162    /**
     163     * @covers WP_REST_Global_Styles_Controller::get_theme_item
     164     * @ticket 54595
     165     */
     166    public function test_get_theme_item_fields() {
     167        wp_set_current_user( self::$admin_id );
     168        $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/tt1-blocks' );
     169        $request->set_param( '_fields', 'settings' );
     170        $response = rest_get_server()->dispatch( $request );
     171        $data     = $response->get_data();
     172        $this->assertArrayHasKey( 'settings', $data );
     173        $this->assertArrayNotHasKey( 'styles', $data );
    159174    }
    160175
     
    224239        $response = rest_get_server()->dispatch( $request );
    225240        $data     = $response->get_data();
    226         unset( $data['_links'] );
     241        $links    = $response->get_links();
    227242
    228243        $this->assertEquals(
     
    238253            $data
    239254        );
     255
     256        $this->assertArrayHasKey( 'self', $links );
     257        $this->assertStringContainsString( '/wp/v2/global-styles/' . self::$global_styles_id, $links['self'][0]['href'] );
    240258    }
    241259
Note: See TracChangeset for help on using the changeset viewer.