CustomersScheduler.php
3 years ago
ImportInterface.php
4 years ago
ImportScheduler.php
1 month ago
MailchimpScheduler.php
1 year ago
OrdersScheduler.php
1 month ago
CustomersScheduler.php
147 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Customer syncing related functions and actions. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Internal\Admin\Schedulers; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache; |
| 11 | use Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore; |
| 12 | |
| 13 | /** |
| 14 | * CustomersScheduler Class. |
| 15 | */ |
| 16 | class CustomersScheduler extends ImportScheduler { |
| 17 | /** |
| 18 | * Slug to identify the scheduler. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | public static $name = 'customers'; |
| 23 | |
| 24 | /** |
| 25 | * Attach customer lookup update hooks. |
| 26 | * |
| 27 | * @internal |
| 28 | */ |
| 29 | public static function init() { |
| 30 | CustomersDataStore::init(); |
| 31 | parent::init(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Add customer dependencies. |
| 36 | * |
| 37 | * @internal |
| 38 | * @return array |
| 39 | */ |
| 40 | public static function get_dependencies() { |
| 41 | return array( |
| 42 | 'delete_batch_init' => OrdersScheduler::get_action( 'delete_batch_init' ), |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Get the customer IDs and total count that need to be synced. |
| 48 | * |
| 49 | * @internal |
| 50 | * @param int $limit Number of records to retrieve. |
| 51 | * @param int $page Page number. |
| 52 | * @param int|bool $days Number of days prior to current date to limit search results. |
| 53 | * @param bool $skip_existing Skip already imported customers. |
| 54 | */ |
| 55 | public static function get_items( $limit = 10, $page = 1, $days = false, $skip_existing = false ) { |
| 56 | $customer_roles = apply_filters( 'woocommerce_analytics_import_customer_roles', array( 'customer' ) ); |
| 57 | $query_args = array( |
| 58 | 'fields' => 'ID', |
| 59 | 'orderby' => 'ID', |
| 60 | 'order' => 'ASC', |
| 61 | 'number' => $limit, |
| 62 | 'paged' => $page, |
| 63 | 'role__in' => $customer_roles, |
| 64 | ); |
| 65 | |
| 66 | if ( is_int( $days ) ) { |
| 67 | $query_args['date_query'] = array( |
| 68 | 'after' => gmdate( 'Y-m-d 00:00:00', time() - ( DAY_IN_SECONDS * $days ) ), |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | if ( $skip_existing ) { |
| 73 | add_action( 'pre_user_query', array( __CLASS__, 'exclude_existing_customers_from_query' ) ); |
| 74 | } |
| 75 | |
| 76 | $customer_query = new \WP_User_Query( $query_args ); |
| 77 | |
| 78 | remove_action( 'pre_user_query', array( __CLASS__, 'exclude_existing_customers_from_query' ) ); |
| 79 | |
| 80 | return (object) array( |
| 81 | 'total' => $customer_query->get_total(), |
| 82 | 'ids' => $customer_query->get_results(), |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Exclude users that already exist in our customer lookup table. |
| 88 | * |
| 89 | * Meant to be hooked into 'pre_user_query' action. |
| 90 | * |
| 91 | * @internal |
| 92 | * @param WP_User_Query $wp_user_query WP_User_Query to modify. |
| 93 | */ |
| 94 | public static function exclude_existing_customers_from_query( $wp_user_query ) { |
| 95 | global $wpdb; |
| 96 | |
| 97 | $wp_user_query->query_where .= " AND NOT EXISTS ( |
| 98 | SELECT ID FROM {$wpdb->prefix}wc_customer_lookup |
| 99 | WHERE {$wpdb->prefix}wc_customer_lookup.user_id = {$wpdb->users}.ID |
| 100 | )"; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get total number of rows imported. |
| 105 | * |
| 106 | * @internal |
| 107 | * @return int |
| 108 | */ |
| 109 | public static function get_total_imported() { |
| 110 | global $wpdb; |
| 111 | return $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}wc_customer_lookup" ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Imports a single customer. |
| 116 | * |
| 117 | * @internal |
| 118 | * @param int $user_id User ID. |
| 119 | * @return void |
| 120 | */ |
| 121 | public static function import( $user_id ) { |
| 122 | CustomersDataStore::update_registered_customer( $user_id ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Delete a batch of customers. |
| 127 | * |
| 128 | * @internal |
| 129 | * @param int $batch_size Number of items to delete. |
| 130 | * @return void |
| 131 | */ |
| 132 | public static function delete( $batch_size ) { |
| 133 | global $wpdb; |
| 134 | |
| 135 | $customer_ids = $wpdb->get_col( |
| 136 | $wpdb->prepare( |
| 137 | "SELECT customer_id FROM {$wpdb->prefix}wc_customer_lookup ORDER BY customer_id ASC LIMIT %d", |
| 138 | $batch_size |
| 139 | ) |
| 140 | ); |
| 141 | |
| 142 | foreach ( $customer_ids as $customer_id ) { |
| 143 | CustomersDataStore::delete_customer( $customer_id ); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 |