From e89258b799d6d9efc7217823fd230361f73439ef Mon Sep 17 00:00:00 2001 From: Tamara Zuk Date: Sat, 21 Nov 2020 02:36:40 -0500 Subject: [PATCH] Update WooCommerce version check to use WC_Version constant - closes #19 --- woocommerce-product-sku-generator.php | 86 ++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 16 deletions(-) diff --git a/woocommerce-product-sku-generator.php b/woocommerce-product-sku-generator.php index 5f0eeb5..d68a69a 100644 --- a/woocommerce-product-sku-generator.php +++ b/woocommerce-product-sku-generator.php @@ -24,15 +24,11 @@ defined( 'ABSPATH' ) or exit; -// WC version check -if ( ! WC_SKU_Generator::is_plugin_active('woocommerce.php' ) || version_compare( get_option( 'woocommerce_db_version' ), WC_SKU_Generator::MIN_WOOCOMMERCE_VERSION, '<' ) ) { - add_action( 'admin_notices', array( 'WC_SKU_Generator', 'render_outdated_wc_version_notice' ) ); +// check if WooCommerce is active +if ( ! WC_SKU_Generator::is_plugin_active( 'woocommerce.php' ) ) { return; } -// Make sure we're loaded after WC and fire it up! -add_action( 'plugins_loaded', 'wc_sku_generator' ); - /** * Class WC_SKU_Generator @@ -61,35 +57,52 @@ class WC_SKU_Generator { */ public function __construct() { + // initialize the plugin + add_action( 'plugins_loaded', [ $this, 'initialize' ] ); + // load translations - add_action( 'init', array( $this, 'load_translation' ) ); + add_action( 'init', [ $this, 'load_translation' ] ); + } + + /** + * Initializes the plugin, bailing if any required conditions are not met, + * including minimum WooCommerce version. + * + * @since 2.4.6-dev.1 + */ + public function initialize() { + + if ( ! $this->is_minimum_wc_version_met() ) { + // halt functionality + return; + } if ( is_admin() ) { // disable variation SKUs when generated - add_action( 'woocommerce_product_write_panel_tabs', array( $this, 'maybe_disable_variation_skus' ), 10, 2 ); + add_action( 'woocommerce_product_write_panel_tabs', [ $this, 'maybe_disable_variation_skus' ], 10, 2 ); // generate SKUs when variations are saved via ajax - add_action( 'woocommerce_ajax_save_product_variations', array( $this, 'ajax_maybe_generate_variation_skus' ) ); + add_action( 'woocommerce_ajax_save_product_variations', [ $this, 'ajax_maybe_generate_variation_skus' ] ); } if ( is_admin() && ! is_ajax() ) { // add settings - add_filter( 'woocommerce_products_general_settings', array( $this, 'add_settings' ) ); - add_action( 'admin_print_scripts', array( $this, 'admin_js' ) ); + add_filter( 'woocommerce_products_general_settings', [ $this, 'add_settings' ] ); + add_action( 'admin_print_scripts', [ $this, 'admin_js' ] ); // add plugin links - add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_plugin_links' ) ); + add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), [ $this, 'add_plugin_links' ] ); // save the generated SKUs during product edit / bulk edit - add_action( 'woocommerce_product_bulk_edit_save', array( $this, 'maybe_save_sku' ) ); - add_action( 'woocommerce_process_product_meta', array( $this, 'maybe_save_sku' ), 100, 2 ); + add_action( 'woocommerce_product_bulk_edit_save', [ $this, 'maybe_save_sku' ] ); + add_action( 'woocommerce_process_product_meta', [ $this, 'maybe_save_sku' ], 100, 2 ); - add_action( 'woocommerce_save_product_variation', array( $this, 'maybe_save_varation_sku' ) ); + add_action( 'woocommerce_save_product_variation', [ $this, 'maybe_save_varation_sku' ] ); // disable SKU fields when being generated by the plugin - add_action( 'init', array( $this, 'maybe_disable_skus' ) ); + add_action( 'init', [ $this, 'maybe_disable_skus' ] ); // run every time $this->install(); @@ -207,6 +220,43 @@ public static function is_plugin_active( $plugin_name ) { } + /** + * Returns the currently installed version of WooCommerce. + * + * @since 2.4.6-dev.1 + * @return string/null The WooCommerce version number or null. + */ + private static function get_wc_version() { + return defined( 'WC_VERSION' ) && WC_VERSION ? WC_VERSION : null; + } + + + /** + * Determines if the minimum WooCommerce version is met. + * + * @since 2.4.6-dev.1 + * @return boolean true if the required version is met, false otherwise + */ + private function is_minimum_wc_version_met() { + + $version_met = true; + + // if a plugin defines a minimum WC version, render a notice and skip loading the plugin + if ( defined( 'self::MIN_WOOCOMMERCE_VERSION' ) && version_compare( self::get_wc_version(), self::MIN_WOOCOMMERCE_VERSION, '<' ) ) { + + if ( is_admin() && ! is_ajax() && ! has_action( 'admin_notices', [ $this, 'render_outdated_wc_version_notice' ] ) ) { + + add_action( 'admin_notices', [ $this, 'render_outdated_wc_version_notice' ] ); + } + + $version_met = false; + } + + return $version_met; + } + + + /** * Renders a notice when WooCommerce version is outdated. * @@ -782,3 +832,7 @@ private function upgrade( $installed_version ) { function wc_sku_generator() { return WC_SKU_Generator::instance(); } + + +// fire it up! +wc_sku_generator();