Make WordPress Core


Ignore:
Timestamp:
09/16/2022 10:55:58 AM (2 years ago)
Author:
spacedmonkey
Message:

Posts, Post Types: Improve performance of the get_user_data_from_wp_global_styles method.

Improve the logic found in get_user_data_from_wp_global_styles method. Replace call to wp_get_recent_posts with the more standard, WP_Query for consistancy. Use transient over standard cache, to improve performance on sites without persistent object caching. Improve handling of cases where wp_insert_post returns a WP_Error.

Props spacedmonkey, adamsilverstein, mukesh27, peterwilsoncc, andregal.
Fixes #55392.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json-resolver.php

    r54162 r54186  
    305305        $user_cpt         = array();
    306306        $post_type_filter = 'wp_global_styles';
     307        $stylesheet       = $theme->get_stylesheet();
    307308        $args             = array(
    308             'numberposts' => 1,
    309             'orderby'     => 'date',
    310             'order'       => 'desc',
    311             'post_type'   => $post_type_filter,
    312             'post_status' => $post_status_filter,
    313             'tax_query'   => array(
     309            'posts_per_page'      => 1,
     310            'orderby'             => 'post_date',
     311            'order'               => 'desc',
     312            'post_type'           => $post_type_filter,
     313            'post_status'         => $post_status_filter,
     314            'ignore_sticky_posts' => true,
     315            'no_found_rows'       => true,
     316            'tax_query'           => array(
    314317                array(
    315318                    'taxonomy' => 'wp_theme',
    316319                    'field'    => 'name',
    317                     'terms'    => $theme->get_stylesheet(),
     320                    'terms'    => $stylesheet,
    318321                ),
    319322            ),
     
    321324
    322325        $cache_key = sprintf( 'wp_global_styles_%s', md5( serialize( $args ) ) );
    323         $post_id   = wp_cache_get( $cache_key );
    324 
    325         if ( (int) $post_id > 0 ) {
    326             return get_post( $post_id, ARRAY_A );
    327         }
    328 
     326        $post_id   = (int) get_transient( $cache_key );
    329327        // Special case: '-1' is a results not found.
    330328        if ( -1 === $post_id && ! $create_post ) {
     
    332330        }
    333331
    334         $recent_posts = wp_get_recent_posts( $args );
    335         if ( is_array( $recent_posts ) && ( count( $recent_posts ) === 1 ) ) {
    336             $user_cpt = $recent_posts[0];
     332        if ( $post_id > 0 && in_array( get_post_status( $post_id ), (array) $post_status_filter, true ) ) {
     333            return get_post( $post_id, ARRAY_A );
     334        }
     335
     336        $global_style_query = new WP_Query();
     337        $recent_posts       = $global_style_query->query( $args );
     338        if ( count( $recent_posts ) === 1 ) {
     339            $user_cpt = get_post( $recent_posts[0], ARRAY_A );
    337340        } elseif ( $create_post ) {
    338341            $cpt_post_id = wp_insert_post(
     
    340343                    'post_content' => '{"version": ' . WP_Theme_JSON::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }',
    341344                    'post_status'  => 'publish',
    342                     'post_title'   => 'Custom Styles',
     345                    'post_title'   => __( 'Custom Styles' ),
    343346                    'post_type'    => $post_type_filter,
    344                     'post_name'    => 'wp-global-styles-' . urlencode( wp_get_theme()->get_stylesheet() ),
     347                    'post_name'    => sprintf( 'wp-global-styles-%s', urlencode( $stylesheet ) ),
    345348                    'tax_input'    => array(
    346                         'wp_theme' => array( wp_get_theme()->get_stylesheet() ),
     349                        'wp_theme' => array( $stylesheet ),
    347350                    ),
    348351                ),
    349352                true
    350353            );
    351             $user_cpt    = get_post( $cpt_post_id, ARRAY_A );
     354            if ( ! is_wp_error( $cpt_post_id ) ) {
     355                $user_cpt = get_post( $cpt_post_id, ARRAY_A );
     356            }
    352357        }
    353358        $cache_expiration = $user_cpt ? DAY_IN_SECONDS : HOUR_IN_SECONDS;
    354         wp_cache_set( $cache_key, $user_cpt ? $user_cpt['ID'] : -1, '', $cache_expiration );
     359        set_transient( $cache_key, $user_cpt ? $user_cpt['ID'] : -1, $cache_expiration );
    355360
    356361        return $user_cpt;
Note: See TracChangeset for help on using the changeset viewer.