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/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..8f0bfa2 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,47 @@ if (!class_exists('WC_Retailcrm_Base')) { ) ); } + + /** + * Deactivate module in marketplace retailCRM + * + * @return void + */ + public function deactivate() + { + $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'); + } + + /** + * @param $settings + * + * @return void + */ + private function activate_integration($settings) + { + $client_id = get_option('retailcrm_client_id'); + + if (!$client_id) { + $client_id = uniqid(); + } + + 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, $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 e18c408..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); @@ -62,6 +66,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 +81,47 @@ class WC_Retailcrm_Plugin { } } + /** + * Edit configuration in CRM + * + * @param WC_Retailcrm_Proxy $api_client + * @param string $client_id + * @param bool $active + * + * @return boolean + */ + public static function integration_module($api_client, $client_id, $active = true) { + if (!$api_client) { + return false; + } + + $configuration = array( + 'clientId' => $client_id, + 'code' => self::INTEGRATION_CODE . '-' . $client_id, + 'integrationCode' => self::INTEGRATION_CODE, + 'active' => $active, + 'name' => 'WooCommerce', + 'logo' => self::MARKETPLACE_LOGO + ); + + $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; 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 @@ 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();