diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index 476d89e9846f6..b823c09391c93 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -361,6 +361,92 @@ function _wp_get_iframed_editor_assets() { ); } +/** + * Finds the first occurrence of a specific block in an array of blocks. + * + * @since 6.3.0 + * + * @param array $blocks Array of blocks. + * @param string $block_name Name of the block to find. + * @return array Found block, or empty array if none found. + */ +function wp_get_first_block( $blocks, $block_name ) { + foreach ( $blocks as $block ) { + if ( $block_name === $block['blockName'] ) { + return $block; + } + if ( ! empty( $block['innerBlocks'] ) ) { + $found_block = wp_get_first_block( $block['innerBlocks'], $block_name ); + + if ( ! empty( $found_block ) ) { + return $found_block; + } + } + } + + return array(); +} + +/** + * Retrieves Post Content block attributes from the current post template. + * + * @since 6.3.0 + * @access private + * + * @global int $post_ID + * + * @return array Post Content block attributes or empty array if they don't exist. + */ +function wp_get_post_content_block_attributes() { + global $post_ID; + + $is_block_theme = wp_is_block_theme(); + + if ( ! $is_block_theme || ! $post_ID ) { + return array(); + } + + $template_slug = get_page_template_slug( $post_ID ); + + if ( ! $template_slug ) { + $post_slug = 'singular'; + $page_slug = 'singular'; + $template_types = get_block_templates(); + + foreach ( $template_types as $template_type ) { + if ( 'page' === $template_type->slug ) { + $page_slug = 'page'; + } + if ( 'single' === $template_type->slug ) { + $post_slug = 'single'; + } + } + + $what_post_type = get_post_type( $post_ID ); + switch ( $what_post_type ) { + case 'page': + $template_slug = $page_slug; + break; + default: + $template_slug = $post_slug; + break; + } + } + + $current_template = get_block_templates( array( 'slug__in' => array( $template_slug ) ) ); + + if ( ! empty( $current_template ) ) { + $template_blocks = parse_blocks( $current_template[0]->content ); + $post_content_block = wp_get_first_block( $template_blocks, 'core/post-content' ); + + if ( ! empty( $post_content_block['attrs'] ) ) { + return $post_content_block['attrs']; + } + } + + return array(); +} + /** * Returns the contextualized block editor settings for a selected editor context. * @@ -529,6 +615,12 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex ), ); + $post_content_block_attributes = wp_get_post_content_block_attributes(); + + if ( ! empty( $post_content_block_attributes ) ) { + $editor_settings['postContentAttributes'] = $post_content_block_attributes; + } + /** * Filters the settings to pass to the block editor for all editor type. * diff --git a/tests/phpunit/data/themedir1/block-theme/templates/single.html b/tests/phpunit/data/themedir1/block-theme/templates/single.html new file mode 100644 index 0000000000000..4ad4c7caec0d1 --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme/templates/single.html @@ -0,0 +1,2 @@ + + diff --git a/tests/phpunit/tests/blocks/editor.php b/tests/phpunit/tests/blocks/editor.php index 0769cbc31c411..e01947a34cee1 100644 --- a/tests/phpunit/tests/blocks/editor.php +++ b/tests/phpunit/tests/blocks/editor.php @@ -27,12 +27,17 @@ public function set_up() { global $wp_rest_server; $wp_rest_server = new Spy_REST_Server(); do_action( 'rest_api_init', $wp_rest_server ); + + global $post_ID; + $post_ID = 1; } public function tear_down() { /** @var WP_REST_Server $wp_rest_server */ global $wp_rest_server; $wp_rest_server = null; + global $post_ID; + $post_ID = null; parent::tear_down(); } @@ -396,6 +401,59 @@ function filter_block_editor_settings_my_editor( $editor_settings ) { $this->assertSame( 12345, $settings['maxUploadFileSize'] ); } + /** + * @ticket 58534 + */ + public function test_wp_get_first_block() { + $block_name = 'core/paragraph'; + $blocks = array( + array( + 'blockName' => 'core/image', + ), + array( + 'blockName' => $block_name, + 'attrs' => array( + 'content' => 'Hello World!', + ), + ), + array( + 'blockName' => 'core/heading', + ), + array( + 'blockName' => $block_name, + ), + ); + $blocks_with_no_paragraph = array( + array( + 'blockName' => 'core/image', + ), + array( + 'blockName' => 'core/heading', + ), + ); + + $this->assertSame( $blocks[1], wp_get_first_block( $blocks, $block_name ) ); + + $this->assertSame( array(), wp_get_first_block( $blocks_with_no_paragraph, $block_name ) ); + } + + /** + * @ticket 58534 + */ + public function test_wp_get_post_content_block_attributes() { + $attributes_with_layout = array( + 'layout' => array( + 'type' => 'constrained', + ), + ); + // With no block theme, expect an empty array. + $this->assertSame( array(), wp_get_post_content_block_attributes() ); + + switch_theme( 'block-theme' ); + + $this->assertSame( $attributes_with_layout, wp_get_post_content_block_attributes() ); + } + /** * @ticket 53458 */ @@ -456,6 +514,15 @@ public function test_get_block_editor_settings_theme_json_settings() { $this->assertSameSets( array( 'rem' ), $settings['enableCustomUnits'] ); // settings.spacing.customPadding $this->assertTrue( $settings['enableCustomSpacing'] ); + // settings.postContentAttributes + $this->assertSameSets( + array( + 'layout' => array( + 'type' => 'constrained', + ), + ), + $settings['postContentAttributes'] + ); switch_theme( WP_DEFAULT_THEME ); }