Skip to content

Commit

Permalink
Prevent plugin initialization if Jetpack 8.1 or earlier is installed (#…
Browse files Browse the repository at this point in the history
…760)

Due to changes in the autoloader, if Jetpack 8.1 or earlier is installed alongside WCPay, the whole site would crash. The crash would happen in the `\Jetpack\Config` initialization code.

I added a quick & dirty check to show an admin notice if Jetpack 8.1 or earlier is installed, and prevent the rest of the plugin from initializing. I didn't want to spend a lot of effort in this so it's not as smart as the main dependency checker (which shows a link to update the dependency, tells you which version of the dependency you have, etc). I just wanted something temporary that's better than the site crashing.

Jetpack 8.2 (which is already compatible with WCPay) was released in February 2020. I think we can remove this piece of code in a few months, maybe when that version is 1 year old? Anyway, I left a TODO comment, if someone stumbles upon it in the distant future it can be removed.
  • Loading branch information
Daniel Rey López authored Sep 1, 2020
1 parent 765a550 commit 4320aa8
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions woocommerce-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
* Initialize the Jetpack connection functionality.
*/
function wcpay_jetpack_init() {
if ( ! wcpay_check_old_jetpack_version() ) {
return;
}
$jetpack_config = new Automattic\Jetpack\Config();
$jetpack_config->ensure(
'connection',
Expand All @@ -52,4 +55,34 @@ function wcpay_init() {
}

// Make sure this is run *after* WooCommerce has a chance to initialize its packages (wc-admin, etc). That is run with priority 10.
// If you change the priority of this action, you'll need to change it in the wcpay_check_old_jetpack_version function too.
add_action( 'plugins_loaded', 'wcpay_init', 11 );

/**
* Check if WCPay is installed alongside an old version of Jetpack (8.1 or earlier). Due to the autoloader code in those old
* versions, the Jetpack Config initialization code would just crash the site.
* TODO: Remove this when Jetpack 8.1 (Released on January 2020) is so old we don't think anyone will run into this problem anymore.
*
* @return bool True if the plugin can keep initializing itself, false otherwise.
*/
function wcpay_check_old_jetpack_version() {
if ( defined( 'JETPACK__VERSION' ) && version_compare( JETPACK__VERSION, '8.2', '<' ) ) {
add_filter( 'admin_notices', 'wcpay_show_old_jetpack_notice' );
// Prevent the rest of the plugin from initializing.
remove_action( 'plugins_loaded', 'wcpay_init', 11 );
return false;
}
return true;
}

/**
* Display an error notice if the installed Jetpack version is too old to even start initializing the plugin.
*/
function wcpay_show_old_jetpack_notice() {
?>
<div class="notice wcpay-notice notice-error">
<p><b><?php echo esc_html( __( 'WooCommerce Payments', 'woocommerce-payments' ) ); ?></b></p>
<p><?php echo esc_html( __( 'The version of Jetpack installed is too old to be used with WooCommerce Payments. WooCommerce Payments has been disabled. Please deactivate or update Jetpack.', 'woocommerce-payments' ) ); ?></p>
</div>
<?php
}

0 comments on commit 4320aa8

Please sign in to comment.