Skip to content

Commit

Permalink
[edit-widgets] Improve the UX of wp_inactive_widgets on the experimen…
Browse files Browse the repository at this point in the history
…tal screen (WordPress#24790)

* Remove wp_inactive_widgets from the experimental screen

* Ensure inactive widgets are saved/retrieved correctly

* Lint

* Add unit tests for inactive widgets API behavior

* Rebase

* Move inactive widgets area to the bottom
  • Loading branch information
adamziel authored Aug 26, 2020
1 parent 652179e commit e7b42e4
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 9 deletions.
17 changes: 12 additions & 5 deletions lib/class-wp-rest-sidebars-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,13 @@ public static function get_sidebar( $id ) {
public static function get_widgets( $sidebar_id ) {
global $wp_registered_widgets, $wp_registered_sidebars;

$widgets = array();
$sidebars_widgets = (array) wp_get_sidebars_widgets();
$widgets = array();
$sidebars_widgets = (array) wp_get_sidebars_widgets();
$registered_sidebar = isset( $wp_registered_sidebars[ $sidebar_id ] ) ? $wp_registered_sidebars[ $sidebar_id ] : (
'wp_inactive_widgets' === $sidebar_id ? array() : null
);

if ( isset( $wp_registered_sidebars[ $sidebar_id ] ) && isset( $sidebars_widgets[ $sidebar_id ] ) ) {
if ( null !== $registered_sidebar && isset( $sidebars_widgets[ $sidebar_id ] ) ) {
foreach ( $sidebars_widgets[ $sidebar_id ] as $widget_id ) {
// Just to be sure.
if ( isset( $wp_registered_widgets[ $widget_id ] ) ) {
Expand All @@ -303,7 +306,7 @@ public static function get_widgets( $sidebar_id ) {
$widget_parameters = array_merge(
array(
array_merge(
$wp_registered_sidebars[ $sidebar_id ],
$registered_sidebar,
array(
'widget_id' => $widget_id,
'widget_name' => $widget['name'],
Expand Down Expand Up @@ -341,7 +344,7 @@ public static function get_widgets( $sidebar_id ) {
$instance = $widget['callback'][0];
$widget['widget_class'] = get_class( $instance );
$widget['settings'] = static::get_sidebar_widget_instance(
$wp_registered_sidebars[ $sidebar_id ],
$registered_sidebar,
$widget_id
);
$widget['number'] = (int) $widget['params'][0]['number'];
Expand Down Expand Up @@ -383,6 +386,10 @@ public function prepare_item_for_response( $raw_sidebar, $request ) {
$sidebar['status'] = 'inactive';
}

if ( 'wp_inactive_widgets' === $sidebar['id'] && empty( $sidebar['name'] ) ) {
$sidebar['name'] = __( 'Inactive widgets', 'gutenberg' );
}

$fields = $this->get_fields_for_response( $request );
if ( rest_is_field_included( 'widgets', $fields ) ) {
$sidebar['widgets'] = self::get_widgets( $sidebar['id'] );
Expand Down
11 changes: 10 additions & 1 deletion packages/edit-widgets/src/store/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ export function* getWidgetAreas() {

const widgetAreaBlocks = [];
const widgetIdToClientId = {};
for ( const widgetArea of widgetAreas ) {
const sortedWidgetAreas = widgetAreas.sort( ( a, b ) => {
if ( a.id === 'wp_inactive_widgets' ) {
return 1;
}
if ( b.id === 'wp_inactive_widgets' ) {
return -1;
}
return 0;
} );
for ( const widgetArea of sortedWidgetAreas ) {
const widgetBlocks = [];
for ( const widget of widgetArea.widgets ) {
const block = transformWidgetToBlock( widget );
Expand Down
8 changes: 5 additions & 3 deletions packages/edit-widgets/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ export const getWidgetAreas = createRegistrySelector( ( select ) => () => {
}

const query = buildWidgetAreasQuery();
return select( 'core' )
.getEntityRecords( KIND, WIDGET_AREA_ENTITY_TYPE, query )
.filter( ( { id } ) => id !== 'wp_inactive_widgets' );
return select( 'core' ).getEntityRecords(
KIND,
WIDGET_AREA_ENTITY_TYPE,
query
);
} );

export const getEditedWidgetAreas = createRegistrySelector(
Expand Down
85 changes: 85 additions & 0 deletions phpunit/class-rest-sidebars-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,91 @@ function() {
);
}

/**
*
*/
public function test_get_items_inactive_widgets() {
$this->setup_widget(
'widget_rss',
1,
array(
'title' => 'RSS test',
)
);
$this->setup_widget(
'widget_text',
1,
array(
'text' => 'Custom text test',
)
);
$this->setup_sidebar(
'sidebar-1',
array(
'name' => 'Test sidebar',
),
array( 'text-1' )
);
update_option(
'sidebars_widgets',
array_merge(
get_option( 'sidebars_widgets' ),
array(
'wp_inactive_widgets' => array( 'rss-1', 'rss' ),
)
)
);

$request = new WP_REST_Request( 'GET', '/__experimental/sidebars' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals(
array(
array(
'id' => 'sidebar-1',
'name' => 'Test sidebar',
'description' => '',
'status' => 'active',
'widgets' => array(
array(
'id' => 'text-1',
'settings' => array(
'text' => 'Custom text test',
),
'id_base' => 'text',
'widget_class' => 'WP_Widget_Text',
'name' => 'Text',
'description' => 'Arbitrary text.',
'number' => 1,
'rendered' => '<div class="textwidget">Custom text test</div>',
),
),
),
array(
'id' => 'wp_inactive_widgets',
'name' => 'Inactive widgets',
'description' => '',
'status' => 'inactive',
'widgets' => array(
array(
'id' => 'rss-1',
'settings' => array(
'title' => 'RSS test',
),
'id_base' => 'rss',
'widget_class' => 'WP_Widget_RSS',
'name' => 'RSS',
'description' => 'Entries from any RSS or Atom feed.',
'number' => 1,
'rendered' => '',
),
),
),
),
$data
);
}

/**
* The test_delete_item() method does not exist for sidebar.
*/
Expand Down

0 comments on commit e7b42e4

Please sign in to comment.