Skip to content

Commit

Permalink
REST API: Restore site logo and icon in index.
Browse files Browse the repository at this point in the history
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].

Reviewed by jorbin.
Merges [57147] to the 6.4 branch.

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

git-svn-id: https://develop.svn.wordpress.org/branches/6.4@57154 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
hellofromtonya committed Dec 4, 2023
1 parent 1029dde commit 7431090
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/wp-includes/rest-api/class-wp-rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,13 @@ public function get_index( $request ) {
$this->add_active_theme_link_to_index( $response );
$this->add_site_logo_to_index( $response );
$this->add_site_icon_to_index( $response );
} else {
if ( rest_is_field_included( 'site_logo', $fields ) ) {
$this->add_site_logo_to_index( $response );
}
if ( rest_is_field_included( 'site_icon', $fields ) || rest_is_field_included( 'site_icon_url', $fields ) ) {
$this->add_site_icon_to_index( $response );
}
}

/**
Expand Down
123 changes: 119 additions & 4 deletions tests/phpunit/tests/rest-api/rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1187,17 +1187,132 @@ public function test_index_includes_link_to_active_theme_if_authenticated() {

/**
* @ticket 52321
* @ticket 59935
*
* @covers WP_REST_Server::get_index
*/
public function test_index_includes_site_icon() {
$server = new WP_REST_Server();
public function test_get_index_should_include_site_icon() {
update_option( 'site_icon', self::$icon_id );

$server = new WP_REST_Server();
$request = new WP_REST_Request( 'GET', '/' );
$index = $server->dispatch( $request );
$data = $index->get_data();

$this->assertArrayHasKey( 'site_icon', $data );
$this->assertSame( self::$icon_id, $data['site_icon'] );
$this->assertArrayHasKey( 'site_logo', $data, 'The "site_logo" field is missing in the response.' );
$this->assertArrayHasKey( 'site_icon', $data, 'The "site_icon" field is missing in the response.' );
$this->assertArrayHasKey( 'site_icon_url', $data, 'The "site_icon_url" field is missing in the response.' );
$this->assertSame( self::$icon_id, $data['site_icon'], 'The response "site_icon" ID does not match.' );
$this->assertStringContainsString( 'test-image-large', $data['site_icon_url'], 'The "site_icon_url" should contain the expected image.' );
}
/**
* @ticket 52321
* @ticket 59935
*
* @covers WP_REST_Server::get_index
*/
public function test_get_index_should_not_include_site_icon() {
$server = new WP_REST_Server();
$request = new WP_REST_Request( 'GET', '/' );
$index = $server->dispatch( $request );
$data = $index->get_data();

$this->assertArrayHasKey( 'site_logo', $data, 'The "site_logo" field is missing in the response.' );
$this->assertArrayHasKey( 'site_icon', $data, 'The "site_icon" field is missing in the response.' );
$this->assertArrayHasKey( 'site_icon_url', $data, 'The "site_icon_url" field is missing in the response.' );
$this->assertSame( 0, $data['site_icon'], 'Response "site_icon" should be 0.' );
$this->assertSame( '', $data['site_icon_url'], 'Response "site_icon_url" should be an empty string.' );
}

/**
* Test that the "get_index" method returns the expected site_icon*
* and site_logo fields based on the specified request parameters.
*
* @ticket 59935
*
* @covers WP_REST_Server::get_index
*
* @dataProvider data_get_index_should_return_site_icon_and_site_logo_fields
*
* @param string $fields List of fields to use in the request.
* @param array $expected_fields Expected fields.
* @param array $unexpected_fields Optional. Fields that should not be in the results. Default array().
* @param bool $is_embed Optional. Whether to use the "_embed" request parameter. Default false.
*/
public function test_get_index_should_return_site_icon_and_site_logo_fields( $fields, $expected_fields, $unexpected_fields = array(), $is_embed = false ) {
$server = new WP_REST_Server();
$request = new WP_REST_Request( 'GET', '/', array() );
$request->set_param( '_fields', $fields );
if ( $is_embed ) {
$request->set_param( '_embed', true );
}

$response = $server->get_index( $request )->get_data();

foreach ( $expected_fields as $expected_field ) {
$this->assertArrayHasKey( $expected_field, $response, "Expected \"{$expected_field}\" field is missing in the response." );
}

foreach ( $unexpected_fields as $unexpected_field ) {
$this->assertArrayNotHasKey( $unexpected_field, $response, "Response must not contain the \"{$unexpected_field}\" field." );
}
}

/**
* Data provider.
*
* @return array
*/
public function data_get_index_should_return_site_icon_and_site_logo_fields() {
return array(
'no site_logo or site_icon fields' => array(
'fields' => 'name',
'expected_fields' => array(),
'unexpected_fields' => array( 'site_logo', 'site_icon', 'site_icon_url' ),
),
'_links request parameter' => array(
'fields' => '_links',
'expected_fields' => array( 'site_logo', 'site_icon', 'site_icon_url' ),
),
'_embed request parameter' => array(
'field' => '_embed',
'expected_fields' => array( 'site_logo', 'site_icon', 'site_icon_url' ),
'unexpected_fields' => array(),
'is_embed' => true,
),
'site_logo field' => array(
'fields' => 'site_logo',
'expected_fields' => array( 'site_logo' ),
'unexpected_fields' => array( 'site_icon', 'site_icon_url' ),
),
'site_icon field' => array(
'fields' => 'site_icon',
'expected_fields' => array( 'site_icon', 'site_icon_url' ),
'unexpected_fields' => array( 'site_logo' ),
),
'site_icon_url field' => array(
'fields' => 'site_icon_url',
'expected_fields' => array( 'site_icon', 'site_icon_url' ),
'unexpected_fields' => array( 'site_logo' ),
),
'site_icon and site_icon_url field' => array(
'fields' => 'site_icon_url',
'expected_fields' => array( 'site_icon', 'site_icon_url' ),
'unexpected_fields' => array( 'site_logo' ),
),
'site_logo and site_icon fields' => array(
'fields' => 'site_logo,site_icon',
'expected_fields' => array( 'site_logo', 'site_icon', 'site_icon_url' ),
),
'site_logo and site_icon_url fields' => array(
'fields' => 'site_logo,site_icon_url',
'expected_fields' => array( 'site_logo', 'site_icon', 'site_icon_url' ),
),
'site_logo, site_icon, and site_icon_url fields' => array(
'fields' => 'site_logo,site_icon,site_icon_url',
'expected_fields' => array( 'site_logo', 'site_icon', 'site_icon_url' ),
),
);
}

public function test_get_namespace_index() {
Expand Down

0 comments on commit 7431090

Please sign in to comment.