Skip to content

Commit

Permalink
Reuse the unique slug generated by the server
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Nov 25, 2021
1 parent 07909a0 commit 034ab76
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 28 deletions.
26 changes: 0 additions & 26 deletions lib/navigation.php
Expand Up @@ -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' );
5 changes: 4 additions & 1 deletion packages/block-library/src/navigation/block.json
Expand Up @@ -9,7 +9,10 @@
"textdomain": "default",
"attributes": {
"navigationMenuId": {
"type": "number"
"type": "number"
},
"slug": {
"type": "string"
},
"textColor": {
"type": "string"
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/navigation/edit/index.js
Expand Up @@ -312,6 +312,7 @@ function Navigation( {
setHasSavedUnsavedInnerBlocks( true );
// Switch to using the wp_navigation entity.
setNavigationMenuId( post.id );
setAttributes( { slug: post.slug } );
} }
/>
);
Expand Down Expand Up @@ -365,8 +366,9 @@ function Navigation( {
>
{ ( { onClose } ) => (
<NavigationMenuSelector
onSelect={ ( { id } ) => {
onSelect={ ( { id, slug } ) => {
setNavigationMenuId( id );
setAttributes( { slug } );
onClose();
} }
onCreateNew={ startWithEmptyMenu }
Expand Down Expand Up @@ -501,6 +503,7 @@ function Navigation( {
setIsPlaceholderShown( false );
if ( post ) {
setNavigationMenuId( post.id );
setAttributes( { slug: post.slug } );
}
selectBlock( clientId );
} }
Expand Down

0 comments on commit 034ab76

Please sign in to comment.