Make WordPress Core


Ignore:
Timestamp:
11/05/2021 02:14:07 AM (2 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-widgets-controller.php

    r51830 r52016  
    242242
    243243    /**
     244     * @ticket 53915
     245     */
     246    public function test_get_items_no_permission_show_in_rest() {
     247        $this->setup_widget(
     248            'text',
     249            1,
     250            array(
     251                'text' => 'Custom text test',
     252            )
     253        );
     254        $this->setup_sidebar(
     255            'sidebar-1',
     256            array(
     257                'name'         => 'Test sidebar',
     258                'show_in_rest' => true,
     259            ),
     260            array( 'text-1', 'testwidget' )
     261        );
     262
     263        $request  = new WP_REST_Request( 'GET', '/wp/v2/widgets' );
     264        $response = rest_get_server()->dispatch( $request );
     265        $data     = $response->get_data();
     266        $data     = $this->remove_links( $data );
     267        $this->assertSameIgnoreEOL(
     268            array(
     269                array(
     270                    'id'       => 'text-1',
     271                    'id_base'  => 'text',
     272                    'sidebar'  => 'sidebar-1',
     273                    'rendered' => '<div class="textwidget">Custom text test</div>',
     274                ),
     275                array(
     276                    'id'       => 'testwidget',
     277                    'id_base'  => 'testwidget',
     278                    'sidebar'  => 'sidebar-1',
     279                    'rendered' => '<h1>Default id</h1><span>Default text</span>',
     280                ),
     281            ),
     282            $data
     283        );
     284    }
     285
     286    /**
     287     * @ticket 53915
     288     */
     289    public function test_get_items_without_show_in_rest_are_removed_from_the_list() {
     290        wp_set_current_user( self::$author_id );
     291        $this->setup_widget(
     292            'text',
     293            1,
     294            array(
     295                'text' => 'Custom text test',
     296            )
     297        );
     298        $this->setup_sidebar(
     299            'sidebar-1',
     300            array(
     301                'name'         => 'Test sidebar 1',
     302                'show_in_rest' => true,
     303            ),
     304            array( 'text-1', 'testwidget' )
     305        );
     306        $this->setup_sidebar(
     307            'sidebar-2',
     308            array(
     309                'name'         => 'Test sidebar 2',
     310                'show_in_rest' => false,
     311            ),
     312            array( 'text-1', 'testwidget' )
     313        );
     314        $request  = new WP_REST_Request( 'GET', '/wp/v2/widgets' );
     315        $response = rest_get_server()->dispatch( $request );
     316        $data     = $response->get_data();
     317        $data     = $this->remove_links( $data );
     318        $this->assertSameIgnoreEOL(
     319            array(
     320                array(
     321                    'id'       => 'text-1',
     322                    'id_base'  => 'text',
     323                    'sidebar'  => 'sidebar-1',
     324                    'rendered' => '<div class="textwidget">Custom text test</div>',
     325                ),
     326                array(
     327                    'id'       => 'testwidget',
     328                    'id_base'  => 'testwidget',
     329                    'sidebar'  => 'sidebar-1',
     330                    'rendered' => '<h1>Default id</h1><span>Default text</span>',
     331                ),
     332            ),
     333            $data
     334        );
     335    }
     336
     337    /**
    244338     * @ticket 41683
    245339     */
     
    296390                    'sidebar'  => 'sidebar-1',
    297391                    'rendered' => '<p>Block test</p>',
    298                     'instance' => array(
    299                         'encoded' => base64_encode(
    300                             serialize(
    301                                 array(
    302                                     'content' => $block_content,
    303                                 )
    304                             )
    305                         ),
    306                         'hash'    => wp_hash(
    307                             serialize(
    308                                 array(
    309                                     'content' => $block_content,
    310                                 )
    311                             )
    312                         ),
    313                         'raw'     => array(
    314                             'content' => $block_content,
    315                         ),
    316                     ),
    317392                ),
    318393                array(
     
    321396                    'sidebar'  => 'sidebar-1',
    322397                    'rendered' => '<a class="rsswidget" href="https://wordpress.org/news/feed"><img class="rss-widget-icon" style="border:0" width="14" height="14" src="http://example.org/wp-includes/images/rss.png" alt="RSS" /></a> <a class="rsswidget" href="https://wordpress.org/news">RSS test</a><ul><li><a class=\'rsswidget\' href=\'https://wordpress.org/news/2020/12/introducing-learn-wordpress/\'>Introducing Learn WordPress</a></li><li><a class=\'rsswidget\' href=\'https://wordpress.org/news/2020/12/simone/\'>WordPress 5.6 “Simone”</a></li><li><a class=\'rsswidget\' href=\'https://wordpress.org/news/2020/12/state-of-the-word-2020/\'>State of the Word 2020</a></li><li><a class=\'rsswidget\' href=\'https://wordpress.org/news/2020/12/the-month-in-wordpress-november-2020/\'>The Month in WordPress: November 2020</a></li><li><a class=\'rsswidget\' href=\'https://wordpress.org/news/2020/12/wordpress-5-6-release-candidate-2/\'>WordPress 5.6 Release Candidate 2</a></li><li><a class=\'rsswidget\' href=\'https://wordpress.org/news/2020/11/wordpress-5-6-release-candidate/\'>WordPress 5.6 Release Candidate</a></li><li><a class=\'rsswidget\' href=\'https://wordpress.org/news/2020/11/wordpress-5-6-beta-4/\'>WordPress 5.6 Beta 4</a></li><li><a class=\'rsswidget\' href=\'https://wordpress.org/news/2020/11/wordpress-5-6-beta-3/\'>WordPress 5.6 Beta 3</a></li><li><a class=\'rsswidget\' href=\'https://wordpress.org/news/2020/11/the-month-in-wordpress-october-2020/\'>The Month in WordPress: October 2020</a></li><li><a class=\'rsswidget\' href=\'https://wordpress.org/news/2020/10/wordpress-5-5-3-maintenance-release/\'>WordPress 5.5.3 Maintenance Release</a></li></ul>',
    323                     'instance' => array(
    324                         'encoded' => base64_encode(
    325                             serialize(
    326                                 array(
    327                                     'title' => 'RSS test',
    328                                     'url'   => 'https://wordpress.org/news/feed',
    329                                 )
    330                             )
    331                         ),
    332                         'hash'    => wp_hash(
    333                             serialize(
    334                                 array(
    335                                     'title' => 'RSS test',
    336                                     'url'   => 'https://wordpress.org/news/feed',
    337                                 )
    338                             )
    339                         ),
    340                     ),
    341398                ),
    342399                array(
     
    345402                    'sidebar'  => 'sidebar-1',
    346403                    'rendered' => '<h1>Default id</h1><span>Default text</span>',
    347                     'instance' => null,
    348404                ),
    349405            ),
     
    470526                'sidebar'  => 'sidebar-1',
    471527                'rendered' => '<div class="textwidget">Custom text test</div>',
    472                 'instance' => array(
    473                     'encoded' => base64_encode(
    474                         serialize(
    475                             array(
    476                                 'text' => 'Custom text test',
    477                             )
    478                         )
    479                     ),
    480                     'hash'    => wp_hash(
    481                         serialize(
    482                             array(
    483                                 'text' => 'Custom text test',
    484                             )
    485                         )
    486                     ),
    487                     'raw'     => array(
    488                         'text' => 'Custom text test',
    489                     ),
    490                 ),
    491528            ),
    492529            $data
     
    542579        $response = rest_get_server()->dispatch( $request );
    543580        $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 403 );
     581    }
     582
     583    /**
     584     * @ticket 53915
     585     */
     586    public function test_get_item_no_permission_show_in_rest() {
     587        wp_set_current_user( 0 );
     588
     589        $this->setup_widget(
     590            'text',
     591            1,
     592            array(
     593                'text' => 'Custom text test',
     594            )
     595        );
     596        $this->setup_sidebar(
     597            'sidebar-1',
     598            array(
     599                'name'         => 'Test sidebar',
     600                'show_in_rest' => true,
     601            ),
     602            array( 'text-1' )
     603        );
     604
     605        $request  = new WP_REST_Request( 'GET', '/wp/v2/widgets/text-1' );
     606        $response = rest_get_server()->dispatch( $request );
     607        $data     = $response->get_data();
     608        $this->assertSameSets(
     609            array(
     610                'id'       => 'text-1',
     611                'id_base'  => 'text',
     612                'sidebar'  => 'sidebar-1',
     613                'rendered' => '<div class="textwidget">Custom text test</div>',
     614            ),
     615            $data
     616        );
    544617    }
    545618
Note: See TracChangeset for help on using the changeset viewer.