diff --git a/wp-includes/class-wp-theme-json-resolver.php b/wp-includes/class-wp-theme-json-resolver.php index 5d0dd099c2..42ad830c73 100644 --- a/wp-includes/class-wp-theme-json-resolver.php +++ b/wp-includes/class-wp-theme-json-resolver.php @@ -304,54 +304,59 @@ class WP_Theme_JSON_Resolver { } $user_cpt = array(); $post_type_filter = 'wp_global_styles'; + $stylesheet = $theme->get_stylesheet(); $args = array( - 'numberposts' => 1, - 'orderby' => 'date', - 'order' => 'desc', - 'post_type' => $post_type_filter, - 'post_status' => $post_status_filter, - 'tax_query' => array( + 'posts_per_page' => 1, + 'orderby' => 'post_date', + 'order' => 'desc', + 'post_type' => $post_type_filter, + 'post_status' => $post_status_filter, + 'ignore_sticky_posts' => true, + 'no_found_rows' => true, + 'tax_query' => array( array( 'taxonomy' => 'wp_theme', 'field' => 'name', - 'terms' => $theme->get_stylesheet(), + 'terms' => $stylesheet, ), ), ); $cache_key = sprintf( 'wp_global_styles_%s', md5( serialize( $args ) ) ); - $post_id = wp_cache_get( $cache_key ); - - if ( (int) $post_id > 0 ) { - return get_post( $post_id, ARRAY_A ); - } - + $post_id = (int) get_transient( $cache_key ); // Special case: '-1' is a results not found. if ( -1 === $post_id && ! $create_post ) { return $user_cpt; } - $recent_posts = wp_get_recent_posts( $args ); - if ( is_array( $recent_posts ) && ( count( $recent_posts ) === 1 ) ) { - $user_cpt = $recent_posts[0]; + if ( $post_id > 0 && in_array( get_post_status( $post_id ), (array) $post_status_filter, true ) ) { + return get_post( $post_id, ARRAY_A ); + } + + $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 ); } 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', + 'post_title' => __( 'Custom Styles' ), 'post_type' => $post_type_filter, - 'post_name' => 'wp-global-styles-' . urlencode( wp_get_theme()->get_stylesheet() ), + 'post_name' => sprintf( 'wp-global-styles-%s', urlencode( $stylesheet ) ), 'tax_input' => array( - 'wp_theme' => array( wp_get_theme()->get_stylesheet() ), + 'wp_theme' => array( $stylesheet ), ), ), true ); - $user_cpt = get_post( $cpt_post_id, ARRAY_A ); + if ( ! is_wp_error( $cpt_post_id ) ) { + $user_cpt = get_post( $cpt_post_id, ARRAY_A ); + } } $cache_expiration = $user_cpt ? DAY_IN_SECONDS : HOUR_IN_SECONDS; - wp_cache_set( $cache_key, $user_cpt ? $user_cpt['ID'] : -1, '', $cache_expiration ); + set_transient( $cache_key, $user_cpt ? $user_cpt['ID'] : -1, $cache_expiration ); return $user_cpt; } diff --git a/wp-includes/version.php b/wp-includes/version.php index 3902e16db8..b570716110 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1-alpha-54185'; +$wp_version = '6.1-alpha-54186'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.