1
0
mirror of synced 2025-03-06 09:06:07 +03:00
woocommerce-module/src/include/class-wc-retailcrm-base.php

384 lines
13 KiB
PHP
Raw Normal View History

<?php
/**
* RetailCRM Integration.
*
* @package WC_Retailcrm_Base
* @category Integration
* @author RetailCRM
*/
if (!class_exists('WC_Retailcrm_Base')) {
2019-04-16 13:37:53 +03:00
if (!class_exists('WC_Retailcrm_Abstracts_Settings')) {
include_once 'abstracts/class-wc-retailcrm-abstracts-settings.php';
}
/**
* Class WC_Retailcrm_Base
*/
2019-03-20 15:51:31 +03:00
class WC_Retailcrm_Base extends WC_Retailcrm_Abstracts_Settings
{
protected $api_url;
protected $api_key;
2019-03-20 15:51:31 +03:00
protected $apiClient;
protected $order_item;
protected $order_address;
protected $customers;
protected $orders;
/**
* Init and hook in the integration.
* @param $retailcrm (default = false)
*/
public function __construct($retailcrm = false) {
2019-03-20 15:51:31 +03:00
parent::__construct();
2019-03-20 15:51:31 +03:00
if (!class_exists( 'WC_Retailcrm_Proxy')) {
include_once(__DIR__ . '/api/class-wc-retailcrm-proxy.php');
}
if ($retailcrm === false) {
$this->apiClient = $this->getApiClient();
} else {
$this->apiClient = $retailcrm;
2019-03-06 12:28:48 +03:00
$this->init_settings_fields();
}
2019-03-20 15:51:31 +03:00
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,
new WC_Retailcrm_Customer_Address
);
$this->orders = new WC_Retailcrm_Orders(
$this->apiClient,
$this->settings,
new WC_Retailcrm_Order_Item($this->settings),
new WC_Retailcrm_Order_Address,
$this->customers,
new WC_Retailcrm_Order($this->settings),
new WC_Retailcrm_Order_Payment($this->settings)
);
// Actions.
add_action('woocommerce_update_options_integration_' . $this->id, array($this, 'process_admin_options'));
2018-08-22 15:54:47 +03:00
add_filter('woocommerce_settings_api_sanitized_fields_' . $this->id, array($this, 'api_sanitized'));
2018-08-30 14:52:48 +03:00
add_action('admin_bar_menu', array($this, 'add_retailcrm_button'), 100 );
add_action('woocommerce_checkout_order_processed', array($this, 'retailcrm_process_order'), 10, 1);
add_action('retailcrm_history', array($this, 'retailcrm_history_get'));
add_action('retailcrm_icml', array($this, 'generate_icml'));
add_action('retailcrm_inventories', array($this, 'load_stocks'));
add_action('wp_ajax_do_upload', array($this, 'upload_to_crm'));
add_action('wp_ajax_generate_icml', array($this, 'generate_icml'));
2018-05-28 12:56:19 +03:00
add_action('wp_ajax_order_upload', array($this, 'order_upload'));
add_action('admin_print_footer_scripts', array($this, 'ajax_upload'), 99);
add_action('admin_print_footer_scripts', array($this, 'ajax_generate_icml'), 99);
2018-05-28 12:56:19 +03:00
add_action('admin_print_footer_scripts', array($this, 'ajax_selected_order'), 99);
add_action('woocommerce_created_customer', array($this, 'create_customer'), 10, 1);
add_action('woocommerce_update_customer', array($this, 'update_customer'), 10, 1);
add_action('wp_print_scripts', array($this, 'initialize_analytics'), 98);
2019-01-16 15:33:26 +03:00
add_action('wp_print_scripts', array($this, 'initialize_daemon_collector'), 99);
add_action('wp_print_footer_scripts', array($this, 'send_analytics'), 99);
2019-03-20 15:51:31 +03:00
if (!$this->get_option('deactivate_update_order')
|| $this->get_option('deactivate_update_order') == static::NO
2019-03-06 10:09:12 +03:00
) {
2019-03-20 15:51:31 +03:00
add_action('woocommerce_update_order', array($this, 'update_order'), 11, 1);
2019-03-06 10:09:12 +03:00
}
// Deactivate hook
add_action('retailcrm_deactivate', array($this, 'deactivate'));
}
2019-03-06 10:09:12 +03:00
/**
* Init settings fields
*/
public function init_settings_fields()
{
$this->init_form_fields();
$this->init_settings();
}
/**
* @param $settings
*
* @return array
*/
2018-08-22 15:54:47 +03:00
public function api_sanitized($settings)
{
2019-03-20 15:51:31 +03:00
if (isset($settings['sync']) && $settings['sync'] == static::YES) {
2018-08-22 15:54:47 +03:00
if (!wp_next_scheduled('retailcrm_inventories')) {
wp_schedule_event(time(), 'fiveteen_minutes', 'retailcrm_inventories');
}
2019-03-20 15:51:31 +03:00
} elseif (isset($settings['sync']) && $settings['sync'] == static::NO) {
2018-08-22 15:54:47 +03:00
wp_clear_scheduled_hook('retailcrm_inventories');
}
2019-03-20 15:51:31 +03:00
if (isset($settings['history']) && $settings['history'] == static::YES) {
2018-08-22 15:54:47 +03:00
if (!wp_next_scheduled('retailcrm_history')) {
wp_schedule_event(time(), 'five_minutes', 'retailcrm_history');
}
2019-03-20 15:51:31 +03:00
} elseif (isset($settings['history']) && $settings['history'] == static::NO) {
2018-08-22 15:54:47 +03:00
wp_clear_scheduled_hook('retailcrm_history');
}
2019-03-20 15:51:31 +03:00
if (isset($settings['icml']) && $settings['icml'] == static::YES) {
2018-08-22 15:54:47 +03:00
if (!wp_next_scheduled('retailcrm_icml')) {
wp_schedule_event(time(), 'three_hours', 'retailcrm_icml');
}
2019-03-20 15:51:31 +03:00
} elseif (isset($settings['icml']) && $settings['icml'] == static::NO) {
2018-08-22 15:54:47 +03:00
wp_clear_scheduled_hook('retailcrm_icml');
}
if (!$this->get_errors() && !get_option('retailcrm_active_in_crm')) {
$this->activate_integration($settings);
}
2018-08-22 15:54:47 +03:00
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';
}
2019-02-15 15:23:06 +03:00
return WP_PLUGIN_DIR . '/woo-retailcrm/include/class-wc-retailcrm-' . $file . '.php';
}
public function generate_icml() {
if (!class_exists('WC_Retailcrm_Icml')) {
2019-03-20 15:51:31 +03:00
require_once (static::checkCustomFile('icml'));
}
$retailcrm_icml = new WC_Retailcrm_Icml();
$retailcrm_icml->generate();
}
/**
* Get history
*/
public function retailcrm_history_get() {
if (!class_exists('WC_Retailcrm_History')) {
2019-03-20 15:51:31 +03:00
include_once(static::checkCustomFile('history'));
}
$retailcrm_history = new WC_Retailcrm_History($this->apiClient);
$retailcrm_history->getHistory();
}
/**
* @param int $order_id
*/
public function retailcrm_process_order($order_id) {
2019-03-20 15:51:31 +03:00
$this->orders->orderCreate($order_id);
}
/**
* Load stock from retailCRM
*/
public function load_stocks() {
if (!class_exists('WC_Retailcrm_Inventories')) {
2019-03-20 15:51:31 +03:00
include_once(static::checkCustomFile('inventories'));
}
$inventories = new WC_Retailcrm_Inventories($this->apiClient);
$inventories->updateQuantity();
}
2018-05-28 12:56:19 +03:00
/**
* Upload selected orders
*/
public function order_upload() {
$ids = false;
if (isset($_GET['order_ids_retailcrm'])) {
$ids = explode(',', $_GET['order_ids_retailcrm']);
}
if ($ids) {
2019-03-20 15:51:31 +03:00
$this->orders->ordersUpload($ids, true);
2018-05-28 12:56:19 +03:00
}
}
/**
* Upload archive customers and order to retailCRM
*/
public function upload_to_crm()
{
2019-03-20 15:51:31 +03:00
$options = array_filter(get_option(static::$option_key));
2019-03-20 15:51:31 +03:00
$this->customers->customersUpload();
$this->orders->ordersUpload();
2019-03-20 15:51:31 +03:00
$options['uploads'] = static::YES;
update_option(static::$option_key, $options);
2018-05-28 12:56:19 +03:00
}
/**
* Create customer in retailCRM
* @param int $customer_id
*/
public function create_customer($customer_id)
{
if (WC_Retailcrm_Plugin::history_running() === true) {
return;
}
2019-03-20 15:51:31 +03:00
$this->customers->createCustomer($customer_id);
}
/**
* Edit customer in retailCRM
* @param int $customer_id
*/
public function update_customer($customer_id)
{
if (WC_Retailcrm_Plugin::history_running() === true) {
return;
}
2019-03-20 15:51:31 +03:00
$this->customers->updateCustomer($customer_id);
}
/**
* Edit order in retailCRM
* @param int $order_id
*/
public function update_order($order_id)
{
if (WC_Retailcrm_Plugin::history_running() === true) {
return;
}
2019-03-20 15:51:31 +03:00
$this->orders->updateOrder($order_id);
}
/**
* Init google analytics code
*/
public function initialize_analytics()
{
if (!class_exists('WC_Retailcrm_Google_Analytics')) {
2019-03-20 15:51:31 +03:00
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();
} else {
echo '';
}
}
/**
* Google analytics send code
*/
public function send_analytics()
{
if (!class_exists('WC_Retailcrm_Google_Analytics')) {
2019-03-20 15:51:31 +03:00
include_once(static::checkCustomFile('ga'));
}
2019-03-20 15:51:31 +03:00
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();
} else {
echo '';
}
}
2019-01-16 15:33:26 +03:00
/**
* Daemon collector
*/
public function initialize_daemon_collector()
{
if (!class_exists('WC_Retailcrm_Daemon_Collector')) {
2019-03-20 15:51:31 +03:00
include_once(static::checkCustomFile('daemon-collector'));
2019-01-16 15:33:26 +03:00
}
2019-03-20 15:51:31 +03:00
if ($this->get_option('daemon_collector') == static::YES && $this->get_option('daemon_collector_key')) {
2019-01-16 15:33:26 +03:00
$retailcrm_daemon_collector = WC_Retailcrm_Daemon_Collector::getInstance($this->settings);
echo $retailcrm_daemon_collector->initialize_daemon_collector();
} else {
echo '';
}
}
/**
* Get retailcrm api client
*
* @return bool|WC_Retailcrm_Proxy
*/
public function getApiClient()
{
if ($this->get_option('api_url') && $this->get_option('api_key')) {
return new WC_Retailcrm_Proxy(
$this->get_option('api_url'),
$this->get_option('api_key'),
$this->get_option('api_version')
);
}
return false;
}
/**
* Deactivate module in marketplace retailCRM
*
* @return void
*/
public function deactivate()
{
$api_client = $this->getApiClient();
$clientId = get_option('retailcrm_client_id');
2018-12-11 17:20:15 +03:00
$api_version = $this->get_option('api_version');
WC_Retailcrm_Plugin::integration_module($api_client, $clientId, $api_version, false);
2018-10-25 11:10:29 +03:00
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) {
2018-10-25 11:10:29 +03:00
$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']
);
2018-12-11 17:20:15 +03:00
$result = WC_Retailcrm_Plugin::integration_module($api_client, $client_id, $settings['api_version']);
if ($result) {
update_option('retailcrm_active_in_crm', true);
update_option('retailcrm_client_id', $client_id);
}
}
}
}
}