Skip to content

Commit

Permalink
Allow opt out of auto-creation of Navigation fallback (#52319)
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave authored and tellthemachines committed Jul 7, 2023
1 parent ef51622 commit 02fb4a7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/compat/wordpress-6.3/class-gutenberg-navigation-fallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
19 changes: 18 additions & 1 deletion phpunit/class-gutenberg-navigation-fallback-gutenberg-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down

0 comments on commit 02fb4a7

Please sign in to comment.