PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.6.0
GiveWP – Donation Plugin and Fundraising Platform v2.6.0
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-logs-meta.php 6 years ago class-give-db-logs.php 6 years ago class-give-db-meta.php 6 years ago class-give-db-payment-meta.php 6 years ago class-give-db-sequential-ordering.php 6 years ago class-give-db-sessions.php 6 years ago class-give-db.php 6 years ago
class-give-db-meta.php
578 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 = array(
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', array( $this, '__add_meta' ), 0, 5 );
94 }
95
96 if ( in_array( 'get_post_metadata', $this->supports ) ) {
97 add_filter( 'get_post_metadata', array( $this, '__get_meta' ), 10, 4 );
98 }
99
100 if ( in_array( 'update_post_metadata', $this->supports ) ) {
101 add_filter( 'update_post_metadata', array( $this, '__update_meta' ), 0, 5 );
102 }
103
104 if ( in_array( 'delete_post_metadata', $this->supports ) ) {
105 add_filter( 'delete_post_metadata', array( $this, '__delete_meta' ), 0, 5 );
106 }
107
108 if ( in_array( 'posts_where', $this->supports ) ) {
109 add_filter( 'posts_where', array( $this, '__rename_meta_table_name_in_query' ), 99999, 2 );
110 }
111
112 if ( in_array( 'posts_join', $this->supports ) ) {
113 add_filter( 'posts_join', array( $this, '__rename_meta_table_name_in_query' ), 99999, 2 );
114 }
115
116 if ( in_array( 'posts_groupby', $this->supports ) ) {
117 add_filter( 'posts_groupby', array( $this, '__rename_meta_table_name_in_query' ), 99999, 2 );
118 }
119
120 if ( in_array( 'posts_orderby', $this->supports ) ) {
121 add_filter( 'posts_orderby', array( $this, '__rename_meta_table_name_in_query' ), 99999, 2 );
122 }
123 }
124
125
126 /**
127 * Retrieve payment meta field for a payment.
128 *
129 * @access public
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 = '';
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
274 $is_meta_deleted = delete_metadata( $this->meta_type, $id, $meta_key, $meta_value, $delete_all );
275
276 if ( $is_meta_deleted ) {
277 $this->delete_cache( $id );
278 }
279
280 $this->is_filter_callback = false;
281
282 return $is_meta_deleted;
283 }
284
285 /**
286 * Rename query clauses of every query for new meta table
287 *
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 * @param $clause
310 * @param $filter
311 *
312 * @return mixed
313 */
314 public function __rename_meta_table_name( $clause, $filter ) {
315 global $wpdb;
316
317 $clause = str_replace( "{$wpdb->postmeta}.post_id", "{$this->table_name}.{$this->meta_type}_id", $clause );
318 $clause = str_replace( $wpdb->postmeta, $this->table_name, $clause );
319
320 switch ( $filter ) {
321 case 'posts_join':
322 $joins = array( 'INNER JOIN', 'LEFT JOIN' );
323
324 foreach ( $joins as $join ) {
325 if ( false !== strpos( $clause, $join ) ) {
326 $clause = explode( $join, $clause );
327
328 foreach ( $clause as $key => $clause_part ) {
329 if ( empty( $clause_part ) ) {
330 continue;
331 }
332
333 preg_match( '/' . $wpdb->prefix . 'give_' . $this->meta_type . 'meta AS (.*) ON/', $clause_part, $alias_table_name );
334
335 if ( isset( $alias_table_name[1] ) ) {
336 $clause[ $key ] = str_replace( "{$alias_table_name[1]}.post_id", "{$alias_table_name[1]}.{$this->meta_type}_id", $clause_part );
337 }
338 }
339
340 $clause = implode( "{$join} ", $clause );
341 }
342 }
343 break;
344
345 case 'posts_where':
346 $clause = str_replace( array( 'mt2.post_id', 'mt1.post_id' ), array(
347 "mt2.{$this->meta_type}_id",
348 "mt1.{$this->meta_type}_id",
349 ), $clause );
350 break;
351 }
352
353 return $clause;
354 }
355
356
357 /**
358 * Check if current query for post type or not.
359 *
360 * @since 2.0
361 * @access protected
362 *
363 * @param WP_Query $wp_query
364 *
365 * @return bool
366 */
367 protected function is_post_type_query( $wp_query ) {
368 $status = false;
369
370 // Check if it is payment query.
371 if ( ! empty( $wp_query->query['post_type'] ) ) {
372 if (
373 is_string( $wp_query->query['post_type'] ) &&
374 $this->post_type === $wp_query->query['post_type']
375 ) {
376 $status = true;
377 } elseif (
378 is_array( $wp_query->query['post_type'] ) &&
379 1 === count( $wp_query->query['post_type'] ) &&
380 in_array( $this->post_type, $wp_query->query['post_type'] )
381 ) {
382 $status = true;
383 }
384 }
385
386 return $status;
387 }
388
389 /**
390 * Check if current id of post type or not
391 *
392 * @since 2.0
393 * @access protected
394 *
395 * @param $ID
396 *
397 * @return bool
398 */
399 protected function is_valid_post_type( $ID ) {
400 return $ID && ( $this->post_type === get_post_type( $ID ) );
401 }
402
403 /**
404 * check if custom meta table enabled or not.
405 *
406 * @since 2.0
407 * @access protected
408 * @return bool
409 */
410 protected function is_custom_meta_table_active() {
411 return false;
412 }
413
414
415 /**
416 * Update last_changed key
417 *
418 * @since 2.0
419 * @access private
420 *
421 * @param int $id
422 * @param string $meta_type
423 *
424 * @return void
425 */
426 private function delete_cache( $id, $meta_type = '' ) {
427 $meta_type = empty( $meta_type ) ? $this->meta_type : $meta_type;
428
429 $group = array(
430 'payment' => 'give-donations', // Backward compatibility
431 'donation' => 'give-donations',
432 'donor' => 'give-donors',
433 'customer' => 'give-donors', // Backward compatibility for pre upgrade in 2.0
434 );
435
436 if ( array_key_exists( $meta_type, $group ) ) {
437 Give_Cache::delete_group( $id, $group[ $meta_type ] );
438 wp_cache_delete( $id, $this->meta_type . '_meta' );
439 }
440 }
441
442 /**
443 * Add support for hidden functions.
444 *
445 * @since 2.0
446 * @access public
447 *
448 * @param $name
449 * @param $arguments
450 *
451 * @return mixed
452 */
453 public function __call( $name, $arguments ) {
454 switch ( $name ) {
455 case '__add_meta':
456 $this->check = $arguments[0];
457 $id = $arguments[1];
458 $meta_key = $arguments[2];
459 $meta_value = $arguments[3];
460 $unique = $arguments[4];
461 $this->is_filter_callback = true;
462
463 // Bailout.
464 if ( ! $this->is_valid_post_type( $id ) ) {
465 return $this->check;
466 }
467
468 return $this->add_meta( $id, $meta_key, $meta_value, $unique );
469
470 case '__get_meta':
471 $this->check = $arguments[0];
472 $id = $arguments[1];
473 $meta_key = $arguments[2];
474 $single = $arguments[3];
475 $this->is_filter_callback = true;
476
477 // Bailout.
478 if ( ! $this->is_valid_post_type( $id ) ) {
479 return $this->check;
480 }
481
482 $this->raw_result = true;
483
484 return $this->get_meta( $id, $meta_key, $single );
485
486 case '__update_meta':
487 $this->check = $arguments[0];
488 $id = $arguments[1];
489 $meta_key = $arguments[2];
490 $meta_value = $arguments[3];
491 $this->is_filter_callback = true;
492
493 // Bailout.
494 if ( ! $this->is_valid_post_type( $id ) ) {
495 return $this->check;
496 }
497
498 return $this->update_meta( $id, $meta_key, $meta_value );
499
500 case '__delete_meta':
501 $this->check = $arguments[0];
502 $id = $arguments[1];
503 $meta_key = $arguments[2];
504 $meta_value = $arguments[3];
505 $delete_all = $arguments[3];
506 $this->is_filter_callback = true;
507
508 // Bailout.
509 if ( ! $this->is_valid_post_type( $id ) ) {
510 return $this->check;
511 }
512
513 return $this->delete_meta( $id, $meta_key, $meta_value, $delete_all );
514 }
515 }
516
517 /**
518 * Create Meta Tables.
519 *
520 * @since 2.0.1
521 * @access public
522 */
523 public function create_table() {
524 global $wpdb;
525
526 $charset_collate = $wpdb->get_charset_collate();
527
528 $sql = "CREATE TABLE {$this->table_name} (
529 meta_id bigint(20) NOT NULL AUTO_INCREMENT,
530 {$this->meta_type}_id bigint(20) NOT NULL,
531 meta_key varchar(255) DEFAULT NULL,
532 meta_value longtext,
533 PRIMARY KEY (meta_id),
534 KEY {$this->meta_type}_id ({$this->meta_type}_id),
535 KEY meta_key (meta_key({$this->min_index_length}))
536 ) {$charset_collate};";
537
538 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
539 dbDelta( $sql );
540
541 update_option( $this->table_name . '_db_version', $this->version, false );
542 }
543
544
545 /**
546 * Get meta type
547 *
548 * @since 2.0.4
549 * @access public
550 *
551 * @return string
552 */
553 public function get_meta_type() {
554 return $this->meta_type;
555 }
556
557 /**
558 * Remove all meta data matching criteria from a meta table.
559 *
560 * @since 2.1.3
561 * @access public
562 *
563 * @param int $id ID.
564 *
565 * @return bool False for failure. True for success.
566 */
567 public function delete_all_meta( $id = 0 ) {
568 global $wpdb;
569 $status = $wpdb->delete( $this->table_name, array( "{$this->meta_type}_id" => $id ), array( '%d' ) );
570
571 if ( $status ) {
572 $this->delete_cache( $id, $this->meta_type );
573 }
574
575 return $status;
576 }
577 }
578