-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Site Editor: Fix routing for Classic themes using block-based template parts #48343
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
/** | ||
* Admin menu overrides for WP 6.2. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Updates "Template Parts" menu URL to include `path` query argument. | ||
* | ||
* Note: Remove when the minimum required WP version is 6.2. | ||
* No need to backport in core. Changes are applied in wp-admin/menu.php. | ||
*/ | ||
function gutenberg_update_template_parts_menu_url() { | ||
if ( wp_is_block_theme() ) { | ||
return; | ||
} | ||
|
||
if ( ! current_theme_supports( 'block-template-parts' ) ) { | ||
return; | ||
} | ||
|
||
global $submenu; | ||
if ( ! isset( $submenu['themes.php'] ) ) { | ||
return; | ||
} | ||
|
||
foreach ( $submenu['themes.php'] as $index => $menu_item ) { | ||
if ( str_contains( $menu_item[2], 'site-editor.php?postType=wp_template_part' ) && ! str_contains( $menu_item[2], 'path=' ) ) { | ||
$submenu['themes.php'][ $index ][2] = 'site-editor.php?postType=wp_template_part&path=/wp_template_part/all'; | ||
break; | ||
} | ||
} | ||
} | ||
add_action( 'admin_menu', 'gutenberg_update_template_parts_menu_url' ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); | ||
|
||
test.describe( 'Template Parts for Classic themes', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great idea to have an e2e test for this, we don't check this flow often manually. |
||
test.beforeAll( async ( { requestUtils } ) => { | ||
await requestUtils.activateTheme( 'emptyhybrid' ); | ||
} ); | ||
|
||
test.afterAll( async ( { requestUtils } ) => { | ||
await requestUtils.activateTheme( 'twentytwentyone' ); | ||
} ); | ||
|
||
test( 'can access template parts list page', async ( { admin, page } ) => { | ||
await admin.visitAdminPage( | ||
'site-editor.php', | ||
'postType=wp_template_part&path=/wp_template_part/all' | ||
); | ||
kevin940726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
await expect( | ||
page.getByRole( 'table' ).getByRole( 'link', { name: 'header' } ) | ||
).toBeVisible(); | ||
} ); | ||
|
||
test( 'can view a template part', async ( { admin, editor, page } ) => { | ||
await admin.visitAdminPage( | ||
'site-editor.php', | ||
'postType=wp_template_part&path=/wp_template_part/all' | ||
); | ||
|
||
const templatePart = page | ||
.getByRole( 'table' ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Off-topic: We should probably fix this by giving the table an accessible name in another PR 😅 . |
||
.getByRole( 'link', { name: 'header' } ); | ||
|
||
await expect( templatePart ).toBeVisible(); | ||
await templatePart.click(); | ||
|
||
await expect( | ||
page.getByRole( 'region', { name: 'Editor content' } ) | ||
).toBeVisible(); | ||
|
||
await editor.canvas.click( 'body' ); | ||
|
||
await expect( | ||
editor.canvas.getByRole( 'document', { | ||
name: 'Block: Site Title', | ||
} ) | ||
).toBeVisible(); | ||
} ); | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
if ( ! function_exists( 'emptyhybrid_support' ) ) : | ||
function emptyhybrid_support() { | ||
|
||
// Adding support for core block visual styles. | ||
add_theme_support( 'wp-block-styles' ); | ||
|
||
// Enqueue editor styles. | ||
add_editor_style( 'style.css' ); | ||
|
||
// Enable block-based template parts support. | ||
add_theme_support( 'block-template-parts' ); | ||
} | ||
add_action( 'after_setup_theme', 'emptyhybrid_support' ); | ||
endif; | ||
|
||
/** | ||
* Enqueue scripts and styles. | ||
*/ | ||
function emptyhybrid_scripts() { | ||
// Enqueue theme stylesheet. | ||
wp_enqueue_style( 'emptyhybrid-style', get_template_directory_uri() . '/style.css', array(), wp_get_theme()->get( 'Version' ) ); | ||
} | ||
add_action( 'wp_enqueue_scripts', 'emptyhybrid_scripts' ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!doctype html> | ||
<html <?php language_attributes(); ?>> | ||
<head> | ||
<meta charset="<?php bloginfo( 'charset' ); ?>" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<?php wp_head(); ?> | ||
</head> | ||
|
||
<body <?php body_class(); ?>> | ||
<?php wp_body_open(); ?> | ||
<div id="page" class="site"> | ||
<?php block_template_part( 'header' ); ?> | ||
</div> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<!-- wp:site-title /--> | ||
|
||
<!-- wp:site-tagline /--> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
Theme Name: Emptyhybrid | ||
Theme URI: https://github.com/wordpress/gutenberg/ | ||
Author: the WordPress team | ||
Description: The testing hybrid theme. | ||
Requires at least: 6.0 | ||
Tested up to: 6.2 | ||
Requires PHP: 5.6 | ||
Version: 1.0 | ||
License: GNU General Public License v2 or later | ||
License URI: http://www.gnu.org/licenses/gpl-2.0.html | ||
Text Domain: Emptyhybrid | ||
Emptytheme WordPress Theme, (C) 2021 WordPress.org | ||
Emptytheme is distributed under the terms of the GNU GPL. | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit weird to be honest, I think the ideal solution for this "mode" is to probably have its own "init function" rather than rely on the default site editor initialization function. In the long term, I'm certain this "hack" is going to bite us. By "Hack" I mean the fact that we kind of try to load a specific version of the site editor for the "template parts" screen of classic themes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's still the same site editor; we don't want some routes to be available for Classic themes.
But I agree this "hack" isn't the best way to solve this problem. I will try to explore different init methods for WP 6.3.