PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.3.34
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.3.34
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.3.34, at app/Utility/Search.php

602 lines 13.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignoreFile
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 Brands by Brand Name.
155 *
156 * @param string $search_term Search Term.
157 * @return array
158 */
159 public static function brands( $search_term = '' ) {
160 $args = array(
161 'taxonomy' => 'product_brand',
162 'orderby' => 'name',
163 'order' => 'ASC',
164 'hide_empty' => false,
165 'number' => 20,
166 );
167
168 if ( ! empty( $search_term ) ) {
169 $args['search'] = $search_term;
170 }
171
172 $get_brands = ( new WP_Term_Query( $args ) )->get_terms();
173
174 $brands = array();
175
176 if ( is_array( $get_brands ) && ! empty( $get_brands ) ) {
177 foreach ( $get_brands as $brand ) {
178 if ( ! ( $brand instanceof \WP_Term ) ) {
179 continue;
180 }
181
182 $brands[] = array(
183 'id' => $brand->term_id,
184 'name' => $brand->name,
185 );
186 }
187 }
188
189 return $brands;
190 }
191
192 /**
193 * Search coupon by coupon name.
194 *
195 * @param string $search_term Search Term.
196 * @return array
197 */
198 public static function coupons( $search_term = '' ) {
199 $args = array(
200 'post_type' => 'shop_coupon',
201 'post_status' => 'publish',
202 'fields' => 'post_title',
203 'number' => 20,
204
205 );
206
207 if ( ! empty( $search_term ) ) {
208 $args['s'] = $search_term;
209 }
210
211 $get_coupons = ( new WP_Query( $args ) )->get_posts();
212
213 $coupons = array();
214
215 if ( ! empty( $get_coupons ) ) {
216 foreach ( $get_coupons as $coupon ) {
217 if ( ! ( $coupon instanceof \WP_Post ) ) {
218 continue;
219 }
220
221 $coupon = new WC_Coupon( $coupon->post_title );
222
223 if ( ! $coupon->is_valid_for_cart() ) {
224 continue;
225 }
226
227 $coupons[] = array(
228 'id' => $coupon->get_id(),
229 'name' => $coupon->get_code(),
230 );
231 }
232 }
233
234 return $coupons;
235 }
236
237 /**
238 * Search Customer by Name or Email.
239 *
240 * @param string $search_term Search Term.
241 * @return array
242 * @throws \Exception If the search term is invalid.
243 */
244 public static function customers( $search_term = '' ) {
245 $args = array(
246 'role' => 'customer',
247 'order' => 'ASC',
248 'orderby' => 'display_name',
249 'number' => 20,
250 );
251
252 if ( ! empty( $search_term ) ) {
253 $args['search'] = '*' . esc_attr( $search_term ) . '*';
254 }
255
256 if ( filter_var( $search_term, FILTER_VALIDATE_EMAIL ) ) {
257 // valid email address
258 $args['search_columns'] = array( 'user_email' );
259 } else {
260 // invalid address
261 $args['meta_query'] = array( //phpcs:ignore
262 'relation' => 'OR',
263 array(
264 'key' => 'first_name',
265 'value' => $search_term,
266 'compare' => 'LIKE',
267 ),
268 array(
269 'key' => 'last_name',
270 'value' => $search_term,
271 'compare' => 'LIKE',
272 ),
273 array(
274 'key' => 'user_email',
275 'value' => $search_term,
276 'compare' => 'LIKE',
277 ),
278 );
279 }
280
281 // Create the WP_User_Query object
282 $get_customers = ( new WP_User_Query( $args ) )->get_results();
283
284 $customers = array();
285
286 if ( ! empty( $get_customers ) ) {
287 foreach ( $get_customers as $customer ) {
288 $customers[] = array(
289 'id' => $customer->ID,
290 'name' => $customer->first_name . ' ' . $customer->last_name . ' (' . $customer->user_email . ')',
291 );
292 }
293 }
294
295 return $customers;
296 }
297
298 /**
299 * Get States by Country Code.
300 *
301 * @param string $search_term Search Term.
302 * @return array
303 */
304 public static function states( $search_term ) {
305 // Initialize WooCommerce Countries object
306 $countries = WC()->countries;
307
308 // Get all states (indexed by country)
309 $states_by_country = $countries->get_states();
310
311 // Initialize an array to hold matching states
312 $matching_states = array();
313
314 // Check if the state array is empty
315 if ( empty( $states_by_country ) ) {
316 return $matching_states;
317 }
318
319 // Loop through each country and its states
320 foreach ( $states_by_country as $country_code => $states ) {
321 $country_names = $countries->get_countries();
322 $country_name = $country_names[$country_code];
323
324 if ( !is_array( $states ) ) {
325 continue;
326 }
327
328 foreach ( $states as $state_code => $state_name ) {
329 // Check if the state name contains the search term (case-insensitive)
330 if ( stripos( $state_name, $search_term ) === false ) {
331 continue;
332 }
333
334 $matching_states[] = array(
335 'id' => $state_code,
336 'name' => $country_name . ' - ' . $state_name,
337 );
338 }
339 }
340
341 return $matching_states;
342 }
343
344 /**
345 * Get States by Country Code.
346 *
347 * @param string $search_term Country or state name.
348 * @return array
349 */
350 public static function countries( $search_term ) {
351 if ( empty( $search_term ) ) {
352 return array();
353 }
354
355 $counties = array();
356 $get_counties = ( new WC_Countries )->get_countries();
357
358 foreach ( $get_counties as $country_key => $country ) {
359 $counties[ $country_key ] = array(
360 'id' => $country_key,
361 'name' => $country,
362 );
363 }
364
365 return array_filter(
366 $counties,
367 static function ( $item ) use ( $search_term ) {
368 $value = $item['id'] . '-' . $item['name'];
369
370 return stripos( $value, $search_term ) !== false;
371 }
372 );
373 }
374
375 /**
376 * Search Attributes by Attribute Name.
377 *
378 * @param string $search_term Search Term.
379 * @return array
380 */
381 public static function attributes( $search_term = ' ' ) {
382 $attributes = self::get_global_attributes( $search_term );
383 $custom_attributes = self::get_custom_attributes( $search_term );
384
385 return array_merge( $attributes, $custom_attributes );
386 }
387
388 /**
389 * Search Users by Name or Email.
390 *
391 * @param string $search_term Search Term.
392 * @return array
393 */
394 public static function user_name( $search_term = '' ) {
395 $args = array(
396 'role__in' => array( 'author', 'editor', 'administrator', 'shop_manager' ),
397 'order' => 'ASC',
398 'orderby' => 'display_name',
399 'number' => 20,
400 );
401
402 if ( ! empty( $search_term ) ) {
403 $args['search'] = '*' . esc_attr( $search_term ) . '*';
404 }
405
406 if ( filter_var( $search_term, FILTER_VALIDATE_EMAIL ) ) {
407 // valid email address
408 $args['search_columns'] = array( 'user_email' );
409 } else {
410 // invalid address
411 $args['meta_query'] = array( //phpcs:ignore
412 'relation' => 'OR',
413 array(
414 'key' => 'first_name',
415 'value' => $search_term,
416 'compare' => 'LIKE',
417 ),
418 array(
419 'key' => 'last_name',
420 'value' => $search_term,
421 'compare' => 'LIKE',
422 ),
423 array(
424 'key' => 'user_email',
425 'value' => $search_term,
426 'compare' => 'LIKE',
427 ),
428 );
429 }
430
431 // Create the WP_User_Query object
432 $get_users = ( new WP_User_Query( $args ) )->get_results();
433
434 $users = array();
435
436 if ( ! empty( $get_users ) ) {
437 foreach ( $get_users as $user ) {
438 $users[] = array(
439 'id' => $user->ID,
440 'name' => $user->first_name . ' ' . $user->last_name,
441 );
442 }
443 }
444
445 return $users;
446 }
447
448 /**
449 * Search Users by Email.
450 *
451 * @param string $search_term Search Term.
452 * @return array
453 */
454 public static function user_email( $search_term = '' ) {
455 $args = array(
456 'role__in' => array( 'author', 'editor', 'administrator', 'shop_manager' ),
457 'order' => 'ASC',
458 'orderby' => 'display_name',
459 'number' => 20,
460 );
461
462 if ( ! empty( $search_term ) ) {
463 $args['search'] = '*' . esc_attr( $search_term ) . '*';
464 }
465
466 if ( filter_var( $search_term, FILTER_VALIDATE_EMAIL ) ) {
467 // valid email address
468 $args['search_columns'] = array( 'user_email' );
469 } else {
470 // invalid address
471 $args['meta_query'] = array( //phpcs:ignore
472 'relation' => 'OR',
473 array(
474 'key' => 'first_name',
475 'value' => $search_term,
476 'compare' => 'LIKE',
477 ),
478 array(
479 'key' => 'last_name',
480 'value' => $search_term,
481 'compare' => 'LIKE',
482 ),
483 array(
484 'key' => 'user_email',
485 'value' => $search_term,
486 'compare' => 'LIKE',
487 ),
488 );
489 }
490
491 // Create the WP_User_Query object
492 $get_users = ( new WP_User_Query( $args ) )->get_results();
493
494 $users = array();
495
496 if ( ! empty( $get_users ) ) {
497 foreach ( $get_users as $user ) {
498 $users[] = array(
499 'id' => $user->ID,
500 'name' => $user->user_email,
501 );
502 }
503 }
504
505 return $users;
506 }
507
508 /**
509 * Search Attributes by Attribute Name.
510 *
511 * @param string $search_term Search Term.
512 * @return array
513 */
514 protected static function get_global_attributes( $search_term ) {
515 $global_attributes = Cache::remember(
516 'wc_global_attributes',
517 array(
518 new Model,
519 'wc_global_attribute_query',
520 ),
521 array( $search_term )
522 );
523 $result = array();
524
525 if ( is_array( $global_attributes ) && ! empty( $global_attributes ) ) {
526 foreach ( $global_attributes as $attribute ) {
527 if ( ! isset( $attribute->attribute_name, $attribute->attribute_label ) ) {
528 continue;
529 }
530
531 $result[] = array(
532 'id' => $attribute->attribute_name,
533 'name' => $attribute->attribute_label,
534 );
535 }
536 }
537
538 return $result;
539 }
540
541 /**
542 * Get all product custom attributes.
543 *
544 * @param string $search_term Search Term.
545 * @return array
546 */
547 protected static function get_custom_attributes( $search_term ) {
548 $custom_attributes = Cache::remember(
549 'wc_custom_attributes',
550 array(
551 new \Disco\App\Utility\Model,
552 'wc_custom_attribute_query',
553 ),
554 array( $search_term )
555 );
556 $result = array();
557
558 if ( is_array( $custom_attributes ) && ! empty( $custom_attributes ) ) {
559 foreach ( $custom_attributes as $value ) {
560 $product_attr = maybe_unserialize( $value->type );
561
562 if ( ! is_array( $product_attr ) ) {
563 continue;
564 }
565
566 $result = array_merge( $result, self::filter_product_attributes( $product_attr, $search_term ) );
567 }
568 }
569
570 return $result;
571 }
572
573 /**
574 * Filter Product Attributes.
575 *
576 * @param array $product_attr Product Attributes.
577 * @param string $search_term Search Term.
578 * @return array
579 */
580 protected static function filter_product_attributes( $product_attr, $search_term ) {
581 $filtered_attrs = array();
582
583 foreach ( $product_attr as $key => $arr_value ) {
584 if ( strpos( $key, 'pa_' ) !== false ) {
585 continue;
586 }
587
588 if ( ! empty( $search_term ) && ( stripos( $arr_value['name'], $search_term ) === false ) ) {
589 continue;
590 }
591
592 $filtered_attrs[] = array(
593 'id' => $key,
594 'name' => ucwords( str_replace( '-', ' ', $arr_value['name'] ) ),
595 );
596 }
597
598 return $filtered_attrs;
599 }
600
601 }
602