actions.php
6 years ago
backward-compatibility.php
6 years ago
class-give-payment.php
6 years ago
class-give-sequential-donation-number.php
6 years ago
class-payment-stats.php
6 years ago
class-payments-query.php
6 years ago
functions.php
6 years ago
class-payments-query.php
938 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Payments Query |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Classes/Stats |
| 7 | * @copyright Copyright (c) 2016, GiveWP |
| 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | * @since 1.0 |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly. |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Give_Payments_Query Class |
| 19 | * |
| 20 | * This class is for retrieving payments data. |
| 21 | * |
| 22 | * Payments can be retrieved for date ranges and pre-defined periods. |
| 23 | * |
| 24 | * @since 1.0 |
| 25 | */ |
| 26 | class Give_Payments_Query extends Give_Stats { |
| 27 | |
| 28 | /** |
| 29 | * Preserve args |
| 30 | * |
| 31 | * @since 1.8.17 |
| 32 | * @access public |
| 33 | * |
| 34 | * @var array |
| 35 | */ |
| 36 | public $_args = array(); |
| 37 | |
| 38 | /** |
| 39 | * The args to pass to the give_get_payments() query |
| 40 | * |
| 41 | * @since 1.0 |
| 42 | * @access public |
| 43 | * |
| 44 | * @var array |
| 45 | */ |
| 46 | public $args = array(); |
| 47 | |
| 48 | /** |
| 49 | * The payments found based on the criteria set |
| 50 | * |
| 51 | * @since 1.0 |
| 52 | * @access public |
| 53 | * |
| 54 | * @var array |
| 55 | */ |
| 56 | public $payments = array(); |
| 57 | |
| 58 | /** |
| 59 | * Default query arguments. |
| 60 | * |
| 61 | * Not all of these are valid arguments that can be passed to WP_Query. The ones that are not, are modified before |
| 62 | * the query is run to convert them to the proper syntax. |
| 63 | * |
| 64 | * @since 1.0 |
| 65 | * @access public |
| 66 | * |
| 67 | * @param $args array The array of arguments that can be passed in and used for setting up this payment query. |
| 68 | */ |
| 69 | public function __construct( $args = array() ) { |
| 70 | $defaults = array( |
| 71 | 'output' => 'payments', |
| 72 | 'post_type' => array( 'give_payment' ), |
| 73 | 'start_date' => false, |
| 74 | 'end_date' => false, |
| 75 | 'number' => 20, |
| 76 | 'page' => null, |
| 77 | 'orderby' => 'ID', |
| 78 | 'order' => 'DESC', |
| 79 | 'user' => null, // deprecated, use donor |
| 80 | 'donor' => null, |
| 81 | 'status' => give_get_payment_status_keys(), |
| 82 | 'meta_key' => null, |
| 83 | 'year' => null, |
| 84 | 'month' => null, |
| 85 | 'day' => null, |
| 86 | 's' => null, |
| 87 | 'search_in_notes' => false, |
| 88 | 'children' => false, |
| 89 | 'fields' => null, |
| 90 | 'gateway' => null, |
| 91 | 'give_forms' => null, |
| 92 | 'offset' => null, |
| 93 | |
| 94 | // Currently these params only works with get_payment_by_group |
| 95 | 'group_by' => '', |
| 96 | 'count' => false, |
| 97 | ); |
| 98 | |
| 99 | // We do not want WordPress to handle meta cache because WordPress stores in under `post_meta` key and cache object while we want it under `donation_meta`. |
| 100 | // Similar for term cache |
| 101 | $args['update_post_meta_cache'] = false; |
| 102 | |
| 103 | $this->args = $this->_args = wp_parse_args( $args, $defaults ); |
| 104 | |
| 105 | $this->init(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Set a query variable. |
| 110 | * |
| 111 | * @since 1.0 |
| 112 | * @access public |
| 113 | * |
| 114 | * @param $query_var |
| 115 | * @param $value |
| 116 | */ |
| 117 | public function __set( $query_var, $value ) { |
| 118 | if ( in_array( $query_var, array( 'meta_query', 'tax_query' ) ) ) { |
| 119 | $this->args[ $query_var ][] = $value; |
| 120 | } else { |
| 121 | $this->args[ $query_var ] = $value; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Unset a query variable. |
| 127 | * |
| 128 | * @since 1.0 |
| 129 | * @access public |
| 130 | * |
| 131 | * @param $query_var |
| 132 | */ |
| 133 | public function __unset( $query_var ) { |
| 134 | unset( $this->args[ $query_var ] ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Modify the query/query arguments before we retrieve payments. |
| 139 | * |
| 140 | * @since 1.0 |
| 141 | * @access public |
| 142 | * |
| 143 | * @return void |
| 144 | */ |
| 145 | public function init() { |
| 146 | } |
| 147 | |
| 148 | |
| 149 | /** |
| 150 | * Set query filter. |
| 151 | * |
| 152 | * @since 1.8.9 |
| 153 | * @access private |
| 154 | */ |
| 155 | private function set_filters() { |
| 156 | // Reset param to apply filters. |
| 157 | // While set filters $args will get override and multiple get_payments call will not work. |
| 158 | $this->args = $this->_args; |
| 159 | |
| 160 | // Whitelist order. |
| 161 | $this->args['order'] = in_array( strtoupper( $this->args['order'] ), array( 'ASC', 'DESC' ) ) ? $this->args['order'] : 'DESC'; |
| 162 | |
| 163 | $this->date_filter_pre(); |
| 164 | $this->orderby(); |
| 165 | $this->status(); |
| 166 | $this->month(); |
| 167 | $this->per_page(); |
| 168 | $this->page(); |
| 169 | $this->user(); |
| 170 | $this->donor(); |
| 171 | $this->search(); |
| 172 | $this->mode(); |
| 173 | $this->children(); |
| 174 | $this->give_forms(); |
| 175 | $this->gateway_filter(); |
| 176 | |
| 177 | add_filter( 'posts_orderby', array( $this, 'custom_orderby' ), 10, 2 ); |
| 178 | |
| 179 | /** |
| 180 | * Fires after setup filters. |
| 181 | * |
| 182 | * @since 1.0 |
| 183 | * |
| 184 | * @param Give_Payments_Query $this Payments query object. |
| 185 | */ |
| 186 | do_action( 'give_pre_get_payments', $this ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Unset query filter. |
| 191 | * |
| 192 | * @since 1.8.9 |
| 193 | * @access private |
| 194 | */ |
| 195 | private function unset_filters() { |
| 196 | remove_filter( 'posts_orderby', array( $this, 'custom_orderby' ) ); |
| 197 | |
| 198 | /** |
| 199 | * Fires after retrieving payments. |
| 200 | * |
| 201 | * @since 1.0 |
| 202 | * |
| 203 | * @param Give_Payments_Query $this Payments query object. |
| 204 | */ |
| 205 | do_action( 'give_post_get_payments', $this ); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | /** |
| 210 | * Retrieve payments. |
| 211 | * |
| 212 | * The query can be modified in two ways; either the action before the |
| 213 | * query is run, or the filter on the arguments (existing mainly for backwards |
| 214 | * compatibility). |
| 215 | * |
| 216 | * @since 1.0 |
| 217 | * @access public |
| 218 | * |
| 219 | * @return array |
| 220 | */ |
| 221 | public function get_payments() { |
| 222 | global $post; |
| 223 | |
| 224 | $results = array(); |
| 225 | $this->payments = array(); |
| 226 | $cache_key = Give_Cache::get_key( 'give_payment_query', $this->args, false ); |
| 227 | $this->payments = Give_Cache::get_db_query( $cache_key ); |
| 228 | |
| 229 | // Return cached result. |
| 230 | if ( ! is_null( $this->payments ) ) { |
| 231 | return $this->payments; |
| 232 | } |
| 233 | |
| 234 | // Modify the query/query arguments before we retrieve payments. |
| 235 | $this->set_filters(); |
| 236 | |
| 237 | /* @var WP_Query $query */ |
| 238 | $query = new WP_Query( $this->args ); |
| 239 | |
| 240 | $custom_output = array( |
| 241 | 'payments', |
| 242 | 'give_payments', |
| 243 | ); |
| 244 | |
| 245 | if ( $query->have_posts() ) { |
| 246 | |
| 247 | // Update meta cache only if query is not for all donations. |
| 248 | // @see https://github.com/impress-org/give/issues/4104 |
| 249 | if ( |
| 250 | ( isset( $this->args['nopaging'] ) && true !== (bool) $this->args['nopaging'] ) |
| 251 | || ( isset( $this->args['posts_per_page'] ) && 0 < $this->args['posts_per_page'] ) |
| 252 | ) { |
| 253 | self::update_meta_cache( wp_list_pluck( $query->posts, 'ID' ) ); |
| 254 | } |
| 255 | |
| 256 | if ( ! in_array( $this->args['output'], $custom_output ) ) { |
| 257 | $results = $query->posts; |
| 258 | |
| 259 | } else { |
| 260 | $previous_post = $post; |
| 261 | |
| 262 | while ( $query->have_posts() ) { |
| 263 | $query->the_post(); |
| 264 | |
| 265 | $payment_id = get_post()->ID; |
| 266 | $payment = new Give_Payment( $payment_id ); |
| 267 | |
| 268 | $this->payments[] = apply_filters( 'give_payment', $payment, $payment_id, $this ); |
| 269 | } |
| 270 | |
| 271 | wp_reset_postdata(); |
| 272 | |
| 273 | // Prevent nest loop from producing unexpected results. |
| 274 | if ( $previous_post instanceof WP_Post ) { |
| 275 | $post = $previous_post; |
| 276 | setup_postdata( $post ); |
| 277 | } |
| 278 | |
| 279 | $results = $this->payments; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | Give_Cache::set_db_query( $cache_key, $results ); |
| 284 | |
| 285 | // Remove query filters after we retrieve payments. |
| 286 | $this->unset_filters(); |
| 287 | |
| 288 | return $results; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Get payments by group |
| 293 | * |
| 294 | * @since 1.8.17 |
| 295 | * @access public |
| 296 | * |
| 297 | * @return array |
| 298 | */ |
| 299 | public function get_payment_by_group() { |
| 300 | global $wpdb; |
| 301 | |
| 302 | $allowed_groups = array( 'post_status' ); |
| 303 | $result = array(); |
| 304 | |
| 305 | if ( in_array( $this->args['group_by'], $allowed_groups ) ) { |
| 306 | // Set only count in result. |
| 307 | if ( $this->args['count'] ) { |
| 308 | |
| 309 | $this->set_filters(); |
| 310 | |
| 311 | $new_results = $wpdb->get_results( $this->get_sql(), ARRAY_N ); |
| 312 | |
| 313 | $this->unset_filters(); |
| 314 | |
| 315 | foreach ( $new_results as $results ) { |
| 316 | $result[ $results[0] ] = $results[1]; |
| 317 | } |
| 318 | |
| 319 | switch ( $this->args['group_by'] ) { |
| 320 | case 'post_status': |
| 321 | /* @var Give_Payment $donation */ |
| 322 | foreach ( give_get_payment_status_keys() as $status ) { |
| 323 | if ( ! isset( $result[ $status ] ) ) { |
| 324 | $result[ $status ] = 0; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | break; |
| 329 | } |
| 330 | } else { |
| 331 | $donations = $this->get_payments(); |
| 332 | |
| 333 | /* @var $donation Give_Payment */ |
| 334 | foreach ( $donations as $donation ) { |
| 335 | $result[ $donation->{$this->args['group_by']} ][] = $donation; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Filter the result |
| 342 | * |
| 343 | * @since 1.8.17 |
| 344 | */ |
| 345 | return apply_filters( 'give_get_payment_by_group', $result, $this ); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * If querying a specific date, add the proper filters. |
| 350 | * |
| 351 | * @since 1.0 |
| 352 | * @access public |
| 353 | * |
| 354 | * @return void |
| 355 | */ |
| 356 | public function date_filter_pre() { |
| 357 | if ( ! ( $this->args['start_date'] || $this->args['end_date'] ) ) { |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | $this->setup_dates( $this->args['start_date'], $this->args['end_date'] ); |
| 362 | |
| 363 | $is_start_date = property_exists( __CLASS__, 'start_date' ); |
| 364 | $is_end_date = property_exists( __CLASS__, 'end_date' ); |
| 365 | |
| 366 | if ( $is_start_date || $is_end_date ) { |
| 367 | $date_query = array(); |
| 368 | |
| 369 | if ( $is_start_date && ! is_wp_error( $this->start_date ) ) { |
| 370 | $date_query['after'] = date( 'Y-m-d H:i:s', $this->start_date ); |
| 371 | } |
| 372 | |
| 373 | if ( $is_end_date && ! is_wp_error( $this->end_date ) ) { |
| 374 | $date_query['before'] = date( 'Y-m-d H:i:s', $this->end_date ); |
| 375 | } |
| 376 | |
| 377 | // Include Start Date and End Date while querying. |
| 378 | $date_query['inclusive'] = true; |
| 379 | |
| 380 | $this->__set( 'date_query', $date_query ); |
| 381 | |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Post Status |
| 387 | * |
| 388 | * @since 1.0 |
| 389 | * @access public |
| 390 | * |
| 391 | * @return void |
| 392 | */ |
| 393 | public function status() { |
| 394 | if ( ! isset( $this->args['status'] ) ) { |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | $this->__set( 'post_status', $this->args['status'] ); |
| 399 | $this->__unset( 'status' ); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Current Page |
| 404 | * |
| 405 | * @since 1.0 |
| 406 | * @access public |
| 407 | * |
| 408 | * @return void |
| 409 | */ |
| 410 | public function page() { |
| 411 | if ( ! isset( $this->args['page'] ) ) { |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | $this->__set( 'paged', $this->args['page'] ); |
| 416 | $this->__unset( 'page' ); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Posts Per Page |
| 421 | * |
| 422 | * @since 1.0 |
| 423 | * @access public |
| 424 | * |
| 425 | * @return void |
| 426 | */ |
| 427 | public function per_page() { |
| 428 | |
| 429 | if ( ! isset( $this->args['number'] ) ) { |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | if ( $this->args['number'] == - 1 ) { |
| 434 | $this->__set( 'nopaging', true ); |
| 435 | } else { |
| 436 | $this->__set( 'posts_per_page', $this->args['number'] ); |
| 437 | } |
| 438 | |
| 439 | $this->__unset( 'number' ); |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Current Month |
| 444 | * |
| 445 | * @since 1.0 |
| 446 | * @access public |
| 447 | * |
| 448 | * @return void |
| 449 | */ |
| 450 | public function month() { |
| 451 | if ( ! isset( $this->args['month'] ) ) { |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | $this->__set( 'monthnum', $this->args['month'] ); |
| 456 | $this->__unset( 'month' ); |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Order by |
| 461 | * |
| 462 | * @since 1.0 |
| 463 | * @access public |
| 464 | * |
| 465 | * @return void |
| 466 | */ |
| 467 | public function orderby() { |
| 468 | switch ( $this->args['orderby'] ) { |
| 469 | case 'amount': |
| 470 | $this->__set( 'orderby', 'meta_value_num' ); |
| 471 | $this->__set( 'meta_key', '_give_payment_total' ); |
| 472 | break; |
| 473 | |
| 474 | case 'status': |
| 475 | $this->__set( 'orderby', 'post_status' ); |
| 476 | break; |
| 477 | |
| 478 | case 'donation_form': |
| 479 | $this->__set( 'orderby', 'meta_value' ); |
| 480 | $this->__set( 'meta_key', '_give_payment_form_title' ); |
| 481 | break; |
| 482 | |
| 483 | default: |
| 484 | $this->__set( 'orderby', $this->args['orderby'] ); |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Custom orderby. |
| 491 | * Note: currently custom sorting is only used for donation listing page. |
| 492 | * |
| 493 | * @since 1.8 |
| 494 | * @access public |
| 495 | * |
| 496 | * @param string $order |
| 497 | * @param WP_Query $query |
| 498 | * |
| 499 | * @return mixed |
| 500 | */ |
| 501 | public function custom_orderby( $order, $query ) { |
| 502 | |
| 503 | if ( ! empty( $query->query['post_type'] ) ) { |
| 504 | $post_types = is_array( $query->query['post_type'] ) ? $query->query['post_type'] : array( $query->query['post_type'] ); |
| 505 | |
| 506 | if ( ! in_array( 'give_payment', $post_types ) || ! isset( $query->query['orderby'] ) || is_array( $query->query['orderby'] ) ) { |
| 507 | return $order; |
| 508 | } |
| 509 | |
| 510 | global $wpdb; |
| 511 | switch ( $query->query['orderby'] ) { |
| 512 | case 'post_status': |
| 513 | $order = $wpdb->posts . '.post_status ' . strtoupper( $query->query['order'] ); |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | return $order; |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Specific User |
| 523 | * |
| 524 | * @since 1.0 |
| 525 | * @access public |
| 526 | * |
| 527 | * @return void |
| 528 | */ |
| 529 | public function user() { |
| 530 | if ( is_null( $this->args['user'] ) ) { |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | $args = array(); |
| 535 | |
| 536 | if ( is_numeric( $this->args['user'] ) ) { |
| 537 | // Backward compatibility: user donor param to get payment attached to donor instead of user |
| 538 | $donor_id = Give()->donors->get_column_by( 'id', 'user_id', $this->args['user'] ); |
| 539 | |
| 540 | $args = array( |
| 541 | 'key' => '_give_payment_donor_id', |
| 542 | 'value' => $donor_id ?: -1, |
| 543 | ); |
| 544 | } elseif ( is_email( $this->args['user'] ) ) { |
| 545 | $args = array( |
| 546 | 'key' => '_give_payment_donor_email', |
| 547 | 'value' => $this->args['user'], |
| 548 | ); |
| 549 | } |
| 550 | |
| 551 | $this->__set( 'meta_query', $args ); |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Specific donor id |
| 556 | * |
| 557 | * @access public |
| 558 | * @since 1.8.9 |
| 559 | * @return void |
| 560 | */ |
| 561 | public function donor() { |
| 562 | if ( is_null( $this->args['donor'] ) || ! is_numeric( $this->args['donor'] ) ) { |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | $donor_meta_type = Give()->donor_meta->meta_type; |
| 567 | |
| 568 | $this->__set( |
| 569 | 'meta_query', |
| 570 | array( |
| 571 | 'key' => "_give_payment_{$donor_meta_type}_id", |
| 572 | 'value' => (int) $this->args['donor'], |
| 573 | ) |
| 574 | ); |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Search |
| 579 | * |
| 580 | * @since 1.0 |
| 581 | * @access public |
| 582 | * |
| 583 | * @return void |
| 584 | */ |
| 585 | public function search() { |
| 586 | |
| 587 | if ( ! isset( $this->args['s'] ) ) { |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | $search = trim( $this->args['s'] ); |
| 592 | |
| 593 | if ( empty( $search ) ) { |
| 594 | return; |
| 595 | } |
| 596 | |
| 597 | $is_email = is_email( $search ) || strpos( $search, '@' ) !== false; |
| 598 | $is_user = strpos( $search, strtolower( 'user:' ) ) !== false; |
| 599 | |
| 600 | if ( ! empty( $this->args['search_in_notes'] ) ) { |
| 601 | |
| 602 | $notes = give_get_payment_notes( 0, $search ); |
| 603 | |
| 604 | if ( ! empty( $notes ) ) { |
| 605 | |
| 606 | $payment_ids = wp_list_pluck( (array) $notes, 'comment_post_ID' ); |
| 607 | |
| 608 | $this->__set( 'post__in', $payment_ids ); |
| 609 | } |
| 610 | |
| 611 | $this->__unset( 's' ); |
| 612 | |
| 613 | } elseif ( $is_email || strlen( $search ) == 32 ) { |
| 614 | |
| 615 | $key = $is_email ? '_give_payment_donor_email' : '_give_payment_purchase_key'; |
| 616 | $search_meta = array( |
| 617 | 'key' => $key, |
| 618 | 'value' => $search, |
| 619 | 'compare' => 'LIKE', |
| 620 | ); |
| 621 | |
| 622 | $this->__set( 'meta_query', $search_meta ); |
| 623 | $this->__unset( 's' ); |
| 624 | |
| 625 | } elseif ( $is_user ) { |
| 626 | |
| 627 | $search_meta = array( |
| 628 | 'key' => '_give_payment_donor_id', |
| 629 | 'value' => trim( str_replace( 'user:', '', strtolower( $search ) ) ), |
| 630 | ); |
| 631 | |
| 632 | $this->__set( 'meta_query', $search_meta ); |
| 633 | |
| 634 | $this->__unset( 's' ); |
| 635 | |
| 636 | } elseif ( is_numeric( $search ) ) { |
| 637 | |
| 638 | $post = get_post( $search ); |
| 639 | |
| 640 | if ( is_object( $post ) && $post->post_type == 'give_payment' ) { |
| 641 | |
| 642 | $arr = array(); |
| 643 | $arr[] = $search; |
| 644 | $this->__set( 'post__in', $arr ); |
| 645 | $this->__unset( 's' ); |
| 646 | } |
| 647 | } elseif ( '#' == substr( $search, 0, 1 ) ) { |
| 648 | |
| 649 | $search = str_replace( '#:', '', $search ); |
| 650 | $search = str_replace( '#', '', $search ); |
| 651 | $this->__set( 'give_forms', $search ); |
| 652 | $this->__unset( 's' ); |
| 653 | |
| 654 | } elseif ( ! empty( $search ) ) { |
| 655 | $search_parts = preg_split( '/\s+/', $search ); |
| 656 | |
| 657 | if ( is_array( $search_parts ) && 2 === count( $search_parts ) ) { |
| 658 | $search_meta = array( |
| 659 | 'relation' => 'AND', |
| 660 | array( |
| 661 | 'key' => '_give_donor_billing_first_name', |
| 662 | 'value' => $search_parts[0], |
| 663 | 'compare' => '=', |
| 664 | ), |
| 665 | array( |
| 666 | 'key' => '_give_donor_billing_last_name', |
| 667 | 'value' => $search_parts[1], |
| 668 | 'compare' => '=', |
| 669 | ), |
| 670 | ); |
| 671 | } else { |
| 672 | $search_meta = array( |
| 673 | 'relation' => 'OR', |
| 674 | array( |
| 675 | 'key' => '_give_donor_billing_first_name', |
| 676 | 'value' => $search, |
| 677 | 'compare' => 'LIKE', |
| 678 | ), |
| 679 | array( |
| 680 | 'key' => '_give_donor_billing_last_name', |
| 681 | 'value' => $search, |
| 682 | 'compare' => 'LIKE', |
| 683 | ), |
| 684 | ); |
| 685 | } |
| 686 | |
| 687 | $this->__set( 'meta_query', $search_meta ); |
| 688 | |
| 689 | $this->__unset( 's' ); |
| 690 | |
| 691 | } else { |
| 692 | $this->__set( 's', $search ); |
| 693 | |
| 694 | } |
| 695 | |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Payment Mode |
| 700 | * |
| 701 | * @since 1.0 |
| 702 | * @access public |
| 703 | * |
| 704 | * @return void |
| 705 | */ |
| 706 | public function mode() { |
| 707 | if ( empty( $this->args['mode'] ) || $this->args['mode'] == 'all' ) { |
| 708 | $this->__unset( 'mode' ); |
| 709 | |
| 710 | return; |
| 711 | } |
| 712 | |
| 713 | $this->__set( |
| 714 | 'meta_query', |
| 715 | array( |
| 716 | 'key' => '_give_payment_mode', |
| 717 | 'value' => $this->args['mode'], |
| 718 | ) |
| 719 | ); |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * Children |
| 724 | * |
| 725 | * @since 1.0 |
| 726 | * @access public |
| 727 | * |
| 728 | * @return void |
| 729 | */ |
| 730 | public function children() { |
| 731 | if ( empty( $this->args['children'] ) ) { |
| 732 | $this->__set( 'post_parent', 0 ); |
| 733 | } |
| 734 | $this->__unset( 'children' ); |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * Specific Give Form |
| 739 | * |
| 740 | * @since 1.0 |
| 741 | * @access public |
| 742 | * |
| 743 | * @return void |
| 744 | */ |
| 745 | public function give_forms() { |
| 746 | |
| 747 | if ( empty( $this->args['give_forms'] ) ) { |
| 748 | return; |
| 749 | } |
| 750 | |
| 751 | $compare = '='; |
| 752 | |
| 753 | if ( is_array( $this->args['give_forms'] ) ) { |
| 754 | $compare = 'IN'; |
| 755 | } |
| 756 | |
| 757 | $this->__set( |
| 758 | 'meta_query', |
| 759 | array( |
| 760 | 'key' => '_give_payment_form_id', |
| 761 | 'value' => $this->args['give_forms'], |
| 762 | 'compare' => $compare, |
| 763 | ) |
| 764 | ); |
| 765 | |
| 766 | $this->__unset( 'give_forms' ); |
| 767 | |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * Specific Gateway |
| 772 | * |
| 773 | * @since 1.8.17 |
| 774 | * @access public |
| 775 | * |
| 776 | * @return void |
| 777 | */ |
| 778 | public function gateway_filter() { |
| 779 | |
| 780 | if ( empty( $this->args['gateway'] ) ) { |
| 781 | return; |
| 782 | } |
| 783 | |
| 784 | $compare = '='; |
| 785 | |
| 786 | if ( is_array( $this->args['gateway'] ) ) { |
| 787 | $compare = 'IN'; |
| 788 | } |
| 789 | |
| 790 | $this->__set( |
| 791 | 'meta_query', |
| 792 | array( |
| 793 | 'key' => '_give_payment_gateway', |
| 794 | 'value' => $this->args['gateway'], |
| 795 | 'compare' => $compare, |
| 796 | ) |
| 797 | ); |
| 798 | |
| 799 | $this->__unset( 'gateway' ); |
| 800 | |
| 801 | } |
| 802 | |
| 803 | |
| 804 | /** |
| 805 | * Get sql query |
| 806 | * |
| 807 | * Note: Internal purpose only. We are developing on this fn. |
| 808 | * |
| 809 | * @since 1.8.18 |
| 810 | * @access public |
| 811 | * @global $wpdb |
| 812 | * |
| 813 | * @return string |
| 814 | */ |
| 815 | private function get_sql() { |
| 816 | global $wpdb; |
| 817 | |
| 818 | $allowed_keys = array( |
| 819 | 'post_name', |
| 820 | 'post_author', |
| 821 | 'post_date', |
| 822 | 'post_title', |
| 823 | 'post_status', |
| 824 | 'post_modified', |
| 825 | 'post_parent', |
| 826 | 'post_type', |
| 827 | 'menu_order', |
| 828 | 'comment_count', |
| 829 | ); |
| 830 | |
| 831 | $this->args['orderby'] = 'post_parent__in'; |
| 832 | |
| 833 | // Whitelist orderby. |
| 834 | if ( ! in_array( $this->args['orderby'], $allowed_keys ) ) { |
| 835 | $this->args['orderby'] = 'ID'; |
| 836 | } |
| 837 | |
| 838 | $where = "WHERE {$wpdb->posts}.post_type = 'give_payment'"; |
| 839 | $where .= " AND {$wpdb->posts}.post_status IN ('" . implode( "','", $this->args['post_status'] ) . "')"; |
| 840 | |
| 841 | if ( is_numeric( $this->args['post_parent'] ) ) { |
| 842 | $where .= " AND {$wpdb->posts}.post_parent={$this->args['post_parent']}"; |
| 843 | } |
| 844 | |
| 845 | // Set orderby. |
| 846 | $orderby = "ORDER BY {$wpdb->posts}.{$this->args['orderby']}"; |
| 847 | $group_by = ''; |
| 848 | |
| 849 | // Set group by. |
| 850 | if ( ! empty( $this->args['group_by'] ) ) { |
| 851 | $group_by = "GROUP BY {$wpdb->posts}.{$this->args['group_by']}"; |
| 852 | } |
| 853 | |
| 854 | // Set offset. |
| 855 | if ( |
| 856 | empty( $this->args['nopaging'] ) && |
| 857 | empty( $this->args['offset'] ) && |
| 858 | ( ! empty( $this->args['page'] ) && 0 < $this->args['page'] ) |
| 859 | ) { |
| 860 | $this->args['offset'] = $this->args['posts_per_page'] * ( $this->args['page'] - 1 ); |
| 861 | } |
| 862 | |
| 863 | // Set fields. |
| 864 | $fields = "{$wpdb->posts}.*"; |
| 865 | if ( ! empty( $this->args['fields'] ) && 'all' !== $this->args['fields'] ) { |
| 866 | if ( is_string( $this->args['fields'] ) ) { |
| 867 | $fields = "{$wpdb->posts}.{$this->args['fields']}"; |
| 868 | } elseif ( is_array( $this->args['fields'] ) ) { |
| 869 | $fields = "{$wpdb->posts}." . implode( " , {$wpdb->posts}.", $this->args['fields'] ); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | // Set count. |
| 874 | if ( ! empty( $this->args['count'] ) ) { |
| 875 | $fields = "COUNT({$wpdb->posts}.ID)"; |
| 876 | |
| 877 | if ( ! empty( $this->args['group_by'] ) ) { |
| 878 | $fields = "{$wpdb->posts}.{$this->args['group_by']}, {$fields}"; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | // Date query. |
| 883 | if ( ! empty( $this->args['date_query'] ) ) { |
| 884 | $date_query_obj = new WP_Date_Query( $this->args['date_query'] ); |
| 885 | $where .= str_replace( |
| 886 | array( |
| 887 | "\n", |
| 888 | '( (', |
| 889 | '))', |
| 890 | ), |
| 891 | array( |
| 892 | '', |
| 893 | '( (', |
| 894 | ') )', |
| 895 | ), |
| 896 | $date_query_obj->get_sql() |
| 897 | ); |
| 898 | } |
| 899 | |
| 900 | // Meta query. |
| 901 | if ( ! empty( $this->args['meta_query'] ) ) { |
| 902 | $meta_query_obj = new WP_Meta_Query( $this->args['meta_query'] ); |
| 903 | $where = implode( ' ', $meta_query_obj->get_sql( 'post', $wpdb->posts, 'ID' ) ) . " {$where}"; |
| 904 | $where = Give()->payment_meta->__rename_meta_table_name( $where, 'posts_where' ); |
| 905 | } |
| 906 | |
| 907 | // Set sql query. |
| 908 | $sql = $wpdb->prepare( |
| 909 | "SELECT {$fields} FROM {$wpdb->posts} LIMIT %d,%d;", |
| 910 | absint( $this->args['offset'] ), |
| 911 | ( empty( $this->args['nopaging'] ) ? absint( $this->args['posts_per_page'] ) : 99999999999 ) |
| 912 | ); |
| 913 | |
| 914 | // $where, $orderby and order already prepared query they can generate notice if you re prepare them in above. |
| 915 | // WordPress consider LIKE condition as placeholder if start with s,f, or d. |
| 916 | $sql = str_replace( 'LIMIT', "{$where} {$group_by} {$orderby} {$this->args['order']} LIMIT", $sql ); |
| 917 | |
| 918 | return $sql; |
| 919 | } |
| 920 | |
| 921 | /** |
| 922 | * Update donations meta cache |
| 923 | * |
| 924 | * @since 2.5.0 |
| 925 | * @access private |
| 926 | * |
| 927 | * @param $donation_ids |
| 928 | */ |
| 929 | public static function update_meta_cache( $donation_ids ) { |
| 930 | // Exit. |
| 931 | if ( empty( $donation_ids ) ) { |
| 932 | return; |
| 933 | } |
| 934 | |
| 935 | update_meta_cache( Give()->payment_meta->get_meta_type(), $donation_ids ); |
| 936 | } |
| 937 | } |
| 938 |