Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #16 from WP-API/use_wp_widget_get_settings
Browse files Browse the repository at this point in the history
Use WP_Widget::get_settings() instead of global
  • Loading branch information
NateWr committed Feb 1, 2016
2 parents ac0b9d6 + 03cc526 commit b0cf078
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 45 deletions.
78 changes: 39 additions & 39 deletions lib/class-wp-rest-widgets-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
public $widgets;

/**
* Registered widgets.
* Widget instances.
*/
public $registered_widgets;
public $instances = array();

/**
* Sidebars
Expand All @@ -28,16 +28,14 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
*
* @param WP_Widget[] $widgets Widget objects.
*/
public function __construct( $widgets, $registered_widgets ) {
public function __construct( $widgets ) {
$this->namespace = 'wp/v2';
$this->rest_base = 'widgets';
$this->widgets = $widgets;
$this->registered_widgets = $registered_widgets;

$this->sidebars = wp_get_sidebars_widgets();

// @todo Now given $this->widgets, inject schema information for Core widgets in lieu of them being in core now. See #35574.

add_action( 'rest_api_init', array( $this, 'register_routes' ) );
}

public function register_routes() {
Expand Down Expand Up @@ -140,7 +138,20 @@ public function get_items_permissions_check( $request ) {
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
if ( empty( $this->registered_widgets ) ) {

foreach( $this->widgets as $widget ) {
$settings = $widget->get_settings();
foreach( $settings as $key => $values ) {
$this->instances[] = array(
'id' => $widget->id_base . '-' . $key,
'array_index' => $key,
'id_base' => $widget->id_base,
'settings' => $values,
);
}
}

if ( empty( $this->instances ) ) {
return rest_ensure_response( array() );
};

Expand All @@ -149,23 +160,23 @@ public function get_items( $request ) {

// TODO pagination

$widgets = array();
foreach( $this->registered_widgets as $instance_id => $widget ) {
if ( !$this->get_instance_permissions_check( $instance_id ) ) {
$instances = array();
foreach( $this->instances as $instance ) {
if ( !$this->get_instance_permissions_check( $instance['id'] ) ) {
continue;
}
if ( !is_null( $args['sidebar'] ) && $args['sidebar'] !== $this->get_instance_sidebar( $instance_id ) ) {
if ( !is_null( $args['sidebar'] ) && $args['sidebar'] !== $this->get_instance_sidebar( $instance['id'] ) ) {
continue;
}
$data = $this->prepare_item_for_response( $widget, $request );
$widgets[] = $this->prepare_response_for_collection( $data );
$data = $this->prepare_item_for_response( $instance, $request );
$instances[] = $this->prepare_response_for_collection( $data );
}

if ( !empty( $widgets ) && !is_null( $args['sidebar'] ) ) {
$widgets = $this->sort_widgets_by_sidebar_order( $args['sidebar'], $widgets );
if ( !empty( $instances ) && !is_null( $args['sidebar'] ) ) {
$instances = $this->sort_widgets_by_sidebar_order( $args['sidebar'], $instances );
}

return rest_ensure_response( $widgets );
return rest_ensure_response( $instances );
}

public function get_item_permissions_check( $request ) {
Expand Down Expand Up @@ -211,19 +222,19 @@ public function get_instance_sidebar( $id ) {
* Widgets not assigned to the specified sidebar will be discarded.
*
* @param string sidebar Sidebar id
* @param array widgets Widgets to sort
* @param array instances Widget instances to sort
* @return array
*/
public function sort_widgets_by_sidebar_order( $sidebar, $widgets ) {
public function sort_widgets_by_sidebar_order( $sidebar, $instances ) {
if ( empty( $this->sidebars[$sidebar] ) ) {
return array();
}

$new_widgets = array();
foreach( $this->sidebars[$sidebar] as $widget_id ) {
foreach( $widgets as $widget ) {
if ( $widget_id === $widget['id'] ) {
$new_widgets[] = $widget;
foreach( $instances as $instance ) {
if ( $widget_id === $instance['id'] ) {
$new_widgets[] = $instance;
break;
}
}
Expand All @@ -247,28 +258,17 @@ public function delete_item( $request ) {
/**
* Prepare a single widget output for response
*
* @param array $widget Widget instance
* @param array $instance Widget instance
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $data
*/
public function prepare_item_for_response( $widget, $request ) {

$id = $widget['id'];
$id_base = $widget['callback'][0]->id_base;
$array_key = $widget['params'][0]['number'];
public function prepare_item_for_response( $instance, $request ) {

$values = array(
'id' => $id,
'type' => $id_base,
);
if ( !empty( $array_key ) ) {
$widgets = get_option( 'widget_' . $id_base );
if ( isset( $widgets[$array_key] ) ) {
$values = array_merge( $values, $widgets[$array_key] );
}
}
$values = $instance['settings'];
$values['id'] = $instance['id'];
$values['type'] = $instance['id_base'];

$schema = $this->get_type_schema( $widget['callback'][0]->id_base );
$schema = $this->get_type_schema( $instance['id_base'] );

$data = array();
foreach( $schema['properties'] as $property_id => $property ) {
Expand All @@ -294,7 +294,7 @@ public function prepare_item_for_response( $widget, $request ) {
* @param array $widget Widget instance.
* @param WP_REST_Request $request Request object.
*/
return apply_filters( 'rest_prepare_widget', $response, $widget, $request );
return apply_filters( 'rest_prepare_widget', $response, $instance, $request );
}

public function get_item_schema() {
Expand Down
7 changes: 1 addition & 6 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ function wp_api_nav_menus_widgets_init_controllers() {
*/
global $wp_widget_factory;

/**
* @type array $wp_registered_widgets
*/
global $wp_registered_widgets;

$widgets_controller = new WP_REST_Widgets_Controller( $wp_widget_factory->widgets, $wp_registered_widgets );
$widgets_controller = new WP_REST_Widgets_Controller( $wp_widget_factory->widgets );
$widgets_controller->register_routes();
}

0 comments on commit b0cf078

Please sign in to comment.