Skip to content

Commit 40c4f11

Browse files
committed
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
1 parent 14a4682 commit 40c4f11

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -333,21 +333,6 @@ public function prepare_item_for_response( $item, $request ) {
333333

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.
353338
*
@@ -369,14 +354,30 @@ public function prepare_item_for_response( $item, $request ) {
369354
* @return array Links for the given block type.
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() ) ),
375360
),
376361
'collection' => array(
377362
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
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

382383
/**

0 commit comments

Comments
 (0)