Skip to content

Commit

Permalink
REST API: Move all links to prepare_links method in theme REST API co…
Browse files Browse the repository at this point in the history
…ntroller.

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
  • Loading branch information
spacedmonkey committed Jun 21, 2022
1 parent 14a4682 commit 40c4f11
Showing 1 changed file with 17 additions and 16 deletions.
Expand Up @@ -333,21 +333,6 @@ public function prepare_item_for_response( $item, $request ) {

$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.
*
Expand All @@ -369,14 +354,30 @@ public function prepare_item_for_response( $item, $request ) {
* @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() ) ),
),
'collection' => array(
'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;
}

/**
Expand Down

0 comments on commit 40c4f11

Please sign in to comment.