Make WordPress Core

Changeset 51385


Ignore:
Timestamp:
07/09/2021 12:59:15 AM (5 years ago)
Author:
desrosj
Message:

REST API: Ensure a sidebar's widgets property is a list.

When a widget is removed from a sidebar, if it was removed from the middle of the list, the widgets property would become an object with numeric keys.

The sidebars controller now forces the widgets property to be a list.

Props walbo, timothyblynjacobs.
Merges [51377] to the 5.8 branch.
Fixes #53612.

Location:
branches/5.8
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.8

  • branches/5.8/src/wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php

    r51317 r51385  
    305305                        );
    306306
    307                         $sidebar['widgets'] = $widgets;
     307                        $sidebar['widgets'] = array_values( $widgets );
    308308                }
    309309
  • branches/5.8/tests/phpunit/tests/rest-api/rest-sidebars-controller.php

    r51305 r51385  
    7676
    7777        private function setup_widget( $option_name, $number, $settings ) {
    78                 update_option(
     78                $this->setup_widgets(
    7979                        $option_name,
    8080                        array(
     
    8282                        )
    8383                );
     84        }
     85
     86        private function setup_widgets( $option_name, $settings ) {
     87                update_option( $option_name, $settings );
    8488        }
    8589
     
    492496
    493497        /**
     498         * @ticket 53612
     499         */
     500        public function test_batch_remove_widgets_from_existing_sidebar() {
     501                wp_widgets_init();
     502
     503                $this->setup_widgets(
     504                        'widget_text',
     505                        array(
     506                                2 => array( 'text' => 'Text widget' ),
     507                                3 => array( 'text' => 'Text widget' ),
     508                                4 => array( 'text' => 'Text widget' ),
     509                                5 => array( 'text' => 'Text widget' ),
     510                                6 => array( 'text' => 'Text widget' ),
     511                        )
     512                );
     513
     514                $this->setup_sidebar(
     515                        'sidebar-1',
     516                        array(
     517                                'name' => 'Test sidebar',
     518                        ),
     519                        array( 'text-2', 'text-3', 'text-4', 'text-5', 'text-6' )
     520                );
     521
     522                $request = new WP_REST_Request( 'POST', '/batch/v1' );
     523                $request->set_body_params(
     524                        array(
     525                                'requests' => array(
     526                                        array(
     527                                                'method' => 'DELETE',
     528                                                'path'   => '/wp/v2/widgets/text-2?force=1',
     529                                        ),
     530                                        array(
     531                                                'method' => 'DELETE',
     532                                                'path'   => '/wp/v2/widgets/text-3?force=1',
     533                                        ),
     534                                ),
     535                        )
     536                );
     537                rest_get_server()->dispatch( $request );
     538
     539                $this->assertSame(
     540                        array( 'text-4', 'text-5', 'text-6' ),
     541                        rest_do_request( '/wp/v2/sidebars/sidebar-1' )->get_data()['widgets']
     542                );
     543        }
     544
     545        /**
    494546         * @ticket 41683
    495547         */
Note: See TracChangeset for help on using the changeset viewer.