Make WordPress Core


Ignore:
Timestamp:
02/06/2023 03:31:50 PM (4 years ago)
Author:
jorgefilipecosta
Message:

Block editor: Update WP_Theme_JSON_Resolver and improve its performance.

This commit includes the latest updates WP_Theme_JSON_Resolver class made in the block editor. Some of these updates improve the performance of the class.

Props Mamaduka, hellofromTonya, flixos90, jorgefilipecosta, oandregal, spacedmonkey, audrasjb, costdev, scruffian.
Closes #57545.

File:
1 edited

Legend:

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

    r55146 r55231  
    428428                $stylesheet       = $theme->get_stylesheet();
    429429                $args             = array(
    430                         'posts_per_page'      => 1,
    431                         'orderby'             => 'date',
    432                         'order'               => 'desc',
    433                         'post_type'           => $post_type_filter,
    434                         'post_status'         => $post_status_filter,
    435                         'ignore_sticky_posts' => true,
    436                         'no_found_rows'       => true,
    437                         'tax_query'           => array(
     430                        'posts_per_page'         => 1,
     431                        'orderby'                => 'date',
     432                        'order'                  => 'desc',
     433                        'post_type'              => $post_type_filter,
     434                        'post_status'            => $post_status_filter,
     435                        'ignore_sticky_posts'    => true,
     436                        'no_found_rows'          => true,
     437                        'update_post_meta_cache' => false,
     438                        'update_post_term_cache' => false,
     439                        'tax_query'              => array(
    438440                                array(
    439441                                        'taxonomy' => 'wp_theme',
     
    447449                $recent_posts       = $global_style_query->query( $args );
    448450                if ( count( $recent_posts ) === 1 ) {
    449                         $user_cpt = get_post( $recent_posts[0], ARRAY_A );
     451                        $user_cpt = get_object_vars( $recent_posts[0] );
    450452                } elseif ( $create_post ) {
    451453                        $cpt_post_id = wp_insert_post(
     
    463465                        );
    464466                        if ( ! is_wp_error( $cpt_post_id ) ) {
    465                                 $user_cpt = get_post( $cpt_post_id, ARRAY_A );
     467                                $user_cpt = get_object_vars( get_post( $cpt_post_id ) );
    466468                        }
    467469                }
     
    526528         * Returns the data merged from multiple origins.
    527529         *
    528          * There are three sources of data (origins) for a site:
    529          * default, theme, and custom. The custom's has higher priority
    530          * than the theme's, and the theme's higher than default's.
     530         * There are four sources of data (origins) for a site:
     531         *
     532         * - default => WordPress
     533         * - blocks  => each one of the blocks provides data for itself
     534         * - theme   => the active theme
     535         * - custom  => data provided by the user
     536         *
     537         * The custom's has higher priority than the theme's, the theme's higher than blocks',
     538         * and block's higher than default's.
    531539         *
    532540         * Unlike the getters
     
    536544         * this method returns data after it has been merged with the previous origins.
    537545         * This means that if the same piece of data is declared in different origins
    538          * (user, theme, and core), the last origin overrides the previous.
     546         * (default, blocks, theme, custom), the last origin overrides the previous.
    539547         *
    540548         * For example, if the user has set a background color
     
    546554         *              added the `$origin` parameter.
    547555         * @since 6.1.0 Added block data and generation of spacingSizes array.
    548          *
    549          * @param string $origin Optional. To what level should we merge data.
    550          *                       Valid values are 'theme' or 'custom'. Default 'custom'.
     556         * @since 6.2.0 Changed ' $origin' parameter values to 'default', 'blocks', 'theme' or 'custom'.
     557         *
     558         * @param string $origin Optional. To what level should we merge data: 'default', 'blocks', 'theme' or 'custom'.
     559         *                       'custom' is used as default value as well as fallback value if the origin is unknown.
    551560         * @return WP_Theme_JSON
    552561         */
     
    557566
    558567                $result = static::get_core_data();
     568                if ( 'default' === $origin ) {
     569                        $result->set_spacing_sizes();
     570                        return $result;
     571                }
     572
    559573                $result->merge( static::get_block_data() );
     574                if ( 'blocks' === $origin ) {
     575                        return $result;
     576                }
     577
    560578                $result->merge( static::get_theme_data() );
    561 
    562                 if ( 'custom' === $origin ) {
    563                         $result->merge( static::get_user_data() );
    564                 }
    565 
    566                 // Generate the default spacingSizes array based on the merged spacingScale settings.
     579                if ( 'theme' === $origin ) {
     580                        $result->set_spacing_sizes();
     581                        return $result;
     582                }
     583
     584                $result->merge( static::get_user_data() );
    567585                $result->set_spacing_sizes();
    568586
     
    651669
    652670        /**
     671         * Returns an array of all nested JSON files within a given directory.
     672         *
     673         * @since 6.2.0
     674         *
     675         * @param string $dir The directory to recursively iterate and list files of.
     676         * @return array The merged array.
     677         */
     678        private static function recursively_iterate_json( $dir ) {
     679                $nested_files      = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) );
     680                $nested_json_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) );
     681                return $nested_json_files;
     682        }
     683
     684
     685        /**
    653686         * Returns the style variations defined by the theme.
    654687         *
    655688         * @since 6.0.0
     689         * @since 6.2.0 Returns parent theme variations if theme is a child.
    656690         *
    657691         * @return array
    658692         */
    659693        public static function get_style_variations() {
    660                 $variations     = array();
    661                 $base_directory = get_stylesheet_directory() . '/styles';
     694                $variation_files    = array();
     695                $variations         = array();
     696                $base_directory     = get_stylesheet_directory() . '/styles';
     697                $template_directory = get_template_directory() . '/styles';
    662698                if ( is_dir( $base_directory ) ) {
    663                         $nested_files      = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) );
    664                         $nested_html_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) );
    665                         ksort( $nested_html_files );
    666                         foreach ( $nested_html_files as $path => $file ) {
    667                                 $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) );
    668                                 if ( is_array( $decoded_file ) ) {
    669                                         $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) );
    670                                         $variation  = ( new WP_Theme_JSON( $translated ) )->get_raw_data();
    671                                         if ( empty( $variation['title'] ) ) {
    672                                                 $variation['title'] = basename( $path, '.json' );
     699                        $variation_files = static::recursively_iterate_json( $base_directory );
     700                }
     701                if ( is_dir( $template_directory ) && $template_directory !== $base_directory ) {
     702                        $variation_files_parent = static::recursively_iterate_json( $template_directory );
     703                        // If the child and parent variation file basename are the same, only include the child theme's.
     704                        foreach ( $variation_files_parent as $parent_path => $parent ) {
     705                                foreach ( $variation_files as $child_path => $child ) {
     706                                        if ( basename( $parent_path ) === basename( $child_path ) ) {
     707                                                unset( $variation_files_parent[ $parent_path ] );
    673708                                        }
    674                                         $variations[] = $variation;
    675709                                }
    676710                        }
     711                        $variation_files = array_merge( $variation_files, $variation_files_parent );
     712                }
     713                ksort( $variation_files );
     714                foreach ( $variation_files as $path => $file ) {
     715                        $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) );
     716                        if ( is_array( $decoded_file ) ) {
     717                                $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) );
     718                                $variation  = ( new WP_Theme_JSON( $translated ) )->get_raw_data();
     719                                if ( empty( $variation['title'] ) ) {
     720                                        $variation['title'] = basename( $path, '.json' );
     721                                }
     722                                $variations[] = $variation;
     723                        }
    677724                }
    678725                return $variations;
    679726        }
    680 
    681727}
Note: See TracChangeset for help on using the changeset viewer.