Make WordPress Core


Ignore:
Timestamp:
11/05/2021 02:14:07 AM (5 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Allow sidebars and their widgets to be public.

By default, only users with the edit_theme_options capability can access the sidebars and widgets REST API endpoints. In this commit, A new show_in_rest parameter is added to the register_sidebar function. When enabled, all users will be able to access that sidebar and any widgets belonging to that sidebar.

This commit reduces the context for a widget's instance information to only edit. This is to ensure that internal widget data is not inadvertently exposed to the public. A future ticket may expose additional APIs to allow widget authors to indicate that their instance data can be safely exposed. REST API consumers intending to access this instance information should take care to explicitly set the context parameter to edit.

Props spacedmonkey, zieladam.
Fixes #53915.

File:
1 edited

Legend:

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

    r51568 r52016  
    166166
    167167        /**
     168         * @ticket 53915
     169         */
     170        public function test_get_items_no_permission_show_in_rest() {
     171                $this->setup_sidebar(
     172                        'sidebar-1',
     173                        array(
     174                                'name'         => 'Test sidebar',
     175                                'show_in_rest' => true,
     176                        )
     177                );
     178                wp_set_current_user( 0 );
     179                $request  = new WP_REST_Request( 'GET', '/wp/v2/sidebars' );
     180                $response = rest_get_server()->dispatch( $request );
     181                $data     = $response->get_data();
     182                $data     = $this->remove_links( $data );
     183                $this->assertSame(
     184                        array(
     185                                array(
     186                                        'id'            => 'sidebar-1',
     187                                        'name'          => 'Test sidebar',
     188                                        'description'   => '',
     189                                        'class'         => '',
     190                                        'before_widget' => '',
     191                                        'after_widget'  => '',
     192                                        'before_title'  => '',
     193                                        'after_title'   => '',
     194                                        'status'        => 'active',
     195                                        'widgets'       => array(),
     196                                ),
     197                        ),
     198                        $data
     199                );
     200        }
     201
     202        /**
     203         * @ticket 53915
     204         */
     205        public function test_get_items_without_show_in_rest_are_removed_from_the_list() {
     206                $this->setup_sidebar(
     207                        'sidebar-1',
     208                        array(
     209                                'name'         => 'Test sidebar 1',
     210                                'show_in_rest' => true,
     211                        )
     212                );
     213                $this->setup_sidebar(
     214                        'sidebar-2',
     215                        array(
     216                                'name'         => 'Test sidebar 2',
     217                                'show_in_rest' => false,
     218                        )
     219                );
     220                $this->setup_sidebar(
     221                        'sidebar-3',
     222                        array(
     223                                'name'         => 'Test sidebar 3',
     224                                'show_in_rest' => true,
     225                        )
     226                );
     227                wp_set_current_user( self::$author_id );
     228                $request  = new WP_REST_Request( 'GET', '/wp/v2/sidebars' );
     229                $response = rest_get_server()->dispatch( $request );
     230                $data     = $response->get_data();
     231                $data     = $this->remove_links( $data );
     232                $this->assertSame(
     233                        array(
     234                                array(
     235                                        'id'            => 'sidebar-1',
     236                                        'name'          => 'Test sidebar 1',
     237                                        'description'   => '',
     238                                        'class'         => '',
     239                                        'before_widget' => '',
     240                                        'after_widget'  => '',
     241                                        'before_title'  => '',
     242                                        'after_title'   => '',
     243                                        'status'        => 'active',
     244                                        'widgets'       => array(),
     245                                ),
     246                                array(
     247                                        'id'            => 'sidebar-3',
     248                                        'name'          => 'Test sidebar 3',
     249                                        'description'   => '',
     250                                        'class'         => '',
     251                                        'before_widget' => '',
     252                                        'after_widget'  => '',
     253                                        'before_title'  => '',
     254                                        'after_title'   => '',
     255                                        'status'        => 'active',
     256                                        'widgets'       => array(),
     257                                ),
     258                        ),
     259                        $data
     260                );
     261        }
     262
     263        /**
    168264         * @ticket 41683
    169265         */
     
    193289                        array(
    194290                                array(
     291                                        'id'            => 'wp_inactive_widgets',
     292                                        'name'          => 'Inactive widgets',
     293                                        'description'   => '',
     294                                        'class'         => '',
     295                                        'before_widget' => '',
     296                                        'after_widget'  => '',
     297                                        'before_title'  => '',
     298                                        'after_title'   => '',
     299                                        'status'        => 'inactive',
     300                                        'widgets'       => array(),
     301                                ),
     302                                array(
    195303                                        'id'            => 'sidebar-1',
    196304                                        'name'          => 'Test sidebar',
     
    207315                        $data
    208316                );
     317
    209318        }
    210319
     
    411520                $response = rest_get_server()->dispatch( $request );
    412521                $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 401 );
     522        }
     523
     524        /**
     525         * @ticket 41683
     526         */
     527        public function test_get_item_no_permission_public() {
     528                wp_set_current_user( 0 );
     529                $this->setup_sidebar(
     530                        'sidebar-1',
     531                        array(
     532                                'name'         => 'Test sidebar',
     533                                'show_in_rest' => true,
     534                        )
     535                );
     536
     537                $request  = new WP_REST_Request( 'GET', '/wp/v2/sidebars/sidebar-1' );
     538                $response = rest_get_server()->dispatch( $request );
     539                $data     = $response->get_data();
     540                $data     = $this->remove_links( $data );
     541                $this->assertSame(
     542                        array(
     543                                'id'            => 'sidebar-1',
     544                                'name'          => 'Test sidebar',
     545                                'description'   => '',
     546                                'class'         => '',
     547                                'before_widget' => '',
     548                                'after_widget'  => '',
     549                                'before_title'  => '',
     550                                'after_title'   => '',
     551                                'status'        => 'active',
     552                                'widgets'       => array(),
     553                        ),
     554                        $data
     555                );
    413556        }
    414557
Note: See TracChangeset for help on using the changeset viewer.