|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Give DB Meta |
|
4
|
|
|
* |
|
5
|
|
|
* @package Give |
|
6
|
|
|
* @subpackage Classes/Give_DB_Meta |
|
7
|
|
|
* @copyright Copyright (c) 2017, 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
|
|
|
class Give_DB_Meta extends Give_DB { |
|
18
|
|
|
/** |
|
19
|
|
|
* Post type |
|
20
|
|
|
* |
|
21
|
|
|
* @since 2.0 |
|
22
|
|
|
* @access protected |
|
23
|
|
|
* @var bool |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $post_type = ''; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Meta type |
|
29
|
|
|
* |
|
30
|
|
|
* @since 2.0 |
|
31
|
|
|
* @access protected |
|
32
|
|
|
* @var bool |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $meta_type = ''; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Flag to handle result type |
|
38
|
|
|
* |
|
39
|
|
|
* @since 2.0 |
|
40
|
|
|
* @access protected |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $raw_result = false; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Flag for short circuit of meta function |
|
46
|
|
|
* |
|
47
|
|
|
* @since 2.0 |
|
48
|
|
|
* @access protected |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $check = false; |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Meta supports. |
|
55
|
|
|
* |
|
56
|
|
|
* @since 2.0 |
|
57
|
|
|
* @access protected |
|
58
|
|
|
* @var array |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $supports = array( |
|
61
|
|
|
'add_post_metadata', |
|
62
|
|
|
'get_post_metadata', |
|
63
|
|
|
'update_post_metadata', |
|
64
|
|
|
'delete_post_metadata', |
|
65
|
|
|
'posts_where', |
|
66
|
|
|
'posts_join', |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Give_DB_Meta constructor. |
|
71
|
|
|
* |
|
72
|
|
|
* @since 2.0 |
|
73
|
|
|
*/ |
|
74
|
|
|
function __construct() { |
|
|
|
|
|
|
75
|
|
|
// Bailout. |
|
76
|
|
|
if ( empty( $this->supports ) || ! $this->is_custom_meta_table_active() ) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if ( in_array( 'add_post_metadata', $this->supports ) ) { |
|
81
|
|
|
add_filter( 'add_post_metadata', array( $this, '__add_meta' ), 0, 5 ); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ( in_array( 'get_post_metadata', $this->supports ) ) { |
|
85
|
|
|
add_filter( 'get_post_metadata', array( $this, '__get_meta' ), 0, 4 ); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ( in_array( 'update_post_metadata', $this->supports ) ) { |
|
89
|
|
|
add_filter( 'update_post_metadata', array( $this, '__update_meta' ), 0, 5 ); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if ( in_array( 'delete_post_metadata', $this->supports ) ) { |
|
93
|
|
|
add_filter( 'delete_post_metadata', array( $this, '__delete_meta' ), 0, 5 ); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ( in_array( 'posts_where', $this->supports ) ) { |
|
97
|
|
|
add_filter( 'posts_where', array( $this, '__posts_where' ), 10, 2 ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ( in_array( 'posts_join', $this->supports ) ) { |
|
101
|
|
|
add_filter( 'posts_join', array( $this, '__posts_join' ), 10, 2 ); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Retrieve payment meta field for a payment. |
|
108
|
|
|
* |
|
109
|
|
|
* @access public |
|
110
|
|
|
* @since 2.0 |
|
111
|
|
|
* |
|
112
|
|
|
* @param int $id Pst Type ID. |
|
113
|
|
|
* @param string $meta_key The meta key to retrieve. |
|
114
|
|
|
* @param bool $single Whether to return a single value. |
|
115
|
|
|
* |
|
116
|
|
|
* @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function get_meta( $id = 0, $meta_key = '', $single = false ) { |
|
119
|
|
|
$id = $this->sanitize_id( $id ); |
|
120
|
|
|
|
|
121
|
|
|
// Bailout. |
|
122
|
|
|
if ( ! $this->is_valid_post_type( $id ) ) { |
|
123
|
|
|
return $this->check; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if ( $this->raw_result ) { |
|
127
|
|
|
if ( ! ( $value = get_metadata( $this->meta_type, $id, $meta_key, false ) ) ) { |
|
128
|
|
|
$value = ''; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// Reset flag. |
|
132
|
|
|
$this->raw_result = false; |
|
133
|
|
|
|
|
134
|
|
|
} else { |
|
135
|
|
|
$value = get_metadata( $this->meta_type, $id, $meta_key, $single ); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $value; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Add meta data field to a payment. |
|
144
|
|
|
* |
|
145
|
|
|
* For internal use only. Use Give_Payment->add_meta() for public usage. |
|
146
|
|
|
* |
|
147
|
|
|
* @access private |
|
148
|
|
|
* @since 2.0 |
|
149
|
|
|
* |
|
150
|
|
|
* @param int $id Post Type ID. |
|
151
|
|
|
* @param string $meta_key Metadata name. |
|
152
|
|
|
* @param mixed $meta_value Metadata value. |
|
153
|
|
|
* @param bool $unique Optional, default is false. Whether the same key should not be added. |
|
154
|
|
|
* |
|
155
|
|
|
* @return bool False for failure. True for success. |
|
156
|
|
|
*/ |
|
157
|
|
View Code Duplication |
public function add_meta( $id = 0, $meta_key = '', $meta_value, $unique = false ) { |
|
|
|
|
|
|
158
|
|
|
$id = $this->sanitize_id( $id ); |
|
159
|
|
|
|
|
160
|
|
|
// Bailout. |
|
161
|
|
|
if ( ! $this->is_valid_post_type( $id ) ) { |
|
162
|
|
|
return $this->check; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return add_metadata( $this->meta_type, $id, $meta_key, $meta_value, $unique ); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Update payment meta field based on Post Type ID. |
|
170
|
|
|
* |
|
171
|
|
|
* For internal use only. Use Give_Payment->update_meta() for public usage. |
|
172
|
|
|
* |
|
173
|
|
|
* Use the $prev_value parameter to differentiate between meta fields with the |
|
174
|
|
|
* same key and Post Type ID. |
|
175
|
|
|
* |
|
176
|
|
|
* If the meta field for the payment does not exist, it will be added. |
|
177
|
|
|
* |
|
178
|
|
|
* @access public |
|
179
|
|
|
* @since 2.0 |
|
180
|
|
|
* |
|
181
|
|
|
* @param int $id Post Type ID. |
|
182
|
|
|
* @param string $meta_key Metadata key. |
|
183
|
|
|
* @param mixed $meta_value Metadata value. |
|
184
|
|
|
* @param mixed $prev_value Optional. Previous value to check before removing. |
|
185
|
|
|
* |
|
186
|
|
|
* @return bool False on failure, true if success. |
|
187
|
|
|
*/ |
|
188
|
|
View Code Duplication |
public function update_meta( $id = 0, $meta_key = '', $meta_value, $prev_value = '' ) { |
|
|
|
|
|
|
189
|
|
|
$id = $this->sanitize_id( $id ); |
|
190
|
|
|
|
|
191
|
|
|
// Bailout. |
|
192
|
|
|
if ( ! $this->is_valid_post_type( $id ) ) { |
|
193
|
|
|
return $this->check; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return update_metadata( $this->meta_type, $id, $meta_key, $meta_value, $prev_value ); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Remove metadata matching criteria from a payment. |
|
201
|
|
|
* |
|
202
|
|
|
* You can match based on the key, or key and value. Removing based on key and |
|
203
|
|
|
* value, will keep from removing duplicate metadata with the same key. It also |
|
204
|
|
|
* allows removing all metadata matching key, if needed. |
|
205
|
|
|
* |
|
206
|
|
|
* @access public |
|
207
|
|
|
* @since 2.0 |
|
208
|
|
|
* |
|
209
|
|
|
* @param int $id Post Type ID. |
|
210
|
|
|
* @param string $meta_key Metadata name. |
|
211
|
|
|
* @param mixed $meta_value Optional. Metadata value. |
|
212
|
|
|
* @param mixed $delete_all Optional. |
|
213
|
|
|
* |
|
214
|
|
|
* @return bool False for failure. True for success. |
|
215
|
|
|
*/ |
|
216
|
|
View Code Duplication |
public function delete_meta( $id = 0, $meta_key = '', $meta_value = '', $delete_all = '' ) { |
|
|
|
|
|
|
217
|
|
|
$id = $this->sanitize_id( $id ); |
|
218
|
|
|
|
|
219
|
|
|
// Bailout. |
|
220
|
|
|
if ( ! $this->is_valid_post_type( $id ) ) { |
|
221
|
|
|
return $this->check; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
return delete_metadata( $this->meta_type, $id, $meta_key, $meta_value, $delete_all ); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Filter where clause of every query for new payment meta table |
|
229
|
|
|
* |
|
230
|
|
|
* @since 2.0 |
|
231
|
|
|
* @access public |
|
232
|
|
|
* |
|
233
|
|
|
* @param string $where |
|
234
|
|
|
* @param WP_Query $wp_query |
|
235
|
|
|
* |
|
236
|
|
|
* @return string |
|
237
|
|
|
*/ |
|
238
|
|
|
public function __posts_where( $where, $wp_query ) { |
|
|
|
|
|
|
239
|
|
|
global $wpdb; |
|
240
|
|
|
|
|
241
|
|
|
$is_payment_post_type = false; |
|
242
|
|
|
|
|
243
|
|
|
// Check if it is payment query. |
|
244
|
|
View Code Duplication |
if ( ! empty( $wp_query->query['post_type'] ) ) { |
|
|
|
|
|
|
245
|
|
|
if ( is_string( $wp_query->query['post_type'] ) && $this->post_type === $wp_query->query['post_type'] ) { |
|
246
|
|
|
$is_payment_post_type = true; |
|
247
|
|
|
} elseif ( is_array( $wp_query->query['post_type'] ) && in_array( $this->post_type, $wp_query->query['post_type'] ) ) { |
|
248
|
|
|
$is_payment_post_type = true; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
// Add new table to sql query. |
|
253
|
|
|
if ( $is_payment_post_type && ! empty( $wp_query->meta_query->queries ) ) { |
|
254
|
|
|
$where = str_replace( $wpdb->postmeta, $this->table_name, $where ); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
return $where; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Filter join clause of every query for new payment meta table |
|
263
|
|
|
* |
|
264
|
|
|
* @since 2.0 |
|
265
|
|
|
* @access public |
|
266
|
|
|
* |
|
267
|
|
|
* @param string $join |
|
268
|
|
|
* @param WP_Query $wp_query |
|
269
|
|
|
* |
|
270
|
|
|
* @return string |
|
271
|
|
|
*/ |
|
272
|
|
|
public function __posts_join( $join, $wp_query ) { |
|
|
|
|
|
|
273
|
|
|
global $wpdb; |
|
274
|
|
|
|
|
275
|
|
|
$is_payment_post_type = false; |
|
276
|
|
|
|
|
277
|
|
|
// Check if it is payment query. |
|
278
|
|
View Code Duplication |
if ( ! empty( $wp_query->query['post_type'] ) ) { |
|
|
|
|
|
|
279
|
|
|
if ( is_string( $wp_query->query['post_type'] ) && $this->post_type === $wp_query->query['post_type'] ) { |
|
280
|
|
|
$is_payment_post_type = true; |
|
281
|
|
|
} elseif ( is_array( $wp_query->query['post_type'] ) && in_array( $this->post_type, $wp_query->query['post_type'] ) ) { |
|
282
|
|
|
$is_payment_post_type = true; |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
// Add new table to sql query. |
|
287
|
|
|
if ( $is_payment_post_type && ! empty( $wp_query->meta_query->queries ) ) { |
|
288
|
|
|
$join = str_replace( "{$wpdb->postmeta}.post_id", "{$this->table_name}.payment_id", $join ); |
|
289
|
|
|
$join = str_replace( $wpdb->postmeta, $this->table_name, $join ); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
return $join; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Add support for hidden functions. |
|
297
|
|
|
* |
|
298
|
|
|
* @since 2.0 |
|
299
|
|
|
* @access public |
|
300
|
|
|
* |
|
301
|
|
|
* @param $name |
|
302
|
|
|
* @param $arguments |
|
303
|
|
|
* |
|
304
|
|
|
* @return mixed |
|
305
|
|
|
*/ |
|
306
|
|
|
public function __call( $name, $arguments ) { |
|
307
|
|
|
switch ( $name ) { |
|
308
|
|
View Code Duplication |
case '__add_meta': |
|
|
|
|
|
|
309
|
|
|
$this->check = $arguments[0]; |
|
310
|
|
|
$id = $arguments[1]; |
|
311
|
|
|
$meta_key = $arguments[2]; |
|
312
|
|
|
$meta_value = $arguments[3]; |
|
313
|
|
|
$unique = $arguments[4]; |
|
314
|
|
|
|
|
315
|
|
|
return $this->add_meta( $id, $meta_key, $meta_value, $unique ); |
|
316
|
|
|
|
|
317
|
|
|
case '__get_meta': |
|
318
|
|
|
$this->check = $arguments[0]; |
|
319
|
|
|
$id = $arguments[1]; |
|
320
|
|
|
$meta_key = $arguments[2]; |
|
321
|
|
|
$single = $arguments[3]; |
|
322
|
|
|
|
|
323
|
|
|
$this->raw_result = true; |
|
324
|
|
|
|
|
325
|
|
|
return $this->get_meta( $id, $meta_key, $single ); |
|
326
|
|
|
|
|
327
|
|
|
case '__update_meta': |
|
328
|
|
|
$this->check = $arguments[0]; |
|
329
|
|
|
$id = $arguments[1]; |
|
330
|
|
|
$meta_key = $arguments[2]; |
|
331
|
|
|
$meta_value = $arguments[3]; |
|
332
|
|
|
|
|
333
|
|
|
return $this->update_meta( $id, $meta_key, $meta_value ); |
|
334
|
|
|
|
|
335
|
|
View Code Duplication |
case '__delete_meta': |
|
|
|
|
|
|
336
|
|
|
$this->check = $arguments[0]; |
|
337
|
|
|
$id = $arguments[1]; |
|
338
|
|
|
$meta_key = $arguments[2]; |
|
339
|
|
|
$meta_value = $arguments[3]; |
|
340
|
|
|
$delete_all = $arguments[3]; |
|
341
|
|
|
|
|
342
|
|
|
return $this->delete_meta( $id, $meta_key, $meta_value, $delete_all ); |
|
343
|
|
|
} |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* Check if current id of post type or not |
|
348
|
|
|
* |
|
349
|
|
|
* @since 2.0 |
|
350
|
|
|
* @access protected |
|
351
|
|
|
* |
|
352
|
|
|
* @param $ID |
|
353
|
|
|
* |
|
354
|
|
|
* @return bool |
|
355
|
|
|
*/ |
|
356
|
|
|
protected function is_valid_post_type( $ID ) { |
|
357
|
|
|
return $ID && ( $this->post_type === get_post_type( $ID ) ); |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
/** |
|
361
|
|
|
* check if custom meta table enabled or not. |
|
362
|
|
|
* |
|
363
|
|
|
* @since 2.0 |
|
364
|
|
|
* @access protected |
|
365
|
|
|
* @return bool |
|
366
|
|
|
*/ |
|
367
|
|
|
protected function is_custom_meta_table_active() { |
|
368
|
|
|
return false; |
|
369
|
|
|
} |
|
370
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.