Skip to content
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 for FSE backport in core #36201

Merged
merged 20 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions lib/compat/wordpress-5.9/block-template-utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php
/**
* Temporary compatibility shims for features present in Gutenberg.
* This file should be removed when WordPress 5.9.0 becomes the lowest
* supported version by this plugin.
*
* @package gutenberg
*/

if ( ! function_exists( 'get_allowed_block_template_part_areas' ) ) {
/**
* Returns a filtered list of allowed area values for template parts.
*
* @return array The supported template part area values.
*/
function get_allowed_block_template_part_areas() {
$default_area_definitions = array(
array(
'area' => WP_TEMPLATE_PART_AREA_UNCATEGORIZED,
'label' => __( 'General', 'gutenberg' ),
'description' => __(
'General templates often perform a specific role like displaying post content, and are not tied to any particular area.',
'gutenberg'
),
'icon' => 'layout',
'area_tag' => 'div',
),
array(
'area' => WP_TEMPLATE_PART_AREA_HEADER,
'label' => __( 'Header', 'gutenberg' ),
'description' => __(
'The Header template defines a page area that typically contains a title, logo, and main navigation.',
'gutenberg'
),
'icon' => 'header',
'area_tag' => 'header',
),
array(
'area' => WP_TEMPLATE_PART_AREA_FOOTER,
'label' => __( 'Footer', 'gutenberg' ),
'description' => __(
'The Footer template defines a page area that typically contains site credits, social links, or any other combination of blocks.',
'gutenberg'
),
'icon' => 'footer',
'area_tag' => 'footer',
),
);

/**
* Filters the list of allowed template part area values.
*
* @param array $default_areas An array of supported area objects.
*/
return apply_filters( 'default_wp_template_part_areas', $default_area_definitions );
}
}

if ( ! function_exists( '_flatten_blocks' ) ) {
/**
* Returns an array containing the references of
* the passed blocks and their inner blocks.
*
* @param array $blocks array of blocks.
*
* @return array block references to the passed blocks and their inner blocks.
*/
function _flatten_blocks( &$blocks ) {
$all_blocks = array();
$queue = array();
foreach ( $blocks as &$block ) {
$queue[] = &$block;
}

while ( count( $queue ) > 0 ) {
$block = &$queue[0];
array_shift( $queue );
$all_blocks[] = &$block;

if ( ! empty( $block['innerBlocks'] ) ) {
foreach ( $block['innerBlocks'] as &$inner_block ) {
$queue[] = &$inner_block;
}
}
}

return $all_blocks;
}
}

if ( ! function_exists( '_inject_theme_attribute_in_block_template_content' ) ) {
/**
* Parses wp_template content and injects the current theme's
* stylesheet as a theme attribute into each wp_template_part
*
* @param string $template_content serialized wp_template content.
*
* @return string Updated wp_template content.
*/
function _inject_theme_attribute_in_block_template_content( $template_content ) {
$has_updated_content = false;
$new_content = '';
$template_blocks = parse_blocks( $template_content );

$blocks = _flatten_blocks( $template_blocks );
foreach ( $blocks as &$block ) {
if (
'core/template-part' === $block['blockName'] &&
! isset( $block['attrs']['theme'] )
) {
$block['attrs']['theme'] = wp_get_theme()->get_stylesheet();
$has_updated_content = true;
}
}

if ( $has_updated_content ) {
foreach ( $template_blocks as &$block ) {
$new_content .= serialize_block( $block );
}

return $new_content;
}

return $template_content;
}
}

if ( ! function_exists( 'block_template_part' ) ) {
/**
* Print a template-part.
*
* @param string $part The template-part to print. Use "header" or "footer".
*
* @return void
*/
function block_template_part( $part ) {
$template_part = gutenberg_get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' );
if ( ! $template_part || empty( $template_part->content ) ) {
return;
}
echo do_blocks( $template_part->content );
}
}

if ( ! function_exists( 'block_header_area' ) ) {
/**
* Print the header template-part.
*
* @return void
*/
function block_header_area() {
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
block_template_part( 'header' );
}
}

if ( ! function_exists( 'block_footer_area' ) ) {
/**
* Print the footer template-part.
*
* @return void
*/
function block_footer_area() {
block_template_part( 'footer' );
}
}
66 changes: 0 additions & 66 deletions lib/full-site-editing/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,72 +165,6 @@ function _gutenberg_add_template_part_area_info( $template_info ) {
return $template_info;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should be able to move all the functions from this file into the new block-template-utils.php file


/**
* Returns an array containing the references of
* the passed blocks and their inner blocks.
*
* @param array $blocks array of blocks.
*
* @return array block references to the passed blocks and their inner blocks.
*/
function _gutenberg_flatten_blocks( &$blocks ) {
$all_blocks = array();
$queue = array();
foreach ( $blocks as &$block ) {
$queue[] = &$block;
}

while ( count( $queue ) > 0 ) {
$block = &$queue[0];
array_shift( $queue );
$all_blocks[] = &$block;

if ( ! empty( $block['innerBlocks'] ) ) {
foreach ( $block['innerBlocks'] as &$inner_block ) {
$queue[] = &$inner_block;
}
}
}

return $all_blocks;
}

if ( ! function_exists( '_inject_theme_attribute_in_block_template_content' ) ) {
/**
* Parses wp_template content and injects the current theme's
* stylesheet as a theme attribute into each wp_template_part
*
* @param string $template_content serialized wp_template content.
*
* @return string Updated wp_template content.
*/
function _inject_theme_attribute_in_block_template_content( $template_content ) {
$has_updated_content = false;
$new_content = '';
$template_blocks = parse_blocks( $template_content );

$blocks = _gutenberg_flatten_blocks( $template_blocks );
foreach ( $blocks as &$block ) {
if (
'core/template-part' === $block['blockName'] &&
! isset( $block['attrs']['theme'] )
) {
$block['attrs']['theme'] = wp_get_theme()->get_stylesheet();
$has_updated_content = true;
}
}

if ( $has_updated_content ) {
foreach ( $template_blocks as &$block ) {
$new_content .= serialize_block( $block );
}

return $new_content;
}

return $template_content;
}
}
/**
* Build a unified template object based on a theme file.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/full-site-editing/edit-site-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function _remove_theme_attribute_from_content( $template_content ) {
$new_content = '';
$template_blocks = parse_blocks( $template_content );

$blocks = _gutenberg_flatten_blocks( $template_blocks );
$blocks = _flatten_blocks( $template_blocks );
foreach ( $blocks as $key => $block ) {
if ( 'core/template-part' === $block['blockName'] && isset( $block['attrs']['theme'] ) ) {
unset( $blocks[ $key ]['attrs']['theme'] );
Expand Down
84 changes: 2 additions & 82 deletions lib/full-site-editing/template-parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,56 +160,9 @@ function set_unique_slug_on_create_template_part( $post_id ) {
wp_set_post_terms( $post_id, wp_get_theme()->get_stylesheet(), 'wp_theme' );
}
}
add_action( 'save_post_wp_template_part', 'set_unique_slug_on_create_template_part' );

if ( ! function_exists( 'get_allowed_block_template_part_areas' ) ) {
/**
* Returns a filtered list of allowed area values for template parts.
*
* @return array The supported template part area values.
*/
function get_allowed_block_template_part_areas() {
$default_area_definitions = array(
array(
'area' => WP_TEMPLATE_PART_AREA_UNCATEGORIZED,
'label' => __( 'General', 'gutenberg' ),
'description' => __(
'General templates often perform a specific role like displaying post content, and are not tied to any particular area.',
'gutenberg'
),
'icon' => 'layout',
'area_tag' => 'div',
),
array(
'area' => WP_TEMPLATE_PART_AREA_HEADER,
'label' => __( 'Header', 'gutenberg' ),
'description' => __(
'The Header template defines a page area that typically contains a title, logo, and main navigation.',
'gutenberg'
),
'icon' => 'header',
'area_tag' => 'header',
),
array(
'area' => WP_TEMPLATE_PART_AREA_FOOTER,
'label' => __( 'Footer', 'gutenberg' ),
'description' => __(
'The Footer template defines a page area that typically contains site credits, social links, or any other combination of blocks.',
'gutenberg'
),
'icon' => 'footer',
'area_tag' => 'footer',
),
);

/**
* Filters the list of allowed template part area values.
*
* @param array $default_areas An array of supported area objects.
*/
return apply_filters( 'default_wp_template_part_areas', $default_area_definitions );
}
}
remove_action( 'save_post_wp_template_part', 'wp_set_unique_slug_on_create_template_part' );
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
add_action( 'save_post_wp_template_part', 'set_unique_slug_on_create_template_part' );

/**
* Checks whether the input 'area' is a supported value.
Expand All @@ -235,36 +188,3 @@ function ( $item ) {
trigger_error( $warning_message, E_USER_NOTICE );
return WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
}

/**
* Print a template-part.
*
* @param string $part The template-part to print. Use "header" or "footer".
*
* @return void
*/
function gutenberg_block_template_part( $part ) {
$template_part = gutenberg_get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' );
if ( ! $template_part || empty( $template_part->content ) ) {
return;
}
echo do_blocks( $template_part->content );
}

/**
* Print the header template-part.
*
* @return void
*/
function gutenberg_block_header_area() {
gutenberg_block_template_part( 'header' );
}

/**
* Print the footer template-part.
*
* @return void
*/
function gutenberg_block_footer_area() {
gutenberg_block_template_part( 'footer' );
}
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-5.9/default-theme-supports.php';
require __DIR__ . '/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php';
require __DIR__ . '/compat/wordpress-5.9/rest-active-global-styles.php';
require __DIR__ . '/compat/wordpress-5.9/block-template-utils.php';

require __DIR__ . '/blocks.php';
require __DIR__ . '/block-patterns.php';
Expand Down
8 changes: 4 additions & 4 deletions phpunit/class-block-templates-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,22 +287,22 @@ function( $template ) {
/**
* Should flatten nested blocks
*/
function test_gutenberg_flatten_blocks() {
function test_flatten_blocks() {
$content_template_part_inside_group = '<!-- wp:group --><!-- wp:template-part {"slug":"header"} /--><!-- /wp:group -->';
$blocks = parse_blocks( $content_template_part_inside_group );
$actual = _gutenberg_flatten_blocks( $blocks );
$actual = _flatten_blocks( $blocks );
$expected = array( $blocks[0], $blocks[0]['innerBlocks'][0] );
$this->assertEquals( $expected, $actual );

$content_template_part_inside_group_inside_group = '<!-- wp:group --><!-- wp:group --><!-- wp:template-part {"slug":"header"} /--><!-- /wp:group --><!-- /wp:group -->';
$blocks = parse_blocks( $content_template_part_inside_group_inside_group );
$actual = _gutenberg_flatten_blocks( $blocks );
$actual = _flatten_blocks( $blocks );
$expected = array( $blocks[0], $blocks[0]['innerBlocks'][0], $blocks[0]['innerBlocks'][0]['innerBlocks'][0] );
$this->assertEquals( $expected, $actual );

$content_without_inner_blocks = '<!-- wp:group /-->';
$blocks = parse_blocks( $content_without_inner_blocks );
$actual = _gutenberg_flatten_blocks( $blocks );
$actual = _flatten_blocks( $blocks );
$expected = array( $blocks[0] );
$this->assertEquals( $expected, $actual );
}
Expand Down