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
This commit is contained in:
Isabel Brison 2023-07-11 05:39:33 +00:00
parent eea94adc54
commit a02886a868
2 changed files with 30 additions and 1 deletions

View File

@ -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;
}

View File

@ -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