wppaste
WordPress

WP_REST_Global_Styles_Controller::get_theme_item( WP_REST_Request $request ): WP_REST_Response|WP_Error

Since
5.9.0, 6.6.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php:557
Returns the given theme global styles config.

Compatibility

WordPress
since 6.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$requestWP_REST_Request
The request instance.

Return value

WP_REST_Response|WP_Error

Performance profile

How much work a call to WP_REST_Global_Styles_Controller::get_theme_item() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
15–92

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 103.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()one call below WP_REST_Global_Styles_Controller::get_theme_item()
  • optionoption read or writeget_option()one call below WP_REST_Global_Styles_Controller::get_theme_item()

Further down the call graph this can also reach cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 13 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Global_Styles_Controller::get_theme_item() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always15get_stylesheet(), __()
!rest_is_field_included()54–55get_stylesheet(), ::WP_Theme_JSON_Resolver(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included()
always58–59get_stylesheet(), ::WP_Theme_JSON_Resolver(), ->get_fields_for_response(), rest_is_field_included(), ->get_settings(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included()
always62–64get_stylesheet(), ::WP_Theme_JSON_Resolver(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), ->get_raw_data(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included()
always66–68get_stylesheet(), ::WP_Theme_JSON_Resolver(), ->get_fields_for_response(), rest_is_field_included(), ->get_settings(), rest_is_field_included(), ->get_raw_data(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included()
always71–74get_stylesheet(), ::WP_Theme_JSON_Resolver(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_url(), ::WP_Theme_JSON_Resolver(), ->add_links()
always75–78get_stylesheet(), ::WP_Theme_JSON_Resolver(), ->get_fields_for_response(), rest_is_field_included(), ->get_settings(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_url(), ::WP_Theme_JSON_Resolver(), ->add_links()
always76–79get_stylesheet(), ::WP_Theme_JSON_Resolver(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), rest_url(), ::WP_Theme_JSON_Resolver(), ->add_links()
always79–83get_stylesheet(), ::WP_Theme_JSON_Resolver(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), ->get_raw_data(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_url(), ::WP_Theme_JSON_Resolver(), ->add_links()
always80–83get_stylesheet(), ::WP_Theme_JSON_Resolver(), ->get_fields_for_response(), rest_is_field_included(), ->get_settings(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), rest_url(), ::WP_Theme_JSON_Resolver(), ->add_links()
rest_is_field_included()83–87get_stylesheet(), ::WP_Theme_JSON_Resolver(), ->get_fields_for_response(), rest_is_field_included(), ->get_settings(), rest_is_field_included(), ->get_raw_data(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_url(), ::WP_Theme_JSON_Resolver(), ->add_links()
always84–88get_stylesheet(), ::WP_Theme_JSON_Resolver(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), ->get_raw_data(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), rest_url(), ::WP_Theme_JSON_Resolver(), ->add_links()
1 further outcome, up to 92 instructions
always88–92get_stylesheet(), ::WP_Theme_JSON_Resolver(), ->get_fields_for_response(), rest_is_field_included(), ->get_settings(), rest_is_field_included(), ->get_raw_data(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), rest_url(), ::WP_Theme_JSON_Resolver(), ->add_links()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10315–928
8.510315–928
8.410315–9281 fewer instruction than PHP 8.3
8.310415–938
8.210415–938
8.110415–9382 fewer instructions than PHP 7.4
7.410615–958

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 11

  • get_stylesheet()Retrieves name of the current stylesheet.
  • __()Retrieves the translation of $text.
  • rest_is_field_included()Given an array of fields to include in a response, some of which may be `nested.fields`, determine whether the provided field should be included in the response body.
  • rest_ensure_response()Ensures a REST response is a response object (for consistency).
  • rest_url()Retrieves the URL to a REST endpoint.
  • WP_Error::__construct()Initializes the error.
  • WP_Theme_JSON_Resolver::get_merged_data()Returns the data merged from multiple origins.
  • WP_REST_Global_Styles_Controller::get_fields_for_response()
  • WP_REST_Global_Styles_Controller::add_additional_fields_to_object()
  • WP_REST_Global_Styles_Controller::filter_response_by_context()
  • WP_Theme_JSON_Resolver::get_resolved_theme_uris()Resolves relative paths in theme.json styles to theme absolute paths and returns them in an array that can be embedded as the value of `_link` object in REST API responses.

Source code

	public function get_theme_item( $request ) {		if ( get_stylesheet() !== $request['stylesheet'] ) {			// This endpoint only supports the active theme for now.			return new WP_Error(				'rest_theme_not_found',				__( 'Theme not found.' ),				array( 'status' => 404 )			);		} 		$theme  = WP_Theme_JSON_Resolver::get_merged_data( 'theme' );		$fields = $this->get_fields_for_response( $request );		$data   = array(); 		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 ); 		if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {			$links               = array(				'self' => array(					'href' => rest_url( sprintf( '%s/%s/themes/%s', $this->namespace, $this->rest_base, $request['stylesheet'] ) ),				),			);			$resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme );			if ( ! empty( $resolved_theme_uris ) ) {				$links['https://api.w.org/theme-file'] = $resolved_theme_uris;			}			$response->add_links( $links );		} 		return $response;	}

Changelog

Introduced in 5.9.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.6.0
Added custom relative theme file URIs to _links.from the docblock
5.9.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.