|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Payment Meta DB class |
|
4
|
|
|
* |
|
5
|
|
|
* @package Give |
|
6
|
|
|
* @subpackage Classes/DB Payment Meta |
|
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
|
8
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
|
9
|
|
|
* @since 2.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
14
|
|
|
exit; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Give_DB_Payment_Meta |
|
19
|
|
|
* |
|
20
|
|
|
* This class is for interacting with the payment meta database table. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 2.0 |
|
23
|
|
|
*/ |
|
24
|
|
View Code Duplication |
class Give_DB_Payment_Meta extends Give_DB { |
|
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Flag to handle result type |
|
27
|
|
|
* |
|
28
|
|
|
* @since 2.0 |
|
29
|
|
|
* @access private |
|
30
|
|
|
*/ |
|
31
|
|
|
private $raw_result = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Give_DB_Payment_Meta constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @access public |
|
37
|
|
|
* @since 2.0 |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct() { |
|
40
|
|
|
/* @var WPDB $wpdb */ |
|
41
|
|
|
global $wpdb; |
|
42
|
|
|
|
|
43
|
|
|
$wpdb->paymentmeta = $this->table_name = $wpdb->prefix . 'give_paymentmeta'; |
|
44
|
|
|
$this->primary_key = 'meta_id'; |
|
45
|
|
|
$this->version = '1.0'; |
|
46
|
|
|
|
|
47
|
|
|
$this->register_table(); |
|
48
|
|
|
|
|
49
|
|
|
add_filter( 'add_post_metadata', array( $this, '__add_meta' ), 0, 4 ); |
|
50
|
|
|
add_filter( 'get_post_metadata', array( $this, '__get_meta' ), 0, 4 ); |
|
51
|
|
|
add_filter( 'update_post_metadata', array( $this, '__update_meta' ), 0, 4 ); |
|
52
|
|
|
add_filter( 'delete_post_metadata', array( $this, '__delete_meta' ), 0, 4 ); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get table columns and data types. |
|
57
|
|
|
* |
|
58
|
|
|
* @access public |
|
59
|
|
|
* @since 2.0 |
|
60
|
|
|
* |
|
61
|
|
|
* @return array Columns and formats. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function get_columns() { |
|
64
|
|
|
return array( |
|
65
|
|
|
'meta_id' => '%d', |
|
66
|
|
|
'payment_id' => '%d', |
|
67
|
|
|
'meta_key' => '%s', |
|
|
|
|
|
|
68
|
|
|
'meta_value' => '%s', |
|
|
|
|
|
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Retrieve payment meta field for a payment. |
|
74
|
|
|
* |
|
75
|
|
|
* For internal use only. Use Give_Payment->get_meta() for public usage. |
|
76
|
|
|
* |
|
77
|
|
|
* @access public |
|
78
|
|
|
* @since 2.0 |
|
79
|
|
|
* |
|
80
|
|
|
* @param int $payment_id Payment ID. |
|
81
|
|
|
* @param string $meta_key The meta key to retrieve. |
|
82
|
|
|
* @param bool $single Whether to return a single value. |
|
83
|
|
|
* |
|
84
|
|
|
* @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. |
|
85
|
|
|
*/ |
|
86
|
|
|
public function get_meta( $payment_id = 0, $meta_key = '', $single = false ) { |
|
87
|
|
|
$payment_id = $this->sanitize_id( $payment_id ); |
|
88
|
|
|
|
|
89
|
|
|
// Bailout. |
|
90
|
|
|
if ( ! $this->is_payment( $payment_id ) ) { |
|
91
|
|
|
return null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if( $this->raw_result ) { |
|
|
|
|
|
|
95
|
|
|
if( ! ( $value = get_metadata( 'payment', $payment_id, $meta_key, false ) ) ) { |
|
|
|
|
|
|
96
|
|
|
$value = ''; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// Reset flag. |
|
100
|
|
|
$this->raw_result = false; |
|
101
|
|
|
|
|
102
|
|
|
}else { |
|
103
|
|
|
$value = get_metadata( 'payment', $payment_id, $meta_key, $single ); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $value; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Add meta data field to a payment. |
|
111
|
|
|
* |
|
112
|
|
|
* For internal use only. Use Give_Payment->add_meta() for public usage. |
|
113
|
|
|
* |
|
114
|
|
|
* @access private |
|
115
|
|
|
* @since 2.0 |
|
116
|
|
|
* |
|
117
|
|
|
* @param int $payment_id Payment ID. |
|
118
|
|
|
* @param string $meta_key Metadata name. |
|
119
|
|
|
* @param mixed $meta_value Metadata value. |
|
120
|
|
|
* @param bool $unique Optional, default is false. Whether the same key should not be added. |
|
121
|
|
|
* |
|
122
|
|
|
* @return bool False for failure. True for success. |
|
123
|
|
|
*/ |
|
124
|
|
|
public function add_meta( $payment_id = 0, $meta_key = '', $meta_value, $unique = false ) { |
|
125
|
|
|
$payment_id = $this->sanitize_id( $payment_id ); |
|
126
|
|
|
|
|
127
|
|
|
// Bailout. |
|
128
|
|
|
if ( ! $this->is_payment( $payment_id ) ) { |
|
129
|
|
|
return null; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return add_metadata( 'payment', $payment_id, $meta_key, $meta_value, $unique ); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Update payment meta field based on Payment ID. |
|
137
|
|
|
* |
|
138
|
|
|
* For internal use only. Use Give_Payment->update_meta() for public usage. |
|
139
|
|
|
* |
|
140
|
|
|
* Use the $prev_value parameter to differentiate between meta fields with the |
|
141
|
|
|
* same key and Payment ID. |
|
142
|
|
|
* |
|
143
|
|
|
* If the meta field for the payment does not exist, it will be added. |
|
144
|
|
|
* |
|
145
|
|
|
* @access public |
|
146
|
|
|
* @since 2.0 |
|
147
|
|
|
* |
|
148
|
|
|
* @param int $payment_id Payment ID. |
|
149
|
|
|
* @param string $meta_key Metadata key. |
|
150
|
|
|
* @param mixed $meta_value Metadata value. |
|
151
|
|
|
* @param mixed $prev_value Optional. Previous value to check before removing. |
|
152
|
|
|
* |
|
153
|
|
|
* @return bool False on failure, true if success. |
|
154
|
|
|
*/ |
|
155
|
|
|
public function update_meta( $payment_id = 0, $meta_key = '', $meta_value, $prev_value = '' ) { |
|
156
|
|
|
$payment_id = $this->sanitize_id( $payment_id ); |
|
157
|
|
|
|
|
158
|
|
|
// Bailout. |
|
159
|
|
|
if ( ! $this->is_payment( $payment_id ) ) { |
|
160
|
|
|
return null; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return update_metadata( 'payment', $payment_id, $meta_key, $meta_value, $prev_value ); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Remove metadata matching criteria from a payment. |
|
168
|
|
|
* |
|
169
|
|
|
* You can match based on the key, or key and value. Removing based on key and |
|
170
|
|
|
* value, will keep from removing duplicate metadata with the same key. It also |
|
171
|
|
|
* allows removing all metadata matching key, if needed. |
|
172
|
|
|
* |
|
173
|
|
|
* @access public |
|
174
|
|
|
* @since 2.0 |
|
175
|
|
|
* |
|
176
|
|
|
* @param int $payment_id Payment ID. |
|
177
|
|
|
* @param string $meta_key Metadata name. |
|
178
|
|
|
* @param mixed $meta_value Optional. Metadata value. |
|
179
|
|
|
* |
|
180
|
|
|
* @return bool False for failure. True for success. |
|
181
|
|
|
*/ |
|
182
|
|
|
public function delete_meta( $payment_id = 0, $meta_key = '', $meta_value = '' ) { |
|
183
|
|
|
$payment_id = $this->sanitize_id( $payment_id ); |
|
184
|
|
|
|
|
185
|
|
|
// Bailout. |
|
186
|
|
|
if ( ! $this->is_payment( $payment_id ) ) { |
|
187
|
|
|
return null; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return delete_metadata( 'payment', $payment_id, $meta_key, $meta_value ); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Create the table |
|
195
|
|
|
* |
|
196
|
|
|
* @access public |
|
197
|
|
|
* @since 2.0 |
|
198
|
|
|
* |
|
199
|
|
|
* @return void |
|
200
|
|
|
*/ |
|
201
|
|
|
public function create_table() { |
|
202
|
|
|
global $wpdb; |
|
203
|
|
|
|
|
204
|
|
|
$charset_collate = $wpdb->get_charset_collate(); |
|
205
|
|
|
|
|
206
|
|
|
$sql = "CREATE TABLE {$this->table_name} ( |
|
207
|
|
|
meta_id bigint(20) NOT NULL AUTO_INCREMENT, |
|
208
|
|
|
payment_id bigint(20) NOT NULL, |
|
209
|
|
|
meta_key varchar(255) DEFAULT NULL, |
|
210
|
|
|
meta_value longtext, |
|
211
|
|
|
PRIMARY KEY (meta_id), |
|
212
|
|
|
KEY payment_id (payment_id), |
|
213
|
|
|
KEY meta_key (meta_key) |
|
214
|
|
|
) {$charset_collate};"; |
|
215
|
|
|
|
|
216
|
|
|
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
|
217
|
|
|
dbDelta( $sql ); |
|
218
|
|
|
|
|
219
|
|
|
update_option( $this->table_name . '_db_version', $this->version ); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Add support for hidden functions. |
|
224
|
|
|
* |
|
225
|
|
|
* @since 2.0 |
|
226
|
|
|
* @access public |
|
227
|
|
|
* |
|
228
|
|
|
* @param $name |
|
229
|
|
|
* @param $arguments |
|
230
|
|
|
* |
|
231
|
|
|
* @return mixed |
|
232
|
|
|
*/ |
|
233
|
|
|
public function __call( $name, $arguments ) { |
|
234
|
|
|
// Bailout. |
|
235
|
|
|
if( ! give_has_upgrade_completed('v20_move_metadata_into_new_table') ) { |
|
|
|
|
|
|
236
|
|
|
return; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
switch ( $name ) { |
|
240
|
|
|
case '__add_meta': |
|
241
|
|
|
$check = $arguments[0]; |
|
|
|
|
|
|
242
|
|
|
$log_id = $arguments[1]; |
|
243
|
|
|
$meta_key = $arguments[2]; |
|
244
|
|
|
$meta_value = $arguments[3]; |
|
245
|
|
|
$unique = $arguments[4]; |
|
246
|
|
|
|
|
247
|
|
|
return $this->add_meta( $log_id, $meta_key, $meta_value, $unique ); |
|
248
|
|
|
|
|
249
|
|
|
case '__get_meta': |
|
250
|
|
|
$check = $arguments[0]; |
|
|
|
|
|
|
251
|
|
|
$log_id = $arguments[1]; |
|
252
|
|
|
$meta_key = $arguments[2]; |
|
253
|
|
|
$single = $arguments[3]; |
|
254
|
|
|
|
|
255
|
|
|
$this->raw_result = true; |
|
256
|
|
|
return $this->get_meta( $log_id, $meta_key, $single ); |
|
257
|
|
|
|
|
258
|
|
|
case '__update_meta': |
|
259
|
|
|
$check = $arguments[0]; |
|
|
|
|
|
|
260
|
|
|
$log_id = $arguments[1]; |
|
261
|
|
|
$meta_key = $arguments[2]; |
|
262
|
|
|
$meta_value = $arguments[3]; |
|
263
|
|
|
|
|
264
|
|
|
return $this->update_meta( $log_id, $meta_key, $meta_value ); |
|
265
|
|
|
|
|
266
|
|
|
case '__delete_meta': |
|
267
|
|
|
$check = $arguments[0]; |
|
|
|
|
|
|
268
|
|
|
$log_id = $arguments[1]; |
|
269
|
|
|
$meta_key = $arguments[2]; |
|
270
|
|
|
$meta_value = $arguments[3]; |
|
271
|
|
|
|
|
272
|
|
|
return $this->delete_meta( $log_id, $meta_key, $meta_value ); |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Check if current id of payment type or not |
|
279
|
|
|
* |
|
280
|
|
|
* @since 2.0 |
|
281
|
|
|
* @access private |
|
282
|
|
|
* |
|
283
|
|
|
* @param $ID |
|
284
|
|
|
* |
|
285
|
|
|
* @return bool |
|
286
|
|
|
*/ |
|
287
|
|
|
private function is_payment( $ID ) { |
|
288
|
|
|
return $ID && ( 'give_payment' === get_post_type( $ID ) ); |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.