Skip to content

Commit

Permalink
Add get_theme_item to global styles controller
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Feb 18, 2022
1 parent 34f96ed commit 0023551
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/compat/wordpress-5.9/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@
],
"text": true
},
"dimensions": {
"height": false
},
"spacing": {
"blockGap": null,
"margin": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,52 @@ public function get_theme_items( $request ) {

return $response;
}

/**
* Returns the given theme global styles config.
*
* @since 5.9.0
*
* @param WP_REST_Request $request The request instance.
* @return WP_REST_Response|WP_Error
*/
public function get_theme_item( $request ) {
if ( wp_get_theme()->get_stylesheet() !== $request['stylesheet'] ) {
// This endpoint only supports the active theme for now.
return new WP_Error(
'rest_theme_not_found',
__( 'Theme not found.', 'gutenberg' ),
array( 'status' => 404 )
);
}

$theme = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( 'theme' );
$data = array();
$fields = $this->get_fields_for_response( $request );

if ( rest_is_field_included( 'settings', $fields ) ) {
$data['settings'] = $theme->get_settings();
}

if ( rest_is_field_included( 'styles', $fields ) ) {
$raw_data = $theme->get_raw_data();
$data['styles'] = isset( $raw_data['styles'] ) ? $raw_data['styles'] : array();
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );

$response = rest_ensure_response( $data );

$links = array(
'self' => array(
'href' => rest_url( sprintf( '%s/%s/themes/%s', $this->namespace, $this->rest_base, $request['stylesheet'] ) ),
),
);

$response->add_links( $links );

return $response;
}
}

0 comments on commit 0023551

Please sign in to comment.