-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add Post Content attributes as a block editor setting #4614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b993c63
33a6cc9
4b484b3
4a4ea46
405e499
df0003b
e3fdf7d
1dbdbb6
b7464cb
1bbc1ab
fbd0981
a4f048a
727f135
42a4d15
e206857
67ae702
fd82019
6a5e931
07e84c9
c104d90
0bcf57a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing docblock for global.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit-pick: It would be good to follow the pattern of putting the |
||
|
|
||
| $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 ) ) { | ||
jeremyfelt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $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. | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <!-- wp:post-title {"level":1,"style":{"spacing":{"margin":{"bottom":"var:preset|spacing|40"}}}} /--> | ||
| <!-- wp:post-content {"layout":{"type":"constrained"}} /--> |
Uh oh!
There was an error while loading. Please reload this page.