REST API: Expose the site icon in the REST API index.

Props spacedmonkey, palmiak.
Fixes #52321.


git-svn-id: https://develop.svn.wordpress.org/trunk@52080 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Timothy Jacobs 2021-11-09 20:36:37 +00:00
parent aae0000cef
commit aed3eae13f
3 changed files with 66 additions and 5 deletions

View File

@ -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,
)
);
}

View File

@ -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() {

View File

@ -10369,7 +10369,8 @@ mockedApiResponse.Schema = {
]
}
},
"site_logo": false
"site_logo": 0,
"site_icon": 0
};
mockedApiResponse.oembed = {