diff --git a/lib/experimental/site-editor-permalinks.php b/lib/experimental/site-editor-permalinks.php index b2cfd978e38e2..d286cbccb64a1 100644 --- a/lib/experimental/site-editor-permalinks.php +++ b/lib/experimental/site-editor-permalinks.php @@ -18,11 +18,104 @@ function ( $settings ) { } ); +function gutenberg_remove_query_args( $args ) { + $query_string = $_SERVER['QUERY_STRING']; + $query = wp_parse_args( $query_string ); + foreach ( $args as $arg_name ) { + unset( $query[ $arg_name ] ); + } + return build_query( $query ); +} + function gutenberg_redirect_site_editor_to_design() { global $pagenow; - if ( 'site-editor.php' === $pagenow && strpos( $_SERVER['REQUEST_URI'], 'wp-admin/site-editor.php' ) ) { - wp_redirect( admin_url( '/design' ), 301 ); + if ( 'site-editor.php' !== $pagenow || ! strpos( $_SERVER['REQUEST_URI'], 'wp-admin/site-editor.php' ) ) { + return; + } + + // The following redirects are for the new permalinks in the site editor. + if ( isset( $_REQUEST['postType'] ) && 'wp_navigation' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) { + wp_redirect( admin_url( '/design/wp_navigation/' . $_REQUEST['postId'] . '?' . gutenberg_remove_query_args( array( 'postType', 'postId' ) ) ), 301 ); + exit; + } + + if ( isset( $_REQUEST['postType'] ) && 'wp_navigation' === $_REQUEST['postType'] && empty( $_REQUEST['postId'] ) ) { + wp_redirect( admin_url( '/design/navigation?' . gutenberg_remove_query_args( array( 'postType' ) ) ), 301 ); + exit; + } + + if ( isset( $_REQUEST['path'] ) && '/wp_global_styles' === $_REQUEST['path'] ) { + wp_redirect( admin_url( '/design/styles?' . gutenberg_remove_query_args( array( 'path' ) ) ), 301 ); + exit; + } + + if ( isset( $_REQUEST['postType'] ) && 'page' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) { + wp_redirect( admin_url( '/design/page?' . gutenberg_remove_query_args( array( 'postType' ) ) ), 301 ); + exit; + } + + if ( isset( $_REQUEST['postType'] ) && 'page' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) { + wp_redirect( admin_url( '/design/page/' . $_REQUEST['postId'] . '?' . gutenberg_remove_query_args( array( 'postType', 'postId' ) ) ), 301 ); + exit; + } + + if ( isset( $_REQUEST['postType'] ) && 'wp_template' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) { + wp_redirect( admin_url( '/design/template?' . gutenberg_remove_query_args( array( 'postType' ) ) ), 301 ); + exit; + } + + if ( isset( $_REQUEST['postType'] ) && 'wp_template' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) { + wp_redirect( admin_url( '/design/wp_template/' . $_REQUEST['postId'] . '?' . gutenberg_remove_query_args( array( 'postType', 'postId' ) ) ), 301 ); + exit; + } + + if ( isset( $_REQUEST['postType'] ) && 'wp_block' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) { + wp_redirect( admin_url( '/design/pattern?' . gutenberg_remove_query_args( array( 'postType' ) ) ), 301 ); exit; } + + if ( isset( $_REQUEST['postType'] ) && 'wp_block' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) { + wp_redirect( admin_url( '/design/wp_block/' . $_REQUEST['postId'] . '?' . gutenberg_remove_query_args( array( 'postType', 'postId' ) ) ), 301 ); + exit; + } + + if ( isset( $_REQUEST['postType'] ) && 'wp_template_part' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) { + wp_redirect( admin_url( '/design/pattern?' . $_SERVER['QUERY_STRING'] ), 301 ); + exit; + } + + if ( isset( $_REQUEST['postType'] ) && 'wp_template_part' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) { + wp_redirect( admin_url( '/design/wp_template_part/' . $_REQUEST['postId'] . '?' . gutenberg_remove_query_args( array( 'postType', 'postId' ) ) ), 301 ); + exit; + } + + // The following redirects are for backward compatibility with the old site editor URLs. + if ( isset( $_REQUEST['path'] ) && '/wp_template_part/all' === $_REQUEST['path'] ) { + wp_redirect( admin_url( '/design/pattern?postType=wp_template_part' ), 301 ); + exit; + } + + if ( isset( $_REQUEST['path'] ) && '/page' === $_REQUEST['path'] ) { + wp_redirect( admin_url( '/design/page' ), 301 ); + exit; + } + + if ( isset( $_REQUEST['path'] ) && '/wp_template' === $_REQUEST['path'] ) { + wp_redirect( admin_url( '/design/template' ), 301 ); + exit; + } + + if ( isset( $_REQUEST['path'] ) && '/patterns' === $_REQUEST['path'] ) { + wp_redirect( admin_url( '/design/pattern' ), 301 ); + exit; + } + + if ( isset( $_REQUEST['path'] ) && '/navigation' === $_REQUEST['path'] ) { + wp_redirect( admin_url( '/design/navigation' ), 301 ); + exit; + } + + wp_redirect( admin_url( '/design' ), 301 ); + exit; } add_action( 'admin_init', 'gutenberg_redirect_site_editor_to_design' ); diff --git a/packages/edit-site/src/components/editor/index.js b/packages/edit-site/src/components/editor/index.js index 102c5cd0df9f9..7e3d4ab4fecc4 100644 --- a/packages/edit-site/src/components/editor/index.js +++ b/packages/edit-site/src/components/editor/index.js @@ -316,7 +316,9 @@ export default function EditSiteEditor( { isPostsList = false } ) { history.navigate( getNavigationPath( location, - postType + postWithTemplate + ? context.postType + : postType ), { transition: diff --git a/packages/edit-site/src/components/layout/router.js b/packages/edit-site/src/components/layout/router.js deleted file mode 100644 index f55ffdaffc04b..0000000000000 --- a/packages/edit-site/src/components/layout/router.js +++ /dev/null @@ -1,61 +0,0 @@ -/** - * WordPress dependencies - */ -import { privateApis as routerPrivateApis } from '@wordpress/router'; -import { useEffect } from '@wordpress/element'; - -/** - * Internal dependencies - */ -import { unlock } from '../../lock-unlock'; -import { - NAVIGATION_POST_TYPE, - PATTERN_TYPES, - TEMPLATE_PART_POST_TYPE, - TEMPLATE_POST_TYPE, -} from '../../utils/constants'; - -const { useLocation, useHistory } = unlock( routerPrivateApis ); - -export function useRedirectOldPaths() { - const history = useHistory(); - const { params } = useLocation(); - useEffect( () => { - const { postType, path, categoryType, ...rest } = params; - - if ( path === '/wp_template_part/all' ) { - history.replace( { postType: TEMPLATE_PART_POST_TYPE } ); - } - - if ( path === '/page' ) { - history.replace( { - postType: 'page', - ...rest, - } ); - } - - if ( path === '/wp_template' ) { - history.replace( { - postType: TEMPLATE_POST_TYPE, - ...rest, - } ); - } - - if ( path === '/patterns' ) { - history.replace( { - postType: - categoryType === TEMPLATE_PART_POST_TYPE - ? TEMPLATE_PART_POST_TYPE - : PATTERN_TYPES.user, - ...rest, - } ); - } - - if ( path === '/navigation' ) { - history.replace( { - postType: NAVIGATION_POST_TYPE, - ...rest, - } ); - } - }, [ history, params ] ); -}