Make WordPress Core


Ignore:
Timestamp:
01/28/2025 04:07:07 AM (5 months ago)
Author:
spacedmonkey
Message:

REST API: Introduce filter for controlling menu read access.

The menu, menu item, and menu location endpoints were added to the REST API in [52079]. In that commit, menu data was treated as private and restricted to logged-in users with the edit_theme_options capability. However, in many cases, this data can be considered public. Previously, there was no simple way for developers to allow this data to be exposed via the REST API.

This commit introduces the rest_menu_read_access filter, enabling developers to control read access to menus, menu items, and menu locations in the REST API. The same filter is applied across all three REST API classes, simplifying the process of opting into exposing this data.

Each instance of the filter provides the current request and the relevant class instance as context, allowing developers to selectively or globally enable access to the data.

Props spacedmonkey, antonvlasenko, kadamwhite, julianmar, masteradhoc.
Fixes #54304.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/wpRestMenusController.php

    r56746 r59718  
    209209    }
    210210
     211
     212    /**
     213     * @ticket 54304
     214     * @covers ::get_items
     215     */
     216    public function test_get_items_filter() {
     217        add_filter( 'rest_menu_read_access', '__return_true' );
     218        wp_update_nav_menu_object(
     219            0,
     220            array(
     221                'description' => 'Test get',
     222                'menu-name'   => 'test Name get',
     223            )
     224        );
     225        $request = new WP_REST_Request( 'GET', '/wp/v2/menus' );
     226        $request->set_param( 'per_page', self::$per_page );
     227        $response = rest_get_server()->dispatch( $request );
     228        $this->check_get_taxonomy_terms_response( $response );
     229    }
     230
     231    /**
     232     * @ticket 54304
     233     * @covers ::get_item
     234     */
     235    public function test_get_item_filter() {
     236        add_filter( 'rest_menu_read_access', '__return_true' );
     237        $nav_menu_id = wp_update_nav_menu_object(
     238            0,
     239            array(
     240                'description' => 'Test menu',
     241                'menu-name'   => 'test Name',
     242            )
     243        );
     244
     245        $this->register_nav_menu_locations( array( 'primary' ) );
     246        set_theme_mod( 'nav_menu_locations', array( 'primary' => $nav_menu_id ) );
     247
     248        $request  = new WP_REST_Request( 'GET', '/wp/v2/menus/' . $nav_menu_id );
     249        $response = rest_get_server()->dispatch( $request );
     250        $this->check_get_taxonomy_term_response( $response, $nav_menu_id );
     251    }
     252
    211253    /**
    212254     * @ticket 40878
Note: See TracChangeset for help on using the changeset viewer.