Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt the Gutenberg plugin's code to work with FSE infrastructure in Core. #32183

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/full-site-editing/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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'] &&
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* REST API: WP_REST_Templates_Controller class
* REST API: Gutenberg_REST_Templates_Controller class
*
* @package Gutenberg
* @subpackage REST_API
Expand All @@ -9,7 +9,7 @@
/**
* Base Templates REST API Controller.
*/
class WP_REST_Templates_Controller extends WP_REST_Controller {
class Gutenberg_REST_Templates_Controller extends WP_REST_Controller {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gutenberg plugin has more logic than core to support file templates... so it needs to use an alternative endpoint.

/**
* Post type.
*
Expand Down Expand Up @@ -406,7 +406,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 ) {
Expand Down
2 changes: 1 addition & 1 deletion lib/full-site-editing/template-parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion lib/full-site-editing/templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 3 additions & 5 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand All @@ -74,7 +72,7 @@ function gutenberg_is_experiment_enabled( $name ) {
}

if ( ! class_exists( 'WP_Widget_Block' ) ) {
require_once __DIR__ . '/class-wp-widget-block.php';
// require_once __DIR__ . '/class-wp-widget-block.php';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is temporary, waiting for #32176

}

require_once __DIR__ . '/widgets-page.php';
Expand All @@ -84,7 +82,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';
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class WP_REST_Template_Controller_Test extends WP_Test_REST_Controller_Testcase {
class Gutenberg_REST_Templates_Controller_Test extends WP_Test_REST_Controller_Testcase {
/**
* @var int
*/
Expand Down