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
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
- Scaling
- Constant
- Instructions
- 36–76
- Plugin surface
- None
- Called by
- 1
Reaches the database via ->query().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 82.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - sqldatabase query
->query()called directly - hookthird-party callbacks
apply_filters()one call below WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles() - optionoption read or write
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
$theme instanceof | 36–39 | ->get_stylesheet(), ->query() |
!($theme instanceof) | 39–42 | wp_get_theme(), ->get_stylesheet(), ->query() |
$theme instanceof && $value instanceof | 43 | ->get_stylesheet(), ->query(), get_object_vars() |
!($theme instanceof) && $value instanceof | 46 | wp_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–66 | wp_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–72 | wp_get_theme(), ->get_stylesheet(), ->query(), urlencode(), wp_theme(), is_wp_error(), get_post() |
$theme instanceof && !is_wp_error() && $post instanceof | 70–73 | ->get_stylesheet(), ->query(), urlencode(), wp_theme(), is_wp_error(), get_post(), get_object_vars() |
!($theme instanceof) && !is_wp_error() && $post instanceof | 73–76 | wp_get_theme(), ->get_stylesheet(), ->query(), urlencode(), wp_theme(), is_wp_error(), get_post(), get_object_vars() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 82 | 36–76 | 6 | |
| 8.5 | 82 | 36–76 | 6 | |
| 8.4 | 82 | 36–76 | 6 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 85 | 36–79 | 6 | |
| 8.2 | 85 | 36–79 | 6 | |
| 8.1 | 85 | 36–79 | 6 | |
| 7.4 | 85 | 36–79 | 6 |
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
- wp_get_theme()Gets a WP_Theme object for a theme.
- wp_insert_post()Inserts or updates a post in the database.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- get_post()Retrieves post data given a post ID or post object.
- WP_Query::__construct()Constructor.
Used by · 1
- WP_REST_Themes_Controller::prepare_links()Prepares links for the request.
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.
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.