Skip to content

Commit

Permalink
Adapt the Gutenberg plugin's code to work with FSE infrastructure in …
Browse files Browse the repository at this point in the history
…Core. (#32183)

* Adapt the Gutenberg plugin's code to work with FSE infrastructure in Core

* small changes coming from the merge ticket

Co-authored-by: André <nosolosw@users.noreply.github.com>
  • Loading branch information
youknowriad and nosolosw authored May 25, 2021
1 parent 9b6dd58 commit 7805bd8
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 18 deletions.
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 {
/**
* Post type.
*
Expand Down Expand Up @@ -335,22 +335,23 @@ 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;
$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';
}
Expand Down Expand Up @@ -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 ) {
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
6 changes: 2 additions & 4 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 Down Expand Up @@ -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';
}

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

0 comments on commit 7805bd8

Please sign in to comment.