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