forked from clifgriffin/shopp
/
customer.php
executable file
·499 lines (438 loc) · 16.5 KB
/
customer.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<?php
/**
* Customer API
*
* Plugin api function for customers
*
* @copyright Ingenesis Limited, June 23, 2011
* @license GNU GPL version 3 ( or later = false ) {@see license.txt}
* @package Shopp/API/Customer
* @version 1.0
* @since 1.2
**/
defined( 'WPINC' ) || header( 'HTTP/1.1 403' ) & exit; // Prevent direct access
/**
* Get a ShoppCustomer with ShoppBilling and ShoppShipping
*
* @api
* @since 1.2
*
* @param int $customer (optional) customer id, WordPress user associated customer, email address associated with customer, or false to load the current global customer object
* @param string $key (optional default:customer) customer for lookup by customer id, wpuser to lookup by WordPress user, or email to lookup by email address
* @return mixed, stdClass representation of the customer, bool false on failure
**/
function shopp_customer ( $customer = false, $key = 'customer' ) {
$Customer = false;
if ( ! $customer ) {
$Customer = &ShoppCustomer();
return $Customer;
}
if ( 'wpuser' == $key ) {
if ( 'wordpress' != shopp_setting('account_system') ) {
shopp_debug(__FUNCTION__ . " failed: Customer $customer could not be found.");
return false;
}
$Customer = new ShoppCustomer($customer, 'wpuser');
} else if ( 'email' == $key ) {
$Customer = new ShoppCustomer($customer, 'email');
} else {
$Customer = new ShoppCustomer($customer);
}
if ( ! $Customer->id ) {
shopp_debug(__FUNCTION__ . " failed: Customer $customer could not be found.");
return false;
}
$Customer->Billing = new BillingAddress($Customer->id, 'customer');
$Customer->Shipping = new ShippingAddress($Customer->id, 'customer');
if ( ! $Customer->id ) $Customer->Shipping->copydata($Customer->Billing);
return $Customer;
}
/**
* Find out if the customer exists
*
* @api
* @since 1.2
*
* @param int $customer (required) customer id, WordPress user associated customer, or email address associated with customer.
* @param string $key (optional default:customer) customer for lookup by customer id, wpuser to lookup by WordPress user, or email to lookup by email address
* @return bool true if the customer exists, else false
**/
function shopp_customer_exists ( $customer = false, $key = 'customer' ) {
if ( ! $customer ) {
shopp_debug(__FUNCTION__ . " failed: customer parameter required.");
return false;
}
$Customer = shopp_customer($customer, $key);
return ( false != $Customer );
}
/**
* Set or get the marketing status for a customer.
*
* @api
* @since 1.2
*
* @param int $customer customer id to check or set
* @param mixed $flag (optional default:null) null to return the marketing status, true to turn on marketing for a customer, false to turn off marketing for a customer
* @return bool true if marketing accepted, false on failure and if marketing is not accepted.
**/
function shopp_customer_marketing ( $customer = false, $flag = null ) {
$Customer = shopp_customer($customer);
if ( $Customer ) {
if ( null === $flag ) return (isset($Customer->marketing) && "yes" == $Customer->marketing);
$Customer->marketing = ( $flag ? "yes" : "no" );
$Customer->save();
return "yes" == $Customer->marketing;
}
return false;
}
/**
* Get a list of customer names, type, and email addresses for marketing
*
* @api
* @since 1.2
*
* @param bool $exclude true to exclude customers that do not allow marketing, false to include all customers
* @return array list of customers for marketing
**/
function shopp_customer_marketing_list ( $exclude = false ) {
$table = ShoppDatabaseObject::tablename(ShoppCustomer::$table);
$where = ( $exclude ? "WHERE marketing='yes'" : "");
$results = db::query( "SELECT id, firstname, lastname, email, marketing, type FROM $table $where", 'array' );
$marketing = array();
foreach ( $results as $c ) {
if ( ! isset($c->id) ) continue;
$marketing[$c->id] = $c;
}
return $marketing;
}
/**
* Create a new customer record
*
* @api
* @since 1.2
*
* @param array $data data to create the new customer record from, including: wpuser, firstname, lastname, email, phone, company, marketing, type, saddress, sxaddress, scity, sstate, scountry, spostcode, sgeocode, residential, baddress, bxaddress, bcity, bstate, bcountry, bpostcode, bgeocode
* @return bool|int returns false on failure, and the new customer id on success
**/
function shopp_add_customer ( $data = array() ) {
if ( empty($data) ) {
shopp_debug("shopp_add_customer - no customer data supplied.");
return false;
}
$map = array('wpuser', 'firstname', 'lastname', 'email', 'phone', 'company', 'marketing', 'type');
$address_map = array( 'saddress' => 'address', 'baddress' => 'address', 'sxaddress' => 'xaddress', 'bxaddress' => 'xaddress', 'scity' => 'city', 'bcity' => 'city', 'sstate' => 'state', 'bstate' => 'state', 'scountry' => 'country', 'bcountry' => 'country', 'spostcode' => 'postcode', 'bpostcode' => 'postcode', 'sgeocode' => 'geocode', 'bgeocode' => 'geocode', 'residential'=>'residential' );
// handle duplicate or missing wpuser
if ( isset($data['wpuser']) ) {
$c = new ShoppCustomer($data['wpuser'], 'wpuser');
if ( $c->id ) {
shopp_debug(__FUNCTION__ . " failed: Customer with WordPress user id {$data['wpuser']} already exists.");
return false;
}
} else if ( "wordpress" == shopp_setting('account_system') ) {
shopp_debug(__FUNCTION__ . " failed: Wordpress account id must by specified in data array with key wpuser.");
return false;
}
// handle duplicate or missing email address
if ( isset($data['email']) ) {
$c = new ShoppCustomer($data['email'], 'email');
if ( $c->id ) {
shopp_debug(__FUNCTION__ . " failed: Customer with email {$data['email']} already exists.");
return false;
}
} else {
shopp_debug(__FUNCTION__ . " failed: Email address must by specified in data array with key email.");
return false;
}
// handle missing first or last name
if ( ! isset($data['firstname']) || ! isset($data['lastname']) ) {
shopp_debug("shopp_add_customer failure: Data array missing firstname or lastname.");
return false;
}
$shipping = array();
$billing = array();
$Customer = new ShoppCustomer();
foreach ( $data as $key => $value ) {
if ( in_array($key, $map) ) $Customer->{$key} = $value;
elseif( SHOPP_DEBUG && ! in_array( $key, array_keys($address_map) ) )
shopp_debug("shopp_add_customer notice: Invalid customer data $key");
if ( in_array( $key, array_keys($address_map) ) ) {
$type = ( 's' == substr($key, 0, 1) ? 'shipping' : 'billing' );
${$type}[$address_map[$key]] = $value;
}
}
$Customer->save();
if ( ! $Customer->id ) {
shopp_debug(__FUNCTION__ . " failed: Could not create customer.");
return false;
}
if ( ! empty($shipping) ) shopp_add_customer_address( $Customer->id, $shipping, 'shipping' );
if ( ! empty($billing) ) shopp_add_customer_address( $Customer->id, $billing, 'billing' );
return $Customer->id;
} // end shopp_add_customer
/**
* Update customer information for a given customer record
*
* @api
* @since 1.3
*
* @param int $customer The ID of the customer record to update
* @param array $data An associative array of customer data to update
* @return int|bool Customer ID on success, false otherwise
**/
function shopp_set_customer ( $customer = false, $data = array() ) {
if ( ! $customer ) {
shopp_debug(__FUNCTION__ . " failed: customer parameter required.");
return false;
}
if ( empty($data) ) {
shopp_debug(__FUNCTION__ . " failed: no customer data supplied.");
return false;
}
$Customer = new ShoppCustomer($customer);
if ( empty($Customer->id) ) {
shopp_debug(__FUNCTION__ . " failed: No such customer with id $customer");
return false;
}
$map = array('wpuser', 'firstname', 'lastname', 'email', 'phone', 'company', 'marketing', 'type');
$address_map = array( 'saddress' => 'address', 'baddress' => 'address', 'sxaddress' => 'xaddress', 'bxaddress' => 'xaddress', 'scity' => 'city', 'bcity' => 'city', 'sstate' => 'state', 'bstate' => 'state', 'scountry' => 'country', 'bcountry' => 'country', 'spostcode' => 'postcode', 'bpostcode' => 'postcode', 'sgeocode' => 'geocode', 'bgeocode' => 'geocode', 'residential'=>'residential' );
// handle duplicate or missing wpuser
if ( isset($data['wpuser']) ) {
$c = new ShoppCustomer($data['wpuser'], 'wpuser');
if ( $c->id ) {
shopp_debug(__FUNCTION__ . " failed: Customer with WordPress user id {$data['wpuser']} already exists.");
return false;
}
} else if ( "wordpress" == shopp_setting('account_system') ) {
shopp_debug(__FUNCTION__ . " failed: Wordpress account id must by specified in data array with key wpuser.");
return false;
}
// handle duplicate or missing email address
if ( isset($data['email']) ) {
$c = new ShoppCustomer($data['email'], 'email');
if ( $c->id ) {
shopp_debug(__FUNCTION__ . " failed: Customer with email {$data['email']} already exists.");
return false;
}
} else {
shopp_debug(__FUNCTION__ . " failed: Email address must by specified in data array with key email.");
return false;
}
// handle missing first or last name
if ( ! isset($data['firstname']) || ! isset($data['lastname']) ) {
shopp_debug("shopp_add_customer failure: Data array missing firstname or lastname.");
return false;
}
$shipping = array();
$billing = array();
foreach ( $data as $key => $value ) {
if ( in_array($key, $map) ) $Customer->{$key} = $value;
elseif( SHOPP_DEBUG && ! in_array( $key, array_keys($address_map) ) )
shopp_debug("shopp_add_customer notice: Invalid customer data $key");
if ( in_array( $key, array_keys($address_map) ) ) {
$type = ( 's' == substr($key, 0, 1) ? 'shipping' : 'billing' );
${$type}[$address_map[$key]] = $value;
}
}
$Customer->save();
if ( ! $Customer->id ) {
shopp_debug(__FUNCTION__ . " failed: Could not create customer.");
return false;
}
if ( ! empty($shipping) ) shopp_add_customer_address( $Customer->id, $shipping, 'shipping' );
if ( ! empty($billing) ) shopp_add_customer_address( $Customer->id, $billing, 'billing' );
return $Customer->id;
} // end shopp_set_customer
/**
* An alias for shopp_add_customer_address
*
* @see shopp_add_customer_address
*
* @api
* @since 1.2
*
* @param int $customer (required) the customer id the address is added to
* @param array $data (required) key value pairs for address, values can be keyed 'address', 'xaddress', 'city', 'state', 'postcode', 'country', 'geocode', and 'residential' (residential added to shipping address)
* @param string $type (optional default: billing) billing, shipping, or both
* @return mixed int id for one address creation/update, array of ids if created/updated both shipping and billing, bool false on error
*/
function shopp_set_customer_address ( $customer = false, $data = false, $type = 'billing' ) {
return shopp_add_customer_address ( $customer, $data, $type );
}
/**
* Add or update an address for a customer
*
* @api
* @since 1.2
*
* @param int $customer (required) the customer id the address is added to
* @param array $data (required) key value pairs for address, values can be keyed 'address', 'xaddress', 'city', 'state', 'postcode', 'country', 'geocode', and 'residential' (residential added to shipping address)
* @param string $type (optional default: billing) billing, shipping, or both
* @return mixed int id for one address creation/update, array of ids if created/updated both shipping and billing, bool false on error
**/
function shopp_add_customer_address ( $customer = false, $data = false, $type = 'billing' ) {
if ( ! $customer ) {
shopp_debug(__FUNCTION__ . " failed: Customer id required.");
return false;
}
if ( empty($data) ) {
shopp_debug(__FUNCTION__ . " failed: data array is empty");
return false;
}
$map = array( 'address', 'xaddress', 'city', 'state', 'postcode', 'country', 'geocode', 'residential' );
$address = array();
foreach ( $map as $property ) {
if ( isset($data[$property]) ) $address[$property] = $data[$property];
if ( isset($data[$property]) && 'residential' == $property ) $address[$property] = Shopp::str_true($data[$property]) ? "on" : "off";
}
if ( in_array($type, array('billing','both')) ) {
$Billing = new BillingAddress($customer, 'customer');
$Billing->customer = $customer;
}
if ( in_array($type, array('shipping','both')) ) {
$Shipping = new ShippingAddress($customer, 'customer');
$Shipping->customer = $customer;
}
if ( 'billing' == $type ) {
$Billing->updates($address);
if ( apply_filters('shopp_validate_address', true, $Billing) ) {
$Billing->save();
return $Billing->id;
}
} else if ( 'shipping' == $type ) {
$Shipping->updates($address);
if ( apply_filters('shopp_validate_address', true, $Shipping) ) {
$Shipping->save();
return $Shipping->id;
}
} else { // both
$Billing->updates($address);
$Shipping->updates($address);
if ( apply_filters('shopp_validate_address', true, $Billing) && apply_filters('shopp_validate_address', true, $Shipping) ) {
$Billing->save();
$Shipping->save();
return array('billing' => $Billing->id, 'shipping' => $Shipping->id);
}
}
shopp_debug(__FUNCTION__ . " failed: one or more addresses did not validate.");
return false;
}
/**
* Remove a customer, and data associated with the customer
*
* @api
* @since 1.2
*
* @param int $customer (required) id of the customer to remove
* @return bool true on success, false on failure
**/
function shopp_rmv_customer ( $customer = false ) {
if ( ! $customer ) {
shopp_debug(__FUNCTION__ . " failed: Customer id required.");
return false;
}
$Customer = new ShoppCustomer($customer);
if ( empty($Customer->id) ) {
shopp_debug("shopp_rmv_customer notice: No such customer with id $customer");
return false;
}
// Remove Addresses
$Billing = new BillingAddress($customer, 'customer');
$Shipping = new ShippingAddress($customer, 'customer');
if ( ! empty($Billing->id) ) $Billing->delete();
if ( ! empty($Shipping->id) ) $Shipping->delete();
// Remove Meta records
$metas = shopp_meta ( $customer, 'customer' );
foreach( $metas as $meta ) shopp_rmv_meta ( $meta->id );
// Remove Customer record
$Customer->delete();
return true;
}
/**
* Return an address record by customer id
*
* @api
* @since 1.2
*
* @param int $id (required) the address id to retrieve, or customer id
* @param string $type (optional default:billing) 'billing' to lookup billing address by customer id, 'shipping' to lookup shipping adress by customer id, or 'id' to lookup by address id
* @return ShoppAddress object
**/
function shopp_address ( $id = false, $type = 'billing' ) {
if ( ! $id ) {
shopp_debug(__FUNCTION__ . " failed: Missing id parameter.");
return false;
}
if ( 'billing' == $type ) {
// use customer id to find billing address
$Address = new BillingAddress($id, 'customer');
} else if ( 'shipping' == $type ) {
// use customer id to find shipping address
$Address = new ShippingAddress($id, 'customer');
} else {
// lookup by address id
$Address = new ShoppAddress($id, 'id');
}
if ( empty($Address->id) ) {
shopp_debug(__FUNCTION__ . " failed: No such address with id $address.");
return false;
}
return $Address;
}
/**
* Get count of addresses stored on customer record
*
* @api
* @since 1.2
*
* @param int $customer (required) the customer id
* @return int number of address records that exist for the customer
**/
function shopp_customer_address_count ( $customer = false ) {
if ( ! $customer ) {
shopp_debug(__FUNCTION__ . " failed: customer id required.");
return false;
}
$table = ShoppDatabaseObject::tablename(Address::$table);
$customer = db::escape($customer);
$results = db::query("SELECT COUNT(*) as addresses FROM $table WHERE customer=$customer");
return ( is_object($results) && $results->addresses ? $results->addresses : 0 );
}
/**
* Get list of addresses for a customer
*
* @api
* @since 1.2
*
* @param int $customer (required) the customer id
* @return array list of addresses
**/
function shopp_customer_addresses ( $customer = false ) {
if ( ! $customer ) {
shopp_debug(__FUNCTION__ . " failed: customer id required.");
return false;
}
$Billing = shopp_address($customer, 'billing');
$Shipping = shopp_address($customer, 'shipping');
return array( 'billing' => $Billing, 'shipping' => $Shipping );
}
/**
* Remove an address
*
* @api
* @since 1.2
*
* @param int $address the address id to remove
* @return bool true on success, false on failure
**/
function shopp_rmv_customer_address ( $address = false ) {
if ( ! $address ) {
shopp_debug(__FUNCTION__ . " failed: Missing address id parameter.");
return false;
}
$Address = new ShoppAddress($address);
if ( empty($Address->id) ) {
shopp_debug(__FUNCTION__ . " failed: No such address with id $address.");
return false;
}
return $Address->delete();
}