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.

git-svn-id: https://develop.svn.wordpress.org/trunk@53544 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris 2022-06-21 10:46:49 +00:00
parent 14a468294f
commit 40c4f11a81

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;
}
/**