PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.2.18
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.2.18
1.4.15 1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 All 178 releases
disco / app / Utility / Search.php

Search.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.2.18, at app/Utility/Search.php

563 lines 12.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php //phpcs:ignore
2
3 /**
4 * Search Utility
5 *
6 * @package Disco
7 * @subpackage App\Utility
8 * @since 1.0.0
9 * @category Utility
10 */
11
12 namespace Disco\App\Utility;
13
14 use WC_Countries;
15 use WC_Coupon;
16 use WC_Data_Store;
17 use WP_Query;
18 use WP_Term_Query;
19 use WP_User_Query;
20
21 /**
22 * Class Search
23 *
24 * @package Disco
25 * @subpackage App\Utility
26 * @author Ohidul Islam <wahid0003@gmail.com>
27 * @link https://webappick.com
28 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
29 * @category Utility
30 */
31 class Search { //phpcs:ignore
32
33 /**
34 * Search Products by Product Title or SKU.
35 *
36 * @param string $search_term Search Term.
37 * @return array
38 */
39 public static function products( $search_term = '' ) {
40 try {
41 $data_store = WC_Data_Store::load( 'product' );
42 // @phpstan-ignore-next-line
43 $get_products = $data_store->search_products( $search_term, 'product', true, false, 200, array(), array() );
44 } catch ( \Throwable $e ) {
45 $get_products = array();
46 }
47
48 $products = array();
49
50 if ( ! empty( $get_products ) ) {
51 foreach ( $get_products as $pid ) {
52 if ( ! $pid ) {
53 continue;
54 }
55
56 $product = wc_get_product( $pid );
57
58 if ( ! ( $product instanceof \WC_Product ) ) {
59 continue;
60 }
61
62 $products[] = array(
63 'id' => $product->get_id(),
64 'sku' => $product->get_sku(),
65 'name' => $product->get_name(),
66 'image' => wp_get_attachment_url( (int) $product->get_image_id() ),
67 );
68 }
69 }
70
71 return $products;
72 }
73
74 /**
75 * Search Categories by Category Name.
76 *
77 * @param string $search_term Search Term.
78 * @return array
79 */
80 public static function categories( $search_term = '' ) {
81 $args = array(
82 'taxonomy' => 'product_cat',
83 'orderby' => 'name',
84 'order' => 'ASC',
85 'hide_empty' => false,
86 'number' => 20,
87 );
88
89 if ( ! empty( $search_term ) ) {
90 $args['search'] = $search_term;
91 }
92
93 $args = (array) $args;
94 $get_categories = ( new WP_Term_Query( $args ) )->get_terms();
95
96 $categories = array();
97
98 if ( is_array( $get_categories ) && ! empty( $get_categories ) ) {
99 foreach ( $get_categories as $category ) {
100 if ( ! ( $category instanceof \WP_Term ) ) {
101 continue;
102 }
103
104 $categories[] = array(
105 'id' => $category->term_id,
106 'name' => $category->name,
107 );
108 }
109 }
110
111 return $categories;
112 }
113
114 /**
115 * Search Tags by Tag Name.
116 *
117 * @param string $search_term Search Term.
118 * @return array
119 */
120 public static function tags( $search_term = '' ) {
121 $args = array(
122 'taxonomy' => 'product_tag',
123 'orderby' => 'name',
124 'order' => 'ASC',
125 'hide_empty' => false,
126 'number' => 20,
127 );
128
129 if ( ! empty( $search_term ) ) {
130 $args['search'] = $search_term;
131 }
132
133 $get_tags = ( new WP_Term_Query( $args ) )->get_terms();
134
135 $tags = array();
136
137 if ( is_array( $get_tags ) && ! empty( $get_tags ) ) {
138 foreach ( $get_tags as $tag ) {
139 if ( ! ( $tag instanceof \WP_Term ) ) {
140 continue;
141 }
142
143 $tags[] = array(
144 'id' => $tag->term_id,
145 'name' => $tag->name,
146 );
147 }
148 }
149
150 return $tags;
151 }
152
153 /**
154 * Search coupon by coupon name.
155 *
156 * @param string $search_term Search Term.
157 * @return array
158 */
159 public static function coupons( $search_term = '' ) {
160 $args = array(
161 'post_type' => 'shop_coupon',
162 'post_status' => 'publish',
163 'fields' => 'post_title',
164 'number' => 20,
165
166 );
167
168 if ( ! empty( $search_term ) ) {
169 $args['s'] = $search_term;
170 }
171
172 $get_coupons = ( new WP_Query( $args ) )->get_posts();
173
174 $coupons = array();
175
176 if ( ! empty( $get_coupons ) ) {
177 foreach ( $get_coupons as $coupon ) {
178 if ( ! ( $coupon instanceof \WP_Post ) ) {
179 continue;
180 }
181
182 $coupon = new WC_Coupon( $coupon->post_title );
183
184 if ( ! $coupon->is_valid_for_cart() ) {
185 continue;
186 }
187
188 $coupons[] = array(
189 'id' => $coupon->get_id(),
190 'name' => $coupon->get_code(),
191 );
192 }
193 }
194
195 return $coupons;
196 }
197
198 /**
199 * Search Customer by Name or Email.
200 *
201 * @param string $search_term Search Term.
202 * @return array
203 * @throws \Exception If the search term is invalid.
204 */
205 public static function customers( $search_term = '' ) {
206 $args = array(
207 'role' => 'customer',
208 'order' => 'ASC',
209 'orderby' => 'display_name',
210 'number' => 20,
211 );
212
213 if ( ! empty( $search_term ) ) {
214 $args['search'] = '*' . esc_attr( $search_term ) . '*';
215 }
216
217 if ( filter_var( $search_term, FILTER_VALIDATE_EMAIL ) ) {
218 // valid email address
219 $args['search_columns'] = array( 'user_email' );
220 } else {
221 // invalid address
222 $args['meta_query'] = array( //phpcs:ignore
223 'relation' => 'OR',
224 array(
225 'key' => 'first_name',
226 'value' => $search_term,
227 'compare' => 'LIKE',
228 ),
229 array(
230 'key' => 'last_name',
231 'value' => $search_term,
232 'compare' => 'LIKE',
233 ),
234 array(
235 'key' => 'user_email',
236 'value' => $search_term,
237 'compare' => 'LIKE',
238 ),
239 );
240 }
241
242 // Create the WP_User_Query object
243 $get_customers = ( new WP_User_Query( $args ) )->get_results();
244
245 $customers = array();
246
247 if ( ! empty( $get_customers ) ) {
248 foreach ( $get_customers as $customer ) {
249 $customers[] = array(
250 'id' => $customer->ID,
251 'name' => $customer->first_name . ' ' . $customer->last_name . ' (' . $customer->user_email . ')',
252 );
253 }
254 }
255
256 return $customers;
257 }
258
259 /**
260 * Get States by Country Code.
261 *
262 * @param string $search_term Search Term.
263 * @return array
264 */
265 public static function states( $search_term ) {
266 // Initialize WooCommerce Countries object
267 $countries = WC()->countries;
268
269 // Get all states (indexed by country)
270 $states_by_country = $countries->get_states();
271
272 // Initialize an array to hold matching states
273 $matching_states = array();
274
275 // Check if the state array is empty
276 if ( empty( $states_by_country ) ) {
277 return $matching_states;
278 }
279
280 // Loop through each country and its states
281 foreach ( $states_by_country as $country_code => $states ) {
282 $country_names = $countries->get_countries();
283 $country_name = $country_names[$country_code];
284
285 if ( !is_array( $states ) ) {
286 continue;
287 }
288
289 foreach ( $states as $state_code => $state_name ) {
290 // Check if the state name contains the search term (case-insensitive)
291 if ( stripos( $state_name, $search_term ) === false ) {
292 continue;
293 }
294
295 $matching_states[] = array(
296 'id' => $state_code,
297 'name' => $country_name . ' - ' . $state_name,
298 );
299 }
300 }
301
302 return $matching_states;
303 }
304
305 /**
306 * Get States by Country Code.
307 *
308 * @param string $search_term Country or state name.
309 * @return array
310 */
311 public static function countries( $search_term ) {
312 if ( empty( $search_term ) ) {
313 return array();
314 }
315
316 $counties = array();
317 $get_counties = ( new WC_Countries )->get_countries();
318
319 foreach ( $get_counties as $country_key => $country ) {
320 $counties[ $country_key ] = array(
321 'id' => $country_key,
322 'name' => $country,
323 );
324 }
325
326 return array_filter(
327 $counties,
328 static function ( $item ) use ( $search_term ) {
329 $value = $item['id'] . '-' . $item['name'];
330
331 return stripos( $value, $search_term ) !== false;
332 }
333 );
334 }
335
336 /**
337 * Search Attributes by Attribute Name.
338 *
339 * @param string $search_term Search Term.
340 * @return array
341 */
342 public static function attributes( $search_term = ' ' ) {
343 $attributes = self::get_global_attributes( $search_term );
344 $custom_attributes = self::get_custom_attributes( $search_term );
345
346 return array_merge( $attributes, $custom_attributes );
347 }
348
349 /**
350 * Search Users by Name or Email.
351 *
352 * @param string $search_term Search Term.
353 * @return array
354 */
355 public static function user_name( $search_term = '' ) {
356 $args = array(
357 'role__in' => array( 'author', 'editor', 'administrator', 'shop_manager' ),
358 'order' => 'ASC',
359 'orderby' => 'display_name',
360 'number' => 20,
361 );
362
363 if ( ! empty( $search_term ) ) {
364 $args['search'] = '*' . esc_attr( $search_term ) . '*';
365 }
366
367 if ( filter_var( $search_term, FILTER_VALIDATE_EMAIL ) ) {
368 // valid email address
369 $args['search_columns'] = array( 'user_email' );
370 } else {
371 // invalid address
372 $args['meta_query'] = array( //phpcs:ignore
373 'relation' => 'OR',
374 array(
375 'key' => 'first_name',
376 'value' => $search_term,
377 'compare' => 'LIKE',
378 ),
379 array(
380 'key' => 'last_name',
381 'value' => $search_term,
382 'compare' => 'LIKE',
383 ),
384 array(
385 'key' => 'user_email',
386 'value' => $search_term,
387 'compare' => 'LIKE',
388 ),
389 );
390 }
391
392 // Create the WP_User_Query object
393 $get_users = ( new WP_User_Query( $args ) )->get_results();
394
395 $users = array();
396
397 if ( ! empty( $get_users ) ) {
398 foreach ( $get_users as $user ) {
399 $users[] = array(
400 'id' => $user->ID,
401 'name' => $user->first_name . ' ' . $user->last_name,
402 );
403 }
404 }
405
406 return $users;
407 }
408
409 /**
410 * Search Users by Email.
411 *
412 * @param string $search_term Search Term.
413 * @return array
414 */
415 public static function user_email( $search_term = '' ) {
416 $args = array(
417 'role__in' => array( 'author', 'editor', 'administrator', 'shop_manager' ),
418 'order' => 'ASC',
419 'orderby' => 'display_name',
420 'number' => 20,
421 );
422
423 if ( ! empty( $search_term ) ) {
424 $args['search'] = '*' . esc_attr( $search_term ) . '*';
425 }
426
427 if ( filter_var( $search_term, FILTER_VALIDATE_EMAIL ) ) {
428 // valid email address
429 $args['search_columns'] = array( 'user_email' );
430 } else {
431 // invalid address
432 $args['meta_query'] = array( //phpcs:ignore
433 'relation' => 'OR',
434 array(
435 'key' => 'first_name',
436 'value' => $search_term,
437 'compare' => 'LIKE',
438 ),
439 array(
440 'key' => 'last_name',
441 'value' => $search_term,
442 'compare' => 'LIKE',
443 ),
444 array(
445 'key' => 'user_email',
446 'value' => $search_term,
447 'compare' => 'LIKE',
448 ),
449 );
450 }
451
452 // Create the WP_User_Query object
453 $get_users = ( new WP_User_Query( $args ) )->get_results();
454
455 $users = array();
456
457 if ( ! empty( $get_users ) ) {
458 foreach ( $get_users as $user ) {
459 $users[] = array(
460 'id' => $user->ID,
461 'name' => $user->user_email,
462 );
463 }
464 }
465
466 return $users;
467 }
468
469 /**
470 * Search Attributes by Attribute Name.
471 *
472 * @param string $search_term Search Term.
473 * @return array
474 */
475 protected static function get_global_attributes( $search_term ) {
476 $global_attributes = Cache::remember(
477 'wc_global_attributes',
478 array(
479 new Model,
480 'wc_global_attribute_query',
481 ),
482 array( $search_term )
483 );
484 $result = array();
485
486 if ( is_array( $global_attributes ) && ! empty( $global_attributes ) ) {
487 foreach ( $global_attributes as $attribute ) {
488 if ( ! isset( $attribute->attribute_name, $attribute->attribute_label ) ) {
489 continue;
490 }
491
492 $result[] = array(
493 'id' => $attribute->attribute_name,
494 'name' => $attribute->attribute_label,
495 );
496 }
497 }
498
499 return $result;
500 }
501
502 /**
503 * Get all product custom attributes.
504 *
505 * @param string $search_term Search Term.
506 * @return array
507 */
508 protected static function get_custom_attributes( $search_term ) {
509 $custom_attributes = Cache::remember(
510 'wc_custom_attributes',
511 array(
512 new \Disco\App\Utility\Model,
513 'wc_custom_attribute_query',
514 ),
515 array( $search_term )
516 );
517 $result = array();
518
519 if ( is_array( $custom_attributes ) && ! empty( $custom_attributes ) ) {
520 foreach ( $custom_attributes as $value ) {
521 $product_attr = maybe_unserialize( $value->type );
522
523 if ( ! is_array( $product_attr ) ) {
524 continue;
525 }
526
527 $result = array_merge( $result, self::filter_product_attributes( $product_attr, $search_term ) );
528 }
529 }
530
531 return $result;
532 }
533
534 /**
535 * Filter Product Attributes.
536 *
537 * @param array $product_attr Product Attributes.
538 * @param string $search_term Search Term.
539 * @return array
540 */
541 protected static function filter_product_attributes( $product_attr, $search_term ) {
542 $filtered_attrs = array();
543
544 foreach ( $product_attr as $key => $arr_value ) {
545 if ( strpos( $key, 'pa_' ) !== false ) {
546 continue;
547 }
548
549 if ( ! empty( $search_term ) && ( stripos( $arr_value['name'], $search_term ) === false ) ) {
550 continue;
551 }
552
553 $filtered_attrs[] = array(
554 'id' => $key,
555 'name' => ucwords( str_replace( '-', ' ', $arr_value['name'] ) ),
556 );
557 }
558
559 return $filtered_attrs;
560 }
561
562 }
563