From 36adaa7c7c8d7c21796766079f3d149e47a04efa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=A9ng=20Nguy=E1=BB=85n?= Date: Mon, 18 Dec 2017 15:36:43 +0700 Subject: [PATCH 01/12] bulk delete --- admin.php | 105 +++++++++++++++++++++++++++++++++++++---------- coupons_list.php | 44 +++++++++++--------- 2 files changed, 108 insertions(+), 41 deletions(-) diff --git a/admin.php b/admin.php index 4a907d7..53e86f8 100644 --- a/admin.php +++ b/admin.php @@ -5,7 +5,8 @@ public function __construct() { add_action( 'admin_menu', [$this,'admin_page'] ); add_action( 'wp_ajax_nhymxu_coupons_ajax_insertupdate', [$this, 'ajax_insert_update'] ); add_action( 'wp_ajax_nhymxu_coupons_ajax_checkcoupon', [$this, 'ajax_check_coupon'] ); - add_action( 'wp_ajax_nhymxu_coupons_ajax_deletecoupon', [$this, 'ajax_delete_coupon'] ); + add_action( 'wp_ajax_nhymxu_coupons_ajax_deletecoupon', [$this, 'ajax_delete_coupon'] ); + add_action( 'wp_ajax_nhymxu_coupons_ajax_bulkdeletecoupon', [$this, 'ajax_bulk_delete_coupon'] ); add_action( 'init', [$this, 'wp_strip_referer'] ); } @@ -86,13 +87,37 @@ public function ajax_delete_coupon() { wp_die(); } + public function ajax_bulk_delete_coupon() { + global $wpdb; + + $ids = ($_POST['ids']) ? $_POST['ids'] : ''; + + if( $ids == '' ) { + echo 'not_found'; + wp_die(); + } + $ids = trim($ids); + + $wpdb->query("START TRANSACTION;"); + try { + $wpdb->query("DELETE FROM {$wpdb->prefix}coupons WHERE id IN({$ids});"); + $wpdb->query("DELETE FROM {$wpdb->prefix}coupon_category_rel WHERE coupon_id IN({$ids});"); + $wpdb->query("COMMIT;"); + echo 'ok'; + } catch ( Exception $e ) { + $wpdb->query("ROLLBACK;"); + echo 'fail'; + } + wp_die(); + } + /* * callback insert function for ajax action */ private function coupon_insert( $input ) { global $wpdb; - $result = $wpdb->insert( + $result = $wpdb->insert( $wpdb->prefix . 'coupons', [ 'type' => $input['merchant'], @@ -109,7 +134,7 @@ private function coupon_insert( $input ) { if( $result !== false ) { $this->coupon_tracking( $input ); } - + return $result; } @@ -119,7 +144,7 @@ private function coupon_insert( $input ) { private function coupon_update( $input ) { global $wpdb; - $result = $wpdb->update( + $result = $wpdb->update( $wpdb->prefix . 'coupons', [ 'type' => $input['merchant'], @@ -131,7 +156,7 @@ private function coupon_update( $input ) { 'save' => ($input['save']) ? $input['save'] : '' ], [ 'id' => $input['cid'] ], - ['%s','%s','%s','%s','%s','%s','%s'], + ['%s','%s','%s','%s','%s','%s','%s'], ['%d'] ); @@ -154,7 +179,7 @@ private function coupon_tracking( $input ) { 'data' => json_encode( $input ) ], 'cookies' => [] - ]); + ]); } private function get_coupon_detail( $coupon_id ) { @@ -168,7 +193,7 @@ private function get_coupon_detail( $coupon_id ) { } return false; - } + } public function admin_page() { add_menu_page( 'Danh sách coupon', 'Smart Coupons', 'manage_options', 'accesstrade_coupon', [$this, 'admin_page_callback_list'], 'dashicons-tickets', 6 ); @@ -188,7 +213,7 @@ public function admin_page_callback_settings() { 'accesskey' => sanitize_text_field($_REQUEST['nhymxu_at_coupon_accesskey']), 'utmsource' => sanitize_text_field($_REQUEST['nhymxu_at_coupon_utmsource']) ]; - + update_option('nhymxu_at_coupon', $input); update_option( 'nhymxu_at_coupon_merchants', [] ); $nhymxu_at_coupon_pro->do_this_daily(); @@ -209,7 +234,7 @@ function nhymxu_force_update_coupons() { if( is_run !== 0 ) { console.log('Đã chạy rồi'); return false; - } + } jQuery('#nhymxu_force_update').attr('disabled', 'disabled'); jQuery.ajax({ type: "POST", @@ -226,7 +251,7 @@ function nhymxu_force_update_merchants() { if( is_run !== 0 ) { console.log('Đã chạy rồi'); return false; - } + } jQuery('#nhymxu_force_update_merchants').attr('disabled', 'disabled'); jQuery.ajax({ type: "POST", @@ -295,7 +320,7 @@ function nhymxu_force_update_merchants() {

Danh các merchant

- get_results("SELECT type FROM {$wpdb->prefix}coupons GROUP BY type", ARRAY_A); foreach( $coupon_type as $row ) { echo $row['type'], ', '; @@ -305,8 +330,8 @@ function nhymxu_force_update_merchants() {


get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}coupons" ); - $today = date('Y-m-d'); - $total_expired_coupon = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}coupons WHERE exp < '{$today}'" ); + $today = date('Y-m-d'); + $total_expired_coupon = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}coupons WHERE exp < '{$today}'" ); ?>

Tổng số coupon trong hệ thống:

Tổng số coupon hết hạn:

@@ -336,7 +361,7 @@ public function admin_page_callback_addnew() { if( !$active_merchants ) { echo 'Chưa có campain nào được duyệt ( hoặc chưa đồng bộ ). vui lòng đồng bộ campain lại ở đây'; return false; - } + } $default_data = [ 'id' => 0, @@ -346,7 +371,7 @@ public function admin_page_callback_addnew() { 'exp' => '', 'note' => '', 'url' => '', - 'save' => '' + 'save' => '' ]; if( isset($_GET['coupon_id']) && $_GET['coupon_id'] != '' ) { @@ -478,7 +503,7 @@ function ajax_database_exec() { alert('Thành công'); exec_after_success(); } - } + } }); } } @@ -599,16 +624,16 @@ function nhymxu_delete_coupon( coupon_id, code ) { } }); } - + return false; } - + jQuery(document).ready(function($) { $('#btn-filter').click(function() { var merchant = $('#filter_merchant').val(); if( merchant !== '' ) { window.location.href = window.location.href + '&filter_merchant=' + merchant; - } + } }); $('#doaction').click(function() { var action = $('#bulk-action-selector-top').val(); @@ -618,10 +643,48 @@ function nhymxu_delete_coupon( coupon_id, code ) { if( $(this).is(':checked') ) { //console.log( $(this).val() ); bulk_id.push( $(this).val() ); - } + } console.log(bulk_id); }); - } + } + }); + $('#nhymxu-bulk_delete').click(function(e) { + e.preventDefault(); + + var $coupon_checked = $('.input_coupon_bulk_action:checked'); + if( $coupon_checked.length < 1 ) { + alert('Hãy chọn các coupon muốn xóa'); + return false; + } + + var coupon_ids = []; + $coupon_checked.each(function() { + coupon_ids.push( $(this).val() ); + }); + var ids = coupon_ids.toString(); + + var answer = confirm('Bạn muốn xóa hàng loạt coupon?'); + if (answer == true) { + jQuery.ajax({ + type: "POST", + url: ajaxurl, + data: { action: 'nhymxu_coupons_ajax_bulkdeletecoupon', ids:ids }, + success: function( resp ) { + resp = jQuery.trim(resp); + if( resp == 'not_found' ) { + alert('Không có coupon ID'); + } else if( resp == 'fail' ) { + alert( 'Xóa thất bại. vui lòng F5 và thử lại.' ); + } else { + alert( 'Xóa thành công!' ); + window.location.reload(); + } + return true; + } + }); + } + + return false; }); }); diff --git a/coupons_list.php b/coupons_list.php index 2e1a802..292bd9d 100644 --- a/coupons_list.php +++ b/coupons_list.php @@ -43,18 +43,18 @@ public function prepare_items() $currentPage = $this->get_pagenum(); $data = $this->table_data( $perPage, $currentPage ); - + $totalItems = $this->get_number_of_records(); - + $this->set_pagination_args( [ 'total_items' => $totalItems, 'per_page' => $perPage ] ); - + $this->_column_headers = [$columns, $hidden, $sortable]; $this->items = $data; } - + private function get_number_of_records() { global $wpdb; @@ -126,7 +126,7 @@ private function table_data( $per_page = 50, $page_number = 1 ) global $wpdb; $sql = "SELECT id, title, type, code, exp, note, save FROM {$wpdb->prefix}coupons"; - + if( $this->active_filter != '' ) { $sql .= ' WHERE type = "'. $this->active_filter .'"'; } @@ -148,8 +148,8 @@ private function table_data( $per_page = 50, $page_number = 1 ) $sql .= " LIMIT $per_page"; $sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page; - $results = $wpdb->get_results( $sql, ARRAY_A ); - + $results = $wpdb->get_results( $sql, ARRAY_A ); + return $results; } /** @@ -206,10 +206,14 @@ private function sort_data( $a, $b ) * Allow filter per merchant */ function extra_tablenav( $which ) { - ?>
filters ) ): - ?> + ?> + +
+ +
+ +
+ filters ) ): ?> -
+
+ 'Xóa coupon']; return $actions; @@ -252,19 +256,19 @@ public function get_bulk_actions() { * Adds row action links to the title column. * e.g. url/admin.php?page=accesstrade_coupon_addnew&coupon_id=1 */ - protected function column_title( $item ) { + protected function column_title( $item ) { $admin_page_url = admin_url('admin.php'); // row action to view usermeta. $query_args_editcoupon = array( 'page' => wp_unslash( 'accesstrade_coupon_addnew' ), 'coupon_id' => absint( $item['id']), ); - $editcoupon_link = esc_url( add_query_arg( $query_args_editcoupon, $admin_page_url ) ); - $actions['edit_coupon'] = 'Sửa'; + $editcoupon_link = esc_url( add_query_arg( $query_args_editcoupon, $admin_page_url ) ); + $actions['edit_coupon'] = 'Sửa'; $actions['delete_coupon'] = 'Xóa'; // similarly add row actions for add usermeta. //$_GET$row_value = '' . $item['title'] . ''; - $row_value = $item['title']; + $row_value = $item['title']; return $row_value . $this->row_actions( $actions ); } } From 5c9cd20ce09fde2ffd5128786cb1594112eb027a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=A9ng=20Nguy=E1=BB=85n?= Date: Thu, 21 Dec 2017 14:10:05 +0700 Subject: [PATCH 02/12] init function sync category --- nhymxu-at-coupon-pro.php | 43 +++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/nhymxu-at-coupon-pro.php b/nhymxu-at-coupon-pro.php index 79b5390..fca4da6 100644 --- a/nhymxu-at-coupon-pro.php +++ b/nhymxu-at-coupon-pro.php @@ -30,8 +30,10 @@ class nhymxu_at_coupon_pro { public function __construct() { add_action( 'nhymxu_at_coupon_sync_merchant_event', [$this,'do_this_daily'] ); + add_action( 'nhymxu_at_coupon_sync_category_event', [$this, 'do_this_weekly'] ); add_action( 'init', [$this, 'init_updater'] ); add_action( 'wp_ajax_nhymxu_coupons_ajax_forceupdate_merchants', [$this, 'ajax_force_update_merchants'] ); + add_action( 'wp_ajax_nhymxu_coupons_ajax_forceupdate_categories', [$this, 'ajax_force_update_categories'] ); } public function do_this_daily() { @@ -42,7 +44,7 @@ public function do_this_daily() { if( $options['accesskey'] == '' ) { return false; - } + } $url = 'https://api.accesstrade.vn/v1/campaigns'; @@ -51,7 +53,7 @@ public function do_this_daily() { 'headers' => ['Authorization' => 'Token '. $options['accesskey'] ], ]; - $result = wp_remote_get( $url, $args ); + $result = wp_remote_get( $url, $args ); if ( is_wp_error( $result ) ) { $msg = []; $msg['previous_time'] = ''; @@ -74,13 +76,26 @@ public function do_this_daily() { } } + public function do_this_weekly() { + + } + /* * Force update merchant list from server */ public function ajax_force_update_merchants() { $this->do_this_daily(); echo 'running'; - wp_die(); + wp_die(); + } + + /* + * Force update category list from server + */ + public function ajax_force_update_categories() { + $this->do_this_weekly(); + echo 'running'; + wp_die(); } public function init_updater() { @@ -98,8 +113,8 @@ public function init_updater() { private function insert_coupon( $data ) { global $wpdb; - - $result = $wpdb->insert( + + $result = $wpdb->insert( $wpdb->prefix . 'coupons', [ 'type' => $data['merchant'], @@ -112,7 +127,7 @@ private function insert_coupon( $data ) { ], ['%s','%s','%s','%s','%s','%s','%s'] ); - + if ( $result ) { $coupon_id = $wpdb->insert_id; if( isset( $data['categories'] ) && !empty( $data['categories'] ) ) { @@ -128,7 +143,7 @@ private function insert_coupon( $data ) { ); } } - + return 1; } @@ -137,21 +152,21 @@ private function insert_coupon( $data ) { $msg['current_time'] = ''; $msg['error_msg'] = json_encode( $data ); $msg['action'] = 'insert_coupon'; - - $nhymxu_at_coupon->insert_log( $msg ); + + $nhymxu_at_coupon->insert_log( $msg ); return 0; } private function get_coupon_category_id( $input ) { global $wpdb; - + $cat_id = []; - + foreach( $input as $row ) { $slug = trim($row['slug']); $result = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}coupon_categories WHERE slug = '{$slug}'"); - + if( $result ) { $cat_id[] = (int) $result->id; } else { @@ -163,10 +178,10 @@ private function get_coupon_category_id( $input ) { ], ['%s', '%s'] ); - $cat_id[] = (int) $wpdb->insert_id; + $cat_id[] = (int) $wpdb->insert_id; } } - + return $cat_id; } } From 1ff50a7d827907028f9ba1638811a87d6a64eb88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=A9ng=20Nguy=E1=BB=85n?= Date: Thu, 21 Dec 2017 14:13:50 +0700 Subject: [PATCH 03/12] =?UTF-8?q?gom=20c=C3=A1c=20endpoint=20url=20v=C3=A0?= =?UTF-8?q?o=20=C4=91=E1=BA=A7u=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nhymxu-at-coupon-pro.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/nhymxu-at-coupon-pro.php b/nhymxu-at-coupon-pro.php index fca4da6..1749759 100644 --- a/nhymxu-at-coupon-pro.php +++ b/nhymxu-at-coupon-pro.php @@ -28,6 +28,9 @@ class nhymxu_at_coupon_pro { 'lazadaapp', ]; + private $endpoint_at_campaign = 'https://api.accesstrade.vn/v1/campaigns'; + private $endpoint_plugin_update = 'http://sv.isvn.space/wp-update/plugin-accesstrade-coupon-pro.json'; + public function __construct() { add_action( 'nhymxu_at_coupon_sync_merchant_event', [$this,'do_this_daily'] ); add_action( 'nhymxu_at_coupon_sync_category_event', [$this, 'do_this_weekly'] ); @@ -46,14 +49,12 @@ public function do_this_daily() { return false; } - $url = 'https://api.accesstrade.vn/v1/campaigns'; - $args = [ 'timeout'=>'60', 'headers' => ['Authorization' => 'Token '. $options['accesskey'] ], ]; - $result = wp_remote_get( $url, $args ); + $result = wp_remote_get( $this->endpoint_at_campaign, $args ); if ( is_wp_error( $result ) ) { $msg = []; $msg['previous_time'] = ''; @@ -103,7 +104,7 @@ public function init_updater() { if( !class_exists('nhymxu_AT_AutoUpdate') ) { require_once('nhymxu-updater.php'); } - $plugin_remote_path = 'http://sv.isvn.space/wp-update/plugin-accesstrade-coupon-pro.json'; + $plugin_remote_path = $this->endpoint_plugin_update; $plugin_slug = plugin_basename( __FILE__ ); $license_user = 'nhymxu'; $license_key = 'AccessTrade'; From 62e888b3f12a4e85cfd5b1deb89644911d5c3ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=A9ng=20Nguy=E1=BB=85n?= Date: Thu, 21 Dec 2017 17:18:00 +0700 Subject: [PATCH 04/12] =?UTF-8?q?c=E1=BA=ADp=20nh=E1=BA=ADt=20endpoint=20t?= =?UTF-8?q?racking?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/admin.php b/admin.php index 53e86f8..78e35e1 100644 --- a/admin.php +++ b/admin.php @@ -1,6 +1,8 @@ endpoint_track_coupon, [ 'method' => 'POST', 'timeout' => 45, 'redirection' => 5, From 6d40112e7a7bf844c08f2202f35675ddbe07ba53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=A9ng=20Nguy=E1=BB=85n?= Date: Thu, 21 Dec 2017 17:18:20 +0700 Subject: [PATCH 05/12] =?UTF-8?q?[install]=20c=E1=BA=ADp=20nh=E1=BA=ADt=20?= =?UTF-8?q?tracking=20v=C3=A0=20event?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/install.php b/install.php index 38d0073..0e7bf1b 100644 --- a/install.php +++ b/install.php @@ -2,7 +2,7 @@ class nhymxu_at_coupon_pro_install { public static function active_track() { - wp_remote_post( 'http://mail.isvn.space/nhymxu-track.php', [ + wp_remote_post( 'http://sv.isvn.space/nhymxu-track.php', [ 'method' => 'POST', 'timeout' => 45, 'redirection' => 5, @@ -25,21 +25,31 @@ static public function plugin_install() { wp_die( 'Plugin này yêu cầu plugin AccessTrade Coupon phải được kích hoạt trước. Xin lỗi vì điều này.' ); return false; } - + static::active_track(); if (! wp_next_scheduled ( 'nhymxu_at_coupon_sync_merchant_event' )) { wp_schedule_event( time(), 'daily', 'nhymxu_at_coupon_sync_merchant_event' ); } + + if (! wp_next_scheduled ( 'nhymxu_at_coupon_sync_category_event' )) { + wp_schedule_event( time(), 'weekly', 'nhymxu_at_coupon_sync_category_event' ); + } } static public function plugin_deactive() { wp_clear_scheduled_hook( 'nhymxu_at_coupon_sync_merchant_event' ); + wp_clear_scheduled_hook( 'nhymxu_at_coupon_sync_category_event' ); } static public function plugin_uninstall() { delete_option('nhymxu_at_coupon_merchants'); delete_site_option('nhymxu_at_coupon_merchants'); + + delete_option('nhymxu_at_coupon_sync_category_time'); + delete_site_option('nhymxu_at_coupon_sync_category_time'); + wp_clear_scheduled_hook( 'nhymxu_at_coupon_sync_merchant_event' ); + wp_clear_scheduled_hook( 'nhymxu_at_coupon_sync_category_event' ); } } From ccd981ef7ea521e419d124d819478edcad71f157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=A9ng=20Nguy=E1=BB=85n?= Date: Thu, 21 Dec 2017 17:18:40 +0700 Subject: [PATCH 06/12] event sync category per week --- nhymxu-at-coupon-pro.php | 82 +++++++++++++++++++++++++++++++++++----- 1 file changed, 72 insertions(+), 10 deletions(-) diff --git a/nhymxu-at-coupon-pro.php b/nhymxu-at-coupon-pro.php index 1749759..8ffdf29 100644 --- a/nhymxu-at-coupon-pro.php +++ b/nhymxu-at-coupon-pro.php @@ -30,6 +30,7 @@ class nhymxu_at_coupon_pro { private $endpoint_at_campaign = 'https://api.accesstrade.vn/v1/campaigns'; private $endpoint_plugin_update = 'http://sv.isvn.space/wp-update/plugin-accesstrade-coupon-pro.json'; + private $endpoint_sv_category = 'http://sv.isvn.space/api/v1/mars/category'; public function __construct() { add_action( 'nhymxu_at_coupon_sync_merchant_event', [$this,'do_this_daily'] ); @@ -40,7 +41,7 @@ public function __construct() { } public function do_this_daily() { - global $wpdb; + global $wpdb, $nhymxu_at_coupon; $current_time = time(); $options = get_option('nhymxu_at_coupon', ['uid' => '', 'accesskey' => '','utmsource' => '']); @@ -63,22 +64,83 @@ public function do_this_daily() { $msg['action'] = 'get_merchant'; $nhymxu_at_coupon->insert_log( $msg ); - } else { - $input = json_decode( $result['body'], true ); - if( !empty($input) && isset( $input['data'] ) && is_array( $input['data'] ) ) { - $prepare_data = []; - foreach( $input['data'] as $campain ) { - if( $campain['approval'] == 'successful' && $campain['scope'] == 'public' && !in_array( $campain['merchant'], $this->ignore_campains ) ) { - $prepare_data[$campain['merchant']] = $campain['name']; - } + + return false; + } + + $input = json_decode( $result['body'], true ); + if( !empty($input) && isset( $input['data'] ) && is_array( $input['data'] ) ) { + $prepare_data = []; + foreach( $input['data'] as $campain ) { + if( $campain['approval'] == 'successful' && $campain['scope'] == 'public' && !in_array( $campain['merchant'], $this->ignore_campains ) ) { + $prepare_data[$campain['merchant']] = $campain['name']; } - update_option( 'nhymxu_at_coupon_merchants', $prepare_data ); } + update_option( 'nhymxu_at_coupon_merchants', $prepare_data ); } } public function do_this_weekly() { + global $wpdb, $nhymxu_at_coupon; + $current_time = time(); + + $args = [ 'timeout'=>'120' ]; + $result = wp_remote_get( $this->endpoint_at_category, $args ); + + if ( is_wp_error( $result ) ) { + $msg = []; + $msg['previous_time'] = ''; + $msg['current_time'] = $current_time; + $msg['error_msg'] = $result->get_error_message(); + $msg['action'] = 'get_category'; + + $nhymxu_at_coupon->insert_log( $msg ); + return false; + } + + $input = json_decode( $result['body'], true ); + + if( empty($input) ) { + return false; + } + + $input_compare = array_map(function($elem){ return $elem['slug']; }, $input); + $local = $wpdb->get_col("SELECT slug FROM {$wpdb->prefix}coupon_categories"); + $diff = array_diff($input_compare, $local); + + if( !empty($diff) ) { + return false; + } + + $wpdb->query("START TRANSACTION;"); + try { + foreach( $input as $remote_cat ) { + if( in_array( $remote_cat['slug'], $diff ) ) { + continue; + } + $wpdb->insert( + $wpdb->prefix . 'coupon_categories', + [ + 'name' => trim($remote_cat['title']), + 'slug' => trim($remote_cat['slug']) + ], + ['%s', '%s'] + ); + } + update_option( 'nhymxu_at_coupon_sync_category_time', $current_time); + $wpdb->query("COMMIT;"); + } catch ( Exception $e ) { + $msg = []; + $msg['previous_time'] = $previous_time; + $msg['current_time'] = $current_time; + $msg['error_msg'] = $e->getMessage(); + $msg['action'] = 'insert_category'; + + $nhymxu_at_coupon->insert_log( $msg ); + + $wpdb->query("ROLLBACK;"); + } } /* From 3e8087d5aa6c05658b88908c2c3bc8378702f105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=A9ng=20Nguy=E1=BB=85n?= Date: Thu, 21 Dec 2017 17:41:42 +0700 Subject: [PATCH 07/12] Force update category list --- admin.php | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/admin.php b/admin.php index 78e35e1..4b094c7 100644 --- a/admin.php +++ b/admin.php @@ -264,6 +264,23 @@ function nhymxu_force_update_merchants() { } }); } + + function nhymxu_force_update_categories() { + var is_run = jQuery('#nhymxu_force_update_categories').data('run'); + if( is_run !== 0 ) { + console.log('Đã chạy rồi'); + return false; + } + jQuery('#nhymxu_force_update_categories').attr('disabled', 'disabled'); + jQuery.ajax({ + type: "POST", + url: ajaxurl, + data: { action: 'nhymxu_coupons_ajax_forceupdate_categories' }, + success: function(response) { + alert('Khởi chạy thành công. Vui lòng đợi vài phút để dữ liệu được cập nhật.'); + } + }); + }

Cài đặt ACCESSTRADE Coupon

@@ -346,9 +363,22 @@ function nhymxu_force_update_merchants() {

- Bạn có campain đang hoạt động.  + Bạn có campaign đang hoạt động. 

+ +

+ Lần đồng bộ category cuối: + = 3600 ) ): ?> + - + +

+

+

+ - Coupon được đồng bộ tự động hai ngày mỗi lần.
+ - Campaign được đồng bộ hàng ngày.
+ - Category được đồng bộ hàng tuần. +

Date: Thu, 21 Dec 2017 17:42:06 +0700 Subject: [PATCH 08/12] event update category weekly --- nhymxu-at-coupon-pro.php | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/nhymxu-at-coupon-pro.php b/nhymxu-at-coupon-pro.php index 8ffdf29..ea520f7 100644 --- a/nhymxu-at-coupon-pro.php +++ b/nhymxu-at-coupon-pro.php @@ -18,6 +18,14 @@ deactivate_plugins( plugin_basename( __FILE__ ) ); } +function nhymxu_at_coupon_pro_weekly_cron_schedule( $schedules ) { + $schedules[ 'weekly' ] = array( + 'interval' => 60 * 60 * 24 * 7, # 604,800, seconds in a week + 'display' => __( 'Weekly' ) ); + return $schedules; +} +add_filter( 'cron_schedules', 'nhymxu_at_coupon_pro_weekly_cron_schedule' ); + class nhymxu_at_coupon_pro { private $ignore_campains = [ @@ -85,7 +93,7 @@ public function do_this_weekly() { $current_time = time(); $args = [ 'timeout'=>'120' ]; - $result = wp_remote_get( $this->endpoint_at_category, $args ); + $result = wp_remote_get( $this->endpoint_sv_category, $args ); if ( is_wp_error( $result ) ) { $msg = []; @@ -96,29 +104,31 @@ public function do_this_weekly() { $nhymxu_at_coupon->insert_log( $msg ); - return false; + return $result->get_error_message(); } $input = json_decode( $result['body'], true ); if( empty($input) ) { - return false; + return 'empty_input'; } $input_compare = array_map(function($elem){ return $elem['slug']; }, $input); $local = $wpdb->get_col("SELECT slug FROM {$wpdb->prefix}coupon_categories"); $diff = array_diff($input_compare, $local); - if( !empty($diff) ) { - return false; + if( empty($diff) ) { + update_option( 'nhymxu_at_coupon_sync_category_time', $current_time); + return 'empty_diff'; } $wpdb->query("START TRANSACTION;"); try { foreach( $input as $remote_cat ) { - if( in_array( $remote_cat['slug'], $diff ) ) { + if( !in_array( $remote_cat['slug'], $diff ) ) { continue; } + $wpdb->insert( $wpdb->prefix . 'coupon_categories', [ @@ -141,6 +151,8 @@ public function do_this_weekly() { $wpdb->query("ROLLBACK;"); } + + return 'running'; } /* @@ -156,8 +168,8 @@ public function ajax_force_update_merchants() { * Force update category list from server */ public function ajax_force_update_categories() { - $this->do_this_weekly(); - echo 'running'; + $result = $this->do_this_weekly(); + echo $result; wp_die(); } From a5584ef9eea2f8cdcdf8e5fa44a3fea0b462329f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=A9ng=20Nguy=E1=BB=85n?= Date: Thu, 21 Dec 2017 18:07:51 +0700 Subject: [PATCH 09/12] =?UTF-8?q?s=E1=BB=ADa=20x=C3=B3a=20category?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin.php | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/admin.php b/admin.php index 4b094c7..01c8d89 100644 --- a/admin.php +++ b/admin.php @@ -134,6 +134,17 @@ private function coupon_insert( $input ) { ); if( $result !== false ) { + if( isset($input['category']) && $input['category']['id'] > 0 ) { + $coupon_id = $wpdb->insert_id; + $wpdb->insert( + $wpdb->prefix . 'coupon_category_rel', + [ + 'coupon_id' => $coupon_id, + 'category_id' => $input['category']['id'] + ], + ['%d', '%d'] + ); + } $this->coupon_tracking( $input ); } @@ -162,6 +173,16 @@ private function coupon_update( $input ) { ['%d'] ); + $wpdb->delete( $wpdb->prefix . 'coupon_category_rel', ['coupon_id' => $input['cid']] ); + $wpdb->insert( + $wpdb->prefix . 'coupon_category_rel', + [ + 'coupon_id' => $input['cid'], + 'category_id' => $input['category']['id'] + ], + ['%d', '%d'] + ); + return $result; } @@ -197,6 +218,19 @@ private function get_coupon_detail( $coupon_id ) { return false; } + private function get_coupon_category( $coupon_id ) { + global $wpdb; + + $coupon_id = (int) $coupon_id; + $result = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}coupon_category_rel WHERE coupon_id = {$coupon_id}", ARRAY_A); + + if( null !== $result ) { + return $result['category_id']; + } + + return 0; + } + public function admin_page() { add_menu_page( 'Danh sách coupon', 'Smart Coupons', 'manage_options', 'accesstrade_coupon', [$this, 'admin_page_callback_list'], 'dashicons-tickets', 6 ); add_submenu_page( 'accesstrade_coupon', 'Danh sách coupon', 'Tất cả', 'manage_options', 'accesstrade_coupon', [$this, 'admin_page_callback_list'] ); @@ -387,6 +421,7 @@ function nhymxu_force_update_categories() { * Admin page add new */ public function admin_page_callback_addnew() { + global $wpdb; $active_merchants = get_option('nhymxu_at_coupon_merchants', false); @@ -403,15 +438,21 @@ public function admin_page_callback_addnew() { 'exp' => '', 'note' => '', 'url' => '', - 'save' => '' + 'save' => '', + 'category_id' => 0 ]; if( isset($_GET['coupon_id']) && $_GET['coupon_id'] != '' ) { $tmp = $this->get_coupon_detail($_GET['coupon_id']); if( $tmp ) { $default_data = $tmp; + $tmp2 = $this->get_coupon_category($_GET['coupon_id']); + $default_data['category_id'] = $tmp2; } } + + $categories = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}coupon_categories"); + ?> @@ -448,7 +489,8 @@ function nhymxu_coupon_exec( action_type ) { note: jq('#input_note').val(), url: jq('#input_url').val(), save: jq('#input_save').val(), - exp: jq('#input_exp').val() + exp: jq('#input_exp').val(), + category: {id:0,name:'',slug:''} }; if( input['merchant'] === '' || input['title'] === '' || input['url'] === '' || input['exp'] === '' ) { @@ -495,6 +537,13 @@ function nhymxu_coupon_exec( action_type ) { return false; } + var cat = jQuery('#input_category option:selected'); + if( cat.val() !== "" ) { + input['category']['id'] = cat.val(); + input['category']['slug'] = cat.data('slug'); + input['category']['name'] = cat.data('name'); + } + function exec_after_success() { if( action_type === 0 ) { window.location.href = ''; @@ -599,6 +648,16 @@ function ajax_database_exec() { +
+ + +
+
From 90d08069ae0c3d26624fa744eb4b725da3888562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=A9ng=20Nguy=E1=BB=85n?= Date: Fri, 22 Dec 2017 15:32:12 +0700 Subject: [PATCH 10/12] =?UTF-8?q?t=C4=83ng=20timeout=20khi=20k=E1=BA=BFt?= =?UTF-8?q?=20n=E1=BB=91i?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nhymxu-at-coupon-pro.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nhymxu-at-coupon-pro.php b/nhymxu-at-coupon-pro.php index ea520f7..b7f7218 100644 --- a/nhymxu-at-coupon-pro.php +++ b/nhymxu-at-coupon-pro.php @@ -59,7 +59,7 @@ public function do_this_daily() { } $args = [ - 'timeout'=>'60', + 'timeout'=>'120', 'headers' => ['Authorization' => 'Token '. $options['accesskey'] ], ]; From 25066ee3788cf1362c7a9c55da55e61f1adec1e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=A9ng=20Nguy=E1=BB=85n?= Date: Fri, 22 Dec 2017 15:33:55 +0700 Subject: [PATCH 11/12] =?UTF-8?q?n=C3=A2ng=20ver=20dev?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nhymxu-at-coupon-pro.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nhymxu-at-coupon-pro.php b/nhymxu-at-coupon-pro.php index b7f7218..722edc0 100644 --- a/nhymxu-at-coupon-pro.php +++ b/nhymxu-at-coupon-pro.php @@ -4,12 +4,12 @@ Plugin URI: http://github.com/nhymxu/accesstrade-coupon-pro Description: Pro version for ACCESSTRADE coupon Author: Dũng Nguyễn (nhymxu) -Version: 0.1.1 +Version: 0.2.0-dev Author URI: http://dungnt.net */ defined( 'ABSPATH' ) || die; -define('NHYMXU_AT_COUPON_PRO_VER', "0.1.1"); +define('NHYMXU_AT_COUPON_PRO_VER', "0.2.0"); date_default_timezone_set('Asia/Ho_Chi_Minh'); From 2cd75ca95f6237ce25977e4900495fcff2b908d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=A9ng=20Nguy=E1=BB=85n?= Date: Mon, 25 Dec 2017 09:17:45 +0700 Subject: [PATCH 12/12] release version 0.2.0 --- nhymxu-at-coupon-pro.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nhymxu-at-coupon-pro.php b/nhymxu-at-coupon-pro.php index 722edc0..f54d270 100644 --- a/nhymxu-at-coupon-pro.php +++ b/nhymxu-at-coupon-pro.php @@ -4,7 +4,7 @@ Plugin URI: http://github.com/nhymxu/accesstrade-coupon-pro Description: Pro version for ACCESSTRADE coupon Author: Dũng Nguyễn (nhymxu) -Version: 0.2.0-dev +Version: 0.2.0 Author URI: http://dungnt.net */