Changeset 55955
- Timestamp:
- 06/21/2023 04:21:04 AM (18 months ago)
- Location:
- trunk
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-editor.php
r55438 r55955 360 360 'scripts' => $scripts, 361 361 ); 362 } 363 364 /** 365 * Finds the first occurrence of a specific block in an array of blocks. 366 * 367 * @since 6.3.0 368 * 369 * @param array $blocks Array of blocks. 370 * @param string $block_name Name of the block to find. 371 * @return array Found block, or empty array if none found. 372 */ 373 function wp_get_first_block( $blocks, $block_name ) { 374 foreach ( $blocks as $block ) { 375 if ( $block_name === $block['blockName'] ) { 376 return $block; 377 } 378 if ( ! empty( $block['innerBlocks'] ) ) { 379 $found_block = wp_get_first_block( $block['innerBlocks'], $block_name ); 380 381 if ( ! empty( $found_block ) ) { 382 return $found_block; 383 } 384 } 385 } 386 387 return array(); 388 } 389 390 /** 391 * Retrieves Post Content block attributes from the current post template. 392 * 393 * @since 6.3.0 394 * @access private 395 * 396 * @global int $post_ID 397 * 398 * @return array Post Content block attributes or empty array if they don't exist. 399 */ 400 function wp_get_post_content_block_attributes() { 401 global $post_ID; 402 403 $is_block_theme = wp_is_block_theme(); 404 405 if ( ! $is_block_theme || ! $post_ID ) { 406 return array(); 407 } 408 409 $template_slug = get_page_template_slug( $post_ID ); 410 411 if ( ! $template_slug ) { 412 $post_slug = 'singular'; 413 $page_slug = 'singular'; 414 $template_types = get_block_templates(); 415 416 foreach ( $template_types as $template_type ) { 417 if ( 'page' === $template_type->slug ) { 418 $page_slug = 'page'; 419 } 420 if ( 'single' === $template_type->slug ) { 421 $post_slug = 'single'; 422 } 423 } 424 425 $what_post_type = get_post_type( $post_ID ); 426 switch ( $what_post_type ) { 427 case 'page': 428 $template_slug = $page_slug; 429 break; 430 default: 431 $template_slug = $post_slug; 432 break; 433 } 434 } 435 436 $current_template = get_block_templates( array( 'slug__in' => array( $template_slug ) ) ); 437 438 if ( ! empty( $current_template ) ) { 439 $template_blocks = parse_blocks( $current_template[0]->content ); 440 $post_content_block = wp_get_first_block( $template_blocks, 'core/post-content' ); 441 442 if ( ! empty( $post_content_block['attrs'] ) ) { 443 return $post_content_block['attrs']; 444 } 445 } 446 447 return array(); 362 448 } 363 449 … … 529 615 ), 530 616 ); 617 618 $post_content_block_attributes = wp_get_post_content_block_attributes(); 619 620 if ( ! empty( $post_content_block_attributes ) ) { 621 $editor_settings['postContentAttributes'] = $post_content_block_attributes; 622 } 531 623 532 624 /** -
trunk/tests/phpunit/tests/blocks/editor.php
r55822 r55955 28 28 $wp_rest_server = new Spy_REST_Server(); 29 29 do_action( 'rest_api_init', $wp_rest_server ); 30 31 global $post_ID; 32 $post_ID = 1; 30 33 } 31 34 … … 34 37 global $wp_rest_server; 35 38 $wp_rest_server = null; 39 global $post_ID; 40 $post_ID = null; 36 41 parent::tear_down(); 37 42 } … … 398 403 399 404 /** 405 * @ticket 58534 406 */ 407 public function test_wp_get_first_block() { 408 $block_name = 'core/paragraph'; 409 $blocks = array( 410 array( 411 'blockName' => 'core/image', 412 ), 413 array( 414 'blockName' => $block_name, 415 'attrs' => array( 416 'content' => 'Hello World!', 417 ), 418 ), 419 array( 420 'blockName' => 'core/heading', 421 ), 422 array( 423 'blockName' => $block_name, 424 ), 425 ); 426 $blocks_with_no_paragraph = array( 427 array( 428 'blockName' => 'core/image', 429 ), 430 array( 431 'blockName' => 'core/heading', 432 ), 433 ); 434 435 $this->assertSame( $blocks[1], wp_get_first_block( $blocks, $block_name ) ); 436 437 $this->assertSame( array(), wp_get_first_block( $blocks_with_no_paragraph, $block_name ) ); 438 } 439 440 /** 441 * @ticket 58534 442 */ 443 public function test_wp_get_post_content_block_attributes() { 444 $attributes_with_layout = array( 445 'layout' => array( 446 'type' => 'constrained', 447 ), 448 ); 449 // With no block theme, expect an empty array. 450 $this->assertSame( array(), wp_get_post_content_block_attributes() ); 451 452 switch_theme( 'block-theme' ); 453 454 $this->assertSame( $attributes_with_layout, wp_get_post_content_block_attributes() ); 455 } 456 457 /** 400 458 * @ticket 53458 401 459 */ … … 457 515 // settings.spacing.customPadding 458 516 $this->assertTrue( $settings['enableCustomSpacing'] ); 517 // settings.postContentAttributes 518 $this->assertSameSets( 519 array( 520 'layout' => array( 521 'type' => 'constrained', 522 ), 523 ), 524 $settings['postContentAttributes'] 525 ); 459 526 460 527 switch_theme( WP_DEFAULT_THEME );
Note: See TracChangeset
for help on using the changeset viewer.