PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 4.15.3
GiveWP – Donation Plugin and Fundraising Platform v4.15.3
4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / database / class-give-db-meta.php
give / includes / database Last commit date
class-give-db-comments-meta.php 6 years ago class-give-db-comments.php 6 years ago class-give-db-donor-meta.php 6 years ago class-give-db-donors.php 6 years ago class-give-db-form-meta.php 6 years ago class-give-db-meta.php 9 months ago class-give-db-payment-meta.php 5 months ago class-give-db-sequential-ordering.php 6 years ago class-give-db-sessions.php 9 months ago class-give-db.php 6 years ago
class-give-db-meta.php
590 lines
1 <?php
2 /**
3 * Give DB Meta
4 *
5 * @package Give
6 * @subpackage Classes/Give_DB_Meta
7 * @copyright Copyright (c) 2017, GiveWP
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 * Flag to check whether meta function called by WP filter or directly
54 *
55 * @since 2.0
56 * @access protected
57 */
58 private $is_filter_callback = false;
59
60
61 /**
62 * Meta supports.
63 *
64 * @since 2.0
65 * @access protected
66 * @var array
67 */
68 protected $supports = [
69 'add_post_metadata',
70 'get_post_metadata',
71 'update_post_metadata',
72 'delete_post_metadata',
73 'posts_where',
74 'posts_join',
75 'posts_groupby',
76 'posts_orderby',
77 ];
78
79 /**
80 * Give_DB_Meta constructor.
81 *
82 * @since 2.0
83 */
84 function __construct() {
85 parent::__construct();
86
87 // Bailout.
88 if ( empty( $this->supports ) || ! $this->is_custom_meta_table_active() ) {
89 return;
90 }
91
92 if ( in_array( 'add_post_metadata', $this->supports ) ) {
93 add_filter( 'add_post_metadata', [ $this, '__add_meta' ], 0, 5 );
94 }
95
96 if ( in_array( 'get_post_metadata', $this->supports ) ) {
97 add_filter( 'get_post_metadata', [ $this, '__get_meta' ], 10, 4 );
98 }
99
100 if ( in_array( 'update_post_metadata', $this->supports ) ) {
101 add_filter( 'update_post_metadata', [ $this, '__update_meta' ], 0, 5 );
102 }
103
104 if ( in_array( 'delete_post_metadata', $this->supports ) ) {
105 add_filter( 'delete_post_metadata', [ $this, '__delete_meta' ], 0, 5 );
106 }
107
108 if ( in_array( 'posts_where', $this->supports ) ) {
109 add_filter( 'posts_where', [$this, 'rename_meta_table_name_in_query'], 99999, 2 );
110 }
111
112 if ( in_array( 'posts_join', $this->supports ) ) {
113 add_filter( 'posts_join', [$this, 'rename_meta_table_name_in_query'], 99999, 2 );
114 }
115
116 if ( in_array( 'posts_groupby', $this->supports ) ) {
117 add_filter( 'posts_groupby', [$this, 'rename_meta_table_name_in_query'], 99999, 2 );
118 }
119
120 if ( in_array( 'posts_orderby', $this->supports ) ) {
121 add_filter( 'posts_orderby', [$this, 'rename_meta_table_name_in_query'], 99999, 2 );
122 }
123 }
124
125
126 /**
127 * Retrieve payment meta field for a payment.
128 *
129 * @since 3.1.0 Return empty array, when request raw metadata if $single is set to false and metadata does not exist.
130 * @since 2.0
131 *
132 * @param int $id Pst Type ID.
133 * @param string $meta_key The meta key to retrieve.
134 * @param bool $single Whether to return a single value.
135 *
136 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
137 * is true.
138 */
139 public function get_meta( $id = 0, $meta_key = '', $single = false ) {
140 if ( ! $this->is_filter_callback ) {
141 return get_metadata( $this->meta_type, $id, $meta_key, $single );
142 }
143
144 $id = $this->sanitize_id( $id );
145
146 // Bailout.
147 if ( ! $this->is_valid_post_type( $id ) ) {
148 return $this->check;
149 }
150
151 if ( $this->raw_result ) {
152 if ( ! ( $value = get_metadata( $this->meta_type, $id, $meta_key, false ) ) ) {
153 $value = $single ? '' : array();
154 }
155
156 // Reset flag.
157 $this->raw_result = false;
158
159 } else {
160 $value = get_metadata( $this->meta_type, $id, $meta_key, $single );
161 }
162
163 $this->is_filter_callback = false;
164
165 return $value;
166 }
167
168
169 /**
170 * Add meta data field to a payment.
171 *
172 * For internal use only. Use Give_Payment->add_meta() for public usage.
173 *
174 * @access private
175 * @since 2.0
176 *
177 * @param int $id Post Type ID.
178 * @param string $meta_key Metadata name.
179 * @param mixed $meta_value Metadata value.
180 * @param bool $unique Optional, default is false. Whether the same key should not be added.
181 *
182 * @return int|bool False for failure. True for success.
183 */
184 public function add_meta( $id, $meta_key, $meta_value, $unique = false ) {
185 if ( $this->is_filter_callback ) {
186 $id = $this->sanitize_id( $id );
187
188 // Bailout.
189 if ( ! $this->is_valid_post_type( $id ) ) {
190 return $this->check;
191 }
192 }
193
194 $meta_id = add_metadata( $this->meta_type, $id, $meta_key, $meta_value, $unique );
195
196 if ( $meta_id ) {
197 $this->delete_cache( $id );
198 }
199
200 $this->is_filter_callback = false;
201
202 return $meta_id;
203 }
204
205 /**
206 * Update payment meta field based on Post Type ID.
207 *
208 * For internal use only. Use Give_Payment->update_meta() for public usage.
209 *
210 * Use the $prev_value parameter to differentiate between meta fields with the
211 * same key and Post Type ID.
212 *
213 * If the meta field for the payment does not exist, it will be added.
214 *
215 * @access public
216 * @since 2.0
217 *
218 * @param int $id Post Type ID.
219 * @param string $meta_key Metadata key.
220 * @param mixed $meta_value Metadata value.
221 * @param mixed $prev_value Optional. Previous value to check before removing.
222 *
223 * @return int|bool False on failure, true if success.
224 */
225 public function update_meta( $id, $meta_key, $meta_value, $prev_value = '' ) {
226 if ( $this->is_filter_callback ) {
227 $id = $this->sanitize_id( $id );
228
229 // Bailout.
230 if ( ! $this->is_valid_post_type( $id ) ) {
231 return $this->check;
232 }
233 }
234
235 $meta_id = update_metadata( $this->meta_type, $id, $meta_key, $meta_value, $prev_value );
236
237 if ( $meta_id ) {
238 $this->delete_cache( $id );
239 }
240
241 $this->is_filter_callback = false;
242
243 return $meta_id;
244 }
245
246 /**
247 * Remove metadata matching criteria from a payment.
248 *
249 * You can match based on the key, or key and value. Removing based on key and
250 * value, will keep from removing duplicate metadata with the same key. It also
251 * allows removing all metadata matching key, if needed.
252 *
253 * @access public
254 * @since 2.0
255 *
256 * @param int $id Post Type ID.
257 * @param string $meta_key Metadata name.
258 * @param mixed $meta_value Optional. Metadata value.
259 * @param mixed $delete_all Optional.
260 *
261 * @return bool False for failure. True for success.
262 */
263 public function delete_meta( $id = 0, $meta_key = '', $meta_value = '', $delete_all = '' ) {
264 if ( $this->is_filter_callback ) {
265 $id = $this->sanitize_id( $id );
266
267 // Bailout.
268 if ( ! $this->is_valid_post_type( $id ) ) {
269 return $this->check;
270 }
271 }
272
273 $is_meta_deleted = delete_metadata( $this->meta_type, $id, $meta_key, $meta_value, $delete_all );
274
275 if ( $is_meta_deleted ) {
276 $this->delete_cache( $id );
277 }
278
279 $this->is_filter_callback = false;
280
281 return $is_meta_deleted;
282 }
283
284 /**
285 * Rename query clauses of every query for new meta table
286 *
287 * @since 4.9.0 rename function - PHP 8 compatibility
288 * @since 2.0
289 * @access public
290 *
291 * @param string $clause
292 * @param WP_Query $wp_query
293 *
294 * @return string
295 */
296 public function rename_meta_table_name_in_query( $clause, $wp_query ) {
297 // Add new table to sql query.
298 if ( $this->is_post_type_query( $wp_query ) && ! empty( $wp_query->meta_query->queries ) ) {
299 $clause = $this->rename_meta_table_name( $clause, current_filter() );
300 }
301
302 return $clause;
303 }
304
305
306 /**
307 * Rename query clauses for new meta table
308 *
309 * @since 4.9.0 rename function - PHP 8 compatibility
310 * @param $clause
311 * @param $filter
312 *
313 * @return mixed
314 */
315 public function rename_meta_table_name( $clause, $filter ) {
316 global $wpdb;
317
318 $clause = str_replace( "{$wpdb->postmeta}.post_id", "{$this->table_name}.{$this->meta_type}_id", $clause );
319 $clause = str_replace( $wpdb->postmeta, $this->table_name, $clause );
320
321 switch ( $filter ) {
322 case 'posts_join':
323 $joins = [ 'INNER JOIN', 'LEFT JOIN' ];
324
325 foreach ( $joins as $join ) {
326 if ( false !== strpos( $clause, $join ) ) {
327 $clause = explode( $join, $clause );
328
329 foreach ( $clause as $key => $clause_part ) {
330 if ( empty( $clause_part ) ) {
331 continue;
332 }
333
334 preg_match( '/' . $wpdb->prefix . 'give_' . $this->meta_type . 'meta AS (.*) ON/', $clause_part, $alias_table_name );
335
336 if ( isset( $alias_table_name[1] ) ) {
337 $clause[ $key ] = str_replace( "{$alias_table_name[1]}.post_id", "{$alias_table_name[1]}.{$this->meta_type}_id", $clause_part );
338 }
339 }
340
341 $clause = implode( "{$join} ", $clause );
342 }
343 }
344 break;
345
346 case 'posts_where':
347 $clause = str_replace(
348 [ 'mt2.post_id', 'mt1.post_id' ],
349 [
350 "mt2.{$this->meta_type}_id",
351 "mt1.{$this->meta_type}_id",
352 ],
353 $clause
354 );
355 break;
356 }
357
358 return $clause;
359 }
360
361
362 /**
363 * Check if current query for post type or not.
364 *
365 * @since 2.0
366 * @access protected
367 *
368 * @param WP_Query $wp_query
369 *
370 * @return bool
371 */
372 protected function is_post_type_query( $wp_query ) {
373 $status = false;
374
375 // Check if it is payment query.
376 if ( ! empty( $wp_query->query['post_type'] ) ) {
377 if (
378 is_string( $wp_query->query['post_type'] ) &&
379 $this->post_type === $wp_query->query['post_type']
380 ) {
381 $status = true;
382 } elseif (
383 is_array( $wp_query->query['post_type'] ) &&
384 1 === count( $wp_query->query['post_type'] ) &&
385 in_array( $this->post_type, $wp_query->query['post_type'] )
386 ) {
387 $status = true;
388 }
389 }
390
391 return $status;
392 }
393
394 /**
395 * Check if current id of post type or not
396 *
397 * @since 2.0
398 * @access protected
399 *
400 * @param $ID
401 *
402 * @return bool
403 */
404 protected function is_valid_post_type( $ID ) {
405 return $ID && ( $this->post_type === get_post_type( $ID ) );
406 }
407
408 /**
409 * check if custom meta table enabled or not.
410 *
411 * @since 2.0
412 * @access protected
413 * @return bool
414 */
415 protected function is_custom_meta_table_active() {
416 return false;
417 }
418
419
420 /**
421 * Update last_changed key
422 *
423 * @since 2.0
424 * @access private
425 *
426 * @param int $id
427 * @param string $meta_type
428 *
429 * @return void
430 */
431 private function delete_cache( $id, $meta_type = '' ) {
432 $meta_type = empty( $meta_type ) ? $this->meta_type : $meta_type;
433
434 $group = [
435 'payment' => 'give-donations', // Backward compatibility
436 'donation' => 'give-donations',
437 'donor' => 'give-donors',
438 'customer' => 'give-donors', // Backward compatibility for pre upgrade in 2.0
439 ];
440
441 if ( array_key_exists( $meta_type, $group ) ) {
442 Give_Cache::delete_group( $id, $group[ $meta_type ] );
443 wp_cache_delete( $id, $this->meta_type . '_meta' );
444 }
445 }
446
447 /**
448 * Add support for hidden functions.
449 *
450 * @since 2.0
451 * @since 2.9.6 Added a Short circuit check when updating meta.
452 *
453 * @access public
454 *
455 * @param $name
456 * @param $arguments
457 *
458 * @return mixed
459 */
460 public function __call( $name, $arguments ) {
461 switch ( $name ) {
462 case '__add_meta':
463 $this->check = $arguments[0];
464 $id = $arguments[1];
465 $meta_key = $arguments[2];
466 $meta_value = $arguments[3];
467 $unique = $arguments[4];
468 $this->is_filter_callback = true;
469
470 // Bailout.
471 if ( ! $this->is_valid_post_type( $id ) ) {
472 return $this->check;
473 }
474
475 return $this->add_meta( $id, $meta_key, $meta_value, $unique );
476
477 case '__get_meta':
478 $this->check = $arguments[0];
479 $id = $arguments[1];
480 $meta_key = $arguments[2];
481 $single = $arguments[3];
482 $this->is_filter_callback = true;
483
484 // Bailout.
485 if ( ! $this->is_valid_post_type( $id ) ) {
486 return $this->check;
487 }
488
489 $this->raw_result = true;
490
491 return $this->get_meta( $id, $meta_key, $single );
492
493 case '__update_meta':
494 $this->check = $arguments[0];
495 $id = $arguments[1];
496 $meta_key = $arguments[2];
497 $meta_value = $arguments[3];
498 $this->is_filter_callback = true;
499
500 // Short circuit if the meta value has already been updated.
501 if ( null !== $this->check ) {
502 return $this->check;
503 }
504
505 // Bailout.
506 if ( ! $this->is_valid_post_type( $id ) ) {
507 return $this->check;
508 }
509
510 return $this->update_meta( $id, $meta_key, $meta_value );
511
512 case '__delete_meta':
513 $this->check = $arguments[0];
514 $id = $arguments[1];
515 $meta_key = $arguments[2];
516 $meta_value = $arguments[3];
517 $delete_all = $arguments[3];
518 $this->is_filter_callback = true;
519
520 // Bailout.
521 if ( ! $this->is_valid_post_type( $id ) ) {
522 return $this->check;
523 }
524
525 return $this->delete_meta( $id, $meta_key, $meta_value, $delete_all );
526 }
527 }
528
529 /**
530 * Create Meta Tables.
531 *
532 * @since 2.0.1
533 * @access public
534 */
535 public function create_table() {
536 global $wpdb;
537
538 $charset_collate = $wpdb->get_charset_collate();
539
540 $sql = "CREATE TABLE {$this->table_name} (
541 meta_id bigint(20) NOT NULL AUTO_INCREMENT,
542 {$this->meta_type}_id bigint(20) NOT NULL,
543 meta_key varchar(255) DEFAULT NULL,
544 meta_value longtext,
545 PRIMARY KEY (meta_id),
546 KEY {$this->meta_type}_id ({$this->meta_type}_id),
547 KEY meta_key (meta_key({$this->min_index_length}))
548 ) {$charset_collate};";
549
550 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
551 dbDelta( $sql );
552
553 update_option( $this->table_name . '_db_version', $this->version, false );
554 }
555
556
557 /**
558 * Get meta type
559 *
560 * @since 2.0.4
561 * @access public
562 *
563 * @return string
564 */
565 public function get_meta_type() {
566 return $this->meta_type;
567 }
568
569 /**
570 * Remove all meta data matching criteria from a meta table.
571 *
572 * @since 2.1.3
573 * @access public
574 *
575 * @param int $id ID.
576 *
577 * @return bool False for failure. True for success.
578 */
579 public function delete_all_meta( $id = 0 ) {
580 global $wpdb;
581 $status = $wpdb->delete( $this->table_name, [ "{$this->meta_type}_id" => $id ], [ '%d' ] );
582
583 if ( $status ) {
584 $this->delete_cache( $id, $this->meta_type );
585 }
586
587 return $status;
588 }
589 }
590