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

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

203 lines 7.7 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 SearchPageController
5 *
6 * @author Ali2Woo Team
7 *
8 * @autoload: a2wl_admin_init
9 */
10
11 namespace AliNext_Lite;;
12
13 use Pages;
14
15 class SearchPageController extends AbstractAdminPage
16 {
17
18 public function __construct(
19 protected Aliexpress $AliexpressModel,
20 protected Country $CountryModel,
21 protected PermanentAlertService $PermanentAlertService,
22 protected TipOfDayService $TipOfDayService,
23
24 protected PromoService $PromoService,
25
26 ) {
27 parent::__construct(
28 Pages::getLabel(Pages::DASHBOARD),
29 Pages::getLabel(Pages::DASHBOARD),
30 Capability::pluginAccess(),
31 Pages::DASHBOARD,
32 10
33 );
34
35 add_filter('a2wl_configure_lang_data', [$this, 'configure_lang_data']);
36 }
37
38 public function configure_lang_data($lang_data)
39 {
40 $lang_data['advance'] = _x('Advance', 'Button', 'ali2woo');
41 $lang_data['simple'] = _x('Simple', 'Button', 'ali2woo');
42 $lang_data['imported_successfully'] = _x('Imported successfully.', 'Status', 'ali2woo');
43 $lang_data['removed_successfully'] = _x('Removed successfully.', 'Status', 'ali2woo');
44 $lang_data['import_failed'] = _x('Import failed.', 'Status', 'ali2woo');
45
46 return $lang_data;
47 }
48
49 public function render($params = []): void
50 {
51 if (!PageGuardHelper::canAccessPage(Pages::DASHBOARD)) {
52 wp_die($this->getErrorTextNoPermissions());
53 }
54
55 $filter = [];
56 if (!empty($_GET['a2wl_search'])) {
57 check_admin_referer(self::PAGE_NONCE_ACTION, self::NONCE);
58 $filter = array_merge($filter, $_GET);
59 if (isset($filter['cur_page'])) {
60 unset($filter['cur_page']);
61 }
62 if (isset($filter['page'])) {
63 unset($filter['page']);
64 }
65 }
66
67 $adv_search_field = [
68 'min_price', 'max_price', 'min_feedback',
69 'max_feedback', 'volume_from', 'volume_to',
70 'shipTo', 'shipFrom', 'freeshipping', 'hotArea',
71 'sellerOnline', 'sellerLevel', 'itemTag'
72 ];
73 $adv_search = false;
74 foreach ($filter as $key => $val) {
75 $new_key = preg_replace('/a2wl_/', '', $key, 1);
76 unset($filter[$key]);
77 $filter[$new_key] = wp_unslash($val);
78 if (in_array($new_key, $adv_search_field)) {
79 $adv_search = true;
80 }
81 }
82
83 if (!isset($filter['sort'])) {
84 $filter['sort'] = "volumeDown";
85 }
86
87 if (!isset($filter['shipTo'])) {
88 $filter['shipTo'] = "US";
89 }
90
91 $page = isset($_GET['cur_page']) && intval($_GET['cur_page']) ? intval($_GET['cur_page']) : 1;
92 $per_page = 20;
93 if (a2wl_check_defined('A2WL_PRODUCT_SEARCH_LIMIT')) {
94 $per_page = intval(A2WL_PRODUCT_SEARCH_LIMIT);
95 }
96
97 if (!empty($_REQUEST['a2wl_search'])) {
98 check_admin_referer(self::PAGE_NONCE_ACTION, self::NONCE);
99 $load_products_result = $this->AliexpressModel->load_products($filter, $page, $per_page);
100 } else {
101 $load_products_result = ResultBuilder::buildError(
102 esc_html__('Please enter some search keywords or select item from category list!', 'ali2woo')
103 );
104 }
105
106 if ($load_products_result['state'] == 'error' || $load_products_result['state'] == 'warn') {
107 add_settings_error(
108 'a2wl_products_list', esc_attr('settings_updated'),
109 $load_products_result['message'], 'error'
110 );
111 }
112
113 if ($load_products_result['state'] != 'error') {
114 $pages_list = [];
115 $links = 4;
116 $last = ceil($load_products_result['total'] / $per_page);
117 $load_products_result['total_pages'] = $last;
118 $start = (($load_products_result['page'] - $links) > 0) ? $load_products_result['page'] - $links : 1;
119 $end = (($load_products_result['page'] + $links) < $last) ? $load_products_result['page'] + $links : $last;
120 if ($start > 1) {
121 $pages_list[] = 1;
122 $pages_list[] = '';
123 }
124 for ($i = $start; $i <= $end; $i++) {
125 $pages_list[] = $i;
126 }
127 if ($end < $last) {
128 $pages_list[] = '';
129 $pages_list[] = $last;
130 }
131 $load_products_result['pages_list'] = $pages_list;
132
133 a2wl_set_transient('a2wl_search_result', $load_products_result['products']);
134 }
135
136 $localizator = AliexpressLocalizator::getInstance();
137
138 $filterSortOptions = [
139 'orignalPriceUp' => _x('Lowest price', 'sort by', 'ali2woo'),
140 'orignalPriceDown' => _x('Highest price', 'sort by', 'ali2woo'),
141 'volumeDown' => _x('Highest sales', 'sort by', 'ali2woo'),
142 'volumeUp' => _x('Lowest sales', 'sort by', 'ali2woo'),
143 'reviewsDown' => _x('Max reviews', 'sort by', 'ali2woo'),
144 'reviewsUp' => _x('Min reviews', 'sort by', 'ali2woo'),
145 ];
146
147 $page = esc_attr(((isset($_GET['page'])) ? $_GET['page'] : ''));
148 $curPage = esc_attr(((isset($_GET['cur_page'])) ? $_GET['cur_page'] : ''));
149
150 $sellerOnlineHours = [
151 '48' => _x('Last 48 hours', 'search page', 'ali2woo'),
152 '72' => _x('Last 72 hours', 'search page', 'ali2woo'),
153 ];
154
155 $sellerLevels = [
156 'gold' => _x('Gold', 'search page', 'ali2woo'),
157 'silver' => _x('Silver', 'search page', 'ali2woo'),
158 ];
159
160 $this->model_put('filter', $filter);
161 $this->model_put('filterSortOptions', $filterSortOptions);
162 $this->model_put('adv_search', $adv_search);
163 $this->model_put('categories', $this->get_categories());
164 $this->model_put('hotCountries', $this->CountryModel->getHotCountryLabels());
165 $this->model_put('countries', $this->CountryModel->get_countries());
166 $this->model_put('sellerOnlineHours', $sellerOnlineHours);
167 $this->model_put('sellerLevels', $sellerLevels);
168 $this->model_put('locale', $localizator->getLangCode());
169 $this->model_put('currency', $localizator->currency);
170 $this->model_put('chrome_ext_import', a2wl_check_defined('A2WL_CHROME_EXT_IMPORT'));
171 $this->model_put('page', $page);
172 $this->model_put('curPage', $curPage);
173
174
175 $this->model_put('promo_data', $this->PromoService->getPromoData());
176
177
178 $this->model_put("PermanentAlerts", $this->PermanentAlertService->getAll());
179 $this->model_put('load_products_result', $load_products_result);
180 $this->model_put("TipOfDay", $this->TipOfDayService->getNextTip());
181
182 $search_version = 'v3';
183 $this->include_view('search_' . $search_version . '.php');
184 }
185
186 protected function get_categories()
187 {
188 if (file_exists(A2WL()->plugin_path() . '/assets/data/user_aliexpress_categories.json')) {
189 $result = json_decode(
190 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
191 file_get_contents(A2WL()->plugin_path() . '/assets/data/user_aliexpress_categories.json'),
192 true
193 );
194 } else {
195 $result = array('categories' => get_option('a2wl_all_categories', array()));
196 }
197 $result = isset($result["categories"]) && is_array($result["categories"]) ? $result["categories"] : array();
198 array_unshift($result, array("id" => "0", "name" => "All categories", "level" => 1));
199 return $result;
200 }
201
202 }
203