From 7c6e55c60ac8ee64a380b7351b64d40efda77dab Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Fri, 31 Aug 2018 11:13:16 +0300 Subject: [PATCH 1/4] Added activate/deactivate in marketplace methods --- .../api/class-wc-retailcrm-client-v5.php | 30 ++++++++++++- src/include/class-wc-retailcrm-base.php | 38 +++++++++++++++- src/include/class-wc-retailcrm-plugin.php | 44 +++++++++++++++++++ 3 files changed, 109 insertions(+), 3 deletions(-) diff --git a/src/include/api/class-wc-retailcrm-client-v5.php b/src/include/api/class-wc-retailcrm-client-v5.php index e298daf..222f784 100644 --- a/src/include/api/class-wc-retailcrm-client-v5.php +++ b/src/include/api/class-wc-retailcrm-client-v5.php @@ -53,7 +53,7 @@ class WC_Retailcrm_Client_V5 /** * Returns api versions list - * + * * @return WC_Retailcrm_Response */ public function apiVersions() @@ -2292,6 +2292,34 @@ class WC_Retailcrm_Client_V5 ); } + /** + * Edit module configuration + * + * @param array $configuration + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function integrationModulesEdit(array $configuration) + { + if (!count($configuration) || empty($configuration['code'])) { + throw new \InvalidArgumentException( + 'Parameter `configuration` must contains a data & configuration `code` must be set' + ); + } + + $code = $configuration['code']; + + return $this->client->makeRequest( + "/integration-modules/$code/edit", + WC_Retailcrm_Request::METHOD_POST, + array('integrationModule' => json_encode($configuration)) + ); + } + /** * Update CRM basic statistic * diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index e8fca93..a8181dd 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -66,6 +66,9 @@ if (!class_exists('WC_Retailcrm_Base')) { add_action('woocommerce_update_order', array($this, 'update_order'), 11, 1); add_action('wp_print_scripts', array($this, 'initialize_analytics'), 98); add_action('wp_print_footer_scripts', array($this, 'send_analytics'), 99); + + // Deactivate hook + add_action('retailcrm_deactivate', array($this, 'deactivate')); } public function api_sanitized($settings) @@ -94,6 +97,10 @@ if (!class_exists('WC_Retailcrm_Base')) { wp_clear_scheduled_hook('retailcrm_icml'); } + if (!$this->get_errors() && !get_option('retailcrm_active_in_crm')) { + $this->activate_integration($settings); + } + return $settings; } @@ -794,9 +801,9 @@ if (!class_exists('WC_Retailcrm_Base')) { WC_Admin_Settings::add_error( esc_html__( 'The selected API version is unavailable', 'retailcrm' ) ); $value = ''; } - - return $value; } + + return $value; } /** @@ -897,6 +904,7 @@ if (!class_exists('WC_Retailcrm_Base')) { return false; } + /** * Add button in admin */ @@ -931,5 +939,31 @@ if (!class_exists('WC_Retailcrm_Base')) { ) ); } + + public function deactivate() + { + $api_client = $this->getApiClient(); + $clientId = get_option('retailcrm_client_id'); + WC_Retailcrm_Plugin::integration_module($api_client, $clientId, false); + } + + private function activate_integration($settings) + { + $clientId = get_option('retailcrm_client_id'); + + if ($settings['api_url'] && $settings['api_key'] && $settings['api_version']) { + $api_client = new WC_Retailcrm_Proxy( + $settings['api_url'], + $settings['api_key'], + $settings['api_version'] + ); + + $result = WC_Retailcrm_Plugin::integration_module($api_client); + + if ($result) { + update_option('retailcrm_active_in_crm', true); + } + } + } } } diff --git a/src/include/class-wc-retailcrm-plugin.php b/src/include/class-wc-retailcrm-plugin.php index e18c408..9adbc3f 100644 --- a/src/include/class-wc-retailcrm-plugin.php +++ b/src/include/class-wc-retailcrm-plugin.php @@ -62,6 +62,8 @@ class WC_Retailcrm_Plugin { } public function deactivate() { + do_action('retailcrm_deactivate'); + if (wp_next_scheduled('retailcrm_icml')) { wp_clear_scheduled_hook('retailcrm_icml'); } @@ -75,6 +77,48 @@ class WC_Retailcrm_Plugin { } } + /** + * Edit configuration in CRM + * + * @param WC_Retailcrm_Proxy $api_client + * @param string $cliendId + * @param bool $active + * + * @return boolean + */ + public static function integration_module($api_client, $cliendId, $active = true) + { + if (!$api_client) { + return false; + } + + $configuration = array( + 'clientId' => $cliendId, + 'code' => 'woocommerce', + 'integrationCode' => 'woocommerce', + 'active' => $active, + 'name' => 'WooCommerce', + 'logo' => 'https://s3.eu-central-1.amazonaws.com/retailcrm-billing/images/5b69ce4bda663-woocommercesvg2.svg' + ); + + $response = $api_client->integrationModulesEdit($configuration); + + if (!$response) { + return false; + } + + if ($response->isSuccessful()) { + return true; + } + + return false; + } + + /** + * Check running history + * + * @return boolean + */ public static function history_running() { return self::$history_run; From 83b668deb3cd1f857cdb79aa177239dae39678e0 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Wed, 5 Sep 2018 11:13:32 +0300 Subject: [PATCH 2/4] Send activate/deactivate information to crm --- src/include/class-wc-retailcrm-base.php | 19 +++++++++++++++++-- src/include/class-wc-retailcrm-plugin.php | 6 +++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index a8181dd..9d35209 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -940,6 +940,11 @@ if (!class_exists('WC_Retailcrm_Base')) { ); } + /** + * Deactivate module in marketplace retailCRM + * + * @return void + */ public function deactivate() { $api_client = $this->getApiClient(); @@ -947,9 +952,18 @@ if (!class_exists('WC_Retailcrm_Base')) { WC_Retailcrm_Plugin::integration_module($api_client, $clientId, false); } + /** + * @param $settings + * + * @return void + */ private function activate_integration($settings) { - $clientId = get_option('retailcrm_client_id'); + $client_id = get_option('retailcrm_client_id'); + + if (!$client_id) { + $client_id = hash('md5', date('Y-m-d H:i:s')); + } if ($settings['api_url'] && $settings['api_key'] && $settings['api_version']) { $api_client = new WC_Retailcrm_Proxy( @@ -958,10 +972,11 @@ if (!class_exists('WC_Retailcrm_Base')) { $settings['api_version'] ); - $result = WC_Retailcrm_Plugin::integration_module($api_client); + $result = WC_Retailcrm_Plugin::integration_module($api_client, $client_id); if ($result) { update_option('retailcrm_active_in_crm', true); + update_option('retailcrm_client_id', $client_id); } } } diff --git a/src/include/class-wc-retailcrm-plugin.php b/src/include/class-wc-retailcrm-plugin.php index 9adbc3f..1310d62 100644 --- a/src/include/class-wc-retailcrm-plugin.php +++ b/src/include/class-wc-retailcrm-plugin.php @@ -81,19 +81,19 @@ class WC_Retailcrm_Plugin { * Edit configuration in CRM * * @param WC_Retailcrm_Proxy $api_client - * @param string $cliendId + * @param string $cliend_id * @param bool $active * * @return boolean */ - public static function integration_module($api_client, $cliendId, $active = true) + public static function integration_module($api_client, $cliend_id, $active = true) { if (!$api_client) { return false; } $configuration = array( - 'clientId' => $cliendId, + 'clientId' => $cliend_id, 'code' => 'woocommerce', 'integrationCode' => 'woocommerce', 'active' => $active, From fead96c4ca7b491257fd33ed62da76abe18de1e8 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Thu, 25 Oct 2018 11:10:29 +0300 Subject: [PATCH 3/4] Add delete options on delete plugin --- src/include/class-wc-retailcrm-base.php | 3 ++- src/include/class-wc-retailcrm-plugin.php | 17 ++++++++++------- src/uninstall.php | 2 ++ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index 9d35209..8f0bfa2 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -950,6 +950,7 @@ if (!class_exists('WC_Retailcrm_Base')) { $api_client = $this->getApiClient(); $clientId = get_option('retailcrm_client_id'); WC_Retailcrm_Plugin::integration_module($api_client, $clientId, false); + delete_option('retailcrm_active_in_crm'); } /** @@ -962,7 +963,7 @@ if (!class_exists('WC_Retailcrm_Base')) { $client_id = get_option('retailcrm_client_id'); if (!$client_id) { - $client_id = hash('md5', date('Y-m-d H:i:s')); + $client_id = uniqid(); } if ($settings['api_url'] && $settings['api_key'] && $settings['api_version']) { diff --git a/src/include/class-wc-retailcrm-plugin.php b/src/include/class-wc-retailcrm-plugin.php index 1310d62..f000ed0 100644 --- a/src/include/class-wc-retailcrm-plugin.php +++ b/src/include/class-wc-retailcrm-plugin.php @@ -3,9 +3,13 @@ class WC_Retailcrm_Plugin { public $file; + public static $history_run = false; private static $instance = null; + const MARKETPLACE_LOGO = 'https://s3.eu-central-1.amazonaws.com/retailcrm-billing/images/5b69ce4bda663-woocommercesvg2.svg'; + const INTEGRATION_CODE = 'woocommerce'; + public static function getInstance($file) { if (self::$instance === null) { self::$instance = new self($file); @@ -81,24 +85,23 @@ class WC_Retailcrm_Plugin { * Edit configuration in CRM * * @param WC_Retailcrm_Proxy $api_client - * @param string $cliend_id + * @param string $client_id * @param bool $active * * @return boolean */ - public static function integration_module($api_client, $cliend_id, $active = true) - { + public static function integration_module($api_client, $client_id, $active = true) { if (!$api_client) { return false; } $configuration = array( - 'clientId' => $cliend_id, - 'code' => 'woocommerce', - 'integrationCode' => 'woocommerce', + 'clientId' => $client_id, + 'code' => self::INTEGRATION_CODE . '-' . $client_id, + 'integrationCode' => self::INTEGRATION_CODE, 'active' => $active, 'name' => 'WooCommerce', - 'logo' => 'https://s3.eu-central-1.amazonaws.com/retailcrm-billing/images/5b69ce4bda663-woocommercesvg2.svg' + 'logo' => self::MARKETPLACE_LOGO ); $response = $api_client->integrationModulesEdit($configuration); diff --git a/src/uninstall.php b/src/uninstall.php index 913fbcc..5e9c27f 100644 --- a/src/uninstall.php +++ b/src/uninstall.php @@ -39,6 +39,8 @@ wp_clear_scheduled_hook('retailcrm_inventories'); $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = 'woocommerce_integration-retailcrm_settings';"); $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = 'retailcrm_customers_history_since_id';"); $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = 'retailcrm_orders_history_since_id';"); +$wpdb->query("DELETE FROM $wpdb->options WHERE option_name = 'retailcrm_active_in_crm';"); +$wpdb->query("DELETE FROM $wpdb->options WHERE option_name = 'retailcrm_client_id';"); // Clear any cached data that has been removed wp_cache_flush(); From 610acefcfba55a6cbd5b2b2808d15ce8de143392 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Thu, 25 Oct 2018 11:20:12 +0300 Subject: [PATCH 4/4] v3.3.5 --- CHANGELOG.md | 3 +++ VERSION | 2 +- src/readme.txt | 5 +++++ src/retailcrm.php | 2 +- src/uninstall.php | 2 +- 5 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20623aa..3ee628e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2018-10-25 v3.3.5 +* Добавлена активация модуля в маркетплейсе retailCRM + ## 2018-08-30 v3.3.4 * Исправлен баг с занулением количества товара в заказе WC diff --git a/VERSION b/VERSION index 2c6109e..0163af7 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.3.4 \ No newline at end of file +3.3.5 \ No newline at end of file diff --git a/src/readme.txt b/src/readme.txt index 8e96f95..3a4806e 100644 --- a/src/readme.txt +++ b/src/readme.txt @@ -45,6 +45,9 @@ API-ключ должен быть для отдельного магазина 2. В появившихся списках справочников настройте соответствие способов доставки и оплаты, а так же статусов заказов. Отметьте галочку "Выгружать остатки", если хотите выгружать остатки из Retailcrm в магазин (подробнее смотрите в описании). == Changelog == += 3.3.5 = +* Добавлена активация модуля в маркетплейсе retailCRM + = 3.3.4 = * Улучшена обработка истории заказов @@ -147,6 +150,8 @@ API-ключ должен быть для отдельного магазина * Исправелены ошибки. == Upgrade Notice == += 3.3.5 = +После обновления плагина необходимо пересохранить настройки для того, чтобы активировать модуль в маркетплейсе retailCRM = 3.3.4 = Улучшена обработка истории заказов diff --git a/src/retailcrm.php b/src/retailcrm.php index e8e4e6c..5441f82 100644 --- a/src/retailcrm.php +++ b/src/retailcrm.php @@ -1,6 +1,6 @@