Changeset 56771
- Timestamp:
- 10/03/2023 06:17:03 PM (18 months ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-patterns.php
r56765 r56771 342 342 343 343 foreach ( $themes as $theme ) { 344 $pattern _data= _wp_get_block_patterns( $theme );345 $dirpath 346 $text_domain 347 348 foreach ( $pattern _data['patterns']as $file => $pattern_data ) {344 $patterns = _wp_get_block_patterns( $theme ); 345 $dirpath = $theme->get_stylesheet_directory() . '/patterns/'; 346 $text_domain = $theme->get( 'TextDomain' ); 347 348 foreach ( $patterns as $file => $pattern_data ) { 349 349 if ( $registry->is_registered( $pattern_data['slug'] ) ) { 350 350 continue; … … 406 406 * @return array Block pattern data. 407 407 */ 408 409 408 function _wp_get_block_patterns( WP_Theme $theme ) { 410 if ( ! $theme->exists() ) {411 return array(412 'version' => false,413 'patterns' => array(),414 );415 }416 417 $transient_name = 'wp_theme_patterns_' . $theme->get_stylesheet();418 $version = $theme->get( 'Version' );419 409 $can_use_cached = ! wp_is_development_mode( 'theme' ); 420 410 421 411 if ( $can_use_cached ) { 422 $pattern_data = get_transient( $transient_name);423 if ( is_array( $pattern_data ) && $pattern_data['version'] === $version) {412 $pattern_data = $theme->get_pattern_cache(); 413 if ( is_array( $pattern_data ) ) { 424 414 return $pattern_data; 425 415 } 426 416 } 427 417 428 $pattern_data = array(429 'version' => $version,430 'patterns' => array(),431 );432 418 $dirpath = $theme->get_stylesheet_directory() . '/patterns/'; 419 $pattern_data = array(); 433 420 434 421 if ( ! file_exists( $dirpath ) ) { 435 422 if ( $can_use_cached ) { 436 set_transient( $transient_name,$pattern_data );423 $theme->set_pattern_cache( $pattern_data ); 437 424 } 438 425 return $pattern_data; … … 441 428 if ( ! $files ) { 442 429 if ( $can_use_cached ) { 443 set_transient( $transient_name,$pattern_data );430 $theme->set_pattern_cache( $pattern_data ); 444 431 } 445 432 return $pattern_data; … … 474 461 __FUNCTION__, 475 462 sprintf( 476 /* translators: %s: file name. */463 /* translators: 1: file name. */ 477 464 __( 'Could not register file "%s" as a block pattern ("Slug" field missing)' ), 478 465 $file … … 487 474 __FUNCTION__, 488 475 sprintf( 489 /* translators: %1s: file name; %2s: slug value found. */476 /* translators: 1: file name; 2: slug value found. */ 490 477 __( 'Could not register file "%1$s" as a block pattern (invalid slug "%2$s")' ), 491 478 $file, … … 501 488 __FUNCTION__, 502 489 sprintf( 503 /* translators: %1s: file name. */490 /* translators: 1: file name. */ 504 491 __( 'Could not register file "%s" as a block pattern ("Title" field missing)' ), 505 492 $file … … 541 528 $key = str_replace( $dirpath, '', $file ); 542 529 543 $pattern_data[ 'patterns'][$key ] = $pattern;530 $pattern_data[ $key ] = $pattern; 544 531 } 545 532 546 533 if ( $can_use_cached ) { 547 set_transient( $transient_name,$pattern_data );534 $theme->set_pattern_cache( $pattern_data ); 548 535 } 549 536 -
trunk/src/wp-includes/class-wp-theme.php
r56765 r56771 826 826 827 827 /** 828 * Clear block pattern cache. 828 * Gets block pattern cache. 829 * 830 * @since 6.4.0 831 * 832 * @return array|false Returns an array of patterns if cache is found, otherwise false. 833 */ 834 public function get_pattern_cache() { 835 if ( ! $this->exists() ) { 836 return false; 837 } 838 $pattern_data = get_transient( 'wp_theme_patterns_' . $this->stylesheet ); 839 if ( is_array( $pattern_data ) && $pattern_data['version'] === $this->get( 'Version' ) ) { 840 return $pattern_data['patterns']; 841 } 842 return false; 843 } 844 845 /** 846 * Sets block pattern cache. 847 * 848 * @since 6.4.0 849 * 850 * @param array $patterns Block patterns data to set in cache. 851 */ 852 public function set_pattern_cache( array $patterns ) { 853 $pattern_data = array( 854 'version' => $this->get( 'Version' ), 855 'patterns' => $patterns, 856 ); 857 set_transient( 'wp_theme_patterns_' . $this->stylesheet, $pattern_data ); 858 } 859 860 /** 861 * Clears block pattern cache. 829 862 * 830 863 * @since 6.4.0 -
trunk/tests/phpunit/tests/blocks/wpGetBlockPatterns.php
r56765 r56771 32 32 $theme = wp_get_theme( 'block-theme-patterns' ); 33 33 _wp_get_block_patterns( $theme ); 34 $transient = get_transient( 'wp_theme_patterns_block-theme-patterns' );35 34 $this->assertSameSets( 36 35 array( 37 'version' => '1.0.0', 38 'patterns' => array( 39 'cta.php' => array( 40 'title' => 'Centered Call To Action', 41 'slug' => 'block-theme-patterns/cta', 42 'description' => '', 43 'categories' => array( 'call-to-action' ), 44 ), 36 'cta.php' => array( 37 'title' => 'Centered Call To Action', 38 'slug' => 'block-theme-patterns/cta', 39 'description' => '', 40 'categories' => array( 'call-to-action' ), 45 41 ), 46 42 ), 47 $t ransient,43 $theme->get_pattern_cache(), 48 44 'The transient for block theme patterns should be set' 49 45 ); 50 $theme->cache_delete(); 51 $transient = get_transient( 'wp_theme_patterns_block-theme-patterns' ); 46 $theme->delete_pattern_cache(); 52 47 $this->assertFalse( 53 $t ransient,48 $theme->get_pattern_cache(), 54 49 'The transient for block theme patterns should have been cleared' 55 50 ); … … 61 56 public function test_should_clear_transient_after_switching_theme() { 62 57 switch_theme( 'block-theme' ); 63 _wp_get_block_patterns( wp_get_theme() ); 58 $theme1 = wp_get_theme(); 59 _wp_get_block_patterns( $theme1 ); 64 60 $this->assertSameSets( 65 array( 66 'version' => '1.0.0', 67 'patterns' => array(), 68 ), 69 get_transient( 'wp_theme_patterns_block-theme' ), 61 array(), 62 $theme1->get_pattern_cache(), 70 63 'The transient for block theme should be set' 71 64 ); 72 65 switch_theme( 'block-theme-patterns' ); 73 $this->assertFalse( get_transient( 'wp_theme_patterns_block-theme'), 'Transient should not be set for block theme after switch theme' );74 $th is->assertFalse( get_transient( 'wp_theme_patterns_block-theme-patterns' ), 'Transient should not be set for block theme patterns before being requested');75 _wp_get_block_patterns( wp_get_theme());76 $transient = get_transient( 'wp_theme_patterns_block-theme-patterns');66 $this->assertFalse( $theme1->get_pattern_cache(), 'Transient should not be set for block theme after switch theme' ); 67 $theme2 = wp_get_theme(); 68 $this->assertFalse( $theme2->get_pattern_cache(), 'Transient should not be set for block theme patterns before being requested' ); 69 _wp_get_block_patterns( $theme2 ); 77 70 $this->assertSameSets( 78 71 array( 79 'version' => '1.0.0', 80 'patterns' => array( 81 'cta.php' => array( 82 'title' => 'Centered Call To Action', 83 'slug' => 'block-theme-patterns/cta', 84 'description' => '', 85 'categories' => array( 'call-to-action' ), 86 ), 72 'cta.php' => array( 73 'title' => 'Centered Call To Action', 74 'slug' => 'block-theme-patterns/cta', 75 'description' => '', 76 'categories' => array( 'call-to-action' ), 87 77 ), 78 88 79 ), 89 $t ransient,80 $theme2->get_pattern_cache(), 90 81 'The transient for block theme patterns should be set' 91 82 ); … … 101 92 array( 102 93 'theme' => 'block-theme', 103 'patterns' => array( 104 'version' => '1.0.0', 105 'patterns' => array(), 106 ), 94 'patterns' => array(), 107 95 ), 108 96 array( 109 97 'theme' => 'block-theme-child', 110 'patterns' => array( 111 'version' => '1.0.0', 112 'patterns' => array(), 113 ), 98 'patterns' => array(), 114 99 ), 115 100 array( 116 101 'theme' => 'block-theme-patterns', 117 102 'patterns' => array( 118 'version' => '1.0.0', 119 'patterns' => array( 120 'cta.php' => array( 121 'title' => 'Centered Call To Action', 122 'slug' => 'block-theme-patterns/cta', 123 'description' => '', 124 'categories' => array( 'call-to-action' ), 125 ), 103 'cta.php' => array( 104 'title' => 'Centered Call To Action', 105 'slug' => 'block-theme-patterns/cta', 106 'description' => '', 107 'categories' => array( 'call-to-action' ), 126 108 ), 127 109 ), … … 129 111 array( 130 112 'theme' => 'broken-theme', 131 'patterns' => array( 132 'version' => false, 133 'patterns' => array(), 134 ), 113 'patterns' => array(), 135 114 ), 136 115 array( 137 116 'theme' => 'invalid', 138 'patterns' => array( 139 'version' => false, 140 'patterns' => array(), 141 ), 117 'patterns' => array(), 142 118 ), 143 119 );
Note: See TracChangeset
for help on using the changeset viewer.