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

Fix: Legacy widget: callback widgets with a edit form. #14395

Closed

Conversation

jorgefilipecosta
Copy link
Member

This PR enhances the work done in #13511 to make sure we support call back widgets with a custom edit form.

Testing

As a test subject, I used "NattyWP feedburner widget". The widget comes with silesia theme that can be downloaded at https://wordpress.org/themes/silesia/. The theme is not updated to WordPress 5.0 and some warning messages appear. For testing purposes, the widget can also be used standalone by pasting the code available https://themes.trac.wordpress.org/browser/silesia/1.0.6/include/widgets/feedburner.php in the functions.php of any other theme.

@jorgefilipecosta jorgefilipecosta added the [Status] In Progress Tracking issues with work in progress label Mar 12, 2019
@jorgefilipecosta jorgefilipecosta force-pushed the fix/legacy-call-back-widgets-with-forms branch 3 times, most recently from 1804beb to 9644b7b Compare March 12, 2019 21:57
@jorgefilipecosta jorgefilipecosta force-pushed the fix/legacy-call-back-widgets-with-forms branch from 9644b7b to 123bf60 Compare March 14, 2019 10:47
@jorgefilipecosta jorgefilipecosta added [Type] Bug An existing feature does not function as intended [Type] Enhancement A suggestion for improvement. [Feature] Widgets Screen The block-based screen that replaced widgets.php. and removed [Status] In Progress Tracking issues with work in progress labels Mar 14, 2019
@jorgefilipecosta jorgefilipecosta force-pushed the fix/legacy-call-back-widgets-with-forms branch from 123bf60 to c4c4e14 Compare March 22, 2019 18:59
@@ -69,6 +69,9 @@ function register_block_core_legacy_widget() {
'isCallbackWidget' => array(
'type' => 'boolean',
),
'hasEditForm' => array(
Copy link
Member

Choose a reason for hiding this comment

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

It seems odd to me to call this an attribute of the block, considering that it's of no consequence to the saved result or semantic meaning of a block's content. It seems like it's being used this way as a convenience?


$widget = $request->get_param( 'identifier' );

private function isValidWidget( $identifier, $is_callback_widget ) {
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason for this not be named using snake_case ?

Use lowercase letters in variable, action/filter, and function names (never camelCase). Separate words via underscores.

https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#naming-conventions

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @aduth the name was updated the snake case.

@jorgefilipecosta jorgefilipecosta force-pushed the fix/legacy-call-back-widgets-with-forms branch 3 times, most recently from 0aea34b to 21f4a7e Compare April 9, 2019 10:20
@aduth aduth added the REST API Interaction Related to REST API label Apr 29, 2019
Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

I added the label "REST API Interaction" in hopes of drawing attention from those with REST API expertise who may be able to provide more feedback.

@@ -34,8 +34,7 @@ public function __construct() {
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<identifier>[\w-_]+)/',
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain why this is changing?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @aduth, previously the identifier was only the name of a PHP class. Now the identifier can be the name of a PHP class or the instance id of a widget (for callback widgets), so the regex needed this update to match instance ids.

*/
public function compute_new_widget( $request ) {
$identifier = $request->get_param( 'identifier' );
$is_callback_widget = $request->get_param( 'is_callback_widget' );
Copy link
Member

Choose a reason for hiding this comment

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

I believe this should be included in args for the register_routes function?

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 was not sure if args should only be used for arguments part of the route itself e.g: /(?P[\w-_]+)/. But in my tests, it seems args also work on arguments part of the post data, so I updated the code to follow your suggestion.

public function compute_new_widget( $request ) {
$identifier = $request->get_param( 'identifier' );
$is_callback_widget = $request->get_param( 'is_callback_widget' );
if ( null === $is_callback_widget ) {
Copy link
Member

Choose a reason for hiding this comment

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

Adding to the previous comment, if it was specified as an args as of type boolean, could we assign the default there such that we never expect it to be any value other than true or false, and not need to include this logic here?

/**
* Returns the new widget instance and the form that represents it.
*
* @since 5.2.0
Copy link
Member

Choose a reason for hiding this comment

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

These @since will need to be updated.

$widget = $request->get_param( 'identifier' );

private function is_valid_widget( $identifier, $is_callback_widget ) {
if ( null === $identifier ) {
Copy link
Member

Choose a reason for hiding this comment

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

It seems like we should just be making these args as required ? Rather than trying to deal with validating the request format from the implementation.

@jorgefilipecosta jorgefilipecosta force-pushed the fix/legacy-call-back-widgets-with-forms branch 3 times, most recently from 1e8fb26 to a29054b Compare May 1, 2019 14:43
@jorgefilipecosta jorgefilipecosta force-pushed the fix/legacy-call-back-widgets-with-forms branch 3 times, most recently from 14b7a92 to b86f9d4 Compare May 2, 2019 13:53
@jorgefilipecosta jorgefilipecosta force-pushed the fix/legacy-call-back-widgets-with-forms branch from b86f9d4 to 7b4ed34 Compare May 15, 2019 09:14
…flag for the identifier. (+2 squashed commits)

Squashed commits:
[86539f0] Remove hasEditForm attribute
[3045a2d] Fix: Legacy widget: allow to use the edit form of callback widgets with a custom form.
@jorgefilipecosta
Copy link
Member Author

I'm closing this PR while we try #15801.

@youknowriad youknowriad deleted the fix/legacy-call-back-widgets-with-forms branch May 27, 2020 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Widgets Screen The block-based screen that replaced widgets.php. REST API Interaction Related to REST API [Type] Bug An existing feature does not function as intended [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants