AffinityCrontab.php
5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
class AffinityCrontab {
public function createCrontabHooks() {
add_action('wp_affinity_cron', array($this, 'cron'));
add_action('wp_affinity_cron_inv', array($this, 'cron_inv'));
add_action('wp_affinity_cron_sync_all', array($this, 'cron_sync_all'));
add_action('wp_affinity_cron_orders', array($this, 'cron_orders'));
}
public static function createInitialCrontab() {
wp_schedule_event(time() + 600, get_option('ebayaffinity_pushinvenorytime'), 'wp_affinity_cron');
wp_schedule_event(time() + 1200, get_option('ebayaffinity_pushinvenorytime'), 'wp_affinity_cron_inv');
wp_schedule_event(time() + 600, 'hourly', 'wp_affinity_cron_orders');
wp_schedule_event(time() + 600 + 900, 'hourly', 'wp_affinity_cron_orders');
wp_schedule_event(time() + 600 + 1800, 'hourly', 'wp_affinity_cron_orders');
wp_schedule_event(time() + 600 + 2700, 'hourly', 'wp_affinity_cron_orders');
}
public static function clearCrontab() {
wp_clear_scheduled_hook('wp_affinity_cron');
wp_clear_scheduled_hook('wp_affinity_cron_inv');
wp_clear_scheduled_hook('wp_affinity_cron_sync_all');
wp_clear_scheduled_hook('wp_affinity_cron_orders');
}
public static function cron_orders() {
$setup1 = get_option('ebayaffinity_setup1');
$setup2 = get_option('ebayaffinity_setup2');
$setup3 = get_option('ebayaffinity_setup3');
$setup4 = get_option('ebayaffinity_setup4');
if ((!empty($setup1)) && (!empty($setup2)) && (!empty($setup3)) && (!empty($setup4))) {
$orders_expire = get_option('affinity_orders_expire');
if (empty($orders_expire) || $orders_expire < time()) {
update_option('affinity_orders_expire', time() + 600);
require_once(__DIR__.'/../service/AffinityLocalUpdateService.php');
$arrResult = AffinityLocalUpdateService::orderNotificationReceived();
}
}
}
public static function cron_suggestions() {
global $wpdb;
require_once(__DIR__.'/../model/AffinityEbayInventory.php');
require_once(__DIR__ ."/../service/AffinityBackendService.php");
require_once(__DIR__ ."/../service/AffinityEnc.php");
$prods = AffinityEbayInventory::getBySearchCategory('', 0, 1, '', '', 'prodtwosets', 3, 100, false);
$backend = get_option('ebayaffinity_backend');
$token = AffinityEnc::getToken();
if ((!empty($backend)) && (!empty($token))) {
foreach ($prods[0] as $prod) {
$product = new WC_Product($prod['id']);
$attrs = $product->get_attributes();
update_post_meta($prod['id'], '_affinity_suggestedCatId', '0');
$json = AffinityBackendService::getSuggestionForTitle($prod['title']);
$a = array();
if (!empty($json['data'])) {
$c = array();
foreach ($json['data'] as $b) {
$c[] = intval($b);
}
$categoryIds = $wpdb->get_results("SELECT categoryId FROM ".$wpdb->prefix."ebayaffinity_categories WHERE categoryId IN (" . implode(',', $c) . ") ");
if (!empty($categoryIds)) {
foreach ($categoryIds as $b) {
$a[] = $b->categoryId;
}
}
if (!empty($a)) {
update_post_meta($prod['id'], '_affinity_suggestedCatId', implode(',', $a));
}
}
}
}
}
public static function cron_cat_suggestions() {
require_once(__DIR__.'/../model/AffinityEbayInventory.php');
require_once(__DIR__.'/../model/AffinityEbayCategory.php');
$cats = AffinityEbayInventory::categoryset(true, true);
$ecats = AffinityEbayCategory::getAlleBay();
foreach ($cats as $k=>$cat) {
if (empty($cat[1]) && empty($cat[2])) {
$sname = AffinityEbayCategory::showWooCatName($cats, $k);
$sname = preg_replace('/[^A-Za-z0-9>]/', ' ', $sname);
$sname = str_replace(' ', ' ', $sname);
$sname = str_replace(' ', ' ', $sname);
$sname = trim($sname);
$asname = explode(' > ', $sname);
$suggecatid = array();
foreach ($ecats as $ecat) {
$ecat->catname = ' '.str_replace("'", '', $ecat->catname).' ';
$ecat->catname = preg_replace('/[^A-Za-z0-9>]/', ' ', $ecat->catname);
$ecat->catname = str_replace(' ', ' ', $ecat->catname);
$ecat->catname = str_replace(' ', ' ', $ecat->catname);
$match = true;
foreach ($asname as $asnam) {
$asnam = ' '.str_replace("'", '', $asnam).' ';
if (stripos($ecat->catname, $asnam) === false) {
$match = false;
}
}
if ($match) {
$suggecatid[] = $ecat->categoryId;
if (count($suggecatid) >= 5) {
break;
}
}
}
if (!empty($suggecatid)) {
update_term_meta($k, '_affinity_suggestedCatId', implode(',', $suggecatid));
}
}
}
}
public static function cron() {
$setup_ebayuserid = get_option('ebayaffinity_ebayuserid');
$setup_token = get_option('affinityPushAccessToken');
self::cron_cat_suggestions();
$setup0 = (!empty($setup_ebayuserid)) && (!empty($setup_token));
if (!empty($setup0)) {
self::cron_suggestions();
}
}
public static function cron_inv() {
$setup1 = get_option('ebayaffinity_setup1');
$setup2 = get_option('ebayaffinity_setup2');
$setup3 = get_option('ebayaffinity_setup3');
$setup4 = get_option('ebayaffinity_setup4');
$setup5 = get_option('ebayaffinity_setup5');
if ((!empty($setup1)) && (!empty($setup2)) && (!empty($setup3)) && (!empty($setup4)) && (!empty($setup5))) {
require_once(__DIR__ . "/AffinityEcommerceProduct.php");
require_once(__DIR__.'/../model/AffinityEbayInventory.php');
AffinityEbayInventory::publishUpdates();
}
}
public static function cron_sched_sync_all() {
$t = wp_next_scheduled('wp_affinity_cron_sync_all');
if ($t === false || ($t - time() > 300) || $t - time() < 0) {
wp_clear_scheduled_hook('wp_affinity_cron_sync_all');
wp_schedule_single_event(time() + 300, 'wp_affinity_cron_sync_all');
}
}
public static function cron_sync_all() {
require_once(__DIR__.'/../model/AffinityEbayInventory.php');
AffinityEbayInventory::syncAll();
}
}