| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Utils\Traits; |
| 4 |
|
| 5 |
use StoreEngine\Addons\Subscription\Classes\Subscription; |
| 6 |
use StoreEngine\Classes\Customer as CustomerClass; |
| 7 |
use StoreEngine\Classes\Customers; |
| 8 |
use StoreEngine\Classes\Exceptions\StoreEngineException; |
| 9 |
use StoreEngine\Classes\Order as OrderClass; |
| 10 |
use StoreEngine\Classes\Orders; |
| 11 |
use StoreEngine\Classes\Subscriptions; |
| 12 |
use WP_User_Query; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
trait Customer { |
| 19 |
|
| 20 |
/** |
| 21 |
* Get Customer by id. |
| 22 |
* |
| 23 |
* @param int|null $id Customer id. |
| 24 |
* @param bool $in_session |
| 25 |
* |
| 26 |
* @return CustomerClass |
| 27 |
*/ |
| 28 |
public static function get_customer( ?int $id = null, bool $in_session = false ): CustomerClass { |
| 29 |
if ( null === $id ) { |
| 30 |
$id = get_current_user_id(); |
| 31 |
} |
| 32 |
|
| 33 |
return new CustomerClass( $id, $in_session ); |
| 34 |
} |
| 35 |
|
| 36 |
public static function get_customers( $args = array() ): array { |
| 37 |
$args = wp_parse_args( $args, array( |
| 38 |
'number' => 10, |
| 39 |
'orderby' => 'ID', |
| 40 |
'order' => 'DESC', |
| 41 |
) ); |
| 42 |
|
| 43 |
return ( new Customers() )->get_customers( $args ); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* Get orders of customer. |
| 49 |
* |
| 50 |
* @param int $customer_id [Optional] Customer id. Default to zero (current user). |
| 51 |
* |
| 52 |
* @return Subscription[] |
| 53 |
* @throws StoreEngineException |
| 54 |
* @deprecated 1.6.7 |
| 55 |
* @see \StoreEngine\Addons\Subscription\Classes\SubscriptionCollection |
| 56 |
*/ |
| 57 |
public static function get_customer_subscriptions( int $customer_id = 0, int $page = 1, int $per_page = 10 ): array { |
| 58 |
return ( new Subscriptions( $page, $per_page ) )->get( [ |
| 59 |
'customer_id' => [ |
| 60 |
'condition' => '=', |
| 61 |
'formatter' => '%d', |
| 62 |
'value' => 0 !== $customer_id ? $customer_id : get_current_user_id(), |
| 63 |
], |
| 64 |
'type' => [ |
| 65 |
'condition' => '=', |
| 66 |
'formatter' => '%s', |
| 67 |
'value' => 'subscription', |
| 68 |
], |
| 69 |
] ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get top customers. |
| 74 |
* |
| 75 |
* @return CustomerClass[] |
| 76 |
*/ |
| 77 |
public static function get_top_customers(): array { |
| 78 |
return ( new Customers() )->get_top_customers(); |
| 79 |
} |
| 80 |
|
| 81 |
public static function get_new_customers_count( $start_date, $end_date ): int { |
| 82 |
return ( new Customers() )->get_new_customers_count( $start_date, $end_date ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get all user ids who have `billing_email` set to any of the email passed in array. |
| 87 |
* |
| 88 |
* @param string[] $emails List of emails to check against. |
| 89 |
* |
| 90 |
* @return array |
| 91 |
*/ |
| 92 |
public static function get_user_ids_for_billing_email( array $emails ): array { |
| 93 |
$emails = array_unique( array_map( fn( $email ) => strtolower( sanitize_email( $email ) ), $emails ) ); |
| 94 |
$users_query = new WP_User_Query( [ |
| 95 |
'fields' => 'ID', |
| 96 |
'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 97 |
[ |
| 98 |
'key' => 'billing_email', |
| 99 |
'value' => $emails, |
| 100 |
'compare' => 'IN', |
| 101 |
], |
| 102 |
], |
| 103 |
] ); |
| 104 |
|
| 105 |
return array_unique( $users_query->get_results() ); |
| 106 |
} |
| 107 |
} |
| 108 |
|