Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow developers to opt out of auto-creation of Navigation fallback #52319

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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