Make WordPress Core


Ignore:
Timestamp:
01/28/2025 04:07:07 AM (6 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/wpRestMenuLocationsController.php

    r56746 r59718  
    122122
    123123    /**
     124     * @ticket 54304
     125     * @covers ::get_items
     126     */
     127    public function test_get_items_filter() {
     128        $menus = array( 'primary', 'secondary' );
     129        $this->register_nav_menu_locations( array( 'primary', 'secondary' ) );
     130        add_filter( 'rest_menu_read_access', '__return_true' );
     131
     132        $request  = new WP_REST_Request( 'GET', '/wp/v2/menu-locations' );
     133        $response = rest_get_server()->dispatch( $request );
     134        $data     = $response->get_data();
     135        $data     = array_values( $data );
     136        $this->assertCount( 2, $data, 'Number of menu location are not 2' );
     137
     138        $names        = wp_list_pluck( $data, 'name' );
     139        $descriptions = wp_list_pluck( $data, 'description' );
     140        $this->assertSame( $menus, $names );
     141        $menu_descriptions = array_map( 'ucfirst', $names );
     142
     143        $this->assertSame( $menu_descriptions, $descriptions, 'Menu descriptions do not match' );
     144    }
     145
     146    /**
     147     * @ticket 54304
     148     * @covers ::get_item
     149     */
     150    public function test_get_item_filter() {
     151        $menu = 'primary';
     152        $this->register_nav_menu_locations( array( $menu ) );
     153
     154        add_filter( 'rest_menu_read_access', '__return_true' );
     155        $request  = new WP_REST_Request( 'GET', '/wp/v2/menu-locations/' . $menu );
     156        $response = rest_get_server()->dispatch( $request );
     157        $data     = $response->get_data();
     158
     159        $this->assertSame( $menu, $data['name'] );
     160    }
     161
     162    /**
    124163     * @ticket 40878
    125164     * @covers ::get_item
Note: See TracChangeset for help on using the changeset viewer.