Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Refactor block types to include script registration and integration classes #3829

Merged
merged 11 commits into from
Feb 17, 2021

Conversation

mikejolley
Copy link
Member

@mikejolley mikejolley commented Feb 11, 2021

This is a fairly hefty PR which refactors our Block Type classes (and makes some changes to the Payments Integration classes).

1. Removes editor script registration from the Assets class to the block type classes

This keeps block type definitions together with the script registration, avoids the need to do additional checks for build type when registering assets (because blocks are not registered if they do not support the current build), and allows each block type to define it's own dependencies.

2. Abstracts PaymentMethodRegistry and PaymentMethodTypeInterface with IntegrationRegistry and IntegrationInterface

Since we needed something similar for other integrations (block types) I created IntegrationRegistry and IntegrationInterface and moved the code that can be reused to those.

AbstractPaymentMethodType takes care of the differences and this seems to be used by WC Pay etc so shouldn't break anything.

3. AbstractBlock refactor

This class will now:

  • Register the editor script
  • Register the frontend script
  • Enqueue the frontend script when the block is rendered
  • Use the IntegrationRegistry based on the block name, and with this append registered dependencies and data whenever assets are registered or enqueued.

@senadir before this is merged I want to update subscriptions with it to ensure it covers all use cases. We will hook in via:

woocommerce_blocks_{registry_identifier}_registration is the hook where 3rd parties can register themselves. In the case of subscriptions it will be both:

  • woocommerce_blocks_cart_block_registration
  • woocommerce_blocks_checkout_block_registration

Fixes #3532

How to test the changes in this Pull Request:

This touches on quite a lot of code so we need to ensure:

  1. All tests pass, which should cover block rendering issues
  2. Smoke test all blocks (frontend and editor) to ensure they render. This will confirm scripts are still loaded.
  3. Payment methods should still show up during checkout. Test full checkout flow.
  4. Check console for 404s to ensure no invalid scripts are loaded.
  5. Check experimental block types like atomic blocks still function

Changelog

Refactor block type registration to support 3rd party integrations.

Dev note:

An important note that internally, this release has modified how AbstractBlock (the base class for all of our blocks) functions, and how it loads assets. AbstractBlock is internal to this project and does not seem like something that would ever need to be extended by 3rd parties, but note if you are doing so for whatever reason, your implementation would need to be updated to match.

@mikejolley mikejolley added status: needs review category: extensibility Work involving adding or updating extensibility. Useful to combine with other scopes impacted. type: refactor The issue/PR is related to refactoring. labels Feb 11, 2021
@mikejolley mikejolley added this to the 4.5.0 milestone Feb 11, 2021
@mikejolley mikejolley requested a review from senadir February 11, 2021 14:43
@mikejolley mikejolley self-assigned this Feb 11, 2021
@mikejolley mikejolley requested a review from a team as a code owner February 11, 2021 14:43
@mikejolley mikejolley force-pushed the add/abstract-integration-classes-3532 branch from 5f3a3aa to c5cd9ab Compare February 11, 2021 16:17
@github-actions
Copy link
Contributor

github-actions bot commented Feb 11, 2021

Size Change: +39 B (0%)

Total Size: 1.15 MB

Filename Size Change
build/style-rtl.css 18.9 kB +20 B (0%)
build/style.css 18.9 kB +19 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/active-filters-frontend.js 8.35 kB 0 B
build/active-filters.js 8.52 kB 0 B
build/all-products-frontend.js 34.7 kB 0 B
build/all-products.js 36.4 kB 0 B
build/all-reviews.js 9.89 kB 0 B
build/atomic-block-components/add-to-cart--atomic-block-components/button.js 3.37 kB 0 B
build/atomic-block-components/add-to-cart--atomic-block-components/image--atomic-block-components/title.js 335 B 0 B
build/atomic-block-components/add-to-cart-frontend.js 9.23 kB 0 B
build/atomic-block-components/add-to-cart.js 7.69 kB 0 B
build/atomic-block-components/button-frontend.js 2.39 kB 0 B
build/atomic-block-components/button.js 840 B 0 B
build/atomic-block-components/category-list-frontend.js 470 B 0 B
build/atomic-block-components/category-list.js 476 B 0 B
build/atomic-block-components/image-frontend.js 1.77 kB 0 B
build/atomic-block-components/image.js 1.23 kB 0 B
build/atomic-block-components/price-frontend.js 1.94 kB 0 B
build/atomic-block-components/price.js 1.96 kB 0 B
build/atomic-block-components/rating-frontend.js 520 B 0 B
build/atomic-block-components/rating.js 526 B 0 B
build/atomic-block-components/sale-badge-frontend.js 857 B 0 B
build/atomic-block-components/sale-badge.js 866 B 0 B
build/atomic-block-components/sku-frontend.js 389 B 0 B
build/atomic-block-components/sku.js 393 B 0 B
build/atomic-block-components/stock-indicator-frontend.js 569 B 0 B
build/atomic-block-components/stock-indicator.js 573 B 0 B
build/atomic-block-components/summary-frontend.js 920 B 0 B
build/atomic-block-components/summary.js 925 B 0 B
build/atomic-block-components/tag-list-frontend.js 465 B 0 B
build/atomic-block-components/tag-list.js 473 B 0 B
build/atomic-block-components/title-frontend.js 1.35 kB 0 B
build/atomic-block-components/title.js 1.21 kB 0 B
build/attribute-filter-frontend.js 18.3 kB 0 B
build/attribute-filter.js 12.5 kB 0 B
build/blocks-checkout.js 16.8 kB 0 B
build/blocks.js 3.49 kB 0 B
build/cart-frontend.js 75.7 kB 0 B
build/cart.js 38.4 kB 0 B
build/checkout-frontend.js 80.4 kB 0 B
build/checkout.js 41.3 kB 0 B
build/editor-rtl.css 14.9 kB 0 B
build/editor.css 14.9 kB 0 B
build/featured-category.js 7.83 kB 0 B
build/featured-product.js 10.1 kB 0 B
build/handpicked-products.js 7.51 kB 0 B
build/price-filter-frontend.js 14.6 kB 0 B
build/price-filter.js 9.95 kB 0 B
build/price-format.js 1.34 kB 0 B
build/product-best-sellers.js 7.58 kB 0 B
build/product-categories.js 3.23 kB 0 B
build/product-category.js 8.52 kB 0 B
build/product-new.js 7.75 kB 0 B
build/product-on-sale.js 8.13 kB 0 B
build/product-search.js 3.58 kB 0 B
build/product-tag.js 6.58 kB 0 B
build/product-top-rated.js 7.72 kB 0 B
build/products-by-attribute.js 8.5 kB 0 B
build/reviews-by-category.js 12 kB 0 B
build/reviews-by-product.js 13.5 kB 0 B
build/reviews-frontend.js 9.52 kB 0 B
build/single-product-frontend.js 37.9 kB 0 B
build/single-product.js 10.3 kB 0 B
build/vendors--atomic-block-components/price-frontend.js 6.53 kB 0 B
build/vendors-style-rtl.css 1.05 kB 0 B
build/vendors-style.css 1.05 kB 0 B
build/vendors.js 418 kB 0 B
build/wc-blocks-data.js 6.97 kB 0 B
build/wc-blocks-middleware.js 1.1 kB 0 B
build/wc-blocks-registry.js 2.67 kB 0 B
build/wc-payment-method-bacs.js 820 B 0 B
build/wc-payment-method-cheque.js 816 B 0 B
build/wc-payment-method-cod.js 913 B 0 B
build/wc-payment-method-paypal.js 853 B 0 B
build/wc-payment-method-stripe.js 12.2 kB 0 B
build/wc-settings.js 2.4 kB 0 B
build/wc-shared-context.js 1.53 kB 0 B
build/wc-shared-hocs.js 1.69 kB 0 B

compressed-size-action

*
* @param IntegrationRegistry $this Instance of the IntegrationRegistry class which exposes the IntegrationRegistry::register() method.
*/
do_action( 'woocommerce_blocks_' . $this->registry_identifier . '_registration', $this );
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we only send ::register() and not the entire class instance?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is matching the pattern in the Payments Registry. There are some other useful methods like unregistration that may be needed.

@budzanowski
Copy link
Contributor

I can't really follow all the changes since these are 3 different things at the same time. The only thing I was able to spot were simple nitpicks. I will just try to test as much as possible.

What is the impact on existing third-party integrations if they are using AbstractBlock? The class constructor signature was changed. Is this a public API? We have it documented in the Contributing section of the documentation.

@budzanowski
Copy link
Contributor

I have tried to test as much as I could, and it all looks good.

@mikejolley
Copy link
Member Author

What is the impact on existing third-party integrations if they are using AbstractBlock? The class constructor signature was changed. Is this a public API? We have it documented in the Contributing section of the documentation.

@budzanowski I wasn't totally sure on this. Yes it's "public" because it's accessible, but it's something internal to our blocks and has not been documented as an extendibility API or anything such as this. I would argue it's internal, even if the scope requires it to be public.

@mikejolley
Copy link
Member Author

Thanks for the review @budzanowski 👍🏻

@senadir did you also get a chance to review this?

@nerrad did you have any thoughts on #3829 (comment)?

@opr opr modified the milestones: 4.5.0, 4.6.0 Feb 15, 2021
@mikejolley mikejolley force-pushed the add/abstract-integration-classes-3532 branch from b6ec400 to 087cb82 Compare February 17, 2021 12:14
@senadir senadir merged commit 8ac4e9b into trunk Feb 17, 2021
@senadir senadir deleted the add/abstract-integration-classes-3532 branch February 17, 2021 13:18
@nerrad
Copy link
Contributor

nerrad commented Feb 17, 2021

@nerrad did you have any thoughts on #3829 (comment)?

I agree with your assessment. However, I think it should still be highlighted in a dev note in whatever release has these changes (would you be able to pre-write one in the PR description for the next release Mike?)

@nerrad nerrad added the needs: dev note PR that has some text that needs to be included in the release notes. label Feb 17, 2021
@mikejolley
Copy link
Member Author

Dev note:

An important note that internally, this release has modified how AbstractBlock (the base class for all of our blocks) functions, and how it loads assets. AbstractBlock is internal to this project and does not seem like something that would ever need to be extended by 3rd parties, but note if you are doing so for whatever reason, your implementation would need to be updated to match.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
category: extensibility Work involving adding or updating extensibility. Useful to combine with other scopes impacted. needs: dev note PR that has some text that needs to be included in the release notes. type: refactor The issue/PR is related to refactoring.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Abstract Payments/API class to be used by other plugins
5 participants