Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Changes for FSE backport in core #36201

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
831 changes: 831 additions & 0 deletions lib/compat/wordpress-5.9/block-template-utils.php

Large diffs are not rendered by default.

586 changes: 0 additions & 586 deletions lib/full-site-editing/block-templates.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand All @@ -173,9 +173,9 @@ 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 = gutenberg_get_block_template( $request['id'], $this->post_type );
$template = get_block_template( $request['id'], $this->post_type );
}

if ( ! $template ) {
Expand All @@ -202,14 +202,14 @@ 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 ) );
}

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 );
Expand All @@ -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
);
}
Expand Down Expand Up @@ -258,19 +258,19 @@ 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' ) );
}
$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
);
}
Expand All @@ -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 ) );
}
Expand Down Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

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

This endpoint can also be moved to the 5.9 folder

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I saw that the controller was introduced in 5.8. Should I put the whole controller under 5.9 folder? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

5.9 because it changed between the two versions, so it should only be removed after 5.9 is the minimum version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure how to handle the endpoint here, as in load I guess we should have the check for if the class exists, but this will be true for 5.8 as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I think we could check something that only exists on 5.9 and have a comment there. Also the call registering the endpoint should use the same check and same comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also we should keep the class name different in order to be able to register it in 5.8 even if it already exists (Gutenberg prefix)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we keep the prefix, do we need a 5.9 check?

Copy link
Contributor

Choose a reason for hiding this comment

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

do we need a 5.9 check?

Not necessarily because the versions are exactly the same, that said, I think keeping the check is important for one case: imagine if in the future, we need to make changes to the endpoint, this means the endpoint need to move to wordpress-6.0 folder but if we don't have the check in place, we won't notice it and we could update it but keep it in wordpress-5.9 which means we'll forget about it when it comes time to make backports for 6.0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm.. okay. I think this is coupled with the registration of the post types (wp_template and wp_template_part) handling as well (rest_controller_class prop) and we can check it in a follow up? I guess a check for Gutenberg_REST_Global_Styles_Controller would be a good candidate when the patch makes it to core?

Copy link
Contributor

Choose a reason for hiding this comment

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

If it complicates things too much, it might not be worth it.

Expand Down Expand Up @@ -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;
}
Expand Down
88 changes: 2 additions & 86 deletions lib/full-site-editing/default-template-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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() );
}
6 changes: 3 additions & 3 deletions lib/full-site-editing/edit-site-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function _remove_theme_attribute_from_content( $template_content ) {
$new_content = '';
$template_blocks = parse_blocks( $template_content );

$blocks = _gutenberg_flatten_blocks( $template_blocks );
$blocks = _flatten_blocks( $template_blocks );
foreach ( $blocks as $key => $block ) {
if ( 'core/template-part' === $block['blockName'] && isset( $block['attrs']['theme'] ) ) {
unset( $blocks[ $key ]['attrs']['theme'] );
Expand Down Expand Up @@ -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 );

Expand All @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion lib/full-site-editing/page-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/full-site-editing/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
123 changes: 2 additions & 121 deletions lib/full-site-editing/template-parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -160,111 +146,6 @@ 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 );
}
}

/**
* 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;
}

/**
* 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' );
}
remove_action( 'save_post_wp_template_part', 'wp_set_unique_slug_on_create_template_part' );
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
add_action( 'save_post_wp_template_part', 'set_unique_slug_on_create_template_part' );
Loading