Skip to content

Commit 551e474

Browse files
committed
Site Editor: Redirect deprecated URLs to path based routing.
The site editor now uses path based routing rather than query string arguments. This redirects the legacy query string URLs to the new routing. Props youknowriad, peterwilsoncc, joemcgill, mukesh27, poena. Fixes #62585. git-svn-id: https://develop.svn.wordpress.org/trunk@59794 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 67bc44b commit 551e474

File tree

1 file changed

+95
-12
lines changed

1 file changed

+95
-12
lines changed

src/wp-admin/site-editor.php

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,102 @@
1919
);
2020
}
2121

22-
$is_template_part = isset( $_GET['postType'] ) && 'wp_template_part' === sanitize_key( $_GET['postType'] );
23-
$is_template_part_path = isset( $_GET['path'] ) && 'wp_template_partall' === sanitize_key( $_GET['path'] );
24-
$is_template_part_editor = $is_template_part || $is_template_part_path;
25-
$is_patterns = isset( $_GET['postType'] ) && 'wp_block' === sanitize_key( $_GET['postType'] );
26-
$is_patterns_path = isset( $_GET['path'] ) && 'patterns' === sanitize_key( $_GET['path'] );
27-
$is_patterns_editor = $is_patterns || $is_patterns_path;
28-
29-
if ( ! wp_is_block_theme() ) {
30-
if ( ! current_theme_supports( 'block-template-parts' ) && $is_template_part_editor ) {
31-
wp_die( __( 'The theme you are currently using is not compatible with the Site Editor.' ) );
32-
} elseif ( ! $is_patterns_editor && ! $is_template_part_editor ) {
33-
wp_die( __( 'The theme you are currently using is not compatible with the Site Editor.' ) );
22+
/**
23+
* Maps old site editor urls to the new updated ones.
24+
*
25+
* @since 6.8.0
26+
* @access private
27+
*
28+
* @global string $pagenow The filename of the current screen.
29+
*
30+
* @return string|false The new URL to redirect to, or false if no redirection is needed.
31+
*/
32+
function _wp_get_site_editor_redirection_url() {
33+
global $pagenow;
34+
if ( 'site-editor.php' !== $pagenow || isset( $_REQUEST['p'] ) || ! $_SERVER['QUERY_STRING'] ) {
35+
return false;
36+
}
37+
38+
// The following redirects are for the new permalinks in the site editor.
39+
if ( isset( $_REQUEST['postType'] ) && 'wp_navigation' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) {
40+
return add_query_arg( array( 'p' => '/wp_navigation/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) );
41+
}
42+
43+
if ( isset( $_REQUEST['postType'] ) && 'wp_navigation' === $_REQUEST['postType'] && empty( $_REQUEST['postId'] ) ) {
44+
return add_query_arg( array( 'p' => '/navigation' ), remove_query_arg( 'postType' ) );
45+
}
46+
47+
if ( isset( $_REQUEST['path'] ) && '/wp_global_styles' === $_REQUEST['path'] ) {
48+
return add_query_arg( array( 'p' => '/styles' ), remove_query_arg( 'path' ) );
3449
}
50+
51+
if ( isset( $_REQUEST['postType'] ) && 'page' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) {
52+
return add_query_arg( array( 'p' => '/page' ), remove_query_arg( 'postType' ) );
53+
}
54+
55+
if ( isset( $_REQUEST['postType'] ) && 'page' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) {
56+
return add_query_arg( array( 'p' => '/page/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) );
57+
}
58+
59+
if ( isset( $_REQUEST['postType'] ) && 'wp_template' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) {
60+
return add_query_arg( array( 'p' => '/template' ), remove_query_arg( 'postType' ) );
61+
}
62+
63+
if ( isset( $_REQUEST['postType'] ) && 'wp_template' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) {
64+
return add_query_arg( array( 'p' => '/wp_template/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) );
65+
}
66+
67+
if ( isset( $_REQUEST['postType'] ) && 'wp_block' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) {
68+
return add_query_arg( array( 'p' => '/pattern' ), remove_query_arg( 'postType' ) );
69+
}
70+
71+
if ( isset( $_REQUEST['postType'] ) && 'wp_block' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) {
72+
return add_query_arg( array( 'p' => '/wp_block/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) );
73+
}
74+
75+
if ( isset( $_REQUEST['postType'] ) && 'wp_template_part' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) {
76+
return add_query_arg( array( 'p' => '/pattern' ) );
77+
}
78+
79+
if ( isset( $_REQUEST['postType'] ) && 'wp_template_part' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) {
80+
return add_query_arg( array( 'p' => '/wp_template_part/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) );
81+
}
82+
83+
// The following redirects are for backward compatibility with the old site editor URLs.
84+
if ( isset( $_REQUEST['path'] ) && '/wp_template_part/all' === $_REQUEST['path'] ) {
85+
return add_query_arg(
86+
array(
87+
'p' => '/pattern',
88+
'postType' => 'wp_template_part',
89+
),
90+
remove_query_arg( 'path' )
91+
);
92+
}
93+
94+
if ( isset( $_REQUEST['path'] ) && '/page' === $_REQUEST['path'] ) {
95+
return add_query_arg( array( 'p' => '/page' ), remove_query_arg( 'path' ) );
96+
}
97+
98+
if ( isset( $_REQUEST['path'] ) && '/wp_template' === $_REQUEST['path'] ) {
99+
return add_query_arg( array( 'p' => '/template' ), remove_query_arg( 'path' ) );
100+
}
101+
102+
if ( isset( $_REQUEST['path'] ) && '/patterns' === $_REQUEST['path'] ) {
103+
return add_query_arg( array( 'p' => '/pattern' ), remove_query_arg( 'path' ) );
104+
}
105+
106+
if ( isset( $_REQUEST['path'] ) && '/navigation' === $_REQUEST['path'] ) {
107+
return add_query_arg( array( 'p' => '/navigation' ), remove_query_arg( 'path' ) );
108+
}
109+
110+
return add_query_arg( array( 'p' => '/' ) );
111+
}
112+
113+
// Redirect to the site editor to the new URLs if needed.
114+
$redirection = _wp_get_site_editor_redirection_url();
115+
if ( false !== $redirection ) {
116+
wp_safe_redirect( $redirection );
117+
exit;
35118
}
36119

37120
// Used in the HTML title tag.

0 commit comments

Comments
 (0)