Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reference widgets to legacy widget blocks. Use ajax admin endpoint on this blocks. #15801

Merged
merged 4 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions lib/class-experimental-wp-widget-blocks-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,26 @@ public static function get_sidebar_as_blocks( $sidebar_id ) {
$wp_registered_sidebars = self::get_wp_registered_sidebars();

foreach ( $sidebars_items[ $sidebar_id ] as $item ) {
$widget_class = self::get_widget_class( $item );
$blocks[] = array(
$widget_class = self::get_widget_class( $item );
list( $object, $number ) = self::get_widget_info( $item );
$new_block = array(
'blockName' => 'core/legacy-widget',
'attrs' => array(
'class' => $widget_class,
'identifier' => $item,
'instance' => self::get_sidebar_widget_instance( $wp_registered_sidebars[ $sidebar_id ], $item ),
'id' => $item,
'instance' => self::get_sidebar_widget_instance( $wp_registered_sidebars[ $sidebar_id ], $item ),
),
'innerHTML' => '',
);
if ( null !== $widget_class ) {
$new_block['attrs']['widgetClass'] = $widget_class;
}
if ( isset( $object->id_base ) ) {
$new_block['attrs']['idBase'] = $object->id_base;
}
if ( is_int( $number ) ) {
$new_block['attrs']['number'] = $number;
}
$blocks[] = $new_block;
}
return $blocks;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Start: Include for phase 2
* Widget Updater REST API: WP_REST_Widget_Updater_Controller class
* Widget Updater REST API: WP_REST_Widget_Forms class
*
* @package gutenberg
* @since 5.2.0
Expand All @@ -14,16 +14,16 @@
*
* @see WP_REST_Controller
*/
class WP_REST_Widget_Updater_Controller extends WP_REST_Controller {
class WP_REST_Widget_Forms extends WP_REST_Controller {

/**
* Constructs the controller.
*
* @access public
*/
public function __construct() {
$this->namespace = 'wp/v2';
$this->rest_base = 'widgets';
$this->namespace = '__experimental';
$this->rest_base = 'widget-forms';
}

/**
Expand All @@ -35,12 +35,24 @@ public function register_routes() {
register_rest_route(
$this->namespace,
// Regex representing a PHP class extracted from http://php.net/manual/en/language.oop5.basic.php.
'/' . $this->rest_base . '/(?P<identifier>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/',
'/' . $this->rest_base . '/(?P<widget_class>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/',
array(
'args' => array(
'identifier' => array(
'description' => __( 'Class name of the widget.', 'gutenberg' ),
'type' => 'string',
'widget_class' => array(
'description' => __( 'Class name of the widget.', 'gutenberg' ),
'type' => 'string',
'required' => true,
'validate_callback' => array( $this, 'is_valid_widget' ),
),
'instance' => array(
'description' => __( 'Current widget instance', 'gutenberg' ),
'type' => 'object',
'default' => array(),
),
'instance_changes' => array(
'description' => __( 'Array of instance changes', 'gutenberg' ),
'type' => 'object',
'default' => array(),
),
),
array(
Expand Down Expand Up @@ -76,51 +88,46 @@ public function compute_new_widget_permissions_check() {
}

/**
* Returns the new widget instance and the form that represents it.
* Checks if the widget being referenced is valid.
*
* @since 5.2.0
* @param string $widget_class Name of the class the widget references.
*
* @return boolean| True if the widget being referenced exists and false otherwise.
*/
private function is_valid_widget( $widget_class ) {
global $wp_widget_factory;
if ( ! $widget_class ) {
return false;
}
return isset( $wp_widget_factory->widgets[ $widget_class ] ) &&
( $wp_widget_factory->widgets[ $widget_class ] instanceof WP_Widget );
}

/**
* Returns the new widget instance and the form that represents it.
*
* @since 5.7.0
* @access public
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function compute_new_widget( $request ) {
$widget = $request->get_param( 'identifier' );
$widget_class = $request->get_param( 'widget_class' );
$instance = $request->get_param( 'instance' );
$instance_changes = $request->get_param( 'instance_changes' );

global $wp_widget_factory;
$widget_obj = $wp_widget_factory->widgets[ $widget_class ];

if (
null === $widget ||
! isset( $wp_widget_factory->widgets[ $widget ] ) ||
! ( $wp_widget_factory->widgets[ $widget ] instanceof WP_Widget )
) {
return new WP_Error(
'widget_invalid',
__( 'Invalid widget.', 'gutenberg' ),
array(
'status' => 404,
)
);
}

$widget_obj = $wp_widget_factory->widgets[ $widget ];

$instance = $request->get_param( 'instance' );
if ( null === $instance ) {
$instance = array();
}
$id_to_use = $request->get_param( 'id_to_use' );
if ( null === $id_to_use ) {
$id_to_use = -1;
}

$widget_obj->_set( $id_to_use );
$widget_obj->_set( -1 );
ob_start();

$instance_changes = $request->get_param( 'instance_changes' );
if ( null !== $instance_changes ) {
if ( ! empty( $instance_changes ) ) {
$old_instance = $instance;
$instance = $widget_obj->update( $instance_changes, $old_instance );

/**
* Filters a widget's settings before saving.
*
Expand Down Expand Up @@ -164,17 +171,12 @@ public function compute_new_widget( $request ) {
*/
do_action_ref_array( 'in_widget_form', array( &$widget_obj, &$return, $instance ) );
}

$id_base = $widget_obj->id_base;
$id = $widget_obj->id;
$form = ob_get_clean();
$form = ob_get_clean();

return rest_ensure_response(
array(
'instance' => $instance,
'form' => $form,
'id_base' => $id_base,
'id' => $id,
)
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ function gutenberg_is_experiment_enabled( $name ) {
/**
* Start: Include for phase 2
*/
if ( ! class_exists( 'WP_REST_Widget_Updater_Controller' ) ) {
require dirname( __FILE__ ) . '/class-wp-rest-widget-updater-controller.php';
if ( ! class_exists( 'WP_REST_Widget_Forms' ) ) {
require dirname( __FILE__ ) . '/class-wp-rest-widget-forms.php';
}
if ( ! class_exists( 'WP_REST_Widget_Areas_Controller' ) ) {
require dirname( __FILE__ ) . '/class-experimental-wp-widget-blocks-manager.php';
Expand Down
4 changes: 2 additions & 2 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ function gutenberg_filter_oembed_result( $response, $handler, $request ) {
* @since 5.0.0
*/
function gutenberg_register_rest_widget_updater_routes() {
$widgets_controller = new WP_REST_Widget_Updater_Controller();
$widgets_controller->register_routes();
$widget_forms = new WP_REST_Widget_Forms();
$widget_forms->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_rest_widget_updater_routes' );

Expand Down
1 change: 1 addition & 0 deletions lib/widgets-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ function gutenberg_widgets_init( $hook ) {
);

wp_enqueue_script( 'wp-edit-widgets' );
wp_enqueue_script( 'admin-widgets' );
wp_enqueue_script( 'wp-format-library' );
wp_enqueue_style( 'wp-edit-widgets' );
wp_enqueue_style( 'wp-format-library' );
Expand Down
42 changes: 32 additions & 10 deletions lib/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ function gutenberg_block_editor_admin_print_footer_scripts() {
*/
function gutenberg_block_editor_admin_footer() {
if ( gutenberg_is_block_editor() ) {
// The function wpWidgets.save needs this nonce to work as expected.
echo implode(
"\n",
array(
'<form method="post">',
wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ),
'</form>',
)
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment. Basically for the widgets code to be able to call the ajax endpoint we need a nonce.

/** This action is documented in wp-admin/admin-footer.php */
// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
do_action( 'admin_footer-widgets.php' );
Expand Down Expand Up @@ -109,40 +118,44 @@ function gutenberg_get_legacy_widget_settings() {
if ( ! empty( $wp_widget_factory ) ) {
foreach ( $wp_widget_factory->widgets as $class => $widget_obj ) {
$available_legacy_widgets[ $class ] = array(
'name' => html_entity_decode( $widget_obj->name ),
'name' => html_entity_decode( $widget_obj->name ),
// wp_widget_description is not being used because its input parameter is a Widget Id.
// Widgets id's reference to a specific widget instance.
// Here we are iterating on all the available widget classes even if no widget instance exists for them.
'description' => isset( $widget_obj->widget_options['description'] ) ?
'description' => isset( $widget_obj->widget_options['description'] ) ?
html_entity_decode( $widget_obj->widget_options['description'] ) :
null,
'isCallbackWidget' => false,
'isHidden' => in_array( $class, $core_widgets, true ),
'isReferenceWidget' => false,
'isHidden' => in_array( $class, $core_widgets, true ),
);
}
}
global $wp_registered_widgets;
if ( ! empty( $wp_registered_widgets ) ) {
foreach ( $wp_registered_widgets as $widget_id => $widget_obj ) {

$block_widget_start = 'blocks-widget-';
if (
is_array( $widget_obj['callback'] ) &&
( is_array( $widget_obj['callback'] ) &&
isset( $widget_obj['callback'][0] ) &&
( $widget_obj['callback'][0] instanceof WP_Widget )
( $widget_obj['callback'][0] instanceof WP_Widget ) ) ||
// $widget_id starts with $block_widget_start.
strncmp( $widget_id, $block_widget_start, strlen( $block_widget_start ) ) === 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more common (and, I think, easier to understand) to see substr used to check that a string begins with another string.

substr( $widget_id, 0, strlen( $block_widget_start ) ) === $block_widget_start

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance is not very relevant in this case. But, substring first unconditionally iterates on the string to copy parts of it and the only in the end the comparison is done. The method I'm using does not copy any character and stops the comparison when the first char difference is found. So I normally use this code because it is more performant. I added a comment to say that this logic is a starts with.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! PHP needs a starts_with function 😛

) {
continue;
}
$available_legacy_widgets[ $widget_id ] = array(
'name' => html_entity_decode( $widget_obj['name'] ),
'description' => html_entity_decode( wp_widget_description( $widget_id ) ),
'isCallbackWidget' => true,
'name' => html_entity_decode( $widget_obj['name'] ),
'description' => html_entity_decode( wp_widget_description( $widget_id ) ),
'isReferenceWidget' => true,
);
}
}

$settings['hasPermissionsToManageWidgets'] = $has_permissions_to_manage_widgets;
$settings['availableLegacyWidgets'] = $available_legacy_widgets;

return $settings;
return gutenberg_experiments_editor_settings( $settings );
}

/**
Expand Down Expand Up @@ -213,3 +226,12 @@ function gutenberg_create_wp_area_post_type() {
add_action( 'init', 'gutenberg_create_wp_area_post_type' );

add_filter( 'sidebars_widgets', 'Experimental_WP_Widget_Blocks_Manager::swap_out_sidebars_blocks_for_block_widgets' );

/**
* Function to enqueue admin-widgets as part of the block editor assets.
*/
function gutenberg_enqueue_widget_scripts() {
wp_enqueue_script( 'admin-widgets' );
}

add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_widget_scripts' );
Loading