From a453a88e09c39331642397de848dd3e73ba3fcb3 Mon Sep 17 00:00:00 2001 From: Dat Hoang Date: Fri, 1 Oct 2021 16:13:35 +0700 Subject: [PATCH] check_plugin_dependencies: remove $silent, return notice message and dependency status flag. --- includes/class-wc-payments.php | 224 +++++++++++++++++---------------- 1 file changed, 113 insertions(+), 111 deletions(-) diff --git a/includes/class-wc-payments.php b/includes/class-wc-payments.php index f7ca3ce4806..b6a3deb70e7 100644 --- a/includes/class-wc-payments.php +++ b/includes/class-wc-payments.php @@ -157,18 +157,26 @@ public static function init() { include_once __DIR__ . '/class-wc-payments-features.php'; include_once __DIR__ . '/class-wc-payments-utils.php'; - if ( ! self::check_plugin_dependencies( true ) ) { - add_filter( 'admin_notices', [ __CLASS__, 'check_plugin_dependencies' ] ); + $check = self::check_plugin_dependencies(); + $message = $check['message']; + $passed = $check['passed']; + + if ( null !== $message ) { + add_action( + 'admin_notices', + function() use ( $message ) { + // Do not show alerts while installing plugins. + if ( self::is_at_plugin_install_page() ) { + return; + } + + self::display_admin_error( $message ); + } + ); + } - /** - * After displaying notice errors in WP Admin, still silently load the plugin if the account is connected. - * - * @since 3.1.0 - */ - $account_data = get_option( 'wcpay_account_data' ); - if ( ! isset( $account_data['account'] ) ) { - return; - }; + if ( false === $passed ) { + return; } add_action( 'admin_init', [ __CLASS__, 'add_woo_admin_notes' ] ); @@ -374,141 +382,135 @@ public static function get_plugin_headers() { /** * Checks if all the dependencies needed to run this plugin are present * - * @param bool $silent True if the function should just return true/false, False if this function should display notice messages for failed dependencies. - * @return bool True if all dependencies are met, false otherwise + * @return array{message: ?string, passed: bool} An array including a notice message for admins, and a flag with value True if dependencies are met or False other wise. */ - public static function check_plugin_dependencies( $silent ) { + public static function check_plugin_dependencies() { + $res = [ + 'message' => null, + 'passed' => false, + ]; + if ( defined( 'WCPAY_TEST_ENV' ) && WCPAY_TEST_ENV ) { - return true; + $res['passed'] = true; + return $res; } $plugin_headers = self::get_plugin_headers(); - - // Do not show alerts while installing plugins. - if ( ! $silent && self::is_at_plugin_install_page() ) { - return true; - } - - $wc_version = $plugin_headers['WCRequires']; - $wp_version = $plugin_headers['RequiresWP']; + $wc_version = $plugin_headers['WCRequires']; + $wp_version = $plugin_headers['RequiresWP']; // Check if WooCommerce is installed and active. if ( ! class_exists( 'WooCommerce' ) ) { - if ( ! $silent ) { - $message = WC_Payments_Utils::esc_interpolated_html( - __( 'WooCommerce Payments requires WooCommerce to be installed and active.', 'woocommerce-payments' ), - [ 'a' => '' ] - ); + $res['message'] = WC_Payments_Utils::esc_interpolated_html( + __( 'WooCommerce Payments requires WooCommerce to be installed and active.', 'woocommerce-payments' ), + [ 'a' => '' ] + ); - if ( current_user_can( 'install_plugins' ) ) { - if ( is_wp_error( validate_plugin( 'woocommerce/woocommerce.php' ) ) ) { - // WooCommerce is not installed. - $activate_url = wp_nonce_url( admin_url( 'update.php?action=install-plugin&plugin=woocommerce' ), 'install-plugin_woocommerce' ); - $activate_text = __( 'Install WooCommerce', 'woocommerce-payments' ); - } else { - // WooCommerce is installed, so it just needs to be enabled. - $activate_url = wp_nonce_url( admin_url( 'plugins.php?action=activate&plugin=woocommerce/woocommerce.php' ), 'activate-plugin_woocommerce/woocommerce.php' ); - $activate_text = __( 'Activate WooCommerce', 'woocommerce-payments' ); - } - $message .= ' ' . $activate_text . ''; + if ( current_user_can( 'install_plugins' ) ) { + if ( is_wp_error( validate_plugin( 'woocommerce/woocommerce.php' ) ) ) { + // WooCommerce is not installed. + $activate_url = wp_nonce_url( admin_url( 'update.php?action=install-plugin&plugin=woocommerce' ), 'install-plugin_woocommerce' ); + $activate_text = __( 'Install WooCommerce', 'woocommerce-payments' ); + } else { + // WooCommerce is installed, so it just needs to be enabled. + $activate_url = wp_nonce_url( admin_url( 'plugins.php?action=activate&plugin=woocommerce/woocommerce.php' ), 'activate-plugin_woocommerce/woocommerce.php' ); + $activate_text = __( 'Activate WooCommerce', 'woocommerce-payments' ); } - - self::display_admin_error( $message ); + $res['message'] .= ' ' . $activate_text . ''; } - return false; + + $res['passed'] = false; + return $res; } // Check if the version of WooCommerce is compatible with WooCommerce Payments. if ( version_compare( WC_VERSION, $wc_version, '<' ) ) { - if ( ! $silent ) { - $message = WC_Payments_Utils::esc_interpolated_html( - sprintf( - /* translators: %1: current WooCommerce Payment version, %2: required WC version number, %3: currently installed WC version number */ - __( 'WooCommerce Payments %1$s requires WooCommerce %2$s or greater to be installed (you are using %3$s). ', 'woocommerce-payments' ), - WCPAY_VERSION_NUMBER, - $wc_version, - WC_VERSION - ), - [ 'strong' => '' ] - ); - if ( current_user_can( 'update_plugins' ) ) { - // Take the user to the "plugins" screen instead of trying to update WooCommerce inline. WooCommerce adds important information - // on its plugin row regarding the currently installed extensions and their compatibility with the latest WC version. - $message .= '
'; - $message .= WC_Payments_Utils::esc_interpolated_html( + $res['message'] = WC_Payments_Utils::esc_interpolated_html( + sprintf( + /* translators: %1: current WooCommerce Payment version, %2: required WC version number, %3: currently installed WC version number */ + __( 'WooCommerce Payments %1$s requires WooCommerce %2$s or greater to be installed (you are using %3$s). ', 'woocommerce-payments' ), + WCPAY_VERSION_NUMBER, + $wc_version, + WC_VERSION + ), + [ 'strong' => '' ] + ); + + if ( current_user_can( 'update_plugins' ) ) { + // Take the user to the "plugins" screen instead of trying to update WooCommerce inline. WooCommerce adds important information + // on its plugin row regarding the currently installed extensions and their compatibility with the latest WC version. + $res['message'] .= '
' . WC_Payments_Utils::esc_interpolated_html( /* translators: a1: link to the Plugins page, a2: link to the page having all previous versions */ - __( 'Update WooCommerce (recommended) or manually re-install a previous version of WooCommerce Payments.', 'woocommerce-payments' ), - [ - - 'a1' => '', - 'strong' => '', - 'a2' => '', - ] - ); - } - self::display_admin_error( $message ); + __( 'Update WooCommerce (recommended) or manually re-install a previous version of WooCommerce Payments.', 'woocommerce-payments' ), + [ + + 'a1' => '', + 'strong' => '', + 'a2' => '', + ] + ); } - return false; + + $res['passed'] = false; + return $res; } // Check if the current WooCommerce version has WooCommerce Admin bundled (WC 4.0+) but it's disabled using a filter. if ( ! defined( 'WC_ADMIN_VERSION_NUMBER' ) ) { - if ( ! $silent ) { - self::display_admin_error( - WC_Payments_Utils::esc_interpolated_html( - __( 'WooCommerce Payments requires WooCommerce Admin to be enabled. Please remove the woocommerce_admin_disabled filter to use WooCommerce Payments.', 'woocommerce-payments' ), - [ 'code' => '' ] - ) - ); - } - return false; + $res['message'] = WC_Payments_Utils::esc_interpolated_html( + __( 'WooCommerce Payments requires WooCommerce Admin to be enabled. Please remove the woocommerce_admin_disabled filter to use WooCommerce Payments.', 'woocommerce-payments' ), + [ 'code' => '' ] + ); + + $res['passed'] = false; + return $res; } // Check if the version of WooCommerce Admin is compatible with WooCommerce Payments. if ( version_compare( WC_ADMIN_VERSION_NUMBER, WCPAY_MIN_WC_ADMIN_VERSION, '<' ) ) { - if ( ! $silent ) { - $message = WC_Payments_Utils::esc_interpolated_html( - sprintf( - /* translators: %1: required WC-Admin version number, %2: currently installed WC-Admin version number */ - __( 'WooCommerce Payments requires WooCommerce Admin %1$s or greater to be installed (you are using %2$s).', 'woocommerce-payments' ), - WCPAY_MIN_WC_ADMIN_VERSION, - WC_ADMIN_VERSION_NUMBER - ), - [ 'strong' => '' ] - ); + $res['message'] = WC_Payments_Utils::esc_interpolated_html( + sprintf( + /* translators: %1: required WC-Admin version number, %2: currently installed WC-Admin version number */ + __( 'WooCommerce Payments requires WooCommerce Admin %1$s or greater to be installed (you are using %2$s).', 'woocommerce-payments' ), + WCPAY_MIN_WC_ADMIN_VERSION, + WC_ADMIN_VERSION_NUMBER + ), + [ 'strong' => '' ] + ); // Let's assume for now that any WC-Admin version bundled with WooCommerce will meet our minimum requirements. - $message .= ' ' . __( 'There is a newer version of WooCommerce Admin bundled with WooCommerce.', 'woocommerce-payments' ); - if ( current_user_can( 'deactivate_plugins' ) ) { - $deactivate_url = wp_nonce_url( admin_url( 'plugins.php?action=deactivate&plugin=woocommerce-admin/woocommerce-admin.php' ), 'deactivate-plugin_woocommerce-admin/woocommerce-admin.php' ); - $message .= ' ' . __( 'Use the bundled version of WooCommerce Admin', 'woocommerce-payments' ) . ''; - } - self::display_admin_error( $message ); + $res['message'] .= ' ' . __( 'There is a newer version of WooCommerce Admin bundled with WooCommerce.', 'woocommerce-payments' ); + + if ( current_user_can( 'deactivate_plugins' ) ) { + $deactivate_url = wp_nonce_url( admin_url( 'plugins.php?action=deactivate&plugin=woocommerce-admin/woocommerce-admin.php' ), 'deactivate-plugin_woocommerce-admin/woocommerce-admin.php' ); + $res['message'] .= ' ' . __( 'Use the bundled version of WooCommerce Admin', 'woocommerce-payments' ) . ''; } - return false; + + $res['passed'] = false; + return $res; } // Check if the version of WordPress is compatible with WooCommerce Payments. if ( version_compare( get_bloginfo( 'version' ), $wp_version, '<' ) ) { - if ( ! $silent ) { - $message = WC_Payments_Utils::esc_interpolated_html( - sprintf( - /* translators: %1: required WP version number, %2: currently installed WP version number */ - __( 'WooCommerce Payments requires WordPress %1$s or greater (you are using %2$s).', 'woocommerce-payments' ), - $wp_version, - get_bloginfo( 'version' ) - ), - [ 'strong' => '' ] - ); - if ( current_user_can( 'update_core' ) ) { - $message .= ' ' . __( 'Update WordPress', 'woocommerce-payments' ) . ''; - } - self::display_admin_error( $message ); + $res['message'] = WC_Payments_Utils::esc_interpolated_html( + sprintf( + /* translators: %1: required WP version number, %2: currently installed WP version number */ + __( 'WooCommerce Payments requires WordPress %1$s or greater (you are using %2$s).', 'woocommerce-payments' ), + $wp_version, + get_bloginfo( 'version' ) + ), + [ 'strong' => '' ] + ); + if ( current_user_can( 'update_core' ) ) { + $res['message'] .= ' ' . __( 'Update WordPress', 'woocommerce-payments' ) . ''; } - return false; + + $res['passed'] = false; + return $res; } - return true; + $res['passed'] = true; + return $res; } /**