Make WordPress Core

Changeset 51049


Ignore:
Timestamp:
05/31/2021 08:56:22 PM (4 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Remove duplicates in the widget types endpoint.

Props noisysocks, spacedmonkey, imath, isabel_brison.
Fixes #53305.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php

    r50995 r51049  
    226226            $widget['classname'] = ltrim( $classname, '_' );
    227227
    228             $widgets[] = $widget;
     228            $widgets[ $widget['id'] ] = $widget;
    229229        }
    230230
  • trunk/tests/phpunit/tests/rest-api/rest-widget-types-controller.php

    r50995 r51049  
    6262    }
    6363
     64    private function setup_widget( $id_base, $number, $settings ) {
     65        global $wp_widget_factory;
     66
     67        $option_name = "widget_$id_base";
     68        update_option(
     69            $option_name,
     70            array(
     71                $number => $settings,
     72            )
     73        );
     74
     75        $widget_object = $wp_widget_factory->get_widget_object( $id_base );
     76        $widget_object->_set( $number );
     77        $widget_object->_register_one( $number );
     78    }
     79
    6480    /**
    6581     * @ticket 41683
     
    107123            $this->check_widget_type_object( $widget_type, $item, $item['_links'] );
    108124        }
    109 
     125    }
     126
     127    /**
     128     * @ticket 53305
     129     */
     130    public function test_get_items_removes_duplicates() {
     131        wp_set_current_user( self::$admin_id );
     132        $this->setup_widget(
     133            'text',
     134            1,
     135            array(
     136                'text' => 'Custom text test',
     137            )
     138        );
     139        $this->setup_widget(
     140            'text',
     141            2,
     142            array(
     143                'text' => 'Custom text test',
     144            )
     145        );
     146        $request      = new WP_REST_Request( 'GET', '/wp/v2/widget-types' );
     147        $response     = rest_get_server()->dispatch( $request );
     148        $data         = $response->get_data();
     149        $text_widgets = array_filter(
     150            $data,
     151            function( $widget ) {
     152                return 'text' === $widget['id'];
     153            }
     154        );
     155        $this->assertCount( 1, $text_widgets );
    110156    }
    111157
Note: See TracChangeset for help on using the changeset viewer.