Make WordPress Core

Changeset 56202


Ignore:
Timestamp:
07/11/2023 05:39:33 AM (15 months ago)
Author:
isabel_brison
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-navigation-fallback.php

    r56052 r56202  
    2727    public static function get_fallback() {
    2828
     29        /**
     30         * Filters whether or not a fallback should be created.
     31         *
     32         * @since 6.3.0
     33         *
     34         * @param bool Whether to create a fallback navigation menu. Default true.
     35         */
     36        $should_create_fallback = apply_filters( 'wp_navigation_should_create_fallback', true );
     37
    2938        $fallback = static::get_most_recently_published_navigation();
    3039
    31         if ( $fallback ) {
     40        if ( $fallback || ! $should_create_fallback ) {
    3241            return $fallback;
    3342        }
  • trunk/tests/phpunit/tests/editor/navigation-fallback.php

    r56052 r56202  
    5858
    5959    /**
     60     * @ticket 58750
     61     *
     62     * @covers WP_REST_Navigation_Fallback_Controller::get_fallback
     63     */
     64    public function test_should_not_automatically_create_fallback_if_filter_is_falsey() {
     65
     66        add_filter( 'wp_navigation_should_create_fallback', '__return_false' );
     67
     68        $data = WP_Navigation_Fallback::get_fallback();
     69
     70        $this->assertEmpty( $data );
     71
     72        $navs_in_db = $this->get_navigations_in_database();
     73
     74        $this->assertCount( 0, $navs_in_db, 'The fallback Navigation post should not have been created.' );
     75
     76        remove_filter( 'wp_navigation_should_create_fallback', '__return_false' );
     77    }
     78
     79    /**
    6080     * @ticket 58557
    6181     * @covers WP_REST_Navigation_Fallback_Controller::get_fallback
Note: See TracChangeset for help on using the changeset viewer.