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