Make WordPress Core


Ignore:
Timestamp:
06/21/2022 10:46:49 AM (4 years ago)
Author:
spacedmonkey
Message:

REST API: Move all links to prepare_links method in theme REST API controller.

Move logic to generate link to global styles to the prepare_links method in theme REST API controller. This way all logic to generate links is within the prepare_links method, bringing this controller inline with other REST API controllers.

Props SergeyBiryukov, Spacedmonkey.
Fixes #56018.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php

    r52610 r53544  
    334334                $response->add_links( $this->prepare_links( $theme ) );
    335335
    336                 if ( $theme->get_stylesheet() === wp_get_theme()->get_stylesheet() ) {
    337                         // This creates a record for the active theme if not existent.
    338                         $id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
    339                 } else {
    340                         $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
    341                         $id       = isset( $user_cpt['ID'] ) ? $user_cpt['ID'] : null;
    342                 }
    343 
    344                 if ( $id ) {
    345                         $response->add_link(
    346                                 'https://api.w.org/user-global-styles',
    347                                 rest_url( 'wp/v2/global-styles/' . $id )
    348                         );
    349                 }
    350 
    351336                /**
    352337                 * Filters theme data returned from the REST API.
    … …  
    370355         */
    371356        protected function prepare_links( $theme ) {
    372                 return array(
     357                $links = array(
    373358                        'self'       => array(
    374359                                'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $theme->get_stylesheet() ) ),
    … …  
    378363                        ),
    379364                );
     365
     366                if ( $this->is_same_theme( $theme, wp_get_theme() ) ) {
     367                        // This creates a record for the active theme if not existent.
     368                        $id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
     369                } else {
     370                        $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
     371                        $id       = isset( $user_cpt['ID'] ) ? $user_cpt['ID'] : null;
     372                }
     373
     374                if ( $id ) {
     375                        $links['https://api.w.org/user-global-styles'] = array(
     376                                'href' => rest_url( 'wp/v2/global-styles/' . $id ),
     377                        );
     378                }
     379
     380                return $links;
    380381        }
    381382
Note: See TracChangeset for help on using the changeset viewer.