Skip to content

Commit

Permalink
Widgets API: Fix null instance property when instance settings are em…
Browse files Browse the repository at this point in the history
…pty (#30713)

* Widgets API: Fix null instance property when instance settings are empty

* Use new stdClass so that JSON result is {} and not []
  • Loading branch information
noisysocks authored Apr 14, 2021
1 parent 5b49012 commit 98c089e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
9 changes: 2 additions & 7 deletions lib/class-wp-rest-widget-types-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,13 +582,8 @@ public function encode_form_data( $request ) {
);

if ( ! empty( $widget_object->show_instance_in_rest ) ) {
if ( empty( $instance ) ) {
// Use new stdClass() instead of array() so that endpoint
// returns {} and not [].
$response['instance']['raw'] = new stdClass;
} else {
$response['instance']['raw'] = $instance;
}
// Use new stdClass so that JSON result is {} and not [].
$response['instance']['raw'] = empty( $instance ) ? new stdClass : $instance;
}

return rest_ensure_response( $response );
Expand Down
27 changes: 23 additions & 4 deletions lib/class-wp-rest-widgets-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,13 +537,14 @@ public function prepare_item_for_response( $item, $request ) {
$widget_object = gutenberg_get_widget_object( $parsed_id['id_base'] );
$instance = gutenberg_get_widget_instance( $widget_id );

if ( $instance ) {
if ( ! is_null( $instance ) ) {
$serialized_instance = serialize( $instance );
$prepared['instance']['encoded'] = base64_encode( $serialized_instance );
$prepared['instance']['hash'] = wp_hash( $serialized_instance );

if ( ! empty( $widget_object->show_instance_in_rest ) ) {
$prepared['instance']['raw'] = $instance;
// Use new stdClass so that JSON result is {} and not [].
$prepared['instance']['raw'] = empty( $instance ) ? new stdClass : $instance;
}
}
}
Expand All @@ -555,8 +556,9 @@ public function prepare_item_for_response( $item, $request ) {
$prepared['description'] = ! empty( $widget['description'] ) ? $widget['description'] : '';
$prepared['number'] = $widget_object ? (int) $parsed_id['number'] : 0;
if ( rest_is_field_included( 'settings', $fields ) ) {
$instance = gutenberg_get_widget_instance( $widget_id );
$prepared['settings'] = $instance ? $instance : array();
$instance = gutenberg_get_widget_instance( $widget_id );
// Use new stdClass so that JSON result is {} and not [].
$prepared['settings'] = empty( $instance ) ? new stdClass : $instance;
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
Expand Down Expand Up @@ -675,6 +677,23 @@ public function get_item_schema() {
'type' => 'object',
'context' => array( 'view', 'edit', 'embed' ),
'default' => null,
'properties' => array(
'encoded' => array(
'description' => __( 'Base64 encoded representation of the instance settings.', 'gutenberg' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
),
'hash' => array(
'description' => __( 'Cryptographic hash of the instance settings.', 'gutenberg' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
),
'raw' => array(
'description' => __( 'Unencoded instance settings, if supported.', 'gutenberg' ),
'type' => 'object',
'context' => array( 'view', 'edit', 'embed' ),
),
),
),
'form_data' => array(
'description' => __( 'URL-encoded form data from the widget admin form. Used to update a widget that does not support instance. Write only.', 'gutenberg' ),
Expand Down
12 changes: 6 additions & 6 deletions phpunit/class-rest-widgets-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public function test_get_items() {
array(
'id' => 'testwidget',
'sidebar' => 'sidebar-1',
'settings' => array(),
'settings' => new stdClass,
'instance' => null,
'id_base' => 'testwidget',
'widget_class' => '',
Expand All @@ -339,7 +339,7 @@ public function test_get_items() {
'rendered' => '<h1>Default id</h1><span>Default text</span>',
),
),
$data
(array) $data
);

$wp_widget_factory->widgets['WP_Widget_RSS']->show_instance_in_rest = true;
Expand Down Expand Up @@ -410,7 +410,7 @@ public function test_get_items_edit_context() {
array(
'id' => 'testwidget',
'sidebar' => 'sidebar-1',
'settings' => array(),
'settings' => new stdClass,
'instance' => null,
'id_base' => 'testwidget',
'widget_class' => '',
Expand Down Expand Up @@ -1132,7 +1132,7 @@ public function test_update_item_legacy_widget() {
array(
'id' => 'testwidget',
'sidebar' => 'sidebar-1',
'settings' => array(),
'settings' => new stdClass,
'instance' => null,
'rendered' => '<h1>My test id</h1><span>My test title</span>',
'name' => 'WP test widget',
Expand Down Expand Up @@ -1180,7 +1180,7 @@ public function test_create_item_legacy_widget() {
array(
'id' => 'testwidget',
'sidebar' => 'sidebar-1',
'settings' => array(),
'settings' => new stdClass,
'instance' => null,
'rendered' => '<h1>My test id</h1><span>My test title</span>',
'name' => 'WP test widget',
Expand Down Expand Up @@ -1501,7 +1501,7 @@ protected function remove_links( $data ) {
}
$count = 0;
foreach ( $data as $item ) {
if ( isset( $item['_links'] ) ) {
if ( is_array( $item ) && isset( $item['_links'] ) ) {
unset( $data[ $count ]['_links'] );
}
$count ++;
Expand Down

0 comments on commit 98c089e

Please sign in to comment.