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

Hide payment methods that have missing dependencies and display an error in the admin #3920

Merged

Conversation

Aljullu
Copy link
Contributor

@Aljullu Aljullu commented Mar 4, 2021

Fixes #3800.

In some occasions, a payment method might be registered but its dependencies might not (read #3800 for a more in-depth explanation). In those cases, this PR makes it so:

  • The payment method with missing dependencies is removed from Cart/Checkout script dependencies. This way, Cart and Checkout block continue working and the broken payment method is silently hidden.
  • It adds an error notice in wp-admin and error_log()'s the error. If WP_DEBUG is set to true, it also includes a link to our docs.

How to test the changes in this Pull Request:

There are two different set-ups that you can try:

  • One consists on recreating the issue we faced with WC Pay. You will need to install WC Pay and remove these lines, so stripe is not registered.
  • Another solution is to add a fake dependency in any other payment method. Ie, in Cheque:
	public function get_payment_method_script_handles() {
		$this->asset_api->register_script(
			'wc-payment-method-cheque',
			'build/wc-payment-method-cheque.js',
+			[ 'missing-dependency' ]
		);
		return [ 'wc-payment-method-cheque' ];
	}
  1. Go to any wp-admin page and verify an error notice is displayed for each faulty payment method:
    imatge
  2. Create a post or page and verify the Cart and Checkout blocks are available. If you add them, you will notice the faulty payment methods are not available.
  3. In the frontend, verify the Cart and Checkout blocks load as usual (but the payment methods with missing dependencies are not loaded).

Changelog

Prevent errant payment methods from keeping Cart and Checkout blocks from loading

@Aljullu Aljullu added status: needs review type: enhancement The issue is a request for an enhancement. category: extensibility Work involving adding or updating extensibility. Useful to combine with other scopes impacted. labels Mar 4, 2021
@Aljullu Aljullu added this to the 4.7.0 milestone Mar 4, 2021
@Aljullu Aljullu requested a review from a team as a code owner March 4, 2021 12:35
@Aljullu Aljullu self-assigned this Mar 4, 2021
@Aljullu Aljullu requested review from ralucaStan and removed request for a team March 4, 2021 12:35
@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2021

Size Change: +248 B (0%)

Total Size: 1.15 MB

Filename Size Change
build/active-filters-frontend.js 8.38 kB -6 B (0%)
build/active-filters.js 8.5 kB +1 B (0%)
build/all-products-frontend.js 34.6 kB -1 B (0%)
build/all-products.js 36.5 kB +17 B (0%)
build/all-reviews.js 9.88 kB -3 B (0%)
build/atomic-block-components/price-frontend.js 1.98 kB +36 B (+2%)
build/atomic-block-components/price.js 2 kB +34 B (+2%)
build/attribute-filter-frontend.js 18.3 kB -2 B (0%)
build/attribute-filter.js 12.5 kB +2 B (0%)
build/blocks-checkout.js 16.6 kB -99 B (-1%)
build/blocks.js 3.5 kB +1 B (0%)
build/cart-frontend.js 75.7 kB +46 B (0%)
build/cart.js 38.5 kB +47 B (0%)
build/checkout-frontend.js 80.3 kB +22 B (0%)
build/checkout.js 41.3 kB +16 B (0%)
build/featured-category.js 7.83 kB +1 B (0%)
build/featured-product.js 10.1 kB -1 B (0%)
build/price-filter-frontend.js 14.7 kB +24 B (0%)
build/price-filter.js 9.98 kB +37 B (0%)
build/price-format.js 1.46 kB +91 B (+7%) 🔍
build/product-best-sellers.js 7.58 kB +1 B (0%)
build/product-categories.js 3.23 kB -1 B (0%)
build/product-category.js 8.53 kB +3 B (0%)
build/product-new.js 7.75 kB +1 B (0%)
build/product-on-sale.js 8.15 kB +2 B (0%)
build/product-search.js 3.58 kB -1 B (0%)
build/product-tag.js 6.58 kB +1 B (0%)
build/product-top-rated.js 7.72 kB +1 B (0%)
build/reviews-by-category.js 12 kB -4 B (0%)
build/reviews-by-product.js 13.5 kB -4 B (0%)
build/single-product-frontend.js 37.7 kB -3 B (0%)
build/single-product.js 10.3 kB -3 B (0%)
build/style-rtl.css 18.9 kB -2 B (0%)
build/style.css 18.9 kB -2 B (0%)
build/vendors.js 418 kB -3 B (0%)
build/wc-blocks-data.js 7.2 kB -2 B (0%)
build/wc-blocks-middleware.js 1.11 kB +2 B (0%)
build/wc-blocks-registry.js 2.69 kB -2 B (0%)
build/wc-settings.js 2.43 kB -1 B (0%)
build/wc-shared-context.js 1.53 kB +1 B (0%)
build/wc-shared-hocs.js 1.72 kB +1 B (0%)
ℹ️ View Unchanged
Filename Size Change
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 334 B 0 B
build/atomic-block-components/add-to-cart-frontend.js 9.19 kB 0 B
build/atomic-block-components/add-to-cart.js 7.68 kB 0 B
build/atomic-block-components/button-frontend.js 2.37 kB 0 B
build/atomic-block-components/button.js 839 B 0 B
build/atomic-block-components/category-list-frontend.js 469 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/rating-frontend.js 521 B 0 B
build/atomic-block-components/rating.js 526 B 0 B
build/atomic-block-components/sale-badge-frontend.js 861 B 0 B
build/atomic-block-components/sale-badge.js 871 B 0 B
build/atomic-block-components/sku-frontend.js 390 B 0 B
build/atomic-block-components/sku.js 394 B 0 B
build/atomic-block-components/stock-indicator-frontend.js 570 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 467 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/editor-rtl.css 14.9 kB 0 B
build/editor.css 14.9 kB 0 B
build/handpicked-products.js 7.51 kB 0 B
build/products-by-attribute.js 8.51 kB 0 B
build/reviews-frontend.js 9.58 kB 0 B
build/vendors--atomic-block-components/price-frontend.js 6.54 kB 0 B
build/vendors-style-rtl.css 1.05 kB 0 B
build/vendors-style.css 1.05 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

compressed-size-action

$payment_method_scripts = $this->payment_method_registry->get_all_active_payment_method_script_dependencies();

foreach ( $payment_method_scripts as $payment_method_script ) {
$deps = $wp_scripts->registered[ $payment_method_script ]->deps;
Copy link
Member

Choose a reason for hiding this comment

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

Is this array item guaranteed to exist or do you need a guard?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a guard in 1845b12.

* an error in the admin.
*/
public function verify_payment_methods_dependencies() {
global $wp_scripts;
Copy link
Member

Choose a reason for hiding this comment

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

I think we can avoid the global by calling $wp_scripts = wp_scripts();?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, much better. Changed in 5d46dbe.

error_log( // phpcs:ignore
sprintf(
/* translators: 1: handle of the payment gateway. 2: handle of the dependency that is missing. */
esc_html__( 'Payment gateway with handle %1$s has been deactivated because its dependency %2$s is not registered.', 'woo-gutenberg-products-block' ),
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to localise debug strings? This is extra burden for translators, and we need to make sense of logs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense not localizing them. Changed in 23a2631.

Copy link
Member

@mikejolley mikejolley left a comment

Choose a reason for hiding this comment

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

This worked (I tested the 2nd case), but I have some concerns regarding notices. Thoughts on that?

esc_html( $payment_method_script ),
'<code>' . esc_html( $dep ) . '</code>'
);
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
Copy link
Member

Choose a reason for hiding this comment

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

Why are we only showing docs with DEBUG on?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea was not to bother merchants with a link that was only useful to payment method developers. But now that we no longer use a notice in the admin, this is not relevant anymore and we can print the URL in all logs. 👍

)
);
add_action(
'admin_notices',
Copy link
Member

Choose a reason for hiding this comment

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

Is this the best place to surface errors? I am aware that notices are easy to add but I think it does add a lot of noise within admin.

I think debug log is good, and perhaps we could add an inline script to log to the console too. Those would be visible by developers and it avoids polluting admin notices.

If that works well we should do the same for the circular dependency notice too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea! I moved this error from being a notice to a console.error() in a9bd620. For now, I didn't change the circular dependency notice, we can take care of it in a follow-up and maybe even creating a util so we can easily log in the browser console from PHP code.

@Aljullu
Copy link
Contributor Author

Aljullu commented Mar 8, 2021

Thanks for the review @mikejolley. I applied your feedback, this is ready for another review.

@Aljullu Aljullu requested a review from mikejolley March 8, 2021 14:37
Copy link
Member

@mikejolley mikejolley left a comment

Choose a reason for hiding this comment

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

<3 :shipit:

@mikejolley mikejolley merged commit 9139d24 into trunk Mar 9, 2021
@mikejolley mikejolley deleted the fix/3800-remove-payment-methods-with-missing-dependencies branch March 9, 2021 11:15
@Aljullu Aljullu mentioned this pull request May 6, 2021
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. type: enhancement The issue is a request for an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prevent errant payment methods from keeping Cart and Checkout blocks from loading.
2 participants