[wp-trac] [WordPress Trac] #54304: Expose menu data public in Menus REST API

WordPress Trac noreply at wordpress.org
Wed Oct 9 17:48:46 UTC 2024


#54304: Expose menu data public in Menus REST API
---------------------------------------------------+-----------------------
 Reporter:  spacedmonkey                           |       Owner:  (none)
     Type:  enhancement                            |      Status:  new
 Priority:  normal                                 |   Milestone:  6.8
Component:  REST API                               |     Version:  4.7
 Severity:  normal                                 |  Resolution:
 Keywords:  has-patch needs-testing needs-refresh  |     Focuses:  rest-api
---------------------------------------------------+-----------------------

Comment (by spacedmonkey):

 The I have the filter setup is like this

 {{{#!php
 /**
                  * Filters whether the current user has read access to
 menu items via the REST API.
                  *
                  * @since 6.8.0
                  * @param $read_only_access bool Whether the current user
 has read access to menu items via the REST API.
                  * @param $request WP_REST_Request Full details about the
 request.
                  * @param $this WP_REST_Controller The current instance of
 the controller.
                  */
                 $read_only_access = apply_filters(
 'rest_menu_read_access', false, $request, $this );
 }}}
 This filter has `WP_REST_Request` and current controller
 `WP_REST_Controller` as context. If you, for example wanted to limit the
 filter, you could do this.
 {{{#!php

 add_filter( 'rest_menu_read_access', function( $current, $request,
 $controller ){
   if ( $controller instanceof WP_REST_Menu_Locations_Controller ) {
     return false;
   }

   return true;
 }, 10, 3 );

 }}}

 You can also limit by the request,

 {{{#!php
 add_filter( 'rest_menu_read_access', function( $current, $request){
   if ( ! empty( $request['id'] ) ) {
     return false;
   }

   return true;
 }, 10, 2 );
 }}}

 I personally can't think of how else you might want to limit this access.
 But this filter seems to cover all bases.

 Are you happy @kadamwhite ?

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/54304#comment:23>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list