diff --git a/lib/compat/wordpress-6.3/class-gutenberg-navigation-fallback.php b/lib/compat/wordpress-6.3/class-gutenberg-navigation-fallback.php index 91417971e22c73..fcf6e13b0954d7 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-navigation-fallback.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-navigation-fallback.php @@ -23,9 +23,18 @@ class Gutenberg_Navigation_Fallback { */ public static function get_fallback() { + /** + * Filters whether or not a fallback should be created. + * + * @since 6.3.0 + * + * @param bool Whether or not to create a fallback. + */ + $should_create_fallback = apply_filters( 'gutenberg_navigation_should_create_fallback', true ); + $fallback = static::get_most_recently_published_navigation(); - if ( $fallback ) { + if ( $fallback || ! $should_create_fallback ) { return $fallback; } diff --git a/phpunit/class-gutenberg-navigation-fallback-gutenberg-test.php b/phpunit/class-gutenberg-navigation-fallback-gutenberg-test.php index 7b0719950df8b5..edc48f1c71761b 100644 --- a/phpunit/class-gutenberg-navigation-fallback-gutenberg-test.php +++ b/phpunit/class-gutenberg-navigation-fallback-gutenberg-test.php @@ -32,7 +32,6 @@ public function test_it_exists() { $this->assertTrue( class_exists( 'Gutenberg_Navigation_Fallback' ), 'Gutenberg_Navigation_Fallback class should exist.' ); } - /** * @covers WP_REST_Navigation_Fallback_Controller::get_fallback */ @@ -54,6 +53,24 @@ public function test_should_return_a_default_fallback_navigation_menu_in_absence $this->assertCount( 1, $navs_in_db, 'The fallback Navigation post should be the only one in the database.' ); } + /** + * @covers WP_REST_Navigation_Fallback_Controller::get_fallback + */ + public function test_should_not_automatically_create_fallback_if_filter_is_falsey() { + + add_filter( 'gutenberg_navigation_should_create_fallback', '__return_false' ); + + $data = Gutenberg_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( 'gutenberg_navigation_should_create_fallback', '__return_false' ); + } + /** * @covers WP_REST_Navigation_Fallback_Controller::get_fallback */