From b25e9e4a2b288a984dd08a2ab10591a70ddb93d2 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Wed, 22 Jul 2020 15:01:10 +0300 Subject: [PATCH] New customization system with fallback to old one --- src/include/class-wc-retailcrm-base.php | 47 ----------------- src/retailcrm.php | 70 ++++++++++++++++++------- tests/test-wc-retailcrm-base.php | 6 --- 3 files changed, 50 insertions(+), 73 deletions(-) diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index 269c1b1..acf71c5 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -56,14 +56,6 @@ if (!class_exists('WC_Retailcrm_Base')) { $this->init_settings_fields(); } - if (!class_exists('WC_Retailcrm_Orders')) { - include_once(static::checkCustomFile('orders')); - } - - if (!class_exists('WC_Retailcrm_Customers')) { - include_once(static::checkCustomFile('customers')); - } - $this->customers = new WC_Retailcrm_Customers( $this->apiClient, $this->settings, @@ -160,26 +152,7 @@ if (!class_exists('WC_Retailcrm_Base')) { return $settings; } - /** - * Check custom file - * - * @param string $file - * - * @return string - */ - public static function checkCustomFile($file) { - if (file_exists( WP_CONTENT_DIR . '/retailcrm-custom/class-wc-retailcrm-' . $file . '.php' )) { - return WP_CONTENT_DIR . '/retailcrm-custom/class-wc-retailcrm-' . $file . '.php'; - } - - return WP_PLUGIN_DIR . '/woo-retailcrm/include/class-wc-retailcrm-' . $file . '.php'; - } - public function generate_icml() { - if (!class_exists('WC_Retailcrm_Icml')) { - require_once (static::checkCustomFile('icml')); - } - $retailcrm_icml = new WC_Retailcrm_Icml(); $retailcrm_icml->generate(); } @@ -188,10 +161,6 @@ if (!class_exists('WC_Retailcrm_Base')) { * Get history */ public function retailcrm_history_get() { - if (!class_exists('WC_Retailcrm_History')) { - include_once(static::checkCustomFile('history')); - } - $retailcrm_history = new WC_Retailcrm_History($this->apiClient); $retailcrm_history->getHistory(); } @@ -207,10 +176,6 @@ if (!class_exists('WC_Retailcrm_Base')) { * Load stock from retailCRM */ public function load_stocks() { - if (!class_exists('WC_Retailcrm_Inventories')) { - include_once(static::checkCustomFile('inventories')); - } - $inventories = new WC_Retailcrm_Inventories($this->apiClient); $inventories->updateQuantity(); } @@ -376,10 +341,6 @@ if (!class_exists('WC_Retailcrm_Base')) { */ public function initialize_analytics() { - if (!class_exists('WC_Retailcrm_Google_Analytics')) { - include_once(static::checkCustomFile('ga')); - } - if ($this->get_option('ua') && $this->get_option('ua_code')) { $retailcrm_analytics = WC_Retailcrm_Google_Analytics::getInstance($this->settings); echo $retailcrm_analytics->initialize_analytics(); @@ -393,10 +354,6 @@ if (!class_exists('WC_Retailcrm_Base')) { */ public function send_analytics() { - if (!class_exists('WC_Retailcrm_Google_Analytics')) { - include_once(static::checkCustomFile('ga')); - } - if ($this->get_option('ua') == static::YES && $this->get_option('ua_code') && is_checkout()) { $retailcrm_analytics = WC_Retailcrm_Google_Analytics::getInstance($this->settings); echo $retailcrm_analytics->send_analytics(); @@ -410,10 +367,6 @@ if (!class_exists('WC_Retailcrm_Base')) { */ public function initialize_daemon_collector() { - if (!class_exists('WC_Retailcrm_Daemon_Collector')) { - include_once(static::checkCustomFile('daemon-collector')); - } - if ($this->get_option('daemon_collector') == static::YES && $this->get_option('daemon_collector_key')) { $retailcrm_daemon_collector = WC_Retailcrm_Daemon_Collector::getInstance($this->settings); echo $retailcrm_daemon_collector->initialize_daemon_collector(); diff --git a/src/retailcrm.php b/src/retailcrm.php index ed10388..2dd55e0 100644 --- a/src/retailcrm.php +++ b/src/retailcrm.php @@ -96,26 +96,56 @@ if (!class_exists( 'WC_Integration_Retailcrm')) : */ public static function load_module() { - require_once(dirname(__FILE__) . '/include/interfaces/class-wc-retailcrm-builder-interface.php'); - require_once(dirname(__FILE__) . '/include/models/class-wc-retailcrm-customer-switcher-state.php'); - require_once(dirname(__FILE__) . '/include/models/class-wc-retailcrm-customer-switcher-result.php'); - require_once(dirname(__FILE__ ) . '/include/components/class-wc-retailcrm-logger.php'); - require_once(dirname(__FILE__ ) . '/include/components/class-wc-retailcrm-history-assembler.php'); - require_once(dirname(__FILE__ ) . '/include/components/class-wc-retailcrm-paginated-request.php'); - require_once(dirname(__FILE__) . '/include/components/class-wc-retailcrm-customer-switcher.php'); - require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstract-builder.php'); - require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-settings.php'); - require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-data.php'); - require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-address.php'); - require_once(dirname(__FILE__ ) . '/include/customer/woocommerce/class-wc-retailcrm-wc-customer-builder.php'); - require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order.php'); - require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-payment.php'); - require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-item.php'); - require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-address.php'); - require_once(dirname(__FILE__ ) . '/include/customer/class-wc-retailcrm-customer-address.php'); - require_once(dirname(__FILE__ ) . '/include/customer/class-wc-retailcrm-customer-corporate-address.php'); - require_once(dirname(__FILE__ ) . '/include/class-wc-retailcrm-base.php'); - require_once(dirname(__FILE__ ) . '/include/functions.php'); + require_once(self::checkCustomFile('include/interfaces/class-wc-retailcrm-builder-interface.php')); + require_once(self::checkCustomFile('include/models/class-wc-retailcrm-customer-switcher-state.php')); + require_once(self::checkCustomFile('include/models/class-wc-retailcrm-customer-switcher-result.php')); + require_once(self::checkCustomFile('include/components/class-wc-retailcrm-logger.php')); + require_once(self::checkCustomFile('include/components/class-wc-retailcrm-history-assembler.php')); + require_once(self::checkCustomFile('include/components/class-wc-retailcrm-paginated-request.php')); + require_once(self::checkCustomFile('include/components/class-wc-retailcrm-customer-switcher.php')); + require_once(self::checkCustomFile('include/abstracts/class-wc-retailcrm-abstract-builder.php')); + require_once(self::checkCustomFile('include/abstracts/class-wc-retailcrm-abstracts-settings.php')); + require_once(self::checkCustomFile('include/abstracts/class-wc-retailcrm-abstracts-data.php')); + require_once(self::checkCustomFile('include/abstracts/class-wc-retailcrm-abstracts-address.php')); + require_once(self::checkCustomFile('include/customer/woocommerce/class-wc-retailcrm-wc-customer-builder.php')); + require_once(self::checkCustomFile('include/order/class-wc-retailcrm-order.php')); + require_once(self::checkCustomFile('include/order/class-wc-retailcrm-order-payment.php')); + require_once(self::checkCustomFile('include/order/class-wc-retailcrm-order-item.php')); + require_once(self::checkCustomFile('include/order/class-wc-retailcrm-order-address.php')); + require_once(self::checkCustomFile('include/customer/class-wc-retailcrm-customer-address.php')); + require_once(self::checkCustomFile('include/customer/class-wc-retailcrm-customer-corporate-address.php')); + require_once(self::checkCustomFile('include/class-wc-retailcrm-icml.php')); + require_once(self::checkCustomFile('include/class-wc-retailcrm-orders.php')); + require_once(self::checkCustomFile('include/class-wc-retailcrm-customers.php')); + require_once(self::checkCustomFile('include/class-wc-retailcrm-inventories.php')); + require_once(self::checkCustomFile('include/class-wc-retailcrm-history.php')); + require_once(self::checkCustomFile('include/class-wc-retailcrm-ga.php')); + require_once(self::checkCustomFile('include/class-wc-retailcrm-daemon-collector.php')); + require_once(self::checkCustomFile('include/class-wc-retailcrm-base.php')); + require_once(self::checkCustomFile('include/functions.php')); + } + + /** + * Check custom file + * + * @param string $file + * + * @return string + */ + public static function checkCustomFile($file) + { + $wooPath = WP_PLUGIN_DIR . '/woo-retailcrm/' . $file; + $withoutInclude = WP_CONTENT_DIR . '/retailcrm-custom/' . str_replace('include/', '', $file); + + if (file_exists($withoutInclude)) { + return $withoutInclude; + } + + if (file_exists($wooPath)) { + return $wooPath; + } + + return dirname(__FILE__) . '/' . $file; } /** diff --git a/tests/test-wc-retailcrm-base.php b/tests/test-wc-retailcrm-base.php index a5efca4..27d72a9 100644 --- a/tests/test-wc-retailcrm-base.php +++ b/tests/test-wc-retailcrm-base.php @@ -34,12 +34,6 @@ class WC_Retailcrm_Base_Test extends WC_Retailcrm_Test_Case_Helper $this->unit = new \WC_Retailcrm_Base($this->apiMock); } - public function test_retailcrm_check_custom_file() - { - $file = \WC_Retailcrm_Base::checkCustomFile('ga'); - $this->assertInternalType('string', $file); - } - public function test_retailcrm_form_fields() { $this->assertInternalType('array', $this->unit->form_fields);