wppaste
WordPress

WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( WP_Theme $theme, bool $create_post = false, array $post_status_filter = array('publish') ): array

Since
5.9.0
Source
wp-includes/class-wp-theme-json-resolver.php:478
Returns the custom post type that contains the user's origin config for the active theme or an empty array if none are found.

Description

This can also create and return a new draft custom post type.

Compatibility

WordPress
since 5.9.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

$themeWP_Theme
The theme object. If empty, it defaults to the active theme.
$create_postbooloptional
Whether a new custom post type should be created if none are found. Default false.Default: false
$post_status_filterarrayoptional
Filter custom post type by post status. Default array( 'publish' ), so it only fetches published posts.Default: array('publish')

Return value

array
Custom Post Type for the user's origin config.

Performance profile

How much work a call to WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles() 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
Heavy

Reaches the database via ->query().

Scaling
Constant

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

Instructions
36–76

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • sqldatabase query->query()called directly
  • hookthird-party callbacksapply_filters()one call below WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles()
  • optionoption read or writeget_option()one call below WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles()

Further down the call graph this can also reach cache, serialize 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 · 10 distinct outcomes

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

WhenInstructionsCalls it makes
$theme instanceof36–39->get_stylesheet(), ->query()
!($theme instanceof)39–42wp_get_theme(), ->get_stylesheet(), ->query()
$theme instanceof && $value instanceof43->get_stylesheet(), ->query(), get_object_vars()
!($theme instanceof) && $value instanceof46wp_get_theme(), ->get_stylesheet(), ->query(), get_object_vars()
$theme instanceof && is_wp_error()60–63->get_stylesheet(), ->query(), urlencode(), wp_theme(), is_wp_error()
!($theme instanceof) && is_wp_error()63–66wp_get_theme(), ->get_stylesheet(), ->query(), urlencode(), wp_theme(), is_wp_error()
$theme instanceof && !is_wp_error() && !($post instanceof)66–69->get_stylesheet(), ->query(), urlencode(), wp_theme(), is_wp_error(), get_post()
!($theme instanceof) && !is_wp_error() && !($post instanceof)69–72wp_get_theme(), ->get_stylesheet(), ->query(), urlencode(), wp_theme(), is_wp_error(), get_post()
$theme instanceof && !is_wp_error() && $post instanceof70–73->get_stylesheet(), ->query(), urlencode(), wp_theme(), is_wp_error(), get_post(), get_object_vars()
!($theme instanceof) && !is_wp_error() && $post instanceof73–76wp_get_theme(), ->get_stylesheet(), ->query(), urlencode(), wp_theme(), is_wp_error(), get_post(), get_object_vars()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8236–766
8.58236–766
8.48236–7663 fewer instructions than PHP 8.3
8.38536–796
8.28536–796
8.18536–796
7.48536–796

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 · 5

Used by · 1

Source code

	public static function get_user_data_from_wp_global_styles( $theme, $create_post = false, $post_status_filter = array( 'publish' ) ) {		if ( ! $theme instanceof WP_Theme ) {			$theme = wp_get_theme();		} 		$user_cpt         = array();		$post_type_filter = 'wp_global_styles';		$stylesheet       = $theme->get_stylesheet();		$args             = array(			'posts_per_page'         => 1,			'orderby'                => 'date',			'order'                  => 'desc',			'post_type'              => $post_type_filter,			'post_status'            => $post_status_filter,			'ignore_sticky_posts'    => true,			'no_found_rows'          => true,			'update_post_meta_cache' => false,			'update_post_term_cache' => false,			'tax_query'              => array(				array(					'taxonomy' => 'wp_theme',					'field'    => 'name',					'terms'    => $stylesheet,				),			),		); 		$global_style_query = new WP_Query();		$recent_posts       = $global_style_query->query( $args );		if ( count( $recent_posts ) === 1 && $recent_posts[0] instanceof WP_Post ) {			$user_cpt = get_object_vars( $recent_posts[0] );		} elseif ( $create_post ) {			$cpt_post_id = wp_insert_post(				array(					'post_content' => '{"version": ' . WP_Theme_JSON::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }',					'post_status'  => 'publish',					'post_title'   => 'Custom Styles', // Do not make string translatable, see https://core.trac.wordpress.org/ticket/54518.					'post_type'    => $post_type_filter,					'post_name'    => sprintf( 'wp-global-styles-%s', urlencode( $stylesheet ) ),					'tax_input'    => array(						'wp_theme' => array( $stylesheet ),					),				),				true			);			if ( ! is_wp_error( $cpt_post_id ) ) {				$post = get_post( $cpt_post_id );				if ( $post instanceof WP_Post ) {					$user_cpt = get_object_vars( $post );				}			}		} 		return $user_cpt;	}

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.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-theme-json-resolver.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.