From c9e948e26d2c27697b57cea857d51af5ea312217 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 1 Nov 2021 21:05:37 +0100 Subject: [PATCH 01/16] Disable block editor for wp_navigation CPT (it doesn't work yet) so it can be managed using the standard UI. --- lib/navigation.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/navigation.php b/lib/navigation.php index 452ee06a41b8f..7426614789fbc 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -418,7 +418,7 @@ function gutenberg_register_navigation_post_type() { 'description' => __( 'Navigation menus.', 'gutenberg' ), 'public' => false, 'has_archive' => false, - 'show_ui' => false, + 'show_ui' => true, 'show_in_menu' => 'themes.php', 'show_in_admin_bar' => false, 'show_in_rest' => true, @@ -435,3 +435,23 @@ function gutenberg_register_navigation_post_type() { register_post_type( 'wp_navigation', $args ); } add_action( 'init', 'gutenberg_register_navigation_post_type' ); + + +/** + * Disable block editor for wp_navigation type posts so they can be managed via the UI. + * + * @param bool $value Whether the CPT supports block editor or not. + * @param string $post_type Post type. + * + * @return bool + */ +function gutenberg_disable_block_editor_for_navigation_post_type( $value, $post_type ) { + if ( 'wp_navigation' === $post_type ) { + return false; + } + + return $value; +} + +add_filter( 'use_block_editor_for_post_type', 'gutenberg_disable_block_editor_for_navigation_post_type', 10, 2 ); + From aa0ff7e1dc34a987a319407a5b3ae086e3011e81 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Tue, 2 Nov 2021 22:57:10 +0100 Subject: [PATCH 02/16] Disable ability to edit wp_navigation type posts because the post editor doesn't correctly work with wp_navigation posts. --- lib/navigation.php | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/navigation.php b/lib/navigation.php index 7426614789fbc..4f7237a169044 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -436,7 +436,6 @@ function gutenberg_register_navigation_post_type() { } add_action( 'init', 'gutenberg_register_navigation_post_type' ); - /** * Disable block editor for wp_navigation type posts so they can be managed via the UI. * @@ -455,3 +454,42 @@ function gutenberg_disable_block_editor_for_navigation_post_type( $value, $post_ add_filter( 'use_block_editor_for_post_type', 'gutenberg_disable_block_editor_for_navigation_post_type', 10, 2 ); +/** + * This function disables ability to edit wp_navigation posts via the UI. + * This is because the post editor doesn't correctly work with wp_navigation type posts. + * + * @param string $url Url of the post. + * @param integer $post_id Post ID. + * + * @return string + */ +function gutenberg_disable_edit_links_for_navigation_post_type($url, $post_id) { + $post = get_post($post_id); + if ( 'wp_navigation' !== $post->post_type ) { + return $url; + } + return 'javascript:void(0)'; +} + +add_filter( 'get_edit_post_link', 'gutenberg_disable_edit_links_for_navigation_post_type', 10, 2); + +/** + * This function disables "Edit" row action for wp_navigation type posts. + * This is because the post editor doesn't correctly work with wp_navigation type posts. + * + * @param array $actions A list of supported row actions for the post. + * @param $post WP_Post object. + * + * @return array + */ +function gutenberg_disable_edit_row_action_for_navigation_post_type($actions, $post) { + $post = get_post($post); + if ( 'wp_navigation' !== $post->post_type ) { + return $actions; + } + + unset($actions['edit']); + return $actions; +} + +add_filter( 'post_row_actions', 'gutenberg_disable_edit_row_action_for_navigation_post_type', 10, 2); From f91c5fb74f276da5c55ca5e885d39546231a0c77 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Tue, 2 Nov 2021 23:00:27 +0100 Subject: [PATCH 03/16] Don't call get_posts when it's not needed. --- lib/navigation.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/navigation.php b/lib/navigation.php index 4f7237a169044..088f47cf03f56 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -483,7 +483,6 @@ function gutenberg_disable_edit_links_for_navigation_post_type($url, $post_id) { * @return array */ function gutenberg_disable_edit_row_action_for_navigation_post_type($actions, $post) { - $post = get_post($post); if ( 'wp_navigation' !== $post->post_type ) { return $actions; } From 0a243b35a07c9b824b90867526f1bc35a01193e0 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 3 Nov 2021 00:02:03 +0100 Subject: [PATCH 04/16] Add unit tests for navigation.php. --- phpunit/navigation-test.php | 72 +++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 phpunit/navigation-test.php diff --git a/phpunit/navigation-test.php b/phpunit/navigation-test.php new file mode 100644 index 0000000000000..7e1ecafd52db2 --- /dev/null +++ b/phpunit/navigation-test.php @@ -0,0 +1,72 @@ +assertTrue($filtered_result); + } + + public function test_it_disables_block_editor_for_navigation_post_types() { + $filtered_result = gutenberg_disable_block_editor_for_navigation_post_type(true, 'wp_navigation'); + $this->assertFalse($filtered_result); + } + + public function test_it_doesnt_disable_edit_links_for_non_navigation_post_types() { + $post = $this->create_sample_post(); + $url = 'someUrl'; + $filtered_url = gutenberg_disable_edit_links_for_navigation_post_type($url, $post); + $this->assertSame($url, $filtered_url); + } + + public function test_it_disables_edit_links_for_navigation_post_types() { + $post = $this->create_navigation_post(); + $url = 'someUrl'; + $filtered_url = gutenberg_disable_edit_links_for_navigation_post_type($url, $post); + $this->assertNotSame($url, $filtered_url); + $this->assertNotEmpty($filtered_url); + $this->assertIsString($filtered_url); + } + + public function test_it_doesnt_remove_edit_row_action_for_non_navigation_post_types() { + $actions = [ + 'edit' => 1, + ]; + + $post = $this->create_sample_post(); + $filtered_actions = gutenberg_disable_edit_row_action_for_navigation_post_type( $actions, $post ); + $this->assertSame( $actions, $filtered_actions ); + } + + public function test_it_removes_edit_row_action_for_navigation_post_types() { + $actions = [ + 'edit' => 1, + ]; + + $post = $this->create_navigation_post(); + $filtered_actions = gutenberg_disable_edit_row_action_for_navigation_post_type( $actions, $post ); + $this->assertSame( array(), $filtered_actions ); + } + + private function create_post($type) + { + $post = new WP_Post(new StdClass()); + $post->post_type = $type; + $post->filter = 'raw'; + return $post; + } + + private function create_sample_post() + { + return $this->create_post('sample_post_type'); + } + + private function create_navigation_post() + { + return $this->create_post('wp_navigation'); + } +} From 066bf4a8cd3c4877e72e71f8915832c48786611a Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 3 Nov 2021 10:26:25 +0100 Subject: [PATCH 05/16] Fix code style. --- lib/navigation.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/navigation.php b/lib/navigation.php index 088f47cf03f56..e4aac676428d0 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -463,15 +463,16 @@ function gutenberg_disable_block_editor_for_navigation_post_type( $value, $post_ * * @return string */ -function gutenberg_disable_edit_links_for_navigation_post_type($url, $post_id) { - $post = get_post($post_id); +function gutenberg_disable_edit_links_for_navigation_post_type( $url, $post_id ) { + $post = get_post( $post_id ); if ( 'wp_navigation' !== $post->post_type ) { return $url; } + return 'javascript:void(0)'; } -add_filter( 'get_edit_post_link', 'gutenberg_disable_edit_links_for_navigation_post_type', 10, 2); +add_filter( 'get_edit_post_link', 'gutenberg_disable_edit_links_for_navigation_post_type', 10, 2 ); /** * This function disables "Edit" row action for wp_navigation type posts. @@ -482,13 +483,13 @@ function gutenberg_disable_edit_links_for_navigation_post_type($url, $post_id) { * * @return array */ -function gutenberg_disable_edit_row_action_for_navigation_post_type($actions, $post) { +function gutenberg_disable_edit_row_action_for_navigation_post_type( $actions, $post ) { if ( 'wp_navigation' !== $post->post_type ) { return $actions; } - unset($actions['edit']); + unset( $actions['edit'] ); return $actions; } -add_filter( 'post_row_actions', 'gutenberg_disable_edit_row_action_for_navigation_post_type', 10, 2); +add_filter( 'post_row_actions', 'gutenberg_disable_edit_row_action_for_navigation_post_type', 10, 2 ); From 0f8688e69428013ae5172d0a0f0deba9fd9488a0 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 3 Nov 2021 10:32:39 +0100 Subject: [PATCH 06/16] Fix code style. --- phpunit/navigation-test.php | 57 ++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/phpunit/navigation-test.php b/phpunit/navigation-test.php index 7e1ecafd52db2..4e23a6e3f4170 100644 --- a/phpunit/navigation-test.php +++ b/phpunit/navigation-test.php @@ -7,66 +7,63 @@ class WP_Navigation_Test extends WP_UnitTestCase { public function test_it_doesnt_disable_block_editor_for_non_navigation_post_types() { - $filtered_result = gutenberg_disable_block_editor_for_navigation_post_type(true, 'sample_post_type'); - $this->assertTrue($filtered_result); + $filtered_result = gutenberg_disable_block_editor_for_navigation_post_type( true, 'sample_post_type' ); + $this->assertTrue( $filtered_result ); } public function test_it_disables_block_editor_for_navigation_post_types() { - $filtered_result = gutenberg_disable_block_editor_for_navigation_post_type(true, 'wp_navigation'); - $this->assertFalse($filtered_result); + $filtered_result = gutenberg_disable_block_editor_for_navigation_post_type( true, 'wp_navigation' ); + $this->assertFalse( $filtered_result ); } public function test_it_doesnt_disable_edit_links_for_non_navigation_post_types() { - $post = $this->create_sample_post(); - $url = 'someUrl'; - $filtered_url = gutenberg_disable_edit_links_for_navigation_post_type($url, $post); - $this->assertSame($url, $filtered_url); + $post = $this->create_sample_post(); + $url = 'someUrl'; + $filtered_url = gutenberg_disable_edit_links_for_navigation_post_type( $url, $post ); + $this->assertSame( $url, $filtered_url ); } public function test_it_disables_edit_links_for_navigation_post_types() { - $post = $this->create_navigation_post(); - $url = 'someUrl'; - $filtered_url = gutenberg_disable_edit_links_for_navigation_post_type($url, $post); - $this->assertNotSame($url, $filtered_url); - $this->assertNotEmpty($filtered_url); - $this->assertIsString($filtered_url); + $post = $this->create_navigation_post(); + $url = 'someUrl'; + $filtered_url = gutenberg_disable_edit_links_for_navigation_post_type( $url, $post ); + $this->assertNotSame( $url, $filtered_url ); + $this->assertNotEmpty( $filtered_url ); + $this->assertIsString( $filtered_url ); } public function test_it_doesnt_remove_edit_row_action_for_non_navigation_post_types() { - $actions = [ + $actions = array( 'edit' => 1, - ]; + ); - $post = $this->create_sample_post(); + $post = $this->create_sample_post(); $filtered_actions = gutenberg_disable_edit_row_action_for_navigation_post_type( $actions, $post ); $this->assertSame( $actions, $filtered_actions ); } public function test_it_removes_edit_row_action_for_navigation_post_types() { - $actions = [ + $actions = array( 'edit' => 1, - ]; + ); - $post = $this->create_navigation_post(); + $post = $this->create_navigation_post(); $filtered_actions = gutenberg_disable_edit_row_action_for_navigation_post_type( $actions, $post ); $this->assertSame( array(), $filtered_actions ); } - private function create_post($type) - { - $post = new WP_Post(new StdClass()); + private function create_post( $type ) { + $post = new WP_Post( new StdClass() ); $post->post_type = $type; - $post->filter = 'raw'; + $post->filter = 'raw'; return $post; } - private function create_sample_post() - { - return $this->create_post('sample_post_type'); + private function create_sample_post() { + return $this->create_post( 'sample_post_type' ); } - private function create_navigation_post() - { - return $this->create_post('wp_navigation'); + private function create_navigation_post() { + return $this->create_post( 'wp_navigation' ); } } From 566c1596afab40ad08a902affcfc5a3c33fd05fd Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 3 Nov 2021 10:47:59 +0100 Subject: [PATCH 07/16] Fix code style. --- lib/navigation.php | 6 +++--- phpunit/navigation-test.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/navigation.php b/lib/navigation.php index e4aac676428d0..3f63e6def6f3d 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -458,7 +458,7 @@ function gutenberg_disable_block_editor_for_navigation_post_type( $value, $post_ * This function disables ability to edit wp_navigation posts via the UI. * This is because the post editor doesn't correctly work with wp_navigation type posts. * - * @param string $url Url of the post. + * @param string $url Url of the post. * @param integer $post_id Post ID. * * @return string @@ -478,8 +478,8 @@ function gutenberg_disable_edit_links_for_navigation_post_type( $url, $post_id ) * This function disables "Edit" row action for wp_navigation type posts. * This is because the post editor doesn't correctly work with wp_navigation type posts. * - * @param array $actions A list of supported row actions for the post. - * @param $post WP_Post object. + * @param array $actions A list of supported row actions for the post. + * @param WP_Post $post An instance of WP_Post class. * * @return array */ diff --git a/phpunit/navigation-test.php b/phpunit/navigation-test.php index 4e23a6e3f4170..95cf8e4be05d6 100644 --- a/phpunit/navigation-test.php +++ b/phpunit/navigation-test.php @@ -60,10 +60,10 @@ private function create_post( $type ) { } private function create_sample_post() { - return $this->create_post( 'sample_post_type' ); + return $this->create_post( 'sample_post_type' ); } private function create_navigation_post() { - return $this->create_post( 'wp_navigation' ); + return $this->create_post( 'wp_navigation' ); } } From cf83ec253fb8dc9b41a6dd4005e2fc04532093f4 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 3 Nov 2021 11:04:33 +0100 Subject: [PATCH 08/16] Use get_post_type because it validates empty $post values. --- lib/navigation.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/navigation.php b/lib/navigation.php index 3f63e6def6f3d..0d5ccc2af3112 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -464,8 +464,8 @@ function gutenberg_disable_block_editor_for_navigation_post_type( $value, $post_ * @return string */ function gutenberg_disable_edit_links_for_navigation_post_type( $url, $post_id ) { - $post = get_post( $post_id ); - if ( 'wp_navigation' !== $post->post_type ) { + $post_type = get_post_type( $post_id ); + if ( 'wp_navigation' !== $post_type ) { return $url; } @@ -484,7 +484,8 @@ function gutenberg_disable_edit_links_for_navigation_post_type( $url, $post_id ) * @return array */ function gutenberg_disable_edit_row_action_for_navigation_post_type( $actions, $post ) { - if ( 'wp_navigation' !== $post->post_type ) { + $post_type = get_post_type( $post ); + if ( 'wp_navigation' !== $post_type ) { return $actions; } From ec24e7258565a560bce28bdf666ae1dc4b179067 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 3 Nov 2021 14:18:16 +0100 Subject: [PATCH 09/16] 1. Disable content editor for wp_navigation type posts. Content editor cannot correctly edit them. 2. Add unit tests. --- lib/navigation.php | 36 ++++------------------ phpunit/navigation-test.php | 61 ++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 64 deletions(-) diff --git a/lib/navigation.php b/lib/navigation.php index 0d5ccc2af3112..935a0c82ca4a9 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -455,42 +455,18 @@ function gutenberg_disable_block_editor_for_navigation_post_type( $value, $post_ add_filter( 'use_block_editor_for_post_type', 'gutenberg_disable_block_editor_for_navigation_post_type', 10, 2 ); /** - * This function disables ability to edit wp_navigation posts via the UI. - * This is because the post editor doesn't correctly work with wp_navigation type posts. + * This function disables content editor for wp_navigation type posts. + * Content editor cannot correctly edit wp_navigation type posts. * - * @param string $url Url of the post. - * @param integer $post_id Post ID. - * - * @return string - */ -function gutenberg_disable_edit_links_for_navigation_post_type( $url, $post_id ) { - $post_type = get_post_type( $post_id ); - if ( 'wp_navigation' !== $post_type ) { - return $url; - } - - return 'javascript:void(0)'; -} - -add_filter( 'get_edit_post_link', 'gutenberg_disable_edit_links_for_navigation_post_type', 10, 2 ); - -/** - * This function disables "Edit" row action for wp_navigation type posts. - * This is because the post editor doesn't correctly work with wp_navigation type posts. - * - * @param array $actions A list of supported row actions for the post. * @param WP_Post $post An instance of WP_Post class. - * - * @return array */ -function gutenberg_disable_edit_row_action_for_navigation_post_type( $actions, $post ) { +function gutenberg_disable_content_editor_for_navigation_post_type( $post ) { $post_type = get_post_type( $post ); if ( 'wp_navigation' !== $post_type ) { - return $actions; + return; } - unset( $actions['edit'] ); - return $actions; + remove_post_type_support( $post_type, 'editor' ); } -add_filter( 'post_row_actions', 'gutenberg_disable_edit_row_action_for_navigation_post_type', 10, 2 ); +add_action( 'edit_form_after_title', 'gutenberg_disable_content_editor_for_navigation_post_type', 10, 1 ); diff --git a/phpunit/navigation-test.php b/phpunit/navigation-test.php index 95cf8e4be05d6..c9648a6250cca 100644 --- a/phpunit/navigation-test.php +++ b/phpunit/navigation-test.php @@ -6,50 +6,39 @@ */ class WP_Navigation_Test extends WP_UnitTestCase { + const NAVIGATION_POST_TYPE = 'wp_navigation'; + const NON_NAVIGATION_POST_TYPE = 'wp_non_navigation'; + + public function tearDown() { + add_post_type_support( static::NAVIGATION_POST_TYPE, 'editor' ); + } + public function test_it_doesnt_disable_block_editor_for_non_navigation_post_types() { - $filtered_result = gutenberg_disable_block_editor_for_navigation_post_type( true, 'sample_post_type' ); + $filtered_result = gutenberg_disable_block_editor_for_navigation_post_type( true, static::NON_NAVIGATION_POST_TYPE ); $this->assertTrue( $filtered_result ); } public function test_it_disables_block_editor_for_navigation_post_types() { - $filtered_result = gutenberg_disable_block_editor_for_navigation_post_type( true, 'wp_navigation' ); + $filtered_result = gutenberg_disable_block_editor_for_navigation_post_type( true, static::NAVIGATION_POST_TYPE ); $this->assertFalse( $filtered_result ); } - public function test_it_doesnt_disable_edit_links_for_non_navigation_post_types() { - $post = $this->create_sample_post(); - $url = 'someUrl'; - $filtered_url = gutenberg_disable_edit_links_for_navigation_post_type( $url, $post ); - $this->assertSame( $url, $filtered_url ); - } + public function test_it_doesnt_disable_content_editor_for_non_navigation_type_posts() { + $post = $this->create_non_navigation_post(); + $this->assertTrue( $this->supports_block_editor() ); - public function test_it_disables_edit_links_for_navigation_post_types() { - $post = $this->create_navigation_post(); - $url = 'someUrl'; - $filtered_url = gutenberg_disable_edit_links_for_navigation_post_type( $url, $post ); - $this->assertNotSame( $url, $filtered_url ); - $this->assertNotEmpty( $filtered_url ); - $this->assertIsString( $filtered_url ); - } + gutenberg_disable_content_editor_for_navigation_post_type( $post ); - public function test_it_doesnt_remove_edit_row_action_for_non_navigation_post_types() { - $actions = array( - 'edit' => 1, - ); - - $post = $this->create_sample_post(); - $filtered_actions = gutenberg_disable_edit_row_action_for_navigation_post_type( $actions, $post ); - $this->assertSame( $actions, $filtered_actions ); + $this->assertTrue( $this->supports_block_editor() ); } - public function test_it_removes_edit_row_action_for_navigation_post_types() { - $actions = array( - 'edit' => 1, - ); + public function test_it_disables_content_editor_for_navigation_type_posts() { + $post = $this->create_navigation_post(); + $this->assertTrue( $this->supports_block_editor() ); + + gutenberg_disable_content_editor_for_navigation_post_type( $post ); - $post = $this->create_navigation_post(); - $filtered_actions = gutenberg_disable_edit_row_action_for_navigation_post_type( $actions, $post ); - $this->assertSame( array(), $filtered_actions ); + $this->assertFalse( $this->supports_block_editor() ); } private function create_post( $type ) { @@ -59,11 +48,15 @@ private function create_post( $type ) { return $post; } - private function create_sample_post() { - return $this->create_post( 'sample_post_type' ); + private function create_non_navigation_post() { + return $this->create_post( static::NON_NAVIGATION_POST_TYPE ); } private function create_navigation_post() { - return $this->create_post( 'wp_navigation' ); + return $this->create_post( static::NAVIGATION_POST_TYPE ); + } + + private function supports_block_editor() { + return post_type_supports( static::NAVIGATION_POST_TYPE, 'editor' ); } } From 39e9570f0936122ac21e9ba92a2d7dade6df5634 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 3 Nov 2021 15:30:04 +0100 Subject: [PATCH 10/16] Fix menu entry. Rename it to "Navigation Menus". --- lib/navigation.php | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/navigation.php b/lib/navigation.php index 935a0c82ca4a9..7bd12d75e831c 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -424,7 +424,7 @@ function gutenberg_register_navigation_post_type() { 'show_in_rest' => true, 'map_meta_cap' => true, 'rest_base' => 'navigation', - 'rest_controller_class' => 'WP_REST_Posts_Controller', + 'rest_controller_class' => WP_REST_Posts_Controller::class, 'supports' => array( 'title', 'editor', @@ -455,8 +455,8 @@ function gutenberg_disable_block_editor_for_navigation_post_type( $value, $post_ add_filter( 'use_block_editor_for_post_type', 'gutenberg_disable_block_editor_for_navigation_post_type', 10, 2 ); /** - * This function disables content editor for wp_navigation type posts. - * Content editor cannot correctly edit wp_navigation type posts. + * This callback disables content editor for wp_navigation type posts. + * Content editor cannot handle wp_navigation type posts correctly. * * @param WP_Post $post An instance of WP_Post class. */ @@ -470,3 +470,25 @@ function gutenberg_disable_content_editor_for_navigation_post_type( $post ) { } add_action( 'edit_form_after_title', 'gutenberg_disable_content_editor_for_navigation_post_type', 10, 1 ); + +/** + * Fixes the label of the 'wp_navigation' admin menu entry. + */ +function gutenberg_fix_navigation_items_admin_menu_entry() { + global $submenu; + if ( ! isset( $submenu['themes.php'] ) ) { + return; + } + $post_type = get_post_type_object( 'wp_navigation' ); + if ( ! $post_type ) { + return; + } + foreach ( $submenu['themes.php'] as $key => $submenu_entry ) { + if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) { + $submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride + return; + } + } +} + +add_action( 'admin_menu', 'gutenberg_fix_navigation_items_admin_menu_entry' ); From 3ecd7b660a02bd00d9715847ec1abb641b08005d Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Thu, 4 Nov 2021 13:24:05 +0100 Subject: [PATCH 11/16] Enable editor support back. We need to enable it back because we disable it to hide the content editor windows. This is required to avoid the incorrect state of wp_navigation CPT. --- lib/navigation.php | 20 ++++++++++++++++++++ phpunit/navigation-test.php | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/navigation.php b/lib/navigation.php index 7bd12d75e831c..590ca6f2732f9 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -471,6 +471,26 @@ function gutenberg_disable_content_editor_for_navigation_post_type( $post ) { add_action( 'edit_form_after_title', 'gutenberg_disable_content_editor_for_navigation_post_type', 10, 1 ); +/** + * This callback enables content editor for wp_navigation type posts. + * We need to enable it back because we disable it to hide + * the content editor for wp_navigation type posts. + * + * @see gutenberg_disable_content_editor_for_navigation_post_type + * + * @param WP_Post $post An instance of WP_Post class. + */ +function gutenberg_enable_content_editor_for_navigation_post_type( $post ) { + $post_type = get_post_type( $post ); + if ( 'wp_navigation' !== $post_type ) { + return; + } + + add_post_type_support( $post_type, 'editor' ); +} + +add_action( 'edit_form_after_editor', 'gutenberg_enable_content_editor_for_navigation_post_type', 10, 1 ); + /** * Fixes the label of the 'wp_navigation' admin menu entry. */ diff --git a/phpunit/navigation-test.php b/phpunit/navigation-test.php index c9648a6250cca..223c31b644b9d 100644 --- a/phpunit/navigation-test.php +++ b/phpunit/navigation-test.php @@ -9,8 +9,12 @@ class WP_Navigation_Test extends WP_UnitTestCase { const NAVIGATION_POST_TYPE = 'wp_navigation'; const NON_NAVIGATION_POST_TYPE = 'wp_non_navigation'; + public function setUp() { + $this->enable_editor_support(); + } + public function tearDown() { - add_post_type_support( static::NAVIGATION_POST_TYPE, 'editor' ); + $this->enable_editor_support(); } public function test_it_doesnt_disable_block_editor_for_non_navigation_post_types() { @@ -41,6 +45,26 @@ public function test_it_disables_content_editor_for_navigation_type_posts() { $this->assertFalse( $this->supports_block_editor() ); } + public function test_it_enables_content_editor_for_non_navigation_type_posts_after_the_content_editor_form() { + $this->disable_editor_support(); + $post = $this->create_navigation_post(); + $this->assertFalse( $this->supports_block_editor() ); + + gutenberg_disable_content_editor_for_navigation_post_type( $post ); + + $this->assertTrue( $this->supports_block_editor() ); + } + + public function test_it_doesnt_enable_content_editor_for_non_navigation_type_posts_after_the_content_editor_form() { + $this->disable_editor_support(); + $post = $this->create_non_navigation_post(); + $this->assertFalse( $this->supports_block_editor() ); + + gutenberg_disable_content_editor_for_navigation_post_type( $post ); + + $this->assertFalse( $this->supports_block_editor() ); + } + private function create_post( $type ) { $post = new WP_Post( new StdClass() ); $post->post_type = $type; @@ -59,4 +83,12 @@ private function create_navigation_post() { private function supports_block_editor() { return post_type_supports( static::NAVIGATION_POST_TYPE, 'editor' ); } + + private function enable_editor_support() { + add_post_type_support( static::NAVIGATION_POST_TYPE, 'editor' ); + } + + private function disable_editor_support() { + remove_post_type_support( static::NAVIGATION_POST_TYPE, 'editor' ); + } } From 14a08e8d035d9298cf5e6c97e096c2f06001b537 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Thu, 4 Nov 2021 13:39:31 +0100 Subject: [PATCH 12/16] Fix the unit test. --- phpunit/navigation-test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit/navigation-test.php b/phpunit/navigation-test.php index 223c31b644b9d..493487d5418a7 100644 --- a/phpunit/navigation-test.php +++ b/phpunit/navigation-test.php @@ -50,7 +50,7 @@ public function test_it_enables_content_editor_for_non_navigation_type_posts_aft $post = $this->create_navigation_post(); $this->assertFalse( $this->supports_block_editor() ); - gutenberg_disable_content_editor_for_navigation_post_type( $post ); + gutenberg_enable_content_editor_for_navigation_post_type( $post ); $this->assertTrue( $this->supports_block_editor() ); } @@ -60,7 +60,7 @@ public function test_it_doesnt_enable_content_editor_for_non_navigation_type_pos $post = $this->create_non_navigation_post(); $this->assertFalse( $this->supports_block_editor() ); - gutenberg_disable_content_editor_for_navigation_post_type( $post ); + gutenberg_enable_content_editor_for_navigation_post_type( $post ); $this->assertFalse( $this->supports_block_editor() ); } From baccf378c44a5b9ba977983f250f589716038075 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Thu, 4 Nov 2021 15:11:47 +0100 Subject: [PATCH 13/16] Refactor the gutenberg_fix_navigation_items_admin_menu_entry function. The new name better describes the purpose of the function. --- lib/navigation.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/navigation.php b/lib/navigation.php index 590ca6f2732f9..8e77b1a45f2c2 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -492,9 +492,9 @@ function gutenberg_enable_content_editor_for_navigation_post_type( $post ) { add_action( 'edit_form_after_editor', 'gutenberg_enable_content_editor_for_navigation_post_type', 10, 1 ); /** - * Fixes the label of the 'wp_navigation' admin menu entry. + * Rename the menu title from "All Navigation Menus" to "Navigation Menus". */ -function gutenberg_fix_navigation_items_admin_menu_entry() { +function gutenberg_rename_navigation_menus_admin_menu_entry() { global $submenu; if ( ! isset( $submenu['themes.php'] ) ) { return; @@ -511,4 +511,4 @@ function gutenberg_fix_navigation_items_admin_menu_entry() { } } -add_action( 'admin_menu', 'gutenberg_fix_navigation_items_admin_menu_entry' ); +add_action( 'admin_menu', 'gutenberg_rename_navigation_menus_admin_menu_entry' ); From a6abfca97496fe9acbb5b7700a8fd3ee8a590853 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Thu, 4 Nov 2021 16:21:53 +0100 Subject: [PATCH 14/16] Rename variables to improve clarify of the code. --- lib/navigation.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/navigation.php b/lib/navigation.php index 8e77b1a45f2c2..3d80adc7ec9cb 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -499,13 +499,16 @@ function gutenberg_rename_navigation_menus_admin_menu_entry() { if ( ! isset( $submenu['themes.php'] ) ) { return; } + $post_type = get_post_type_object( 'wp_navigation' ); if ( ! $post_type ) { return; } - foreach ( $submenu['themes.php'] as $key => $submenu_entry ) { - if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) { - $submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride + + $menu_title_index = 0; + foreach ( $submenu['themes.php'] as $key => $menu_item ) { + if ( $post_type->labels->all_items === $menu_item[ $menu_title_index ] ) { + $submenu['themes.php'][ $key ][ $menu_title_index ] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride return; } } From 38dbd8e19b65218ccebf46c07ef8515dc8ac667a Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Thu, 4 Nov 2021 16:24:42 +0100 Subject: [PATCH 15/16] Fix description of the class. --- phpunit/navigation-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/navigation-test.php b/phpunit/navigation-test.php index 493487d5418a7..d10f8cd24b835 100644 --- a/phpunit/navigation-test.php +++ b/phpunit/navigation-test.php @@ -1,6 +1,6 @@ Date: Thu, 4 Nov 2021 17:58:07 +0100 Subject: [PATCH 16/16] Fix PHPDOC block. Now description better explains the purpose of this callback function. --- lib/navigation.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/navigation.php b/lib/navigation.php index 3d80adc7ec9cb..1e7d31398c5b9 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -455,8 +455,10 @@ function gutenberg_disable_block_editor_for_navigation_post_type( $value, $post_ add_filter( 'use_block_editor_for_post_type', 'gutenberg_disable_block_editor_for_navigation_post_type', 10, 2 ); /** - * This callback disables content editor for wp_navigation type posts. + * This callback disables the content editor for wp_navigation type posts. * Content editor cannot handle wp_navigation type posts correctly. + * We cannot disable the "editor" feature in the wp_navigation's CPT definition + * because it disables the ability to save navigation blocks via REST API. * * @param WP_Post $post An instance of WP_Post class. */