From f6b386188f49ad3f63e09642ab1376bf9f25e36e Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Thu, 25 Nov 2021 17:31:18 +0800 Subject: [PATCH 1/9] Use core version of template and template part post types and REST endpoints --- lib/full-site-editing/template-parts.php | 6 ++++++ lib/full-site-editing/templates.php | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index 979a357a083dfb..5f69fe375098c0 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -13,6 +13,12 @@ function gutenberg_register_template_part_post_type() { return; } + // If the post type has already been registered (by WordPress core), skip + // registration. + if ( post_type_exists( 'wp_template_part' ) ) { + return; + } + $labels = array( 'name' => __( 'Template Parts', 'gutenberg' ), 'singular_name' => __( 'Template Part', 'gutenberg' ), diff --git a/lib/full-site-editing/templates.php b/lib/full-site-editing/templates.php index f20e5f5e4c40b8..523d327360ba39 100644 --- a/lib/full-site-editing/templates.php +++ b/lib/full-site-editing/templates.php @@ -13,6 +13,12 @@ function gutenberg_register_template_post_type() { return; } + // If the post type has already been registered (by WordPress core), skip + // registration. + if ( post_type_exists( 'wp_template' ) ) { + return; + } + $labels = array( 'name' => __( 'Templates', 'gutenberg' ), 'singular_name' => __( 'Template', 'gutenberg' ), From d81854126a4bbb12eedbc8baac336c608b28f790 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 1 Dec 2021 12:28:48 +0800 Subject: [PATCH 2/9] Move files and add explanation --- .../wordpress-5.9}/template-parts.php | 12 ++++++++---- .../wordpress-5.9}/templates.php | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) rename lib/{full-site-editing => compat/wordpress-5.9}/template-parts.php (95%) rename lib/{full-site-editing => compat/wordpress-5.9}/templates.php (97%) diff --git a/lib/full-site-editing/template-parts.php b/lib/compat/wordpress-5.9/template-parts.php similarity index 95% rename from lib/full-site-editing/template-parts.php rename to lib/compat/wordpress-5.9/template-parts.php index 5f69fe375098c0..40510fd8bca13d 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/compat/wordpress-5.9/template-parts.php @@ -2,6 +2,12 @@ /** * Block template part functions. * + * This is a temporary compatibility fix for WordPress 5.8.x, which is missing + * some features for template parts that are present in 5.9. + * + * Once 5.9 is the minimum supported WordPress version for the Gutenberg + * plugin, this shim can be removed. + * * @package gutenberg */ @@ -9,13 +15,11 @@ * Registers block editor 'wp_template_part' post type. */ function gutenberg_register_template_part_post_type() { - if ( ! gutenberg_supports_block_templates() ) { + if ( post_type_exists( 'wp_template_part' ) ) { return; } - // If the post type has already been registered (by WordPress core), skip - // registration. - if ( post_type_exists( 'wp_template_part' ) ) { + if ( ! gutenberg_supports_block_templates() ) { return; } diff --git a/lib/full-site-editing/templates.php b/lib/compat/wordpress-5.9/templates.php similarity index 97% rename from lib/full-site-editing/templates.php rename to lib/compat/wordpress-5.9/templates.php index 523d327360ba39..ed1040bf36d8ab 100644 --- a/lib/full-site-editing/templates.php +++ b/lib/compat/wordpress-5.9/templates.php @@ -2,6 +2,12 @@ /** * Block template functions. * + * This is a temporary compatibility fix for WordPress 5.8.x, which is missing + * some features for templates that are present in 5.9. + * + * Once 5.9 is the minimum supported WordPress version for the Gutenberg + * plugin, this shim can be removed. + * * @package gutenberg */ @@ -9,13 +15,11 @@ * Registers block editor 'wp_template' post type. */ function gutenberg_register_template_post_type() { - if ( ! gutenberg_supports_block_templates() ) { + if ( post_type_exists( 'wp_template' ) ) { return; } - // If the post type has already been registered (by WordPress core), skip - // registration. - if ( post_type_exists( 'wp_template' ) ) { + if ( ! gutenberg_supports_block_templates() ) { return; } From 85f2cf7a1a6eaf3b3385c30d3802f9797774d0ac Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 1 Dec 2021 12:54:50 +0800 Subject: [PATCH 3/9] Only register gutenberg version of templates and template parts when WP5.8.x is active --- lib/compat/wordpress-5.9/template-parts.php | 273 +++++------ lib/compat/wordpress-5.9/templates.php | 490 ++++++++++---------- 2 files changed, 382 insertions(+), 381 deletions(-) diff --git a/lib/compat/wordpress-5.9/template-parts.php b/lib/compat/wordpress-5.9/template-parts.php index 40510fd8bca13d..fd3587cdbf67e1 100644 --- a/lib/compat/wordpress-5.9/template-parts.php +++ b/lib/compat/wordpress-5.9/template-parts.php @@ -11,157 +11,158 @@ * @package gutenberg */ -/** - * Registers block editor 'wp_template_part' post type. - */ -function gutenberg_register_template_part_post_type() { - if ( post_type_exists( 'wp_template_part' ) ) { - return; - } - if ( ! gutenberg_supports_block_templates() ) { - return; - } - - $labels = array( - 'name' => __( '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' ), - ); +// Only run any of the code in this file if the version is less than 5.9. +// wp_is_block_template_theme was introduced in 5.9 +if ( ! function_exists( 'wp_is_block_template_theme' ) ) { + /** + * Registers block editor 'wp_template_part' post type. + */ + function gutenberg_register_template_part_post_type() { + if ( ! gutenberg_supports_block_templates() ) { + return; + } - $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', - ), - ); + $labels = array( + 'name' => __( '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' ), + ); - register_post_type( 'wp_template_part', $args ); -} -add_action( 'init', 'gutenberg_register_template_part_post_type' ); + $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', + ), + ); -/** - * Registers the 'wp_template_part_area' taxonomy. - */ -function gutenberg_register_wp_template_part_area_taxonomy() { - if ( ! gutenberg_supports_block_templates() ) { - return; + register_post_type( 'wp_template_part', $args ); } + add_action( 'init', 'gutenberg_register_template_part_post_type' ); - 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' ); + /** + * Registers the 'wp_template_part_area' taxonomy. + */ + function gutenberg_register_wp_template_part_area_taxonomy() { + if ( ! gutenberg_supports_block_templates() ) { + return; + } -/** - * 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; + 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' ); - 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; + /** + * 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' ); + 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' ); + // 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; - } + /** + * 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; - } + $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(), - ) - ); - } + 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' ); + $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' ); + 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 index ed1040bf36d8ab..838e1f2c6c8c8a 100644 --- a/lib/compat/wordpress-5.9/templates.php +++ b/lib/compat/wordpress-5.9/templates.php @@ -11,282 +11,282 @@ * @package gutenberg */ -/** - * Registers block editor 'wp_template' post type. - */ -function gutenberg_register_template_post_type() { - if ( post_type_exists( 'wp_template' ) ) { - return; - } - - if ( ! gutenberg_supports_block_templates() ) { - return; - } - - $labels = array( - 'name' => __( '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' ); +// Only run any of the code in this file if the version is less than 5.9. +// wp_is_block_template_theme was introduced in 5.9 +if ( ! function_exists( 'wp_is_block_template_theme' ) ) { + /** + * Registers block editor 'wp_template' post type. + */ + function gutenberg_register_template_post_type() { + if ( ! gutenberg_supports_block_templates() ) { + return; + } -/** - * 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; - } + $labels = array( + 'name' => __( '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' ), + ); - 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' ), + $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', ), - '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']; + register_post_type( 'wp_template', $args ); } + add_action( 'init', 'gutenberg_register_template_post_type' ); - 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; + /** + * 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; } - } -} -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( + register_taxonomy( + 'wp_theme', + array( 'wp_template', 'wp_template_part', 'wp_global_styles' ), array( - 'ID' => $post_id, - 'post_name' => 'custom_slug_' . uniqid(), + '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' ); - $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' ); + /** + * 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']; + } -/** - * 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; + return $allcaps; } + add_filter( 'user_has_cap', 'gutenberg_grant_template_caps' ); - // Early exit if not a block template. - global $_wp_current_template_content; - if ( ! $_wp_current_template_content ) { - return; + /** + * 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' ); - - - - - '; + + // Inject the skip link. + sibling.parentElement.insertBefore( skipLink, sibling ); + }() ); + + Date: Wed, 1 Dec 2021 13:36:38 +0800 Subject: [PATCH 4/9] Rename WP_Block_Template to Gutenberg_Block_Template so that 5.8 receives improvements to it --- .../wordpress-5.9/class-gutenberg-block-template.php} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename lib/{full-site-editing/class-wp-block-template.php => compat/wordpress-5.9/class-gutenberg-block-template.php} (94%) 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 @@ Date: Wed, 1 Dec 2021 13:36:51 +0800 Subject: [PATCH 5/9] Load moved files --- lib/load.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/load.php b/lib/load.php index b34b9c1acbeb92..a34d0ec08db951 100644 --- a/lib/load.php +++ b/lib/load.php @@ -98,11 +98,11 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-5.9/get-global-styles-and-settings.php'; require __DIR__ . '/compat/wordpress-5.9/json-file-decode.php'; require __DIR__ . '/compat/wordpress-5.9/translate-settings-using-i18n-schema.php'; +require __DIR__ . '/compat/wordpress-5.9/class-wp-block-template.php'; +require __DIR__ . '/compat/wordpress-5.9/templates.php'; +require __DIR__ . '/compat/wordpress-5.9/template-parts.php'; require __DIR__ . '/editor-settings.php'; -if ( ! class_exists( 'WP_Block_Template' ) ) { - require __DIR__ . '/full-site-editing/class-wp-block-template.php'; -} // These are used by some FSE features // as well as global styles. @@ -115,8 +115,6 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/full-site-editing/default-template-types.php'; require __DIR__ . '/full-site-editing/templates-utils.php'; require __DIR__ . '/full-site-editing/page-templates.php'; -require __DIR__ . '/full-site-editing/templates.php'; -require __DIR__ . '/full-site-editing/template-parts.php'; require __DIR__ . '/full-site-editing/template-loader.php'; require __DIR__ . '/full-site-editing/edit-site-page.php'; require __DIR__ . '/compat/wordpress-5.9/default-theme-supports.php'; From b48f64338833bc5fd979b9f2e2c6fc1a2b113ec0 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 1 Dec 2021 13:37:17 +0800 Subject: [PATCH 6/9] Update references and sync some code --- .../wordpress-5.9/block-template-utils.php | 37 ++++++++----------- ...ss-gutenberg-rest-templates-controller.php | 2 +- lib/full-site-editing/template-loader.php | 2 +- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index 8a2aee344f8ad5..14021fba1b06ec 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() @@ -474,12 +473,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 +488,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 +531,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 +551,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 +606,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 +709,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 +727,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 +737,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 +784,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 +799,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 +809,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 +844,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/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php b/lib/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php index be6116c6c2a55b..3f8cf22e3a650a 100644 --- a/lib/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php +++ b/lib/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php @@ -452,7 +452,7 @@ protected function prepare_item_for_database( $request ) { /** * Prepare a single template output for response * - * @param WP_Block_Template $template Template instance. + * @param Gutenberg_Block_Template $template Template instance. * @param WP_REST_Request $request Request object. * * @return WP_REST_Response $data diff --git a/lib/full-site-editing/template-loader.php b/lib/full-site-editing/template-loader.php index 9f29783aa34895..40953108f37ec5 100644 --- a/lib/full-site-editing/template-loader.php +++ b/lib/full-site-editing/template-loader.php @@ -111,7 +111,7 @@ function gutenberg_override_query_template( $template, $type, array $templates ) * * @param string $template_type The current template type. * @param string[] $template_hierarchy (optional) The current template hierarchy, ordered by priority. - * @return null|WP_Block_Template A block template if found. Null if not. + * @return null|Gutenberg_Block_Template A block template if found. Null if not. */ function gutenberg_resolve_template( $template_type, $template_hierarchy ) { if ( ! $template_type ) { From fbf6265bb4034542ff4f36d9dcb3ac157bccbdf9 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 1 Dec 2021 13:42:07 +0800 Subject: [PATCH 7/9] Fix filename --- lib/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/load.php b/lib/load.php index a34d0ec08db951..75afd120772ef8 100644 --- a/lib/load.php +++ b/lib/load.php @@ -98,7 +98,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-5.9/get-global-styles-and-settings.php'; require __DIR__ . '/compat/wordpress-5.9/json-file-decode.php'; require __DIR__ . '/compat/wordpress-5.9/translate-settings-using-i18n-schema.php'; -require __DIR__ . '/compat/wordpress-5.9/class-wp-block-template.php'; +require __DIR__ . '/compat/wordpress-5.9/class-gutenberg-block-template.php'; require __DIR__ . '/compat/wordpress-5.9/templates.php'; require __DIR__ . '/compat/wordpress-5.9/template-parts.php'; require __DIR__ . '/editor-settings.php'; From 6d15485deb4bc07d291397da3d12d82cbdbfd284 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 1 Dec 2021 14:47:56 +0800 Subject: [PATCH 8/9] Remove templates tests since Gutenberg now defaults to using the WP core implementation --- phpunit/class-block-templates-test.php | 362 ----------------- ...utenberg-rest-template-controller-test.php | 374 ------------------ 2 files changed, 736 deletions(-) delete mode 100644 phpunit/class-block-templates-test.php delete mode 100644 phpunit/class-gutenberg-rest-template-controller-test.php diff --git a/phpunit/class-block-templates-test.php b/phpunit/class-block-templates-test.php deleted file mode 100644 index bf57c61c2016ed..00000000000000 --- a/phpunit/class-block-templates-test.php +++ /dev/null @@ -1,362 +0,0 @@ - '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-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(); - } -} From 293a96a10faa209c01bef77d52e6de86f4e6717f Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 1 Dec 2021 15:33:25 +0800 Subject: [PATCH 9/9] Linting and test fixes --- lib/compat/wordpress-5.9/block-template-utils.php | 1 - .../class-gutenberg-rest-templates-controller.php | 2 +- lib/compat/wordpress-5.9/template-parts.php | 3 +-- lib/compat/wordpress-5.9/templates.php | 2 +- phpunit/class-gutenberg-rest-global-styles-controller-test.php | 1 - 5 files changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index 14021fba1b06ec..2ecc53b121adeb 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -462,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 ); diff --git a/lib/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php b/lib/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php index 3f8cf22e3a650a..d909ffd2526254 100644 --- a/lib/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php +++ b/lib/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php @@ -453,7 +453,7 @@ protected function prepare_item_for_database( $request ) { * Prepare a single template output for response * * @param Gutenberg_Block_Template $template Template instance. - * @param WP_REST_Request $request Request object. + * @param WP_REST_Request $request Request object. * * @return WP_REST_Response $data */ diff --git a/lib/compat/wordpress-5.9/template-parts.php b/lib/compat/wordpress-5.9/template-parts.php index fd3587cdbf67e1..e25a1f80014cb8 100644 --- a/lib/compat/wordpress-5.9/template-parts.php +++ b/lib/compat/wordpress-5.9/template-parts.php @@ -11,9 +11,8 @@ * @package gutenberg */ - // Only run any of the code in this file if the version is less than 5.9. -// wp_is_block_template_theme was introduced in 5.9 +// wp_is_block_template_theme was introduced in 5.9. if ( ! function_exists( 'wp_is_block_template_theme' ) ) { /** * Registers block editor 'wp_template_part' post type. diff --git a/lib/compat/wordpress-5.9/templates.php b/lib/compat/wordpress-5.9/templates.php index 838e1f2c6c8c8a..819e6474abfd53 100644 --- a/lib/compat/wordpress-5.9/templates.php +++ b/lib/compat/wordpress-5.9/templates.php @@ -12,7 +12,7 @@ */ // Only run any of the code in this file if the version is less than 5.9. -// wp_is_block_template_theme was introduced in 5.9 +// wp_is_block_template_theme was introduced in 5.9. if ( ! function_exists( 'wp_is_block_template_theme' ) ) { /** * Registers block editor 'wp_template' post type. 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',