actions.php
8 years ago
backward-compatibility.php
8 years ago
class-give-payment.php
8 years ago
class-payment-stats.php
8 years ago
class-payments-query.php
8 years ago
functions.php
8 years ago
class-payments-query.php
859 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Payments Query |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Classes/Stats |
| 7 | * @copyright Copyright (c) 2016, WordImpress |
| 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, |
| 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 | $this->args = $this->_args = wp_parse_args( $args, $defaults ); |
| 100 | |
| 101 | $this->init(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Set a query variable. |
| 106 | * |
| 107 | * @since 1.0 |
| 108 | * @access public |
| 109 | * |
| 110 | * @param $query_var |
| 111 | * @param $value |
| 112 | */ |
| 113 | public function __set( $query_var, $value ) { |
| 114 | if ( in_array( $query_var, array( 'meta_query', 'tax_query' ) ) ) { |
| 115 | $this->args[ $query_var ][] = $value; |
| 116 | } else { |
| 117 | $this->args[ $query_var ] = $value; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Unset a query variable. |
| 123 | * |
| 124 | * @since 1.0 |
| 125 | * @access public |
| 126 | * |
| 127 | * @param $query_var |
| 128 | */ |
| 129 | public function __unset( $query_var ) { |
| 130 | unset( $this->args[ $query_var ] ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Modify the query/query arguments before we retrieve payments. |
| 135 | * |
| 136 | * @since 1.0 |
| 137 | * @access public |
| 138 | * |
| 139 | * @return void |
| 140 | */ |
| 141 | public function init() { |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * Set query filter. |
| 147 | * |
| 148 | * @since 1.8.9 |
| 149 | * @access private |
| 150 | */ |
| 151 | private function set_filters() { |
| 152 | // Reset param to apply filters. |
| 153 | // While set filters $args will get override and multiple get_payments call will not work. |
| 154 | $this->args = $this->_args; |
| 155 | |
| 156 | $this->date_filter_pre(); |
| 157 | $this->orderby(); |
| 158 | $this->status(); |
| 159 | $this->month(); |
| 160 | $this->per_page(); |
| 161 | $this->page(); |
| 162 | $this->user(); |
| 163 | $this->donor(); |
| 164 | $this->search(); |
| 165 | $this->mode(); |
| 166 | $this->children(); |
| 167 | $this->give_forms(); |
| 168 | $this->gateway_filter(); |
| 169 | |
| 170 | add_filter( 'posts_orderby', array( $this, 'custom_orderby' ), 10, 2 ); |
| 171 | |
| 172 | /** |
| 173 | * Fires after setup filters. |
| 174 | * |
| 175 | * @since 1.0 |
| 176 | * |
| 177 | * @param Give_Payments_Query $this Payments query object. |
| 178 | */ |
| 179 | do_action( 'give_pre_get_payments', $this ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Unset query filter. |
| 184 | * |
| 185 | * @since 1.8.9 |
| 186 | * @access private |
| 187 | */ |
| 188 | private function unset_filters() { |
| 189 | remove_filter( 'posts_orderby', array( $this, 'custom_orderby' ) ); |
| 190 | |
| 191 | /** |
| 192 | * Fires after retrieving payments. |
| 193 | * |
| 194 | * @since 1.0 |
| 195 | * |
| 196 | * @param Give_Payments_Query $this Payments query object. |
| 197 | */ |
| 198 | do_action( 'give_post_get_payments', $this ); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | /** |
| 203 | * Retrieve payments. |
| 204 | * |
| 205 | * The query can be modified in two ways; either the action before the |
| 206 | * query is run, or the filter on the arguments (existing mainly for backwards |
| 207 | * compatibility). |
| 208 | * |
| 209 | * @since 1.0 |
| 210 | * @access public |
| 211 | * |
| 212 | * @return array |
| 213 | */ |
| 214 | public function get_payments() { |
| 215 | $cache_key = Give_Cache::get_key( 'give_payment_query', $this->args, false ); |
| 216 | $this->payments = Give_Cache::get_db_query( $cache_key ); |
| 217 | |
| 218 | // Return cached result. |
| 219 | if ( ! is_null( $this->payments ) ) { |
| 220 | return $this->payments; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | // Modify the query/query arguments before we retrieve payments. |
| 225 | $this->set_filters(); |
| 226 | |
| 227 | $query = new WP_Query( $this->args ); |
| 228 | $this->payments = array(); |
| 229 | |
| 230 | $custom_output = array( |
| 231 | 'payments', |
| 232 | 'give_payments', |
| 233 | ); |
| 234 | |
| 235 | if ( ! in_array( $this->args['output'], $custom_output ) ) { |
| 236 | return $query->posts; |
| 237 | } |
| 238 | |
| 239 | if ( $query->have_posts() ) { |
| 240 | while ( $query->have_posts() ) { |
| 241 | $query->the_post(); |
| 242 | |
| 243 | $payment_id = get_post()->ID; |
| 244 | $payment = new Give_Payment( $payment_id ); |
| 245 | |
| 246 | $this->payments[] = apply_filters( 'give_payment', $payment, $payment_id, $this ); |
| 247 | } |
| 248 | |
| 249 | wp_reset_postdata(); |
| 250 | } |
| 251 | |
| 252 | Give_Cache::set_db_query( $cache_key, $this->payments ); |
| 253 | |
| 254 | // Remove query filters after we retrieve payments. |
| 255 | $this->unset_filters(); |
| 256 | |
| 257 | return $this->payments; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Get payments by group |
| 262 | * |
| 263 | * @since 1.8.17 |
| 264 | * @access public |
| 265 | * |
| 266 | * @return array |
| 267 | */ |
| 268 | public function get_payment_by_group() { |
| 269 | global $wpdb; |
| 270 | |
| 271 | $allowed_groups = array( 'post_status' ); |
| 272 | $result = array(); |
| 273 | |
| 274 | |
| 275 | if ( in_array( $this->args['group_by'], $allowed_groups ) ) { |
| 276 | // Set only count in result. |
| 277 | if ( $this->args['count'] ) { |
| 278 | |
| 279 | $this->set_filters(); |
| 280 | |
| 281 | $new_results = $wpdb->get_results( $this->get_sql(), ARRAY_N ); |
| 282 | |
| 283 | $this->unset_filters(); |
| 284 | |
| 285 | foreach ( $new_results as $results ) { |
| 286 | $result[ $results[0] ] = $results[1]; |
| 287 | } |
| 288 | |
| 289 | switch ( $this->args['group_by'] ) { |
| 290 | case 'post_status': |
| 291 | |
| 292 | /* @var Give_Payment $donation */ |
| 293 | foreach ( give_get_payment_status_keys() as $status ) { |
| 294 | if ( ! isset( $result[ $status ] ) ) { |
| 295 | $result[ $status ] = 0; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | break; |
| 300 | } |
| 301 | } else { |
| 302 | $donations = $this->get_payments(); |
| 303 | |
| 304 | /* @var $donation Give_Payment */ |
| 305 | foreach ( $donations as $donation ) { |
| 306 | $result[ $donation->{$this->args['group_by']} ][] = $donation; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | |
| 312 | /** |
| 313 | * Filter the result |
| 314 | * |
| 315 | * @since 1.8.17 |
| 316 | */ |
| 317 | return apply_filters( 'give_get_payment_by_group', $result, $this ); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * If querying a specific date, add the proper filters. |
| 322 | * |
| 323 | * @since 1.0 |
| 324 | * @access public |
| 325 | * |
| 326 | * @return void |
| 327 | */ |
| 328 | public function date_filter_pre() { |
| 329 | if ( ! ( $this->args['start_date'] || $this->args['end_date'] ) ) { |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | $this->setup_dates( $this->args['start_date'], $this->args['end_date'] ); |
| 334 | $is_start_date = property_exists( __CLASS__, 'start_date' ); |
| 335 | $is_end_date = property_exists( __CLASS__, 'end_date' ); |
| 336 | |
| 337 | if ( $is_start_date || $is_end_date ) { |
| 338 | $date_query = array(); |
| 339 | |
| 340 | if ( $is_start_date && ! is_wp_error( $this->start_date ) ) { |
| 341 | $date_query['after'] = date( 'Y-m-d H:i:s', $this->start_date ); |
| 342 | } |
| 343 | |
| 344 | if ( $is_end_date && ! is_wp_error( $this->end_date ) ) { |
| 345 | $date_query['before'] = date( 'Y-m-d H:i:s', $this->end_date ); |
| 346 | } |
| 347 | |
| 348 | // Include Start Date and End Date while querying. |
| 349 | $date_query['inclusive'] = true; |
| 350 | |
| 351 | $this->__set( 'date_query', $date_query ); |
| 352 | |
| 353 | } |
| 354 | |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Post Status |
| 359 | * |
| 360 | * @since 1.0 |
| 361 | * @access public |
| 362 | * |
| 363 | * @return void |
| 364 | */ |
| 365 | public function status() { |
| 366 | if ( ! isset( $this->args['status'] ) ) { |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | $this->__set( 'post_status', $this->args['status'] ); |
| 371 | $this->__unset( 'status' ); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Current Page |
| 376 | * |
| 377 | * @since 1.0 |
| 378 | * @access public |
| 379 | * |
| 380 | * @return void |
| 381 | */ |
| 382 | public function page() { |
| 383 | if ( ! isset( $this->args['page'] ) ) { |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | $this->__set( 'paged', $this->args['page'] ); |
| 388 | $this->__unset( 'page' ); |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Posts Per Page |
| 393 | * |
| 394 | * @since 1.0 |
| 395 | * @access public |
| 396 | * |
| 397 | * @return void |
| 398 | */ |
| 399 | public function per_page() { |
| 400 | |
| 401 | if ( ! isset( $this->args['number'] ) ) { |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | if ( $this->args['number'] == - 1 ) { |
| 406 | $this->__set( 'nopaging', true ); |
| 407 | } else { |
| 408 | $this->__set( 'posts_per_page', $this->args['number'] ); |
| 409 | } |
| 410 | |
| 411 | $this->__unset( 'number' ); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Current Month |
| 416 | * |
| 417 | * @since 1.0 |
| 418 | * @access public |
| 419 | * |
| 420 | * @return void |
| 421 | */ |
| 422 | public function month() { |
| 423 | if ( ! isset( $this->args['month'] ) ) { |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | $this->__set( 'monthnum', $this->args['month'] ); |
| 428 | $this->__unset( 'month' ); |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Order by |
| 433 | * |
| 434 | * @since 1.0 |
| 435 | * @access public |
| 436 | * |
| 437 | * @return void |
| 438 | */ |
| 439 | public function orderby() { |
| 440 | switch ( $this->args['orderby'] ) { |
| 441 | case 'amount': |
| 442 | $this->__set( 'orderby', 'meta_value_num' ); |
| 443 | $this->__set( 'meta_key', '_give_payment_total' ); |
| 444 | break; |
| 445 | |
| 446 | case 'status': |
| 447 | $this->__set( 'orderby', 'post_status' ); |
| 448 | break; |
| 449 | |
| 450 | case 'donation_form': |
| 451 | $this->__set( 'orderby', 'meta_value' ); |
| 452 | $this->__set( 'meta_key', '_give_payment_form_title' ); |
| 453 | break; |
| 454 | |
| 455 | default: |
| 456 | $this->__set( 'orderby', $this->args['orderby'] ); |
| 457 | break; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Custom orderby. |
| 463 | * Note: currently custom sorting is only used for donation listing page. |
| 464 | * |
| 465 | * @since 1.8 |
| 466 | * @access public |
| 467 | * |
| 468 | * @param string $order |
| 469 | * @param WP_Query $query |
| 470 | * |
| 471 | * @return mixed |
| 472 | */ |
| 473 | public function custom_orderby( $order, $query ) { |
| 474 | |
| 475 | if ( ! empty( $query->query['post_type'] ) ) { |
| 476 | $post_types = is_array( $query->query['post_type'] ) ? $query->query['post_type'] : array( $query->query['post_type'] ); |
| 477 | |
| 478 | if ( ! in_array( 'give_payment', $post_types ) || is_array( $query->query['orderby'] ) ) { |
| 479 | return $order; |
| 480 | } |
| 481 | |
| 482 | global $wpdb; |
| 483 | switch ( $query->query['orderby'] ) { |
| 484 | case 'post_status': |
| 485 | $order = $wpdb->posts . '.post_status ' . strtoupper( $query->query['order'] ); |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | return $order; |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Specific User |
| 495 | * |
| 496 | * @since 1.0 |
| 497 | * @access public |
| 498 | * |
| 499 | * @return void |
| 500 | */ |
| 501 | public function user() { |
| 502 | if ( is_null( $this->args['user'] ) ) { |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | if ( is_numeric( $this->args['user'] ) ) { |
| 507 | $user_key = '_give_payment_donor_id'; |
| 508 | } else { |
| 509 | $user_key = '_give_payment_donor_email'; |
| 510 | } |
| 511 | |
| 512 | $this->__set( |
| 513 | 'meta_query', array( |
| 514 | 'key' => $user_key, |
| 515 | 'value' => $this->args['user'], |
| 516 | ) |
| 517 | ); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Specific donor id |
| 522 | * |
| 523 | * @access public |
| 524 | * @since 1.8.9 |
| 525 | * @return void |
| 526 | */ |
| 527 | public function donor() { |
| 528 | if ( is_null( $this->args['donor'] ) || ! is_numeric( $this->args['donor'] ) ) { |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | $donor_meta_type = Give()->donor_meta->meta_type; |
| 533 | |
| 534 | $this->__set( 'meta_query', array( |
| 535 | 'key' => "_give_payment_{$donor_meta_type}_id", |
| 536 | 'value' => (int) $this->args['donor'], |
| 537 | ) ); |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Search |
| 542 | * |
| 543 | * @since 1.0 |
| 544 | * @access public |
| 545 | * |
| 546 | * @return void |
| 547 | */ |
| 548 | public function search() { |
| 549 | |
| 550 | if ( ! isset( $this->args['s'] ) ) { |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | $search = trim( $this->args['s'] ); |
| 555 | |
| 556 | if ( empty( $search ) ) { |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | $is_email = is_email( $search ) || strpos( $search, '@' ) !== false; |
| 561 | $is_user = strpos( $search, strtolower( 'user:' ) ) !== false; |
| 562 | |
| 563 | if ( ! empty( $this->args['search_in_notes'] ) ) { |
| 564 | |
| 565 | $notes = give_get_payment_notes( 0, $search ); |
| 566 | |
| 567 | if ( ! empty( $notes ) ) { |
| 568 | |
| 569 | $payment_ids = wp_list_pluck( (array) $notes, 'comment_post_ID' ); |
| 570 | |
| 571 | $this->__set( 'post__in', $payment_ids ); |
| 572 | } |
| 573 | |
| 574 | $this->__unset( 's' ); |
| 575 | |
| 576 | } elseif ( $is_email || strlen( $search ) == 32 ) { |
| 577 | |
| 578 | $key = $is_email ? '_give_payment_donor_email' : '_give_payment_purchase_key'; |
| 579 | $search_meta = array( |
| 580 | 'key' => $key, |
| 581 | 'value' => $search, |
| 582 | 'compare' => 'LIKE', |
| 583 | ); |
| 584 | |
| 585 | $this->__set( 'meta_query', $search_meta ); |
| 586 | $this->__unset( 's' ); |
| 587 | |
| 588 | } elseif ( $is_user ) { |
| 589 | |
| 590 | $search_meta = array( |
| 591 | 'key' => '_give_payment_donor_id', |
| 592 | 'value' => trim( str_replace( 'user:', '', strtolower( $search ) ) ), |
| 593 | ); |
| 594 | |
| 595 | $this->__set( 'meta_query', $search_meta ); |
| 596 | |
| 597 | if ( give_get_option( 'enable_sequential' ) ) { |
| 598 | |
| 599 | $search_meta = array( |
| 600 | 'key' => '_give_payment_number', |
| 601 | 'value' => $search, |
| 602 | 'compare' => 'LIKE', |
| 603 | ); |
| 604 | |
| 605 | $this->__set( 'meta_query', $search_meta ); |
| 606 | |
| 607 | $this->args['meta_query']['relation'] = 'OR'; |
| 608 | |
| 609 | } |
| 610 | |
| 611 | $this->__unset( 's' ); |
| 612 | |
| 613 | } elseif ( |
| 614 | give_get_option( 'enable_sequential' ) && |
| 615 | ( |
| 616 | false !== strpos( $search, give_get_option( 'sequential_prefix' ) ) || |
| 617 | false !== strpos( $search, give_get_option( 'sequential_postfix' ) ) |
| 618 | ) |
| 619 | ) { |
| 620 | |
| 621 | $search_meta = array( |
| 622 | 'key' => '_give_payment_number', |
| 623 | 'value' => $search, |
| 624 | 'compare' => 'LIKE', |
| 625 | ); |
| 626 | |
| 627 | $this->__set( 'meta_query', $search_meta ); |
| 628 | $this->__unset( 's' ); |
| 629 | |
| 630 | } elseif ( is_numeric( $search ) ) { |
| 631 | |
| 632 | $post = get_post( $search ); |
| 633 | |
| 634 | if ( is_object( $post ) && $post->post_type == 'give_payment' ) { |
| 635 | |
| 636 | $arr = array(); |
| 637 | $arr[] = $search; |
| 638 | $this->__set( 'post__in', $arr ); |
| 639 | $this->__unset( 's' ); |
| 640 | } |
| 641 | } elseif ( '#' == substr( $search, 0, 1 ) ) { |
| 642 | |
| 643 | $search = str_replace( '#:', '', $search ); |
| 644 | $search = str_replace( '#', '', $search ); |
| 645 | $this->__set( 'give_forms', $search ); |
| 646 | $this->__unset( 's' ); |
| 647 | |
| 648 | } else { |
| 649 | $this->__set( 's', $search ); |
| 650 | |
| 651 | } |
| 652 | |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Payment Mode |
| 657 | * |
| 658 | * @since 1.0 |
| 659 | * @access public |
| 660 | * |
| 661 | * @return void |
| 662 | */ |
| 663 | public function mode() { |
| 664 | if ( empty( $this->args['mode'] ) || $this->args['mode'] == 'all' ) { |
| 665 | $this->__unset( 'mode' ); |
| 666 | |
| 667 | return; |
| 668 | } |
| 669 | |
| 670 | $this->__set( |
| 671 | 'meta_query', array( |
| 672 | 'key' => '_give_payment_mode', |
| 673 | 'value' => $this->args['mode'], |
| 674 | ) |
| 675 | ); |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * Children |
| 680 | * |
| 681 | * @since 1.0 |
| 682 | * @access public |
| 683 | * |
| 684 | * @return void |
| 685 | */ |
| 686 | public function children() { |
| 687 | if ( empty( $this->args['children'] ) ) { |
| 688 | $this->__set( 'post_parent', 0 ); |
| 689 | } |
| 690 | $this->__unset( 'children' ); |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Specific Give Form |
| 695 | * |
| 696 | * @since 1.0 |
| 697 | * @access public |
| 698 | * |
| 699 | * @return void |
| 700 | */ |
| 701 | public function give_forms() { |
| 702 | |
| 703 | if ( empty( $this->args['give_forms'] ) ) { |
| 704 | return; |
| 705 | } |
| 706 | |
| 707 | $compare = '='; |
| 708 | |
| 709 | if ( is_array( $this->args['give_forms'] ) ) { |
| 710 | $compare = 'IN'; |
| 711 | } |
| 712 | |
| 713 | $this->__set( |
| 714 | 'meta_query', |
| 715 | array( |
| 716 | 'key' => '_give_payment_form_id', |
| 717 | 'value' => $this->args['give_forms'], |
| 718 | 'compare' => $compare, |
| 719 | ) |
| 720 | ); |
| 721 | |
| 722 | $this->__unset( 'give_forms' ); |
| 723 | |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * Specific Gateway |
| 728 | * |
| 729 | * @since 1.8.17 |
| 730 | * @access public |
| 731 | * |
| 732 | * @return void |
| 733 | */ |
| 734 | public function gateway_filter() { |
| 735 | |
| 736 | if ( empty( $this->args['gateway'] ) ) { |
| 737 | return; |
| 738 | } |
| 739 | |
| 740 | $compare = '='; |
| 741 | |
| 742 | if ( is_array( $this->args['gateway'] ) ) { |
| 743 | $compare = 'IN'; |
| 744 | } |
| 745 | |
| 746 | $this->__set( |
| 747 | 'meta_query', array( |
| 748 | array( |
| 749 | 'key' => '_give_payment_gateway', |
| 750 | 'value' => $this->args['gateway'], |
| 751 | 'compare' => $compare, |
| 752 | ), |
| 753 | ) |
| 754 | ); |
| 755 | |
| 756 | $this->__unset( 'gateway' ); |
| 757 | |
| 758 | } |
| 759 | |
| 760 | |
| 761 | /** |
| 762 | * Get sql query |
| 763 | * |
| 764 | * Note: Internal purpose only. We are developing on this fn. |
| 765 | * |
| 766 | * @since 1.8.18 |
| 767 | * @access public |
| 768 | * @global $wpdb |
| 769 | * |
| 770 | * @return string |
| 771 | */ |
| 772 | private function get_sql() { |
| 773 | global $wpdb; |
| 774 | |
| 775 | $where = "WHERE {$wpdb->posts}.post_type = 'give_payment'"; |
| 776 | $where .= " AND {$wpdb->posts}.post_status IN ('" . implode( "','", $this->args['post_status'] ) . "')"; |
| 777 | |
| 778 | if( is_numeric( $this->args['post_parent'] ) ) { |
| 779 | $where .= " AND {$wpdb->posts}.post_parent={$this->args['post_parent']}"; |
| 780 | } |
| 781 | |
| 782 | // Set orderby. |
| 783 | $orderby = "ORDER BY {$wpdb->posts}.{$this->args['orderby']}"; |
| 784 | $group_by = ''; |
| 785 | |
| 786 | // Set group by. |
| 787 | if ( ! empty( $this->args['group_by'] ) ) { |
| 788 | $group_by = "GROUP BY {$wpdb->posts}.{$this->args['group_by']}"; |
| 789 | } |
| 790 | |
| 791 | // Set offset. |
| 792 | if ( |
| 793 | empty( $this->args['nopaging'] ) && |
| 794 | empty( $this->args['offset'] ) && |
| 795 | ( ! empty( $this->args['page'] ) && 0 < $this->args['page'] ) |
| 796 | ) { |
| 797 | $this->args['offset'] = $this->args['posts_per_page'] * ( $this->args['page'] - 1 ); |
| 798 | } |
| 799 | |
| 800 | // Set fields. |
| 801 | $fields = "{$wpdb->posts}.*"; |
| 802 | if ( ! empty( $this->args['fields'] ) && 'all' !== $this->args['fields'] ) { |
| 803 | if ( is_string( $this->args['fields'] ) ) { |
| 804 | $fields = "{$wpdb->posts}.{$this->args['fields']}"; |
| 805 | } elseif ( is_array( $this->args['fields'] ) ) { |
| 806 | $fields = "{$wpdb->posts}." . implode( " , {$wpdb->posts}.", $this->args['fields'] ); |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | // Set count. |
| 811 | if ( ! empty( $this->args['count'] ) ) { |
| 812 | $fields = "COUNT({$wpdb->posts}.ID)"; |
| 813 | |
| 814 | if ( ! empty( $this->args['group_by'] ) ) { |
| 815 | $fields = "{$wpdb->posts}.{$this->args['group_by']}, {$fields}"; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | // Date query. |
| 820 | if ( ! empty( $this->args['date_query'] ) ) { |
| 821 | $date_query_obj = new WP_Date_Query( $this->args['date_query'] ); |
| 822 | $where .= str_replace( |
| 823 | array( |
| 824 | "\n", |
| 825 | '( (', |
| 826 | '))', |
| 827 | ), |
| 828 | array( |
| 829 | '', |
| 830 | '( (', |
| 831 | ') )', |
| 832 | ), |
| 833 | $date_query_obj->get_sql() |
| 834 | ); |
| 835 | } |
| 836 | |
| 837 | // Meta query. |
| 838 | if ( ! empty( $this->args['meta_query'] ) ) { |
| 839 | $meta_query_obj = new WP_Meta_Query( $this->args['meta_query'] ); |
| 840 | $where = implode( ' ', $meta_query_obj->get_sql( 'post', $wpdb->posts, 'ID' ) ) . " {$where}"; |
| 841 | $where = Give()->payment_meta->__rename_meta_table_name( $where, 'posts_where' ); |
| 842 | } |
| 843 | |
| 844 | // Set sql query. |
| 845 | $sql = $wpdb->prepare( |
| 846 | "SELECT {$fields} FROM {$wpdb->posts} LIMIT %d,%d;", |
| 847 | absint( $this->args['offset'] ), |
| 848 | ( empty( $this->args['nopaging'] ) ? absint( $this->args['posts_per_page'] ) : 99999999999 ) |
| 849 | ); |
| 850 | |
| 851 | // $where, $orderby and order already prepared query they can generate notice if you re prepare them in above. |
| 852 | // WordPress consider LIKE condition as placeholder if start with s,f, or d. |
| 853 | $sql = str_replace( 'LIMIT', "{$where} {$group_by} {$orderby} {$this->args['order']} LIMIT", $sql ); |
| 854 | |
| 855 | return $sql; |
| 856 | } |
| 857 | |
| 858 | } |
| 859 |