PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.0.2
GiveWP – Donation Plugin and Fundraising Platform v2.0.2
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 / class-give-db-meta.php
give / includes Last commit date
admin 8 years ago api 8 years ago deprecated 8 years ago donors 8 years ago emails 8 years ago forms 8 years ago gateways 8 years ago libraries 8 years ago payments 8 years ago actions.php 8 years ago ajax-functions.php 8 years ago class-give-async-process.php 8 years ago class-give-background-updater.php 8 years ago class-give-cache.php 8 years ago class-give-cli-commands.php 8 years ago class-give-cron.php 8 years ago class-give-db-donor-meta.php 8 years ago class-give-db-donors.php 8 years ago class-give-db-form-meta.php 8 years ago class-give-db-logs-meta.php 8 years ago class-give-db-logs.php 8 years ago class-give-db-meta.php 8 years ago class-give-db-payment-meta.php 8 years ago class-give-db.php 8 years ago class-give-donate-form.php 8 years ago class-give-donor.php 8 years ago class-give-email-access.php 8 years ago class-give-gravatars.php 8 years ago class-give-html-elements.php 8 years ago class-give-license-handler.php 8 years ago class-give-logging.php 8 years ago class-give-roles.php 8 years ago class-give-session.php 8 years ago class-give-stats.php 8 years ago class-give-template-loader.php 9 years ago class-give-tooltips.php 8 years ago class-give-translation.php 8 years ago class-notices.php 8 years ago country-functions.php 8 years ago currency-functions.php 8 years ago error-tracking.php 8 years ago filters.php 8 years ago formatting.php 8 years ago import-functions.php 8 years ago install.php 8 years ago login-register.php 8 years ago misc-functions.php 8 years ago plugin-compatibility.php 8 years ago post-types.php 8 years ago price-functions.php 8 years ago process-donation.php 8 years ago scripts.php 8 years ago shortcodes.php 8 years ago template-functions.php 8 years ago user-functions.php 8 years ago
class-give-db-meta.php
506 lines
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 'posts_groupby',
68 'posts_orderby'
69 );
70
71 /**
72 * Give_DB_Meta constructor.
73 *
74 * @since 2.0
75 */
76 function __construct() {
77 // Bailout.
78 if ( empty( $this->supports ) || ! $this->is_custom_meta_table_active() ) {
79 return;
80 }
81
82 if ( in_array( 'add_post_metadata', $this->supports ) ) {
83 add_filter( 'add_post_metadata', array( $this, '__add_meta' ), 0, 5 );
84 }
85
86 if ( in_array( 'get_post_metadata', $this->supports ) ) {
87 add_filter( 'get_post_metadata', array( $this, '__get_meta' ), 0, 4 );
88 }
89
90 if ( in_array( 'update_post_metadata', $this->supports ) ) {
91 add_filter( 'update_post_metadata', array( $this, '__update_meta' ), 0, 5 );
92 }
93
94 if ( in_array( 'delete_post_metadata', $this->supports ) ) {
95 add_filter( 'delete_post_metadata', array( $this, '__delete_meta' ), 0, 5 );
96 }
97
98 if ( in_array( 'posts_where', $this->supports ) ) {
99 add_filter( 'posts_where', array( $this, '__rename_meta_table_name_in_query' ), 99999, 2 );
100 }
101
102 if ( in_array( 'posts_join', $this->supports ) ) {
103 add_filter( 'posts_join', array( $this, '__rename_meta_table_name_in_query' ), 99999, 2 );
104 }
105
106 if ( in_array( 'posts_groupby', $this->supports ) ) {
107 add_filter( 'posts_groupby', array( $this, '__rename_meta_table_name_in_query' ), 99999, 2 );
108 }
109
110 if ( in_array( 'posts_orderby', $this->supports ) ) {
111 add_filter( 'posts_orderby', array( $this, '__rename_meta_table_name_in_query' ), 99999, 2 );
112 }
113 }
114
115
116 /**
117 * Retrieve payment meta field for a payment.
118 *
119 * @access public
120 * @since 2.0
121 *
122 * @param int $id Pst Type ID.
123 * @param string $meta_key The meta key to retrieve.
124 * @param bool $single Whether to return a single value.
125 *
126 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
127 * is true.
128 */
129 public function get_meta( $id = 0, $meta_key = '', $single = false ) {
130 $id = $this->sanitize_id( $id );
131
132 // Bailout.
133 if ( ! $this->is_valid_post_type( $id ) ) {
134 return $this->check;
135 }
136
137 if ( $this->raw_result ) {
138 if ( ! ( $value = get_metadata( $this->meta_type, $id, $meta_key, false ) ) ) {
139 $value = '';
140 }
141
142 // Reset flag.
143 $this->raw_result = false;
144
145 } else {
146 $value = get_metadata( $this->meta_type, $id, $meta_key, $single );
147 }
148
149 return $value;
150 }
151
152
153 /**
154 * Add meta data field to a payment.
155 *
156 * For internal use only. Use Give_Payment->add_meta() for public usage.
157 *
158 * @access private
159 * @since 2.0
160 *
161 * @param int $id Post Type ID.
162 * @param string $meta_key Metadata name.
163 * @param mixed $meta_value Metadata value.
164 * @param bool $unique Optional, default is false. Whether the same key should not be added.
165 *
166 * @return int|bool False for failure. True for success.
167 */
168 public function add_meta( $id = 0, $meta_key = '', $meta_value, $unique = false ) {
169 $id = $this->sanitize_id( $id );
170
171 // Bailout.
172 if ( ! $this->is_valid_post_type( $id ) ) {
173 return $this->check;
174 }
175
176 $meta_id = add_metadata( $this->meta_type, $id, $meta_key, $meta_value, $unique );
177
178 if ( $meta_id ) {
179 $this->delete_cache( $id );
180 }
181
182 return $meta_id;
183 }
184
185 /**
186 * Update payment meta field based on Post Type ID.
187 *
188 * For internal use only. Use Give_Payment->update_meta() for public usage.
189 *
190 * Use the $prev_value parameter to differentiate between meta fields with the
191 * same key and Post Type ID.
192 *
193 * If the meta field for the payment does not exist, it will be added.
194 *
195 * @access public
196 * @since 2.0
197 *
198 * @param int $id Post Type ID.
199 * @param string $meta_key Metadata key.
200 * @param mixed $meta_value Metadata value.
201 * @param mixed $prev_value Optional. Previous value to check before removing.
202 *
203 * @return int|bool False on failure, true if success.
204 */
205 public function update_meta( $id = 0, $meta_key = '', $meta_value, $prev_value = '' ) {
206 $id = $this->sanitize_id( $id );
207
208 // Bailout.
209 if ( ! $this->is_valid_post_type( $id ) ) {
210 return $this->check;
211 }
212
213 $meta_id = update_metadata( $this->meta_type, $id, $meta_key, $meta_value, $prev_value );
214
215 if ( $meta_id ) {
216 $this->delete_cache( $id );
217 }
218
219 return $meta_id;
220 }
221
222 /**
223 * Remove metadata matching criteria from a payment.
224 *
225 * You can match based on the key, or key and value. Removing based on key and
226 * value, will keep from removing duplicate metadata with the same key. It also
227 * allows removing all metadata matching key, if needed.
228 *
229 * @access public
230 * @since 2.0
231 *
232 * @param int $id Post Type ID.
233 * @param string $meta_key Metadata name.
234 * @param mixed $meta_value Optional. Metadata value.
235 * @param mixed $delete_all Optional.
236 *
237 * @return bool False for failure. True for success.
238 */
239 public function delete_meta( $id = 0, $meta_key = '', $meta_value = '', $delete_all = '' ) {
240 $id = $this->sanitize_id( $id );
241
242 // Bailout.
243 if ( ! $this->is_valid_post_type( $id ) ) {
244 return $this->check;
245 }
246
247 $is_meta_deleted = delete_metadata( $this->meta_type, $id, $meta_key, $meta_value, $delete_all );
248
249 if ( $is_meta_deleted ) {
250 $this->delete_cache( $id );
251 }
252
253 return $is_meta_deleted;
254 }
255
256 /**
257 * Rename query clauses of every query for new meta table
258 *
259 * @since 2.0
260 * @access public
261 *
262 * @param string $clause
263 * @param WP_Query $wp_query
264 *
265 * @return string
266 */
267 public function __rename_meta_table_name_in_query( $clause, $wp_query ) {
268 // Add new table to sql query.
269 if ( $this->is_post_type_query( $wp_query ) && ! empty( $wp_query->meta_query->queries ) ) {
270 $clause = $this->__rename_meta_table_name( $clause, current_filter() );
271 }
272
273 return $clause;
274 }
275
276
277 /**
278 * Rename query clauses for new meta table
279 *
280 * @param $clause
281 * @param $filter
282 *
283 * @return mixed
284 */
285 public function __rename_meta_table_name( $clause, $filter ){
286 global $wpdb;
287
288 $clause = str_replace( "{$wpdb->postmeta}.post_id", "{$this->table_name}.{$this->meta_type}_id", $clause );
289 $clause = str_replace( $wpdb->postmeta, $this->table_name, $clause );
290
291 switch( $filter ) {
292 case 'posts_join':
293 $joins = array( 'INNER JOIN', 'LEFT JOIN' );
294
295 foreach ( $joins as $join ) {
296 if( false !== strpos( $clause, $join ) ) {
297 $clause = explode( $join, $clause );
298
299 foreach ( $clause as $key => $clause_part ) {
300 if( empty( $clause_part ) ) {
301 continue;
302 }
303
304 preg_match( '/' . $wpdb->prefix . 'give_' . $this->meta_type . 'meta AS (.*) ON/', $clause_part, $alias_table_name );
305
306 if( isset( $alias_table_name[1] ) ) {
307 $clause[$key] = str_replace( "{$alias_table_name[1]}.post_id", "{$alias_table_name[1]}.{$this->meta_type}_id", $clause_part );
308 }
309 }
310
311 $clause = implode( "{$join} ", $clause );
312 }
313 }
314 break;
315
316 case 'posts_where':
317 $clause = str_replace( array( 'mt2.post_id', 'mt1.post_id' ), array( "mt2.{$this->meta_type}_id", "mt1.{$this->meta_type}_id" ), $clause );
318 break;
319 }
320
321 return $clause;
322 }
323
324
325 /**
326 * Check if current query for post type or not.
327 *
328 * @since 2.0
329 * @access protected
330 *
331 * @param WP_Query $wp_query
332 *
333 * @return bool
334 */
335 protected function is_post_type_query( $wp_query ) {
336 $status = false;
337
338 // Check if it is payment query.
339 if ( ! empty( $wp_query->query['post_type'] ) ) {
340 if (
341 is_string( $wp_query->query['post_type'] ) &&
342 $this->post_type === $wp_query->query['post_type']
343 ) {
344 $status = true;
345 } elseif (
346 is_array( $wp_query->query['post_type'] ) &&
347 in_array( $this->post_type, $wp_query->query['post_type'] )
348 ) {
349 $status = true;
350 }
351 }
352
353 return $status;
354 }
355
356 /**
357 * Check if current id of post type or not
358 *
359 * @since 2.0
360 * @access protected
361 *
362 * @param $ID
363 *
364 * @return bool
365 */
366 protected function is_valid_post_type( $ID ) {
367 return $ID && ( $this->post_type === get_post_type( $ID ) );
368 }
369
370 /**
371 * check if custom meta table enabled or not.
372 *
373 * @since 2.0
374 * @access protected
375 * @return bool
376 */
377 protected function is_custom_meta_table_active() {
378 return false;
379 }
380
381
382 /**
383 * Update last_changed key
384 *
385 * @since 2.0
386 * @access private
387 *
388 * @param int $id
389 * @param string $meta_type
390 *
391 * @return void
392 */
393 private function delete_cache( $id, $meta_type = '' ) {
394 $meta_type = empty( $meta_type ) ? $this->meta_type : $meta_type;
395
396 $group = array(
397 // 'form' => 'give-forms',
398 'payment' => 'give-donations',
399 'donor' => 'give-donors',
400 'customer' => 'give-donors', // Backward compatibility for pre upgrade in 2.0
401 // 'log' => 'give-logs',
402 );
403
404 if ( array_key_exists( $meta_type, $group ) ) {
405 Give_Cache::delete_group( $id, $group[ $meta_type ] );
406 }
407 }
408
409 /**
410 * Add support for hidden functions.
411 *
412 * @since 2.0
413 * @access public
414 *
415 * @param $name
416 * @param $arguments
417 *
418 * @return mixed
419 */
420 public function __call( $name, $arguments ) {
421 switch ( $name ) {
422 case '__add_meta':
423 $this->check = $arguments[0];
424 $id = $arguments[1];
425 $meta_key = $arguments[2];
426 $meta_value = $arguments[3];
427 $unique = $arguments[4];
428
429 // Bailout.
430 if ( ! $this->is_valid_post_type( $id ) ) {
431 return $this->check;
432 }
433
434 return $this->add_meta( $id, $meta_key, $meta_value, $unique );
435
436 case '__get_meta':
437 $this->check = $arguments[0];
438 $id = $arguments[1];
439 $meta_key = $arguments[2];
440 $single = $arguments[3];
441
442 // Bailout.
443 if ( ! $this->is_valid_post_type( $id ) ) {
444 return $this->check;
445 }
446
447 $this->raw_result = true;
448
449 return $this->get_meta( $id, $meta_key, $single );
450
451 case '__update_meta':
452 $this->check = $arguments[0];
453 $id = $arguments[1];
454 $meta_key = $arguments[2];
455 $meta_value = $arguments[3];
456
457 // Bailout.
458 if ( ! $this->is_valid_post_type( $id ) ) {
459 return $this->check;
460 }
461
462 return $this->update_meta( $id, $meta_key, $meta_value );
463
464 case '__delete_meta':
465 $this->check = $arguments[0];
466 $id = $arguments[1];
467 $meta_key = $arguments[2];
468 $meta_value = $arguments[3];
469 $delete_all = $arguments[3];
470
471 // Bailout.
472 if ( ! $this->is_valid_post_type( $id ) ) {
473 return $this->check;
474 }
475
476 return $this->delete_meta( $id, $meta_key, $meta_value, $delete_all );
477 }
478 }
479
480 /**
481 * Create Meta Tables.
482 *
483 * @since 2.0.1
484 * @access public
485 */
486 public function create_table() {
487 global $wpdb;
488
489 $charset_collate = $wpdb->get_charset_collate();
490
491 $sql = "CREATE TABLE {$this->table_name} (
492 meta_id bigint(20) NOT NULL AUTO_INCREMENT,
493 {$this->meta_type}_id bigint(20) NOT NULL,
494 meta_key varchar(255) DEFAULT NULL,
495 meta_value longtext,
496 PRIMARY KEY (meta_id),
497 KEY {$this->meta_type}_id ({$this->meta_type}_id),
498 KEY meta_key (meta_key({$this->min_index_length}))
499 ) {$charset_collate};";
500
501 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
502 dbDelta( $sql );
503
504 update_option( $this->table_name . '_db_version', $this->version );
505 }
506 }