From 7805bd87e299c809e4ff9c459a9637abee86fe6d Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 25 May 2021 16:07:31 +0100 Subject: [PATCH] Adapt the Gutenberg plugin's code to work with FSE infrastructure in Core. (#32183) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adapt the Gutenberg plugin's code to work with FSE infrastructure in Core * small changes coming from the merge ticket Co-authored-by: André --- lib/full-site-editing/block-templates.php | 8 ++++---- ...class-gutenberg-rest-templates-controller.php} | 15 ++++++++------- lib/full-site-editing/template-parts.php | 2 +- lib/full-site-editing/templates.php | 2 +- lib/load.php | 6 ++---- ...s-gutenberg-rest-template-controller-test.php} | 2 +- 6 files changed, 17 insertions(+), 18 deletions(-) rename lib/full-site-editing/{class-wp-rest-templates-controller.php => class-gutenberg-rest-templates-controller.php} (97%) rename phpunit/{class-wp-rest-template-controller-test.php => class-gutenberg-rest-template-controller-test.php} (98%) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 74f18280d138eb..b96235ae1bf2f5 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -145,7 +145,7 @@ function _gutenberg_add_template_part_area_info( $template_info ) { * * @return array block references to the passed blocks and their inner blocks. */ -function _flatten_blocks( &$blocks ) { +function _gutenberg_flatten_blocks( &$blocks ) { $all_blocks = array(); $queue = array(); foreach ( $blocks as &$block ) { @@ -175,12 +175,12 @@ function _flatten_blocks( &$blocks ) { * * @return string Updated wp_template content. */ -function _inject_theme_attribute_in_content( $template_content ) { +function _gutenberg_inject_theme_attribute_in_content( $template_content ) { $has_updated_content = false; $new_content = ''; $template_blocks = parse_blocks( $template_content ); - $blocks = _flatten_blocks( $template_blocks ); + $blocks = _gutenberg_flatten_blocks( $template_blocks ); foreach ( $blocks as &$block ) { if ( 'core/template-part' === $block['blockName'] && @@ -218,7 +218,7 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t $template = new WP_Block_Template(); $template->id = $theme . '//' . $template_file['slug']; $template->theme = $theme; - $template->content = _inject_theme_attribute_in_content( $template_content ); + $template->content = _gutenberg_inject_theme_attribute_in_content( $template_content ); $template->slug = $template_file['slug']; $template->source = 'theme'; $template->type = $template_type; diff --git a/lib/full-site-editing/class-wp-rest-templates-controller.php b/lib/full-site-editing/class-gutenberg-rest-templates-controller.php similarity index 97% rename from lib/full-site-editing/class-wp-rest-templates-controller.php rename to lib/full-site-editing/class-gutenberg-rest-templates-controller.php index e6b3a90efd3d38..c5435e1e525721 100644 --- a/lib/full-site-editing/class-wp-rest-templates-controller.php +++ b/lib/full-site-editing/class-gutenberg-rest-templates-controller.php @@ -1,6 +1,6 @@ post_type ) : null; - $changes = new stdClass(); - $changes->post_name = $template->slug; + $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; $changes->post_status = 'publish'; $changes->tax_input = array( - 'wp_theme' => isset( $request['theme'] ) ? $request['content'] : wp_get_theme()->get_stylesheet(), + 'wp_theme' => isset( $request['theme'] ) ? $request['theme'] : wp_get_theme()->get_stylesheet(), ); } elseif ( 'custom' !== $template->source ) { + $changes->post_name = $template->slug; $changes->post_type = $this->post_type; $changes->post_status = 'publish'; $changes->tax_input = array( 'wp_theme' => $template->theme, ); } else { + $changes->post_name = $template->slug; $changes->ID = $template->wp_id; $changes->post_status = 'publish'; } @@ -406,7 +407,7 @@ public function prepare_item_for_response( $template, $request ) { // phpcs:igno ), 'status' => $template->status, 'wp_id' => $template->wp_id, - 'has_theme_file' => $template->has_theme_file, + 'has_theme_file' => $template->has_theme_file ); if ( 'wp_template_part' === $template->type ) { diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index 6212c190d5bf39..2693821d03ace9 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -45,7 +45,7 @@ function gutenberg_register_template_part_post_type() { 'show_in_admin_bar' => false, 'show_in_rest' => true, 'rest_base' => 'template-parts', - 'rest_controller_class' => 'WP_REST_Templates_Controller', + 'rest_controller_class' => 'Gutenberg_REST_Templates_Controller', 'map_meta_cap' => true, 'supports' => array( 'title', diff --git a/lib/full-site-editing/templates.php b/lib/full-site-editing/templates.php index ec5ed3ca8deb3f..b391439243d05d 100644 --- a/lib/full-site-editing/templates.php +++ b/lib/full-site-editing/templates.php @@ -45,7 +45,7 @@ function gutenberg_register_template_post_type() { 'show_in_admin_bar' => false, 'show_in_rest' => true, 'rest_base' => 'templates', - 'rest_controller_class' => 'WP_REST_Templates_Controller', + 'rest_controller_class' => 'Gutenberg_REST_Templates_Controller', 'capability_type' => array( 'template', 'templates' ), 'map_meta_cap' => true, 'supports' => array( diff --git a/lib/load.php b/lib/load.php index d9c81b7ae3251b..f81e7f3e8621cb 100644 --- a/lib/load.php +++ b/lib/load.php @@ -56,9 +56,7 @@ function gutenberg_is_experiment_enabled( $name ) { if ( ! class_exists( 'WP_Rest_Customizer_Nonces' ) ) { require_once __DIR__ . '/class-wp-rest-customizer-nonces.php'; } - if ( ! class_exists( 'WP_REST_Templates_Controller' ) ) { - require_once __DIR__ . '/full-site-editing/class-wp-rest-templates-controller.php'; - } + require_once __DIR__ . '/full-site-editing/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'; } @@ -87,7 +85,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/utils.php'; require __DIR__ . '/editor-settings.php'; -if ( ! class_exists( 'WP_Block_Template ' ) ) { +if ( ! class_exists( 'WP_Block_Template' ) ) { require __DIR__ . '/full-site-editing/class-wp-block-template.php'; } diff --git a/phpunit/class-wp-rest-template-controller-test.php b/phpunit/class-gutenberg-rest-template-controller-test.php similarity index 98% rename from phpunit/class-wp-rest-template-controller-test.php rename to phpunit/class-gutenberg-rest-template-controller-test.php index cf5931a68a0a82..5979096a5bb8bc 100644 --- a/phpunit/class-wp-rest-template-controller-test.php +++ b/phpunit/class-gutenberg-rest-template-controller-test.php @@ -1,6 +1,6 @@