-
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: Use path based routing instead of query args and site-editor.php routes #67199
Changes from all commits
15f1913
edd3075
6a4cc01
937ebf0
c3bff73
2c0b7f7
3c9a42c
b811a48
5153567
bc404a3
b4d15dc
e8ec07a
a3324ac
2b9d0bd
5a51c5a
b03d304
551674f
0bce3ba
29eb798
ff1411c
e46b7ef
544fe66
97a7ee7
233fa6b
7fb975e
2e4c03d
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,3 @@ | ||
https://github.com/WordPress/wordpress-develop/pull/7903 | ||
|
||
* https://github.com/WordPress/gutenberg/pull/67199 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
/** | ||
* Updates to the site editor in 6.8. | ||
* | ||
* Adds a mandatory dashboard link and redirects old urls. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
add_filter( | ||
'block_editor_settings_all', | ||
function ( $settings ) { | ||
$settings['__experimentalDashboardLink'] = admin_url( '/' ); | ||
return $settings; | ||
} | ||
); | ||
|
||
function gutenberg_get_site_editor_redirection() { | ||
global $pagenow; | ||
if ( 'site-editor.php' !== $pagenow || isset( $_REQUEST['p'] ) || ! $_SERVER['QUERY_STRING'] ) { | ||
return false; | ||
} | ||
|
||
// The following redirects are for the new permalinks in the site editor. | ||
if ( isset( $_REQUEST['postType'] ) && 'wp_navigation' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) { | ||
return add_query_arg( array( 'p' => '/wp_navigation/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) ); | ||
} | ||
|
||
if ( isset( $_REQUEST['postType'] ) && 'wp_navigation' === $_REQUEST['postType'] && empty( $_REQUEST['postId'] ) ) { | ||
return add_query_arg( array( 'p' => '/navigation' ), remove_query_arg( 'postType' ) ); | ||
} | ||
|
||
if ( isset( $_REQUEST['path'] ) && '/wp_global_styles' === $_REQUEST['path'] ) { | ||
return add_query_arg( array( 'p' => '/styles' ), remove_query_arg( 'path' ) ); | ||
} | ||
|
||
if ( isset( $_REQUEST['postType'] ) && 'page' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) { | ||
return add_query_arg( array( 'p' => '/page' ), remove_query_arg( 'postType' ) ); | ||
} | ||
|
||
if ( isset( $_REQUEST['postType'] ) && 'page' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) { | ||
return add_query_arg( array( 'p' => '/page/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) ); | ||
} | ||
|
||
if ( isset( $_REQUEST['postType'] ) && 'wp_template' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) { | ||
return add_query_arg( array( 'p' => '/template' ), remove_query_arg( 'postType' ) ); | ||
} | ||
|
||
if ( isset( $_REQUEST['postType'] ) && 'wp_template' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) { | ||
return add_query_arg( array( 'p' => '/wp_template/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) ); | ||
} | ||
|
||
if ( isset( $_REQUEST['postType'] ) && 'wp_block' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) { | ||
return add_query_arg( array( 'p' => '/pattern' ), remove_query_arg( 'postType' ) ); | ||
} | ||
|
||
if ( isset( $_REQUEST['postType'] ) && 'wp_block' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) { | ||
return add_query_arg( array( 'p' => '/wp_block/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) ); | ||
} | ||
|
||
if ( isset( $_REQUEST['postType'] ) && 'wp_template_part' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) { | ||
return add_query_arg( array( 'p' => '/pattern' ) ); | ||
} | ||
|
||
if ( isset( $_REQUEST['postType'] ) && 'wp_template_part' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) { | ||
return add_query_arg( array( 'p' => '/wp_template_part/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) ); | ||
} | ||
|
||
// The following redirects are for backward compatibility with the old site editor URLs. | ||
if ( isset( $_REQUEST['path'] ) && '/wp_template_part/all' === $_REQUEST['path'] ) { | ||
return add_query_arg( | ||
array( | ||
'p' => '/pattern', | ||
'postType' => 'wp_template_part', | ||
), | ||
remove_query_arg( 'path' ) | ||
); | ||
} | ||
|
||
if ( isset( $_REQUEST['path'] ) && '/page' === $_REQUEST['path'] ) { | ||
return add_query_arg( array( 'p' => '/page' ), remove_query_arg( 'path' ) ); | ||
} | ||
|
||
if ( isset( $_REQUEST['path'] ) && '/wp_template' === $_REQUEST['path'] ) { | ||
return add_query_arg( array( 'p' => '/template' ), remove_query_arg( 'path' ) ); | ||
} | ||
|
||
if ( isset( $_REQUEST['path'] ) && '/patterns' === $_REQUEST['path'] ) { | ||
return add_query_arg( array( 'p' => '/pattern' ), remove_query_arg( 'path' ) ); | ||
} | ||
|
||
if ( isset( $_REQUEST['path'] ) && '/navigation' === $_REQUEST['path'] ) { | ||
return add_query_arg( array( 'p' => '/navigation' ), remove_query_arg( 'path' ) ); | ||
} | ||
|
||
return add_query_arg( array( 'p' => '/' ) ); | ||
} | ||
|
||
function gutenberg_redirect_site_editor_deprecated_urls() { | ||
$redirection = gutenberg_get_site_editor_redirection(); | ||
if ( false !== $redirection ) { | ||
wp_redirect( $redirection, 301 ); | ||
exit; | ||
} | ||
} | ||
add_action( 'admin_init', 'gutenberg_redirect_site_editor_deprecated_urls' ); | ||
|
||
/** | ||
* Filter the `wp_die_handler` to allow access to the Site Editor's new pages page | ||
* for Classic themes. | ||
* | ||
* site-editor.php's access is forbidden for hybrid/classic themes and only allowed with some very special query args (some very special pages like template parts...). | ||
* The only way to disable this protection since we're changing the urls in Gutenberg is to override the wp_die_handler. | ||
* | ||
* @param callable $default_handler The default handler. | ||
* @return callable The default handler or a custom handler. | ||
*/ | ||
function gutenberg_styles_wp_die_handler( $default_handler ) { | ||
if ( ! wp_is_block_theme() && str_contains( $_SERVER['REQUEST_URI'], 'site-editor.php' ) && isset( $_GET['p'] ) ) { | ||
return '__return_false'; | ||
} | ||
return $default_handler; | ||
} | ||
add_filter( 'wp_die_handler', 'gutenberg_styles_wp_die_handler' ); | ||
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. I think this fix deserves some detailed explanation in a comment. It looks completely magical to me, I don't know what it does, I don't even know what a "hybrid theme" is. 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. haha, you're right. Basically, site-editor.php's access is forbidden for hybrid/classic themes and only allowed with some very special query args (some very special pages like template parts...). This is done in Core and the only way to disable it if we change urls in Gutenberg is to override the I think in the backport PR of this PR, I'm going to try to remove that blocking condition entirely from Core cause I believe it doesn't really make much sense anymore. But I'll add a comment. 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. I see, so we're trying to undo the effect of this code? What would explain it to me would be:
Such information will be also super useful for the person who will actually do the 6.8 backporting. 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. Is the last comment good enough or you think I should modify it? I'm doing the backporting btw since now it's mandatory to open a PR before being able to merge this PR into Gutenberg :) 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. Now when I know what's going on I'm personally satisfied 🙂 And it's written down in GitHub comments, a motivated researcher can track it down. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Why do we need
&& isset( $_GET['p'] )
? This means it's not possible to access the site editor root, which I need for #66851There 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.
I'm not entirely sure that it's necessary tbh, it was just defensive coding because
p
is almost always defined now due to the redirects in place.