Skip to content

Commit

Permalink
REST API: Expose the site icon in the REST API index.
Browse files Browse the repository at this point in the history
Props spacedmonkey, palmiak.
Fixes #52321.


git-svn-id: https://develop.svn.wordpress.org/trunk@52080 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
TimothyBJacobs committed Nov 9, 2021
1 parent aae0000 commit aed3eae
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
43 changes: 39 additions & 4 deletions src/wp-includes/rest-api/class-wp-rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,7 @@ public function get_index( $request ) {
$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.
Expand Down Expand Up @@ -1275,6 +1276,7 @@ protected function add_active_theme_link_to_index( WP_REST_Response $response )

/**
* Exposes the site logo through the WordPress REST API.
*
* This is used for fetching this information when user has no rights
* to update settings.
*
Expand All @@ -1283,14 +1285,47 @@ protected function add_active_theme_link_to_index( WP_REST_Response $response )
* @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,
)
);
}
Expand Down
25 changes: 25 additions & 0 deletions tests/phpunit/tests/rest-api/rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -1007,6 +1014,24 @@ public function test_get_index() {

$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() {
Expand Down
3 changes: 2 additions & 1 deletion tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -10369,7 +10369,8 @@ mockedApiResponse.Schema = {
]
}
},
"site_logo": false
"site_logo": 0,
"site_icon": 0
};

mockedApiResponse.oembed = {
Expand Down

0 comments on commit aed3eae

Please sign in to comment.