AffinityAjaxService.php
7.11 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
class AffinityAjaxService {
public function init() {
add_action('wp_ajax_blockunblock', array($this, 'ajax_blockunblock'));
add_action('wp_ajax_ebayproducts', array($this, 'ajax_ebayproducts'));
add_action('wp_ajax_ebaycategories', array($this, 'ajax_ebaycategories'));
add_action('wp_ajax_ebayitemspecificmapping', array($this, 'ajax_ebayitemspecificsmapping'));
add_action('wp_ajax_ebaysubcategories', array($this, 'ajax_ebaysubcategories'));
add_action('wp_ajax_ebaytitlerules', array($this, 'ajax_ebaytitlerules'));
add_action('wp_ajax_ebaytitlerules_upsertrule', array($this, 'ajax_ebaytitlerules_upsertrule'));
add_action('wp_ajax_affinity_product_shiprule', array($this, 'ajax_affinity_product_shiprule'));
add_action('wp_ajax_affinity_category_shiprule', array($this, 'ajax_affinity_category_shiprule'));
add_action('wp_ajax_nopriv_createorder', array($this, 'ajax_createorder'));
add_action('wp_ajax_nopriv_croninv', array($this, 'ajax_croninv'));
add_action('wp_ajax_nopriv_cron', array($this, 'ajax_cron'));
add_action('wp_ajax_nopriv_cron_sync_all', array($this, 'ajax_cron_sync_all'));
}
function ajax_blockunblock() {
require_once(__DIR__.'/../model/AffinityEbayInventory.php');
if (isset($_POST['id']) && isset($_POST['blocked'])) {
if ($_POST['blocked'] == '1') {
$arr = explode(',', $_POST['id']);
foreach ($arr as $el) {
update_post_meta($el, '_affinity_block', '1');
}
} else {
$arr = explode(',', $_POST['id']);
foreach ($arr as $el) {
delete_post_meta($el, '_affinity_block');
}
}
$arr = explode(',', $_POST['id']);
foreach ($arr as $el) {
update_post_meta($_POST['id'], '_affinity_prod_update_status', '1');
}
}
}
function ajax_ebayproducts() {
require_once(__DIR__.'/../model/AffinityEbayInventory.php');
require_once(__DIR__.'/../model/AffinityEbayCategory.php');
$_POST['s'] = stripslashes($_POST['s']);
if (!isset($_POST['key'])) {
$_POST['key'] = null;
}
if (!isset($_POST['value'])) {
$_POST['value'] = null;
}
if (!isset($_POST['exkey'])) {
$_POST['exkey'] = null;
}
if (!isset($_POST['exvalue'])) {
$_POST['exvalue'] = null;
}
if ($_POST['exkey'] === 'prodtwosets') {
$catrules_cats = AffinityEbayCategory::getCategoriesCatRules();
$catsRules = array();
foreach ($catrules_cats as $k=>$catrules_cat) {
if (!empty($catrules_cat[1])) {
$catsRules[$k] = $k;
}
}
if ($_POST['exvalue'] == 0) {
$ret = array(
AffinityEbayInventory::getBySearchCategory($_POST['s'], $_POST['categoryId'], $_POST['paged'], $_POST['key'], $_POST['value'], $_POST['exkey'], 1, 20, true, $catsRules),
AffinityEbayInventory::getBySearchCategory($_POST['s'], $_POST['categoryId'], 1, $_POST['key'], $_POST['value'], $_POST['exkey'], 2, 1, false, $catsRules)
);
} else {
$ret = array(
AffinityEbayInventory::getBySearchCategory($_POST['s'], $_POST['categoryId'], 1, $_POST['key'], $_POST['value'], $_POST['exkey'], 1, 1, true, $catsRules),
AffinityEbayInventory::getBySearchCategory($_POST['s'], $_POST['categoryId'], $_POST['paged'], $_POST['key'], $_POST['value'], $_POST['exkey'], 2, 20, false, $catsRules)
);
}
print json_encode($ret);
} else {
print json_encode(AffinityEbayInventory::getBySearchCategory($_POST['s'], $_POST['categoryId'], $_POST['paged'], $_POST['key'], $_POST['value'], $_POST['exkey'], $_POST['exvalue']));
}
wp_die();
}
function ajax_ebaycategories() {
require_once(__DIR__.'/../model/AffinityEbayCategory.php');
print json_encode(AffinityEbayCategory::getByParent(intval($_POST['id'])));
wp_die();
}
function ajax_ebaysubcategories() {
require_once(__DIR__.'/../model/AffinityEbayCategory.php');
print json_encode(AffinityEbayCategory::getSubsByParent(intval($_POST['id'])));
wp_die();
}
function ajax_ebayitemspecificsmapping() {
if (!current_user_can( 'manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}
require_once(__DIR__ . '/../model/AffinityItemSpecificMapping.php');
AffinityItemSpecificMapping::saveFromJson($_POST['obj']);
require_once(__DIR__ . "/../ecommerce-adapters/AffinityCrontab.php");
AffinityCrontab::cron_sched_sync_all();
wp_die();
}
function ajax_ebaytitlerules() {
require_once(__DIR__.'/../model/AffinityTitleRule.php');
print json_encode(AffinityTitleRule::getAllRules());
wp_die();
}
function ajax_ebaytitlerules_upsertrule() {
require_once(__DIR__.'/../model/AffinityTitleRule.php');
require_once(__DIR__.'/../ecommerce-adapters/AffinityEcommerceCategory.php');
$obj = new AffinityTitleRule();
if (!empty($_POST['id'])) {
$obj->_id = $_POST['id'];
}
if (!empty($_POST['titleTemplate'])) {
$obj->_titleTemplate = stripslashes($_POST['titleTemplate']);
}
if (!empty($_POST['arrEbayCategoriesToApply'])) {
$obj->_arrEbayCategoriesToApply = explode(',', $_POST['arrEbayCategoriesToApply']);
}
if (!empty($_POST['arrProductsToApply'])) {
$obj->_arrProductsToApply = explode(',', $_POST['arrProductsToApply']);;
}
print $obj->upsert();
wp_die();
}
function ajax_affinity_category_shiprule() {
if (isset($_POST['id']) && isset($_POST['rule'])) {
if (!empty($_POST['rule'])) {
update_term_meta($_POST['id'], '_affinity_shiprule', intval($_POST['rule']));
} else {
delete_term_meta($_POST['id'], '_affinity_shiprule');
}
}
}
function ajax_createorder() {
require_once(__DIR__.'/AffinityLocalUpdateService.php');
$token = $_GET['aT'];
$arrResult = AffinityLocalUpdateService::orderNotificationReceivedToken($token);
@ob_end_clean();
echo json_encode($arrResult);
exit();
}
function ajax_cron_sync_all() {
require_once(__DIR__.'/AffinityLocalUpdateService.php');
$token = $_GET['aT'];
$arrResult = AffinityLocalUpdateService::cronSyncAll($token);
@ob_end_clean();
echo json_encode($arrResult);
exit();
}
function ajax_croninv() {
$t = wp_next_scheduled('wp_affinity_cron_inv');
$a = wp_get_schedule('wp_affinity_cron_inv');
$s = wp_get_schedules();
$diff = $s[$a]['interval'] - ($t - time());
if ($diff < 120 && $diff > -120) {
print '{"not working":"not working"}';
exit();
}
require_once(__DIR__.'/AffinityLocalUpdateService.php');
$token = $_GET['aT'];
$arrResult = AffinityLocalUpdateService::cronInv($token);
@ob_end_clean();
echo json_encode($arrResult);
exit();
}
function ajax_cron() {
$t = wp_next_scheduled('wp_affinity_cron');
$a = wp_get_schedule('wp_affinity_cron');
$s = wp_get_schedules();
$diff = $s[$a]['interval'] - ($t - time());
if ($diff < 120 && $diff > -120) {
print '{"not working":"not working"}';
exit();
}
require_once(__DIR__.'/AffinityLocalUpdateService.php');
$token = $_GET['aT'];
$arrResult = AffinityLocalUpdateService::cron($token);
@ob_end_clean();
echo json_encode($arrResult);
exit();
}
function ajax_affinity_product_shiprule() {
if (isset($_POST['id']) && isset($_POST['rule'])) {
if (!empty($_POST['rule'])) {
update_post_meta($_POST['id'], '_affinity_shiprule', intval($_POST['rule']));
} else {
delete_post_meta($_POST['id'], '_affinity_shiprule');
}
}
}
}