diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php
index 8a2aee344f8ad5..2ecc53b121adeb 100644
--- a/lib/compat/wordpress-5.9/block-template-utils.php
+++ b/lib/compat/wordpress-5.9/block-template-utils.php
@@ -21,7 +21,6 @@
define( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED', 'uncategorized' );
}
-
if ( ! function_exists( 'get_block_theme_folders' ) ) {
/**
* For backward compatibility reasons,
@@ -197,7 +196,7 @@ function get_default_block_template_types() {
*/
function _filter_block_template_part_area( $type ) {
$allowed_areas = array_map(
- function ( $item ) {
+ static function ( $item ) {
return $item['area'];
},
get_allowed_block_template_part_areas()
@@ -463,7 +462,6 @@ function _inject_theme_attribute_in_block_template_content( $template_content )
*/
function _remove_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 );
@@ -474,12 +472,8 @@ function _remove_theme_attribute_in_block_template_content( $template_content )
}
}
- if ( $has_updated_content ) {
- foreach ( $template_blocks as $block ) {
- $new_content .= serialize_block( $block );
- }
-
- return $new_content;
+ if ( ! $has_updated_content ) {
+ return $template_content;
}
return $template_content;
@@ -493,26 +487,24 @@ function _remove_theme_attribute_in_block_template_content( $template_content )
* @param array $template_file Theme file.
* @param array $template_type wp_template or wp_template_part.
*
- * @return WP_Block_Template Template.
+ * @return Gutenberg_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 = new Gutenberg_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->origin = null;
$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;
- $template->author = 0;
if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) {
$template->description = $default_template_types[ $template_file['slug'] ]['description'];
@@ -538,7 +530,7 @@ function _build_block_template_result_from_file( $template_file, $template_type
*
* @param WP_Post $post Template post.
*
- * @return WP_Block_Template|WP_Error Template.
+ * @return Gutenberg_Block_Template|WP_Error Template.
*/
function _build_block_template_result_from_post( $post ) {
$default_template_types = get_default_block_template_types();
@@ -558,7 +550,7 @@ function _build_block_template_result_from_post( $post ) {
$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 = new Gutenberg_Block_Template();
$template->wp_id = $post->ID;
$template->id = $theme . '//' . $post->post_name;
$template->theme = $theme;
@@ -613,7 +605,7 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t
*
* @since 10.8
*
- * @param WP_Block_Template[]|null $block_templates Return an array of block templates to short-circuit the default query,
+ * @param Gutenberg_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.
@@ -716,7 +708,7 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t
*
* @since 10.8
*
- * @param WP_Block_Template[] $query_result Array of found block templates.
+ * @param Gutenberg_Block_Template[] $query_result Array of found block templates.
* @param array $query {
* Optional. Arguments to retrieve templates.
*
@@ -734,7 +726,7 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t
* @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.
+ * @return Gutenberg_Block_Template|null Template.
*/
function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) {
/**
@@ -744,7 +736,7 @@ function gutenberg_get_block_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,
+ * @param Gutenberg_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.
@@ -791,7 +783,7 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) {
*
* @since 10.8
*
- * @param WP_Block_Template|null $block_template The found block template, or null if there isn't one.
+ * @param Gutenberg_Block_Template|null $block_template The found block template, or null if there isn't one.
* @param string $id Template unique identifier (example: theme_slug//template_slug).
* @param array $template_type wp_template or wp_template_part.
*/
@@ -806,7 +798,7 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_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.
+ * @return Gutenberg_Block_Template|null File template.
*/
function get_block_file_template( $id, $template_type = 'wp_template' ) {
/**
@@ -816,7 +808,7 @@ 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,
+ * @param Gutenberg_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.
@@ -851,7 +843,7 @@ function get_block_file_template( $id, $template_type = 'wp_template' ) {
*
* @since 10.8
*
- * @param null|WP_Block_Template $block_template The found block template.
+ * @param null|Gutenberg_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.
*/
diff --git a/lib/full-site-editing/class-wp-block-template.php b/lib/compat/wordpress-5.9/class-gutenberg-block-template.php
similarity index 94%
rename from lib/full-site-editing/class-wp-block-template.php
rename to lib/compat/wordpress-5.9/class-gutenberg-block-template.php
index 0068ebb6eec900..87ba1d2df16fd5 100644
--- a/lib/full-site-editing/class-wp-block-template.php
+++ b/lib/compat/wordpress-5.9/class-gutenberg-block-template.php
@@ -1,6 +1,6 @@
__( 'Template Parts', 'gutenberg' ),
+ 'singular_name' => __( 'Template Part', 'gutenberg' ),
+ 'menu_name' => _x( 'Template Parts', 'Admin Menu text', 'gutenberg' ),
+ 'add_new' => _x( 'Add New', 'Template Part', 'gutenberg' ),
+ 'add_new_item' => __( 'Add New Template Part', 'gutenberg' ),
+ 'new_item' => __( 'New Template Part', 'gutenberg' ),
+ 'edit_item' => __( 'Edit Template Part', 'gutenberg' ),
+ 'view_item' => __( 'View Template Part', 'gutenberg' ),
+ 'view_items' => __( 'View Template Parts', 'gutenberg' ),
+ 'all_items' => __( 'All Template Parts', 'gutenberg' ),
+ 'search_items' => __( 'Search Template Parts', 'gutenberg' ),
+ 'parent_item_colon' => __( 'Parent Template Part:', 'gutenberg' ),
+ 'not_found' => __( 'No template parts found.', 'gutenberg' ),
+ 'not_found_in_trash' => __( 'No template parts found in Trash.', 'gutenberg' ),
+ 'archives' => __( 'Template part archives', 'gutenberg' ),
+ 'insert_into_item' => __( 'Insert into template part', 'gutenberg' ),
+ 'uploaded_to_this_item' => __( 'Uploaded to this template part', 'gutenberg' ),
+ 'filter_items_list' => __( 'Filter template parts list', 'gutenberg' ),
+ 'items_list_navigation' => __( 'Template parts list navigation', 'gutenberg' ),
+ 'items_list' => __( 'Template parts list', 'gutenberg' ),
+ );
+
+ $args = array(
+ 'labels' => $labels,
+ 'description' => __( 'Template parts to include in your templates.', 'gutenberg' ),
+ 'public' => false,
+ 'has_archive' => false,
+ 'show_ui' => true,
+ 'show_in_menu' => false,
+ 'show_in_admin_bar' => false,
+ 'show_in_rest' => true,
+ 'rest_base' => 'template-parts',
+ 'rest_controller_class' => 'Gutenberg_REST_Templates_Controller',
+ 'map_meta_cap' => true,
+ 'supports' => array(
+ 'title',
+ 'slug',
+ 'excerpt',
+ 'editor',
+ 'revisions',
+ 'author',
+ ),
+ );
+
+ register_post_type( 'wp_template_part', $args );
+ }
+ add_action( 'init', 'gutenberg_register_template_part_post_type' );
+
+ /**
+ * Registers the 'wp_template_part_area' taxonomy.
+ */
+ function gutenberg_register_wp_template_part_area_taxonomy() {
+ if ( ! gutenberg_supports_block_templates() ) {
+ return;
+ }
+
+ register_taxonomy(
+ 'wp_template_part_area',
+ array( 'wp_template_part' ),
+ array(
+ 'public' => false,
+ 'hierarchical' => false,
+ 'labels' => array(
+ 'name' => __( 'Template Part Areas', 'gutenberg' ),
+ 'singular_name' => __( 'Template Part Area', 'gutenberg' ),
+ ),
+ 'query_var' => false,
+ 'rewrite' => false,
+ 'show_ui' => false,
+ '_builtin' => true,
+ 'show_in_nav_menus' => false,
+ 'show_in_rest' => false,
+ )
+ );
+ }
+ add_action( 'init', 'gutenberg_register_wp_template_part_area_taxonomy' );
+
+ /**
+ * Fixes the label of the 'wp_template_part' admin menu entry.
+ */
+ function gutenberg_fix_template_part_admin_menu_entry() {
+ if ( ! gutenberg_supports_block_templates() ) {
+ return;
+ }
+
+ global $submenu;
+ if ( ! isset( $submenu['themes.php'] ) ) {
+ return;
+ }
+ $post_type = get_post_type_object( 'wp_template_part' );
+ if ( ! $post_type ) {
+ return;
+ }
+ foreach ( $submenu['themes.php'] as $key => $submenu_entry ) {
+ if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) {
+ $submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
+ break;
+ }
+ }
+ }
+ add_action( 'admin_menu', 'gutenberg_fix_template_part_admin_menu_entry' );
+
+ // Customize the `wp_template` admin list.
+ add_filter( 'manage_wp_template_part_posts_columns', 'gutenberg_templates_lists_custom_columns' );
+ add_action( 'manage_wp_template_part_posts_custom_column', 'gutenberg_render_templates_lists_custom_column', 10, 2 );
+ add_filter( 'views_edit-wp_template_part', 'gutenberg_filter_templates_edit_views' );
+
+ /**
+ * Sets a custom slug when creating auto-draft template parts.
+ * This is only needed for auto-drafts created by the regular WP editor.
+ * If this page is to be removed, this won't be necessary.
+ *
+ * @param int $post_id 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;
+ }
+
+ if ( ! $post->post_name ) {
+ wp_update_post(
+ array(
+ 'ID' => $post_id,
+ 'post_name' => 'custom_slug_' . uniqid(),
+ )
+ );
+ }
+
+ $terms = get_the_terms( $post_id, 'wp_theme' );
+ if ( ! $terms || ! count( $terms ) ) {
+ wp_set_post_terms( $post_id, wp_get_theme()->get_stylesheet(), 'wp_theme' );
+ }
+ }
+
+ add_action( 'save_post_wp_template_part', 'gutenberg_set_unique_slug_on_create_template_part' );
+}
diff --git a/lib/compat/wordpress-5.9/templates.php b/lib/compat/wordpress-5.9/templates.php
new file mode 100644
index 00000000000000..819e6474abfd53
--- /dev/null
+++ b/lib/compat/wordpress-5.9/templates.php
@@ -0,0 +1,292 @@
+ __( 'Templates', 'gutenberg' ),
+ 'singular_name' => __( 'Template', 'gutenberg' ),
+ 'menu_name' => _x( 'Templates', 'Admin Menu text', 'gutenberg' ),
+ 'add_new' => _x( 'Add New', 'Template', 'gutenberg' ),
+ 'add_new_item' => __( 'Add New Template', 'gutenberg' ),
+ 'new_item' => __( 'New Template', 'gutenberg' ),
+ 'edit_item' => __( 'Edit Template', 'gutenberg' ),
+ 'view_item' => __( 'View Template', 'gutenberg' ),
+ 'view_items' => __( 'View Templates', 'gutenberg' ),
+ 'all_items' => __( 'All Templates', 'gutenberg' ),
+ 'search_items' => __( 'Search Templates', 'gutenberg' ),
+ 'parent_item_colon' => __( 'Parent Template:', 'gutenberg' ),
+ 'not_found' => __( 'No templates found.', 'gutenberg' ),
+ 'not_found_in_trash' => __( 'No templates found in Trash.', 'gutenberg' ),
+ 'archives' => __( 'Template archives', 'gutenberg' ),
+ 'insert_into_item' => __( 'Insert into template', 'gutenberg' ),
+ 'uploaded_to_this_item' => __( 'Uploaded to this template', 'gutenberg' ),
+ 'filter_items_list' => __( 'Filter templates list', 'gutenberg' ),
+ 'items_list_navigation' => __( 'Templates list navigation', 'gutenberg' ),
+ 'items_list' => __( 'Templates list', 'gutenberg' ),
+ );
+
+ $args = array(
+ 'labels' => $labels,
+ 'description' => __( 'Templates to include in your theme.', 'gutenberg' ),
+ 'public' => false,
+ 'has_archive' => false,
+ 'show_ui' => true,
+ 'show_in_menu' => false,
+ 'show_in_admin_bar' => false,
+ 'show_in_rest' => true,
+ 'rest_base' => 'templates',
+ 'rest_controller_class' => 'Gutenberg_REST_Templates_Controller',
+ 'capability_type' => array( 'template', 'templates' ),
+ 'map_meta_cap' => true,
+ 'supports' => array(
+ 'title',
+ 'slug',
+ 'excerpt',
+ 'editor',
+ 'revisions',
+ 'author',
+ ),
+ );
+
+ register_post_type( 'wp_template', $args );
+ }
+ add_action( 'init', 'gutenberg_register_template_post_type' );
+
+ /**
+ * Registers block editor 'wp_theme' taxonomy.
+ */
+ function gutenberg_register_wp_theme_taxonomy() {
+ if ( ! gutenberg_supports_block_templates() && ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
+ return;
+ }
+
+ register_taxonomy(
+ 'wp_theme',
+ array( 'wp_template', 'wp_template_part', 'wp_global_styles' ),
+ array(
+ 'public' => false,
+ 'hierarchical' => false,
+ 'labels' => array(
+ 'name' => __( 'Themes', 'gutenberg' ),
+ 'singular_name' => __( 'Theme', 'gutenberg' ),
+ ),
+ 'query_var' => false,
+ 'rewrite' => false,
+ 'show_ui' => false,
+ '_builtin' => true,
+ 'show_in_nav_menus' => false,
+ 'show_in_rest' => false,
+ )
+ );
+ }
+ add_action( 'init', 'gutenberg_register_wp_theme_taxonomy' );
+
+ /**
+ * Filters the capabilities of a user to conditionally grant them capabilities for managing 'wp_template' posts.
+ *
+ * Any user who can 'edit_theme_options' will have access.
+ *
+ * @param array $allcaps A user's capabilities.
+ * @return array Filtered $allcaps.
+ */
+ function gutenberg_grant_template_caps( array $allcaps ) {
+ if ( isset( $allcaps['edit_theme_options'] ) ) {
+ $allcaps['edit_templates'] = $allcaps['edit_theme_options'];
+ $allcaps['edit_others_templates'] = $allcaps['edit_theme_options'];
+ $allcaps['edit_published_templates'] = $allcaps['edit_theme_options'];
+ $allcaps['edit_private_templates'] = $allcaps['edit_theme_options'];
+ $allcaps['delete_templates'] = $allcaps['edit_theme_options'];
+ $allcaps['delete_others_templates'] = $allcaps['edit_theme_options'];
+ $allcaps['delete_published_templates'] = $allcaps['edit_theme_options'];
+ $allcaps['delete_private_templates'] = $allcaps['edit_theme_options'];
+ $allcaps['publish_templates'] = $allcaps['edit_theme_options'];
+ $allcaps['read_private_templates'] = $allcaps['edit_theme_options'];
+ }
+
+ return $allcaps;
+ }
+ add_filter( 'user_has_cap', 'gutenberg_grant_template_caps' );
+
+ /**
+ * Fixes the label of the 'wp_template' admin menu entry.
+ */
+ function gutenberg_fix_template_admin_menu_entry() {
+ if ( ! gutenberg_supports_block_templates() ) {
+ return;
+ }
+ global $submenu;
+ if ( ! isset( $submenu['themes.php'] ) ) {
+ return;
+ }
+ $post_type = get_post_type_object( 'wp_template' );
+ if ( ! $post_type ) {
+ return;
+ }
+ foreach ( $submenu['themes.php'] as $key => $submenu_entry ) {
+ if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) {
+ $submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
+ break;
+ }
+ }
+ }
+ add_action( 'admin_menu', 'gutenberg_fix_template_admin_menu_entry' );
+
+ // Customize the `wp_template` admin list.
+ add_filter( 'manage_wp_template_posts_columns', 'gutenberg_templates_lists_custom_columns' );
+ add_action( 'manage_wp_template_posts_custom_column', 'gutenberg_render_templates_lists_custom_column', 10, 2 );
+ add_filter( 'views_edit-wp_template', 'gutenberg_filter_templates_edit_views' );
+
+ /**
+ * Sets a custom slug when creating auto-draft templates.
+ * This is only needed for auto-drafts created by the regular WP editor.
+ * If this page is to be removed, this won't be necessary.
+ *
+ * @param int $post_id Post ID.
+ */
+ function set_unique_slug_on_create_template( $post_id ) {
+ $post = get_post( $post_id );
+ if ( 'auto-draft' !== $post->post_status ) {
+ return;
+ }
+
+ if ( ! $post->post_name ) {
+ wp_update_post(
+ array(
+ 'ID' => $post_id,
+ 'post_name' => 'custom_slug_' . uniqid(),
+ )
+ );
+ }
+
+ $terms = get_the_terms( $post_id, 'wp_theme' );
+ if ( ! $terms || ! count( $terms ) ) {
+ wp_set_post_terms( $post_id, wp_get_theme()->get_stylesheet(), 'wp_theme' );
+ }
+ }
+ add_action( 'save_post_wp_template', 'set_unique_slug_on_create_template' );
+
+ /**
+ * Print the skip-link script & styles.
+ *
+ * @todo Remove this when WP 5.8 is the minimum required version.
+ *
+ * @return void
+ */
+ function gutenberg_the_skip_link() {
+ // Early exit if not a block theme.
+ if ( ! gutenberg_supports_block_templates() ) {
+ return;
+ }
+
+ // Early exit if not a block template.
+ global $_wp_current_template_content;
+ if ( ! $_wp_current_template_content ) {
+ return;
+ }
+ ?>
+
+
+
+
+
+ __( 'Template Parts', 'gutenberg' ),
- 'singular_name' => __( 'Template Part', 'gutenberg' ),
- 'menu_name' => _x( 'Template Parts', 'Admin Menu text', 'gutenberg' ),
- 'add_new' => _x( 'Add New', 'Template Part', 'gutenberg' ),
- 'add_new_item' => __( 'Add New Template Part', 'gutenberg' ),
- 'new_item' => __( 'New Template Part', 'gutenberg' ),
- 'edit_item' => __( 'Edit Template Part', 'gutenberg' ),
- 'view_item' => __( 'View Template Part', 'gutenberg' ),
- 'view_items' => __( 'View Template Parts', 'gutenberg' ),
- 'all_items' => __( 'All Template Parts', 'gutenberg' ),
- 'search_items' => __( 'Search Template Parts', 'gutenberg' ),
- 'parent_item_colon' => __( 'Parent Template Part:', 'gutenberg' ),
- 'not_found' => __( 'No template parts found.', 'gutenberg' ),
- 'not_found_in_trash' => __( 'No template parts found in Trash.', 'gutenberg' ),
- 'archives' => __( 'Template part archives', 'gutenberg' ),
- 'insert_into_item' => __( 'Insert into template part', 'gutenberg' ),
- 'uploaded_to_this_item' => __( 'Uploaded to this template part', 'gutenberg' ),
- 'filter_items_list' => __( 'Filter template parts list', 'gutenberg' ),
- 'items_list_navigation' => __( 'Template parts list navigation', 'gutenberg' ),
- 'items_list' => __( 'Template parts list', 'gutenberg' ),
- );
-
- $args = array(
- 'labels' => $labels,
- 'description' => __( 'Template parts to include in your templates.', 'gutenberg' ),
- 'public' => false,
- 'has_archive' => false,
- 'show_ui' => true,
- 'show_in_menu' => false,
- 'show_in_admin_bar' => false,
- 'show_in_rest' => true,
- 'rest_base' => 'template-parts',
- 'rest_controller_class' => 'Gutenberg_REST_Templates_Controller',
- 'map_meta_cap' => true,
- 'supports' => array(
- 'title',
- 'slug',
- 'excerpt',
- 'editor',
- 'revisions',
- 'author',
- ),
- );
-
- register_post_type( 'wp_template_part', $args );
-}
-add_action( 'init', 'gutenberg_register_template_part_post_type' );
-
-/**
- * Registers the 'wp_template_part_area' taxonomy.
- */
-function gutenberg_register_wp_template_part_area_taxonomy() {
- if ( ! gutenberg_supports_block_templates() ) {
- return;
- }
-
- register_taxonomy(
- 'wp_template_part_area',
- array( 'wp_template_part' ),
- array(
- 'public' => false,
- 'hierarchical' => false,
- 'labels' => array(
- 'name' => __( 'Template Part Areas', 'gutenberg' ),
- 'singular_name' => __( 'Template Part Area', 'gutenberg' ),
- ),
- 'query_var' => false,
- 'rewrite' => false,
- 'show_ui' => false,
- '_builtin' => true,
- 'show_in_nav_menus' => false,
- 'show_in_rest' => false,
- )
- );
-}
-add_action( 'init', 'gutenberg_register_wp_template_part_area_taxonomy' );
-
-/**
- * Fixes the label of the 'wp_template_part' admin menu entry.
- */
-function gutenberg_fix_template_part_admin_menu_entry() {
- if ( ! gutenberg_supports_block_templates() ) {
- return;
- }
-
- global $submenu;
- if ( ! isset( $submenu['themes.php'] ) ) {
- return;
- }
- $post_type = get_post_type_object( 'wp_template_part' );
- if ( ! $post_type ) {
- return;
- }
- foreach ( $submenu['themes.php'] as $key => $submenu_entry ) {
- if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) {
- $submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
- break;
- }
- }
-}
-add_action( 'admin_menu', 'gutenberg_fix_template_part_admin_menu_entry' );
-
-// Customize the `wp_template` admin list.
-add_filter( 'manage_wp_template_part_posts_columns', 'gutenberg_templates_lists_custom_columns' );
-add_action( 'manage_wp_template_part_posts_custom_column', 'gutenberg_render_templates_lists_custom_column', 10, 2 );
-add_filter( 'views_edit-wp_template_part', 'gutenberg_filter_templates_edit_views' );
-
-/**
- * Sets a custom slug when creating auto-draft template parts.
- * This is only needed for auto-drafts created by the regular WP editor.
- * If this page is to be removed, this won't be necessary.
- *
- * @param int $post_id 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;
- }
-
- if ( ! $post->post_name ) {
- wp_update_post(
- array(
- 'ID' => $post_id,
- 'post_name' => 'custom_slug_' . uniqid(),
- )
- );
- }
-
- $terms = get_the_terms( $post_id, 'wp_theme' );
- if ( ! $terms || ! count( $terms ) ) {
- wp_set_post_terms( $post_id, wp_get_theme()->get_stylesheet(), 'wp_theme' );
- }
-}
-
-add_action( 'save_post_wp_template_part', 'gutenberg_set_unique_slug_on_create_template_part' );
diff --git a/lib/full-site-editing/templates.php b/lib/full-site-editing/templates.php
deleted file mode 100644
index f20e5f5e4c40b8..00000000000000
--- a/lib/full-site-editing/templates.php
+++ /dev/null
@@ -1,282 +0,0 @@
- __( 'Templates', 'gutenberg' ),
- 'singular_name' => __( 'Template', 'gutenberg' ),
- 'menu_name' => _x( 'Templates', 'Admin Menu text', 'gutenberg' ),
- 'add_new' => _x( 'Add New', 'Template', 'gutenberg' ),
- 'add_new_item' => __( 'Add New Template', 'gutenberg' ),
- 'new_item' => __( 'New Template', 'gutenberg' ),
- 'edit_item' => __( 'Edit Template', 'gutenberg' ),
- 'view_item' => __( 'View Template', 'gutenberg' ),
- 'view_items' => __( 'View Templates', 'gutenberg' ),
- 'all_items' => __( 'All Templates', 'gutenberg' ),
- 'search_items' => __( 'Search Templates', 'gutenberg' ),
- 'parent_item_colon' => __( 'Parent Template:', 'gutenberg' ),
- 'not_found' => __( 'No templates found.', 'gutenberg' ),
- 'not_found_in_trash' => __( 'No templates found in Trash.', 'gutenberg' ),
- 'archives' => __( 'Template archives', 'gutenberg' ),
- 'insert_into_item' => __( 'Insert into template', 'gutenberg' ),
- 'uploaded_to_this_item' => __( 'Uploaded to this template', 'gutenberg' ),
- 'filter_items_list' => __( 'Filter templates list', 'gutenberg' ),
- 'items_list_navigation' => __( 'Templates list navigation', 'gutenberg' ),
- 'items_list' => __( 'Templates list', 'gutenberg' ),
- );
-
- $args = array(
- 'labels' => $labels,
- 'description' => __( 'Templates to include in your theme.', 'gutenberg' ),
- 'public' => false,
- 'has_archive' => false,
- 'show_ui' => true,
- 'show_in_menu' => false,
- 'show_in_admin_bar' => false,
- 'show_in_rest' => true,
- 'rest_base' => 'templates',
- 'rest_controller_class' => 'Gutenberg_REST_Templates_Controller',
- 'capability_type' => array( 'template', 'templates' ),
- 'map_meta_cap' => true,
- 'supports' => array(
- 'title',
- 'slug',
- 'excerpt',
- 'editor',
- 'revisions',
- 'author',
- ),
- );
-
- register_post_type( 'wp_template', $args );
-}
-add_action( 'init', 'gutenberg_register_template_post_type' );
-
-/**
- * Registers block editor 'wp_theme' taxonomy.
- */
-function gutenberg_register_wp_theme_taxonomy() {
- if ( ! gutenberg_supports_block_templates() && ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
- return;
- }
-
- register_taxonomy(
- 'wp_theme',
- array( 'wp_template', 'wp_template_part', 'wp_global_styles' ),
- array(
- 'public' => false,
- 'hierarchical' => false,
- 'labels' => array(
- 'name' => __( 'Themes', 'gutenberg' ),
- 'singular_name' => __( 'Theme', 'gutenberg' ),
- ),
- 'query_var' => false,
- 'rewrite' => false,
- 'show_ui' => false,
- '_builtin' => true,
- 'show_in_nav_menus' => false,
- 'show_in_rest' => false,
- )
- );
-}
-add_action( 'init', 'gutenberg_register_wp_theme_taxonomy' );
-
-/**
- * Filters the capabilities of a user to conditionally grant them capabilities for managing 'wp_template' posts.
- *
- * Any user who can 'edit_theme_options' will have access.
- *
- * @param array $allcaps A user's capabilities.
- * @return array Filtered $allcaps.
- */
-function gutenberg_grant_template_caps( array $allcaps ) {
- if ( isset( $allcaps['edit_theme_options'] ) ) {
- $allcaps['edit_templates'] = $allcaps['edit_theme_options'];
- $allcaps['edit_others_templates'] = $allcaps['edit_theme_options'];
- $allcaps['edit_published_templates'] = $allcaps['edit_theme_options'];
- $allcaps['edit_private_templates'] = $allcaps['edit_theme_options'];
- $allcaps['delete_templates'] = $allcaps['edit_theme_options'];
- $allcaps['delete_others_templates'] = $allcaps['edit_theme_options'];
- $allcaps['delete_published_templates'] = $allcaps['edit_theme_options'];
- $allcaps['delete_private_templates'] = $allcaps['edit_theme_options'];
- $allcaps['publish_templates'] = $allcaps['edit_theme_options'];
- $allcaps['read_private_templates'] = $allcaps['edit_theme_options'];
- }
-
- return $allcaps;
-}
-add_filter( 'user_has_cap', 'gutenberg_grant_template_caps' );
-
-/**
- * Fixes the label of the 'wp_template' admin menu entry.
- */
-function gutenberg_fix_template_admin_menu_entry() {
- if ( ! gutenberg_supports_block_templates() ) {
- return;
- }
- global $submenu;
- if ( ! isset( $submenu['themes.php'] ) ) {
- return;
- }
- $post_type = get_post_type_object( 'wp_template' );
- if ( ! $post_type ) {
- return;
- }
- foreach ( $submenu['themes.php'] as $key => $submenu_entry ) {
- if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) {
- $submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
- break;
- }
- }
-}
-add_action( 'admin_menu', 'gutenberg_fix_template_admin_menu_entry' );
-
-// Customize the `wp_template` admin list.
-add_filter( 'manage_wp_template_posts_columns', 'gutenberg_templates_lists_custom_columns' );
-add_action( 'manage_wp_template_posts_custom_column', 'gutenberg_render_templates_lists_custom_column', 10, 2 );
-add_filter( 'views_edit-wp_template', 'gutenberg_filter_templates_edit_views' );
-
-/**
- * Sets a custom slug when creating auto-draft templates.
- * This is only needed for auto-drafts created by the regular WP editor.
- * If this page is to be removed, this won't be necessary.
- *
- * @param int $post_id Post ID.
- */
-function set_unique_slug_on_create_template( $post_id ) {
- $post = get_post( $post_id );
- if ( 'auto-draft' !== $post->post_status ) {
- return;
- }
-
- if ( ! $post->post_name ) {
- wp_update_post(
- array(
- 'ID' => $post_id,
- 'post_name' => 'custom_slug_' . uniqid(),
- )
- );
- }
-
- $terms = get_the_terms( $post_id, 'wp_theme' );
- if ( ! $terms || ! count( $terms ) ) {
- wp_set_post_terms( $post_id, wp_get_theme()->get_stylesheet(), 'wp_theme' );
- }
-}
-add_action( 'save_post_wp_template', 'set_unique_slug_on_create_template' );
-
-/**
- * Print the skip-link script & styles.
- *
- * @todo Remove this when WP 5.8 is the minimum required version.
- *
- * @return void
- */
-function gutenberg_the_skip_link() {
- // Early exit if not a block theme.
- if ( ! gutenberg_supports_block_templates() ) {
- return;
- }
-
- // Early exit if not a block template.
- global $_wp_current_template_content;
- if ( ! $_wp_current_template_content ) {
- return;
- }
- ?>
-
-
-
-
-
- 'wp_template',
- 'post_name' => 'my_template',
- 'post_title' => 'My Template',
- 'post_content' => 'Content',
- 'post_excerpt' => 'Description of my template',
- 'tax_input' => array(
- 'wp_theme' => array(
- 'this-theme-should-not-resolve',
- ),
- ),
- );
- self::$post = self::factory()->post->create_and_get( $args );
- wp_set_post_terms( self::$post->ID, 'this-theme-should-not-resolve', 'wp_theme' );
-
- // Set up template post.
- $args = array(
- 'post_type' => 'wp_template',
- 'post_name' => 'my_template',
- 'post_title' => 'My Template',
- 'post_content' => 'Content',
- 'post_excerpt' => 'Description of my template',
- 'tax_input' => array(
- 'wp_theme' => array(
- get_stylesheet(),
- ),
- ),
- );
- self::$post = self::factory()->post->create_and_get( $args );
- wp_set_post_terms( self::$post->ID, get_stylesheet(), 'wp_theme' );
-
- // Set up template part post.
- $template_part_args = array(
- 'post_type' => 'wp_template_part',
- 'post_name' => 'my_template_part',
- 'post_title' => 'My Template Part',
- 'post_content' => 'Content',
- 'post_excerpt' => 'Description of my template part',
- 'tax_input' => array(
- 'wp_theme' => array(
- get_stylesheet(),
- ),
- 'wp_template_part_area' => array(
- WP_TEMPLATE_PART_AREA_HEADER,
- ),
- ),
- );
- self::$template_part_post = self::factory()->post->create_and_get( $template_part_args );
- wp_set_post_terms( self::$template_part_post->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' );
- wp_set_post_terms( self::$template_part_post->ID, get_stylesheet(), 'wp_theme' );
- }
-
- public static function wpTearDownAfterClass() {
- wp_delete_post( self::$post->ID );
- }
-
- function test_build_block_template_result_from_file() {
- $template = _build_block_template_result_from_file(
- array(
- 'slug' => 'single',
- 'path' => __DIR__ . '/fixtures/template.html',
- ),
- 'wp_template'
- );
-
- $this->assertEquals( get_stylesheet() . '//single', $template->id );
- $this->assertEquals( get_stylesheet(), $template->theme );
- $this->assertEquals( 'single', $template->slug );
- $this->assertEquals( 'publish', $template->status );
- $this->assertEquals( 'theme', $template->source );
- $this->assertEquals( 'Single Post', $template->title );
- $this->assertEquals( 'Template used to display a single blog post.', $template->description );
- $this->assertEquals( 'wp_template', $template->type );
-
- // Test template parts.
- $template_part = _build_block_template_result_from_file(
- array(
- 'slug' => 'header',
- 'path' => __DIR__ . '/fixtures/template.html',
- 'area' => WP_TEMPLATE_PART_AREA_HEADER,
- ),
- 'wp_template_part'
- );
- $this->assertEquals( get_stylesheet() . '//header', $template_part->id );
- $this->assertEquals( get_stylesheet(), $template_part->theme );
- $this->assertEquals( 'header', $template_part->slug );
- $this->assertEquals( 'publish', $template_part->status );
- $this->assertEquals( 'theme', $template_part->source );
- $this->assertEquals( 'header', $template_part->title );
- $this->assertEquals( '', $template_part->description );
- $this->assertEquals( 'wp_template_part', $template_part->type );
- $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );
- }
-
- function test_build_block_template_result_from_post() {
- $template = _build_block_template_result_from_post(
- self::$post,
- 'wp_template'
- );
-
- $this->assertNotWPError( $template );
- $this->assertEquals( get_stylesheet() . '//my_template', $template->id );
- $this->assertEquals( get_stylesheet(), $template->theme );
- $this->assertEquals( 'my_template', $template->slug );
- $this->assertEquals( 'publish', $template->status );
- $this->assertEquals( 'custom', $template->source );
- $this->assertEquals( 'My Template', $template->title );
- $this->assertEquals( 'Description of my template', $template->description );
- $this->assertEquals( 'wp_template', $template->type );
-
- // Test template parts.
- $template_part = _build_block_template_result_from_post(
- self::$template_part_post,
- 'wp_template_part'
- );
- $this->assertNotWPError( $template_part );
- $this->assertEquals( get_stylesheet() . '//my_template_part', $template_part->id );
- $this->assertEquals( get_stylesheet(), $template_part->theme );
- $this->assertEquals( 'my_template_part', $template_part->slug );
- $this->assertEquals( 'publish', $template_part->status );
- $this->assertEquals( 'custom', $template_part->source );
- $this->assertEquals( 'My Template Part', $template_part->title );
- $this->assertEquals( 'Description of my template part', $template_part->description );
- $this->assertEquals( 'wp_template_part', $template_part->type );
- $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );
- }
-
- function test_inject_theme_attribute_in_block_template_content() {
- $theme = get_stylesheet();
- $content_without_theme_attribute = '';
- $template_content = _inject_theme_attribute_in_block_template_content(
- $content_without_theme_attribute,
- $theme
- );
- $expected = sprintf(
- '',
- get_stylesheet()
- );
- $this->assertEquals( $expected, $template_content );
-
- $content_without_theme_attribute_nested = '';
- $template_content = _inject_theme_attribute_in_block_template_content(
- $content_without_theme_attribute_nested,
- $theme
- );
- $expected = sprintf(
- '',
- get_stylesheet()
- );
- $this->assertEquals( $expected, $template_content );
-
- // Does not inject theme when there is an existing theme attribute.
- $content_with_existing_theme_attribute = '';
- $template_content = _inject_theme_attribute_in_block_template_content(
- $content_with_existing_theme_attribute,
- $theme
- );
- $this->assertEquals( $content_with_existing_theme_attribute, $template_content );
-
- // Does not inject theme when there is no template part.
- $content_with_no_template_part = '';
- $template_content = _inject_theme_attribute_in_block_template_content(
- $content_with_no_template_part,
- $theme
- );
- $this->assertEquals( $content_with_no_template_part, $template_content );
- }
-
- function test_remove_theme_attribute_in_block_template_content() {
- $content_with_existing_theme_attribute = '';
- $template_content = _remove_theme_attribute_in_block_template_content( $content_with_existing_theme_attribute );
- $expected = '';
- $this->assertEquals( $expected, $template_content );
-
- $content_with_existing_theme_attribute_nested = '';
- $template_content = _remove_theme_attribute_in_block_template_content( $content_with_existing_theme_attribute_nested );
- $expected = '';
- $this->assertEquals( $expected, $template_content );
-
- // Does not modify content when there is no existing theme attribute.
- $content_without_theme_attribute = '';
- $template_content = _remove_theme_attribute_in_block_template_content( $content_without_theme_attribute );
- $this->assertEquals( $content_without_theme_attribute, $template_content );
-
- // Does not remove theme when there is no template part.
- $content_with_no_template_part = '';
- $template_content = _remove_theme_attribute_in_block_template_content( $content_with_no_template_part );
- $this->assertEquals( $content_with_no_template_part, $template_content );
- }
-
- /**
- * Should retrieve the template from the theme files.
- */
- function test_get_block_template_from_file() {
- $id = get_stylesheet() . '//' . 'index';
- $template = gutenberg_get_block_template( $id, 'wp_template' );
- $this->assertEquals( $id, $template->id );
- $this->assertEquals( get_stylesheet(), $template->theme );
- $this->assertEquals( 'index', $template->slug );
- $this->assertEquals( 'publish', $template->status );
- $this->assertEquals( 'theme', $template->source );
- $this->assertEquals( 'wp_template', $template->type );
-
- // Test template parts.
- $id = get_stylesheet() . '//' . 'header';
- $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 );
- $this->assertEquals( 'publish', $template->status );
- $this->assertEquals( 'theme', $template->source );
- $this->assertEquals( 'wp_template_part', $template->type );
- $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template->area );
- }
-
- /**
- * Should retrieve the template from the CPT.
- */
- function test_get_block_template_from_post() {
- $id = get_stylesheet() . '//' . 'my_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 );
- $this->assertEquals( 'publish', $template->status );
- $this->assertEquals( 'custom', $template->source );
- $this->assertEquals( 'wp_template', $template->type );
-
- // Test template parts.
- $id = get_stylesheet() . '//' . 'my_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 );
- $this->assertEquals( 'publish', $template->status );
- $this->assertEquals( 'custom', $template->source );
- $this->assertEquals( 'wp_template_part', $template->type );
- $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template->area );
- }
-
- /**
- * Should retrieve block templates (file and CPT)
- */
- function test_gutenberg_get_block_templates() {
- function get_template_ids( $templates ) {
- return array_map(
- function( $template ) {
- return $template->id;
- },
- $templates
- );
- }
-
- // All results.
- $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.
- $this->assertContains( get_stylesheet() . '//' . 'my_template', $template_ids );
- $this->assertContains( get_stylesheet() . '//' . 'index', $template_ids );
-
- // Filter by slug.
- $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 = 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 = gutenberg_get_block_templates( array( 'area' => WP_TEMPLATE_PART_AREA_HEADER ), 'wp_template_part' );
- $template_ids = get_template_ids( $templates );
- $this->assertEquals(
- array(
- get_stylesheet() . '//' . 'my_template_part',
- get_stylesheet() . '//' . 'header',
- ),
- $template_ids
- );
- }
-
- /**
- * Should flatten nested blocks
- */
- function test_flatten_blocks() {
- $content_template_part_inside_group = '';
- $blocks = parse_blocks( $content_template_part_inside_group );
- $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 = _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 = _flatten_blocks( $blocks );
- $expected = array( $blocks[0] );
- $this->assertEquals( $expected, $actual );
- }
-
- /**
- * Should generate block templates export file.
- */
- function test_wp_generate_block_templates_export_file() {
- $filename = wp_generate_block_templates_export_file();
- $this->assertFileExists( $filename, 'zip file is created at the specified path' );
- $this->assertTrue( filesize( $filename ) > 0, 'zip file is larger than 0 bytes' );
-
- // Open ZIP file and make sure the directories exist.
- $zip = new ZipArchive();
- $zip->open( $filename );
- $has_theme_dir = $zip->locateName( 'theme/' ) !== false;
- $has_block_templates_dir = $zip->locateName( 'theme/templates/' ) !== false;
- $has_block_template_parts_dir = $zip->locateName( 'theme/parts/' ) !== false;
- $this->assertTrue( $has_theme_dir, 'theme directory exists' );
- $this->assertTrue( $has_block_templates_dir, 'theme/templates directory exists' );
- $this->assertTrue( $has_block_template_parts_dir, 'theme/parts directory exists' );
-
- // ZIP file contains at least one HTML file.
- $has_html_files = false;
- $num_files = $zip->numFiles; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
- for ( $i = 0; $i < $num_files; $i++ ) {
- $filename = $zip->getNameIndex( $i );
- if ( '.html' === substr( $filename, -5 ) ) {
- $has_html_files = true;
- break;
- }
- }
- $this->assertTrue( $has_html_files, 'contains at least one html file' );
- }
-}
diff --git a/phpunit/class-gutenberg-rest-global-styles-controller-test.php b/phpunit/class-gutenberg-rest-global-styles-controller-test.php
index 52adb2ffee783a..de782a1657f655 100644
--- a/phpunit/class-gutenberg-rest-global-styles-controller-test.php
+++ b/phpunit/class-gutenberg-rest-global-styles-controller-test.php
@@ -33,7 +33,6 @@ public function set_up() {
* @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
*/
public static function wpSetupBeforeClass( $factory ) {
- gutenberg_register_wp_theme_taxonomy();
self::$admin_id = $factory->user->create(
array(
'role' => 'administrator',
diff --git a/phpunit/class-gutenberg-rest-template-controller-test.php b/phpunit/class-gutenberg-rest-template-controller-test.php
deleted file mode 100644
index 34d2ed168122f1..00000000000000
--- a/phpunit/class-gutenberg-rest-template-controller-test.php
+++ /dev/null
@@ -1,374 +0,0 @@
-user->create(
- array(
- 'role' => 'administrator',
- )
- );
- }
-
- public function test_register_routes() {
- $routes = rest_get_server()->get_routes();
- $this->assertArrayHasKey( '/wp/v2/templates', $routes );
- $this->assertArrayHasKey( '/wp/v2/templates/(?P[\/\w-]+)', $routes );
-
- $this->assertArrayHasKey( '/wp/v2/template-parts', $routes );
- $this->assertArrayHasKey( '/wp/v2/template-parts/(?P[\/\w-]+)', $routes );
- }
-
- public function test_context_param() {
- // TODO: Implement test_context_param() method.
- $this->markTestIncomplete();
- }
-
- public function test_get_items() {
- function find_and_normalize_template_by_id( $templates, $id ) {
- foreach ( $templates as $template ) {
- if ( $template['id'] === $id ) {
- unset( $template['content'] );
- unset( $template['_links'] );
- return $template;
- }
- }
-
- return null;
- }
-
- wp_set_current_user( 0 );
- $request = new WP_REST_Request( 'GET', '/wp/v2/templates' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_cannot_manage_templates', $response, 401 );
-
- wp_set_current_user( self::$admin_id );
- $request = new WP_REST_Request( 'GET', '/wp/v2/templates' );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
-
- $this->assertEquals(
- array(
- 'id' => 'tt1-blocks//index',
- 'theme' => 'tt1-blocks',
- 'slug' => 'index',
- 'title' => array(
- 'raw' => 'Index',
- 'rendered' => 'Index',
- ),
- 'description' => 'The default template used when no other template is available. This is a required template in WordPress.',
- 'status' => 'publish',
- 'source' => 'theme',
- 'type' => 'wp_template',
- 'wp_id' => null,
- 'has_theme_file' => true,
- 'is_custom' => false,
- 'origin' => null,
- 'author' => 0,
- ),
- find_and_normalize_template_by_id( $data, 'tt1-blocks//index' )
- );
-
- // Test template parts.
- $request = new WP_REST_Request( 'GET', '/wp/v2/template-parts' );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
-
- $this->assertEquals(
- array(
- 'id' => 'tt1-blocks//header',
- 'theme' => 'tt1-blocks',
- 'slug' => 'header',
- 'title' => array(
- 'raw' => 'header',
- 'rendered' => 'header',
- ),
- 'description' => '',
- 'status' => 'publish',
- 'source' => 'theme',
- 'type' => 'wp_template_part',
- 'wp_id' => null,
- 'area' => WP_TEMPLATE_PART_AREA_HEADER,
- 'has_theme_file' => true,
- 'origin' => null,
- 'author' => 0,
- ),
- find_and_normalize_template_by_id( $data, 'tt1-blocks//header' )
- );
- }
-
- public function test_get_item() {
- wp_set_current_user( self::$admin_id );
- $request = new WP_REST_Request( 'GET', '/wp/v2/templates/tt1-blocks//index' );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- unset( $data['content'] );
- unset( $data['_links'] );
-
- $this->assertEquals(
- array(
- 'id' => 'tt1-blocks//index',
- 'theme' => 'tt1-blocks',
- 'slug' => 'index',
- 'title' => array(
- 'raw' => 'Index',
- 'rendered' => 'Index',
- ),
- 'description' => 'The default template used when no other template is available. This is a required template in WordPress.',
- 'status' => 'publish',
- 'source' => 'theme',
- 'type' => 'wp_template',
- 'wp_id' => null,
- 'has_theme_file' => true,
- 'is_custom' => false,
- 'origin' => null,
- 'author' => 0,
- ),
- $data
- );
-
- // Test template parts.
- $request = new WP_REST_Request( 'GET', '/wp/v2/template-parts/tt1-blocks//header' );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- unset( $data['content'] );
- unset( $data['_links'] );
- $this->assertEquals(
- array(
- 'id' => 'tt1-blocks//header',
- 'theme' => 'tt1-blocks',
- 'slug' => 'header',
- 'title' => array(
- 'raw' => 'header',
- 'rendered' => 'header',
- ),
- 'description' => '',
- 'status' => 'publish',
- 'source' => 'theme',
- 'type' => 'wp_template_part',
- 'wp_id' => null,
- 'area' => WP_TEMPLATE_PART_AREA_HEADER,
- 'has_theme_file' => true,
- 'origin' => null,
- 'author' => 0,
- ),
- $data
- );
- }
-
- /**
- * Ticket 54507
- *
- * @dataProvider get_template_endpoint_urls
- */
- public function test_get_item_works_with_a_single_slash( $endpoint_url ) {
- wp_set_current_user( self::$admin_id );
- $request = new WP_REST_Request( 'GET', $endpoint_url );
- $response = rest_get_server()->dispatch( $request );
-
- $data = $response->get_data();
- unset( $data['content'] );
- unset( $data['_links'] );
-
- $this->assertEquals(
- array(
- 'id' => 'tt1-blocks//index',
- 'theme' => 'tt1-blocks',
- 'slug' => 'index',
- 'title' => array(
- 'raw' => 'Index',
- 'rendered' => 'Index',
- ),
- 'description' => 'The default template used when no other template is available. This is a required template in WordPress.',
- 'status' => 'publish',
- 'source' => 'theme',
- 'type' => 'wp_template',
- 'wp_id' => null,
- 'has_theme_file' => true,
- 'is_custom' => false,
- 'origin' => null,
- 'author' => 0,
- ),
- $data
- );
- }
-
- /**
- *
- */
- public function get_template_endpoint_urls() {
- return array(
- array( '/wp/v2/templates/tt1-blocks/index' ),
- array( '/wp/v2/templates/tt1-blocks//index' ),
- );
- }
-
- /**
- * Ticket 54507
- *
- * @dataProvider get_template_ids_to_sanitize
- */
- public function test_sanitize_template_id( $input_id, $sanitized_id ) {
- $endpoint = new Gutenberg_REST_Templates_Controller( 'wp_template' );
- $this->assertEquals(
- $sanitized_id,
- $endpoint->_sanitize_template_id( $input_id )
- );
- }
-
- /**
- *
- */
- public function get_template_ids_to_sanitize() {
- return array(
- array( 'tt1-blocks/index', 'tt1-blocks//index' ),
- array( 'tt1-blocks//index', 'tt1-blocks//index' ),
-
- array( 'theme-experiments/tt1-blocks/index', 'theme-experiments/tt1-blocks//index' ),
- array( 'theme-experiments/tt1-blocks//index', 'theme-experiments/tt1-blocks//index' ),
- );
- }
-
- public function test_create_item() {
- wp_set_current_user( self::$admin_id );
- $request = new WP_REST_Request( 'POST', '/wp/v2/templates' );
- $request->set_body_params(
- array(
- 'slug' => 'my_custom_template',
- 'title' => 'My Template',
- 'description' => 'Just a description',
- 'content' => 'Content',
- )
- );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- unset( $data['_links'] );
- unset( $data['wp_id'] );
-
- $this->assertEquals(
- array(
- 'id' => 'tt1-blocks//my_custom_template',
- 'theme' => 'tt1-blocks',
- 'slug' => 'my_custom_template',
- 'title' => array(
- 'raw' => 'My Template',
- 'rendered' => 'My Template',
- ),
- 'description' => 'Just a description',
- 'status' => 'publish',
- 'source' => 'custom',
- 'type' => 'wp_template',
- 'content' => array(
- 'raw' => 'Content',
- ),
- 'has_theme_file' => false,
- 'is_custom' => true,
- 'origin' => null,
- 'author' => self::$admin_id,
- ),
- $data
- );
-
- // Test template parts.
- $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts' );
- $request->set_body_params(
- array(
- 'slug' => 'my_custom_template_part',
- 'title' => 'My Template Part',
- 'description' => 'Just a description of a template part',
- 'content' => 'Content',
- 'area' => 'header',
- )
- );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- unset( $data['_links'] );
- unset( $data['wp_id'] );
-
- $this->assertEquals(
- array(
- 'id' => 'tt1-blocks//my_custom_template_part',
- 'theme' => 'tt1-blocks',
- 'slug' => 'my_custom_template_part',
- 'title' => array(
- 'raw' => 'My Template Part',
- 'rendered' => 'My Template Part',
- ),
- 'description' => 'Just a description of a template part',
- 'status' => 'publish',
- 'source' => 'custom',
- 'type' => 'wp_template_part',
- 'content' => array(
- 'raw' => 'Content',
- ),
- 'area' => 'header',
- 'has_theme_file' => false,
- 'origin' => null,
- 'author' => self::$admin_id,
- ),
- $data
- );
- }
-
- public function test_update_item() {
- wp_set_current_user( self::$admin_id );
- $request = new WP_REST_Request( 'PUT', '/wp/v2/templates/tt1-blocks//index' );
- $request->set_body_params(
- array(
- 'title' => 'My new Index Title',
- )
- );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $this->assertEquals( 'My new Index Title', $data['title']['raw'] );
- $this->assertEquals( 'custom', $data['source'] );
-
- // Test template parts.
- $request = new WP_REST_Request( 'PUT', '/wp/v2/template-parts/tt1-blocks//header' );
- $request->set_body_params(
- array(
- 'area' => 'something unsupported',
- )
- );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $this->assertEquals( WP_TEMPLATE_PART_AREA_UNCATEGORIZED, $data['area'] );
- $this->assertEquals( 'custom', $data['source'] );
- }
-
- public function test_delete_item() {
- wp_set_current_user( self::$admin_id );
- $request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/justrandom//template' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_template_not_found', $response, 404 );
-
- wp_set_current_user( self::$admin_id );
- $request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/tt1-blocks//single' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_invalid_template', $response, 400 );
- }
-
- public function test_prepare_item() {
- // TODO: Implement test_prepare_item() method.
- $this->markTestIncomplete();
- }
-
- public function test_get_item_schema() {
- // TODO: Implement test_get_item_schema() method.
- $this->markTestIncomplete();
- }
-}