Skip to content

Commit aed3eae

Browse files
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
1 parent aae0000 commit aed3eae

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

src/wp-includes/rest-api/class-wp-rest-server.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,7 @@ public function get_index( $request ) {
12291229
$response->add_link( 'help', 'https://developer.wordpress.org/rest-api/' );
12301230
$this->add_active_theme_link_to_index( $response );
12311231
$this->add_site_logo_to_index( $response );
1232+
$this->add_site_icon_to_index( $response );
12321233

12331234
/**
12341235
* Filters the REST API root index data.
@@ -1275,6 +1276,7 @@ protected function add_active_theme_link_to_index( WP_REST_Response $response )
12751276

12761277
/**
12771278
* Exposes the site logo through the WordPress REST API.
1279+
*
12781280
* This is used for fetching this information when user has no rights
12791281
* to update settings.
12801282
*
@@ -1283,14 +1285,47 @@ protected function add_active_theme_link_to_index( WP_REST_Response $response )
12831285
* @param WP_REST_Response $response REST API response.
12841286
*/
12851287
protected function add_site_logo_to_index( WP_REST_Response $response ) {
1286-
$site_logo_id = get_theme_mod( 'custom_logo' );
1287-
$response->data['site_logo'] = $site_logo_id;
1288-
if ( $site_logo_id ) {
1288+
$site_logo_id = get_theme_mod( 'custom_logo', 0 );
1289+
1290+
$this->add_image_to_index( $response, $site_logo_id, 'site_logo' );
1291+
}
1292+
1293+
/**
1294+
* Exposes the site icon through the WordPress REST API.
1295+
*
1296+
* This is used for fetching this information when user has no rights
1297+
* to update settings.
1298+
*
1299+
* @since 5.9.0
1300+
*
1301+
* @param WP_REST_Response $response REST API response.
1302+
*/
1303+
protected function add_site_icon_to_index( WP_REST_Response $response ) {
1304+
$site_icon_id = get_option( 'site_icon', 0 );
1305+
1306+
$this->add_image_to_index( $response, $site_icon_id, 'site_icon' );
1307+
}
1308+
1309+
/**
1310+
* Exposes an image through the WordPress REST API.
1311+
* This is used for fetching this information when user has no rights
1312+
* to update settings.
1313+
*
1314+
* @since 5.9.0
1315+
*
1316+
* @param WP_REST_Response $response REST API response.
1317+
* @param int $image_id Image attachment ID.
1318+
* @param string $type Type of Image.
1319+
*/
1320+
protected function add_image_to_index( WP_REST_Response $response, $image_id, $type ) {
1321+
$response->data[ $type ] = (int) $image_id;
1322+
if ( $image_id ) {
12891323
$response->add_link(
12901324
'https://api.w.org/featuredmedia',
1291-
rest_url( rest_get_route_for_post( $site_logo_id ) ),
1325+
rest_url( rest_get_route_for_post( $image_id ) ),
12921326
array(
12931327
'embeddable' => true,
1328+
'type' => $type,
12941329
)
12951330
);
12961331
}

tests/phpunit/tests/rest-api/rest-server.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
* @group restapi
1111
*/
1212
class Tests_REST_Server extends WP_Test_REST_TestCase {
13+
protected static $icon_id;
14+
15+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
16+
$filename = DIR_TESTDATA . '/images/test-image-large.jpg';
17+
self::$icon_id = $factory->attachment->create_upload_object( $filename );
18+
}
19+
1320
public function set_up() {
1421
parent::set_up();
1522

@@ -1007,6 +1014,24 @@ public function test_get_index() {
10071014

10081015
$this->assertArrayHasKey( 'help', $index->get_links() );
10091016
$this->assertArrayNotHasKey( 'wp:active-theme', $index->get_links() );
1017+
1018+
// Check site icon.
1019+
$this->assertArrayHasKey( 'site_icon', $data );
1020+
}
1021+
1022+
/**
1023+
* @ticket 52321
1024+
*/
1025+
public function test_get_index_with_site_icon() {
1026+
$server = new WP_REST_Server();
1027+
update_option( 'site_icon', self::$icon_id );
1028+
1029+
$request = new WP_REST_Request( 'GET', '/' );
1030+
$index = $server->dispatch( $request );
1031+
$data = $index->get_data();
1032+
1033+
$this->assertArrayHasKey( 'site_icon', $data );
1034+
$this->assertEquals( self::$icon_id, $data['site_icon'] );
10101035
}
10111036

10121037
public function test_get_namespace_index() {

tests/qunit/fixtures/wp-api-generated.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10369,7 +10369,8 @@ mockedApiResponse.Schema = {
1036910369
]
1037010370
}
1037110371
},
10372-
"site_logo": false
10372+
"site_logo": 0,
10373+
"site_icon": 0
1037310374
};
1037410375

1037510376
mockedApiResponse.oembed = {

0 commit comments

Comments
 (0)