From 157af1e73f9902e3732caa83d9c713e1bbc2a8d1 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 11:23:37 +0200 Subject: [PATCH 01/20] Changes for FSE backport in core --- lib/compat/wordpress-5.9/index.php | 127 +++++++++++++++++++++ lib/full-site-editing/block-templates.php | 66 ----------- lib/full-site-editing/edit-site-export.php | 2 +- lib/full-site-editing/template-parts.php | 51 +-------- lib/load.php | 1 + phpunit/class-block-templates-test.php | 8 +- 6 files changed, 135 insertions(+), 120 deletions(-) create mode 100644 lib/compat/wordpress-5.9/index.php diff --git a/lib/compat/wordpress-5.9/index.php b/lib/compat/wordpress-5.9/index.php new file mode 100644 index 0000000000000..a5bf1a5b56763 --- /dev/null +++ b/lib/compat/wordpress-5.9/index.php @@ -0,0 +1,127 @@ + 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( '_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( '_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; + } +} diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index cc1621f671397..a0721432e3785 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -165,72 +165,6 @@ function _gutenberg_add_template_part_area_info( $template_info ) { return $template_info; } -/** - * 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. * diff --git a/lib/full-site-editing/edit-site-export.php b/lib/full-site-editing/edit-site-export.php index 93ca0127a41fe..6a29c23543495 100644 --- a/lib/full-site-editing/edit-site-export.php +++ b/lib/full-site-editing/edit-site-export.php @@ -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'] ); diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index f7b3e075088a0..f42f5ed93699d 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -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' ); +add_action( 'save_post_wp_template_part', 'set_unique_slug_on_create_template_part' ); /** * Checks whether the input 'area' is a supported value. diff --git a/lib/load.php b/lib/load.php index 3d07c8832a7ed..73e9cd1416d76 100644 --- a/lib/load.php +++ b/lib/load.php @@ -83,6 +83,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat.php'; require __DIR__ . '/compat/wordpress-5.8/index.php'; require __DIR__ . '/compat/wordpress-5.8.1/index.php'; +require __DIR__ . '/compat/wordpress-5.9/index.php'; require __DIR__ . '/compat/wordpress-5.9/default-editor-styles.php'; require __DIR__ . '/compat/wordpress-5.9/get-global-styles-and-settings.php'; require __DIR__ . '/utils.php'; diff --git a/phpunit/class-block-templates-test.php b/phpunit/class-block-templates-test.php index 280f18c20d134..feccac992a8a6 100644 --- a/phpunit/class-block-templates-test.php +++ b/phpunit/class-block-templates-test.php @@ -287,22 +287,22 @@ function( $template ) { /** * Should flatten nested blocks */ - function test_gutenberg_flatten_blocks() { + function test_flatten_blocks() { $content_template_part_inside_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 = ''; $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 = ''; $blocks = parse_blocks( $content_without_inner_blocks ); - $actual = _gutenberg_flatten_blocks( $blocks ); + $actual = _flatten_blocks( $blocks ); $expected = array( $blocks[0] ); $this->assertEquals( $expected, $actual ); } From 55d6173d0de40791f745c74a8a25313c9fdd609b Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 11:37:32 +0200 Subject: [PATCH 02/20] split files --- .../{index.php => block-templates.php} | 100 +++++------------- lib/compat/wordpress-5.9/template-parts.php | 57 ++++++++++ lib/load.php | 3 +- 3 files changed, 84 insertions(+), 76 deletions(-) rename lib/compat/wordpress-5.9/{index.php => block-templates.php} (55%) create mode 100644 lib/compat/wordpress-5.9/template-parts.php diff --git a/lib/compat/wordpress-5.9/index.php b/lib/compat/wordpress-5.9/block-templates.php similarity index 55% rename from lib/compat/wordpress-5.9/index.php rename to lib/compat/wordpress-5.9/block-templates.php index a5bf1a5b56763..35dd632ea67f3 100644 --- a/lib/compat/wordpress-5.9/index.php +++ b/lib/compat/wordpress-5.9/block-templates.php @@ -7,52 +7,35 @@ * @package gutenberg */ -if ( ! function_exists( 'get_allowed_block_template_part_areas' ) ) { +if ( ! function_exists( '_flatten_blocks' ) ) { /** - * Returns a filtered list of allowed area values for template parts. + * Returns an array containing the references of + * the passed blocks and their inner blocks. + * + * @param array $blocks array of blocks. * - * @return array The supported template part area values. + * @return array block references to the passed blocks and their inner blocks. */ - 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', - ), - ); + function _flatten_blocks( &$blocks ) { + $all_blocks = array(); + $queue = array(); + foreach ( $blocks as &$block ) { + $queue[] = &$block; + } - /** - * 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 ); + 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; } } @@ -92,36 +75,3 @@ function _inject_theme_attribute_in_block_template_content( $template_content ) return $template_content; } } - - -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; - } -} diff --git a/lib/compat/wordpress-5.9/template-parts.php b/lib/compat/wordpress-5.9/template-parts.php new file mode 100644 index 0000000000000..7679d802b2b3c --- /dev/null +++ b/lib/compat/wordpress-5.9/template-parts.php @@ -0,0 +1,57 @@ + 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 ); + } +} diff --git a/lib/load.php b/lib/load.php index 73e9cd1416d76..c1a5fde360da6 100644 --- a/lib/load.php +++ b/lib/load.php @@ -83,7 +83,6 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat.php'; require __DIR__ . '/compat/wordpress-5.8/index.php'; require __DIR__ . '/compat/wordpress-5.8.1/index.php'; -require __DIR__ . '/compat/wordpress-5.9/index.php'; require __DIR__ . '/compat/wordpress-5.9/default-editor-styles.php'; require __DIR__ . '/compat/wordpress-5.9/get-global-styles-and-settings.php'; require __DIR__ . '/utils.php'; @@ -115,6 +114,8 @@ 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-templates.php'; +require __DIR__ . '/compat/wordpress-5.9/template-parts.php'; require __DIR__ . '/blocks.php'; require __DIR__ . '/block-patterns.php'; From f7bda3816d31723a8fe3b91e638c85e9c6a8deca Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 11:46:08 +0200 Subject: [PATCH 03/20] rename files --- ...templates.php => block-template-utils.php} | 49 ++++++++++++++++ lib/compat/wordpress-5.9/template-parts.php | 57 ------------------- lib/load.php | 3 +- 3 files changed, 50 insertions(+), 59 deletions(-) rename lib/compat/wordpress-5.9/{block-templates.php => block-template-utils.php} (55%) delete mode 100644 lib/compat/wordpress-5.9/template-parts.php diff --git a/lib/compat/wordpress-5.9/block-templates.php b/lib/compat/wordpress-5.9/block-template-utils.php similarity index 55% rename from lib/compat/wordpress-5.9/block-templates.php rename to lib/compat/wordpress-5.9/block-template-utils.php index 35dd632ea67f3..1edae132a626c 100644 --- a/lib/compat/wordpress-5.9/block-templates.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -7,6 +7,55 @@ * @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 diff --git a/lib/compat/wordpress-5.9/template-parts.php b/lib/compat/wordpress-5.9/template-parts.php deleted file mode 100644 index 7679d802b2b3c..0000000000000 --- a/lib/compat/wordpress-5.9/template-parts.php +++ /dev/null @@ -1,57 +0,0 @@ - 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 ); - } -} diff --git a/lib/load.php b/lib/load.php index c1a5fde360da6..374862e764939 100644 --- a/lib/load.php +++ b/lib/load.php @@ -114,8 +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-templates.php'; -require __DIR__ . '/compat/wordpress-5.9/template-parts.php'; +require __DIR__ . '/compat/wordpress-5.9/block-template-utils.php'; require __DIR__ . '/blocks.php'; require __DIR__ . '/block-patterns.php'; From 11e116b66717cf921289a5eb93123af09f8485b8 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 12:00:46 +0200 Subject: [PATCH 04/20] more changes --- .../wordpress-5.9/block-template-utils.php | 39 +++++++++++++++++++ lib/full-site-editing/template-parts.php | 33 ---------------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index 1edae132a626c..bd60a219a3b5f 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -124,3 +124,42 @@ function _inject_theme_attribute_in_block_template_content( $template_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() { + 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' ); + } +} diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index f42f5ed93699d..4037231e2d672 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -188,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' ); -} From 882f0216d2023bae600d0bbc83451d69de03154b Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 12:12:00 +0200 Subject: [PATCH 05/20] rename gutenberg_get_block_template --- .../wordpress-5.9/block-template-utils.php | 74 ++++++++++++++++++- lib/full-site-editing/block-templates.php | 70 ------------------ ...ss-gutenberg-rest-templates-controller.php | 16 ++-- phpunit/class-block-templates-test.php | 8 +- 4 files changed, 85 insertions(+), 83 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index bd60a219a3b5f..e4a6e30c827c7 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -125,6 +125,78 @@ function _inject_theme_attribute_in_block_template_content( $template_content ) } } +if ( ! function_exists( 'get_block_template' ) ) { + /** + * Retrieves a single unified template object using its id. + * + * @param string $id Template unique identifier (example: theme_slug//template_slug). + * @param array $template_type wp_template or wp_template_part. + * + * @return WP_Block_Template|null Template. + */ + function get_block_template( $id, $template_type = 'wp_template' ) { + /** + * Filters the block templates array before the query takes place. + * + * Return a non-null value to bypass the WordPress queries. + * + * @since 10.8 + * + * @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query, + * or null to allow WP to run it's normal queries. + * @param string $id Template unique identifier (example: theme_slug//template_slug). + * @param array $template_type wp_template or wp_template_part. + */ + $block_template = apply_filters( 'pre_get_block_template', null, $id, $template_type ); + if ( ! is_null( $block_template ) ) { + return $block_template; + } + + $parts = explode( '//', $id, 2 ); + if ( count( $parts ) < 2 ) { + return null; + } + list( $theme, $slug ) = $parts; + $wp_query_args = array( + 'post_name__in' => array( $slug ), + 'post_type' => $template_type, + 'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ), + 'posts_per_page' => 1, + 'no_found_rows' => true, + 'tax_query' => array( + array( + 'taxonomy' => 'wp_theme', + 'field' => 'name', + 'terms' => $theme, + ), + ), + ); + $template_query = new WP_Query( $wp_query_args ); + $posts = $template_query->posts; + + if ( count( $posts ) > 0 ) { + $template = _gutenberg_build_template_result_from_post( $posts[0] ); + + if ( ! is_wp_error( $template ) ) { + return $template; + } + } + + $block_template = gutenberg_get_block_file_template( $id, $template_type ); + + /** + * Filters the array of queried block templates array after they've been fetched. + * + * @since 10.8 + * + * @param WP_Block_Template $block_template The found block template. + * @param string $id Template unique identifier (example: theme_slug//template_slug). + * @param array $template_type wp_template or wp_template_part. + */ + return apply_filters( 'get_block_template', $block_template, $id, $template_type ); + } +} + if ( ! function_exists( 'block_template_part' ) ) { /** * Print a template-part. @@ -134,7 +206,7 @@ function _inject_theme_attribute_in_block_template_content( $template_content ) * @return void */ function block_template_part( $part ) { - $template_part = gutenberg_get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' ); + $template_part = get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' ); if ( ! $template_part || empty( $template_part->content ) ) { return; } diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index a0721432e3785..498cdd868bb43 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -397,76 +397,6 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t return apply_filters( 'get_block_templates', $query_result, $query, $template_type ); } -/** - * Retrieves a single unified template object using its id. - * - * @param string $id Template unique identifier (example: theme_slug//template_slug). - * @param array $template_type wp_template or wp_template_part. - * - * @return WP_Block_Template|null Template. - */ -function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) { - /** - * Filters the block templates array before the query takes place. - * - * Return a non-null value to bypass the WordPress queries. - * - * @since 10.8 - * - * @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query, - * or null to allow WP to run it's normal queries. - * @param string $id Template unique identifier (example: theme_slug//template_slug). - * @param array $template_type wp_template or wp_template_part. - */ - $block_template = apply_filters( 'pre_get_block_template', null, $id, $template_type ); - if ( ! is_null( $block_template ) ) { - return $block_template; - } - - $parts = explode( '//', $id, 2 ); - if ( count( $parts ) < 2 ) { - return null; - } - list( $theme, $slug ) = $parts; - $wp_query_args = array( - 'post_name__in' => array( $slug ), - 'post_type' => $template_type, - 'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ), - 'posts_per_page' => 1, - 'no_found_rows' => true, - 'tax_query' => array( - array( - 'taxonomy' => 'wp_theme', - 'field' => 'name', - 'terms' => $theme, - ), - ), - ); - $template_query = new WP_Query( $wp_query_args ); - $posts = $template_query->posts; - - if ( count( $posts ) > 0 ) { - $template = _gutenberg_build_template_result_from_post( $posts[0] ); - - if ( ! is_wp_error( $template ) ) { - return $template; - } - } - - $block_template = gutenberg_get_block_file_template( $id, $template_type ); - - /** - * Filters the array of queried block templates array after they've been fetched. - * - * @since 10.8 - * - * @param WP_Block_Template $block_template The found block template. - * @param string $id Template unique identifier (example: theme_slug//template_slug). - * @param array $template_type wp_template or wp_template_part. - */ - return apply_filters( 'get_block_template', $block_template, $id, $template_type ); -} - /** * Retrieves a single unified template object using its id. * Retrieves the file template. diff --git a/lib/full-site-editing/class-gutenberg-rest-templates-controller.php b/lib/full-site-editing/class-gutenberg-rest-templates-controller.php index dafb15509bd01..ec616368e9882 100644 --- a/lib/full-site-editing/class-gutenberg-rest-templates-controller.php +++ b/lib/full-site-editing/class-gutenberg-rest-templates-controller.php @@ -175,7 +175,7 @@ public function get_item( $request ) { if ( isset( $request['source'] ) && 'theme' === $request['source'] ) { $template = gutenberg_get_block_file_template( $request['id'], $this->post_type ); } else { - $template = gutenberg_get_block_template( $request['id'], $this->post_type ); + $template = get_block_template( $request['id'], $this->post_type ); } if ( ! $template ) { @@ -202,7 +202,7 @@ public function update_item_permissions_check( $request ) { * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function update_item( $request ) { - $template = gutenberg_get_block_template( $request['id'], $this->post_type ); + $template = get_block_template( $request['id'], $this->post_type ); if ( ! $template ) { return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.', 'gutenberg' ), array( 'status' => 404 ) ); } @@ -223,14 +223,14 @@ public function update_item( $request ) { return $result; } - $template = gutenberg_get_block_template( $request['id'], $this->post_type ); + $template = get_block_template( $request['id'], $this->post_type ); $fields_update = $this->update_additional_fields_for_object( $template, $request ); if ( is_wp_error( $fields_update ) ) { return $fields_update; } return $this->prepare_item_for_response( - gutenberg_get_block_template( $request['id'], $this->post_type ), + get_block_template( $request['id'], $this->post_type ), $request ); } @@ -263,14 +263,14 @@ public function create_item( $request ) { return new WP_Error( 'rest_template_insert_error', __( 'No templates exist with that id.', 'gutenberg' ) ); } $id = $posts[0]->id; - $template = gutenberg_get_block_template( $id, $this->post_type ); + $template = get_block_template( $id, $this->post_type ); $fields_update = $this->update_additional_fields_for_object( $template, $request ); if ( is_wp_error( $fields_update ) ) { return $fields_update; } return $this->prepare_item_for_response( - gutenberg_get_block_template( $id, $this->post_type ), + get_block_template( $id, $this->post_type ), $request ); } @@ -292,7 +292,7 @@ public function delete_item_permissions_check( $request ) { * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function delete_item( $request ) { - $template = gutenberg_get_block_template( $request['id'], $this->post_type ); + $template = get_block_template( $request['id'], $this->post_type ); if ( ! $template ) { return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.', 'gutenberg' ), array( 'status' => 404 ) ); } @@ -339,7 +339,7 @@ public function delete_item( $request ) { * @return stdClass Changes to pass to wp_update_post. */ protected function prepare_item_for_database( $request ) { - $template = $request['id'] ? gutenberg_get_block_template( $request['id'], $this->post_type ) : null; + $template = $request['id'] ? get_block_template( $request['id'], $this->post_type ) : null; $changes = new stdClass(); if ( null === $template ) { $changes->post_type = $this->post_type; diff --git a/phpunit/class-block-templates-test.php b/phpunit/class-block-templates-test.php index feccac992a8a6..6070d8d2a1175 100644 --- a/phpunit/class-block-templates-test.php +++ b/phpunit/class-block-templates-test.php @@ -196,7 +196,7 @@ function test_inject_theme_attribute_in_block_template_content() { */ function test_gutenberg_get_block_template_from_file() { $id = get_stylesheet() . '//' . 'index'; - $template = gutenberg_get_block_template( $id, 'wp_template' ); + $template = get_block_template( $id, 'wp_template' ); $this->assertEquals( $id, $template->id ); $this->assertEquals( get_stylesheet(), $template->theme ); $this->assertEquals( 'index', $template->slug ); @@ -206,7 +206,7 @@ function test_gutenberg_get_block_template_from_file() { // Test template parts. $id = get_stylesheet() . '//' . 'header'; - $template = gutenberg_get_block_template( $id, 'wp_template_part' ); + $template = get_block_template( $id, 'wp_template_part' ); $this->assertEquals( $id, $template->id ); $this->assertEquals( get_stylesheet(), $template->theme ); $this->assertEquals( 'header', $template->slug ); @@ -221,7 +221,7 @@ function test_gutenberg_get_block_template_from_file() { */ function test_gutenberg_get_block_template_from_post() { $id = get_stylesheet() . '//' . 'my_template'; - $template = gutenberg_get_block_template( $id, 'wp_template' ); + $template = get_block_template( $id, 'wp_template' ); $this->assertEquals( $id, $template->id ); $this->assertEquals( get_stylesheet(), $template->theme ); $this->assertEquals( 'my_template', $template->slug ); @@ -231,7 +231,7 @@ function test_gutenberg_get_block_template_from_post() { // Test template parts. $id = get_stylesheet() . '//' . 'my_template_part'; - $template = gutenberg_get_block_template( $id, 'wp_template_part' ); + $template = get_block_template( $id, 'wp_template_part' ); $this->assertEquals( $id, $template->id ); $this->assertEquals( get_stylesheet(), $template->theme ); $this->assertEquals( 'my_template_part', $template->slug ); From a6e8def421bebd02d0c0e29f8bf146bbae8aa83c Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 12:17:38 +0200 Subject: [PATCH 06/20] rename _gutenberg_build_template_result_from_post --- .../wordpress-5.9/block-template-utils.php | 55 ++++++++++++++++++- lib/full-site-editing/block-templates.php | 53 +----------------- phpunit/class-block-templates-test.php | 6 +- 3 files changed, 58 insertions(+), 56 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index e4a6e30c827c7..82702239d9eb0 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -125,6 +125,59 @@ function _inject_theme_attribute_in_block_template_content( $template_content ) } } +if ( ! function_exists( '_build_block_template_result_from_post' ) ) { + /** + * Build a unified template object based a post Object. + * + * @param WP_Post $post Template post. + * + * @return WP_Block_Template|WP_Error Template. + */ + function _build_block_template_result_from_post( $post ) { + $default_template_types = gutenberg_get_default_template_types(); + $terms = get_the_terms( $post, 'wp_theme' ); + + if ( is_wp_error( $terms ) ) { + return $terms; + } + + if ( ! $terms ) { + return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.', 'gutenberg' ) ); + } + + $theme = $terms[0]->name; + $has_theme_file = wp_get_theme()->get_stylesheet() === $theme && + null !== _gutenberg_get_template_file( $post->post_type, $post->post_name ); + + $template = new WP_Block_Template(); + $template->wp_id = $post->ID; + $template->id = $theme . '//' . $post->post_name; + $template->theme = $theme; + $template->content = $post->post_content; + $template->slug = $post->post_name; + $template->source = 'custom'; + $template->type = $post->post_type; + $template->description = $post->post_excerpt; + $template->title = $post->post_title; + $template->status = $post->post_status; + $template->has_theme_file = $has_theme_file; + $template->is_custom = true; + + if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) { + $template->is_custom = false; + } + + if ( 'wp_template_part' === $post->post_type ) { + $type_terms = get_the_terms( $post, 'wp_template_part_area' ); + if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { + $template->area = $type_terms[0]->name; + } + } + + return $template; + } +} + if ( ! function_exists( 'get_block_template' ) ) { /** * Retrieves a single unified template object using its id. @@ -175,7 +228,7 @@ function get_block_template( $id, $template_type = 'wp_template' ) { $posts = $template_query->posts; if ( count( $posts ) > 0 ) { - $template = _gutenberg_build_template_result_from_post( $posts[0] ); + $template = _build_block_template_result_from_post( $posts[0] ); if ( ! is_wp_error( $template ) ) { return $template; diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 498cdd868bb43..af56f5592a2e1 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -207,57 +207,6 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t return $template; } -/** - * Build a unified template object based a post Object. - * - * @param WP_Post $post Template post. - * - * @return WP_Block_Template|WP_Error Template. - */ -function _gutenberg_build_template_result_from_post( $post ) { - $default_template_types = gutenberg_get_default_template_types(); - $terms = get_the_terms( $post, 'wp_theme' ); - - if ( is_wp_error( $terms ) ) { - return $terms; - } - - if ( ! $terms ) { - return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.', 'gutenberg' ) ); - } - - $theme = $terms[0]->name; - $has_theme_file = wp_get_theme()->get_stylesheet() === $theme && - null !== _gutenberg_get_template_file( $post->post_type, $post->post_name ); - - $template = new WP_Block_Template(); - $template->wp_id = $post->ID; - $template->id = $theme . '//' . $post->post_name; - $template->theme = $theme; - $template->content = $post->post_content; - $template->slug = $post->post_name; - $template->source = 'custom'; - $template->type = $post->post_type; - $template->description = $post->post_excerpt; - $template->title = $post->post_title; - $template->status = $post->post_status; - $template->has_theme_file = $has_theme_file; - $template->is_custom = true; - - if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) { - $template->is_custom = false; - } - - if ( 'wp_template_part' === $post->post_type ) { - $type_terms = get_the_terms( $post, 'wp_template_part_area' ); - if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { - $template->area = $type_terms[0]->name; - } - } - - return $template; -} - /** * Retrieves a list of unified template objects based on a query. * @@ -335,7 +284,7 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t $template_query = new WP_Query( $wp_query_args ); $query_result = array(); foreach ( $template_query->posts as $post ) { - $template = _gutenberg_build_template_result_from_post( $post ); + $template = _build_block_template_result_from_post( $post ); if ( is_wp_error( $template ) ) { continue; diff --git a/phpunit/class-block-templates-test.php b/phpunit/class-block-templates-test.php index 6070d8d2a1175..45e2ea06144d4 100644 --- a/phpunit/class-block-templates-test.php +++ b/phpunit/class-block-templates-test.php @@ -117,8 +117,8 @@ function test_gutenberg_build_template_result_from_file() { $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area ); } - function test_gutenberg_build_template_result_from_post() { - $template = _gutenberg_build_template_result_from_post( + function test_build_block_template_result_from_post() { + $template = _build_block_template_result_from_post( self::$post, 'wp_template' ); @@ -134,7 +134,7 @@ function test_gutenberg_build_template_result_from_post() { $this->assertEquals( 'wp_template', $template->type ); // Test template parts. - $template_part = _gutenberg_build_template_result_from_post( + $template_part = _build_block_template_result_from_post( self::$template_part_post, 'wp_template_part' ); From 939489d2ceff93e6c36fff72644d08737c1a62b0 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 12:23:15 +0200 Subject: [PATCH 07/20] rename gutenberg_get_default_template_types --- .../wordpress-5.9/block-template-utils.php | 88 ++++++++++++++++++- lib/full-site-editing/block-templates.php | 2 +- .../default-template-types.php | 88 +------------------ 3 files changed, 90 insertions(+), 88 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index 82702239d9eb0..cda58e1339b6d 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -125,6 +125,92 @@ function _inject_theme_attribute_in_block_template_content( $template_content ) } } +if ( ! function_exists( 'get_default_block_template_types' ) ) { + /** + * Returns a filtered list of default template types, containing their + * localized titles and descriptions. + * + * @return array The default template types. + */ + function get_default_block_template_types() { + $default_template_types = array( + 'index' => array( + 'title' => _x( 'Index', 'Template name', 'gutenberg' ), + 'description' => __( 'The default template used when no other template is available. This is a required template in WordPress.', 'gutenberg' ), + ), + 'home' => array( + 'title' => _x( 'Home', 'Template name', 'gutenberg' ), + 'description' => __( 'Template used for the main page that displays blog posts. This is the front page by default in WordPress. If a static front page is set, this is the template used for the page that contains the latest blog posts.', 'gutenberg' ), + ), + 'front-page' => array( + 'title' => _x( 'Front Page', 'Template name', 'gutenberg' ), + 'description' => __( 'Template used to render the front page of the site, whether it displays blog posts or a static page. The front page template takes precedence over the "Home" template.', 'gutenberg' ), + ), + 'singular' => array( + 'title' => _x( 'Singular', 'Template name', 'gutenberg' ), + 'description' => __( 'Template used for displaying single views of the content. This template is a fallback for the Single, Post, and Page templates, which take precedence when they exist.', 'gutenberg' ), + ), + 'single' => array( + 'title' => _x( 'Single Post', 'Template name', 'gutenberg' ), + 'description' => __( 'Template used to display a single blog post.', 'gutenberg' ), + ), + 'page' => array( + 'title' => _x( 'Page', 'Template name', 'gutenberg' ), + 'description' => __( 'Template used to display individual pages.', 'gutenberg' ), + ), + 'archive' => array( + 'title' => _x( 'Archive', 'Template name', 'gutenberg' ), + 'description' => __( 'The archive template displays multiple entries at once. It is used as a fallback for the Category, Author, and Date templates, which take precedence when they are available.', 'gutenberg' ), + ), + 'author' => array( + 'title' => _x( 'Author', 'Template name', 'gutenberg' ), + 'description' => __( 'Archive template used to display a list of posts from a single author.', 'gutenberg' ), + ), + 'category' => array( + 'title' => _x( 'Category', 'Template name', 'gutenberg' ), + 'description' => __( 'Archive template used to display a list of posts from the same category.', 'gutenberg' ), + ), + 'taxonomy' => array( + 'title' => _x( 'Taxonomy', 'Template name', 'gutenberg' ), + 'description' => __( 'Archive template used to display a list of posts from the same taxonomy.', 'gutenberg' ), + ), + 'date' => array( + 'title' => _x( 'Date', 'Template name', 'gutenberg' ), + 'description' => __( 'Archive template used to display a list of posts from a specific date.', 'gutenberg' ), + ), + 'tag' => array( + 'title' => _x( 'Tag', 'Template name', 'gutenberg' ), + 'description' => __( 'Archive template used to display a list of posts with a given tag.', 'gutenberg' ), + ), + 'attachment' => array( + 'title' => __( 'Media', 'gutenberg' ), + 'description' => __( 'Template used to display individual media items or attachments.', 'gutenberg' ), + ), + 'search' => array( + 'title' => _x( 'Search', 'Template name', 'gutenberg' ), + 'description' => __( 'Template used to display search results.', 'gutenberg' ), + ), + 'privacy-policy' => array( + 'title' => __( 'Privacy Policy', 'gutenberg' ), + 'description' => '', + ), + '404' => array( + 'title' => _x( '404', 'Template name', 'gutenberg' ), + 'description' => __( 'Template shown when no content is found.', 'gutenberg' ), + ), + ); + + /** + * Filters the list of template types. + * + * @param array $default_template_types An array of template types, formatted as [ slug => [ title, description ] ]. + * + * @since 5.x.x + */ + return apply_filters( 'default_template_types', $default_template_types ); + } +} + if ( ! function_exists( '_build_block_template_result_from_post' ) ) { /** * Build a unified template object based a post Object. @@ -134,7 +220,7 @@ function _inject_theme_attribute_in_block_template_content( $template_content ) * @return WP_Block_Template|WP_Error Template. */ function _build_block_template_result_from_post( $post ) { - $default_template_types = gutenberg_get_default_template_types(); + $default_template_types = get_default_block_template_types(); $terms = get_the_terms( $post, 'wp_theme' ); if ( is_wp_error( $terms ) ) { diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index af56f5592a2e1..080f33a5c0980 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -174,7 +174,7 @@ function _gutenberg_add_template_part_area_info( $template_info ) { * @return WP_Block_Template Template. */ function _gutenberg_build_template_result_from_file( $template_file, $template_type ) { - $default_template_types = gutenberg_get_default_template_types(); + $default_template_types = get_default_block_template_types(); $template_content = file_get_contents( $template_file['path'] ); $theme = wp_get_theme()->get_stylesheet(); diff --git a/lib/full-site-editing/default-template-types.php b/lib/full-site-editing/default-template-types.php index 1a9c2b023dbe8..daaf3c5fb9e5b 100644 --- a/lib/full-site-editing/default-template-types.php +++ b/lib/full-site-editing/default-template-types.php @@ -5,90 +5,6 @@ * @package gutenberg */ -/** - * Returns a filtered list of default template types, containing their - * localized titles and descriptions. - * - * @return array The default template types. - */ -function gutenberg_get_default_template_types() { - $default_template_types = array( - 'index' => array( - 'title' => _x( 'Index', 'Template name', 'gutenberg' ), - 'description' => __( 'The default template used when no other template is available. This is a required template in WordPress.', 'gutenberg' ), - ), - 'home' => array( - 'title' => _x( 'Home', 'Template name', 'gutenberg' ), - 'description' => __( 'Template used for the main page that displays blog posts. This is the front page by default in WordPress. If a static front page is set, this is the template used for the page that contains the latest blog posts.', 'gutenberg' ), - ), - 'front-page' => array( - 'title' => _x( 'Front Page', 'Template name', 'gutenberg' ), - 'description' => __( 'Template used to render the front page of the site, whether it displays blog posts or a static page. The front page template takes precedence over the "Home" template.', 'gutenberg' ), - ), - 'singular' => array( - 'title' => _x( 'Singular', 'Template name', 'gutenberg' ), - 'description' => __( 'Template used for displaying single views of the content. This template is a fallback for the Single, Post, and Page templates, which take precedence when they exist.', 'gutenberg' ), - ), - 'single' => array( - 'title' => _x( 'Single Post', 'Template name', 'gutenberg' ), - 'description' => __( 'Template used to display a single blog post.', 'gutenberg' ), - ), - 'page' => array( - 'title' => _x( 'Page', 'Template name', 'gutenberg' ), - 'description' => __( 'Template used to display individual pages.', 'gutenberg' ), - ), - 'archive' => array( - 'title' => _x( 'Archive', 'Template name', 'gutenberg' ), - 'description' => __( 'The archive template displays multiple entries at once. It is used as a fallback for the Category, Author, and Date templates, which take precedence when they are available.', 'gutenberg' ), - ), - 'author' => array( - 'title' => _x( 'Author', 'Template name', 'gutenberg' ), - 'description' => __( 'Archive template used to display a list of posts from a single author.', 'gutenberg' ), - ), - 'category' => array( - 'title' => _x( 'Category', 'Template name', 'gutenberg' ), - 'description' => __( 'Archive template used to display a list of posts from the same category.', 'gutenberg' ), - ), - 'taxonomy' => array( - 'title' => _x( 'Taxonomy', 'Template name', 'gutenberg' ), - 'description' => __( 'Archive template used to display a list of posts from the same taxonomy.', 'gutenberg' ), - ), - 'date' => array( - 'title' => _x( 'Date', 'Template name', 'gutenberg' ), - 'description' => __( 'Archive template used to display a list of posts from a specific date.', 'gutenberg' ), - ), - 'tag' => array( - 'title' => _x( 'Tag', 'Template name', 'gutenberg' ), - 'description' => __( 'Archive template used to display a list of posts with a given tag.', 'gutenberg' ), - ), - 'attachment' => array( - 'title' => __( 'Media', 'gutenberg' ), - 'description' => __( 'Template used to display individual media items or attachments.', 'gutenberg' ), - ), - 'search' => array( - 'title' => _x( 'Search', 'Template name', 'gutenberg' ), - 'description' => __( 'Template used to display search results.', 'gutenberg' ), - ), - 'privacy-policy' => array( - 'title' => __( 'Privacy Policy', 'gutenberg' ), - 'description' => '', - ), - '404' => array( - 'title' => _x( '404', 'Template name', 'gutenberg' ), - 'description' => __( 'Template shown when no content is found.', 'gutenberg' ), - ), - ); - - /** - * Filters the list of template types. - * - * @param array $default_template_types An array of template types, formatted as [ slug => [ title, description ] ]. - * - * @since 5.x.x - */ - return apply_filters( 'default_template_types', $default_template_types ); -} - /** * Converts the default template types array from associative to indexed, * to be used in JS, where numeric keys (e.g. '404') always appear before alphabetical @@ -104,7 +20,7 @@ function gutenberg_get_default_template_types() { */ function gutenberg_get_indexed_default_template_types() { $indexed_template_types = array(); - $default_template_types = gutenberg_get_default_template_types(); + $default_template_types = get_default_block_template_types(); foreach ( $default_template_types as $slug => $template_type ) { $template_type['slug'] = (string) $slug; $indexed_template_types[] = $template_type; @@ -120,5 +36,5 @@ function gutenberg_get_indexed_default_template_types() { * @return string[] List of all overrideable default template type slugs. */ function gutenberg_get_template_type_slugs() { - return array_keys( gutenberg_get_default_template_types() ); + return array_keys( get_default_block_template_types() ); } From 88612a6e47e80f212fcf3d9585b0d2ec99927668 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 12:39:59 +0200 Subject: [PATCH 08/20] rename _gutenberg_get_template_file --- .../wordpress-5.9/block-template-utils.php | 49 ++++++++++++++++++- lib/full-site-editing/block-templates.php | 47 +----------------- 2 files changed, 49 insertions(+), 47 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index cda58e1339b6d..af38bc84ad86d 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -211,6 +211,53 @@ function get_default_block_template_types() { } } +if ( ! function_exists( '_get_block_template_file' ) ) { + /** + * Retrieves the template file from the theme for a given slug. + * + * @access private + * @internal + * + * @param string $template_type wp_template or wp_template_part. + * @param string $slug template slug. + * + * @return array|null Template. + */ + function _get_block_template_file( $template_type, $slug ) { + $template_base_paths = array( + 'wp_template' => 'block-templates', + 'wp_template_part' => 'block-template-parts', + ); + $themes = array( + get_stylesheet() => get_stylesheet_directory(), + get_template() => get_template_directory(), + ); + foreach ( $themes as $theme_slug => $theme_dir ) { + $file_path = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html'; + if ( file_exists( $file_path ) ) { + $new_template_item = array( + 'slug' => $slug, + 'path' => $file_path, + 'theme' => $theme_slug, + 'type' => $template_type, + ); + + if ( 'wp_template_part' === $template_type ) { + return _gutenberg_add_template_part_area_info( $new_template_item ); + } + + if ( 'wp_template' === $template_type ) { + return _gutenberg_add_template_info( $new_template_item ); + } + + return $new_template_item; + } + } + + return null; + } +} + if ( ! function_exists( '_build_block_template_result_from_post' ) ) { /** * Build a unified template object based a post Object. @@ -233,7 +280,7 @@ function _build_block_template_result_from_post( $post ) { $theme = $terms[0]->name; $has_theme_file = wp_get_theme()->get_stylesheet() === $theme && - null !== _gutenberg_get_template_file( $post->post_type, $post->post_name ); + null !== _get_block_template_file( $post->post_type, $post->post_name ); $template = new WP_Block_Template(); $template->wp_id = $post->ID; diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 080f33a5c0980..04e659cbdf27b 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -26,51 +26,6 @@ function _gutenberg_get_template_paths( $base_directory ) { return $path_list; } -/** - * Retrieves the template file from the theme for a given slug. - * - * @access private - * @internal - * - * @param string $template_type wp_template or wp_template_part. - * @param string $slug template slug. - * - * @return array|null Template. - */ -function _gutenberg_get_template_file( $template_type, $slug ) { - $template_base_paths = array( - 'wp_template' => 'block-templates', - 'wp_template_part' => 'block-template-parts', - ); - $themes = array( - get_stylesheet() => get_stylesheet_directory(), - get_template() => get_template_directory(), - ); - foreach ( $themes as $theme_slug => $theme_dir ) { - $file_path = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html'; - if ( file_exists( $file_path ) ) { - $new_template_item = array( - 'slug' => $slug, - 'path' => $file_path, - 'theme' => $theme_slug, - 'type' => $template_type, - ); - - if ( 'wp_template_part' === $template_type ) { - return _gutenberg_add_template_part_area_info( $new_template_item ); - } - - if ( 'wp_template' === $template_type ) { - return _gutenberg_add_template_info( $new_template_item ); - } - - return $new_template_item; - } - } - - return null; -} - /** * Retrieves the template files from the theme. * @@ -385,7 +340,7 @@ function gutenberg_get_block_file_template( $id, $template_type = 'wp_template' return apply_filters( 'get_block_file_template', null, $id, $template_type ); } - $template_file = _gutenberg_get_template_file( $template_type, $slug ); + $template_file = _get_block_template_file( $template_type, $slug ); if ( null === $template_file ) { /** This filter is documented at the end of this function */ return apply_filters( 'get_block_file_template', null, $id, $template_type ); From 9029417702757de3a57917af61e36082c2282dc0 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 12:48:10 +0200 Subject: [PATCH 09/20] rename _gutenberg_add_template_info + _gutenberg_add_template_part_area_info --- .../wordpress-5.9/block-template-utils.php | 50 ++++++++++++++++++- lib/full-site-editing/block-templates.php | 46 +---------------- 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index af38bc84ad86d..1a8a87507d0cb 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -211,6 +211,52 @@ function get_default_block_template_types() { } } +if ( ! function_exists( '_add_block_template_part_area_info' ) ) { + /** + * Attempts to add the template part's area information to the input template. + * + * @param array $template_info Template to add information to (requires 'type' and 'slug' fields). + * + * @return array Template. + */ + function _add_block_template_part_area_info( $template_info ) { + if ( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) { + $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_template_parts(); + } + + if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) { + $template_info['title'] = $theme_data[ $template_info['slug'] ]['title']; + $template_info['area'] = gutenberg_filter_template_part_area( $theme_data[ $template_info['slug'] ]['area'] ); + } else { + $template_info['area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED; + } + + return $template_info; + } +} + +if ( ! function_exists( '_add_block_template_info' ) ) { + /** + * Attempts to add custom template information to the template item. + * + * @param array $template_item Template to add information to (requires 'slug' field). + * @return array Template + */ + function _add_block_template_info( $template_item ) { + if ( ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) { + return $template_item; + } + + $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_custom_templates(); + if ( isset( $theme_data[ $template_item['slug'] ] ) ) { + $template_item['title'] = $theme_data[ $template_item['slug'] ]['title']; + $template_item['postTypes'] = $theme_data[ $template_item['slug'] ]['postTypes']; + } + + return $template_item; + } +} + if ( ! function_exists( '_get_block_template_file' ) ) { /** * Retrieves the template file from the theme for a given slug. @@ -243,11 +289,11 @@ function _get_block_template_file( $template_type, $slug ) { ); if ( 'wp_template_part' === $template_type ) { - return _gutenberg_add_template_part_area_info( $new_template_item ); + return _add_block_template_part_area_info( $new_template_item ); } if ( 'wp_template' === $template_type ) { - return _gutenberg_add_template_info( $new_template_item ); + return _add_block_template_info( $new_template_item ); } return $new_template_item; diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 04e659cbdf27b..2f8c2f4bfc9d7 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -66,11 +66,11 @@ function _gutenberg_get_template_files( $template_type ) { ); if ( 'wp_template_part' === $template_type ) { - $template_files[] = _gutenberg_add_template_part_area_info( $new_template_item ); + $template_files[] = _add_block_template_part_area_info( $new_template_item ); } if ( 'wp_template' === $template_type ) { - $template_files[] = _gutenberg_add_template_info( $new_template_item ); + $template_files[] = _add_block_template_info( $new_template_item ); } } } @@ -78,48 +78,6 @@ function _gutenberg_get_template_files( $template_type ) { return $template_files; } -/** - * Attempts to add custom template information to the template item. - * - * @param array $template_item Template to add information to (requires 'slug' field). - * @return array Template - */ -function _gutenberg_add_template_info( $template_item ) { - if ( ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) { - return $template_item; - } - - $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_custom_templates(); - if ( isset( $theme_data[ $template_item['slug'] ] ) ) { - $template_item['title'] = $theme_data[ $template_item['slug'] ]['title']; - $template_item['postTypes'] = $theme_data[ $template_item['slug'] ]['postTypes']; - } - - return $template_item; -} - -/** - * Attempts to add the template part's area information to the input template. - * - * @param array $template_info Template to add information to (requires 'type' and 'slug' fields). - * - * @return array Template. - */ -function _gutenberg_add_template_part_area_info( $template_info ) { - if ( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) { - $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_template_parts(); - } - - if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) { - $template_info['title'] = $theme_data[ $template_info['slug'] ]['title']; - $template_info['area'] = gutenberg_filter_template_part_area( $theme_data[ $template_info['slug'] ]['area'] ); - } else { - $template_info['area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED; - } - - return $template_info; -} - /** * Build a unified template object based on a theme file. * From 679bd49706039907383ff7dd42230139f5869324 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 12:51:42 +0200 Subject: [PATCH 10/20] rename gutenberg_get_block_file_template --- .../wordpress-5.9/block-template-utils.php | 63 ++++++++++++++++++- lib/full-site-editing/block-templates.php | 59 ----------------- ...ss-gutenberg-rest-templates-controller.php | 4 +- 3 files changed, 64 insertions(+), 62 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index 1a8a87507d0cb..703d6b1b8e90c 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -357,6 +357,67 @@ function _build_block_template_result_from_post( $post ) { } } +if ( ! function_exists( 'get_block_file_template' ) ) { + /** + * Retrieves a single unified template object using its id. + * Retrieves the file template. + * + * @param string $id Template unique identifier (example: theme_slug//template_slug). + * @param array $template_type wp_template or wp_template_part. + * + * @return WP_Block_Template|null File template. + */ + function get_block_file_template( $id, $template_type = 'wp_template' ) { + /** + * Filters the block templates array before the query takes place. + * + * Return a non-null value to bypass the WordPress queries. + * + * @since 10.8 + * + * @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query, + * or null to allow WP to run it's normal queries. + * @param string $id Template unique identifier (example: theme_slug//template_slug). + * @param array $template_type wp_template or wp_template_part. + */ + $block_template = apply_filters( 'pre_get_block_file_template', null, $id, $template_type ); + if ( ! is_null( $block_template ) ) { + return $block_template; + } + + $parts = explode( '//', $id, 2 ); + if ( count( $parts ) < 2 ) { + /** This filter is documented at the end of this function */ + return apply_filters( 'get_block_file_template', null, $id, $template_type ); + } + list( $theme, $slug ) = $parts; + + if ( wp_get_theme()->get_stylesheet() !== $theme ) { + /** This filter is documented at the end of this function */ + return apply_filters( 'get_block_file_template', null, $id, $template_type ); + } + + $template_file = _get_block_template_file( $template_type, $slug ); + if ( null === $template_file ) { + /** This filter is documented at the end of this function */ + return apply_filters( 'get_block_file_template', null, $id, $template_type ); + } + + $block_template = _gutenberg_build_template_result_from_file( $template_file, $template_type ); + + /** + * Filters the array of queried block templates array after they've been fetched. + * + * @since 10.8 + * + * @param null|WP_Block_Template $block_template The found block template. + * @param string $id Template unique identifier (example: theme_slug//template_slug). + * @param array $template_type wp_template or wp_template_part. + */ + return apply_filters( 'get_block_file_template', $block_template, $id, $template_type ); + } +} + if ( ! function_exists( 'get_block_template' ) ) { /** * Retrieves a single unified template object using its id. @@ -414,7 +475,7 @@ function get_block_template( $id, $template_type = 'wp_template' ) { } } - $block_template = gutenberg_get_block_file_template( $id, $template_type ); + $block_template = get_block_file_template( $id, $template_type ); /** * Filters the array of queried block templates array after they've been fetched. diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 2f8c2f4bfc9d7..5b58bfdd8492c 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -259,65 +259,6 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t return apply_filters( 'get_block_templates', $query_result, $query, $template_type ); } -/** - * Retrieves a single unified template object using its id. - * Retrieves the file template. - * - * @param string $id Template unique identifier (example: theme_slug//template_slug). - * @param array $template_type wp_template or wp_template_part. - * - * @return WP_Block_Template|null File template. - */ -function gutenberg_get_block_file_template( $id, $template_type = 'wp_template' ) { - /** - * Filters the block templates array before the query takes place. - * - * Return a non-null value to bypass the WordPress queries. - * - * @since 10.8 - * - * @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query, - * or null to allow WP to run it's normal queries. - * @param string $id Template unique identifier (example: theme_slug//template_slug). - * @param array $template_type wp_template or wp_template_part. - */ - $block_template = apply_filters( 'pre_get_block_file_template', null, $id, $template_type ); - if ( ! is_null( $block_template ) ) { - return $block_template; - } - - $parts = explode( '//', $id, 2 ); - if ( count( $parts ) < 2 ) { - /** This filter is documented at the end of this function */ - return apply_filters( 'get_block_file_template', null, $id, $template_type ); - } - list( $theme, $slug ) = $parts; - - if ( wp_get_theme()->get_stylesheet() !== $theme ) { - /** This filter is documented at the end of this function */ - return apply_filters( 'get_block_file_template', null, $id, $template_type ); - } - - $template_file = _get_block_template_file( $template_type, $slug ); - if ( null === $template_file ) { - /** This filter is documented at the end of this function */ - return apply_filters( 'get_block_file_template', null, $id, $template_type ); - } - - $block_template = _gutenberg_build_template_result_from_file( $template_file, $template_type ); - - /** - * Filters the array of queried block templates array after they've been fetched. - * - * @since 10.8 - * - * @param null|WP_Block_Template $block_template The found block template. - * @param string $id Template unique identifier (example: theme_slug//template_slug). - * @param array $template_type wp_template or wp_template_part. - */ - return apply_filters( 'get_block_file_template', $block_template, $id, $template_type ); -} - /** * Generates a unique slug for templates or template parts. * diff --git a/lib/full-site-editing/class-gutenberg-rest-templates-controller.php b/lib/full-site-editing/class-gutenberg-rest-templates-controller.php index ec616368e9882..b0ee8d9af6c77 100644 --- a/lib/full-site-editing/class-gutenberg-rest-templates-controller.php +++ b/lib/full-site-editing/class-gutenberg-rest-templates-controller.php @@ -173,7 +173,7 @@ public function get_item_permissions_check( $request ) { */ public function get_item( $request ) { if ( isset( $request['source'] ) && 'theme' === $request['source'] ) { - $template = gutenberg_get_block_file_template( $request['id'], $this->post_type ); + $template = get_block_file_template( $request['id'], $this->post_type ); } else { $template = get_block_template( $request['id'], $this->post_type ); } @@ -209,7 +209,7 @@ public function update_item( $request ) { if ( isset( $request['source'] ) && 'theme' === $request['source'] ) { wp_delete_post( $template->wp_id, true ); - return $this->prepare_item_for_response( gutenberg_get_block_file_template( $request['id'], $this->post_type ), $request ); + return $this->prepare_item_for_response( get_block_file_template( $request['id'], $this->post_type ), $request ); } $changes = $this->prepare_item_for_database( $request ); From a44cb1bc0ce7915535e935466bd4ffa069ca8959 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 12:54:51 +0200 Subject: [PATCH 11/20] rename _gutenberg_build_template_result_from_file --- .../wordpress-5.9/block-template-utils.php | 46 ++++++++++++++++++- lib/full-site-editing/block-templates.php | 44 +----------------- phpunit/class-block-templates-test.php | 6 +-- 3 files changed, 49 insertions(+), 47 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index 703d6b1b8e90c..eb3886e12b440 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -357,6 +357,50 @@ function _build_block_template_result_from_post( $post ) { } } +if ( ! function_exists( '_build_block_template_result_from_file' ) ) { + /** + * Build a unified template object based on a theme file. + * + * @param array $template_file Theme file. + * @param array $template_type wp_template or wp_template_part. + * + * @return WP_Block_Template Template. + */ + function _build_block_template_result_from_file( $template_file, $template_type ) { + $default_template_types = get_default_block_template_types(); + $template_content = file_get_contents( $template_file['path'] ); + $theme = wp_get_theme()->get_stylesheet(); + + $template = new WP_Block_Template(); + $template->id = $theme . '//' . $template_file['slug']; + $template->theme = $theme; + $template->content = _inject_theme_attribute_in_block_template_content( $template_content ); + $template->slug = $template_file['slug']; + $template->source = 'theme'; + $template->type = $template_type; + $template->title = ! empty( $template_file['title'] ) ? $template_file['title'] : $template_file['slug']; + $template->status = 'publish'; + $template->has_theme_file = true; + $template->is_custom = true; + + if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) { + $template->description = $default_template_types[ $template_file['slug'] ]['description']; + $template->title = $default_template_types[ $template_file['slug'] ]['title']; + $template->is_custom = false; + } + + if ( 'wp_template' === $template_type && isset( $template_file['postTypes'] ) ) { + $template->post_types = $template_file['postTypes']; + } + + if ( 'wp_template_part' === $template_type && isset( $template_file['area'] ) ) { + $template->area = $template_file['area']; + } + + return $template; + } +} + if ( ! function_exists( 'get_block_file_template' ) ) { /** * Retrieves a single unified template object using its id. @@ -403,7 +447,7 @@ function get_block_file_template( $id, $template_type = 'wp_template' ) { return apply_filters( 'get_block_file_template', null, $id, $template_type ); } - $block_template = _gutenberg_build_template_result_from_file( $template_file, $template_type ); + $block_template = _build_block_template_result_from_file( $template_file, $template_type ); /** * Filters the array of queried block templates array after they've been fetched. diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 5b58bfdd8492c..c84bdbc40f9b9 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -78,48 +78,6 @@ function _gutenberg_get_template_files( $template_type ) { return $template_files; } -/** - * Build a unified template object based on a theme file. - * - * @param array $template_file Theme file. - * @param array $template_type wp_template or wp_template_part. - * - * @return WP_Block_Template Template. - */ -function _gutenberg_build_template_result_from_file( $template_file, $template_type ) { - $default_template_types = get_default_block_template_types(); - $template_content = file_get_contents( $template_file['path'] ); - $theme = wp_get_theme()->get_stylesheet(); - - $template = new WP_Block_Template(); - $template->id = $theme . '//' . $template_file['slug']; - $template->theme = $theme; - $template->content = _inject_theme_attribute_in_block_template_content( $template_content ); - $template->slug = $template_file['slug']; - $template->source = 'theme'; - $template->type = $template_type; - $template->title = ! empty( $template_file['title'] ) ? $template_file['title'] : $template_file['slug']; - $template->status = 'publish'; - $template->has_theme_file = true; - $template->is_custom = true; - - if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) { - $template->description = $default_template_types[ $template_file['slug'] ]['description']; - $template->title = $default_template_types[ $template_file['slug'] ]['title']; - $template->is_custom = false; - } - - if ( 'wp_template' === $template_type && isset( $template_file['postTypes'] ) ) { - $template->post_types = $template_file['postTypes']; - } - - if ( 'wp_template_part' === $template_type && isset( $template_file['area'] ) ) { - $template->area = $template_file['area']; - } - - return $template; -} - /** * Retrieves a list of unified template objects based on a query. * @@ -213,7 +171,7 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t if ( ! isset( $query['wp_id'] ) ) { $template_files = _gutenberg_get_template_files( $template_type ); foreach ( $template_files as $template_file ) { - $template = _gutenberg_build_template_result_from_file( $template_file, $template_type ); + $template = _build_block_template_result_from_file( $template_file, $template_type ); if ( $post_type && ! $template->is_custom ) { continue; diff --git a/phpunit/class-block-templates-test.php b/phpunit/class-block-templates-test.php index 45e2ea06144d4..82f4d7fe7febd 100644 --- a/phpunit/class-block-templates-test.php +++ b/phpunit/class-block-templates-test.php @@ -79,8 +79,8 @@ public static function wpTearDownAfterClass() { wp_delete_post( self::$post->ID ); } - function test_gutenberg_build_template_result_from_file() { - $template = _gutenberg_build_template_result_from_file( + function test_build_block_template_result_from_file() { + $template = _build_block_template_result_from_file( array( 'slug' => 'single', 'path' => __DIR__ . '/fixtures/template.html', @@ -98,7 +98,7 @@ function test_gutenberg_build_template_result_from_file() { $this->assertEquals( 'wp_template', $template->type ); // Test template parts. - $template_part = _gutenberg_build_template_result_from_file( + $template_part = _build_block_template_result_from_file( array( 'slug' => 'header', 'path' => __DIR__ . '/fixtures/template.html', From ce18660d30b97f65627a867c45c0c1bffacbbb10 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 13:00:56 +0200 Subject: [PATCH 12/20] rename gutenberg_filter_template_part_area --- .../wordpress-5.9/block-template-utils.php | 29 ++++++++++++++++++- ...ss-gutenberg-rest-templates-controller.php | 4 +-- lib/full-site-editing/template-parts.php | 25 ---------------- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index eb3886e12b440..a93816455d644 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -211,6 +211,33 @@ function get_default_block_template_types() { } } +if ( ! function_exists( '_filter_block_template_part_area' ) ) { + /** + * Checks whether the input 'area' is a supported value. + * Returns the input if supported, otherwise returns the 'uncategorized' value. + * + * @param string $type Template part area name. + * + * @return string Input if supported, else the uncategorized value. + */ + function _filter_block_template_part_area( $type ) { + $allowed_areas = array_map( + function ( $item ) { + return $item['area']; + }, + get_allowed_block_template_part_areas() + ); + if ( in_array( $type, $allowed_areas, true ) ) { + return $type; + } + + /* translators: %1$s: Template area type, %2$s: the uncategorized template area value. */ + $warning_message = sprintf( __( '"%1$s" is not a supported wp_template_part area value and has been added as "%2$s".', 'gutenberg' ), $type, WP_TEMPLATE_PART_AREA_UNCATEGORIZED ); + trigger_error( $warning_message, E_USER_NOTICE ); + return WP_TEMPLATE_PART_AREA_UNCATEGORIZED; + } +} + if ( ! function_exists( '_add_block_template_part_area_info' ) ) { /** * Attempts to add the template part's area information to the input template. @@ -226,7 +253,7 @@ function _add_block_template_part_area_info( $template_info ) { if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) { $template_info['title'] = $theme_data[ $template_info['slug'] ]['title']; - $template_info['area'] = gutenberg_filter_template_part_area( $theme_data[ $template_info['slug'] ]['area'] ); + $template_info['area'] = _filter_block_template_part_area( $theme_data[ $template_info['slug'] ]['area'] ); } else { $template_info['area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED; } diff --git a/lib/full-site-editing/class-gutenberg-rest-templates-controller.php b/lib/full-site-editing/class-gutenberg-rest-templates-controller.php index b0ee8d9af6c77..50135fdeb5b77 100644 --- a/lib/full-site-editing/class-gutenberg-rest-templates-controller.php +++ b/lib/full-site-editing/class-gutenberg-rest-templates-controller.php @@ -377,9 +377,9 @@ protected function prepare_item_for_database( $request ) { if ( 'wp_template_part' === $this->post_type ) { if ( isset( $request['area'] ) ) { - $changes->tax_input['wp_template_part_area'] = gutenberg_filter_template_part_area( $request['area'] ); + $changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $request['area'] ); } elseif ( null !== $template && 'custom' !== $template->source && $template->area ) { - $changes->tax_input['wp_template_part_area'] = gutenberg_filter_template_part_area( $template->area ); + $changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $template->area ); } elseif ( ! $template->area ) { $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED; } diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index 4037231e2d672..d95fc669388d9 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -163,28 +163,3 @@ function set_unique_slug_on_create_template_part( $post_id ) { remove_action( 'save_post_wp_template_part', 'wp_set_unique_slug_on_create_template_part' ); add_action( 'save_post_wp_template_part', 'set_unique_slug_on_create_template_part' ); - -/** - * Checks whether the input 'area' is a supported value. - * Returns the input if supported, otherwise returns the 'uncategorized' value. - * - * @param string $type Template part area name. - * - * @return string Input if supported, else the uncategorized value. - */ -function gutenberg_filter_template_part_area( $type ) { - $allowed_areas = array_map( - function ( $item ) { - return $item['area']; - }, - get_allowed_block_template_part_areas() - ); - if ( in_array( $type, $allowed_areas, true ) ) { - return $type; - } - - /* translators: %1$s: Template area type, %2$s: the uncategorized template area value. */ - $warning_message = sprintf( __( '"%1$s" is not a supported wp_template_part area value and has been added as "%2$s".', 'gutenberg' ), $type, WP_TEMPLATE_PART_AREA_UNCATEGORIZED ); - trigger_error( $warning_message, E_USER_NOTICE ); - return WP_TEMPLATE_PART_AREA_UNCATEGORIZED; -} From f3f4519f4b356d26de1b596d819cde7e1158b766 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 13:24:37 +0200 Subject: [PATCH 13/20] reorder functions to match core file --- .../wordpress-5.9/block-template-utils.php | 533 ++++++++++++------ lib/full-site-editing/block-templates.php | 161 +----- ...ss-gutenberg-rest-templates-controller.php | 4 +- lib/full-site-editing/edit-site-export.php | 4 +- lib/full-site-editing/page-templates.php | 2 +- lib/full-site-editing/template-loader.php | 2 +- lib/full-site-editing/template-parts.php | 14 - phpunit/class-block-templates-test.php | 10 +- 8 files changed, 367 insertions(+), 363 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index a93816455d644..a609cfad8dc4c 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -7,6 +7,20 @@ * @package gutenberg */ +// Define constants for supported wp_template_part_area taxonomy. +if ( ! defined( 'WP_TEMPLATE_PART_AREA_HEADER' ) ) { + define( 'WP_TEMPLATE_PART_AREA_HEADER', 'header' ); +} +if ( ! defined( 'WP_TEMPLATE_PART_AREA_FOOTER' ) ) { + define( 'WP_TEMPLATE_PART_AREA_FOOTER', 'footer' ); +} +if ( ! defined( 'WP_TEMPLATE_PART_AREA_SIDEBAR' ) ) { + define( 'WP_TEMPLATE_PART_AREA_SIDEBAR', 'sidebar' ); +} +if ( ! defined( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED' ) ) { + define( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED', 'uncategorized' ); +} + if ( ! function_exists( 'get_allowed_block_template_part_areas' ) ) { /** * Returns a filtered list of allowed area values for template parts. @@ -56,75 +70,6 @@ function get_allowed_block_template_part_areas() { } } -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( 'get_default_block_template_types' ) ) { /** * Returns a filtered list of default template types, containing their @@ -238,49 +183,25 @@ function ( $item ) { } } -if ( ! function_exists( '_add_block_template_part_area_info' ) ) { +if ( ! function_exists( '_get_block_templates_paths' ) ) { /** - * Attempts to add the template part's area information to the input template. - * - * @param array $template_info Template to add information to (requires 'type' and 'slug' fields). + * Finds all nested template part file paths in a theme's directory. * - * @return array Template. - */ - function _add_block_template_part_area_info( $template_info ) { - if ( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) { - $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_template_parts(); - } - - if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) { - $template_info['title'] = $theme_data[ $template_info['slug'] ]['title']; - $template_info['area'] = _filter_block_template_part_area( $theme_data[ $template_info['slug'] ]['area'] ); - } else { - $template_info['area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED; - } - - return $template_info; - } -} - -if ( ! function_exists( '_add_block_template_info' ) ) { - /** - * Attempts to add custom template information to the template item. + * @access private * - * @param array $template_item Template to add information to (requires 'slug' field). - * @return array Template + * @param string $base_directory The theme's file path. + * @return array $path_list A list of paths to all template part files. */ - function _add_block_template_info( $template_item ) { - if ( ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) { - return $template_item; - } - - $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_custom_templates(); - if ( isset( $theme_data[ $template_item['slug'] ] ) ) { - $template_item['title'] = $theme_data[ $template_item['slug'] ]['title']; - $template_item['postTypes'] = $theme_data[ $template_item['slug'] ]['postTypes']; + function _get_block_templates_paths( $base_directory ) { + $path_list = array(); + if ( file_exists( $base_directory ) ) { + $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) ); + $nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH ); + foreach ( $nested_html_files as $path => $file ) { + $path_list[] = $path; + } } - - return $template_item; + return $path_list; } } @@ -331,56 +252,118 @@ function _get_block_template_file( $template_type, $slug ) { } } -if ( ! function_exists( '_build_block_template_result_from_post' ) ) { +if ( ! function_exists( '_add_block_template_info' ) ) { /** - * Build a unified template object based a post Object. + * Attempts to add custom template information to the template item. * - * @param WP_Post $post Template post. + * @param array $template_item Template to add information to (requires 'slug' field). + * @return array Template + */ + function _add_block_template_info( $template_item ) { + if ( ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) { + return $template_item; + } + + $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_custom_templates(); + if ( isset( $theme_data[ $template_item['slug'] ] ) ) { + $template_item['title'] = $theme_data[ $template_item['slug'] ]['title']; + $template_item['postTypes'] = $theme_data[ $template_item['slug'] ]['postTypes']; + } + + return $template_item; + } +} + +if ( ! function_exists( '_add_block_template_part_area_info' ) ) { + /** + * Attempts to add the template part's area information to the input template. * - * @return WP_Block_Template|WP_Error Template. + * @param array $template_info Template to add information to (requires 'type' and 'slug' fields). + * + * @return array Template. */ - function _build_block_template_result_from_post( $post ) { - $default_template_types = get_default_block_template_types(); - $terms = get_the_terms( $post, 'wp_theme' ); + function _add_block_template_part_area_info( $template_info ) { + if ( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) { + $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_template_parts(); + } - if ( is_wp_error( $terms ) ) { - return $terms; + if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) { + $template_info['title'] = $theme_data[ $template_info['slug'] ]['title']; + $template_info['area'] = _filter_block_template_part_area( $theme_data[ $template_info['slug'] ]['area'] ); + } else { + $template_info['area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED; } - if ( ! $terms ) { - return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.', 'gutenberg' ) ); + return $template_info; + } +} + +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; } - $theme = $terms[0]->name; - $has_theme_file = wp_get_theme()->get_stylesheet() === $theme && - null !== _get_block_template_file( $post->post_type, $post->post_name ); + while ( count( $queue ) > 0 ) { + $block = &$queue[0]; + array_shift( $queue ); + $all_blocks[] = &$block; - $template = new WP_Block_Template(); - $template->wp_id = $post->ID; - $template->id = $theme . '//' . $post->post_name; - $template->theme = $theme; - $template->content = $post->post_content; - $template->slug = $post->post_name; - $template->source = 'custom'; - $template->type = $post->post_type; - $template->description = $post->post_excerpt; - $template->title = $post->post_title; - $template->status = $post->post_status; - $template->has_theme_file = $has_theme_file; - $template->is_custom = true; + if ( ! empty( $block['innerBlocks'] ) ) { + foreach ( $block['innerBlocks'] as &$inner_block ) { + $queue[] = &$inner_block; + } + } + } - if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) { - $template->is_custom = false; + 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 ( 'wp_template_part' === $post->post_type ) { - $type_terms = get_the_terms( $post, 'wp_template_part_area' ); - if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { - $template->area = $type_terms[0]->name; + if ( $has_updated_content ) { + foreach ( $template_blocks as &$block ) { + $new_content .= serialize_block( $block ); } + + return $new_content; } - return $template; + return $template_content; } } @@ -428,17 +411,76 @@ function _build_block_template_result_from_file( $template_file, $template_type } } -if ( ! function_exists( 'get_block_file_template' ) ) { +if ( ! function_exists( '_build_block_template_result_from_post' ) ) { /** - * Retrieves a single unified template object using its id. - * Retrieves the file template. + * Build a unified template object based a post Object. * - * @param string $id Template unique identifier (example: theme_slug//template_slug). - * @param array $template_type wp_template or wp_template_part. + * @param WP_Post $post Template post. * - * @return WP_Block_Template|null File template. + * @return WP_Block_Template|WP_Error Template. */ - function get_block_file_template( $id, $template_type = 'wp_template' ) { + function _build_block_template_result_from_post( $post ) { + $default_template_types = get_default_block_template_types(); + $terms = get_the_terms( $post, 'wp_theme' ); + + if ( is_wp_error( $terms ) ) { + return $terms; + } + + if ( ! $terms ) { + return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.', 'gutenberg' ) ); + } + + $theme = $terms[0]->name; + $has_theme_file = wp_get_theme()->get_stylesheet() === $theme && + null !== _get_block_template_file( $post->post_type, $post->post_name ); + + $template = new WP_Block_Template(); + $template->wp_id = $post->ID; + $template->id = $theme . '//' . $post->post_name; + $template->theme = $theme; + $template->content = $post->post_content; + $template->slug = $post->post_name; + $template->source = 'custom'; + $template->type = $post->post_type; + $template->description = $post->post_excerpt; + $template->title = $post->post_title; + $template->status = $post->post_status; + $template->has_theme_file = $has_theme_file; + $template->is_custom = true; + + if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) { + $template->is_custom = false; + } + + if ( 'wp_template_part' === $post->post_type ) { + $type_terms = get_the_terms( $post, 'wp_template_part_area' ); + if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { + $template->area = $type_terms[0]->name; + } + } + + return $template; + } +} + +if ( ! function_exists( 'get_block_templates' ) ) { + /** + * Retrieves a list of unified template objects based on a query. + * + * @param array $query { + * Optional. Arguments to retrieve templates. + * + * @type array $slug__in List of slugs to include. + * @type int $wp_id Post ID of customized template. + * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for wp_template_part template type only). + * @type string $post_type Post type to get the templates for. + * } + * @param array $template_type wp_template or wp_template_part. + * + * @return array Templates. + */ + function get_block_templates( $query = array(), $template_type = 'wp_template' ) { /** * Filters the block templates array before the query takes place. * @@ -446,47 +488,121 @@ function get_block_file_template( $id, $template_type = 'wp_template' ) { * * @since 10.8 * - * @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query, - * or null to allow WP to run it's normal queries. - * @param string $id Template unique identifier (example: theme_slug//template_slug). - * @param array $template_type wp_template or wp_template_part. + * @param WP_Block_Template[]|null $block_templates Return an array of block templates to short-circuit the default query, + * or null to allow WP to run it's normal queries. + * @param array $query { + * Optional. Arguments to retrieve templates. + * + * @type array $slug__in List of slugs to include. + * @type int $wp_id Post ID of customized template. + * @type string $post_type Post type to get the templates for. + * } + * @param array $template_type wp_template or wp_template_part. */ - $block_template = apply_filters( 'pre_get_block_file_template', null, $id, $template_type ); - if ( ! is_null( $block_template ) ) { - return $block_template; + $templates = apply_filters( 'pre_get_block_templates', null, $query, $template_type ); + if ( ! is_null( $templates ) ) { + return $templates; } - $parts = explode( '//', $id, 2 ); - if ( count( $parts ) < 2 ) { - /** This filter is documented at the end of this function */ - return apply_filters( 'get_block_file_template', null, $id, $template_type ); + $post_type = isset( $query['post_type'] ) ? $query['post_type'] : ''; + $wp_query_args = array( + 'post_status' => array( 'auto-draft', 'draft', 'publish' ), + 'post_type' => $template_type, + 'posts_per_page' => -1, + 'no_found_rows' => true, + 'tax_query' => array( + array( + 'taxonomy' => 'wp_theme', + 'field' => 'name', + 'terms' => wp_get_theme()->get_stylesheet(), + ), + ), + ); + + if ( 'wp_template_part' === $template_type && isset( $query['area'] ) ) { + $wp_query_args['tax_query'][] = array( + 'taxonomy' => 'wp_template_part_area', + 'field' => 'name', + 'terms' => $query['area'], + ); + $wp_query_args['tax_query']['relation'] = 'AND'; } - list( $theme, $slug ) = $parts; - if ( wp_get_theme()->get_stylesheet() !== $theme ) { - /** This filter is documented at the end of this function */ - return apply_filters( 'get_block_file_template', null, $id, $template_type ); + if ( isset( $query['slug__in'] ) ) { + $wp_query_args['post_name__in'] = $query['slug__in']; } - $template_file = _get_block_template_file( $template_type, $slug ); - if ( null === $template_file ) { - /** This filter is documented at the end of this function */ - return apply_filters( 'get_block_file_template', null, $id, $template_type ); + // This is only needed for the regular templates/template parts CPT listing and editor. + if ( isset( $query['wp_id'] ) ) { + $wp_query_args['p'] = $query['wp_id']; + } else { + $wp_query_args['post_status'] = 'publish'; } - $block_template = _build_block_template_result_from_file( $template_file, $template_type ); + $template_query = new WP_Query( $wp_query_args ); + $query_result = array(); + foreach ( $template_query->posts as $post ) { + $template = _build_block_template_result_from_post( $post ); - /** - * Filters the array of queried block templates array after they've been fetched. - * - * @since 10.8 - * - * @param null|WP_Block_Template $block_template The found block template. - * @param string $id Template unique identifier (example: theme_slug//template_slug). - * @param array $template_type wp_template or wp_template_part. - */ - return apply_filters( 'get_block_file_template', $block_template, $id, $template_type ); + if ( is_wp_error( $template ) ) { + continue; + } + + if ( $post_type && ! $template->is_custom ) { + continue; + } + + $query_result[] = $template; + } + + if ( ! isset( $query['wp_id'] ) ) { + $template_files = _gutenberg_get_template_files( $template_type ); + foreach ( $template_files as $template_file ) { + $template = _build_block_template_result_from_file( $template_file, $template_type ); + + if ( $post_type && ! $template->is_custom ) { + continue; + } + + if ( $post_type && + isset( $template->post_types ) && + ! in_array( $post_type, $template->post_types, true ) + ) { + continue; + } + + $is_not_custom = false === array_search( + wp_get_theme()->get_stylesheet() . '//' . $template_file['slug'], + array_column( $query_result, 'id' ), + true + ); + $fits_slug_query = + ! isset( $query['slug__in'] ) || in_array( $template_file['slug'], $query['slug__in'], true ); + $fits_area_query = + ! isset( $query['area'] ) || $template_file['area'] === $query['area']; + $should_include = $is_not_custom && $fits_slug_query && $fits_area_query; + if ( $should_include ) { + $query_result[] = $template; + } + } + } } + + /** + * Filters the array of queried block templates array after they've been fetched. + * + * @since 10.8 + * + * @param WP_Block_Template[] $query_result Array of found block templates. + * @param array $query { + * Optional. Arguments to retrieve templates. + * + * @type array $slug__in List of slugs to include. + * @type int $wp_id Post ID of customized template. + * } + * @param array $template_type wp_template or wp_template_part. + */ + return apply_filters( 'get_block_templates', $query_result, $query, $template_type ); } if ( ! function_exists( 'get_block_template' ) ) { @@ -561,6 +677,67 @@ function get_block_template( $id, $template_type = 'wp_template' ) { } } +if ( ! function_exists( 'get_block_file_template' ) ) { + /** + * Retrieves a single unified template object using its id. + * Retrieves the file template. + * + * @param string $id Template unique identifier (example: theme_slug//template_slug). + * @param array $template_type wp_template or wp_template_part. + * + * @return WP_Block_Template|null File template. + */ + function get_block_file_template( $id, $template_type = 'wp_template' ) { + /** + * Filters the block templates array before the query takes place. + * + * Return a non-null value to bypass the WordPress queries. + * + * @since 10.8 + * + * @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query, + * or null to allow WP to run it's normal queries. + * @param string $id Template unique identifier (example: theme_slug//template_slug). + * @param array $template_type wp_template or wp_template_part. + */ + $block_template = apply_filters( 'pre_get_block_file_template', null, $id, $template_type ); + if ( ! is_null( $block_template ) ) { + return $block_template; + } + + $parts = explode( '//', $id, 2 ); + if ( count( $parts ) < 2 ) { + /** This filter is documented at the end of this function */ + return apply_filters( 'get_block_file_template', null, $id, $template_type ); + } + list( $theme, $slug ) = $parts; + + if ( wp_get_theme()->get_stylesheet() !== $theme ) { + /** This filter is documented at the end of this function */ + return apply_filters( 'get_block_file_template', null, $id, $template_type ); + } + + $template_file = _get_block_template_file( $template_type, $slug ); + if ( null === $template_file ) { + /** This filter is documented at the end of this function */ + return apply_filters( 'get_block_file_template', null, $id, $template_type ); + } + + $block_template = _build_block_template_result_from_file( $template_file, $template_type ); + + /** + * Filters the array of queried block templates array after they've been fetched. + * + * @since 10.8 + * + * @param null|WP_Block_Template $block_template The found block template. + * @param string $id Template unique identifier (example: theme_slug//template_slug). + * @param array $template_type wp_template or wp_template_part. + */ + return apply_filters( 'get_block_file_template', $block_template, $id, $template_type ); + } +} + if ( ! function_exists( 'block_template_part' ) ) { /** * Print a template-part. diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index c84bdbc40f9b9..6a5c834840b14 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -6,26 +6,6 @@ * @subpackage REST_API */ -/** - * Finds all nested template part file paths in a theme's directory. - * - * @access private - * - * @param string $base_directory The theme's file path. - * @return array $path_list A list of paths to all template part files. - */ -function _gutenberg_get_template_paths( $base_directory ) { - $path_list = array(); - if ( file_exists( $base_directory ) ) { - $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) ); - $nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH ); - foreach ( $nested_html_files as $path => $file ) { - $path_list[] = $path; - } - } - return $path_list; -} - /** * Retrieves the template files from the theme. * @@ -48,7 +28,7 @@ function _gutenberg_get_template_files( $template_type ) { $template_files = array(); foreach ( $themes as $theme_slug => $theme_dir ) { - $theme_template_files = _gutenberg_get_template_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] ); + $theme_template_files = _get_block_templates_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] ); foreach ( $theme_template_files as $template_file ) { $template_base_path = $template_base_paths[ $template_type ]; $template_slug = substr( @@ -78,145 +58,6 @@ function _gutenberg_get_template_files( $template_type ) { return $template_files; } -/** - * Retrieves a list of unified template objects based on a query. - * - * @param array $query { - * Optional. Arguments to retrieve templates. - * - * @type array $slug__in List of slugs to include. - * @type int $wp_id Post ID of customized template. - * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for wp_template_part template type only). - * @type string $post_type Post type to get the templates for. - * } - * @param array $template_type wp_template or wp_template_part. - * - * @return array Templates. - */ -function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_template' ) { - /** - * Filters the block templates array before the query takes place. - * - * Return a non-null value to bypass the WordPress queries. - * - * @since 10.8 - * - * @param WP_Block_Template[]|null $block_templates Return an array of block templates to short-circuit the default query, - * or null to allow WP to run it's normal queries. - * @param array $query { - * Optional. Arguments to retrieve templates. - * - * @type array $slug__in List of slugs to include. - * @type int $wp_id Post ID of customized template. - * @type string $post_type Post type to get the templates for. - * } - * @param array $template_type wp_template or wp_template_part. - */ - $templates = apply_filters( 'pre_get_block_templates', null, $query, $template_type ); - if ( ! is_null( $templates ) ) { - return $templates; - } - - $post_type = isset( $query['post_type'] ) ? $query['post_type'] : ''; - $wp_query_args = array( - 'post_status' => array( 'auto-draft', 'draft', 'publish' ), - 'post_type' => $template_type, - 'posts_per_page' => -1, - 'no_found_rows' => true, - 'tax_query' => array( - array( - 'taxonomy' => 'wp_theme', - 'field' => 'name', - 'terms' => wp_get_theme()->get_stylesheet(), - ), - ), - ); - - if ( 'wp_template_part' === $template_type && isset( $query['area'] ) ) { - $wp_query_args['tax_query'][] = array( - 'taxonomy' => 'wp_template_part_area', - 'field' => 'name', - 'terms' => $query['area'], - ); - $wp_query_args['tax_query']['relation'] = 'AND'; - } - - if ( isset( $query['slug__in'] ) ) { - $wp_query_args['post_name__in'] = $query['slug__in']; - } - - // This is only needed for the regular templates/template parts CPT listing and editor. - if ( isset( $query['wp_id'] ) ) { - $wp_query_args['p'] = $query['wp_id']; - } else { - $wp_query_args['post_status'] = 'publish'; - } - - $template_query = new WP_Query( $wp_query_args ); - $query_result = array(); - foreach ( $template_query->posts as $post ) { - $template = _build_block_template_result_from_post( $post ); - - if ( is_wp_error( $template ) ) { - continue; - } - - if ( $post_type && ! $template->is_custom ) { - continue; - } - - $query_result[] = $template; - } - - if ( ! isset( $query['wp_id'] ) ) { - $template_files = _gutenberg_get_template_files( $template_type ); - foreach ( $template_files as $template_file ) { - $template = _build_block_template_result_from_file( $template_file, $template_type ); - - if ( $post_type && ! $template->is_custom ) { - continue; - } - - if ( $post_type && - isset( $template->post_types ) && - ! in_array( $post_type, $template->post_types, true ) - ) { - continue; - } - - $is_not_custom = false === array_search( - wp_get_theme()->get_stylesheet() . '//' . $template_file['slug'], - array_column( $query_result, 'id' ), - true - ); - $fits_slug_query = - ! isset( $query['slug__in'] ) || in_array( $template_file['slug'], $query['slug__in'], true ); - $fits_area_query = - ! isset( $query['area'] ) || $template_file['area'] === $query['area']; - $should_include = $is_not_custom && $fits_slug_query && $fits_area_query; - if ( $should_include ) { - $query_result[] = $template; - } - } - } - - /** - * Filters the array of queried block templates array after they've been fetched. - * - * @since 10.8 - * - * @param WP_Block_Template[] $query_result Array of found block templates. - * @param array $query { - * Optional. Arguments to retrieve templates. - * - * @type array $slug__in List of slugs to include. - * @type int $wp_id Post ID of customized template. - * } - * @param array $template_type wp_template or wp_template_part. - */ - return apply_filters( 'get_block_templates', $query_result, $query, $template_type ); -} - /** * Generates a unique slug for templates or template parts. * diff --git a/lib/full-site-editing/class-gutenberg-rest-templates-controller.php b/lib/full-site-editing/class-gutenberg-rest-templates-controller.php index 50135fdeb5b77..8cf305df8e16a 100644 --- a/lib/full-site-editing/class-gutenberg-rest-templates-controller.php +++ b/lib/full-site-editing/class-gutenberg-rest-templates-controller.php @@ -146,7 +146,7 @@ public function get_items( $request ) { } $templates = array(); - foreach ( gutenberg_get_block_templates( $query, $this->post_type ) as $template ) { + foreach ( get_block_templates( $query, $this->post_type ) as $template ) { $data = $this->prepare_item_for_response( $template, $request ); $templates[] = $this->prepare_response_for_collection( $data ); } @@ -258,7 +258,7 @@ public function create_item( $request ) { if ( is_wp_error( $result ) ) { return $result; } - $posts = gutenberg_get_block_templates( array( 'wp_id' => $result ), $this->post_type ); + $posts = get_block_templates( array( 'wp_id' => $result ), $this->post_type ); if ( ! count( $posts ) ) { return new WP_Error( 'rest_template_insert_error', __( 'No templates exist with that id.', 'gutenberg' ) ); } diff --git a/lib/full-site-editing/edit-site-export.php b/lib/full-site-editing/edit-site-export.php index 6a29c23543495..c8915e6e69bc0 100644 --- a/lib/full-site-editing/edit-site-export.php +++ b/lib/full-site-editing/edit-site-export.php @@ -56,7 +56,7 @@ function gutenberg_edit_site_export_create_zip( $filename ) { $zip->addEmptyDir( 'theme/block-template-parts' ); // Load templates into the zip file. - $templates = gutenberg_get_block_templates(); + $templates = get_block_templates(); foreach ( $templates as $template ) { $template->content = _remove_theme_attribute_from_content( $template->content ); @@ -67,7 +67,7 @@ function gutenberg_edit_site_export_create_zip( $filename ) { } // Load template parts into the zip file. - $template_parts = gutenberg_get_block_templates( array(), 'wp_template_part' ); + $template_parts = get_block_templates( array(), 'wp_template_part' ); foreach ( $template_parts as $template_part ) { $zip->addFromString( 'theme/block-template-parts/' . $template_part->slug . '.html', diff --git a/lib/full-site-editing/page-templates.php b/lib/full-site-editing/page-templates.php index 1e2fc63264b1b..38c611f89f194 100644 --- a/lib/full-site-editing/page-templates.php +++ b/lib/full-site-editing/page-templates.php @@ -19,7 +19,7 @@ function gutenberg_load_block_page_templates( $templates, $theme, $post, $post_t return $templates; } - $block_templates = gutenberg_get_block_templates( array( 'post_type' => $post_type ), 'wp_template' ); + $block_templates = get_block_templates( array( 'post_type' => $post_type ), 'wp_template' ); foreach ( $block_templates as $template ) { $templates[ $template->slug ] = $template->title; } diff --git a/lib/full-site-editing/template-loader.php b/lib/full-site-editing/template-loader.php index efac8b066d32a..d022db59df5fc 100644 --- a/lib/full-site-editing/template-loader.php +++ b/lib/full-site-editing/template-loader.php @@ -132,7 +132,7 @@ function gutenberg_resolve_template( $template_type, $template_hierarchy ) { 'theme' => wp_get_theme()->get_stylesheet(), 'slug__in' => $slugs, ); - $templates = gutenberg_get_block_templates( $query ); + $templates = get_block_templates( $query ); // Order these templates per slug priority. // Build map of template slugs to their priority in the current hierarchy. diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index d95fc669388d9..474379884bd0f 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -89,20 +89,6 @@ function gutenberg_register_wp_template_part_area_taxonomy() { } add_action( 'init', 'gutenberg_register_wp_template_part_area_taxonomy' ); -// Define constants for supported wp_template_part_area taxonomy. -if ( ! defined( 'WP_TEMPLATE_PART_AREA_HEADER' ) ) { - define( 'WP_TEMPLATE_PART_AREA_HEADER', 'header' ); -} -if ( ! defined( 'WP_TEMPLATE_PART_AREA_FOOTER' ) ) { - define( 'WP_TEMPLATE_PART_AREA_FOOTER', 'footer' ); -} -if ( ! defined( 'WP_TEMPLATE_PART_AREA_SIDEBAR' ) ) { - define( 'WP_TEMPLATE_PART_AREA_SIDEBAR', 'sidebar' ); -} -if ( ! defined( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED' ) ) { - define( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED', 'uncategorized' ); -} - /** * Fixes the label of the 'wp_template_part' admin menu entry. */ diff --git a/phpunit/class-block-templates-test.php b/phpunit/class-block-templates-test.php index 82f4d7fe7febd..9c2abfdec2705 100644 --- a/phpunit/class-block-templates-test.php +++ b/phpunit/class-block-templates-test.php @@ -244,7 +244,7 @@ function test_gutenberg_get_block_template_from_post() { /** * Should retrieve block templates (file and CPT) */ - function test_gutenberg_get_block_templates() { + function test_get_block_templates() { function get_template_ids( $templates ) { return array_map( function( $template ) { @@ -255,7 +255,7 @@ function( $template ) { } // All results. - $templates = gutenberg_get_block_templates( array(), 'wp_template' ); + $templates = get_block_templates( array(), 'wp_template' ); $template_ids = get_template_ids( $templates ); // Avoid testing the entire array because the theme might add/remove templates. @@ -263,17 +263,17 @@ function( $template ) { $this->assertContains( get_stylesheet() . '//' . 'index', $template_ids ); // Filter by slug. - $templates = gutenberg_get_block_templates( array( 'slug__in' => array( 'my_template' ) ), 'wp_template' ); + $templates = get_block_templates( array( 'slug__in' => array( 'my_template' ) ), 'wp_template' ); $template_ids = get_template_ids( $templates ); $this->assertEquals( array( get_stylesheet() . '//' . 'my_template' ), $template_ids ); // Filter by CPT ID. - $templates = gutenberg_get_block_templates( array( 'wp_id' => self::$post->ID ), 'wp_template' ); + $templates = get_block_templates( array( 'wp_id' => self::$post->ID ), 'wp_template' ); $template_ids = get_template_ids( $templates ); $this->assertEquals( array( get_stylesheet() . '//' . 'my_template' ), $template_ids ); // Filter template part by area. - $templates = gutenberg_get_block_templates( array( 'area' => WP_TEMPLATE_PART_AREA_HEADER ), 'wp_template_part' ); + $templates = get_block_templates( array( 'area' => WP_TEMPLATE_PART_AREA_HEADER ), 'wp_template_part' ); $template_ids = get_template_ids( $templates ); $this->assertEquals( array( From 6788aea4fbeb511b64f0cc8f7332c3b4e66f7f65 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 13:29:29 +0200 Subject: [PATCH 14/20] rename _gutenberg_get_template_files --- .../wordpress-5.9/block-template-utils.php | 56 ++++++++++++++++++- lib/full-site-editing/block-templates.php | 52 ----------------- 2 files changed, 55 insertions(+), 53 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index a609cfad8dc4c..d75a068ec61ea 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -252,6 +252,60 @@ function _get_block_template_file( $template_type, $slug ) { } } +if ( ! function_exists( '_get_block_templates_files' ) ) { + /** + * Retrieves the template files from the theme. + * + * @access private + * @internal + * + * @param string $template_type wp_template or wp_template_part. + * + * @return array Template. + */ + function _get_block_templates_files( $template_type ) { + $template_base_paths = array( + 'wp_template' => 'block-templates', + 'wp_template_part' => 'block-template-parts', + ); + $themes = array( + get_stylesheet() => get_stylesheet_directory(), + get_template() => get_template_directory(), + ); + + $template_files = array(); + foreach ( $themes as $theme_slug => $theme_dir ) { + $theme_template_files = _get_block_templates_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] ); + foreach ( $theme_template_files as $template_file ) { + $template_base_path = $template_base_paths[ $template_type ]; + $template_slug = substr( + $template_file, + // Starting position of slug. + strpos( $template_file, $template_base_path . DIRECTORY_SEPARATOR ) + 1 + strlen( $template_base_path ), + // Subtract ending '.html'. + -5 + ); + $new_template_item = array( + 'slug' => $template_slug, + 'path' => $template_file, + 'theme' => $theme_slug, + 'type' => $template_type, + ); + + if ( 'wp_template_part' === $template_type ) { + $template_files[] = _add_block_template_part_area_info( $new_template_item ); + } + + if ( 'wp_template' === $template_type ) { + $template_files[] = _add_block_template_info( $new_template_item ); + } + } + } + + return $template_files; + } +} + if ( ! function_exists( '_add_block_template_info' ) ) { /** * Attempts to add custom template information to the template item. @@ -556,7 +610,7 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' ) } if ( ! isset( $query['wp_id'] ) ) { - $template_files = _gutenberg_get_template_files( $template_type ); + $template_files = _get_block_templates_files( $template_type ); foreach ( $template_files as $template_file ) { $template = _build_block_template_result_from_file( $template_file, $template_type ); diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 6a5c834840b14..a3db2bd040250 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -6,58 +6,6 @@ * @subpackage REST_API */ -/** - * Retrieves the template files from the theme. - * - * @access private - * @internal - * - * @param string $template_type wp_template or wp_template_part. - * - * @return array Template. - */ -function _gutenberg_get_template_files( $template_type ) { - $template_base_paths = array( - 'wp_template' => 'block-templates', - 'wp_template_part' => 'block-template-parts', - ); - $themes = array( - get_stylesheet() => get_stylesheet_directory(), - get_template() => get_template_directory(), - ); - - $template_files = array(); - foreach ( $themes as $theme_slug => $theme_dir ) { - $theme_template_files = _get_block_templates_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] ); - foreach ( $theme_template_files as $template_file ) { - $template_base_path = $template_base_paths[ $template_type ]; - $template_slug = substr( - $template_file, - // Starting position of slug. - strpos( $template_file, $template_base_path . DIRECTORY_SEPARATOR ) + 1 + strlen( $template_base_path ), - // Subtract ending '.html'. - -5 - ); - $new_template_item = array( - 'slug' => $template_slug, - 'path' => $template_file, - 'theme' => $theme_slug, - 'type' => $template_type, - ); - - if ( 'wp_template_part' === $template_type ) { - $template_files[] = _add_block_template_part_area_info( $new_template_item ); - } - - if ( 'wp_template' === $template_type ) { - $template_files[] = _add_block_template_info( $new_template_item ); - } - } - } - - return $template_files; -} - /** * Generates a unique slug for templates or template parts. * From ce829f517d70125d7c025ac8e028beaf2f4f5717 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 13:52:20 +0200 Subject: [PATCH 15/20] fix get_block_templates --- .../wordpress-5.9/block-template-utils.php | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index d75a068ec61ea..ed121103c8aa4 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -640,23 +640,22 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' ) } } } + /** + * Filters the array of queried block templates array after they've been fetched. + * + * @since 10.8 + * + * @param WP_Block_Template[] $query_result Array of found block templates. + * @param array $query { + * Optional. Arguments to retrieve templates. + * + * @type array $slug__in List of slugs to include. + * @type int $wp_id Post ID of customized template. + * } + * @param array $template_type wp_template or wp_template_part. + */ + return apply_filters( 'get_block_templates', $query_result, $query, $template_type ); } - - /** - * Filters the array of queried block templates array after they've been fetched. - * - * @since 10.8 - * - * @param WP_Block_Template[] $query_result Array of found block templates. - * @param array $query { - * Optional. Arguments to retrieve templates. - * - * @type array $slug__in List of slugs to include. - * @type int $wp_id Post ID of customized template. - * } - * @param array $template_type wp_template or wp_template_part. - */ - return apply_filters( 'get_block_templates', $query_result, $query, $template_type ); } if ( ! function_exists( 'get_block_template' ) ) { From ec558eccc7cce2ffa79bd39409be9e29ce82afe1 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 14:56:47 +0200 Subject: [PATCH 16/20] load utils earlier --- lib/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/load.php b/lib/load.php index 374862e764939..b14ebfee83ba5 100644 --- a/lib/load.php +++ b/lib/load.php @@ -83,6 +83,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat.php'; require __DIR__ . '/compat/wordpress-5.8/index.php'; require __DIR__ . '/compat/wordpress-5.8.1/index.php'; +require __DIR__ . '/compat/wordpress-5.9/block-template-utils.php'; require __DIR__ . '/compat/wordpress-5.9/default-editor-styles.php'; require __DIR__ . '/compat/wordpress-5.9/get-global-styles-and-settings.php'; require __DIR__ . '/utils.php'; @@ -114,7 +115,6 @@ 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'; From bc011db18bd4a49d828313cfa34c577ebc4d5f76 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 4 Nov 2021 16:53:01 +0200 Subject: [PATCH 17/20] rename test functions --- phpunit/class-block-templates-test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit/class-block-templates-test.php b/phpunit/class-block-templates-test.php index 9c2abfdec2705..96319414400be 100644 --- a/phpunit/class-block-templates-test.php +++ b/phpunit/class-block-templates-test.php @@ -194,7 +194,7 @@ function test_inject_theme_attribute_in_block_template_content() { /** * Should retrieve the template from the theme files. */ - function test_gutenberg_get_block_template_from_file() { + function test_get_block_template_from_file() { $id = get_stylesheet() . '//' . 'index'; $template = get_block_template( $id, 'wp_template' ); $this->assertEquals( $id, $template->id ); @@ -219,7 +219,7 @@ function test_gutenberg_get_block_template_from_file() { /** * Should retrieve the template from the CPT. */ - function test_gutenberg_get_block_template_from_post() { + function test_get_block_template_from_post() { $id = get_stylesheet() . '//' . 'my_template'; $template = get_block_template( $id, 'wp_template' ); $this->assertEquals( $id, $template->id ); From 91d63d419556505259ab09599a091aa28d357941 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Fri, 5 Nov 2021 09:44:37 +0200 Subject: [PATCH 18/20] add back `gutenberg` prefix in get_block_template and get_block_templates, as they already exist from 5.8 --- .../wordpress-5.9/block-template-utils.php | 335 +++++++++--------- ...ss-gutenberg-rest-templates-controller.php | 20 +- lib/full-site-editing/edit-site-export.php | 4 +- lib/full-site-editing/page-templates.php | 2 +- lib/full-site-editing/template-loader.php | 2 +- phpunit/class-block-templates-test.php | 18 +- 6 files changed, 189 insertions(+), 192 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index ed121103c8aa4..66f65cec82eeb 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -518,216 +518,213 @@ function _build_block_template_result_from_post( $post ) { } } -if ( ! function_exists( 'get_block_templates' ) ) { + +/** + * Retrieves a list of unified template objects based on a query. + * + * @param array $query { + * Optional. Arguments to retrieve templates. + * + * @type array $slug__in List of slugs to include. + * @type int $wp_id Post ID of customized template. + * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for wp_template_part template type only). + * @type string $post_type Post type to get the templates for. + * } + * @param array $template_type wp_template or wp_template_part. + * + * @return array Templates. + */ +function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_template' ) { /** - * Retrieves a list of unified template objects based on a query. + * Filters the block templates array before the query takes place. + * + * Return a non-null value to bypass the WordPress queries. + * + * @since 10.8 * + * @param WP_Block_Template[]|null $block_templates Return an array of block templates to short-circuit the default query, + * or null to allow WP to run it's normal queries. * @param array $query { * Optional. Arguments to retrieve templates. * - * @type array $slug__in List of slugs to include. - * @type int $wp_id Post ID of customized template. - * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for wp_template_part template type only). + * @type array $slug__in List of slugs to include. + * @type int $wp_id Post ID of customized template. * @type string $post_type Post type to get the templates for. * } * @param array $template_type wp_template or wp_template_part. - * - * @return array Templates. */ - function get_block_templates( $query = array(), $template_type = 'wp_template' ) { - /** - * Filters the block templates array before the query takes place. - * - * Return a non-null value to bypass the WordPress queries. - * - * @since 10.8 - * - * @param WP_Block_Template[]|null $block_templates Return an array of block templates to short-circuit the default query, - * or null to allow WP to run it's normal queries. - * @param array $query { - * Optional. Arguments to retrieve templates. - * - * @type array $slug__in List of slugs to include. - * @type int $wp_id Post ID of customized template. - * @type string $post_type Post type to get the templates for. - * } - * @param array $template_type wp_template or wp_template_part. - */ - $templates = apply_filters( 'pre_get_block_templates', null, $query, $template_type ); - if ( ! is_null( $templates ) ) { - return $templates; - } + $templates = apply_filters( 'pre_get_block_templates', null, $query, $template_type ); + if ( ! is_null( $templates ) ) { + return $templates; + } - $post_type = isset( $query['post_type'] ) ? $query['post_type'] : ''; - $wp_query_args = array( - 'post_status' => array( 'auto-draft', 'draft', 'publish' ), - 'post_type' => $template_type, - 'posts_per_page' => -1, - 'no_found_rows' => true, - 'tax_query' => array( - array( - 'taxonomy' => 'wp_theme', - 'field' => 'name', - 'terms' => wp_get_theme()->get_stylesheet(), - ), + $post_type = isset( $query['post_type'] ) ? $query['post_type'] : ''; + $wp_query_args = array( + 'post_status' => array( 'auto-draft', 'draft', 'publish' ), + 'post_type' => $template_type, + 'posts_per_page' => -1, + 'no_found_rows' => true, + 'tax_query' => array( + array( + 'taxonomy' => 'wp_theme', + 'field' => 'name', + 'terms' => wp_get_theme()->get_stylesheet(), ), + ), + ); + + if ( 'wp_template_part' === $template_type && isset( $query['area'] ) ) { + $wp_query_args['tax_query'][] = array( + 'taxonomy' => 'wp_template_part_area', + 'field' => 'name', + 'terms' => $query['area'], ); + $wp_query_args['tax_query']['relation'] = 'AND'; + } - if ( 'wp_template_part' === $template_type && isset( $query['area'] ) ) { - $wp_query_args['tax_query'][] = array( - 'taxonomy' => 'wp_template_part_area', - 'field' => 'name', - 'terms' => $query['area'], - ); - $wp_query_args['tax_query']['relation'] = 'AND'; - } + if ( isset( $query['slug__in'] ) ) { + $wp_query_args['post_name__in'] = $query['slug__in']; + } - if ( isset( $query['slug__in'] ) ) { - $wp_query_args['post_name__in'] = $query['slug__in']; + // This is only needed for the regular templates/template parts CPT listing and editor. + if ( isset( $query['wp_id'] ) ) { + $wp_query_args['p'] = $query['wp_id']; + } else { + $wp_query_args['post_status'] = 'publish'; + } + + $template_query = new WP_Query( $wp_query_args ); + $query_result = array(); + foreach ( $template_query->posts as $post ) { + $template = _build_block_template_result_from_post( $post ); + + if ( is_wp_error( $template ) ) { + continue; } - // This is only needed for the regular templates/template parts CPT listing and editor. - if ( isset( $query['wp_id'] ) ) { - $wp_query_args['p'] = $query['wp_id']; - } else { - $wp_query_args['post_status'] = 'publish'; + if ( $post_type && ! $template->is_custom ) { + continue; } - $template_query = new WP_Query( $wp_query_args ); - $query_result = array(); - foreach ( $template_query->posts as $post ) { - $template = _build_block_template_result_from_post( $post ); + $query_result[] = $template; + } - if ( is_wp_error( $template ) ) { - continue; - } + if ( ! isset( $query['wp_id'] ) ) { + $template_files = _get_block_templates_files( $template_type ); + foreach ( $template_files as $template_file ) { + $template = _build_block_template_result_from_file( $template_file, $template_type ); if ( $post_type && ! $template->is_custom ) { continue; } - $query_result[] = $template; - } - - if ( ! isset( $query['wp_id'] ) ) { - $template_files = _get_block_templates_files( $template_type ); - foreach ( $template_files as $template_file ) { - $template = _build_block_template_result_from_file( $template_file, $template_type ); - - if ( $post_type && ! $template->is_custom ) { - continue; - } - - if ( $post_type && - isset( $template->post_types ) && - ! in_array( $post_type, $template->post_types, true ) - ) { - continue; - } + if ( $post_type && + isset( $template->post_types ) && + ! in_array( $post_type, $template->post_types, true ) + ) { + continue; + } - $is_not_custom = false === array_search( - wp_get_theme()->get_stylesheet() . '//' . $template_file['slug'], - array_column( $query_result, 'id' ), - true - ); - $fits_slug_query = - ! isset( $query['slug__in'] ) || in_array( $template_file['slug'], $query['slug__in'], true ); - $fits_area_query = - ! isset( $query['area'] ) || $template_file['area'] === $query['area']; - $should_include = $is_not_custom && $fits_slug_query && $fits_area_query; - if ( $should_include ) { - $query_result[] = $template; - } + $is_not_custom = false === array_search( + wp_get_theme()->get_stylesheet() . '//' . $template_file['slug'], + array_column( $query_result, 'id' ), + true + ); + $fits_slug_query = + ! isset( $query['slug__in'] ) || in_array( $template_file['slug'], $query['slug__in'], true ); + $fits_area_query = + ! isset( $query['area'] ) || $template_file['area'] === $query['area']; + $should_include = $is_not_custom && $fits_slug_query && $fits_area_query; + if ( $should_include ) { + $query_result[] = $template; } } - /** - * Filters the array of queried block templates array after they've been fetched. - * - * @since 10.8 - * - * @param WP_Block_Template[] $query_result Array of found block templates. - * @param array $query { - * Optional. Arguments to retrieve templates. - * - * @type array $slug__in List of slugs to include. - * @type int $wp_id Post ID of customized template. - * } - * @param array $template_type wp_template or wp_template_part. - */ - return apply_filters( 'get_block_templates', $query_result, $query, $template_type ); } + /** + * Filters the array of queried block templates array after they've been fetched. + * + * @since 10.8 + * + * @param WP_Block_Template[] $query_result Array of found block templates. + * @param array $query { + * Optional. Arguments to retrieve templates. + * + * @type array $slug__in List of slugs to include. + * @type int $wp_id Post ID of customized template. + * } + * @param array $template_type wp_template or wp_template_part. + */ + return apply_filters( 'get_block_templates', $query_result, $query, $template_type ); } -if ( ! function_exists( 'get_block_template' ) ) { +/** + * Retrieves a single unified template object using its id. + * + * @param string $id Template unique identifier (example: theme_slug//template_slug). + * @param array $template_type wp_template or wp_template_part. + * + * @return WP_Block_Template|null Template. + */ +function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) { /** - * Retrieves a single unified template object using its id. + * Filters the block templates array before the query takes place. + * + * Return a non-null value to bypass the WordPress queries. * + * @since 10.8 + * + * @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query, + * or null to allow WP to run it's normal queries. * @param string $id Template unique identifier (example: theme_slug//template_slug). * @param array $template_type wp_template or wp_template_part. - * - * @return WP_Block_Template|null Template. */ - function get_block_template( $id, $template_type = 'wp_template' ) { - /** - * Filters the block templates array before the query takes place. - * - * Return a non-null value to bypass the WordPress queries. - * - * @since 10.8 - * - * @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query, - * or null to allow WP to run it's normal queries. - * @param string $id Template unique identifier (example: theme_slug//template_slug). - * @param array $template_type wp_template or wp_template_part. - */ - $block_template = apply_filters( 'pre_get_block_template', null, $id, $template_type ); - if ( ! is_null( $block_template ) ) { - return $block_template; - } + $block_template = apply_filters( 'pre_get_block_template', null, $id, $template_type ); + if ( ! is_null( $block_template ) ) { + return $block_template; + } - $parts = explode( '//', $id, 2 ); - if ( count( $parts ) < 2 ) { - return null; - } - list( $theme, $slug ) = $parts; - $wp_query_args = array( - 'post_name__in' => array( $slug ), - 'post_type' => $template_type, - 'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ), - 'posts_per_page' => 1, - 'no_found_rows' => true, - 'tax_query' => array( - array( - 'taxonomy' => 'wp_theme', - 'field' => 'name', - 'terms' => $theme, - ), + $parts = explode( '//', $id, 2 ); + if ( count( $parts ) < 2 ) { + return null; + } + list( $theme, $slug ) = $parts; + $wp_query_args = array( + 'post_name__in' => array( $slug ), + 'post_type' => $template_type, + 'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ), + 'posts_per_page' => 1, + 'no_found_rows' => true, + 'tax_query' => array( + array( + 'taxonomy' => 'wp_theme', + 'field' => 'name', + 'terms' => $theme, ), - ); - $template_query = new WP_Query( $wp_query_args ); - $posts = $template_query->posts; + ), + ); + $template_query = new WP_Query( $wp_query_args ); + $posts = $template_query->posts; - if ( count( $posts ) > 0 ) { - $template = _build_block_template_result_from_post( $posts[0] ); + if ( count( $posts ) > 0 ) { + $template = _build_block_template_result_from_post( $posts[0] ); - if ( ! is_wp_error( $template ) ) { - return $template; - } + if ( ! is_wp_error( $template ) ) { + return $template; } + } - $block_template = get_block_file_template( $id, $template_type ); + $block_template = get_block_file_template( $id, $template_type ); - /** - * Filters the array of queried block templates array after they've been fetched. - * - * @since 10.8 - * - * @param WP_Block_Template $block_template The found block template. - * @param string $id Template unique identifier (example: theme_slug//template_slug). - * @param array $template_type wp_template or wp_template_part. - */ - return apply_filters( 'get_block_template', $block_template, $id, $template_type ); - } + /** + * Filters the array of queried block templates array after they've been fetched. + * + * @since 10.8 + * + * @param WP_Block_Template $block_template The found block template. + * @param string $id Template unique identifier (example: theme_slug//template_slug). + * @param array $template_type wp_template or wp_template_part. + */ + return apply_filters( 'get_block_template', $block_template, $id, $template_type ); } if ( ! function_exists( 'get_block_file_template' ) ) { @@ -800,7 +797,7 @@ function get_block_file_template( $id, $template_type = 'wp_template' ) { * @return void */ function block_template_part( $part ) { - $template_part = get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' ); + $template_part = gutenberg_get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' ); if ( ! $template_part || empty( $template_part->content ) ) { return; } diff --git a/lib/full-site-editing/class-gutenberg-rest-templates-controller.php b/lib/full-site-editing/class-gutenberg-rest-templates-controller.php index 8cf305df8e16a..eb7f047a8e98d 100644 --- a/lib/full-site-editing/class-gutenberg-rest-templates-controller.php +++ b/lib/full-site-editing/class-gutenberg-rest-templates-controller.php @@ -146,7 +146,7 @@ public function get_items( $request ) { } $templates = array(); - foreach ( get_block_templates( $query, $this->post_type ) as $template ) { + foreach ( gutenberg_get_block_templates( $query, $this->post_type ) as $template ) { $data = $this->prepare_item_for_response( $template, $request ); $templates[] = $this->prepare_response_for_collection( $data ); } @@ -175,7 +175,7 @@ public function get_item( $request ) { if ( isset( $request['source'] ) && 'theme' === $request['source'] ) { $template = get_block_file_template( $request['id'], $this->post_type ); } else { - $template = get_block_template( $request['id'], $this->post_type ); + $template = gutenberg_get_block_template( $request['id'], $this->post_type ); } if ( ! $template ) { @@ -202,7 +202,7 @@ public function update_item_permissions_check( $request ) { * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function update_item( $request ) { - $template = get_block_template( $request['id'], $this->post_type ); + $template = gutenberg_get_block_template( $request['id'], $this->post_type ); if ( ! $template ) { return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.', 'gutenberg' ), array( 'status' => 404 ) ); } @@ -223,14 +223,14 @@ public function update_item( $request ) { return $result; } - $template = get_block_template( $request['id'], $this->post_type ); + $template = gutenberg_get_block_template( $request['id'], $this->post_type ); $fields_update = $this->update_additional_fields_for_object( $template, $request ); if ( is_wp_error( $fields_update ) ) { return $fields_update; } return $this->prepare_item_for_response( - get_block_template( $request['id'], $this->post_type ), + gutenberg_get_block_template( $request['id'], $this->post_type ), $request ); } @@ -258,19 +258,19 @@ public function create_item( $request ) { if ( is_wp_error( $result ) ) { return $result; } - $posts = get_block_templates( array( 'wp_id' => $result ), $this->post_type ); + $posts = gutenberg_get_block_templates( array( 'wp_id' => $result ), $this->post_type ); if ( ! count( $posts ) ) { return new WP_Error( 'rest_template_insert_error', __( 'No templates exist with that id.', 'gutenberg' ) ); } $id = $posts[0]->id; - $template = get_block_template( $id, $this->post_type ); + $template = gutenberg_get_block_template( $id, $this->post_type ); $fields_update = $this->update_additional_fields_for_object( $template, $request ); if ( is_wp_error( $fields_update ) ) { return $fields_update; } return $this->prepare_item_for_response( - get_block_template( $id, $this->post_type ), + gutenberg_get_block_template( $id, $this->post_type ), $request ); } @@ -292,7 +292,7 @@ public function delete_item_permissions_check( $request ) { * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function delete_item( $request ) { - $template = get_block_template( $request['id'], $this->post_type ); + $template = gutenberg_get_block_template( $request['id'], $this->post_type ); if ( ! $template ) { return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.', 'gutenberg' ), array( 'status' => 404 ) ); } @@ -339,7 +339,7 @@ public function delete_item( $request ) { * @return stdClass Changes to pass to wp_update_post. */ protected function prepare_item_for_database( $request ) { - $template = $request['id'] ? get_block_template( $request['id'], $this->post_type ) : null; + $template = $request['id'] ? gutenberg_get_block_template( $request['id'], $this->post_type ) : null; $changes = new stdClass(); if ( null === $template ) { $changes->post_type = $this->post_type; diff --git a/lib/full-site-editing/edit-site-export.php b/lib/full-site-editing/edit-site-export.php index c8915e6e69bc0..6a29c23543495 100644 --- a/lib/full-site-editing/edit-site-export.php +++ b/lib/full-site-editing/edit-site-export.php @@ -56,7 +56,7 @@ function gutenberg_edit_site_export_create_zip( $filename ) { $zip->addEmptyDir( 'theme/block-template-parts' ); // Load templates into the zip file. - $templates = get_block_templates(); + $templates = gutenberg_get_block_templates(); foreach ( $templates as $template ) { $template->content = _remove_theme_attribute_from_content( $template->content ); @@ -67,7 +67,7 @@ function gutenberg_edit_site_export_create_zip( $filename ) { } // Load template parts into the zip file. - $template_parts = get_block_templates( array(), 'wp_template_part' ); + $template_parts = gutenberg_get_block_templates( array(), 'wp_template_part' ); foreach ( $template_parts as $template_part ) { $zip->addFromString( 'theme/block-template-parts/' . $template_part->slug . '.html', diff --git a/lib/full-site-editing/page-templates.php b/lib/full-site-editing/page-templates.php index 38c611f89f194..1e2fc63264b1b 100644 --- a/lib/full-site-editing/page-templates.php +++ b/lib/full-site-editing/page-templates.php @@ -19,7 +19,7 @@ function gutenberg_load_block_page_templates( $templates, $theme, $post, $post_t return $templates; } - $block_templates = get_block_templates( array( 'post_type' => $post_type ), 'wp_template' ); + $block_templates = gutenberg_get_block_templates( array( 'post_type' => $post_type ), 'wp_template' ); foreach ( $block_templates as $template ) { $templates[ $template->slug ] = $template->title; } diff --git a/lib/full-site-editing/template-loader.php b/lib/full-site-editing/template-loader.php index d022db59df5fc..efac8b066d32a 100644 --- a/lib/full-site-editing/template-loader.php +++ b/lib/full-site-editing/template-loader.php @@ -132,7 +132,7 @@ function gutenberg_resolve_template( $template_type, $template_hierarchy ) { 'theme' => wp_get_theme()->get_stylesheet(), 'slug__in' => $slugs, ); - $templates = get_block_templates( $query ); + $templates = gutenberg_get_block_templates( $query ); // Order these templates per slug priority. // Build map of template slugs to their priority in the current hierarchy. diff --git a/phpunit/class-block-templates-test.php b/phpunit/class-block-templates-test.php index 96319414400be..7d0cfa3b3bc6c 100644 --- a/phpunit/class-block-templates-test.php +++ b/phpunit/class-block-templates-test.php @@ -196,7 +196,7 @@ function test_inject_theme_attribute_in_block_template_content() { */ function test_get_block_template_from_file() { $id = get_stylesheet() . '//' . 'index'; - $template = get_block_template( $id, 'wp_template' ); + $template = gutenberg_get_block_template( $id, 'wp_template' ); $this->assertEquals( $id, $template->id ); $this->assertEquals( get_stylesheet(), $template->theme ); $this->assertEquals( 'index', $template->slug ); @@ -206,7 +206,7 @@ function test_get_block_template_from_file() { // Test template parts. $id = get_stylesheet() . '//' . 'header'; - $template = get_block_template( $id, 'wp_template_part' ); + $template = gutenberg_get_block_template( $id, 'wp_template_part' ); $this->assertEquals( $id, $template->id ); $this->assertEquals( get_stylesheet(), $template->theme ); $this->assertEquals( 'header', $template->slug ); @@ -221,7 +221,7 @@ function test_get_block_template_from_file() { */ function test_get_block_template_from_post() { $id = get_stylesheet() . '//' . 'my_template'; - $template = get_block_template( $id, 'wp_template' ); + $template = gutenberg_get_block_template( $id, 'wp_template' ); $this->assertEquals( $id, $template->id ); $this->assertEquals( get_stylesheet(), $template->theme ); $this->assertEquals( 'my_template', $template->slug ); @@ -231,7 +231,7 @@ function test_get_block_template_from_post() { // Test template parts. $id = get_stylesheet() . '//' . 'my_template_part'; - $template = get_block_template( $id, 'wp_template_part' ); + $template = gutenberg_get_block_template( $id, 'wp_template_part' ); $this->assertEquals( $id, $template->id ); $this->assertEquals( get_stylesheet(), $template->theme ); $this->assertEquals( 'my_template_part', $template->slug ); @@ -244,7 +244,7 @@ function test_get_block_template_from_post() { /** * Should retrieve block templates (file and CPT) */ - function test_get_block_templates() { + function test_gutenberg_get_block_templates() { function get_template_ids( $templates ) { return array_map( function( $template ) { @@ -255,7 +255,7 @@ function( $template ) { } // All results. - $templates = get_block_templates( array(), 'wp_template' ); + $templates = gutenberg_get_block_templates( array(), 'wp_template' ); $template_ids = get_template_ids( $templates ); // Avoid testing the entire array because the theme might add/remove templates. @@ -263,17 +263,17 @@ function( $template ) { $this->assertContains( get_stylesheet() . '//' . 'index', $template_ids ); // Filter by slug. - $templates = get_block_templates( array( 'slug__in' => array( 'my_template' ) ), 'wp_template' ); + $templates = gutenberg_get_block_templates( array( 'slug__in' => array( 'my_template' ) ), 'wp_template' ); $template_ids = get_template_ids( $templates ); $this->assertEquals( array( get_stylesheet() . '//' . 'my_template' ), $template_ids ); // Filter by CPT ID. - $templates = get_block_templates( array( 'wp_id' => self::$post->ID ), 'wp_template' ); + $templates = gutenberg_get_block_templates( array( 'wp_id' => self::$post->ID ), 'wp_template' ); $template_ids = get_template_ids( $templates ); $this->assertEquals( array( get_stylesheet() . '//' . 'my_template' ), $template_ids ); // Filter template part by area. - $templates = get_block_templates( array( 'area' => WP_TEMPLATE_PART_AREA_HEADER ), 'wp_template_part' ); + $templates = gutenberg_get_block_templates( array( 'area' => WP_TEMPLATE_PART_AREA_HEADER ), 'wp_template_part' ); $template_ids = get_template_ids( $templates ); $this->assertEquals( array( From 529df1fa7f2e31f022f4552585dc36507fd552a5 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Fri, 5 Nov 2021 10:32:20 +0200 Subject: [PATCH 19/20] move templates controller to compat/5.9 --- .../class-gutenberg-rest-templates-controller.php | 0 lib/load.php | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename lib/{full-site-editing => compat/wordpress-5.9}/class-gutenberg-rest-templates-controller.php (100%) diff --git a/lib/full-site-editing/class-gutenberg-rest-templates-controller.php b/lib/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php similarity index 100% rename from lib/full-site-editing/class-gutenberg-rest-templates-controller.php rename to lib/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php diff --git a/lib/load.php b/lib/load.php index b14ebfee83ba5..e234d062b981e 100644 --- a/lib/load.php +++ b/lib/load.php @@ -56,7 +56,7 @@ function gutenberg_is_experiment_enabled( $name ) { if ( ! class_exists( 'WP_Rest_Customizer_Nonces' ) ) { require_once __DIR__ . '/class-wp-rest-customizer-nonces.php'; } - require_once __DIR__ . '/full-site-editing/class-gutenberg-rest-templates-controller.php'; + require_once __DIR__ . '/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php'; if ( ! class_exists( 'WP_REST_Block_Editor_Settings_Controller' ) ) { require_once dirname( __FILE__ ) . '/class-wp-rest-block-editor-settings-controller.php'; } From 76645bf1086e52a2964c7b14be6da0e89dd2ae91 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Fri, 5 Nov 2021 10:55:48 +0200 Subject: [PATCH 20/20] rename to `gutenberg_set_unique_slug_on_create_template_part` and bail early for 5.9 --- lib/full-site-editing/template-parts.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index 474379884bd0f..3b3dd6fa16e4d 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -126,7 +126,12 @@ function gutenberg_fix_template_part_admin_menu_entry() { * * @param int $post_id Post ID. */ -function set_unique_slug_on_create_template_part( $post_id ) { +function gutenberg_set_unique_slug_on_create_template_part( $post_id ) { + // This is the core function with the same functionality. + if ( function_exists( 'wp_set_unique_slug_on_create_template_part' ) ) { + return; + } + $post = get_post( $post_id ); if ( 'auto-draft' !== $post->post_status ) { return; @@ -147,5 +152,4 @@ function set_unique_slug_on_create_template_part( $post_id ) { } } -remove_action( 'save_post_wp_template_part', 'wp_set_unique_slug_on_create_template_part' ); -add_action( 'save_post_wp_template_part', 'set_unique_slug_on_create_template_part' ); +add_action( 'save_post_wp_template_part', 'gutenberg_set_unique_slug_on_create_template_part' );