From 85ac1aacb803bcd120a321d5b76cd7ecac202be6 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 20 Sep 2022 10:46:37 +0100 Subject: [PATCH] Backport change from WordPress 6.0. --- .../class-wp-theme-json-resolver-6-0.php | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/lib/compat/wordpress-6.0/class-wp-theme-json-resolver-6-0.php b/lib/compat/wordpress-6.0/class-wp-theme-json-resolver-6-0.php index b85e8e335037af..5a158d4dfbcf8f 100644 --- a/lib/compat/wordpress-6.0/class-wp-theme-json-resolver-6-0.php +++ b/lib/compat/wordpress-6.0/class-wp-theme-json-resolver-6-0.php @@ -168,54 +168,59 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post } $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_Gutenberg::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }', 'post_status' => 'publish', - 'post_title' => __( 'Custom Styles', 'default' ), + 'post_title' => 'Custom Styles', // Do not make string translatable, see https://core.trac.wordpress.org/ticket/54518. '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; }