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