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

Widgets: Fix the Simple Payments widget in the widgets.php page #9848

Closed
wants to merge 12 commits into from

Conversation

Copons
Copy link
Contributor

@Copons Copons commented Jul 3, 2018

Fixes Automattic/wp-calypso#25576

The newly introduced Simple Payments widget heavily relies on the Customizer JS API and therefore it doesn't work in the Widgets (widgets.php) page.

This PR is an attempt to make it work in both pages flawlessly.

My initial attempt consisted in revers engineer the Customizer itself, to figure out when and why all widget events are triggered, in order to emulate them in the Widgets page.
This was sort of possible, but required an amount of hacks and workarounds several orders of magnitude larger than what I'm comfortable with.

This is the second attempt, and it's completely independent from the Customizer JS API.
It relies entirely on the event delegation, which is easy and performant enough to pull off with jQuery.
I haven't modified the existing form.php and customizer.js files, but instead opted to write two new files from scratch in order to keep both version always open side by side, trying to stay as true to the original code as possible, while also rewriting it and learning from it.

The idea behind this attempt is that we observe the widgets container, and only react when one of the buttons in a Simple Payments widget is clicked. From there, that widget becomes the context where everything happens.

Clicking on "Add New" will simply open an empty form.
Clicking on "Edit Selected" will fetch via AJAX the details of the selected product to populate the form.
Saving will simply save via AJAX whatever is in the form, and update the products selector accordingly.
Deleting will delete the product and update the products selector.
Closing the form will simply hide and clear it.

Bar corner cases and polish, this works fine in both the Widgets page and the Customizer.

There is one gotcha that I haven't got the time to study because I caught it too late before my AFK time.
When adding a new product or editing an existing one, the Customizer does not refresh accordingly until the product is saved.
This happens because the selective refresh relies on the values returned by the WP_Widget::update() method. In this case, the only values actually needed for the widget are the title and a product ID.
All the product details (name, price, image, etc.) are saved via AJAX and stored in their own posts of CPT jp_pay_product.
I know for a fact that @rodrigoi solved this issue, but for my poor understanding of the selective refresh magic, I can't seem to figure out exactly how.

Since my AFK is near, I'm leaving this PR here hoping it will be of some inspiration to fix that nasty issue.

Changes proposed in this Pull Request:

  • WIP

Testing instructions:

  • On a Pro JP site, open both the Widgets page and the Customizer.
  • Add multiple Simple Payments widgets to a sidebar.
  • Try to add new products, and edit and deleting existing ones.
  • Make sure all the operations work as expected.
  • Make sure the Customizer preview updates accordingly to the edit (note: as of now, products are only updated when saving them).

@Copons Copons added [Type] Bug When a feature is broken and / or not performing as intended [Feature] Extra Sidebar Widgets [Status] In Progress Simple Payments labels Jul 3, 2018
@Copons Copons self-assigned this Jul 3, 2018
@Copons Copons requested a review from a team as a code owner July 3, 2018 13:04
@Copons Copons added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] In Progress labels Jul 3, 2018
@Copons Copons requested review from rodrigoi and a team July 3, 2018 16:16
@gwwar
Copy link
Contributor

gwwar commented Jul 5, 2018

For Jetpack folks looking 👀this PR is experimental and is not aimed at a particular release yet.

If this is distracting we can temporarily close this PR.

@oskosk
Copy link
Contributor

oskosk commented Jul 5, 2018

All good as it is now @gwwar. Thanks for the heads-up

@Copons Copons force-pushed the try/simple-payments-widgets-page branch from 69b7f31 to 255c5e2 Compare July 16, 2018 09:49
@brbrr
Copy link
Contributor

brbrr commented Aug 7, 2018

This PR needs a rebase

@brbrr brbrr removed the [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. label Aug 7, 2018
@Copons
Copy link
Contributor Author

Copons commented Aug 7, 2018

I'll close this and move it back to our backlog for now!

@Copons Copons closed this Aug 7, 2018
@Copons Copons deleted the try/simple-payments-widgets-page branch August 7, 2018 09:10
@jeherve jeherve added the [Feature] Pay with PayPal aka Simple Payments label Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Extra Sidebar Widgets [Feature] Pay with PayPal aka Simple Payments [Type] Bug When a feature is broken and / or not performing as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Customizer: Simple Payments Widget does not work on WP-Admin's Appearance > Widgets interface.
5 participants