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