diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..2cd442d --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,28 @@ + + + + + tests/phpunit + + + + + ./include + retailcrm.php + uninstall.php + + ./include/api + ./include/class-wc-retailcrm-base.php + + + + \ No newline at end of file diff --git a/tests/bin/deploy.sh b/tests/bin/deploy.sh new file mode 100644 index 0000000..f1f641a --- /dev/null +++ b/tests/bin/deploy.sh @@ -0,0 +1 @@ +#!/usr/bin/env bash diff --git a/tests/bin/install.sh b/tests/bin/install.sh new file mode 100755 index 0000000..69379a7 --- /dev/null +++ b/tests/bin/install.sh @@ -0,0 +1,146 @@ +#!/usr/bin/env bash +# See https://raw.githubusercontent.com/wp-cli/scaffold-command/master/templates/install-wp-tests.sh + +if [ $# -lt 3 ]; then + echo "usage: $0 [db-host] [wp-version] [skip-database-creation]" + exit 1 +fi + +DB_NAME=$1 +DB_USER=$2 +DB_PASS=$3 +DB_HOST=${4-localhost} +WP_VERSION=${5-latest} +SKIP_DB_CREATE=${6-false} + +WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib} +WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/} + +download() { + if [ `which curl` ]; then + curl -s "$1" > "$2"; + elif [ `which wget` ]; then + wget -nv -O "$2" "$1" + fi +} + +if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then + WP_TESTS_TAG="tags/$WP_VERSION" +elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then + WP_TESTS_TAG="trunk" +else + # http serves a single offer, whereas https serves multiple. we only want one + download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json + grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json + LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//') + if [[ -z "$LATEST_VERSION" ]]; then + echo "Latest WordPress version could not be found" + exit 1 + fi + WP_TESTS_TAG="tags/$LATEST_VERSION" +fi + +set -ex + +install_wp() { + + if [ -d $WP_CORE_DIR ]; then + return; + fi + + mkdir -p $WP_CORE_DIR + + if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then + mkdir -p /tmp/wordpress-nightly + download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip + unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/ + mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR + else + if [ $WP_VERSION == 'latest' ]; then + local ARCHIVE_NAME='latest' + else + local ARCHIVE_NAME="wordpress-$WP_VERSION" + fi + download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz + tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR + fi + + download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php +} + +install_woocommerce() { + cd $TRAVIS_BUILD_DIR + cd .. + git clone https://github.com/woocommerce/woocommerce.git + cd woocommerce + git checkout $WC_VERSION + cd - +} + +install_test_suite() { + # portable in-place argument for both GNU sed and Mac OSX sed + if [[ $(uname -s) == 'Darwin' ]]; then + local ioption='-i .bak' + else + local ioption='-i' + fi + + # set up testing suite if it doesn't yet exist + if [ ! -d $WP_TESTS_DIR ]; then + # set up testing suite + mkdir -p $WP_TESTS_DIR + svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes + svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data + fi + + if [ ! -f wp-tests-config.php ]; then + download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php + # remove all forward slashes in the end + WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::") + sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php + fi + +} + +install_db() { + + if [ ${SKIP_DB_CREATE} = "true" ]; then + return 0 + fi + + # parse DB_HOST for port or socket references + local PARTS=(${DB_HOST//\:/ }) + local DB_HOSTNAME=${PARTS[0]}; + local DB_SOCK_OR_PORT=${PARTS[1]}; + local EXTRA="" + + if ! [ -z $DB_HOSTNAME ] ; then + if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then + EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp" + elif ! [ -z $DB_SOCK_OR_PORT ] ; then + EXTRA=" --socket=$DB_SOCK_OR_PORT" + elif ! [ -z $DB_HOSTNAME ] ; then + EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" + fi + fi + + # create database + mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA +} + +#install_wc() { +# +# mkdir -p /tmp/wordpress/wp-content/plugins/woocommerce +# svn co --quiet https://plugins.svn.wordpress.org/woocommerce/trunk/ /tmp/wordpress/wp-content/plugins/woocommerce +# +#} + +install_wp +install_test_suite +install_woocommerce +install_db +#install_wc diff --git a/tests/phpunit/bootstrap.php b/tests/phpunit/bootstrap.php new file mode 100644 index 0000000..61766c8 --- /dev/null +++ b/tests/phpunit/bootstrap.php @@ -0,0 +1,34 @@ +apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') + ->disableOriginalConstructor() + ->getMock(); + + $this->unit = new \WC_Retailcrm_Base(); + $this->unit->apiClient = $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); + $this->assertArrayHasKey('api_url', $this->unit->form_fields); + $this->assertArrayHasKey('api_key', $this->unit->form_fields); + $this->assertArrayHasKey('api_version', $this->unit->form_fields); + } +} diff --git a/tests/phpunit/test-wc-retailcrm-customers.php b/tests/phpunit/test-wc-retailcrm-customers.php new file mode 100644 index 0000000..b951751 --- /dev/null +++ b/tests/phpunit/test-wc-retailcrm-customers.php @@ -0,0 +1,82 @@ +responseMock = $this->getMockBuilder('\WC_Retailcrm_Response') + ->disableOriginalConstructor() + ->setMethods(array( + 'isSuccessful' + )) + ->getMock(); + + $this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') + ->disableOriginalConstructor() + ->setMethods(array( + 'ordersGet', + 'ordersUpload', + 'ordersCreate', + 'ordersEdit', + 'customersGet', + 'customersUpload', + 'customersCreate', + 'customersEdit' + )) + ->getMock(); + + $this->customer = new WC_Customer(); + $this->customer->set_email(uniqid(md5(date('Y-m-d H:i:s'))) . '@mail.com'); + $this->customer->set_password('password'); + $this->customer->set_role('customer'); + $this->customer->save(); + } + + /** + * @param retailcrm + * @dataProvider dataProviderApiClient + */ + public function test_customers_upload($retailcrm) + { + $retailcrm_customer = new WC_Retailcrm_Customers($retailcrm); + $retailcrm_customer->customersUpload(); + } + + /** + * @param $retailcrm + * @dataProvider dataProviderApiClient + */ + public function test_create_customer($retailcrm) + { + $retailcrm_customer = new WC_Retailcrm_Customers($retailcrm); + $retailcrm_customer->createCustomer($this->customer->get_id()); + } + + /** + * @param $retailcrm + * @dataProvider dataProviderApiClient + */ + public function test_update_customer($retailcrm) + { + $retailcrm_customer = new WC_Retailcrm_Customers($retailcrm); + $retailcrm_customer->updateCustomer($this->customer->get_id()); + } + + public function dataProviderApiClient() + { + $this->setUp(); + + return array( + array( + 'retailcrm' => $this->apiMock + ), + array( + 'retailcrm' => false + ) + ); + } +} \ No newline at end of file diff --git a/tests/phpunit/test-wc-retailcrm-inventories.php b/tests/phpunit/test-wc-retailcrm-inventories.php new file mode 100644 index 0000000..8d62588 --- /dev/null +++ b/tests/phpunit/test-wc-retailcrm-inventories.php @@ -0,0 +1,79 @@ +offer = new WC_Product_Simple(); + $this->offer->save(); + + $this->responseMock = $this->getMockBuilder('\WC_Retailcrm_Response') + ->disableOriginalConstructor() + ->setMethods(array( + 'isSuccessful' + )) + ->getMock(); + + $this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') + ->disableOriginalConstructor() + ->setMethods(array( + 'storeInventories' + )) + ->getMock(); + + $this->apiMock->expects($this->any()) + ->method('storeInventories') + ->willReturn($this->getTestData()); + + parent::setUp(); + } + + /** + * @param $retailcrm + * @dataProvider dataProviderLoadStocks + */ + public function test_load_stocks($retailcrm) + { + $retailcrm_inventories = new WC_Retailcrm_Inventories($retailcrm); + $retailcrm_inventories->load_stocks(); + } + + private function getTestData() + { + return array( + 'success' => true, + 'pagination' => array( + 'limit' => 250, + 'totalCount' => 1, + 'currentPage' => 1, + 'totalPageCount' => 1 + ), + 'offers' => array( + array( + 'id' => 1, + 'externalId' => $this->offer->get_id(), + 'xmlId' => 'xmlId', + 'quantity' => 1 + ) + ) + ); + } + + public function dataProviderLoadStocks() + { + $this->setUp(); + + return array( + array( + 'retailcrm' => $this->apiMock + ), + array( + 'retailcrm' => false + ) + ); + } +} \ No newline at end of file diff --git a/tests/phpunit/test-wc-retailcrm-orders.php b/tests/phpunit/test-wc-retailcrm-orders.php new file mode 100644 index 0000000..8f91f6d --- /dev/null +++ b/tests/phpunit/test-wc-retailcrm-orders.php @@ -0,0 +1,131 @@ +responseMock = $this->getMockBuilder('\WC_Retailcrm_Response') + ->disableOriginalConstructor() + ->setMethods(array( + 'isSuccessful' + )) + ->getMock(); + + $this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') + ->disableOriginalConstructor() + ->setMethods(array( + 'ordersGet', + 'ordersUpload', + 'ordersCreate', + 'ordersEdit', + 'customersGet', + 'customersCreate' + )) + ->getMock(); + + $this->apiMock->expects($this->any()) + ->method('ordersEdit') + ->willReturn($this->responseMock); + + $this->order = new WC_Order(); + $this->order->save(); + parent::setUp(); + } + + /** + * @param $retailcrm + * @dataProvider dataProviderRetailcrm + */ + public function test_order_upload($retailcrm) + { + $retailcrm_orders = new WC_Retailcrm_Orders($retailcrm); + $retailcrm_orders->ordersUpload(); + } + + /** + * @param $retailcrm + * @dataProvider dataProviderRetailcrm + */ + public function test_order_create($retailcrm) + { + $retailcrm_orders = new WC_Retailcrm_Orders($retailcrm); + $retailcrm_orders->orderCreate($this->order->get_id()); + } + + /** + * @param $retailcrm + * @dataProvider dataProviderRetailcrm + */ + public function test_order_update_status($retailcrm) + { + $retailcrm_orders = new WC_Retailcrm_Orders($retailcrm); + $retailcrm_orders->orderUpdateStatus($this->order->get_id()); + } + + /** + * @param $retailcrm + * @dataProvider dataProviderRetailcrm + */ + public function test_order_update_payment($retailcrm) + { + $retailcrm_orders = new WC_Retailcrm_Orders($retailcrm); + $retailcrm_orders->orderUpdatePayment($this->order->get_id()); + } + + /** + * @param $isSuccessful + * @param $retailcrm + * @dataProvider dataProviderUpdateOrder + */ + public function test_update_order($isSuccessful, $retailcrm) + { + $this->responseMock->expects($this->any()) + ->method('isSuccessful') + ->willReturn($isSuccessful); + + $retailcrm_orders = new WC_Retailcrm_Orders($retailcrm); + $retailcrm_orders->updateOrder($this->order->get_id()); + } + + public function dataProviderUpdateOrder() + { + $this->setUp(); + + return array( + array( + 'is_successful' => true, + 'retailcrm' => $this->apiMock + ), + array( + 'is_successful' => false, + 'retailcrm' => $this->apiMock + ), + array( + 'is_successful' => true, + 'retailcrm' => false + ), + array( + 'is_successful' => false, + 'retailcrm' => false + ) + ); + } + + public function dataProviderRetailcrm() + { + $this->setUp(); + + return array( + array( + 'retailcrm' => $this->apiMock + ), + array( + 'retailcrm' => false + ) + ); + } +} diff --git a/woo-retailcrm/include/class-wc-retailcrm-base.php b/woo-retailcrm/include/class-wc-retailcrm-base.php index 4b47186..11ee216 100644 --- a/woo-retailcrm/include/class-wc-retailcrm-base.php +++ b/woo-retailcrm/include/class-wc-retailcrm-base.php @@ -7,521 +7,789 @@ * @author RetailCRM */ -if ( ! class_exists( 'WC_Retailcrm_Base' ) ) : +if (!class_exists('WC_Retailcrm_Base')) { /** * Class WC_Retailcrm_Base */ class WC_Retailcrm_Base extends WC_Integration { - protected $api_url; - protected $api_key; + public static $option_key; - /** - * Init and hook in the integration. - */ - public function __construct() { - //global $woocommerce; + public $apiClient; + protected $api_url; + protected $api_key; - if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) { - include_once( __DIR__ . '/api/class-wc-retailcrm-proxy.php' ); + /** + * Init and hook in the integration. + */ + public function __construct() { + //global $woocommerce; + + if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) { + include_once( __DIR__ . '/api/class-wc-retailcrm-proxy.php' ); + } + + $this->id = 'integration-retailcrm'; + $this->method_title = __('RetailCRM', 'retailcrm'); + $this->method_description = __('Integration with eComlogic managament system.', 'retailcrm'); + + $this->apiClient = $this->getApiClient(); + self::$option_key = $this->get_option_key(); + // Load the settings. + $this->init_form_fields(); + $this->init_settings(); + + // Actions. + add_action('woocommerce_update_options_integration_' . $this->id, array($this, 'process_admin_options')); + + add_filter('cron_schedules', array($this, 'filter_cron_schedules'), 10, 1); + 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('init', array($this, 'register_load_inventories')); + add_action('init', array($this, 'register_icml_generation')); + add_action('init', array($this, 'register_retailcrm_history')); + add_action('wp_ajax_do_upload', array($this, 'upload_to_crm')); + add_action('wp_ajax_generate_icml', array($this, 'generate_icml')); + add_action('admin_print_footer_scripts', array($this, 'ajax_upload'), 99); + add_action('admin_print_footer_scripts', array($this, 'ajax_generate_icml'), 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('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); } - $this->id = 'integration-retailcrm'; - $this->method_title = __('RetailCRM', 'retailcrm'); - $this->method_description = __('Integration with eComlogic managament system.', 'retailcrm'); + /** + * 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'; + } - // Load the settings. + return 'class-wc-retailcrm-' . $file . '.php'; + } - $this->init_form_fields(); - $this->init_settings(); - - // Actions. - add_action( 'woocommerce_update_options_integration_' . $this->id, array( $this, 'process_admin_options' ) ); - } - - /** - * Initialize integration settings form fields. - */ - public function init_form_fields() { - - $this->form_fields = array( - array( 'title' => __( 'General Options', 'retailcrm' ), 'type' => 'title', 'desc' => '', 'id' => 'general_options' ), - - 'api_url' => array( - 'title' => __( 'API URL', 'retailcrm' ), - 'type' => 'text', - 'description' => __( 'Enter with your API URL (https://yourdomain.ecomlogic.com).', 'retailcrm' ), - 'desc_tip' => true, - 'default' => '' - ), - 'api_key' => array( - 'title' => __( 'API Key', 'retailcrm' ), - 'type' => 'text', - 'description' => __( 'Enter with your API Key. You can find this in eComlogic admin interface.', 'retailcrm' ), - 'desc_tip' => true, - 'default' => '' - ) - ); - - $api_version_list = array('v4' => 'v4','v5' => 'v5'); - - $this->form_fields[] = array( - 'title' => __( 'API settings', 'retailcrm' ), - 'type' => 'title', - 'description' => '', - 'id' => 'api_options' - ); - - $this->form_fields['api_version'] = array( - 'title' => __( 'API version', 'retailcrm' ), - 'description' => __( 'Select the API version you want to use', 'retailcrm' ), - 'css' => 'min-width:50px;', - 'class' => 'select', - 'type' => 'select', - 'options' => $api_version_list, - 'desc_tip' => true, - ); - - $this->form_fields[] = array( - 'title' => __( 'Catalog settings', 'retailcrm' ), - 'type' => 'title', - 'description' => '', - 'id' => 'catalog_options' - ); - - foreach (get_post_statuses() as $status_key => $status_value) { - $this->form_fields['p_' . $status_key] = array( - 'title' => $status_value, - 'label' => ' ', - 'description' => '', - 'class' => 'checkbox', - 'type' => 'checkbox', - 'desc_tip' => true, + public function filter_cron_schedules($schedules) { + return array_merge( + $schedules, + array( + 'five_minutes' => array( + 'interval' => 300, // seconds + 'display' => __('Every 5 minutes') + ), + 'three_hours' => array( + 'interval' => 10800, // seconds + 'display' => __('Every 3 hours') + ), + 'fiveteen_minutes' => array( + 'interval' => 900, // seconds + 'display' => __('Every 15 minutes') + ) + ) ); } - if ($this->get_option( 'api_url' ) != '' && $this->get_option( 'api_key' ) != '') { - if (isset($_GET['page']) && $_GET['page'] == 'wc-settings' && isset($_GET['tab']) && $_GET['tab'] == 'integration') { - add_action('admin_print_footer_scripts', array($this, 'show_blocks'), 99); + public function generate_icml() { + if (!class_exists('WC_Retailcrm_Icml')) { + require_once (self::checkCustomFile('icml')); + } - $retailcrm = new WC_Retailcrm_Proxy( - $this->get_option( 'api_url' ), - $this->get_option( 'api_key' ), - $this->get_option( 'api_version') - ); + $retailcrm_icml = new WC_Retailcrm_Icml(); + $retailcrm_icml->generate(); + } - /** - * Order methods options - */ - $order_methods_option = array(); - $order_methods_list = $retailcrm->orderMethodsList(); + /** + * Get history + */ + public function retailcrm_history_get() { + if (!class_exists('WC_Retailcrm_History')) { + include_once(self::checkCustomFile('history')); + } - if ($order_methods_list->isSuccessful()) { - foreach ($order_methods_list['orderMethods'] as $order_method) { - if ($order_method['active'] == false) { - continue; - } + $retailcrm_history = new WC_Retailcrm_History($this->apiClient); + $retailcrm_history->getHistory(); + } - $order_methods_option[$order_method['code']] = $order_method['name']; - } + /** + * @param int $order_id + */ + public function retailcrm_process_order($order_id) { + if (!class_exists('WC_Retailcrm_Orders')) { + include_once(self::checkCustomFile('orders')); + } - $this->form_fields[] = array( - 'title' => __('Order methods', 'retailcrm'), - 'type' => 'heading', - 'description' => '', - 'id' => 'order_methods_options' - ); + $retailcm_order = new WC_Retailcrm_Orders($this->apiClient); + $retailcm_order->orderCreate($order_id); + } - $this->form_fields['order_methods'] = array( - 'label' => ' ', - 'title' => __('Ordering methods available for downloading from eComlogic', 'retailcrm'), - 'class' => '', - 'type' => 'multiselect', - 'description' => __('Select the order methods that will be uploaded from eComlogic to site', 'retailcrm'), - 'options' => $order_methods_option, - 'select_buttons' => true - ); - } - - /** - * Shipping options - */ - $shipping_option_list = array(); - $retailcrm_shipping_list = $retailcrm->deliveryTypesList(); + /** + * Load stock from retailCRM + */ + public function load_stocks() { + if (!class_exists('WC_Retailcrm_Inventories')) { + include_once(self::checkCustomFile('inventories')); + } - if ($retailcrm_shipping_list->isSuccessful()) { - foreach ($retailcrm_shipping_list['deliveryTypes'] as $retailcrm_shipping_type) { - $shipping_option_list[$retailcrm_shipping_type['code']] = $retailcrm_shipping_type['name']; - } + $inventories = new WC_Retailcrm_Inventories($this->apiClient); + $inventories->updateQuantity(); + } - $wc_shipping_list = get_wc_shipping_methods(); - - $this->form_fields[] = array( - 'title' => __('Shipping methods', 'retailcrm'), - 'type' => 'heading', - 'description' => '', - 'id' => 'shipping_options' - ); - - foreach ( $wc_shipping_list as $shipping_code => $shipping ) { - if ( isset( $shipping['enabled'] ) && $shipping['enabled'] == 'yes' ) { - $this->form_fields[$shipping_code] = array( - 'title' => __($shipping['title'], 'woocommerce'), - 'description' => __($shipping['description'], 'woocommerce'), - 'css' => 'min-width:350px;', - 'class' => 'select', - 'type' => 'select', - 'options' => $shipping_option_list, - 'desc_tip' => true, - ); - } - } - } - - /** - * Payment options - */ - $payment_option_list = array(); - $retailcrm_payment_list = $retailcrm->paymentTypesList(); - - if ($retailcrm_payment_list->isSuccessful()) { - foreach ($retailcrm_payment_list['paymentTypes'] as $retailcrm_payment_type) { - $payment_option_list[$retailcrm_payment_type['code']] = $retailcrm_payment_type['name']; - } - - $wc_payment = new WC_Payment_Gateways(); - - $this->form_fields[] = array( - 'title' => __('Payment methods', 'retailcrm'), - 'type' => 'heading', - 'description' => '', - 'id' => 'payment_options' - ); - - foreach ( $wc_payment->payment_gateways as $payment ) { - if ( isset( $payment->enabled ) && $payment->enabled == 'yes' ) { - $key = $payment->id; - $name = $key; - $this->form_fields[$name] = array( - 'title' => __($payment->method_title, 'woocommerce'), - 'description' => __($payment->method_description, 'woocommerce'), - 'css' => 'min-width:350px;', - 'class' => 'select', - 'type' => 'select', - 'options' => $payment_option_list, - 'desc_tip' => true, - ); - } - } - } - - /** - * Statuses options - */ - $statuses_option_list = array(); - $retailcrm_statuses_list = $retailcrm->statusesList(); - - if ($retailcrm_statuses_list->isSuccessful()) { - foreach ($retailcrm_statuses_list['statuses'] as $retailcrm_status) { - $statuses_option_list[$retailcrm_status['code']] = $retailcrm_status['name']; - } - - $wc_statuses = wc_get_order_statuses(); - - $this->form_fields[] = array( - 'title' => __('Statuses', 'retailcrm'), - 'type' => 'heading', - 'description' => '', - 'id' => 'statuses_options' - ); - - foreach ( $wc_statuses as $idx => $name ) { - $uid = str_replace('wc-', '', $idx); - $this->form_fields[$uid] = array( - 'title' => __($name, 'woocommerce'), - 'css' => 'min-width:350px;', - 'class' => 'select', - 'type' => 'select', - 'options' => $statuses_option_list, - 'desc_tip' => true, - ); - } - } - - /** - * Inventories options - */ - $this->form_fields[] = array( - 'title' => __('Inventories settings', 'retailcrm'), - 'type' => 'heading', - 'description' => '', - 'id' => 'invent_options' - ); - - $this->form_fields['sync'] = array( - 'label' => __('Sync inventories', 'retailcrm'), - 'title' => __('Inventories', 'retailcrm'), - 'class' => 'checkbox', - 'type' => 'checkbox', - 'description' => __('Check this checkbox if you want to unload the rest of the products from CRM to site.', 'retailcrm') - ); - - /** - * UA options - */ - $this->form_fields[] = array( - 'title' => __('UA settings', 'retailcrm'), - 'type' => 'heading', - 'description' => '', - 'id' => 'invent_options' - ); - - $this->form_fields['ua'] = array( - 'label' => __('Activate UA', 'retailcrm'), - 'title' => __('UA', 'retailcrm'), - 'class' => 'checkbox', - 'type' => 'checkbox', - 'description' => __('Check this checkbox if you want to unload information to UA.', 'retailcrm') - ); - - $this->form_fields['ua_code'] = array( - 'title' => __('UA code', 'retailcrm'), - 'class' => 'input', - 'type' => 'input' - ); - - $this->form_fields['ua_custom'] = array( - 'title' => __('Custom parameter', 'retailcrm'), - 'class' => 'input', - 'type' => 'input' - ); - - /** - * Uploads options - */ - $options = array_filter(get_option( 'woocommerce_integration-retailcrm_settings' )); - - if (!isset($options['uploads'])) { - $this->form_fields[] = array( - 'title' => __('Uploads settings', 'retailcrm'), - 'type' => 'heading', - 'description' => '', - 'id' => 'upload_options' - ); - - $this->form_fields['upload-button'] = array( - 'label' => __('Upload', 'retailcrm'), - 'title' => __('Upload all customers and orders', 'retailcrm' ), - 'type' => 'button', - 'description' => __('Batch unloading of existing customers and orders.', 'retailcrm' ), - 'desc_tip' => true, - 'id' => 'uploads-retailcrm' - ); - } - - /* - * Generate icml file - */ - $this->form_fields[] = array( - 'title' => __( 'Generate ICML catalog', 'retailcrm' ), - 'type' => 'title', - 'description' => '', - 'id' => 'icml_options' - ); - - $this->form_fields[] = array( - 'label' => __('Generate', 'retailcrm'), - 'title' => __('Generate ICML', 'retailcrm'), - 'type' => 'button', - 'description' => __('This functionality allows you to generate a catalog of products for downloading to CRM.', 'retailcrm'), - 'desc_tip' => true, - 'id' => 'icml-retailcrm' - ); + public function register_load_inventories() { + if ( !wp_next_scheduled( 'retailcrm_inventories' ) ) { + // Schedule the event + wp_schedule_event( time(), 'fiveteen_minutes', 'retailcrm_inventories' ); } } - } - /** - * Generate html button - * - * @param string $key - * @param array $data - * - * @return string - */ - public function generate_button_html( $key, $data ) { - $field = $this->plugin_id . $this->id . '_' . $key; - $defaults = array( - 'class' => 'button-secondary', - 'css' => '', - 'custom_attributes' => array(), - 'desc_tip' => false, - 'description' => '', - 'title' => '', - ); + public function register_icml_generation() { + // Make sure this event hasn't been scheduled + if ( !wp_next_scheduled( 'retailcrm_icml' ) ) { + // Schedule the event + wp_schedule_event( time(), 'three_hours', 'retailcrm_icml' ); + } + } - $data = wp_parse_args( $data, $defaults ); + public function register_retailcrm_history() { + // Make sure this event hasn't been scheduled + if ( !wp_next_scheduled( 'retailcrm_history' ) ) { + // Schedule the event + wp_schedule_event( time(), 'five_minutes', 'retailcrm_history' ); + } + } - ob_start(); - ?> - - - - get_tooltip_html( $data ); ?> - - -
- - - get_description_html( $data ); ?> -
- - - get_field_key( $key ); - $defaults = array( - 'title' => '', - 'class' => '', - ); + if (!class_exists('WC_Retailcrm_Customers')) { + include_once(self::checkCustomFile('customers')); + } - $data = wp_parse_args( $data, $defaults ); + $options = array_filter(get_option(self::$option_key)); - ob_start(); - ?> - -

- -

- - - apiClient); + $retailcrm_orders = new WC_Retailcrm_Orders($this->apiClient); - return ob_get_clean(); - } + $retailcrm_customers->customersUpload(); + $retailcrm_orders->ordersUpload(); - /** - * Validate API version - * - * @param string $key - * @param string $value - * - * @return string - */ - public function validate_api_version_field( $key, $value ) { - $post = $this->get_post_data(); + $options['uploads'] = 'yes'; + update_option('woocommerce_integration-retailcrm_settings', $options); + } - $versionMap = array( - 'v4' => '4.0', - 'v5' => '5.0' - ); - - $api = new WC_Retailcrm_Proxy( - $post[$this->plugin_id . $this->id . '_api_url'], - $post[$this->plugin_id . $this->id . '_api_key'] - ); + public function ajax_upload() { + $ajax_url = admin_url('admin-ajax.php'); + ?> + + apiVersions(); + public function ajax_generate_icml() { + $ajax_url = admin_url('admin-ajax.php'); + ?> + + isSuccessful()) { - if (!in_array($versionMap[$value], $response['versions'])) { - WC_Admin_Settings::add_error( esc_html__( 'The selected version of the API is unavailable', 'retailcrm' ) ); + /** + * Create customer in retailCRM + * @param int $customer_id + */ + public function create_customer($customer_id) { + if (!class_exists( 'WC_Retailcrm_Customers')) { + include_once(self::checkCustomFile('customers')); + } + + $retailcrm_customer = new WC_Retailcrm_Customers($this->apiClient); + $retailcrm_customer->createCustomer($customer_id); + } + + /** + * Edit customer in retailCRM + * @param int $customer_id + */ + public function update_customer($customer_id) { + if (!class_exists('WC_Retailcrm_Customers')) { + include_once(self::checkCustomFile('customers')); + } + + $retailcrm_customer = new WC_Retailcrm_Customers($this->apiClient); + $retailcrm_customer->updateCustomer($customer_id); + } + + /** + * Edit order in retailCRM + * @param int $order_id + */ + public function update_order($order_id) { + if (!class_exists('WC_Retailcrm_Orders')) { + include_once(self::checkCustomFile('orders')); + } + + $retailcrm_order = new WC_Retailcrm_Orders($this->apiClient); + $retailcrm_order->updateOrder($order_id); + } + + /** + * Init google analytics code + */ + public function initialize_analytics() { + if (!class_exists('WC_Retailcrm_Google_Analytics')) { + include_once(self::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')) { + include_once(self::checkCustomFile('ga')); + } + + if ($this->get_option('ua') && $this->get_option('ua_code')) { + $retailcrm_analytics = WC_Retailcrm_Google_Analytics::getInstance($this->settings); + echo $retailcrm_analytics->send_analytics(); + } else { + echo ''; + } + } + + /** + * Initialize integration settings form fields. + */ + public function init_form_fields() { + + $this->form_fields = array( + array( 'title' => __( 'General Options', 'retailcrm' ), 'type' => 'title', 'desc' => '', 'id' => 'general_options' ), + + 'api_url' => array( + 'title' => __( 'API URL', 'retailcrm' ), + 'type' => 'text', + 'description' => __( 'Enter with your API URL (https://yourdomain.ecomlogic.com).', 'retailcrm' ), + 'desc_tip' => true, + 'default' => '' + ), + 'api_key' => array( + 'title' => __( 'API Key', 'retailcrm' ), + 'type' => 'text', + 'description' => __( 'Enter with your API Key. You can find this in eComlogic admin interface.', 'retailcrm' ), + 'desc_tip' => true, + 'default' => '' + ) + ); + + $api_version_list = array( + 'v4' => 'v4', + 'v5' => 'v5' + ); + + $this->form_fields[] = array( + 'title' => __( 'API settings', 'retailcrm' ), + 'type' => 'title', + 'description' => '', + 'id' => 'api_options' + ); + + $this->form_fields['api_version'] = array( + 'title' => __( 'API version', 'retailcrm' ), + 'description' => __( 'Select the API version you want to use', 'retailcrm' ), + 'css' => 'min-width:50px;', + 'class' => 'select', + 'type' => 'select', + 'options' => $api_version_list, + 'desc_tip' => true, + ); + + $this->form_fields[] = array( + 'title' => __( 'Catalog settings', 'retailcrm' ), + 'type' => 'title', + 'description' => '', + 'id' => 'catalog_options' + ); + + foreach (get_post_statuses() as $status_key => $status_value) { + $this->form_fields['p_' . $status_key] = array( + 'title' => $status_value, + 'label' => ' ', + 'description' => '', + 'class' => 'checkbox', + 'type' => 'checkbox', + 'desc_tip' => true, + ); + } + + if ($this->apiClient) { + if (isset($_GET['page']) && $_GET['page'] == 'wc-settings' + && isset($_GET['tab']) && $_GET['tab'] == 'integration' + ) { + add_action('admin_print_footer_scripts', array($this, 'show_blocks'), 99); + + /** + * Order methods options + */ + $order_methods_option = array(); + $order_methods_list = $this->apiClient->orderMethodsList(); + + if ($order_methods_list->isSuccessful()) { + foreach ($order_methods_list['orderMethods'] as $order_method) { + if ($order_method['active'] == false) { + continue; + } + + $order_methods_option[$order_method['code']] = $order_method['name']; + } + + $this->form_fields[] = array( + 'title' => __('Order methods', 'retailcrm'), + 'type' => 'heading', + 'description' => '', + 'id' => 'order_methods_options' + ); + + $this->form_fields['order_methods'] = array( + 'label' => ' ', + 'title' => __('Ordering methods available for downloading from eComlogic', 'retailcrm'), + 'class' => '', + 'type' => 'multiselect', + 'description' => __('Select the order methods that will be uploaded from eComlogic to site', 'retailcrm'), + 'options' => $order_methods_option, + 'select_buttons' => true + ); + } + + /** + * Shipping options + */ + $shipping_option_list = array(); + $retailcrm_shipping_list = $this->apiClient->deliveryTypesList(); + + if ($retailcrm_shipping_list->isSuccessful()) { + foreach ($retailcrm_shipping_list['deliveryTypes'] as $retailcrm_shipping_type) { + $shipping_option_list[$retailcrm_shipping_type['code']] = $retailcrm_shipping_type['name']; + } + + $wc_shipping_list = get_wc_shipping_methods(); + + $this->form_fields[] = array( + 'title' => __('Shipping methods', 'retailcrm'), + 'type' => 'heading', + 'description' => '', + 'id' => 'shipping_options' + ); + + foreach ( $wc_shipping_list as $shipping_code => $shipping ) { + if ( isset( $shipping['enabled'] ) && $shipping['enabled'] == 'yes' ) { + $this->form_fields[$shipping_code] = array( + 'title' => __($shipping['title'], 'woocommerce'), + 'description' => __($shipping['description'], 'woocommerce'), + 'css' => 'min-width:350px;', + 'class' => 'select', + 'type' => 'select', + 'options' => $shipping_option_list, + 'desc_tip' => true, + ); + } + } + } + + /** + * Payment options + */ + $payment_option_list = array(); + $retailcrm_payment_list = $this->apiClient->paymentTypesList(); + + if ($retailcrm_payment_list->isSuccessful()) { + foreach ($retailcrm_payment_list['paymentTypes'] as $retailcrm_payment_type) { + $payment_option_list[$retailcrm_payment_type['code']] = $retailcrm_payment_type['name']; + } + + $wc_payment = new WC_Payment_Gateways(); + + $this->form_fields[] = array( + 'title' => __('Payment methods', 'retailcrm'), + 'type' => 'heading', + 'description' => '', + 'id' => 'payment_options' + ); + + foreach ( $wc_payment->payment_gateways as $payment ) { + if ( isset( $payment->enabled ) && $payment->enabled == 'yes' ) { + $key = $payment->id; + $name = $key; + $this->form_fields[$name] = array( + 'title' => __($payment->method_title, 'woocommerce'), + 'description' => __($payment->method_description, 'woocommerce'), + 'css' => 'min-width:350px;', + 'class' => 'select', + 'type' => 'select', + 'options' => $payment_option_list, + 'desc_tip' => true, + ); + } + } + } + + /** + * Statuses options + */ + $statuses_option_list = array(); + $retailcrm_statuses_list = $this->apiClient->statusesList(); + + if ($retailcrm_statuses_list->isSuccessful()) { + foreach ($retailcrm_statuses_list['statuses'] as $retailcrm_status) { + $statuses_option_list[$retailcrm_status['code']] = $retailcrm_status['name']; + } + + $wc_statuses = wc_get_order_statuses(); + + $this->form_fields[] = array( + 'title' => __('Statuses', 'retailcrm'), + 'type' => 'heading', + 'description' => '', + 'id' => 'statuses_options' + ); + + foreach ( $wc_statuses as $idx => $name ) { + $uid = str_replace('wc-', '', $idx); + $this->form_fields[$uid] = array( + 'title' => __($name, 'woocommerce'), + 'css' => 'min-width:350px;', + 'class' => 'select', + 'type' => 'select', + 'options' => $statuses_option_list, + 'desc_tip' => true, + ); + } + } + + /** + * Inventories options + */ + $this->form_fields[] = array( + 'title' => __('Inventories settings', 'retailcrm'), + 'type' => 'heading', + 'description' => '', + 'id' => 'invent_options' + ); + + $this->form_fields['sync'] = array( + 'label' => __('Sync inventories', 'retailcrm'), + 'title' => __('Inventories', 'retailcrm'), + 'class' => 'checkbox', + 'type' => 'checkbox', + 'description' => __('Check this checkbox if you want to unload the rest of the products from CRM to site.', 'retailcrm') + ); + + /** + * UA options + */ + $this->form_fields[] = array( + 'title' => __('UA settings', 'retailcrm'), + 'type' => 'heading', + 'description' => '', + 'id' => 'invent_options' + ); + + $this->form_fields['ua'] = array( + 'label' => __('Activate UA', 'retailcrm'), + 'title' => __('UA', 'retailcrm'), + 'class' => 'checkbox', + 'type' => 'checkbox', + 'description' => __('Check this checkbox if you want to unload information to UA.', 'retailcrm') + ); + + $this->form_fields['ua_code'] = array( + 'title' => __('UA code', 'retailcrm'), + 'class' => 'input', + 'type' => 'input' + ); + + $this->form_fields['ua_custom'] = array( + 'title' => __('Custom parameter', 'retailcrm'), + 'class' => 'input', + 'type' => 'input' + ); + + /** + * Uploads options + */ + $options = array_filter(get_option(self::$option_key)); + + if (!isset($options['uploads'])) { + $this->form_fields[] = array( + 'title' => __('Uploads settings', 'retailcrm'), + 'type' => 'heading', + 'description' => '', + 'id' => 'upload_options' + ); + + $this->form_fields['upload-button'] = array( + 'label' => __('Upload', 'retailcrm'), + 'title' => __('Upload all customers and orders', 'retailcrm' ), + 'type' => 'button', + 'description' => __('Batch unloading of existing customers and orders.', 'retailcrm' ), + 'desc_tip' => true, + 'id' => 'uploads-retailcrm' + ); + } + + /* + * Generate icml file + */ + $this->form_fields[] = array( + 'title' => __( 'Generate ICML catalog', 'retailcrm' ), + 'type' => 'title', + 'description' => '', + 'id' => 'icml_options' + ); + + $this->form_fields[] = array( + 'label' => __('Generate', 'retailcrm'), + 'title' => __('Generate ICML', 'retailcrm'), + 'type' => 'button', + 'description' => __('This functionality allows you to generate a catalog of products for downloading to CRM.', 'retailcrm'), + 'desc_tip' => true, + 'id' => 'icml-retailcrm' + ); + } + } + } + + /** + * Generate html button + * + * @param string $key + * @param array $data + * + * @return string + */ + public function generate_button_html( $key, $data ) { + $field = $this->plugin_id . $this->id . '_' . $key; + $defaults = array( + 'class' => 'button-secondary', + 'css' => '', + 'custom_attributes' => array(), + 'desc_tip' => false, + 'description' => '', + 'title' => '', + ); + + $data = wp_parse_args( $data, $defaults ); + + ob_start(); + ?> + + + + + get_field_key( $key ); + $defaults = array( + 'title' => '', + 'class' => '', + ); + + $data = wp_parse_args( $data, $defaults ); + + ob_start(); + ?> + +

+ +

+ + + get_post_data(); + + $versionMap = array( + 'v4' => '4.0', + 'v5' => '5.0' + ); + + $api = new WC_Retailcrm_Proxy( + $post[$this->plugin_id . $this->id . '_api_url'], + $post[$this->plugin_id . $this->id . '_api_key'] + ); + + $response = $api->apiVersions(); + + if ($response && $response->isSuccessful()) { + if (!in_array($versionMap[$value], $response['versions'])) { + WC_Admin_Settings::add_error( esc_html__( 'The selected version of the API is unavailable', 'retailcrm' ) ); + $value = ''; + } + + return $value; + } + } + + /** + * Validate API url + * + * @param string $key + * @param string $value + * + * @return string + */ + public function validate_api_url_field( $key, $value ) { + $post = $this->get_post_data(); + $api = new WC_Retailcrm_Proxy( + $value, + $post[$this->plugin_id . $this->id . '_api_key'] + ); + + $response = $api->apiVersions(); + + if ($response == NULL) { + WC_Admin_Settings::add_error( esc_html__( 'Enter the correct CRM address', 'retailcrm' ) ); $value = ''; } return $value; } - } - /** - * Validate API url - * - * @param string $key - * @param string $value - * - * @return string - */ - public function validate_api_url_field( $key, $value ) { - $post = $this->get_post_data(); - $api = new WC_Retailcrm_Proxy( - $value, - $post[$this->plugin_id . $this->id . '_api_key'] - ); - - $response = $api->apiVersions(); - - if ($response == NULL) { - WC_Admin_Settings::add_error( esc_html__( 'Enter the correct CRM address', 'retailcrm' ) ); - $value = ''; - } - - return $value; - } - - /** - * Validate API key - * - * @param string $key - * @param string $value - * - * @return string - */ - public function validate_api_key_field( $key, $value ) { - $post = $this->get_post_data(); - $api = new WC_Retailcrm_Proxy( - $post[$this->plugin_id . $this->id . '_api_url'], - $value - ); - - $response = $api->apiVersions(); - - if (!is_object($response)) { - $value = ''; - } - - if (!$response->isSuccessful()) { - WC_Admin_Settings::add_error( esc_html__( 'Enter the correct API key', 'retailcrm' ) ); - $value = ''; - } - - return $value; - } - - /** - * Scritp show|hide block settings - */ - function show_blocks() { - ?> - - apiVersions(); + + if (!is_object($response)) { + $value = ''; + } + + if (!$response->isSuccessful()) { + WC_Admin_Settings::add_error( esc_html__( 'Enter the correct API key', 'retailcrm' ) ); + $value = ''; + } + + return $value; + } + + /** + * Scritp show|hide block settings + */ + function show_blocks() { + ?> + + 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; + } } } - -endif; diff --git a/woo-retailcrm/include/class-wc-retailcrm-customers.php b/woo-retailcrm/include/class-wc-retailcrm-customers.php index 819252f..1526254 100644 --- a/woo-retailcrm/include/class-wc-retailcrm-customers.php +++ b/woo-retailcrm/include/class-wc-retailcrm-customers.php @@ -14,33 +14,37 @@ if ( ! class_exists( 'WC_Retailcrm_Customers' ) ) : */ class WC_Retailcrm_Customers { - public function __construct() + const CUSTOMER_ROLE = 'customer'; + + protected $retailcrm; + protected $retailcrm_settings; + + /** + * WC_Retailcrm_Customers constructor. + * @param $retailcrm + */ + public function __construct($retailcrm = false) { - $this->retailcrm_settings = get_option( 'woocommerce_integration-retailcrm_settings' ); - - if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) { - include_once( WP_PLUGIN_DIR . '/woo-retailcrm/include/api/class-wc-retailcrm-proxy.php' ); - } - - $this->retailcrm = new WC_Retailcrm_Proxy( - $this->retailcrm_settings['api_url'], - $this->retailcrm_settings['api_key'], - $this->retailcrm_settings['api_version'] - ); + $this->retailcrm_settings = get_option(WC_Retailcrm_Base::$option_key); + $this->retailcrm = $retailcrm; } /** * Upload customers to CRM - * + * * @return void */ public function customersUpload() { + if (!$this->retailcrm) { + return; + } + $users = get_users(); $data_customers = array(); foreach ($users as $user) { - if (!in_array('customer', $user->roles)) { + if (!in_array(self::CUSTOMER_ROLE, $user->roles)) { continue; } @@ -86,9 +90,13 @@ if ( ! class_exists( 'WC_Retailcrm_Customers' ) ) : */ public function createCustomer($customer_id) { + if (!$this->retailcrm) { + return; + } + $customer = new WC_Customer($customer_id); - if ($customer->get_role() == 'customer'){ + if ($customer->get_role() == self::CUSTOMER_ROLE) { $data_customer = $this->processCustomer($customer); $this->retailcrm->customersCreate($data_customer); @@ -104,9 +112,13 @@ if ( ! class_exists( 'WC_Retailcrm_Customers' ) ) : */ public function updateCustomer($customer_id) { + if (!$this->retailcrm) { + return; + } + $customer = new WC_Customer($customer_id); - if ($customer->get_role() == 'customer'){ + if ($customer->get_role() == self::CUSTOMER_ROLE){ $data_customer = $this->processCustomer($customer); $this->retailcrm->customersEdit($data_customer); @@ -116,7 +128,7 @@ if ( ! class_exists( 'WC_Retailcrm_Customers' ) ) : /** * Process customer * - * @param object $customer + * @param WC_Customer $customer * * @return array $data_customer */ @@ -145,7 +157,7 @@ if ( ! class_exists( 'WC_Retailcrm_Customers' ) ) : ); } - return $data_customer; + return apply_filters('retailcrm_process_customer', $data_customer); } } endif; diff --git a/woo-retailcrm/include/class-wc-retailcrm-ga.php b/woo-retailcrm/include/class-wc-retailcrm-ga.php new file mode 100644 index 0000000..80fcb15 --- /dev/null +++ b/woo-retailcrm/include/class-wc-retailcrm-ga.php @@ -0,0 +1,115 @@ +options = $options; + } + + /** + * @return string + */ + public function initialize_analytics() { + return " + + "; + } + + /** + * @return string + */ + public function send_analytics() { + $js = ''; + $order_id = wc_get_order_id_by_order_key($_GET['key']); + $order = wc_get_order($order_id); + + foreach ($order->get_items() as $item) { + $uid = ($item['variation_id'] > 0) ? $item['variation_id'] : $item['product_id']; + $_product = wc_get_product($uid); + + if ($_product) { + $order_item = array( + 'id' => $uid, + 'name' => $item['name'], + 'price' => (float)$_product->get_price(), + 'quantity' => $item['qty'], + ); + + $order_items[] = $order_item; + } + } + + $url = parse_url(get_site_url()); + $domain = $url['host']; + + $js .= " + + "; + } + + return $js; + } + } +} diff --git a/woo-retailcrm/include/class-wc-retailcrm-history.php b/woo-retailcrm/include/class-wc-retailcrm-history.php index aea417a..c7f8397 100644 --- a/woo-retailcrm/include/class-wc-retailcrm-history.php +++ b/woo-retailcrm/include/class-wc-retailcrm-history.php @@ -18,29 +18,23 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : protected $startDateCustomers; protected $startDate; protected $retailcrm_settings; + protected $retailcrm; protected $order_methods = array(); /** - * Constructor WC_Retailcrm_History + * WC_Retailcrm_History constructor. + * @param bool $retailcrm */ - public function __construct() + public function __construct($retailcrm = false) { - $this->retailcrm_settings = get_option( 'woocommerce_integration-retailcrm_settings' ); + $this->retailcrm_settings = get_option(WC_Retailcrm_Base::$option_key); if (isset($this->retailcrm_settings['order_methods'])) { $this->order_methods = $this->retailcrm_settings['order_methods']; unset($this->retailcrm_settings['order_methods']); } - if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) { - include_once( WP_PLUGIN_DIR . '/woo-retailcrm/include/api/class-wc-retailcrm-proxy.php' ); - } - - $this->retailcrm = new WC_Retailcrm_Proxy( - $this->retailcrm_settings['api_url'], - $this->retailcrm_settings['api_key'], - $this->retailcrm_settings['api_version'] - ); + $this->retailcrm = $retailcrm; $this->startDate = new DateTime(date('Y-m-d H:i:s', strtotime('-1 days', strtotime(date('Y-m-d H:i:s'))))); $this->startDateOrders = $this->startDate; diff --git a/woo-retailcrm/include/class-wc-retailcrm-icml.php b/woo-retailcrm/include/class-wc-retailcrm-icml.php index 10431cd..e361e71 100644 --- a/woo-retailcrm/include/class-wc-retailcrm-icml.php +++ b/woo-retailcrm/include/class-wc-retailcrm-icml.php @@ -544,7 +544,7 @@ if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) : * @return array */ private function checkPostStatuses() { - $options = get_option( 'woocommerce_integration-retailcrm_settings' ); + $options = get_option(WC_Retailcrm_Base::$option_key); $status_args = array(); foreach (get_post_statuses() as $key => $value) { diff --git a/woo-retailcrm/include/class-wc-retailcrm-inventories.php b/woo-retailcrm/include/class-wc-retailcrm-inventories.php index 6fe7cb2..aa192db 100644 --- a/woo-retailcrm/include/class-wc-retailcrm-inventories.php +++ b/woo-retailcrm/include/class-wc-retailcrm-inventories.php @@ -14,23 +14,25 @@ if ( ! class_exists( 'WC_Retailcrm_Inventories' ) ) : */ class WC_Retailcrm_Inventories { - public function __construct() + protected $retailcrm; + protected $retailcrm_settings; + + /** + * WC_Retailcrm_Inventories constructor. + * @param bool $retailcrm + */ + public function __construct($retailcrm = false) { - $this->retailcrm_settings = get_option( 'woocommerce_integration-retailcrm_settings' ); - - if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) { - include_once( WP_PLUGIN_DIR . '/woo-retailcrm/include/api/class-wc-retailcrm-proxy.php' ); - } - - $this->retailcrm = new WC_Retailcrm_Proxy( - $this->retailcrm_settings['api_url'], - $this->retailcrm_settings['api_key'], - $this->retailcrm_settings['api_version'] - ); + $this->retailcrm_settings = get_option(WC_Retailcrm_Base::$option_key); + $this->retailcrm = $retailcrm; } public function load_stocks() - { + { + if (!$this->retailcrm) { + return; + } + $page = 1; do { @@ -52,14 +54,13 @@ if ( ! class_exists( 'WC_Retailcrm_Inventories' ) ) : } } } - } while ($page <= $totalPageCount); } public function updateQuantity() - { - $options = array_filter(get_option( 'woocommerce_integration-retailcrm_settings' )); - + { + $options = array_filter(get_option(WC_Retailcrm_Base::$option_key)); + if ($options['sync'] == 'yes') { $this->load_stocks(); } else { diff --git a/woo-retailcrm/include/class-wc-retailcrm-orders.php b/woo-retailcrm/include/class-wc-retailcrm-orders.php index 9f66a60..06cb3fc 100644 --- a/woo-retailcrm/include/class-wc-retailcrm-orders.php +++ b/woo-retailcrm/include/class-wc-retailcrm-orders.php @@ -17,19 +17,10 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : protected $retailcrm_settings; protected $retailcrm; - public function __construct() + public function __construct($retailcrm = false) { - $this->retailcrm_settings = get_option( 'woocommerce_integration-retailcrm_settings' ); - - if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) { - include_once( WP_PLUGIN_DIR . '/woo-retailcrm/include/api/class-wc-retailcrm-proxy.php' ); - } - - $this->retailcrm = new WC_Retailcrm_Proxy( - $this->retailcrm_settings['api_url'], - $this->retailcrm_settings['api_key'], - $this->retailcrm_settings['api_version'] - ); + $this->retailcrm_settings = get_option(WC_Retailcrm_Base::$option_key); + $this->retailcrm = $retailcrm; } /** @@ -37,6 +28,10 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : */ public function ordersUpload() { + if (!$this->retailcrm) { + return; + } + $orders = get_posts(array( 'numberposts' => -1, 'post_type' => wc_get_order_types('view-orders'), @@ -61,7 +56,7 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $uploadOrders = array_chunk($orders_data, 50); foreach ($uploadOrders as $uploadOrder) { - $this->retailcrm->ordersUpload($uploadOrder); + $this->retailcrm->ordersUpload($uploadOrder); } } @@ -72,6 +67,10 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : */ public function orderCreate($order_id) { + if (!$this->retailcrm) { + return; + } + $order_data = $this->processOrder($order_id); $order = new WC_Order($order_id); @@ -97,23 +96,17 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $this->retailcrm->ordersCreate($order_data); } - /** - * Update shipping address - * - * @param $order_id, $address - */ - public function orderUpdateShippingAddress($order_id, $address) { - $address['externalId'] = $order_id; - - $this->retailcrm->ordersEdit($address); - } - /** * Update order status * * @param $order_id */ - public function orderUpdateStatus($order_id) { + public function orderUpdateStatus($order_id) + { + if (!$this->retailcrm) { + return; + } + $order = new WC_Order( $order_id ); $order_data = array( @@ -128,10 +121,10 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : * Update order payment type * * @param $order_id - * + * * @return null */ - public function orderUpdatePaymentType($order_id, $payment_method) { + protected function orderUpdatePaymentType($order_id, $payment_method) { if (!isset($this->retailcrm_settings[$payment_method])) { return; @@ -174,7 +167,11 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : * * @param $order_id */ - public function orderUpdatePayment($order_id) { + public function orderUpdatePayment($order_id) + { + if (!$this->retailcrm) { + return; + } if ($this->retailcrm_settings['api_version'] != 'v5') { $order_data = array( @@ -191,91 +188,26 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $this->retailcrm->ordersPaymentsEdit($payment); } - } /** - * Update order items - * - * @param $order_id, $data - */ - public function orderUpdateItems($order_id, $data) { - $order = new WC_Order( $order_id ); - - $order_data['externalId'] = $order_id; - $shipping_method = end($data['shipping_method']); - $shipping_cost = end($data['shipping_cost']); - $products = $order->get_items(); - $items = array(); - - foreach ($products as $order_item_id => $product) { - if ($product['variation_id'] > 0) { - $offer_id = $product['variation_id']; - } else { - $offer_id = $product['product_id']; - } - - $_product = wc_get_product($offer_id); - - if ($this->retailcrm_settings['api_version'] != 'v3') { - $items[] = array( - 'offer' => array('externalId' => $offer_id), - 'productName' => $product['name'], - 'initialPrice' => (float)$_product->get_price(), - 'quantity' => $product['qty'] - ); - } else { - $items[] = array( - 'productId' => $offer_id, - 'productName' => $product['name'], - 'initialPrice' => (float)$_product->get_price(), - 'quantity' => $product['qty'] - ); - } - } - - $order_data['items'] = $items; - - if (!empty($shipping_method) && !empty($this->retailcrm_settings[$shipping_method])) { - $order_data['delivery']['code'] = $this->retailcrm_settings[$shipping_method]; - } - - if (!empty($shipping_cost)) { - $shipping_cost = str_replace(',', '.', $shipping_cost); - $order_data['delivery']['cost'] = $shipping_cost; - } - - $this->retailcrm->ordersEdit($order_data); - } - - /** - * get order data depending on woocommerce version + * Get order data * * @param int $order_id * - * @return arr + * @return array $order_data_arr */ - public function getOrderData($order_id) { + protected function getOrderData($order_id) { $order = new WC_Order( $order_id ); $order_data_arr = array(); + $order_info = $order->get_data(); - if (version_compare(get_option('woocommerce_db_version'), '3.0', '<' )) { - $order_data_arr['id'] = $order->id; - $order_data_arr['date'] = $order->order_date; - $order_data_arr['payment_method'] = $order->payment_method; - $order_data_arr['discount_total'] = $order->data['discount_total']; - $order_data_arr['discount_tax'] = $order->data['discount_tax']; - $order_data_arr['customer_comment'] = $order->data['customerComment']; - } else { - $order_info = $order->get_data(); - - $order_data_arr['id'] = $order_info['id']; - $order_data_arr['payment_method'] = $order->get_payment_method(); - $order_data_arr['date'] = $order_info['date_created']->date('Y-m-d H:i:s'); - $order_data_arr['discount_total'] = $order_info['discount_total']; - $order_data_arr['discount_tax'] = $order_info['discount_tax']; - $order_data_arr['customer_comment'] = $order->get_customer_note(); - } + $order_data_arr['id'] = $order_info['id']; + $order_data_arr['payment_method'] = $order->get_payment_method(); + $order_data_arr['date'] = $order_info['date_created']->date('Y-m-d H:i:s'); + $order_data_arr['discount_total'] = $order_info['discount_total']; + $order_data_arr['discount_tax'] = $order_info['discount_tax']; + $order_data_arr['customer_comment'] = $order->get_customer_note(); return $order_data_arr; } @@ -288,7 +220,7 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : * * @return array $order_data */ - public function processOrder($order_id, $update = false) + protected function processOrder($order_id, $update = false) { if ( !$order_id ){ return; @@ -491,6 +423,10 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : */ public function updateOrder($order_id) { + if (!$this->retailcrm) { + return; + } + $order = $this->processOrder($order_id, true); $response = $this->retailcrm->ordersEdit($order); diff --git a/woo-retailcrm/include/class-wc-retailcrm-plugin.php b/woo-retailcrm/include/class-wc-retailcrm-plugin.php new file mode 100644 index 0000000..96d9b73 --- /dev/null +++ b/woo-retailcrm/include/class-wc-retailcrm-plugin.php @@ -0,0 +1,55 @@ +file = $file; + } + + public function register_activation_hook() { + register_activation_hook($this->file, array($this, 'activate')); + } + + public function register_deactivation_hook() { + register_deactivation_hook($this->file, array($this, 'deactivate')); + } + + public function activate() { + if (!class_exists('WC_Retailcrm_Icml')) { + require_once (dirname(__FILE__) . '/class-wc-retailcrm-icml.php'); + } + + if (!class_exists('WC_Retailcrm_Base')) { + require_once (dirname(__FILE__) . '/class-wc-retailcrm-base.php'); + } + + $retailcrm_icml = new WC_Retailcrm_Icml(); + $retailcrm_icml->generate(); + } + + public function deactivate() { + if ( wp_next_scheduled ( 'retailcrm_icml' )) { + wp_clear_scheduled_hook('retailcrm_icml'); + } + + if ( wp_next_scheduled ( 'retailcrm_history' )) { + wp_clear_scheduled_hook('retailcrm_history'); + } + + if ( wp_next_scheduled ( 'retailcrm_inventories' )) { + wp_clear_scheduled_hook('retailcrm_inventories'); + } + } +} \ No newline at end of file diff --git a/woo-retailcrm/languages/retailcrm-ru_RU.mo b/woo-retailcrm/languages/retailcrm-ru_RU.mo index feebbe1..e69de29 100644 Binary files a/woo-retailcrm/languages/retailcrm-ru_RU.mo and b/woo-retailcrm/languages/retailcrm-ru_RU.mo differ diff --git a/woo-retailcrm/retailcrm.php b/woo-retailcrm/retailcrm.php index 94615ce..f85c982 100644 --- a/woo-retailcrm/retailcrm.php +++ b/woo-retailcrm/retailcrm.php @@ -1,6 +1,8 @@ register_activation_hook(); + $plugin->register_deactivation_hook(); + + add_action( 'plugins_loaded', array( 'WC_Integration_Retailcrm', 'get_instance' ), 0 ); endif; - -/* - * Check icml custom class - */ -function check_custom_icml() -{ - if (file_exists( WP_CONTENT_DIR . '/retailcrm-custom/class-wc-retailcrm-icml.php' )) { - $file = WP_CONTENT_DIR . '/retailcrm-custom/class-wc-retailcrm-icml.php'; - } else { - $file = __DIR__ . '/include/class-wc-retailcrm-icml.php'; - } - return $file; -} - -/* - * Check orders custom class - */ -function check_custom_orders() -{ - if (file_exists( WP_CONTENT_DIR . '/retailcrm-custom/class-wc-retailcrm-orders.php' )) { - $file = WP_CONTENT_DIR . '/retailcrm-custom/class-wc-retailcrm-orders.php'; - } else { - $file = __DIR__ . '/include/class-wc-retailcrm-orders.php'; - } - return $file; -} - -/* - * Check customers custom class - */ -function check_custom_customers() -{ - if (file_exists( WP_CONTENT_DIR . '/retailcrm-custom/class-wc-retailcrm-customers.php' )) { - $file = WP_CONTENT_DIR . '/retailcrm-custom/class-wc-retailcrm-customers.php'; - } else { - $file = __DIR__ . '/include/class-wc-retailcrm-customers.php'; - } - return $file; -} - -/* - * Check inventories custom class - */ -function check_custom_inventories() -{ - if (file_exists( WP_CONTENT_DIR . '/retailcrm-custom/class-wc-retailcrm-inventories.php' )) { - $file = WP_CONTENT_DIR . '/retailcrm-custom/class-wc-retailcrm-inventories.php'; - } else { - $file = __DIR__ . '/include/class-wc-retailcrm-inventories.php'; - } - return $file; -} - -/* - * Check history custom class - */ -function check_custom_history() -{ - if (file_exists( WP_CONTENT_DIR . '/retailcrm-custom/class-wc-retailcrm-history.php' )) { - $file = WP_CONTENT_DIR . '/retailcrm-custom/class-wc-retailcrm-history.php'; - } else { - $file = __DIR__ . '/include/class-wc-retailcrm-history.php'; - } - return $file; -} - -/** - * Activation action - */ -function retailcrm_install() -{ - if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option( 'active_plugins')))) { - generate_icml(); - } -} - -/** - * Deactivation action - */ -function retailcrm_deactivation() -{ - if ( wp_next_scheduled ( 'retailcrm_icml' )) { - wp_clear_scheduled_hook('retailcrm_icml'); - } -} - -/** - * Load stocks from CRM - */ -function load_stocks() -{ - if ( ! class_exists( 'WC_Retailcrm_Inventories' ) ) { - include_once( check_custom_inventories() ); - } - - $inventories = new WC_Retailcrm_Inventories(); - $inventories->updateQuantity(); -} - -/** - * Generate ICML file - */ -function generate_icml() -{ - if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) { - include_once( check_custom_icml() ); - } - - $icml = new WC_Retailcrm_Icml(); - $icml->generate(); -} - -/** - * Create order - * - * @param $order_id - */ -function retailcrm_process_order($order_id) -{ - if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) { - include_once( check_custom_orders() ); - } - - $order_class = new WC_Retailcrm_Orders(); - $order_class->orderCreate($order_id); -} - -/** - * Update order status - * - * @param $order_id - */ -function retailcrm_update_order_status($order_id) -{ - if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) { - include_once( check_custom_orders() ); - } - - $order_class = new WC_Retailcrm_Orders(); - $order_class->orderUpdateStatus($order_id); -} - -/** - * Update order payment - * - * @param $order_id - */ -function retailcrm_update_order_payment($order_id) -{ - if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) { - include_once( check_custom_orders() ); - } - - $order_class = new WC_Retailcrm_Orders(); - $order_class->orderUpdatePayment($order_id); -} - -/** - * Update order items - * - * @param $order_id, $data - */ -function retailcrm_update_order_items($order_id, $data) -{ - if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) { - include_once( check_custom_orders() ); - } - - $order_class = new WC_Retailcrm_Orders(); - $order_class->orderUpdateItems($order_id, $data); -} - -function retailcrm_history_get() -{ - if ( ! class_exists( 'WC_Retailcrm_History' ) ) { - include_once( check_custom_history() ); - } - - $history_class = new WC_Retailcrm_History(); - $history_class->getHistory(); -} - -function create_customer($customer_id) { - if ( ! class_exists( 'WC_Retailcrm_Customers' ) ) { - include_once( check_custom_customers() ); - } - - $customer_class = new WC_Retailcrm_Customers(); - $customer_class->createCustomer($customer_id); -} - -function update_customer($customer_id) { - if ( ! class_exists( 'WC_Retailcrm_Customers' ) ) { - include_once( check_custom_customers() ); - } - - $customer_class = new WC_Retailcrm_Customers(); - $customer_class->updateCustomer($customer_id); -} - -function register_icml_generation() { - // Make sure this event hasn't been scheduled - if( !wp_next_scheduled( 'retailcrm_icml' ) ) { - // Schedule the event - wp_schedule_event( time(), 'three_hours', 'retailcrm_icml' ); - } -} - -function register_retailcrm_history() { - // Make sure this event hasn't been scheduled - if( !wp_next_scheduled( 'retailcrm_history' ) ) { - // Schedule the event - wp_schedule_event( time(), 'five_minutes', 'retailcrm_history' ); - } -} - -function check_inventories() { - if( !wp_next_scheduled( 'retailcrm_inventories' ) ) { - // Schedule the event - wp_schedule_event( time(), 'fiveteen_minutes', 'retailcrm_inventories' ); - } -} - -function filter_cron_schedules($param) { - return array( - 'five_minutes' => array( - 'interval' => 300, // seconds - 'display' => __('Every 5 minutes') - ), - 'three_hours' => array( - 'interval' => 10800, // seconds - 'display' => __('Every 3 hours') - ), - 'fiveteen_minutes' => array( - 'interval' => 900, // seconds - 'display' => __('Every 15 minutes') - ) - ); -} - -function upload_to_crm() { - if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) { - include_once( check_custom_orders() ); - } - - if ( ! class_exists( 'WC_Retailcrm_Customers' ) ) { - include_once( check_custom_customers() ); - } - - $options = array_filter(get_option( 'woocommerce_integration-retailcrm_settings' )); - - $orders = new WC_Retailcrm_Orders(); - $customers = new WC_Retailcrm_Customers(); - $customers->customersUpload(); - $orders->ordersUpload(); - - $options['uploads'] = 'yes'; - update_option('woocommerce_integration-retailcrm_settings', $options); -} - -function ajax_upload() { - $ajax_url = admin_url('admin-ajax.php'); - ?> - - - - updateOrder($order_id); -} - -function initialize_analytics() { - $options = get_option('woocommerce_integration-retailcrm_settings'); - - if ($options && is_array($options)) { - $options = array_filter($options); - - if (isset($options['ua']) && $options['ua'] == 'yes') { - ?> - - get_items() as $item) { - $uid = ($item['variation_id'] > 0) ? $item['variation_id'] : $item['product_id'] ; - $_product = wc_get_product($uid); - if ($_product) { - $order_item = array( - 'id' => $uid, - 'name' => $item['name'], - 'price' => (float)$_product->get_price(), - 'quantity' => $item['qty'], - ); - - $order_items[] = $order_item; - } - } - - $url = parse_url(get_site_url()); - $domain = $url['host']; - ?> - -