Skip to content

Commit d72aad0

Browse files
authored
Allow opt out of auto-creation of Navigation fallback (#52319)
1 parent f4c1046 commit d72aad0

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/compat/wordpress-6.3/class-gutenberg-navigation-fallback.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,18 @@ class Gutenberg_Navigation_Fallback {
2323
*/
2424
public static function get_fallback() {
2525

26+
/**
27+
* Filters whether or not a fallback should be created.
28+
*
29+
* @since 6.3.0
30+
*
31+
* @param bool Whether or not to create a fallback.
32+
*/
33+
$should_create_fallback = apply_filters( 'gutenberg_navigation_should_create_fallback', true );
34+
2635
$fallback = static::get_most_recently_published_navigation();
2736

28-
if ( $fallback ) {
37+
if ( $fallback || ! $should_create_fallback ) {
2938
return $fallback;
3039
}
3140

phpunit/class-gutenberg-navigation-fallback-gutenberg-test.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public function test_it_exists() {
3232
$this->assertTrue( class_exists( 'Gutenberg_Navigation_Fallback' ), 'Gutenberg_Navigation_Fallback class should exist.' );
3333
}
3434

35-
3635
/**
3736
* @covers WP_REST_Navigation_Fallback_Controller::get_fallback
3837
*/
@@ -54,6 +53,24 @@ public function test_should_return_a_default_fallback_navigation_menu_in_absence
5453
$this->assertCount( 1, $navs_in_db, 'The fallback Navigation post should be the only one in the database.' );
5554
}
5655

56+
/**
57+
* @covers WP_REST_Navigation_Fallback_Controller::get_fallback
58+
*/
59+
public function test_should_not_automatically_create_fallback_if_filter_is_falsey() {
60+
61+
add_filter( 'gutenberg_navigation_should_create_fallback', '__return_false' );
62+
63+
$data = Gutenberg_Navigation_Fallback::get_fallback();
64+
65+
$this->assertEmpty( $data );
66+
67+
$navs_in_db = $this->get_navigations_in_database();
68+
69+
$this->assertCount( 0, $navs_in_db, 'The fallback Navigation post should not have been created.' );
70+
71+
remove_filter( 'gutenberg_navigation_should_create_fallback', '__return_false' );
72+
}
73+
5774
/**
5875
* @covers WP_REST_Navigation_Fallback_Controller::get_fallback
5976
*/

0 commit comments

Comments
 (0)