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.
Built from https://develop.svn.wordpress.org/trunk@53544


git-svn-id: http://core.svn.wordpress.org/trunk@53133 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2022-06-21 10:48:21 +00:00
parent 721877d3d2
commit 65b78390ee
2 changed files with 18 additions and 17 deletions

View File

@ -333,21 +333,6 @@ class WP_REST_Themes_Controller extends WP_REST_Controller {
$response->add_links( $this->prepare_links( $theme ) );
if ( $theme->get_stylesheet() === wp_get_theme()->get_stylesheet() ) {
// This creates a record for the active theme if not existent.
$id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
} else {
$user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
$id = isset( $user_cpt['ID'] ) ? $user_cpt['ID'] : null;
}
if ( $id ) {
$response->add_link(
'https://api.w.org/user-global-styles',
rest_url( 'wp/v2/global-styles/' . $id )
);
}
/**
* Filters theme data returned from the REST API.
*
@ -369,7 +354,7 @@ class WP_REST_Themes_Controller extends WP_REST_Controller {
* @return array Links for the given block type.
*/
protected function prepare_links( $theme ) {
return array(
$links = array(
'self' => array(
'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $theme->get_stylesheet() ) ),
),
@ -377,6 +362,22 @@ class WP_REST_Themes_Controller extends WP_REST_Controller {
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
),
);
if ( $this->is_same_theme( $theme, wp_get_theme() ) ) {
// This creates a record for the active theme if not existent.
$id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
} else {
$user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
$id = isset( $user_cpt['ID'] ) ? $user_cpt['ID'] : null;
}
if ( $id ) {
$links['https://api.w.org/user-global-styles'] = array(
'href' => rest_url( 'wp/v2/global-styles/' . $id ),
);
}
return $links;
}
/**

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.1-alpha-53543';
$wp_version = '6.1-alpha-53544';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.