Changeset 55231 for trunk/src/wp-includes/class-wp-theme-json-resolver.php
- Timestamp:
- 02/06/2023 03:31:50 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-theme-json-resolver.php
r55146 r55231 428 428 $stylesheet = $theme->get_stylesheet(); 429 429 $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( 438 440 array( 439 441 'taxonomy' => 'wp_theme', … … 447 449 $recent_posts = $global_style_query->query( $args ); 448 450 if ( count( $recent_posts ) === 1 ) { 449 $user_cpt = get_ post( $recent_posts[0], ARRAY_A);451 $user_cpt = get_object_vars( $recent_posts[0] ); 450 452 } elseif ( $create_post ) { 451 453 $cpt_post_id = wp_insert_post( … … 463 465 ); 464 466 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 ) ); 466 468 } 467 469 } … … 526 528 * Returns the data merged from multiple origins. 527 529 * 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. 531 539 * 532 540 * Unlike the getters … … 536 544 * this method returns data after it has been merged with the previous origins. 537 545 * 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. 539 547 * 540 548 * For example, if the user has set a background color … … 546 554 * added the `$origin` parameter. 547 555 * @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. 551 560 * @return WP_Theme_JSON 552 561 */ … … 557 566 558 567 $result = static::get_core_data(); 568 if ( 'default' === $origin ) { 569 $result->set_spacing_sizes(); 570 return $result; 571 } 572 559 573 $result->merge( static::get_block_data() ); 574 if ( 'blocks' === $origin ) { 575 return $result; 576 } 577 560 578 $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() ); 567 585 $result->set_spacing_sizes(); 568 586 … … 651 669 652 670 /** 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 /** 653 686 * Returns the style variations defined by the theme. 654 687 * 655 688 * @since 6.0.0 689 * @since 6.2.0 Returns parent theme variations if theme is a child. 656 690 * 657 691 * @return array 658 692 */ 659 693 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'; 662 698 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 ] ); 673 708 } 674 $variations[] = $variation;675 709 } 676 710 } 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 } 677 724 } 678 725 return $variations; 679 726 } 680 681 727 }
Note: See TracChangeset
for help on using the changeset viewer.