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/wpRestMenuItemsController.php

    r58783 r59718  
    178178    public function test_get_item() {
    179179        wp_set_current_user( self::$admin_id );
     180        $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/menu-items/%d', $this->menu_item_id ) );
     181        $response = rest_get_server()->dispatch( $request );
     182
     183        $this->check_get_menu_item_response( $response, 'view' );
     184    }
     185
     186    /**
     187     * @ticket 54304
     188     * @covers ::get_items
     189     */
     190    public function test_get_items_filter() {
     191        add_filter( 'rest_menu_read_access', '__return_true' );
     192        $request  = new WP_REST_Request( 'GET', '/wp/v2/menu-items' );
     193        $response = rest_get_server()->dispatch( $request );
     194
     195        $this->check_get_menu_items_response( $response );
     196    }
     197
     198    /**
     199     * @ticket 54304
     200     * @covers ::get_item
     201     */
     202    public function test_get_item_filter() {
     203        add_filter( 'rest_menu_read_access', '__return_true' );
    180204        $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/menu-items/%d', $this->menu_item_id ) );
    181205        $response = rest_get_server()->dispatch( $request );
Note: See TracChangeset for help on using the changeset viewer.