PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.1.3
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.1.3
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Background / CustomerSyncService.php
CustomerSyncService.php
155 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 namespace SureCart\Background;
5
6 use SureCart\Models\Customer;
7 use SureCart\Models\Purchase;
8 use SureCart\Models\User;
9
10 /**
11 * Syncs customer records to WordPress users.
12 */
13 class CustomerSyncService {
14 /**
15 * Bootstrap any actions.
16 *
17 * @return void
18 */
19 public function bootstrap() {
20 add_action( 'surecart/sync/customers', [ $this, 'sync' ], 10, 4 );
21 add_action( 'admin_notices', [ $this, 'showSyncNotice' ] );
22 }
23
24 /**
25 * Is this sync running.
26 *
27 * @return boolean
28 */
29 public function isRunning() {
30 return as_has_scheduled_action( 'surecart/sync/customer' ) || as_has_scheduled_action( 'surecart/sync/customers' );
31 }
32
33 /**
34 * Show an admin notice if customers are being synced.
35 *
36 * @return void
37 */
38 public function showSyncNotice() {
39 if ( ! $this->isRunning() ) {
40 return;
41 }
42
43 echo wp_kses_post(
44 \SureCart::notices()->render(
45 [
46 'type' => 'info',
47 'title' => esc_html__( 'SureCart customer sync in progress', 'surecart' ),
48 'text' => '<p>' . esc_html__( 'SureCart is syncing customers in the background. The process may take a little while, so please be patient.', 'surecart' ) . '</p>',
49 ]
50 )
51 );
52 }
53
54 /**
55 * Sync customers.
56 *
57 * @param string $page Current page.
58 * @param integer $batch_size Batch size.
59 * @param boolean $create_user Create user.
60 * @param boolean $run_actions Run actions.
61 *
62 * @return void
63 */
64 public function sync( $page, $batch_size, $create_user, $run_actions ) {
65 // get customers.
66 $customers = Customer::with( [ 'purchases' ] )->paginate(
67 [
68 'per_page' => $batch_size,
69 'page' => $page,
70 ]
71 );
72
73 // enqueue actions to sync an individual customer.
74 foreach ( $customers->data as $customer ) {
75 $this->syncCustomer( $customer, $create_user, $run_actions );
76 }
77
78 $total_pages = ceil( $customers->pagination->count / $customers->pagination->limit );
79 // if the total number of pages less than or equal to the current page, we don't have another page.
80 if ( $total_pages <= $customers->pagination->page ) {
81 // we don't have another page.
82 return;
83 }
84
85 // get the next batch.
86 return as_enqueue_async_action(
87 'surecart/sync/customers',
88 [
89 'page' => $page + 1,
90 'batch_size' => $batch_size,
91 'create_user' => $create_user,
92 'run_actions' => $run_actions,
93 ],
94 'surecart'
95 );
96 }
97
98 /**
99 * Sync an individual customer.
100 *
101 * @param \SureCart\Models\Customer $customer Customer.
102 * @param boolean $create_user Create user.
103 * @param boolean $run_actions Run actions.
104 *
105 * @return \SureCart\Models\Customer|\WP_Error
106 */
107 public function syncCustomer( $customer, $create_user, $run_actions ) {
108 if ( ! $customer || is_wp_error( $customer ) ) {
109 return $customer;
110 }
111
112 // create user.
113 if ( $create_user ) {
114 $user = $customer->getUser();
115 if ( ! $user ) {
116 $user = User::getUserBy( 'email', $customer->email );
117 if ( $user ) {
118 $user->setCustomerId( $customer->id, $customer->live_mode ? 'live' : 'test' );
119 } else {
120 $customer->createUser();
121 }
122 }
123 }
124
125 // run purchase actions.
126 if ( $run_actions ) {
127 $unrevoked_purchases = array_filter(
128 $customer->purchases->data ?? [],
129 function( $purchase ) {
130 return ! $purchase->revoked;
131 }
132 );
133
134 // A customer has more than the 20 purchases that are expanded, we need to fetch the rest.
135 if ( ! empty( $customer->purchases->pagination->count ) && $customer->purchases->pagination->count > $customer->purchases->pagination->limit ) {
136 // get 100 purchases.
137 $unrevoked_purchases = Purchase::where(
138 [
139 'customer_id' => $customer->id,
140 'revoked' => false,
141 ]
142 )->get();
143 }
144
145 foreach ( $unrevoked_purchases as $purchase ) {
146 if ( $purchase->getWPUser() ) {
147 do_action( 'surecart/purchase_invoked', $purchase );
148 }
149 }
150 }
151
152 return $customer;
153 }
154 }
155