diff --git a/affili_ir.php b/affili_ir.php index 4dd8588..d9985df 100644 --- a/affili_ir.php +++ b/affili_ir.php @@ -7,7 +7,7 @@ * Plugin Name: affili_ir * Plugin URI: https://github.com/affili-ir/wordpress * Description: The WordPress plugin for Affili's merchants. - * Version: 3.2.0 + * Version: 2.0.0 * Author: Affili IR * Author URI: https://affili.ir * License: GPLv2 or later diff --git a/assets/css/admin-style-main.css b/assets/css/admin-style-main.css index 5a592d5..a101a7f 100644 --- a/assets/css/admin-style-main.css +++ b/assets/css/admin-style-main.css @@ -39,44 +39,6 @@ } } -/* For Select2 -.select2-container .select2-selection--single { - height: inherit; - min-height: calc(1.5em + .75rem + 2px); -} */ - - -.wp-core-ui .select2-container .select2-selection--single { - display: block; - width: 100%; - height: calc(1.5em + .75rem + 2px); - padding: .375rem .75rem; - font-size: 1rem; - font-weight: 400; - line-height: 1.5; - color: #495057; - background-color: #fff; - background-clip: padding-box; - border: 1px solid #ced4da; - border-radius: .25rem; - transition: border-color .15s ease-in-out,box-shadow .15s ease-in-out; -} - -.wp-core-ui .select2-container .select2-selection--single { - font-size: 14px; - line-height: 2; - color: #32373c; - border-color: #7e8993; - box-shadow: none; - border-radius: 3px; - padding: 0 8px 0 24px; - min-height: 30px; - -webkit-appearance: none; - cursor: pointer; - vertical-align: middle; -} - - .affili-card-inform { min-width:100%; margin-top:5px!important; diff --git a/assets/js/admin-script-main.js b/assets/js/admin-script-main.js index 5f1071b..7b18dd3 100644 --- a/assets/js/admin-script-main.js +++ b/assets/js/admin-script-main.js @@ -1,31 +1,3 @@ jQuery(function($){ - // multiple select with AJAX search - $('#affili-ir-select2-categories').select2({ - ajax: { - url: ajaxurl, // AJAX URL is predefined in WordPress admin - dataType: 'json', - delay: 250, // delay in ms while typing when to perform a AJAX search - data: function (params) { - return { - q: params.term, // search query - action: 'affili_find_category' // AJAX action for admin-ajax.php - }; - }, - processResults: function( data ) { - var options = []; - if ( data ) { - // data is the array of arrays, and each of them contains ID and the Label of the option - $.each( data, function( index, text ) { // do not forget that "index" is just auto incremented value - options.push( { id: text[0], text: text[1] } ); - }); - - } - return { - results: options - }; - }, - cache: true - }, - minimumInputLength: 3 // the minimum of symbols to input before perform a search - }); + // }); \ No newline at end of file diff --git a/classes/Action.php b/classes/Action.php index 93df327..a79a569 100644 --- a/classes/Action.php +++ b/classes/Action.php @@ -4,14 +4,10 @@ require_once 'Woocommerce.php'; -require_once 'ListTable.php'; require_once 'Installer.php'; -require_once 'ActivePluginsCheck.php'; -use AffiliIR\ActivePluginsCheck as AffiliIR_ActivePluginsCheck; use AffiliIR\Woocommerce as AffiliIR_Woocommerce; -use AffiliIR\ListTable as AffiliIR_ListTable; use AffiliIR\Installer as AffiliIR_Installer; class Action @@ -57,27 +53,18 @@ public function menu() public function renderPage() { - $show_brand = AffiliIR_ActivePluginsCheck::wooBrandActiveCheck(); - $woocommerce = new AffiliIR_Woocommerce; - $account_id = $this->getAccountId(); $custom_code = $this->getCustomCode(); $plugin_name = $this->plugin_name; - $list_table = new AffiliIR_ListTable(); - $list_table->prepare_items(); - include_once __DIR__.'/../views/form.php'; } public function loadAdminStyles() { - wp_enqueue_style( 'affili-ir-admin-style', plugins_url('assets/css/admin-style-main.css',__DIR__), false, '1.0.0' ); + wp_enqueue_style( 'affili-ir-admin-style', plugins_url('assets/css/admin-style-main.css',__DIR__), false, '2.0.0' ); - wp_enqueue_style('select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css' ); - wp_enqueue_script('select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js', array('jquery') ); - - wp_enqueue_script('affili-ir-admin-script', plugins_url('assets/js/admin-script-main.js', __DIR__), array( 'jquery', 'select2' ) ); + wp_enqueue_script('affili-ir-admin-script', plugins_url('assets/js/admin-script-main.js', __DIR__), array( 'jquery') ); } public function setAccountId() @@ -104,9 +91,6 @@ public function setAccountId() ]); } - $woocommerce = new AffiliIR_Woocommerce; - $woocommerce->insertCommissionKeys($_POST['item']); - $admin_notice = "success"; $message = __('Data saved successful.', $this->plugin_name); @@ -186,7 +170,7 @@ public function setAffiliJs() { $script = $this->createInlineScript(); - wp_enqueue_script("affili-ir-script", "https://analytics.affili.ir/scripts/affili-js.js"); + wp_enqueue_script("affili-ir-script", "https://analytics.affili.ir/scripts/affili-v2.js"); wp_add_inline_script("affili-ir-script", $script); } @@ -212,33 +196,28 @@ public function createInlineScript() public function trackOrders($order_id) { - $order_id = apply_filters('woocommerce_thankyou_order_id', absint($GLOBALS['order-received'])); - $order_key = apply_filters('woocommerce_thankyou_order_key', empty($_GET['key']) ? '' : wc_clean($_GET['key'])); + $order_id = apply_filters('woocommerce_thankyou_order_id', absint($GLOBALS['order-received'])); + $order_key = apply_filters('woocommerce_thankyou_order_key', empty($_GET['key']) ? '' : wc_clean($_GET['key'])); $woocommerce = new AffiliIR_Woocommerce; - $order = wc_get_order($order_id); + $order = wc_get_order($order_id); if ($order_id <= 0) return; $order_key_check = $woocommerce->isWoo3() ? $order->get_order_key() : $order->order_key; - if ($order_key_check !== $order_key) return; + if ($order_key_check != $order_key) return; $data = $woocommerce->getOrderData($order); - $commissions = $data['commissions']; - $options = $data['options']; - $external_id = $data['external_id']; - $amount = $data['amount']; - $is_multi = $data['is_multi']; - $default_name = $data['default_name']; - // $order_key = $data['order_key']; - - // Check if we have multiple commission names - if($is_multi) { - $script = "affili('conversionMulti', '{$external_id}', '{$amount}', {$commissions}, {$options});"; - }else { - $script = "affili('conversion', '{$external_id}', '{$amount}', '{$default_name}', {$options})"; - } + $order_id = $data['order_id']; + $amount = $data['amount']; + $options = [ + 'coupon' => $data['coupon'], + 'products' => $data['products'] + ]; + $options = count($options) ? json_encode($options) : json_encode($options, JSON_FORCE_OBJECT); + + $script = "affili('conversion', '{$order_id}', '{$amount}', {$options})"; wp_add_inline_script("affili-ir-script", $script); } @@ -263,9 +242,6 @@ public function setup() add_action('wp_head', [$this, 'setAffiliJs'] ); add_action('woocommerce_thankyou', [$this, 'trackOrders']); - - add_action('wp_ajax_affili_find_category', [$this, 'findCategoryAjax']); - add_action('wp_ajax_affili_find_brand', [$this, 'findBrandAjax']); } public static function factory() @@ -329,43 +305,6 @@ protected function addFlashNotice($notice = '', $type = 'success', $dismissible update_option('affili_flash_notices', $notices ); } - public function findCategoryAjax() - { - // we will pass category IDs and titles to this array - $return = []; - - $search_results = (new AffiliIR_Woocommerce)->getCategories(null, [ - 'name__like' => $_GET['q'], - ]); - foreach($search_results as $result) { - $return[] = [ - $result->cat_ID, - $result->cat_name, - ]; - } - echo json_encode( $return ); - wp_die(); - } - - public function findBrandAjax() - { - // we will pass brand IDs and names to this array - $return = []; - - $search_results = (new AffiliIR_Woocommerce)->getBrands(null, [ - 'name__like' => $_GET['q'], - ]); - - foreach($search_results as $result) { - $return[] = [ - $result->term_id, - $result->name, - ]; - } - echo json_encode( $return ); - wp_die(); - } - private function createTableIfNotExists() { $sql = AffiliIR_Installer::sqlString(); diff --git a/classes/ActivePluginsCheck.php b/classes/ActivePluginsCheck.php deleted file mode 100644 index 27509f3..0000000 --- a/classes/ActivePluginsCheck.php +++ /dev/null @@ -1,43 +0,0 @@ -get_columns(); - $hidden = $this->get_hidden_columns(); - $sortable = $this->get_sortable_columns(); - - $data = $this->table_data(); - usort( $data, array( &$this, 'sort_data' ) ); - - $perPage = 20; - $currentPage = $this->get_pagenum(); - $totalItems = count($data); - - $this->set_pagination_args( array( - 'total_items' => $totalItems, - 'per_page' => $perPage - ) ); - - $data = array_slice($data,(($currentPage-1)*$perPage),$perPage); - - $this->_column_headers = array($columns, $hidden, $sortable); - $this->items = $data; - } - - /** - * Override the parent columns method. Defines the columns to use in your listing table - * - * @return Array - */ - public function get_columns() - { - $columns = [ - 'id' => __('Category ID', $this->plugin_name), - 'category' => __('Category', $this->plugin_name), - ]; - - if(AffiliIR_ActivePluginsCheck::wooBrandActiveCheck()) { - $columns += [ - 'brand' => __('Brand', $this->plugin_name), - ]; - } - - $columns += [ - 'value' => __('Commission key', $this->plugin_name), - ]; - - return $columns; - } - - /** - * Define which columns are hidden - * - * @return Array - */ - public function get_hidden_columns() - { - return []; - } - - /** - * Define the sortable columns - * - * @return Array - */ - public function get_sortable_columns() - { - return [ - 'id' => ['id', false] - ]; - } - - /** - * Get the table data - * - * @return Array - */ - private function table_data() - { - $woocommerce = new AffiliIR_Woocommerce; - - $commission_keys = $woocommerce->getCommissionKeys(); - foreach($commission_keys as $key => $commission) { - $cat_id = str_replace('commission-cat-', '', $commission->name); - if($this->strContains($cat_id, 'brand-')) { - $cat_id = $this->strBefore($cat_id, 'brand-'); - } - - $brand_id = $this->strAfter($commission->name, 'brand-'); - - $commission_keys[$key]->brand = $brand_id ? get_the_category_by_ID((int)$brand_id) : null; - $commission_keys[$key]->category = get_the_category_by_ID((int)$cat_id); - } - - return json_decode( json_encode($commission_keys), 1 ); - } - - /** - * Define what data to show on each column of the table - * - * @param Array $item Data - * @param String $column_name - Current column name - * - * @return Mixed - */ - public function column_default( $item, $column_name ) - { - switch( $column_name ) { - case 'id': - case 'category': - case 'brand': - case 'value': - return $item[ $column_name ]; - - default: - return print_r( $item, true ) ; - } - } - - /** - * Allows you to sort the data by the variables set in the $_GET - * - * @return Mixed - */ - private function sort_data( $a, $b ) - { - // Set defaults - $orderby = 'id'; - $order = 'asc'; - - // If orderby is set, use this as the sort column - if(!empty($_GET['orderby'])) - { - $orderby = $_GET['orderby']; - } - - // If order is set use this as the order - if(!empty($_GET['order'])) - { - $order = $_GET['order']; - } - - - $result = strcmp( $a[$orderby], $b[$orderby] ); - - if($order === 'asc') - { - return $result; - } - - return -$result; - } - - private function strContains($haystack, $needles) - { - foreach ((array) $needles as $needle) { - if ($needle !== '' && mb_strpos($haystack, $needle) !== false) { - return true; - } - } - - return false; - } - - private function strBefore($subject, $search) - { - return $search === '' ? $subject : explode($search, $subject)[0]; - } - - private function strAfter($subject, $search) - { - $result = $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0]; - - return $result === $subject ? null : $result; - } -} -?> \ No newline at end of file diff --git a/classes/Woocommerce.php b/classes/Woocommerce.php index 07f1ee1..24998d1 100644 --- a/classes/Woocommerce.php +++ b/classes/Woocommerce.php @@ -3,15 +3,10 @@ namespace AffiliIR; -require_once 'ActivePluginsCheck.php'; - - -use AffiliIR\ActivePluginsCheck as AffiliIR_ActivePluginsCheck; - class Woocommerce { - private $table_name; - private $wpdb; + protected $table_name; + protected $wpdb; public function __construct() { @@ -21,231 +16,42 @@ public function __construct() $this->table_name = $wpdb->prefix . 'affili'; } - public function getCategories($category_id = null, $options = []) - { - $taxonomy = 'product_cat'; - $orderby = 'id'; - $show_count = 0; // 1 for yes, 0 for no - $pad_counts = 0; // 1 for yes, 0 for no - $hierarchical = 1; // 1 for yes, 0 for no - $title = ''; - $empty = 0; - - $args = [ - 'taxonomy' => $taxonomy, - 'orderby' => $orderby, - 'show_count' => $show_count, - 'pad_counts' => $pad_counts, - 'hierarchical' => $hierarchical, - 'title_li' => $title, - 'hide_empty' => $empty, - ]; - - if($category_id !== null) { - $args += [ - 'parent' => $category_id, - 'child_of' => 0, - ]; - } - - if(is_array($options) && $options) { - $args = array_merge($args, $options); - } - - return get_categories( $args ); - } - - public function getBrands($brand_id = null, $options = []) - { - $taxonomy = 'product_brand'; - $orderby = 'id'; - $show_count = 0; // 1 for yes, 0 for no - $pad_counts = 0; // 1 for yes, 0 for no - $hierarchical = 1; // 1 for yes, 0 for no - $title = ''; - $empty = 0; - - $args = [ - 'taxonomy' => $taxonomy, - 'orderby' => $orderby, - 'show_count' => $show_count, - 'pad_counts' => $pad_counts, - 'hierarchical' => $hierarchical, - 'title_li' => $title, - 'hide_empty' => $empty, - ]; - - if($brand_id !== null) { - $args += [ - 'parent' => $brand_id, - 'child_of' => 0, - ]; - } - - if(is_array($options) && $options) { - $args = array_merge($args, $options); - } - - $brands = get_terms($args); - - if (is_wp_error($brands)) { - $brands = []; - } - - return $brands; - } - - public function insertCommissionKeys($item) - { - $category_id = $item['category_id'] ?? null; - $brand_id = $item['brand_id'] ?? null; - - if($category_id === null) { - return; - } - - $commission_key = $this->findCommissionKey($category_id, $brand_id); - - $name = $brand_id ? "commission-cat-{$category_id}-brand-{$brand_id}" - : "commission-cat-{$category_id}" - ; - $data = [ - 'name' => $name, - 'value' => $item['commission_key'], - ]; - - if(empty($commission_key)) { - $this->wpdb->insert($this->table_name, $data, '%s'); - }else { - $this->wpdb->update($this->table_name, $data, [ - 'id' => $commission_key->id - ]); - } - } - - public function findCommissionKey($category_id, $brand_id = null) - { - $name = $brand_id ? "commission-cat-{$category_id}-brand-{$brand_id}" - : "commission-cat-{$category_id}" - ; - $result = $this->wpdb->get_results( - "SELECT * FROM {$this->table_name} WHERE name = '{$name}' limit 1" - ); - $result = is_array($result) ? array_pop($result) : []; - - return $result; - } - - public function getCommissionKeys() - { - $sql = "SELECT * FROM {$this->table_name} WHERE name LIKE 'commission-cat-%'"; - if(!AffiliIR_ActivePluginsCheck::wooBrandActiveCheck()) { - $sql .= " AND name NOT LIKE 'commission-cat-%-brand-%'"; - } - - $result = $this->wpdb->get_results($sql); - - return $result; - } - public function getOrderData($order) { - $options = []; - $commissions = []; - $holder = []; - + $coupon = ''; + $products = []; foreach ($order->get_items() as $item) { - $subtotal = $this->getOrderItemSubtotal($order, $item); - $item_commission = $this->getOrderItemCommissions($item, $subtotal); - - $commission_name = $item_commission['name']; - - $holder[$commission_name] = - ($holder[$commission_name] ?? 0) + $item_commission['sub_amount'] - ; - - $key = "product-{$item->get_product_id()}"; - $line_item = "{$item['name']} - qty: {$item['qty']}"; - - $options['meta_data'][$key] = $line_item; - } - - foreach($holder as $name => $sub_amount) { - $commissions[] = [ - 'name' => $name, - 'sub_amount' => $sub_amount + $item_subtotal = floatval($this->wooRound($item->get_subtotal())); + + $products[] = [ + 'product_page_id' => $item->get_product_id(), + 'sku' => $item['name'], + 'unit_price' => $item_subtotal / $item['qty'], + 'quantity' => $item['qty'], + 'total_price' => $item_subtotal, ]; } - if($coupons = $order->get_coupon_codes()) { - $options['coupon'] = array_values($coupons)[0] ?? ''; - } - - $external_id = $this->isWoo3() ? $order->get_id() : $order->id; - - $amount = $order->get_subtotal() - $order->get_total_discount(); - $amount = $order->get_currency() === 'IRT' ? $amount * 10 : $amount; - - $uniq_names = array_unique(array_column($commissions, 'name')); - $is_multi = count($uniq_names) > 1; - - $default_name = count($uniq_names) === 1 ? $uniq_names[0] : 'default'; - - $commissions = count($commissions) ? json_encode($commissions) : json_encode($commissions, JSON_FORCE_OBJECT); - $options = count($options) ? json_encode($options) : json_encode($options, JSON_FORCE_OBJECT); - - return [ - 'commissions' => $commissions, - 'options' => $options, - 'external_id' => $external_id, - 'amount' => $amount, - 'is_multi' => $is_multi, - 'default_name' => $default_name, - // 'order_key' => $order->get_order_key(), - ]; - - } - - private function getOrderItemSubtotal($order, $item) - { - $item_subtotal = floatval($this->wooRound($item->get_subtotal())); - if ($item_subtotal === 0.00) { - return 0; - } - - $proportional_discount = ( - $item_subtotal / $this->wooRound($order->get_subtotal()) - ) * $order->get_total_discount(); - - $subtotal = $item_subtotal - $proportional_discount; - $subtotal = $order->get_currency() === 'IRT' ? $subtotal * 10 : $subtotal; - - return $subtotal; - } - - private function getOrderItemCommissions($item, $subtotal) - { - $category_commission_type = false; - $categories = wp_get_post_terms($item->get_product_id(), 'product_cat'); - - $brand_id = null; - if(AffiliIR_ActivePluginsCheck::wooBrandActiveCheck()) { - $brands = get_the_terms($item->get_product_id(), 'product_brand'); - if($brands) { - $brand_id = end($brands)->term_id ?? null; + if ($this->isWoo3()) { + if ($coupons = $order->get_data()['coupon_lines']) { + $coupon = array_values($coupons)[0]->get_code() ?? ''; } - } - - foreach ( $categories as $category ) { - $commission_key = $this->findCommissionKey($category->term_id, $brand_id); - if($commission_key && $commission_key->value) { - $category_commission_type = $commission_key->value; + } else { + if ($coupons = $order->get_coupon_codes()) { + $coupon = array_values($coupons)[0] ?? ''; } } + $order_id = $this->isWoo3() ? $order->get_id() : $order->id; + + $amount = $order->get_subtotal() - $order->get_total_discount(); + $amount = $order->get_currency() === 'IRT' ? $amount * 10 : $amount; + return [ - 'sub_amount' => $subtotal, - 'name' => $category_commission_type ?: 'default', + 'order_id' => $order_id, + 'amount' => $amount, + 'products' => $products, + 'coupon' => $coupon ]; } diff --git a/languages/affili_ir-fa_IR.mo b/languages/affili_ir-fa_IR.mo index 1890a48..4d4ac19 100644 Binary files a/languages/affili_ir-fa_IR.mo and b/languages/affili_ir-fa_IR.mo differ diff --git a/languages/affili_ir-fa_IR.po b/languages/affili_ir-fa_IR.po index 334c0a7..cb4fa89 100644 --- a/languages/affili_ir-fa_IR.po +++ b/languages/affili_ir-fa_IR.po @@ -43,7 +43,4 @@ msgid "Add Commission key" msgstr "افزودن کلید کمیسیون" msgid "Custom Code" -msgstr "کد دلخواه" - -msgid "Brand" -msgstr "برند" \ No newline at end of file +msgstr "کد دلخواه" \ No newline at end of file diff --git a/views/form.php b/views/form.php index 74ea4a7..07f7690 100644 --- a/views/form.php +++ b/views/form.php @@ -8,31 +8,10 @@ -
-