Changeset 55236
- Timestamp:
- 02/06/2023 07:57:41 PM (21 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-theme.php
r54236 r55236 115 115 116 116 /** 117 * Is this theme a block theme. 118 * 119 * @since 6.2.0 120 * @var bool 121 */ 122 private $block_theme; 123 124 /** 117 125 * Header name from the theme's style.css after being translated. 118 126 * … … 251 259 252 260 if ( is_array( $cache ) ) { 253 foreach ( array( ' errors', 'headers', 'template' ) as $key ) {261 foreach ( array( 'block_theme', 'errors', 'headers', 'template' ) as $key ) { 254 262 if ( isset( $cache[ $key ] ) ) { 255 263 $this->$key = $cache[ $key ]; … … 276 284 $this->errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) ); 277 285 } 278 $this->template = $this->stylesheet; 286 $this->template = $this->stylesheet; 287 $this->block_theme = false; 279 288 $this->cache_add( 280 289 'theme', 281 290 array( 282 'headers' => $this->headers, 283 'errors' => $this->errors, 284 'stylesheet' => $this->stylesheet, 285 'template' => $this->template, 291 'block_theme' => $this->block_theme, 292 'headers' => $this->headers, 293 'errors' => $this->errors, 294 'stylesheet' => $this->stylesheet, 295 'template' => $this->template, 286 296 ) 287 297 ); … … 294 304 $this->errors = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) ); 295 305 $this->template = $this->stylesheet; 306 $this->block_theme = false; 296 307 $this->cache_add( 297 308 'theme', 298 309 array( 299 'headers' => $this->headers, 300 'errors' => $this->errors, 301 'stylesheet' => $this->stylesheet, 302 'template' => $this->template, 310 'block_theme' => $this->block_theme, 311 'headers' => $this->headers, 312 'errors' => $this->errors, 313 'stylesheet' => $this->stylesheet, 314 'template' => $this->template, 303 315 ) 304 316 ); … … 328 340 'theme', 329 341 array( 330 'headers' => $this->headers, 331 'errors' => $this->errors, 332 'stylesheet' => $this->stylesheet, 342 'block_theme' => $this->is_block_theme(), 343 'headers' => $this->headers, 344 'errors' => $this->errors, 345 'stylesheet' => $this->stylesheet, 333 346 ) 334 347 ); … … 364 377 'theme', 365 378 array( 366 'headers' => $this->headers, 367 'errors' => $this->errors, 368 'stylesheet' => $this->stylesheet, 369 'template' => $this->template, 379 'block_theme' => $this->is_block_theme(), 380 'headers' => $this->headers, 381 'errors' => $this->errors, 382 'stylesheet' => $this->stylesheet, 383 'template' => $this->template, 370 384 ) 371 385 ); … … 400 414 'theme', 401 415 array( 402 'headers' => $this->headers, 403 'errors' => $this->errors, 404 'stylesheet' => $this->stylesheet, 405 'template' => $this->template, 416 'block_theme' => $this->is_block_theme(), 417 'headers' => $this->headers, 418 'errors' => $this->errors, 419 'stylesheet' => $this->stylesheet, 420 'template' => $this->template, 406 421 ) 407 422 ); … … 427 442 'theme', 428 443 array( 429 'headers' => $_child->headers, 430 'errors' => $_child->errors, 431 'stylesheet' => $_child->stylesheet, 432 'template' => $_child->template, 444 'block_theme' => $_child->is_block_theme(), 445 'headers' => $_child->headers, 446 'errors' => $_child->errors, 447 'stylesheet' => $_child->stylesheet, 448 'template' => $_child->template, 433 449 ) 434 450 ); … … 446 462 'theme', 447 463 array( 448 'headers' => $this->headers, 449 'errors' => $this->errors, 450 'stylesheet' => $this->stylesheet, 451 'template' => $this->template, 464 'block_theme' => $this->is_block_theme(), 465 'headers' => $this->headers, 466 'errors' => $this->errors, 467 'stylesheet' => $this->stylesheet, 468 'template' => $this->template, 452 469 ) 453 470 ); … … 466 483 if ( ! is_array( $cache ) ) { 467 484 $cache = array( 468 'headers' => $this->headers, 469 'errors' => $this->errors, 470 'stylesheet' => $this->stylesheet, 471 'template' => $this->template, 485 'block_theme' => $this->is_block_theme(), 486 'headers' => $this->headers, 487 'errors' => $this->errors, 488 'stylesheet' => $this->stylesheet, 489 'template' => $this->template, 472 490 ); 473 491 // If the parent theme is in another root, we'll want to cache this. Avoids an entire branch of filesystem calls above. … … 763 781 $this->headers_sanitized = null; 764 782 $this->name_translated = null; 783 $this->block_theme = null; 765 784 $this->headers = array(); 766 785 $this->__construct( $this->stylesheet, $this->theme_root ); … … 1492 1511 */ 1493 1512 public function is_block_theme() { 1513 if ( isset( $this->block_theme ) ) { 1514 return $this->block_theme; 1515 } 1516 1494 1517 $paths_to_index_block_template = array( 1495 1518 $this->get_file_path( '/block-templates/index.html' ), … … 1497 1520 ); 1498 1521 1522 $this->block_theme = false; 1523 1499 1524 foreach ( $paths_to_index_block_template as $path_to_index_block_template ) { 1500 1525 if ( is_file( $path_to_index_block_template ) && is_readable( $path_to_index_block_template ) ) { 1501 return true; 1502 } 1503 } 1504 1505 return false; 1526 $this->block_theme = true; 1527 break; 1528 } 1529 } 1530 1531 return $this->block_theme; 1506 1532 } 1507 1533 -
trunk/tests/phpunit/tests/theme/wpTheme.php
r53933 r55236 277 277 278 278 /** 279 * @ticket 57114 280 * 281 * @covers WP_Theme::is_block_theme 282 * 283 * @dataProvider data_is_block_theme 284 */ 285 public function test_is_block_theme_property( $theme_dir, $expected ) { 286 $theme = new WP_Theme( $theme_dir, $this->theme_root ); 287 $theme->is_block_theme(); 288 $reflection = new ReflectionClass( $theme ); 289 $reflection_property = $reflection->getProperty( 'block_theme' ); 290 $reflection_property->setAccessible( true ); 291 292 $this->assertSame( $expected, $reflection_property->getValue( $theme ) ); 293 } 294 295 /** 296 * @ticket 57114 297 * 298 * @covers WP_Theme::is_block_theme 299 * @covers WP_Theme::cache_get 300 */ 301 public function test_is_block_theme_check_cache() { 302 $filter = new MockAction(); 303 add_filter( 'theme_file_path', array( $filter, 'filter' ) ); 304 305 $theme1 = new WP_Theme( 'block-theme', $this->theme_root ); 306 // First run. 307 $this->assertTrue( $theme1->is_block_theme(), 'is_block_theme should return true on first run' ); 308 309 $theme2 = new WP_Theme( 'block-theme', $this->theme_root ); 310 // Second run. 311 $this->assertTrue( $theme2->is_block_theme(), 'is_block_theme should return true on second run' ); 312 $this->assertCount( 0, $filter->get_events(), 'Should only be 0, as second run should be cached' ); 313 } 314 315 /** 316 * @ticket 57114 317 * 318 * @covers WP_Theme::is_block_theme 319 * @covers WP_Theme::cache_delete 320 */ 321 public function test_is_block_theme_delete_cache() { 322 $filter = new MockAction(); 323 add_filter( 'theme_file_path', array( $filter, 'filter' ) ); 324 325 $theme = new WP_Theme( 'block-theme', $this->theme_root ); 326 // First run. 327 $this->assertTrue( $theme->is_block_theme(), 'is_block_theme should return true on first run' ); 328 // Clear cache. 329 $theme->cache_delete(); 330 // Second run. 331 $this->assertTrue( $theme->is_block_theme(), 'is_block_theme should return true on second run' ); 332 $this->assertCount( 2, $filter->get_events(), 'Should only be 4, as second run should not be cached' ); 333 } 334 335 /** 279 336 * Test get_files for an existing theme. 280 337 *
Note: See TracChangeset
for help on using the changeset viewer.