1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Customer Meta DB class |
4
|
|
|
* |
5
|
|
|
* This class is for interacting with the customer meta database table |
6
|
|
|
* |
7
|
|
|
* @package Give |
8
|
|
|
* @subpackage Classes/DB Customer Meta |
9
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
10
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
11
|
|
|
* @since 1.6 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
// Exit if accessed directly |
15
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
16
|
|
|
exit; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Give_DB_Customer_Meta |
21
|
|
|
*/ |
22
|
|
|
class Give_DB_Customer_Meta extends Give_DB { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get things started |
26
|
|
|
* |
27
|
|
|
* @access public |
28
|
|
|
* @since 1.6 |
29
|
|
|
*/ |
30
|
|
|
public function __construct() { |
31
|
|
|
/* @var WPDB $wpdb */ |
32
|
|
|
global $wpdb; |
|
|
|
|
33
|
|
|
|
34
|
|
|
$this->table_name = $wpdb->prefix . 'give_customermeta'; |
35
|
|
|
$this->primary_key = 'meta_id'; |
36
|
|
|
$this->version = '1.0'; |
37
|
|
|
|
38
|
|
|
add_action( 'plugins_loaded', array( $this, 'register_table' ), 11 ); |
39
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get table columns and data types |
44
|
|
|
* |
45
|
|
|
* @access public |
46
|
|
|
* @since 1.6 |
47
|
|
|
*/ |
48
|
|
|
public function get_columns() { |
49
|
|
|
return array( |
50
|
|
|
'meta_id' => '%d', |
51
|
|
|
'customer_id' => '%d', |
52
|
|
|
'meta_key' => '%s', |
53
|
|
|
'meta_value' => '%s', |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Register the table with $wpdb so the metadata api can find it |
59
|
|
|
* |
60
|
|
|
* @access public |
61
|
|
|
* @since 1.6 |
62
|
|
|
*/ |
63
|
|
|
public function register_table() { |
64
|
|
|
global $wpdb; |
|
|
|
|
65
|
|
|
$wpdb->customermeta = $this->table_name; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Retrieve customer meta field for a customer. |
70
|
|
|
* |
71
|
|
|
* For internal use only. Use Give_Customer->get_meta() for public usage. |
72
|
|
|
* |
73
|
|
|
* @param int $customer_id Customer ID. |
74
|
|
|
* @param string $meta_key The meta key to retrieve. |
75
|
|
|
* @param bool $single Whether to return a single value. |
76
|
|
|
* |
77
|
|
|
* @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. |
78
|
|
|
* |
79
|
|
|
* @access private |
80
|
|
|
* @since 1.6 |
81
|
|
|
*/ |
82
|
|
|
public function get_meta( $customer_id = 0, $meta_key = '', $single = false ) { |
83
|
|
|
$customer_id = $this->sanitize_customer_id( $customer_id ); |
84
|
|
|
if ( false === $customer_id ) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return get_metadata( 'customer', $customer_id, $meta_key, $single ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Add meta data field to a customer. |
93
|
|
|
* |
94
|
|
|
* For internal use only. Use Give_Customer->add_meta() for public usage. |
95
|
|
|
* |
96
|
|
|
* @param int $customer_id Customer ID. |
97
|
|
|
* @param string $meta_key Metadata name. |
98
|
|
|
* @param mixed $meta_value Metadata value. |
99
|
|
|
* @param bool $unique Optional, default is false. Whether the same key should not be added. |
100
|
|
|
* |
101
|
|
|
* @return bool False for failure. True for success. |
102
|
|
|
* |
103
|
|
|
* @access private |
104
|
|
|
* @since 1.6 |
105
|
|
|
*/ |
106
|
|
|
public function add_meta( $customer_id = 0, $meta_key = '', $meta_value, $unique = false ) { |
107
|
|
|
$customer_id = $this->sanitize_customer_id( $customer_id ); |
108
|
|
|
if ( false === $customer_id ) { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return add_metadata( 'customer', $customer_id, $meta_key, $meta_value, $unique ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Update customer meta field based on Customer ID. |
117
|
|
|
* |
118
|
|
|
* For internal use only. Use Give_Customer->update_meta() for public usage. |
119
|
|
|
* |
120
|
|
|
* Use the $prev_value parameter to differentiate between meta fields with the |
121
|
|
|
* same key and Customer ID. |
122
|
|
|
* |
123
|
|
|
* If the meta field for the customer does not exist, it will be added. |
124
|
|
|
* |
125
|
|
|
* @param int $customer_id Customer ID. |
126
|
|
|
* @param string $meta_key Metadata key. |
127
|
|
|
* @param mixed $meta_value Metadata value. |
128
|
|
|
* @param mixed $prev_value Optional. Previous value to check before removing. |
129
|
|
|
* |
130
|
|
|
* @return bool False on failure, true if success. |
131
|
|
|
* |
132
|
|
|
* @access private |
133
|
|
|
* @since 1.6 |
134
|
|
|
*/ |
135
|
|
|
public function update_meta( $customer_id = 0, $meta_key = '', $meta_value, $prev_value = '' ) { |
136
|
|
|
$customer_id = $this->sanitize_customer_id( $customer_id ); |
137
|
|
|
if ( false === $customer_id ) { |
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return update_metadata( 'customer', $customer_id, $meta_key, $meta_value, $prev_value ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Remove metadata matching criteria from a customer. |
146
|
|
|
* |
147
|
|
|
* For internal use only. Use Give_Customer->delete_meta() for public usage. |
148
|
|
|
* |
149
|
|
|
* You can match based on the key, or key and value. Removing based on key and |
150
|
|
|
* value, will keep from removing duplicate metadata with the same key. It also |
151
|
|
|
* allows removing all metadata matching key, if needed. |
152
|
|
|
* |
153
|
|
|
* @param int $customer_id Customer ID. |
154
|
|
|
* @param string $meta_key Metadata name. |
155
|
|
|
* @param mixed $meta_value Optional. Metadata value. |
156
|
|
|
* |
157
|
|
|
* @return bool False for failure. True for success. |
158
|
|
|
* |
159
|
|
|
* @access private |
160
|
|
|
* @since 1.6 |
161
|
|
|
*/ |
162
|
|
|
public function delete_meta( $customer_id = 0, $meta_key = '', $meta_value = '' ) { |
163
|
|
|
return delete_metadata( 'customer', $customer_id, $meta_key, $meta_value ); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Create the table |
168
|
|
|
* |
169
|
|
|
* @access public |
170
|
|
|
* @since 1.6 |
171
|
|
|
*/ |
172
|
|
|
public function create_table() { |
173
|
|
|
|
174
|
|
|
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
175
|
|
|
|
176
|
|
|
$sql = "CREATE TABLE {$this->table_name} ( |
177
|
|
|
meta_id bigint(20) NOT NULL AUTO_INCREMENT, |
178
|
|
|
customer_id bigint(20) NOT NULL, |
179
|
|
|
meta_key varchar(255) DEFAULT NULL, |
180
|
|
|
meta_value longtext, |
181
|
|
|
PRIMARY KEY (meta_id), |
182
|
|
|
KEY customer_id (customer_id), |
183
|
|
|
KEY meta_key (meta_key) |
184
|
|
|
) CHARACTER SET utf8 COLLATE utf8_general_ci;"; |
185
|
|
|
|
186
|
|
|
dbDelta( $sql ); |
187
|
|
|
|
188
|
|
|
update_option( $this->table_name . '_db_version', $this->version ); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Given a customer ID, make sure it's a positive number, greater than zero before inserting or adding. |
193
|
|
|
* |
194
|
|
|
* @since 1.6 |
195
|
|
|
* |
196
|
|
|
* @param int|stripe $customer_id A passed customer ID. |
197
|
|
|
* |
198
|
|
|
* @return int|bool The normalized customer ID or false if it's found to not be valid. |
199
|
|
|
*/ |
200
|
|
|
private function sanitize_customer_id( $customer_id ) { |
201
|
|
|
if ( ! is_numeric( $customer_id ) ) { |
202
|
|
|
return false; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$customer_id = (int) $customer_id; |
206
|
|
|
|
207
|
|
|
// We were given a non positive number |
208
|
|
|
if ( absint( $customer_id ) !== $customer_id ) { |
209
|
|
|
return false; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if ( empty( $customer_id ) ) { |
213
|
|
|
return false; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return absint( $customer_id ); |
217
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.