Skip to content

Commit

Permalink
Update WooCommerce version check to use WC_Version constant - closes g…
Browse files Browse the repository at this point in the history
  • Loading branch information
tamarazuk committed Nov 21, 2020
1 parent 6de043d commit e89258b
Showing 1 changed file with 70 additions and 16 deletions.
86 changes: 70 additions & 16 deletions woocommerce-product-sku-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -782,3 +832,7 @@ private function upgrade( $installed_version ) {
function wc_sku_generator() {
return WC_SKU_Generator::instance();
}


// fire it up!
wc_sku_generator();

0 comments on commit e89258b

Please sign in to comment.