diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php index b7451ffc18..1a94dd3fcf 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -1229,6 +1229,7 @@ class WP_REST_Server { $response->add_link( 'help', 'https://developer.wordpress.org/rest-api/' ); $this->add_active_theme_link_to_index( $response ); $this->add_site_logo_to_index( $response ); + $this->add_site_icon_to_index( $response ); /** * Filters the REST API root index data. @@ -1275,6 +1276,7 @@ class WP_REST_Server { /** * Exposes the site logo through the WordPress REST API. + * * This is used for fetching this information when user has no rights * to update settings. * @@ -1283,14 +1285,47 @@ class WP_REST_Server { * @param WP_REST_Response $response REST API response. */ protected function add_site_logo_to_index( WP_REST_Response $response ) { - $site_logo_id = get_theme_mod( 'custom_logo' ); - $response->data['site_logo'] = $site_logo_id; - if ( $site_logo_id ) { + $site_logo_id = get_theme_mod( 'custom_logo', 0 ); + + $this->add_image_to_index( $response, $site_logo_id, 'site_logo' ); + } + + /** + * Exposes the site icon through the WordPress REST API. + * + * This is used for fetching this information when user has no rights + * to update settings. + * + * @since 5.9.0 + * + * @param WP_REST_Response $response REST API response. + */ + protected function add_site_icon_to_index( WP_REST_Response $response ) { + $site_icon_id = get_option( 'site_icon', 0 ); + + $this->add_image_to_index( $response, $site_icon_id, 'site_icon' ); + } + + /** + * Exposes an image through the WordPress REST API. + * This is used for fetching this information when user has no rights + * to update settings. + * + * @since 5.9.0 + * + * @param WP_REST_Response $response REST API response. + * @param int $image_id Image attachment ID. + * @param string $type Type of Image. + */ + protected function add_image_to_index( WP_REST_Response $response, $image_id, $type ) { + $response->data[ $type ] = (int) $image_id; + if ( $image_id ) { $response->add_link( 'https://api.w.org/featuredmedia', - rest_url( rest_get_route_for_post( $site_logo_id ) ), + rest_url( rest_get_route_for_post( $image_id ) ), array( 'embeddable' => true, + 'type' => $type, ) ); } diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php index 57951463ae..248886a76e 100644 --- a/tests/phpunit/tests/rest-api/rest-server.php +++ b/tests/phpunit/tests/rest-api/rest-server.php @@ -10,6 +10,13 @@ * @group restapi */ class Tests_REST_Server extends WP_Test_REST_TestCase { + protected static $icon_id; + + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { + $filename = DIR_TESTDATA . '/images/test-image-large.jpg'; + self::$icon_id = $factory->attachment->create_upload_object( $filename ); + } + public function set_up() { parent::set_up(); @@ -1007,6 +1014,24 @@ class Tests_REST_Server extends WP_Test_REST_TestCase { $this->assertArrayHasKey( 'help', $index->get_links() ); $this->assertArrayNotHasKey( 'wp:active-theme', $index->get_links() ); + + // Check site icon. + $this->assertArrayHasKey( 'site_icon', $data ); + } + + /** + * @ticket 52321 + */ + public function test_get_index_with_site_icon() { + $server = new WP_REST_Server(); + update_option( 'site_icon', self::$icon_id ); + + $request = new WP_REST_Request( 'GET', '/' ); + $index = $server->dispatch( $request ); + $data = $index->get_data(); + + $this->assertArrayHasKey( 'site_icon', $data ); + $this->assertEquals( self::$icon_id, $data['site_icon'] ); } public function test_get_namespace_index() { diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 9ec0077690..f6c0026248 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -10369,7 +10369,8 @@ mockedApiResponse.Schema = { ] } }, - "site_logo": false + "site_logo": 0, + "site_icon": 0 }; mockedApiResponse.oembed = {