PluginProbe
AliNext – WooCommerce Dropshipping Plugin for AliExpress / trunk
AliNext – WooCommerce Dropshipping Plugin for AliExpress vtrunk
ali2woo-lite / includes / classes / controller / SettingPageController.php

SettingPageController.php in AliNext – WooCommerce Dropshipping Plugin for AliExpress trunk, at includes/classes/controller/SettingPageController.php

596 lines 22.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Description of SettingPageController
5 *
6 * @author Ali2Woo Team
7 *
8 * @autoload: a2wl_admin_init
9 */
10
11 namespace AliNext_Lite;;
12
13 use Pages;
14
15 class SettingPageController extends AbstractAdminPage
16 {
17 public const FIELD_FIELD_NO_AVATAR_PHOTO = 'a2wl_review_noavatar_photo';
18
19 public const SETTING_COMMON = 'common';
20 public const SETTING_VIDEO = 'video';
21 public const SETTING_SHIPPING = 'shipping';
22
23 public function __construct(
24 protected LocalService $LocalService,
25 protected CommonSettingService $CommonSettingService,
26 protected ShippingSettingService $ShippingSettingService,
27 protected PermanentAlertService $PermanentAlertService,
28 protected TipOfDayService $TipOfDayService,
29
30 ) {
31 parent::__construct(
32 Pages::getLabel(Pages::SETTINGS),
33 Pages::getLabel(Pages::SETTINGS),
34 Capability::pluginAccess(),
35 Pages::SETTINGS,
36 30
37 );
38
39 add_filter('a2wl_setting_view', [$this, 'setting_view']);
40 add_filter('a2wl_configure_lang_data', array($this, 'configure_lang_data'));
41
42 $this->add_style(
43 'a2wl-settings-common',
44 '/assets/css/pages/settings-common.css',
45 ['a2wl-admin-style']
46 );
47 }
48
49 public function configure_lang_data($lang_data)
50 {
51 if ($this->is_current_page()) {
52 $lang_data = array(
53 /* translators: %d is replaced with "digit" */
54 'process_loading_d_of_d_erros_d' => _x('Process loading %d of %d. Errors: %d.', 'Status', 'ali2woo'),
55 /* translators: %d is replaced with "digit" */
56 'load_button_text' => _x('Load %d images', 'Status', 'ali2woo'),
57 'all_images_loaded_text' => _x('All images loaded', 'Status', 'ali2woo'),
58 'leave_blank_to_allow_all_countries' => esc_html__('leave blank to allow all countries', 'ali2woo'),
59 );
60 }
61 return $lang_data;
62 }
63
64 public function render($params = []): void
65 {
66 if (!empty($_POST)) {
67 check_admin_referer(self::PAGE_NONCE_ACTION, self::NONCE);
68 }
69
70 if (!PageGuardHelper::canAccessPage(Pages::SETTINGS)) {
71 wp_die($this->getErrorTextNoPermissions());
72 }
73
74 $current_module = $_REQUEST['subpage'] ?? 'common';
75 $this->model_put("PermanentAlerts", $this->PermanentAlertService->getAll());
76 $this->model_put("TipOfDay", $this->TipOfDayService->getNextTip());
77 $this->model_put("modules", $this->getModules());
78 $this->model_put("current_module", $current_module);
79
80 $this->include_view([
81 "settings/settings_head.php",
82 apply_filters('a2wl_setting_view', $current_module),
83 "settings/settings_footer.php"
84 ]);
85 }
86
87 public function getModules(): array
88 {
89 return apply_filters('a2wl_setting_modules', [
90 ['id' => self::SETTING_COMMON, 'name' => esc_html__('Common settings', 'ali2woo')],
91 ['id' => self::SETTING_VIDEO, 'name' => esc_html__('Video settings', 'ali2woo')],
92 ['id' => 'account', 'name' => esc_html__('Account settings', 'ali2woo')],
93 ['id' => 'price_formula', 'name' => esc_html__('Pricing Rules', 'ali2woo')],
94 ['id' => 'reviews', 'name' => esc_html__('Reviews settings', 'ali2woo')],
95 ['id' => self::SETTING_SHIPPING, 'name' => esc_html__('Shipping settings', 'ali2woo')],
96 ['id' => 'phrase_filter', 'name' => esc_html__('Phrase Filtering', 'ali2woo')],
97 ['id' => 'chrome_api', 'name' => esc_html__('API Keys', 'ali2woo')],
98 ['id' => 'system_info', 'name' => esc_html__('System Info', 'ali2woo')],
99 ]);
100 }
101
102 public function setting_view($current_module): string
103 {
104 $view = "";
105 switch ($current_module) {
106 case self::SETTING_COMMON:
107 $view = $this->common_handle();
108 break;
109 case self::SETTING_VIDEO:
110 $view = $this->videoSettingsHandle();
111 break;
112 case 'account':
113 $view = $this->account_handle();
114 break;
115 case 'price_formula':
116 $view = $this->price_formula();
117 break;
118 case 'reviews':
119 $view = $this->reviews();
120 break;
121 case self::SETTING_SHIPPING:
122 $view = $this->shipping();
123 break;
124 case 'phrase_filter':
125 $view = $this->phrase_filter();
126 break;
127 case 'chrome_api':
128 $view = $this->chrome_api();
129 break;
130 case 'system_info':
131 $view = $this->system_info();
132 break;
133 }
134 return $view;
135 }
136
137 private function common_handle(): string
138 {
139 if (!empty($_POST)) {
140 check_admin_referer(self::PAGE_NONCE_ACTION, self::NONCE);
141 }
142
143 if (isset($_POST['setting_form'])) {
144 $this->CommonSettingService->handle();
145 }
146
147 $model = $this->CommonSettingService->collectModel();
148
149 foreach ($model as $key => $value) {
150 $this->model_put($key, $value);
151 }
152
153 return "settings/common.php";
154 }
155
156 private function videoSettingsHandle(): string
157 {
158 if (!empty($_POST)) {
159 check_admin_referer(self::PAGE_NONCE_ACTION, self::NONCE);
160 }
161
162 if (isset($_POST['setting_form'])) {
163 settings()->auto_commit(false);
164 set_setting(
165 Settings::SETTING_IMPORT_VIDEO,
166 isset($_POST['a2wl_' . Settings::SETTING_IMPORT_VIDEO])
167 );
168 set_setting(
169 Settings::SETTING_SHOW_PRODUCT_VIDEO_TAB,
170 isset($_POST['a2wl_' . Settings::SETTING_SHOW_PRODUCT_VIDEO_TAB])
171 );
172
173 if (isset($_POST['a2wl_' . Settings::SETTING_VIDEO_TAB_PRIORITY])) {
174 $videoTabPriority = intval($_POST['a2wl_' . Settings::SETTING_VIDEO_TAB_PRIORITY]);
175 set_setting(
176 Settings::SETTING_VIDEO_TAB_PRIORITY,
177 $videoTabPriority
178 );
179 }
180
181 set_setting(
182 Settings::SETTING_MAKE_VIDEO_FULL_TAB_WIDTH,
183 isset($_POST['a2wl_' . Settings::SETTING_MAKE_VIDEO_FULL_TAB_WIDTH])
184 );
185
186 if (isset($_POST['a2wl_' . Settings::SETTING_ADD_VIDEO_TO_DESCRIPTION])) {
187 $addVideoToDescription = $_POST['a2wl_' . Settings::SETTING_ADD_VIDEO_TO_DESCRIPTION];
188 if (in_array($addVideoToDescription, ['none', 'after', 'before'], true)) {
189 set_setting(
190 Settings::SETTING_ADD_VIDEO_TO_DESCRIPTION,
191 $addVideoToDescription
192 );
193 }
194 }
195
196 settings()->commit();
197 settings()->auto_commit(true);
198 }
199
200 $this->model_put("addVideoToDescriptionTypes", ['none', 'before', 'after']);
201
202 return "settings/video.php";
203 }
204
205 private function account_handle(): string
206 {
207 if (!empty($_POST)) {
208 check_admin_referer(self::PAGE_NONCE_ACTION, self::NONCE);
209 }
210
211 $account = Account::getInstance();
212
213 $token = AliexpressToken::getInstance();
214
215 if (isset($_POST['setting_form'])) {
216 $accountType = 'aliexpress';
217
218 $accountTypeCheck = isset($_POST['a2wl_account_type']) &&
219 in_array($_POST['a2wl_account_type'], ['aliexpress', 'admitad', 'epn']);
220
221 if ($accountTypeCheck) {
222 $accountType = $_POST['a2wl_account_type'];
223 }
224
225 $account->set_account_type($accountType);
226 $account->use_custom_account(isset($_POST['a2wl_use_custom_account']));
227 if ($account->custom_account && isset($_POST['a2wl_account_type'])) {
228 if ($_POST['a2wl_account_type'] == 'aliexpress') {
229 $appkey = isset($_POST['a2wl_appkey']) ?
230 trim(sanitize_text_field($_POST['a2wl_appkey'])) : null;
231 $secretkey = isset($_POST['a2wl_secretkey']) ?
232 trim(sanitize_text_field($_POST['a2wl_secretkey'])) : null;
233 $trackingid = isset($_POST['a2wl_trackingid']) ?
234 trim(sanitize_text_field($_POST['a2wl_trackingid'])) : null;
235
236 $appkey = $this->isNoWhiteSpace($appkey) ? $appkey : null;
237 $secretkey = $this->isNoWhiteSpace($secretkey) ? $secretkey : null;
238 $trackingid = $this->isNoWhiteSpace($trackingid) ? $trackingid : null;
239 $account->save_aliexpress_account($appkey, $secretkey, $trackingid);
240 } else if ($_POST['a2wl_account_type'] == 'admitad') {
241 $cashback_url = isset($_POST['a2wl_admitad_cashback_url']) ?
242 trim(sanitize_text_field($_POST['a2wl_admitad_cashback_url'])) : '';
243 $account_name = isset($_POST['a2wl_admitad_account_name']) ?
244 trim(sanitize_text_field($_POST['a2wl_admitad_account_name'])) : '';
245
246 $cashback_url = $this->isNoWhiteSpace($cashback_url) ? $cashback_url : '';
247 $account_name = $this->isNoWhiteSpace($account_name) ? $account_name : '';
248 $account->save_admitad_account($cashback_url, $account_name);
249 } else if ($_POST['a2wl_account_type'] == 'epn') {
250 $cashback_url = isset($_POST['a2wl_epn_cashback_url']) ?
251 trim(sanitize_text_field($_POST['a2wl_epn_cashback_url'])) : '';
252
253 $cashback_url = $this->isNoWhiteSpace($cashback_url) ? $cashback_url : '';
254 $account->save_epn_account($cashback_url);
255 }
256 }
257 }
258
259 $this->model_put("account", $account);
260
261 $this->model_put("tokens", $token->tokens());
262
263 return "settings/account.php";
264 }
265
266 function isNoWhiteSpace(string $value): bool
267 {
268 return preg_match('/^\S+$/', $value);
269 }
270
271 private function price_formula(): string
272 {
273 $PriceFormulaSettingsRepository= A2WL()->getDI()->get('AliNext_Lite\PriceFormulaSettingsRepository');
274 $PriceFormulaRepository = A2WL()->getDI()->get('AliNext_Lite\PriceFormulaRepository');
275 $PriceFormulaSetRepository = A2WL()->getDI()->get('AliNext_Lite\PriceFormulaSetRepository');
276 $PriceFormulaFactory = A2WL()->getDI()->get('AliNext_Lite\PriceFormulaFactory');
277 $WoocommerceModel = A2WL()->getDI()->get('AliNext_Lite\Woocommerce');
278
279 $formulas = $PriceFormulaRepository->getExtendedFormulas();
280
281 if ($formulas) {
282 $add_formula = $PriceFormulaFactory->createFormula();
283 $add_formula->min_price = floatval($formulas[count($formulas) - 1]->max_price) + 0.01;
284 $formulas[] = $add_formula;
285 $this->model_put("formulas", $formulas);
286 } else {
287 $this->model_put("formulas", $PriceFormulaRepository->getDefaultFormulas());
288 }
289
290 $this->model_put("PriceFormulaSets", $PriceFormulaSetRepository->getAll());
291 //$a2wl_pricing_rules_type
292 $this->model_put("pricingRuleTypes", PriceFormula::pricing_rules_types());
293 $this->model_put("PriceFormulaSettingsRepository", $PriceFormulaSettingsRepository);
294
295 $this->model_put("default_formula", $PriceFormulaRepository->getDefaultFormula());
296
297 $this->model_put('cents', get_setting('price_cents'));
298 $this->model_put('compared_cents', get_setting('price_compared_cents'));
299 $this->model_put("categories", $WoocommerceModel->get_categories());
300
301 return "settings/price_formula.php";
302 }
303
304 private function reviews(): string
305 {
306 if (!empty($_POST)) {
307 check_admin_referer(self::PAGE_NONCE_ACTION, self::NONCE);
308 }
309
310 if (isset($_POST['setting_form'])) {
311 settings()->auto_commit(false);
312 set_setting('load_review', isset($_POST['a2wl_load_review']));
313 set_setting('review_status', isset($_POST['a2wl_review_status']));
314 set_setting('review_translated', isset($_POST['a2wl_review_translated']));
315 set_setting('review_avatar_import', isset($_POST['a2wl_review_avatar_import']));
316
317 set_setting('review_schedule_load_period', 'a2wl_15_mins');
318
319 //review number fields
320 $review_number_from = 10;
321 $review_number_to = 20;
322
323 if (isset($_POST['a2wl_review_min_per_product'])) {
324 $review_number_from = intval($_POST['a2wl_review_min_per_product']);
325 }
326
327 if (isset($_POST['a2wl_review_max_per_product'])) {
328 $review_number_to = intval($_POST['a2wl_review_max_per_product']);
329 }
330
331 if ($review_number_to < 1) {
332 $review_number_to = 20;
333 }
334
335 if ($review_number_from < 1 || $review_number_from > $review_number_to) {
336 $review_number_from = $review_number_to;
337 }
338
339 set_setting('review_min_per_product', $review_number_from);
340 set_setting('review_max_per_product', $review_number_to);
341
342 //clear this meta in all products, it will be recalculated during reviews loading
343 Review::clear_all_product_max_number_review_meta();
344
345 //raiting fields
346 $raiting_from = 1;
347 $raiting_to = 5;
348 if (isset($_POST['a2wl_review_raiting_from'])) {
349 $raiting_from = intval($_POST['a2wl_review_raiting_from']);
350 }
351
352 if (isset($_POST['a2wl_review_raiting_to'])) {
353 $raiting_to = intval($_POST['a2wl_review_raiting_to']);
354 }
355
356 if ($raiting_from >= 5) {
357 $raiting_from = 5;
358 }
359
360 if ($raiting_from < 1 || $raiting_from > $raiting_to) {
361 $raiting_from = 1;
362 }
363
364 if ($raiting_to >= 5) {
365 $raiting_to = 5;
366 }
367
368 if ($raiting_to < 1) {
369 $raiting_to = 1;
370 }
371
372 set_setting('review_raiting_from', $raiting_from);
373 set_setting('review_raiting_to', $raiting_to);
374
375 //update more field
376 set_setting('review_load_attributes', isset($_POST['a2wl_review_load_attributes']));
377
378 set_setting('review_show_image_list', isset($_POST['a2wl_review_show_image_list']));
379
380 if (isset($_POST['a2wl_review_show_image_list'])) {
381 $a2wl_review_thumb_width = intval($_POST['a2wl_review_thumb_width']);
382
383 if ($a2wl_review_thumb_width > 0) {
384 set_setting('review_thumb_width', $a2wl_review_thumb_width);
385 } else {
386 set_setting('review_thumb_width', 30);
387 }
388 }
389
390 set_setting('review_skip_empty', isset($_POST['a2wl_review_skip_empty']));
391 set_setting('review_skip_keywords',
392 isset($_POST['a2wl_review_skip_keywords']) ? trim(wp_unslash($_POST['a2wl_review_skip_keywords'])) : ''
393 );
394
395 if (!isset($_POST['a2wl_review_country']) || !is_array($_POST['a2wl_review_country'])) {
396 set_setting('review_country', []);
397 } else {
398 set_setting('review_country', $_POST['a2wl_review_country']);
399 }
400
401 set_setting('moderation_reviews', isset($_POST['a2wl_moderation_reviews']));
402
403 $checkNoAvatarPhoto = $_FILES && isset($_FILES[self::FIELD_FIELD_NO_AVATAR_PHOTO]) &&
404 UPLOAD_ERR_OK === $_FILES[self::FIELD_FIELD_NO_AVATAR_PHOTO]['error'];
405
406 if ($checkNoAvatarPhoto) {
407 if (!function_exists('wp_handle_upload')) {
408 require_once ABSPATH . 'wp-admin/includes/file.php';
409 }
410
411 $uploadedfile = $_FILES[self::FIELD_FIELD_NO_AVATAR_PHOTO];
412 $movefile = wp_handle_upload($uploadedfile, ['test_form' => false]);
413 if ($movefile && !isset($movefile['error'])) {
414 set_setting('review_noavatar_photo', $movefile['url']);
415 } else {
416 error_log('review avatar upload error: ' . $movefile['error']);
417 }
418 } else {
419 del_setting('review_noavatar_photo');
420 }
421
422 settings()->commit();
423 settings()->auto_commit(true);
424 }
425
426 $countryModel = new Country();
427 $countries = $countryModel->get_countries();
428
429 unset($countries[0]);
430
431 $this->model_put("reviews_countries", $countries);
432
433 return "settings/reviews.php";
434 }
435
436 private function shipping(): string
437 {
438 if (!empty($_POST)) {
439 check_admin_referer(self::PAGE_NONCE_ACTION, self::NONCE);
440 }
441
442 if (isset($_POST['setting_form'])) {
443 $this->ShippingSettingService->handle();
444 }
445
446 $model = $this->ShippingSettingService->collectModel();
447
448 foreach ($model as $key => $value) {
449 $this->model_put($key, $value);
450 }
451
452 return "settings/shipping/index.php";
453 }
454
455 private function phrase_filter(): string
456 {
457 $phrases = PhraseFilter::load_phrases();
458
459 if ($phrases) {
460 $this->model_put("phrases", $phrases);
461 } else {
462 $this->model_put("phrases", array());
463 }
464
465 return "settings/phrase_filter.php";
466 }
467
468 private function chrome_api(): string
469 {
470 if (!empty($_POST)) {
471 check_admin_referer(self::PAGE_NONCE_ACTION, self::NONCE);
472 }
473
474 $api_keys = get_setting('api_keys', []);
475
476 if (!empty($_REQUEST['delete-key'])) {
477 foreach ($api_keys as $k => $key) {
478 if ($key['id'] === $_REQUEST['delete-key']) {
479 unset($api_keys[$k]);
480 set_setting('api_keys', $api_keys);
481 break;
482 }
483 }
484 wp_redirect(admin_url('admin.php?page=a2wl_setting&subpage=chrome_api'));
485 } else if (!empty($_POST['a2wl_api_key'])) {
486 $key_id = sanitize_text_field($_POST['a2wl_api_key']);
487 $key_name = !empty($_POST['a2wl_api_key_name']) ?
488 sanitize_text_field($_POST['a2wl_api_key_name']) :
489 "New key";
490
491 $is_new = true;
492 foreach ($api_keys as &$key) {
493 if ($key['id'] === $key_id) {
494 $key['name'] = $key_name;
495 $is_new = false;
496 break;
497 }
498 }
499
500 if ($is_new) {
501 $api_keys[] = [
502 'id' => $key_id,
503 'name' => $key_name
504 ];
505 }
506
507 set_setting('api_keys', $api_keys);
508
509 wp_redirect(admin_url('admin.php?page=a2wl_setting&subpage=chrome_api&edit-key=' . $key_id));
510 } else if (isset($_REQUEST['edit-key'])) {
511 $api_key = [
512 'id' => md5("a2wkey" . wp_rand() . microtime()),
513 'name' => "New key"
514 ];
515 $is_new = true;
516 if (empty($_REQUEST['edit-key'])) {
517 $api_keys[] = $api_key;
518 set_setting('api_keys', $api_keys);
519
520 wp_redirect(admin_url('admin.php?page=a2wl_setting&subpage=chrome_api&edit-key=' . $api_key['id']));
521 } else if (!empty($_REQUEST['edit-key']) && $api_keys && is_array($api_keys)) {
522 foreach ($api_keys as $key) {
523 if ($key['id'] === $_REQUEST['edit-key']) {
524 $api_key = $key;
525 $is_new = false;
526 }
527 }
528 }
529 $this->model_put("api_key", $api_key);
530 $this->model_put("is_new_api_key", $is_new);
531 }
532
533 $this->model_put("api_keys", $api_keys);
534
535 return "settings/chrome.php";
536 }
537
538 private function system_info(): string
539 {
540 if (!empty($_POST)) {
541 check_admin_referer(self::PAGE_NONCE_ACTION, self::NONCE);
542 }
543
544 if (isset($_POST['setting_form'])) {
545 set_setting('write_info_log', isset($_POST['a2wl_write_info_log']));
546 set_setting(
547 Settings::SETTINGS_TIP_OF_DAY_DISABLED,
548 !isset($_POST['a2wl_tip_of_day_disabled'])
549 );
550 }
551
552 $server_ip = '-';
553 if (array_key_exists('SERVER_ADDR', $_SERVER)) {
554 $server_ip = $_SERVER['SERVER_ADDR'];
555 } elseif (array_key_exists('LOCAL_ADDR', $_SERVER)) {
556 $server_ip = $_SERVER['LOCAL_ADDR'];
557 } elseif (array_key_exists('SERVER_NAME', $_SERVER)) {
558 $server_ip = gethostbyname($_SERVER['SERVER_NAME']);
559 } else {
560 // Running CLI
561 if (stristr(PHP_OS, 'WIN')) {
562 $server_ip = gethostbyname(php_uname("n"));
563 } else {
564 $ifconfig = shell_exec('/sbin/ifconfig eth0');
565 preg_match('/addr:([\d\.]+)/', $ifconfig, $match);
566 $server_ip = $match[1];
567 }
568 }
569
570 $processorCores = $this->LocalService->getNumberOfProcessorCores();
571 $systemAverageLoadStatus = true;
572 $systemAverageLoad = $this->LocalService->getSystemLoadAverage();
573 foreach ($systemAverageLoad as $systemLoad) {
574 if ($systemLoad > $processorCores) {
575 $systemAverageLoadStatus = false;
576 break;
577 }
578 }
579
580 $memoryUsage = $this->LocalService->getMemoryUsageInBytes();
581
582 $this->model_put("processorCores", $processorCores);
583 $this->model_put(
584 'systemLoadAverage', implode(', ', $systemAverageLoad)
585 );
586 $this->model_put("systemAverageLoadStatus", $systemAverageLoadStatus);
587 $this->model_put("memoryUsage", size_format($memoryUsage));
588
589 $this->model_put("server_ip", $server_ip);
590
591
592
593 return "settings/system_info.php";
594 }
595 }
596