From 034ab76935ca604793dca45c60e68210c98e8fc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Thu, 25 Nov 2021 12:09:47 +0100 Subject: [PATCH] Reuse the unique slug generated by the server --- lib/navigation.php | 26 ------------------- .../block-library/src/navigation/block.json | 5 +++- .../src/navigation/edit/index.js | 5 +++- 3 files changed, 8 insertions(+), 28 deletions(-) diff --git a/lib/navigation.php b/lib/navigation.php index d23f1c5f3203f..816e2582b8840 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -449,29 +449,3 @@ function gutenberg_hide_visibility_and_status_for_navigation_posts( $hook ) { } add_action( 'admin_enqueue_scripts', 'gutenberg_hide_visibility_and_status_for_navigation_posts' ); - -/** - * Sets a custom slug when creating auto-draft wp_navigatino posts. - * 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_wp_navigation( $post_id ) { - // This is the core function with the same functionality. - if ( function_exists( 'gutenberg_set_unique_slug_on_create_wp_navigation' ) ) { - return; - } - - $post = get_post( $post_id ); - if ( ! $post->post_name ) { - wp_update_post( - array( - 'ID' => $post_id, - 'post_name' => $post_id . '', - ) - ); - } -} - -add_action( 'save_post_wp_navigation', 'gutenberg_set_unique_slug_on_create_wp_navigation' ); diff --git a/packages/block-library/src/navigation/block.json b/packages/block-library/src/navigation/block.json index 664d814e88bbb..1f0b339525133 100644 --- a/packages/block-library/src/navigation/block.json +++ b/packages/block-library/src/navigation/block.json @@ -9,7 +9,10 @@ "textdomain": "default", "attributes": { "navigationMenuId": { - "type": "number" + "type": "number" + }, + "slug": { + "type": "string" }, "textColor": { "type": "string" diff --git a/packages/block-library/src/navigation/edit/index.js b/packages/block-library/src/navigation/edit/index.js index eb283a5a9709b..73f3326089d6b 100644 --- a/packages/block-library/src/navigation/edit/index.js +++ b/packages/block-library/src/navigation/edit/index.js @@ -312,6 +312,7 @@ function Navigation( { setHasSavedUnsavedInnerBlocks( true ); // Switch to using the wp_navigation entity. setNavigationMenuId( post.id ); + setAttributes( { slug: post.slug } ); } } /> ); @@ -365,8 +366,9 @@ function Navigation( { > { ( { onClose } ) => ( { + onSelect={ ( { id, slug } ) => { setNavigationMenuId( id ); + setAttributes( { slug } ); onClose(); } } onCreateNew={ startWithEmptyMenu } @@ -501,6 +503,7 @@ function Navigation( { setIsPlaceholderShown( false ); if ( post ) { setNavigationMenuId( post.id ); + setAttributes( { slug: post.slug } ); } selectBlock( clientId ); } }