-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Performance global styles WP_Query #46043
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,88 @@ | |
* @access private | ||
*/ | ||
class WP_Theme_JSON_Resolver_Gutenberg extends WP_Theme_JSON_Resolver_6_2 { | ||
|
||
/** | ||
* Returns the custom post type that contains the user's origin config | ||
* for the active theme or a void array if none are found. | ||
* | ||
* This can also create and return a new draft custom post type. | ||
* | ||
* | ||
* @param WP_Theme $theme The theme object. If empty, it | ||
* defaults to the active theme. | ||
* @param bool $create_post Optional. Whether a new custom post | ||
* type should be created if none are | ||
* found. Default false. | ||
* @param array $post_status_filter Optional. Filter custom post type by | ||
* post status. Default `array( 'publish' )`, | ||
* so it only fetches published posts. | ||
* @return array Custom Post Type for the user's origin config. | ||
*/ | ||
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(); | ||
} | ||
|
||
/* | ||
* Bail early if the theme does not support a theme.json. | ||
* | ||
* Since WP_Theme_JSON_Resolver::theme_has_support() only supports the active | ||
* theme, the extra condition for whether $theme is the active theme is | ||
* present here. | ||
*/ | ||
if ( $theme->get_stylesheet() === get_stylesheet() && ! static::theme_has_support() ) { | ||
return array(); | ||
} | ||
|
||
$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 ) { | ||
$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 ) ) { | ||
$user_cpt = get_object_vars( get_post( $cpt_post_id ) ); | ||
} | ||
} | ||
|
||
return $user_cpt; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The diff against the version in WordPress core: @@ -34,15 +34,17 @@
$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,
- 'tax_query' => array(
+ $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',
@@ -54,7 +56,7 @@
$global_style_query = new WP_Query();
$recent_posts = $global_style_query->query( $args );
if ( count( $recent_posts ) === 1 ) {
- $user_cpt = get_post( $recent_posts[0], ARRAY_A );
+ $user_cpt = get_object_vars( $recent_posts[0] );
} elseif ( $create_post ) {
$cpt_post_id = wp_insert_post(
array(
@@ -70,7 +72,7 @@
true
);
if ( ! is_wp_error( $cpt_post_id ) ) {
- $user_cpt = get_post( $cpt_post_id, ARRAY_A );
+ $user_cpt = get_object_vars( get_post( $cpt_post_id ) );
}
} |
||
|
||
/** | ||
* Returns the theme's data. | ||
* | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The to_array method of WP_Post, does some weird stuff that loads terms and meta. We dont need this data here. So let's use
get_object_vars
instead.