From a02886a868e4bdeb586658926607bdffdd65a7f2 Mon Sep 17 00:00:00 2001 From: Isabel Brison Date: Tue, 11 Jul 2023 05:39:33 +0000 Subject: [PATCH] Editor: opt out of Navigation fallback. Allows developers to opt out of the auto-creation of the Navigation fallback through a filter. Props get_dave, spacedmonkey, ramonopoly. Fixes #58750. git-svn-id: https://develop.svn.wordpress.org/trunk@56202 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-navigation-fallback.php | 11 +++++++++- .../tests/editor/navigation-fallback.php | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-navigation-fallback.php b/src/wp-includes/class-wp-navigation-fallback.php index 6df4993a0f..c99b71a41a 100644 --- a/src/wp-includes/class-wp-navigation-fallback.php +++ b/src/wp-includes/class-wp-navigation-fallback.php @@ -26,9 +26,18 @@ class WP_Navigation_Fallback { */ public static function get_fallback() { + /** + * Filters whether or not a fallback should be created. + * + * @since 6.3.0 + * + * @param bool Whether to create a fallback navigation menu. Default true. + */ + $should_create_fallback = apply_filters( 'wp_navigation_should_create_fallback', true ); + $fallback = static::get_most_recently_published_navigation(); - if ( $fallback ) { + if ( $fallback || ! $should_create_fallback ) { return $fallback; } diff --git a/tests/phpunit/tests/editor/navigation-fallback.php b/tests/phpunit/tests/editor/navigation-fallback.php index 3d73b72a4d..46befacc3c 100644 --- a/tests/phpunit/tests/editor/navigation-fallback.php +++ b/tests/phpunit/tests/editor/navigation-fallback.php @@ -56,6 +56,26 @@ class WP_Navigation_Fallback_Test extends WP_UnitTestCase { $this->assertCount( 1, $navs_in_db, 'The fallback Navigation post should be the only one in the database.' ); } + /** + * @ticket 58750 + * + * @covers WP_REST_Navigation_Fallback_Controller::get_fallback + */ + public function test_should_not_automatically_create_fallback_if_filter_is_falsey() { + + add_filter( 'wp_navigation_should_create_fallback', '__return_false' ); + + $data = WP_Navigation_Fallback::get_fallback(); + + $this->assertEmpty( $data ); + + $navs_in_db = $this->get_navigations_in_database(); + + $this->assertCount( 0, $navs_in_db, 'The fallback Navigation post should not have been created.' ); + + remove_filter( 'wp_navigation_should_create_fallback', '__return_false' ); + } + /** * @ticket 58557 * @covers WP_REST_Navigation_Fallback_Controller::get_fallback