From 4320aa84449d3419164271fc968077e0e153f029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Rey=20L=C3=B3pez?= Date: Tue, 1 Sep 2020 12:19:52 +0100 Subject: [PATCH] Prevent plugin initialization if Jetpack 8.1 or earlier is installed (#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. --- woocommerce-payments.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/woocommerce-payments.php b/woocommerce-payments.php index 6fe181dcc78..d0aa3417fd1 100644 --- a/woocommerce-payments.php +++ b/woocommerce-payments.php @@ -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', @@ -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() { + ?> +
+

+

+
+