give-recurring-cache.php
4 years ago
give-recurring-cron.php
4 years ago
give-recurring-db-subscription-meta.php
4 years ago
give-recurring-helpers.php
4 years ago
give-recurring-subscriber.php
3 years ago
give-subscription.php
3 years ago
give-subscriptions-api.php
4 years ago
give-subscriptions-db.php
3 years ago
give-subscriptions-db.php
725 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Give Recurring Subscription DB |
| 4 | * |
| 5 | * @package Give |
| 6 | * @since 1.0 |
| 7 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 8 | * @copyright Copyright (c) 2016, GiveWP |
| 9 | */ |
| 10 | |
| 11 | // Exit if accessed directly |
| 12 | if (!defined('ABSPATH')) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Class Give_Subscriptions_DB |
| 18 | * |
| 19 | * The Subscriptions DB Class. |
| 20 | * |
| 21 | * @since 2.19.0 - migrated from give-recurring |
| 22 | * @since 1.0 |
| 23 | */ |
| 24 | class Give_Subscriptions_DB extends Give_DB |
| 25 | { |
| 26 | /** |
| 27 | * Get things started. |
| 28 | * |
| 29 | * @access public |
| 30 | * @since 1.0 |
| 31 | */ |
| 32 | public function __construct() |
| 33 | { |
| 34 | global $wpdb; |
| 35 | |
| 36 | $this->table_name = $wpdb->prefix . 'give_subscriptions'; |
| 37 | $this->primary_key = 'id'; |
| 38 | $this->version = '1.1'; |
| 39 | |
| 40 | parent::__construct(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get columns and formats |
| 45 | * |
| 46 | * @access public |
| 47 | * |
| 48 | * @since 2.24.0 add payment_mode column |
| 49 | * @since 1.0 |
| 50 | */ |
| 51 | public function get_columns() |
| 52 | { |
| 53 | return [ |
| 54 | 'id' => '%d', |
| 55 | 'customer_id' => '%d', |
| 56 | 'period' => '%s', |
| 57 | 'frequency' => '%d', |
| 58 | 'initial_amount' => '%s', |
| 59 | 'recurring_amount' => '%s', |
| 60 | 'recurring_fee_amount' => '%F', |
| 61 | 'bill_times' => '%d', |
| 62 | 'transaction_id' => '%s', |
| 63 | 'parent_payment_id' => '%d', |
| 64 | 'product_id' => '%d', |
| 65 | 'created' => '%s', |
| 66 | 'expiration' => '%s', |
| 67 | 'payment_mode' => '%s', |
| 68 | 'status' => '%s', |
| 69 | 'notes' => '%s', |
| 70 | 'profile_id' => '%s', |
| 71 | ]; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /** |
| 76 | * Get default column values |
| 77 | * |
| 78 | * @access public |
| 79 | * @since 1.0 |
| 80 | */ |
| 81 | public function get_column_defaults() |
| 82 | { |
| 83 | return [ |
| 84 | 'customer_id' => 0, |
| 85 | 'period' => '', |
| 86 | 'frequency' => 1, |
| 87 | 'initial_amount' => '', |
| 88 | 'recurring_amount' => '', |
| 89 | 'recurring_fee_amount' => 0, |
| 90 | 'bill_times' => 0, |
| 91 | 'transaction_id' => '', |
| 92 | 'parent_payment_id' => 0, |
| 93 | 'product_id' => 0, |
| 94 | 'created' => date('Y-m-d H:i:s'), |
| 95 | 'expiration' => date('Y-m-d H:i:s'), |
| 96 | 'status' => '', |
| 97 | 'notes' => '', |
| 98 | 'profile_id' => '', |
| 99 | ]; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get subscription by specific data |
| 104 | * |
| 105 | * @since 1.6 |
| 106 | * |
| 107 | * @param int $row_id |
| 108 | * |
| 109 | * @param int $column |
| 110 | * |
| 111 | * @return mixed|object |
| 112 | */ |
| 113 | public function get_by($column, $row_id) |
| 114 | { |
| 115 | $cache_key = Give_Cache::get_key("{$column}_{$row_id}", [$column, $row_id], false); |
| 116 | $subscription = Give_Recurring_Cache::get_subscription($cache_key); |
| 117 | |
| 118 | if (is_null($subscription)) { |
| 119 | $subscription = parent::get_by($column, $row_id); |
| 120 | Give_Recurring_Cache::set_subscription($cache_key, $subscription); |
| 121 | } |
| 122 | |
| 123 | return $subscription; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get subscription |
| 128 | * |
| 129 | * @since 1.6 |
| 130 | * |
| 131 | * @param int $row_id |
| 132 | * |
| 133 | * @return mixed|object |
| 134 | */ |
| 135 | public function get($row_id) |
| 136 | { |
| 137 | $cache_key = Give_Cache::get_key($row_id, '', false); |
| 138 | $subscription = Give_Recurring_Cache::get_subscription($cache_key); |
| 139 | |
| 140 | if (is_null($subscription)) { |
| 141 | $subscription = parent::get($row_id); |
| 142 | Give_Recurring_Cache::set_subscription($cache_key, $subscription); |
| 143 | } |
| 144 | |
| 145 | return $subscription; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Update subscription |
| 150 | * |
| 151 | * @since 1.6 |
| 152 | * |
| 153 | * @param array $data |
| 154 | * @param string $where |
| 155 | * |
| 156 | * @param int $row_id |
| 157 | * |
| 158 | * @return bool |
| 159 | */ |
| 160 | public function update($row_id, $data = [], $where = '') |
| 161 | { |
| 162 | $status = parent::update($row_id, $data, $where); |
| 163 | |
| 164 | Give_Recurring_Cache::get_instance()->flush_on_subscription_update($status, $row_id, $data, $where); |
| 165 | |
| 166 | /** |
| 167 | * Fire the action when subscriptions updated |
| 168 | * |
| 169 | * @since 1.6 |
| 170 | */ |
| 171 | do_action('give_subscription_updated', $status, $row_id, $data, $where); |
| 172 | |
| 173 | |
| 174 | return $status; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Create subscription |
| 179 | * |
| 180 | * @since 1.6 |
| 181 | * |
| 182 | * @param array $data |
| 183 | * |
| 184 | * @return int|mixed |
| 185 | */ |
| 186 | public function create($data) |
| 187 | { |
| 188 | $subcription_id = parent::insert($data, 'subscription'); |
| 189 | |
| 190 | /** |
| 191 | * Fire the action when subscriptions updated |
| 192 | * |
| 193 | * @since 1.6 |
| 194 | */ |
| 195 | do_action('give_subscription_inserted', $subcription_id, $data); |
| 196 | |
| 197 | return $subcription_id; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Delete subscription |
| 202 | * |
| 203 | * @param int $subscription_id |
| 204 | * |
| 205 | * @return bool |
| 206 | */ |
| 207 | public function delete($subscription_id = 0) |
| 208 | { |
| 209 | $subscriptionData = $this->get($subscription_id); |
| 210 | $status = parent::delete($subscription_id); |
| 211 | |
| 212 | /** |
| 213 | * Fire the action when subscriptions updated |
| 214 | * |
| 215 | * @since 1.6 |
| 216 | * @since 1.11.0 Added third parameter |
| 217 | */ |
| 218 | do_action('give_subscription_deleted', $status, $subscription_id, $subscriptionData); |
| 219 | |
| 220 | return $status; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | /** |
| 225 | * Retrieve all subscriptions for a donor. |
| 226 | * |
| 227 | * @since 1.0 |
| 228 | * |
| 229 | * @param array $args |
| 230 | * |
| 231 | * @access public |
| 232 | * @return Give_Subscription[] |
| 233 | */ |
| 234 | public function get_subscriptions($args = []) |
| 235 | { |
| 236 | global $wpdb; |
| 237 | |
| 238 | $defaults = [ |
| 239 | 'number' => 20, |
| 240 | 'offset' => 0, |
| 241 | 'search' => '', |
| 242 | 'form_id' => 0, |
| 243 | 'customer_id' => 0, |
| 244 | 'orderby' => 'id', |
| 245 | 'order' => 'DESC', |
| 246 | ]; |
| 247 | |
| 248 | $args = wp_parse_args($args, $defaults); |
| 249 | |
| 250 | if ($args['number'] < 1) { |
| 251 | $args['number'] = 999999999999; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | $args['orderby'] = !array_key_exists($args['orderby'], $this->get_columns()) ? 'id' : $args['orderby']; |
| 256 | |
| 257 | if ('amount' == $args['orderby']) { |
| 258 | $args['orderby'] = 'amount+0'; |
| 259 | } |
| 260 | |
| 261 | $cache_key = Give_Cache::get_key('give_subscriptions', $args, false); |
| 262 | $subscriptions = Give_Recurring_Cache::get_db_query($cache_key); |
| 263 | |
| 264 | // If no cache key, get subscriptions. |
| 265 | if (is_null($subscriptions)) { |
| 266 | $where = $this->generate_where_clause($args); |
| 267 | |
| 268 | $args['orderby'] = esc_sql($args['orderby']); |
| 269 | $args['order'] = esc_sql($args['order']); |
| 270 | |
| 271 | $subscriptions = $wpdb->get_results( |
| 272 | $wpdb->prepare( |
| 273 | "SELECT * FROM $this->table_name $where ORDER BY {$args['orderby']} {$args['order']} LIMIT %d,%d;", |
| 274 | absint($args['offset']), |
| 275 | absint($args['number']) |
| 276 | ), |
| 277 | OBJECT |
| 278 | ); |
| 279 | |
| 280 | if (!empty($subscriptions)) { |
| 281 | foreach ($subscriptions as $key => $subscription) { |
| 282 | $subscriptions[$key] = new Give_Subscription($subscription); |
| 283 | } |
| 284 | |
| 285 | Give_Recurring_Cache::set_db_query($cache_key, $subscriptions); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return $subscriptions; |
| 290 | } |
| 291 | |
| 292 | |
| 293 | /** |
| 294 | * Count the total number of subscriptions in the database. |
| 295 | * |
| 296 | * @param array $args |
| 297 | * |
| 298 | * @return int|array/null |
| 299 | */ |
| 300 | public function count($args = []) |
| 301 | { |
| 302 | global $wpdb; |
| 303 | |
| 304 | $cache_key = Give_Cache::get_key('give_subscriptions_count', $args, false); |
| 305 | $count = Give_Recurring_Cache::get_db_query($cache_key); |
| 306 | $group_by_args = !empty($args['groupBy']) ? $args['groupBy'] : ''; |
| 307 | $return_count = empty($group_by_args); |
| 308 | |
| 309 | $result = null; |
| 310 | |
| 311 | if (null === $count) { |
| 312 | $groupBy = $this->generate_groupby_clause($group_by_args); |
| 313 | $where = $this->generate_where_clause($args); |
| 314 | $count = $return_count ? "COUNT({$this->primary_key})" : "{$group_by_args}, COUNT({$this->primary_key})"; |
| 315 | $sql = "SELECT {$count} FROM {$this->table_name} {$where} {$groupBy};"; |
| 316 | |
| 317 | $result = $return_count ? $wpdb->get_var($sql) : $wpdb->get_results($sql, ARRAY_A); |
| 318 | |
| 319 | // Simplify result if query for groupBy. |
| 320 | if ($group_by_args && $result) { |
| 321 | $temp = []; |
| 322 | foreach ($result as $data) { |
| 323 | $temp[$data[$group_by_args]] = $data['COUNT(id)']; |
| 324 | } |
| 325 | |
| 326 | $result = $temp; |
| 327 | } |
| 328 | |
| 329 | Give_Recurring_Cache::set_db_query($cache_key, $result); |
| 330 | } |
| 331 | |
| 332 | return $return_count ? absint($result) : $result; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Create the table. |
| 337 | * |
| 338 | * @access public |
| 339 | * @since 1.0 |
| 340 | */ |
| 341 | public function create_table() |
| 342 | { |
| 343 | global $wpdb; |
| 344 | |
| 345 | if ($wpdb->get_var("SHOW TABLES LIKE '$this->table_name'") !== $this->table_name) { |
| 346 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 347 | |
| 348 | $sql = 'CREATE TABLE ' . $this->table_name . ' ( |
| 349 | id bigint(20) NOT NULL AUTO_INCREMENT, |
| 350 | customer_id bigint(20) NOT NULL, |
| 351 | period varchar(20) NOT NULL, |
| 352 | frequency bigint(20) DEFAULT "1" NOT NULL, |
| 353 | initial_amount decimal(18,10) NOT NULL, |
| 354 | recurring_amount decimal(18,10) NOT NULL, |
| 355 | recurring_fee_amount decimal(18,10) NOT NULL, |
| 356 | bill_times bigint(20) NOT NULL, |
| 357 | transaction_id varchar(60) NOT NULL, |
| 358 | parent_payment_id bigint(20) NOT NULL, |
| 359 | product_id bigint(20) NOT NULL, |
| 360 | created datetime NOT NULL, |
| 361 | expiration datetime NOT NULL, |
| 362 | status varchar(20) NOT NULL, |
| 363 | profile_id varchar(60) NOT NULL, |
| 364 | notes longtext NOT NULL, |
| 365 | PRIMARY KEY (id), |
| 366 | KEY profile_id (profile_id), |
| 367 | KEY customer (customer_id), |
| 368 | KEY transaction (transaction_id), |
| 369 | INDEX customer_and_status ( customer_id, status) |
| 370 | ) CHARACTER SET utf8 COLLATE utf8_general_ci;'; |
| 371 | |
| 372 | dbDelta($sql); |
| 373 | |
| 374 | update_option($this->table_name . '_db_version', $this->version); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Get Renewing Subscriptions |
| 380 | * |
| 381 | * @param string $period |
| 382 | * |
| 383 | * @return array|bool|mixed|null|object |
| 384 | */ |
| 385 | public function get_renewing_subscriptions($period = '+1month') |
| 386 | { |
| 387 | global $wpdb; |
| 388 | |
| 389 | $args = [ |
| 390 | 'number' => 99999, |
| 391 | 'status' => 'active', |
| 392 | 'offset' => 0, |
| 393 | 'orderby' => 'id', |
| 394 | 'order' => 'DESC', |
| 395 | 'expiration' => [ |
| 396 | 'start' => date('Y-m-d H:i:s', strtotime($period . ' midnight')), |
| 397 | 'end' => date('Y-m-d H:i:s', strtotime($period . ' midnight') + (DAY_IN_SECONDS - 1)), |
| 398 | ], |
| 399 | ]; |
| 400 | |
| 401 | $cache_key = Give_Cache::get_key('give_renewing_subscriptions', $args, false); |
| 402 | $subscriptions = Give_Recurring_Cache::get_db_query($cache_key); |
| 403 | |
| 404 | if (is_null($subscriptions)) { |
| 405 | $where = $this->generate_where_clause($args); |
| 406 | |
| 407 | $query = $wpdb->prepare( |
| 408 | "SELECT * FROM $this->table_name $where ORDER BY {$args['orderby']} {$args['order']} LIMIT %d,%d;", |
| 409 | absint($args['offset']), |
| 410 | absint($args['number']) |
| 411 | ); |
| 412 | $subscriptions = $wpdb->get_results($query); |
| 413 | Give_Recurring_Cache::set_db_query($cache_key, $subscriptions); |
| 414 | } |
| 415 | |
| 416 | return $subscriptions; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Get expiring subscriptions. |
| 421 | * |
| 422 | * @param string $period |
| 423 | * |
| 424 | * @return array|bool|mixed|null|object |
| 425 | */ |
| 426 | public function get_expiring_subscriptions($period = '+1month') |
| 427 | { |
| 428 | global $wpdb; |
| 429 | |
| 430 | $args = [ |
| 431 | 'number' => 99999, |
| 432 | 'status' => 'active', |
| 433 | 'offset' => 0, |
| 434 | 'orderby' => 'id', |
| 435 | 'order' => 'DESC', |
| 436 | 'expiration' => [ |
| 437 | 'start' => date('Y-m-d H:i:s', strtotime($period . ' midnight')), |
| 438 | 'end' => date('Y-m-d H:i:s', strtotime($period . ' midnight') + (DAY_IN_SECONDS - 1)), |
| 439 | ], |
| 440 | ]; |
| 441 | |
| 442 | $cache_key = Give_Cache::get_key('give_expiring_subscriptions', $args, false); |
| 443 | $subscriptions = Give_Recurring_Cache::get_db_query($cache_key); |
| 444 | |
| 445 | if (is_null($subscriptions)) { |
| 446 | $where = $this->generate_where_clause($args); |
| 447 | $where .= ' AND `bill_times` != 0'; |
| 448 | $where .= ' AND ( SELECT COUNT(ID) FROM ' . $wpdb->prefix . 'posts WHERE `post_parent` = ' . $this->table_name . '.`parent_payment_id` OR `ID` = ' . $this->table_name . '.`parent_payment_id` ) + 1 >= `bill_times`'; |
| 449 | |
| 450 | $query = $wpdb->prepare( |
| 451 | "SELECT * FROM $this->table_name $where ORDER BY {$args['orderby']} {$args['order']} LIMIT %d,%d;", |
| 452 | absint($args['offset']), |
| 453 | absint($args['number']) |
| 454 | ); |
| 455 | $subscriptions = $wpdb->get_results($query); |
| 456 | Give_Recurring_Cache::set_db_query($cache_key, $subscriptions); |
| 457 | } |
| 458 | |
| 459 | return $subscriptions; |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Generate a cache key from args. |
| 464 | * |
| 465 | * @deprecated 1.6 |
| 466 | * |
| 467 | * @param $args |
| 468 | * |
| 469 | * @param $prefix |
| 470 | * |
| 471 | * @return string |
| 472 | */ |
| 473 | protected function generate_cache_key($prefix, $args) |
| 474 | { |
| 475 | return md5($prefix . serialize($args)); |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Build the query args for subscriptions. |
| 480 | * |
| 481 | * @param array $args |
| 482 | * |
| 483 | * @return string The mysql "where" query part. |
| 484 | */ |
| 485 | public function generate_where_clause($args = []) |
| 486 | { |
| 487 | $where = ' WHERE 1=1'; |
| 488 | |
| 489 | // Specific ID. |
| 490 | if (!empty($args['id'])) { |
| 491 | if (is_array($args['id'])) { |
| 492 | $ids = implode(',', array_map('intval', $args['id'])); |
| 493 | } else { |
| 494 | $ids = intval($args['id']); |
| 495 | } |
| 496 | |
| 497 | $where .= " AND `id` IN( {$ids} ) "; |
| 498 | } |
| 499 | |
| 500 | // Specific donation forms. |
| 501 | if (!empty($args['form_id'])) { |
| 502 | if (is_array($args['form_id'])) { |
| 503 | $form_ids = implode(',', array_map('intval', $args['form_id'])); |
| 504 | } else { |
| 505 | $form_ids = intval($args['form_id']); |
| 506 | } |
| 507 | |
| 508 | $where .= " AND `product_id` IN( {$form_ids} ) "; |
| 509 | } |
| 510 | |
| 511 | // Specific parent payments |
| 512 | if (!empty($args['parent_payment_id'])) { |
| 513 | if (is_array($args['parent_payment_id'])) { |
| 514 | $parent_payment_ids = implode(',', array_map('intval', $args['parent_payment_id'])); |
| 515 | } else { |
| 516 | $parent_payment_ids = intval($args['parent_payment_id']); |
| 517 | } |
| 518 | |
| 519 | $where .= " AND `parent_payment_id` IN( {$parent_payment_ids} ) "; |
| 520 | } |
| 521 | |
| 522 | // @TODO: Remove after consolidating terminology. |
| 523 | if (isset($args['donor_id']) && !empty($args['donor_id'])) { |
| 524 | $args['customer_id'] = $args['donor_id']; |
| 525 | } |
| 526 | |
| 527 | // Subscriptions for specific customers/donors |
| 528 | if (!empty($args['customer_id'])) { |
| 529 | if (is_array($args['customer_id'])) { |
| 530 | $customer_ids = implode(',', array_map('intval', $args['customer_id'])); |
| 531 | } else { |
| 532 | $customer_ids = intval($args['customer_id']); |
| 533 | } |
| 534 | |
| 535 | $where .= " AND `customer_id` IN( {$customer_ids} ) "; |
| 536 | } |
| 537 | |
| 538 | // Subscriptions for specific profile IDs |
| 539 | if (!empty($args['profile_id'])) { |
| 540 | if (is_array($args['profile_id'])) { |
| 541 | $profile_ids = implode('\',\'', $args['profile_id']); |
| 542 | } else { |
| 543 | $profile_ids = $args['profile_id']; |
| 544 | } |
| 545 | |
| 546 | $where .= " AND `profile_id` IN( '{$profile_ids}' ) "; |
| 547 | } |
| 548 | |
| 549 | // Specific transaction IDs |
| 550 | if (!empty($args['transaction_id'])) { |
| 551 | if (is_array($args['transaction_id'])) { |
| 552 | $transaction_ids = implode('\',\'', array_map('sanitize_text_field', $args['transaction_id'])); |
| 553 | } else { |
| 554 | $transaction_ids = sanitize_text_field($args['transaction_id']); |
| 555 | } |
| 556 | |
| 557 | $where .= " AND `transaction_id` IN( '{$transaction_ids}' ) "; |
| 558 | } |
| 559 | |
| 560 | // Subscriptions for specific statuses |
| 561 | if (!empty($args['status'])) { |
| 562 | if (is_array($args['status'])) { |
| 563 | $statuses = implode('\',\'', $args['status']); |
| 564 | $where .= " AND `status` IN( '{$statuses}' ) "; |
| 565 | } else { |
| 566 | $statuses = $args['status']; |
| 567 | $where .= " AND `status` = '{$statuses}' "; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | if (!empty($args['date'])) { |
| 572 | $where .= $this->mysql_where_args_date($args); |
| 573 | } |
| 574 | |
| 575 | if (!empty($args['expiration'])) { |
| 576 | $where .= $this->mysql_where_args_expiration($args); |
| 577 | } |
| 578 | |
| 579 | if (!empty($args['search'])) { |
| 580 | $where .= $this->mysql_where_args_search($args); |
| 581 | } |
| 582 | |
| 583 | return apply_filters('give_subscriptions_mysql_query', $where); |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Build the query args for subscriptions. |
| 588 | * |
| 589 | * @param string $groupby |
| 590 | * |
| 591 | * @return string The mysql "where" query part. |
| 592 | */ |
| 593 | private function generate_groupby_clause($groupby = '') |
| 594 | { |
| 595 | if (!$groupby) { |
| 596 | return ''; |
| 597 | } |
| 598 | |
| 599 | return "GROUP BY {$groupby}"; |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * @since 2.26.0 Replace deprecated get_page_by_title() with give_get_page_by_title(). |
| 604 | * |
| 605 | * @param $args |
| 606 | * |
| 607 | * @return string |
| 608 | */ |
| 609 | private function mysql_where_args_search($args) |
| 610 | { |
| 611 | $where = ''; |
| 612 | $donors_db = new Give_DB_Donors(); |
| 613 | if (is_email($args['search'])) { |
| 614 | $customer = new Give_Donor($args['search']); |
| 615 | if ($customer && $customer->id > 0) { |
| 616 | $where = " AND `customer_id` = " . absint($customer->id) . ""; |
| 617 | } |
| 618 | } elseif (false !== strpos($args['search'], 'txn:')) { |
| 619 | $args['search'] = trim(str_replace('txn:', '', $args['search'])); |
| 620 | $where .= " AND `transaction_id` = '" . esc_sql($args['search']) . "'"; |
| 621 | } elseif (false !== strpos($args['search'], 'profile_id:')) { |
| 622 | $args['search'] = trim(str_replace('profile_id:', '', $args['search'])); |
| 623 | $where .= " AND `profile_id` = '" . esc_sql($args['search']) . "'"; |
| 624 | } elseif (false !== strpos($args['search'], 'form_id:')) { |
| 625 | $args['search'] = trim(str_replace('form_id:', '', $args['search'])); |
| 626 | $where .= " AND `product_id` = " . absint($args['search']) . ""; |
| 627 | } elseif (false !== strpos($args['search'], 'product_id:')) { |
| 628 | $args['search'] = trim(str_replace('product_id:', '', $args['search'])); |
| 629 | $where .= " AND `product_id` = " . absint($args['search']) . ""; |
| 630 | } elseif (false !== strpos($args['search'], 'customer_id:')) { |
| 631 | $args['search'] = trim(str_replace('customer_id:', '', $args['search'])); |
| 632 | $where .= " AND `customer_id` = '" . esc_sql($args['search']) . "'"; |
| 633 | } elseif (false !== strpos($args['search'], 'id:') || is_numeric($args['search'])) { |
| 634 | $args['search'] = trim(str_replace('id:', '', $args['search'])); |
| 635 | $where .= " AND `id` = " . absint($args['search']) . ""; |
| 636 | } else { |
| 637 | // See if search matches a product name |
| 638 | $form = give_get_page_by_title(trim($args['search']), OBJECT, 'give_forms'); |
| 639 | if ($form) { |
| 640 | $args['search'] = $form->ID; |
| 641 | $where .= " AND `product_id` = " . absint($args['search']) . ""; |
| 642 | } else { |
| 643 | global $wpdb; |
| 644 | $query = $wpdb->prepare( |
| 645 | " |
| 646 | SELECT id,name FROM {$donors_db->table_name} |
| 647 | WHERE name |
| 648 | LIKE '%s'", |
| 649 | '%' . $args['search'] . '%' |
| 650 | ); |
| 651 | $subscription_donor_id = []; |
| 652 | $donor_ids = $wpdb->get_results($query, ARRAY_A); |
| 653 | if (!empty($donor_ids) && count($donor_ids) > 0) { |
| 654 | foreach ($donor_ids as $key => $val) { |
| 655 | $subscription_donor_id[] = absint($val['id']); |
| 656 | } |
| 657 | } |
| 658 | $subscription_donor_id = implode(',', array_map('intval', $subscription_donor_id)); |
| 659 | $where .= " AND {$this->table_name}.customer_id IN ({$subscription_donor_id})"; |
| 660 | } |
| 661 | }// End if(). |
| 662 | return $where; |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * @param $args |
| 667 | * |
| 668 | * @return string |
| 669 | */ |
| 670 | private function mysql_where_args_date($args) |
| 671 | { |
| 672 | $where = ''; |
| 673 | if (is_array($args['date'])) { |
| 674 | if (!empty($args['date']['start'])) { |
| 675 | $start = date('Y-m-d H:i:s', strtotime($args['date']['start'])); |
| 676 | $where .= " AND `expiration` >= '{$start}'"; |
| 677 | } |
| 678 | if (!empty($args['date']['end'])) { |
| 679 | $end = date('Y-m-d H:i:s', strtotime($args['date']['end'])); |
| 680 | $where .= " AND `expiration` <= '{$end}'"; |
| 681 | } |
| 682 | } else { |
| 683 | $year = date('Y', strtotime($args['date'])); |
| 684 | $month = date('m', strtotime($args['date'])); |
| 685 | $day = date('d', strtotime($args['date'])); |
| 686 | $where .= " AND $year = YEAR ( created ) AND $month = MONTH ( created ) AND $day = DAY ( created )"; |
| 687 | } |
| 688 | |
| 689 | return $where; |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * @param $args |
| 694 | * |
| 695 | * @return string |
| 696 | */ |
| 697 | private function mysql_where_args_expiration($args) |
| 698 | { |
| 699 | $where = ''; |
| 700 | |
| 701 | if (is_array($args['expiration'])) { |
| 702 | if (!empty($args['expiration']['start'])) { |
| 703 | $start = date('Y-m-d H:i:s', strtotime($args['expiration']['start'])); |
| 704 | |
| 705 | $where .= " AND `expiration` >= '{$start}'"; |
| 706 | } |
| 707 | |
| 708 | if (!empty($args['expiration']['end'])) { |
| 709 | $end = date('Y-m-d H:i:s', strtotime($args['expiration']['end'])); |
| 710 | |
| 711 | $where .= " AND `expiration` <= '{$end}'"; |
| 712 | } |
| 713 | } else { |
| 714 | $year = date('Y', strtotime($args['expiration'])); |
| 715 | $month = date('m', strtotime($args['expiration'])); |
| 716 | $day = date('d', strtotime($args['expiration'])); |
| 717 | |
| 718 | $where .= " AND $year = YEAR ( expiration ) AND $month = MONTH ( expiration ) AND $day = DAY ( expiration )"; |
| 719 | } |
| 720 | |
| 721 | return $where; |
| 722 | } |
| 723 | |
| 724 | } |
| 725 |