Make WordPress Core

Changeset 52080


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

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

Props spacedmonkey, palmiak.
Fixes #52321.

Location:
trunk
Files:
3 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            );
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r52068 r52080  
    1111 */
    1212class 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();
     
    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
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r52079 r52080  
    1037010370        }
    1037110371    },
    10372     "site_logo": false
     10372    "site_logo": 0,
     10373    "site_icon": 0
    1037310374};
    1037410375
Note: See TracChangeset for help on using the changeset viewer.