@@ -14,9 +14,9 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
1414 public $ widgets ;
1515
1616 /**
17- * Registered widgets .
17+ * Widget instances .
1818 */
19- public $ registered_widgets ;
19+ public $ instances = array () ;
2020
2121 /**
2222 * Sidebars
@@ -28,16 +28,14 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
2828 *
2929 * @param WP_Widget[] $widgets Widget objects.
3030 */
31- public function __construct ( $ widgets, $ registered_widgets ) {
31+ public function __construct ( $ widgets ) {
3232 $ this ->namespace = 'wp/v2 ' ;
3333 $ this ->rest_base = 'widgets ' ;
3434 $ this ->widgets = $ widgets ;
35- $ this -> registered_widgets = $ registered_widgets ;
35+
3636 $ this ->sidebars = wp_get_sidebars_widgets ();
3737
3838 // @todo Now given $this->widgets, inject schema information for Core widgets in lieu of them being in core now. See #35574.
39-
40- add_action ( 'rest_api_init ' , array ( $ this , 'register_routes ' ) );
4139 }
4240
4341 public function register_routes () {
@@ -140,7 +138,20 @@ public function get_items_permissions_check( $request ) {
140138 * @return WP_Error|WP_REST_Response
141139 */
142140 public function get_items ( $ request ) {
143- if ( empty ( $ this ->registered_widgets ) ) {
141+
142+ foreach ( $ this ->widgets as $ widget ) {
143+ $ settings = $ widget ->get_settings ();
144+ foreach ( $ settings as $ key => $ values ) {
145+ $ this ->instances [] = array (
146+ 'id ' => $ widget ->id_base . '- ' . $ key ,
147+ 'array_index ' => $ key ,
148+ 'id_base ' => $ widget ->id_base ,
149+ 'settings ' => $ values ,
150+ );
151+ }
152+ }
153+
154+ if ( empty ( $ this ->instances ) ) {
144155 return rest_ensure_response ( array () );
145156 };
146157
@@ -149,23 +160,23 @@ public function get_items( $request ) {
149160
150161 // TODO pagination
151162
152- $ widgets = array ();
153- foreach ( $ this ->registered_widgets as $ instance_id => $ widget ) {
154- if ( !$ this ->get_instance_permissions_check ( $ instance_id ) ) {
163+ $ instances = array ();
164+ foreach ( $ this ->instances as $ instance ) {
165+ if ( !$ this ->get_instance_permissions_check ( $ instance [ ' id ' ] ) ) {
155166 continue ;
156167 }
157- if ( !is_null ( $ args ['sidebar ' ] ) && $ args ['sidebar ' ] !== $ this ->get_instance_sidebar ( $ instance_id ) ) {
168+ if ( !is_null ( $ args ['sidebar ' ] ) && $ args ['sidebar ' ] !== $ this ->get_instance_sidebar ( $ instance [ ' id ' ] ) ) {
158169 continue ;
159170 }
160- $ data = $ this ->prepare_item_for_response ( $ widget , $ request );
161- $ widgets [] = $ this ->prepare_response_for_collection ( $ data );
171+ $ data = $ this ->prepare_item_for_response ( $ instance , $ request );
172+ $ instances [] = $ this ->prepare_response_for_collection ( $ data );
162173 }
163174
164- if ( !empty ( $ widgets ) && !is_null ( $ args ['sidebar ' ] ) ) {
165- $ widgets = $ this ->sort_widgets_by_sidebar_order ( $ args ['sidebar ' ], $ widgets );
175+ if ( !empty ( $ instances ) && !is_null ( $ args ['sidebar ' ] ) ) {
176+ $ instances = $ this ->sort_widgets_by_sidebar_order ( $ args ['sidebar ' ], $ instances );
166177 }
167178
168- return rest_ensure_response ( $ widgets );
179+ return rest_ensure_response ( $ instances );
169180 }
170181
171182 public function get_item_permissions_check ( $ request ) {
@@ -211,19 +222,19 @@ public function get_instance_sidebar( $id ) {
211222 * Widgets not assigned to the specified sidebar will be discarded.
212223 *
213224 * @param string sidebar Sidebar id
214- * @param array widgets Widgets to sort
225+ * @param array instances Widget instances to sort
215226 * @return array
216227 */
217- public function sort_widgets_by_sidebar_order ( $ sidebar , $ widgets ) {
228+ public function sort_widgets_by_sidebar_order ( $ sidebar , $ instances ) {
218229 if ( empty ( $ this ->sidebars [$ sidebar ] ) ) {
219230 return array ();
220231 }
221232
222233 $ new_widgets = array ();
223234 foreach ( $ this ->sidebars [$ sidebar ] as $ widget_id ) {
224- foreach ( $ widgets as $ widget ) {
225- if ( $ widget_id === $ widget ['id ' ] ) {
226- $ new_widgets [] = $ widget ;
235+ foreach ( $ instances as $ instance ) {
236+ if ( $ widget_id === $ instance ['id ' ] ) {
237+ $ new_widgets [] = $ instance ;
227238 break ;
228239 }
229240 }
@@ -247,28 +258,17 @@ public function delete_item( $request ) {
247258 /**
248259 * Prepare a single widget output for response
249260 *
250- * @param array $widget Widget instance
261+ * @param array $instance Widget instance
251262 * @param WP_REST_Request $request Request object.
252263 * @return WP_REST_Response $data
253264 */
254- public function prepare_item_for_response ( $ widget , $ request ) {
255-
256- $ id = $ widget ['id ' ];
257- $ id_base = $ widget ['callback ' ][0 ]->id_base ;
258- $ array_key = $ widget ['params ' ][0 ]['number ' ];
265+ public function prepare_item_for_response ( $ instance , $ request ) {
259266
260- $ values = array (
261- 'id ' => $ id ,
262- 'type ' => $ id_base ,
263- );
264- if ( !empty ( $ array_key ) ) {
265- $ widgets = get_option ( 'widget_ ' . $ id_base );
266- if ( isset ( $ widgets [$ array_key ] ) ) {
267- $ values = array_merge ( $ values , $ widgets [$ array_key ] );
268- }
269- }
267+ $ values = $ instance ['settings ' ];
268+ $ values ['id ' ] = $ instance ['id ' ];
269+ $ values ['type ' ] = $ instance ['id_base ' ];
270270
271- $ schema = $ this ->get_type_schema ( $ widget [ ' callback ' ][ 0 ]-> id_base );
271+ $ schema = $ this ->get_type_schema ( $ instance [ ' id_base ' ] );
272272
273273 $ data = array ();
274274 foreach ( $ schema ['properties ' ] as $ property_id => $ property ) {
@@ -294,7 +294,7 @@ public function prepare_item_for_response( $widget, $request ) {
294294 * @param array $widget Widget instance.
295295 * @param WP_REST_Request $request Request object.
296296 */
297- return apply_filters ( 'rest_prepare_widget ' , $ response , $ widget , $ request );
297+ return apply_filters ( 'rest_prepare_widget ' , $ response , $ instance , $ request );
298298 }
299299
300300 public function get_item_schema () {
0 commit comments