Make WordPress Core

Changeset 57147


Ignore:
Timestamp:
11/30/2023 02:52:52 PM (5 months ago)
Author:
hellofromTonya
Message:

REST API: Restore site logo and icon in index.

Restores setting the site's logo, icon, and wp-admin's back button image (which defaults to W).

Prior to [56566], the site logo and icon were unconditionally added to the index. [56566] changed this by conditionally adding them if either the _links or _embedded fields were included. However, these fields are not included when using the Site Logo block, as it uses the site_logo, site_icon, and site_icon_url fields instead.

This changeset restores the functionality by checking specifically for the site_* fields when neither of the _links or _embedded fields are present.

Follow up to [56566].

Props antonvlasenko, hellofromTonya, ironprogrammer, priethor, wildworks.
Fixes #59935.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/class-wp-rest-server.php

    r57126 r57147  
    12971297            $this->add_site_logo_to_index( $response );
    12981298            $this->add_site_icon_to_index( $response );
     1299        } else {
     1300            if ( rest_is_field_included( 'site_logo', $fields ) ) {
     1301                $this->add_site_logo_to_index( $response );
     1302            }
     1303            if ( rest_is_field_included( 'site_icon', $fields ) || rest_is_field_included( 'site_icon_url', $fields ) ) {
     1304                $this->add_site_icon_to_index( $response );
     1305            }
    12991306        }
    13001307
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r56566 r57147  
    11881188    /**
    11891189     * @ticket 52321
    1190      */
    1191     public function test_index_includes_site_icon() {
    1192         $server = new WP_REST_Server();
     1190     * @ticket 59935
     1191     *
     1192     * @covers WP_REST_Server::get_index
     1193     */
     1194    public function test_get_index_should_include_site_icon() {
    11931195        update_option( 'site_icon', self::$icon_id );
    11941196
     1197        $server  = new WP_REST_Server();
    11951198        $request = new WP_REST_Request( 'GET', '/' );
    11961199        $index   = $server->dispatch( $request );
    11971200        $data    = $index->get_data();
    11981201
    1199         $this->assertArrayHasKey( 'site_icon', $data );
    1200         $this->assertSame( self::$icon_id, $data['site_icon'] );
     1202        $this->assertArrayHasKey( 'site_logo', $data, 'The "site_logo" field is missing in the response.' );
     1203        $this->assertArrayHasKey( 'site_icon', $data, 'The "site_icon" field is missing in the response.' );
     1204        $this->assertArrayHasKey( 'site_icon_url', $data, 'The "site_icon_url" field is missing in the response.' );
     1205        $this->assertSame( self::$icon_id, $data['site_icon'], 'The response "site_icon" ID does not match.' );
     1206        $this->assertStringContainsString( 'test-image-large', $data['site_icon_url'], 'The "site_icon_url" should contain the expected image.' );
     1207    }
     1208    /**
     1209     * @ticket 52321
     1210     * @ticket 59935
     1211     *
     1212     * @covers WP_REST_Server::get_index
     1213     */
     1214    public function test_get_index_should_not_include_site_icon() {
     1215        $server  = new WP_REST_Server();
     1216        $request = new WP_REST_Request( 'GET', '/' );
     1217        $index   = $server->dispatch( $request );
     1218        $data    = $index->get_data();
     1219
     1220        $this->assertArrayHasKey( 'site_logo', $data, 'The "site_logo" field is missing in the response.' );
     1221        $this->assertArrayHasKey( 'site_icon', $data, 'The "site_icon" field is missing in the response.' );
     1222        $this->assertArrayHasKey( 'site_icon_url', $data, 'The "site_icon_url" field is missing in the response.' );
     1223        $this->assertSame( 0, $data['site_icon'], 'Response "site_icon" should be 0.' );
     1224        $this->assertSame( '', $data['site_icon_url'], 'Response "site_icon_url" should be an empty string.' );
     1225    }
     1226
     1227    /**
     1228     * Test that the "get_index" method returns the expected site_icon*
     1229     * and site_logo fields based on the specified request parameters.
     1230     *
     1231     * @ticket 59935
     1232     *
     1233     * @covers WP_REST_Server::get_index
     1234     *
     1235     * @dataProvider data_get_index_should_return_site_icon_and_site_logo_fields
     1236     *
     1237     * @param string $fields            List of fields to use in the request.
     1238     * @param array  $expected_fields   Expected fields.
     1239     * @param array  $unexpected_fields Optional. Fields that should not be in the results. Default array().
     1240     * @param bool   $is_embed          Optional. Whether to use the "_embed" request parameter. Default false.
     1241     */
     1242    public function test_get_index_should_return_site_icon_and_site_logo_fields( $fields, $expected_fields, $unexpected_fields = array(), $is_embed = false ) {
     1243        $server  = new WP_REST_Server();
     1244        $request = new WP_REST_Request( 'GET', '/', array() );
     1245        $request->set_param( '_fields', $fields );
     1246        if ( $is_embed ) {
     1247            $request->set_param( '_embed', true );
     1248        }
     1249
     1250        $response = $server->get_index( $request )->get_data();
     1251
     1252        foreach ( $expected_fields as $expected_field ) {
     1253            $this->assertArrayHasKey( $expected_field, $response, "Expected \"{$expected_field}\" field is missing in the response." );
     1254        }
     1255
     1256        foreach ( $unexpected_fields as $unexpected_field ) {
     1257            $this->assertArrayNotHasKey( $unexpected_field, $response, "Response must not contain the \"{$unexpected_field}\" field." );
     1258        }
     1259    }
     1260
     1261    /**
     1262     * Data provider.
     1263     *
     1264     * @return array
     1265     */
     1266    public function data_get_index_should_return_site_icon_and_site_logo_fields() {
     1267        return array(
     1268            'no site_logo or site_icon fields'   => array(
     1269                'fields'            => 'name',
     1270                'expected_fields'   => array(),
     1271                'unexpected_fields' => array( 'site_logo', 'site_icon', 'site_icon_url' ),
     1272            ),
     1273            '_links request parameter'           => array(
     1274                'fields'          => '_links',
     1275                'expected_fields' => array( 'site_logo', 'site_icon', 'site_icon_url' ),
     1276            ),
     1277            '_embed request parameter'           => array(
     1278                'field'             => '_embed',
     1279                'expected_fields'   => array( 'site_logo', 'site_icon', 'site_icon_url' ),
     1280                'unexpected_fields' => array(),
     1281                'is_embed'          => true,
     1282            ),
     1283            'site_logo field'                    => array(
     1284                'fields'            => 'site_logo',
     1285                'expected_fields'   => array( 'site_logo' ),
     1286                'unexpected_fields' => array( 'site_icon', 'site_icon_url' ),
     1287            ),
     1288            'site_icon field'                    => array(
     1289                'fields'            => 'site_icon',
     1290                'expected_fields'   => array( 'site_icon', 'site_icon_url' ),
     1291                'unexpected_fields' => array( 'site_logo' ),
     1292            ),
     1293            'site_icon_url field'                => array(
     1294                'fields'            => 'site_icon_url',
     1295                'expected_fields'   => array( 'site_icon', 'site_icon_url' ),
     1296                'unexpected_fields' => array( 'site_logo' ),
     1297            ),
     1298            'site_icon and site_icon_url field'  => array(
     1299                'fields'            => 'site_icon_url',
     1300                'expected_fields'   => array( 'site_icon', 'site_icon_url' ),
     1301                'unexpected_fields' => array( 'site_logo' ),
     1302            ),
     1303            'site_logo and site_icon fields'     => array(
     1304                'fields'          => 'site_logo,site_icon',
     1305                'expected_fields' => array( 'site_logo', 'site_icon', 'site_icon_url' ),
     1306            ),
     1307            'site_logo and site_icon_url fields' => array(
     1308                'fields'          => 'site_logo,site_icon_url',
     1309                'expected_fields' => array( 'site_logo', 'site_icon', 'site_icon_url' ),
     1310            ),
     1311            'site_logo, site_icon, and site_icon_url fields' => array(
     1312                'fields'          => 'site_logo,site_icon,site_icon_url',
     1313                'expected_fields' => array( 'site_logo', 'site_icon', 'site_icon_url' ),
     1314            ),
     1315        );
    12011316    }
    12021317
Note: See TracChangeset for help on using the changeset viewer.