|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* GetPaid_Customer_Data_Store class file. |
|
5
|
|
|
* |
|
6
|
|
|
*/ |
|
7
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
8
|
|
|
exit; |
|
9
|
|
|
} |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Customer Data Store: Stored in a custom table. |
|
13
|
|
|
* |
|
14
|
|
|
* @version 1.0.19 |
|
15
|
|
|
*/ |
|
16
|
|
|
class GetPaid_Customer_Data_Store { |
|
17
|
|
|
|
|
18
|
|
|
/* |
|
19
|
|
|
|-------------------------------------------------------------------------- |
|
20
|
|
|
| CRUD Methods |
|
21
|
|
|
|-------------------------------------------------------------------------- |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Method to create a new customer in the database. |
|
26
|
|
|
* |
|
27
|
|
|
* @param GetPaid_Customer $customer customer object. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function create( &$customer ) { |
|
30
|
|
|
global $wpdb; |
|
31
|
|
|
|
|
32
|
|
|
$values = array(); |
|
33
|
|
|
$formats = array(); |
|
34
|
|
|
|
|
35
|
|
|
$fields = self::get_database_fields(); |
|
36
|
|
|
unset( $fields['id'] ); |
|
37
|
|
|
|
|
38
|
|
|
foreach ( $fields as $key => $format ) { |
|
39
|
|
|
$values[ $key ] = $customer->get( $key, 'edit' ); |
|
40
|
|
|
$formats[] = $format; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$result = $wpdb->insert( $wpdb->prefix . 'getpaid_customers', $values, $formats ); |
|
44
|
|
|
|
|
45
|
|
|
if ( $result ) { |
|
46
|
|
|
$customer->set_id( $wpdb->insert_id ); |
|
47
|
|
|
$customer->apply_changes(); |
|
48
|
|
|
$customer->clear_cache(); |
|
49
|
|
|
do_action( 'getpaid_new_customer', $customer ); |
|
50
|
|
|
return true; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Method to read a customer from the database. |
|
58
|
|
|
* |
|
59
|
|
|
* @param GetPaid_Customer $customer customer object. |
|
60
|
|
|
* |
|
61
|
|
|
*/ |
|
62
|
|
|
public function read( &$customer ) { |
|
63
|
|
|
global $wpdb; |
|
64
|
|
|
|
|
65
|
|
|
$customer->set_defaults(); |
|
66
|
|
|
|
|
67
|
|
|
if ( ! $customer->get_id() ) { |
|
68
|
|
|
$customer->last_error = 'Invalid customer.'; |
|
69
|
|
|
$customer->set_id( 0 ); |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Maybe retrieve from the cache. |
|
74
|
|
|
$raw_customer = wp_cache_get( $customer->get_id(), 'getpaid_customers' ); |
|
75
|
|
|
|
|
76
|
|
|
// If not found, retrieve from the db. |
|
77
|
|
|
if ( false === $raw_customer ) { |
|
78
|
|
|
|
|
79
|
|
|
$raw_customer = $wpdb->get_row( |
|
80
|
|
|
$wpdb->prepare( |
|
81
|
|
|
"SELECT * FROM {$wpdb->prefix}getpaid_customers WHERE id = %d", |
|
82
|
|
|
$customer->get_id() |
|
83
|
|
|
) |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
// Update the cache with our data |
|
87
|
|
|
wp_cache_set( $customer->get_id(), $raw_customer, 'getpaid_customers' ); |
|
88
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ( ! $raw_customer ) { |
|
92
|
|
|
$raw_customer->last_error = 'Invalid customer.'; |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Loop through raw customer fields. |
|
97
|
|
|
foreach ( (array) $raw_customer as $key => $value ) { |
|
98
|
|
|
$customer->set( $key, $value ); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$customer->set_object_read( true ); |
|
102
|
|
|
do_action( 'getpaid_read_customer', $customer ); |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Method to update a customer in the database. |
|
108
|
|
|
* |
|
109
|
|
|
* @param GetPaid_Customer $customer Customer object. |
|
110
|
|
|
*/ |
|
111
|
|
|
public function update( &$customer ) { |
|
112
|
|
|
global $wpdb; |
|
113
|
|
|
|
|
114
|
|
|
do_action( 'getpaid_before_update_customer', $customer, $customer->get_changes() ); |
|
115
|
|
|
|
|
116
|
|
|
$changes = $customer->get_changes(); |
|
117
|
|
|
$values = array(); |
|
118
|
|
|
$format = array(); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
foreach ( self::get_database_fields() as $key => $format ) { |
|
121
|
|
|
if ( array_key_exists( $key, $changes ) ) { |
|
122
|
|
|
$values[ $key ] = $customer->get( $key, 'edit' ); |
|
123
|
|
|
$formats[] = $format; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if ( empty( $values ) ) { |
|
128
|
|
|
return; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$wpdb->update( |
|
132
|
|
|
$wpdb->prefix . 'getpaid_customers', |
|
133
|
|
|
$values, |
|
134
|
|
|
array( |
|
135
|
|
|
'id' => $customer->get_id(), |
|
136
|
|
|
), |
|
137
|
|
|
$formats, |
|
|
|
|
|
|
138
|
|
|
'%d' |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
// Apply the changes. |
|
142
|
|
|
$customer->apply_changes(); |
|
143
|
|
|
|
|
144
|
|
|
// Delete cache. |
|
145
|
|
|
$customer->clear_cache(); |
|
146
|
|
|
|
|
147
|
|
|
// Fire a hook. |
|
148
|
|
|
do_action( 'getpaid_update_customer', $customer ); |
|
149
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Method to delete a customer from the database. |
|
154
|
|
|
* |
|
155
|
|
|
* @param GetPaid_Customer $customer |
|
156
|
|
|
*/ |
|
157
|
|
|
public function delete( &$customer ) { |
|
158
|
|
|
global $wpdb; |
|
159
|
|
|
|
|
160
|
|
|
do_action( 'getpaid_before_delete_customer', $customer ); |
|
161
|
|
|
|
|
162
|
|
|
$wpdb->delete( |
|
163
|
|
|
$wpdb->prefix . 'getpaid_customers', |
|
164
|
|
|
array( |
|
165
|
|
|
'id' => $customer->get_id(), |
|
166
|
|
|
), |
|
167
|
|
|
'%d' |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
|
|
// Delete cache. |
|
171
|
|
|
$customer->clear_cache(); |
|
172
|
|
|
|
|
173
|
|
|
// Fire a hook. |
|
174
|
|
|
do_action( 'getpaid_delete_customer', $customer ); |
|
175
|
|
|
|
|
176
|
|
|
$customer->set_id( 0 ); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/* |
|
180
|
|
|
|-------------------------------------------------------------------------- |
|
181
|
|
|
| Additional Methods |
|
182
|
|
|
|-------------------------------------------------------------------------- |
|
183
|
|
|
*/ |
|
184
|
|
|
public static function get_database_fields() { |
|
185
|
|
|
|
|
186
|
|
|
$fields = array( |
|
187
|
|
|
'id' => '%d', |
|
188
|
|
|
'user_id' => '%d', |
|
189
|
|
|
'email' => '%s', |
|
190
|
|
|
'email_cc' => '%s', |
|
191
|
|
|
'status' => '%s', |
|
192
|
|
|
'purchase_value' => '%f', |
|
193
|
|
|
'purchase_count' => '%d', |
|
194
|
|
|
'date_created' => '%s', |
|
195
|
|
|
'date_modified' => '%s', |
|
196
|
|
|
'uuid' => '%s', |
|
197
|
|
|
); |
|
198
|
|
|
|
|
199
|
|
|
// Add address fields. |
|
200
|
|
|
foreach ( array_keys( getpaid_user_address_fields() ) as $field ) { |
|
201
|
|
|
|
|
202
|
|
|
// Skip id, user_id and email. |
|
203
|
|
|
if ( ! in_array( $field, array( 'id', 'user_id', 'email', 'purchase_value', 'purchase_count', 'date_created', 'date_modified', 'uuid' ), true ) ) { |
|
204
|
|
|
$fields[ $field ] = '%s'; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return $fields; |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|