/var/www/html_uk/wp-content/plugins/automatewoo/includes/Customer_Factory.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php

namespace AutomateWoo;

use 
WC_Order;
use 
WP_User;

/**
 * @class Customer_Factory
 * @since 3.0.0
 */
class Customer_Factory extends Factory {

    
/**
     * Model class to use for an object.
     *
     * @var string
     */
    
public static $model 'AutomateWoo\Customer';


    
/**
     * Get a customer.
     *
     * @param int $customer_id
     *
     * @return Customer|bool
     */
    
public static function get$customer_id ) {
        return 
parent::get$customer_id );
    }


    
/**
     * Get a customer from a user ID.
     *
     * @param int  $user_id
     * @param bool $create  Create the customer record if it doesn't exist.
     *
     * @return Customer|bool Returns false if there is no user ID or customer record can't be created (create = true).
     */
    
public static function get_by_user_id$user_id$create true ) {

        if ( ! 
$user_id ) {
            
Logger::error'customer''No user ID provided for getting a customer.' );
            return 
false;
        }

        
$user_id Clean::id$user_id );

        if ( 
Cache::exists$user_id'customer_user_id' ) ) {
            return static::
getCache::get$user_id'customer_user_id' ) );
        }

        
$customer = new Customer();
        
$customer->get_by'user_id'$user_id );

        if ( 
$customer->exists ) {
            return 
$customer;
        }

        if ( 
$create ) {
            
// attempt to create the customer
            
$customer = static::create_from_user$user_id );

            if ( 
$customer ) {
                return 
$customer;
            }
        }

        return 
false;
    }


    
/**
     * Get a customer object from a guest ID.
     *
     * @param int  $guest_id Guest ID.
     * @param bool $create   Create the customer record if it doesn't exist.
     *
     * @return Customer|bool Returns false if there is no guest ID or customer record can't be created (create = true).
     */
    
public static function get_by_guest_id$guest_id$create true ) {

        if ( ! 
$guest_id ) {
            
Logger::error'customer''No guest ID provided for getting a customer.' );
            return 
false;
        }

        
$guest_id Clean::id$guest_id );

        if ( 
Cache::exists$guest_id'customer_guest_id' ) ) {
            return static::
getCache::get$guest_id'customer_guest_id' ) );
        }

        
$customer = new Customer();
        
$customer->get_by'guest_id'$guest_id );

        if ( 
$customer->exists ) {
            return 
$customer;
        }

        if ( 
$create ) {
            
// attempt to create the customer
            
$customer = static::create_from_guest$guest_id );

            if ( 
$customer ) {
                return 
$customer;
            }
        }

        return 
false;
    }


    
/**
     * Get a customer object from an email address linked to a user or a guest.
     *
     * @param string $email
     * @param bool   $create Create the customer record if it doesn't exist.
     * @return Customer|bool Returns false if there is no valid email or customer record can't be created (create = true).
     */
    
public static function get_by_email$email$create true ) {

        if ( ! 
is_email$email ) ) {
            return 
false;
        }

        
$email Clean::email$email );

        
// check for matching user
        
$user get_user_by'email'$email );
        if ( 
$user ) {
            return static::
get_by_user_id$user->ID$create );
        }

        
// check for matching guest
        
$guest Guest_Factory::get_by_email$email );
        if ( 
$guest ) {
            return static::
get_by_guest_id$guest->get_id(), $create );
        }

        if ( 
$create ) {
            
// create guest for new customer
            
$guest Guest_Factory::create$email );
            return static::
get_by_guest_id$guest->get_id() );
        }

        return 
false;
    }


    
/**
     * Get a customer by key.
     *
     * @param string $key
     *
     * @return Customer|bool Returns false if there is no key or customer can't be found.
     */
    
public static function get_by_key$key ) {

        if ( ! 
$key ) {
            
Logger::error'customer''No key provided for getting a customer.' );
            return 
false;
        }

        
$key Clean::string$key );

        if ( 
Cache::exists$key'customer_key' ) ) {
            return static::
getCache::get$key'customer_key' ) );
        }

        
$customer = new Customer();
        
$customer->get_by'id_key'$key );

        if ( 
$customer->exists ) {
            return 
$customer;
        }

        return 
false;
    }


    
/**
     * Get a customer from a user object.
     *
     * @param WP_User|Order_Guest $user   User data item.
     * @param bool                $create Create the customer record if it doesn't exist.
     *
     * @return Customer|bool Returns false if the data item is not valid or customer record can't be created (create = true).
     */
    
public static function get_by_user_data_item$user$create true ) {
        if ( 
is_a$user'WP_User' ) ) {
            return static::
get_by_user_id$user->ID$create );
        } elseif ( 
is_a$user'AutomateWoo\Order_Guest' ) ) {
            return static::
get_by_email$user->user_email$create );
        }

        
Logger::error'customer''No valid user object provided for getting a customer.' );
        return 
false;
    }


    
/**
     * Get a customer object from the customer/guest that purchased the order.
     *
     * @param WC_Order $order  Order object.
     * @param bool     $create Create the customer record if it doesn't exist.
     * @param bool     $log_error Log an error if the order can't be found.
     *
     * @return Customer|bool Returns false if there is no order or customer record can't be created (create = true).
     */
    
public static function get_by_order$order$create true$log_error true ) {
        if ( ! 
$order || ! is_a$order'WC_Order' ) ) {
            if ( 
$log_error ) {
                
Logger::error'customer''No valid order provided for getting a customer.' );
            }
            return 
false;
        }

        if ( 
$order->get_user_id() ) {
            return static::
get_by_user_id$order->get_user_id(), $create );
        } else {
            return static::
get_by_email$order->get_billing_email(), $create );
        }
    }


    
/**
     * Get a customer from a review.
     *
     * @param Review $review
     * @param bool   $create Create the customer record if it doesn't exist.
     *
     * @return Customer|bool Returns false if there is no review or customer record can't be created (create = true).
     */
    
public static function get_by_review$review$create true ) {
        if ( ! 
$review || ! is_a$review'AutomateWoo\Review' ) ) {
            
Logger::error'customer''No valid review provided for getting a customer.' );
            return 
false;
        }

        if ( 
$review->get_user_id() ) {
            return static::
get_by_user_id$review->get_user_id(), $create );
        } else {
            return static::
get_by_email$review->get_email(), $create );
        }
    }


    
/**
     * Create a customer record from a user ID.
     *
     * @param int $user_id
     *
     * @return Customer|bool Returns false if the user is not found.
     */
    
private static function create_from_user$user_id ) {

        
$user get_userdata$user_id );

        if ( ! 
$user ) {
            
Logger::error'customer'sprintf'User %d not found when creating customer.'$user_id ) );
            return 
false;
        }

        
$customer = new Customer();
        
$customer->set_user_id$user_id );
        
$customer->set_key( static::generate_unique_customer_key() );
        
$customer->save();

        return 
$customer;
    }


    
/**
     * Create a customer record from a guest ID.
     *
     * @param int $guest_id
     *
     * @return Customer|bool Returns false if the guest is not found.
     */
    
private static function create_from_guest$guest_id ) {

        
$guest Guest_Factory::get$guest_id );

        if ( ! 
$guest ) {
            
Logger::error'customer'sprintf'Guest %d not found when creating customer.'$guest_id ) );
            return 
false;
        }

        
$customer = new Customer();
        
$customer->set_guest_id$guest_id );
        
$customer->set_key( static::generate_unique_customer_key() );
        
$customer->save();

        return 
$customer;
    }


    
/**
     * Generates a unique customer key (which doesn't exist yet in the DB).
     *
     * @return string
     */
    
private static function generate_unique_customer_key() {

        
$key aw_generate_key20false );

        
$query = new Customer_Query();
        
$query->where'id_key'$key );

        
// If the customer ID already exists, then generate another one.
        
if ( $query->has_results() ) {
            return static::
generate_unique_customer_key();
        }

        return 
$key;
    }


    
/**
     * Updates a customer in the cache.
     *
     * @param Customer $customer
     */
    
public static function update_cache$customer ) {
        
parent::update_cache$customer );

        if ( 
$customer->get_user_id() ) {
            
Cache::set$customer->get_user_id(), $customer->get_id(), 'customer_user_id' );
        }

        if ( 
$customer->get_guest_id() ) {
            
Cache::set$customer->get_guest_id(), $customer->get_id(), 'customer_guest_id' );
        }

        
Cache::set$customer->get_key(), $customer->get_id(), 'customer_key' );
    }


    
/**
     * Clears a customer from the cache.
     *
     * @param Customer $customer
     */
    
public static function clean_cache$customer ) {
        
parent::clean_cache$customer );

        static::
clear_cached_prop$customer'user_id''customer_user_id' );
        static::
clear_cached_prop$customer'guest_id''customer_guest_id' );
        static::
clear_cached_prop$customer'id_key''customer_key' );
    }
}