Make WordPress Core


Ignore:
Timestamp:
11/09/2021 08:36:37 PM (5 years ago)
Author:
TimothyBlynJacobs
Message:

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

Props spacedmonkey, palmiak.
Fixes #52321.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/class-wp-rest-server.php

    r52068 r52080  
    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                /**
     
    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.
     
    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                        );
Note: See TracChangeset for help on using the changeset viewer.