PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.2.1
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.2.1
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 All 66 releases
better-payment / includes / Admin / DB.php

DB.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 2.2.1, at includes/Admin/DB.php

760 lines 29.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Better_Payment\Lite\Admin;
4
5 use Better_Payment\Lite\Classes\Helper;
6
7 /**
8 * Exit if accessed directly
9 */
10 if (!defined('ABSPATH')) {
11 exit;
12 }
13
14 /**
15 * This class responsible for database work
16 * using wordpress functionality
17 * get_option and update_option.
18 *
19 * @since 0.0.1
20 */
21 class DB {
22
23 public static function get_table_name() {
24 global $wpdb;
25
26 $table = "{$wpdb->prefix}better_payment";
27 return $table;
28 }
29 /**
30 * Get all default settings value.
31 *
32 * @param string $name
33 * @return array
34 * @since 0.0.1
35 */
36 public static function default_settings() {
37 return apply_filters('better_payment_option_default_settings', array(
38 'better_payment_settings_general_general_paypal' => 'yes',
39 'better_payment_settings_general_general_stripe' => 'yes',
40 'better_payment_settings_general_general_paystack' => '',
41 'better_payment_settings_general_general_user_dashboard' => 'yes',
42 'better_payment_settings_general_general_fundraising_campaign' => 'yes',
43 'better_payment_settings_general_general_email' => 'yes',
44 'better_payment_settings_general_general_currency' => 'USD',
45
46 'better_payment_settings_general_email_to' => '',
47 'better_payment_settings_general_email_subject' => '',
48 'better_payment_settings_general_email_message_admin' => '',
49 'better_payment_settings_general_email_from_email' => '',
50 'better_payment_settings_general_email_from_name' => '',
51 'better_payment_settings_general_email_reply_to' => '',
52 'better_payment_settings_general_email_cc' => '',
53 'better_payment_settings_general_email_bcc' => '',
54 'better_payment_settings_general_email_send_as' => 'html',
55
56 'better_payment_settings_general_email_subject_customer' => '',
57 'better_payment_settings_general_email_message_customer' => '',
58 'better_payment_settings_general_email_from_email_customer' => '',
59 'better_payment_settings_general_email_from_name_customer' => '',
60 'better_payment_settings_general_email_reply_to_customer' => '',
61 'better_payment_settings_general_email_cc_customer' => '',
62 'better_payment_settings_general_email_bcc_customer' => '',
63 'better_payment_settings_general_email_send_as_customer' => 'html',
64
65 'better_payment_settings_payment_paypal_live_mode' => 'no',
66 'better_payment_settings_payment_paypal_email' => '',
67 'better_payment_settings_opt_in' => '',
68 'better_payment_settings_payment_paypal_live_client_id' => '',
69 'better_payment_settings_payment_paypal_test_client_id' => '',
70 'better_payment_settings_payment_paypal_live_secret' => '',
71 'better_payment_settings_payment_paypal_test_secret' => '',
72 'better_payment_settings_payment_stripe_live_mode' => 'no',
73 'better_payment_settings_payment_stripe_live_public' => '',
74 'better_payment_settings_payment_stripe_live_secret' => '',
75 'better_payment_settings_payment_stripe_test_public' => '',
76 'better_payment_settings_payment_stripe_test_secret' => '',
77 'better_payment_settings_payment_paystack_live_mode' => 'no',
78 'better_payment_settings_payment_paystack_live_public' => '',
79 'better_payment_settings_payment_paystack_live_secret' => '',
80 'better_payment_settings_payment_paystack_test_public' => '',
81 'better_payment_settings_payment_paystack_test_secret' => '',
82 ));
83 }
84 /**
85 * Get all settings value from options table.
86 *
87 * @param string $name
88 * @return array
89 * @since 0.0.1
90 */
91 public static function get_settings($name = '')
92 {
93 $settings = get_option('better_payment_settings', true);
94 $default = self::default_settings();
95
96 $settings = wp_parse_args($settings, $default);
97
98 if (!empty($name) && isset($settings[$name])) {
99 return $settings[$name];
100 }
101
102 if (!empty($name) && !isset($settings[$name]) && isset($default[$name])) {
103 return $default[$name];
104 }
105
106 if (!empty($name) && !isset($settings[$name]) && !isset($default[$name])) {
107 return '';
108 }
109
110 return is_array($settings) ? $settings : [];
111 }
112 /**
113 * Update settings
114 * @param array $value
115 * @return boolean
116 * @since 0.0.1
117 */
118 public static function update_settings($value, $key = '')
119 {
120 if (!empty($key)) {
121 return update_option($key, $value);
122 }
123 return update_option('better_payment_settings', $value);
124 }
125
126 /**
127 * Get all transactions from better payment table.
128 *
129 * @param string $args
130 * @return array
131 * @since 0.0.1
132 */
133 public static function get_transactions($args = [], $count_only=0, $version = 'v1', $fetchNull = 0, $transaction_type = '')
134 {
135 global $wpdb;
136 $items = '';
137
138 $defaults = array(
139 'search_text' => '',
140 'payment_date_from' => '',
141 'payment_date_to' => '',
142 'order_by' => 'payment_date',
143 'order' => 'DESC',
144 'currency' => 'all',
145 'source' => '',
146 'paged' => 1,
147 'per_page' => 20,
148 'offset' => 0,
149 'status' => 'all',
150 );
151
152 $allowed_order_by = array(
153 'id',
154 'payment_date',
155 'email',
156 'amount'
157 );
158
159 $allowed_order = array(
160 'DESC',
161 'ASC'
162 );
163
164 $better_payment_db_obj = new DB();
165 $better_payment_helper_obj = new Helper();
166
167 $allowed_statuses = $better_payment_db_obj->allowed_statuses('all', 'v2');
168 $allowed_statuses_types = $better_payment_db_obj->allowed_statuses_types('v2');
169
170 $args = wp_parse_args($args, $defaults);
171 $table = self::get_table_name();
172
173 $search_text = sanitize_text_field($args['search_text']);
174 $search_text = empty($search_text) ? '' : "%$search_text%";
175
176 $order_by = in_array($args['order_by'], $allowed_order_by) ? sanitize_text_field($args['order_by']) : $defaults['order_by'];
177 $order = in_array($args['order'], $allowed_order) ? sanitize_text_field($args['order']) : $defaults['order'];
178 $offset = $args['offset'] > 0 ? intval($args['offset']) : $defaults['offset'];
179 $per_page = $args['per_page'] > 0 ? intval($args['per_page']) : $defaults['per_page'];
180
181 if($args['status'] == 'null') {
182 $status = sanitize_text_field($args['status']);
183 }else {
184 $allowed_statuses_by_version = ($version == 'v1') ? $allowed_statuses : $allowed_statuses_types;
185 $status = in_array($args['status'], $allowed_statuses_by_version) ? sanitize_text_field($args['status']) : $defaults['status'];
186 }
187
188 $statuses_by_transaction_type = array();
189
190 if( !empty($transaction_type) ){
191 $statuses_by_types = $better_payment_helper_obj->transaction_statuses_with_type_v2();
192 $statuses_by_transaction_type = isset($statuses_by_types[$transaction_type]['statuses']) ? $statuses_by_types[$transaction_type]['statuses'] : $defaults['status'];
193 $status = '';
194 }
195
196 $whereQuery = '';
197
198 $valid_search_text = $search_text != '' && strlen($search_text) >= 2;
199 if($valid_search_text){
200 $whereQuery = $wpdb->prepare(' AND ( email LIKE %s OR form_fields_info LIKE %s OR transaction_id LIKE %s OR amount LIKE %s OR source LIKE %s ) ', $search_text, $search_text ,$search_text, $search_text, $search_text );
201 }
202
203 if( $args['source'] != '' ){
204 $whereQuery .= $wpdb->prepare(" AND source = %s", $args['source']);
205 }
206
207 if( $args['currency'] != 'all' ){
208 $whereQuery .= $wpdb->prepare(" AND currency = %s", $args['currency']);
209 }
210
211 if( $status != 'all' ){
212 if($status == 'null'){
213 $whereQuery .= esc_sql(" AND status is NULL");
214 }else {
215 //version 2
216 if('v1' === $version){
217 $whereQuery .= $wpdb->prepare( " AND status = %s OR status = %s", $status, strtolower($status) );
218 } else {
219
220 if($transaction_type){
221 $statuses_by_type = $statuses_by_transaction_type;
222 }else {
223 $statuses_by_type = $better_payment_db_obj->allowed_statuses($status, 'v2');
224 }
225
226 $statuses = array_map(function($status) {
227 return "'" . esc_sql($status) . "'";
228 }, $statuses_by_type);
229
230 $statuses = implode(',', $statuses);
231
232 $nullDataQuery = $fetchNull ? " OR status IS NULL " : '';
233 $whereQuery .= " AND ( status IN ( $statuses ) " . esc_sql($nullDataQuery) . " ) ";
234 }
235 }
236 }
237
238 if( $args['payment_date_from'] != '' && $args['payment_date_to'] != ''){
239 $whereQuery .= $wpdb->prepare(" AND payment_date BETWEEN %s AND %s", $args['payment_date_from'], $args['payment_date_to']);
240 }
241
242 if($count_only === 1){
243 if($valid_search_text){
244 $items = $wpdb->get_var(
245 "SELECT count(id) FROM $table WHERE 1
246 $whereQuery
247 ORDER BY $order_by $order
248 "
249 );
250 }else {
251 $items = $wpdb->get_var(
252 $wpdb->prepare(
253 "SELECT count(id) FROM $table WHERE 1
254 $whereQuery
255 ORDER BY $order_by $order
256 LIMIT %d, %d
257 ",
258 $offset,
259 $per_page
260 )
261 );
262 }
263
264 }else {
265 $items = $wpdb->get_results(
266 $wpdb->prepare(
267 "SELECT * FROM $table WHERE 1
268 $whereQuery
269 ORDER BY $order_by $order
270 LIMIT %d, %d
271 ",
272 $offset,
273 $per_page
274 )
275 );
276
277 }
278
279 return $items;
280 }
281
282 /**
283 * Get the count of total transactions
284 *
285 * @return int
286 * @since 0.0.1
287 */
288 public static function get_transaction_count($args = '', $version = 'v1', $fetchNull = 0, $transaction_type = '')
289 {
290 global $wpdb;
291 $table = self::get_table_name();
292
293 $count = 0;
294
295 if( !empty($transaction_type) ){
296 $count = self::get_transactions($args, 1, $version, $fetchNull, $transaction_type);
297 return $count;
298 }
299
300 if($args !== ''){
301 $count = self::get_transactions($args, 1, $version, $fetchNull);
302 }else {
303 $count = $wpdb->get_var(
304 "SELECT count(id) FROM $table"
305 );
306 }
307
308 return $count;
309 }
310
311 /**
312 * Get transactions analytics
313 *
314 * @return int
315 * @since 0.0.1
316 */
317 public static function get_transactions_analytics($filtered_transactions = array())
318 {
319 global $wpdb;
320 $transaction_analytics = [];
321
322 $table = self::get_table_name();
323 $better_payment_db_obj = new DB();
324
325 $total_transactions = $better_payment_db_obj->get_transactions_amount_by_statuses(array(), '', '', 'count', 1, $filtered_transactions );
326
327 $completed_statuses = $better_payment_db_obj->allowed_statuses('completed', 'v2');
328 $completed_transactions = $better_payment_db_obj->get_transactions_amount_by_statuses( $completed_statuses, '', '', 'count', 0, $filtered_transactions );
329
330 $incomplete_statuses = $better_payment_db_obj->allowed_statuses('incomplete', 'v2');
331 $incomplete_transactions = $better_payment_db_obj->get_transactions_amount_by_statuses( $incomplete_statuses, '', '', 'count', 1, $filtered_transactions );
332
333 $transaction_analytics['total_transactions'] = $total_transactions;
334 $transaction_analytics['completed_transactions'] = $completed_transactions;
335 $transaction_analytics['incomplete_transactions'] = $incomplete_transactions;
336
337 return $transaction_analytics;
338 }
339
340 /**
341 * Get transactions analytics
342 *
343 * @return int
344 * @since 0.0.1
345 */
346 public static function get_transactions_analytics_dashboard($filtered_transactions = array())
347 {
348 global $wpdb;
349 $transaction_analytics = [];
350
351 $table = self::get_table_name();
352 $better_payment_db_obj = new DB();
353
354 $total_transactions_count = 0;
355 $completed_transactions_count = 0;
356 $incomplete_transactions_count = 0;
357 $refunded_transactions_count = 0;
358
359 $total_transactions_amount = 0;
360 $completed_transactions_amount = 0;
361 $incomplete_transactions_amount = 0;
362 $refunded_transactions_amount = 0;
363
364 if ( is_array( $filtered_transactions ) && count( $filtered_transactions ) ) {
365 $total_transactions_count = $better_payment_db_obj->get_transactions_amount_by_statuses(array(), '', '', 'count', 1, $filtered_transactions );
366 $total_transactions_amount = $better_payment_db_obj->get_transactions_amount_by_statuses(array(), '', '', 'amount', 1, $filtered_transactions );
367
368 $completed_statuses = $better_payment_db_obj->allowed_statuses('completed', 'v2');
369 $completed_transactions_count = $better_payment_db_obj->get_transactions_amount_by_statuses( $completed_statuses, '', '', 'count', 0, $filtered_transactions );
370 $completed_transactions_amount = $better_payment_db_obj->get_transactions_amount_by_statuses( $completed_statuses, '', '', 'amount', 0, $filtered_transactions );
371
372 $incomplete_statuses = $better_payment_db_obj->allowed_statuses('incomplete', 'v2');
373 $incomplete_transactions_count = $better_payment_db_obj->get_transactions_amount_by_statuses( $incomplete_statuses, '', '', 'count', 1, $filtered_transactions );
374 $incomplete_transactions_amount = $better_payment_db_obj->get_transactions_amount_by_statuses( $incomplete_statuses, '', '', 'amount', 1, $filtered_transactions );
375
376 $refunded_statuses = $better_payment_db_obj->allowed_statuses('refunded', 'v2');
377 $refunded_transactions_count = $better_payment_db_obj->get_transactions_amount_by_statuses( $refunded_statuses, '', '', 'count', 0, $filtered_transactions );
378 $refunded_transactions_amount = $better_payment_db_obj->get_transactions_amount_by_statuses( $refunded_statuses, '', '', 'amount', 0, $filtered_transactions );
379 }
380
381 $transaction_analytics['total_transactions_count'] = $total_transactions_count;
382 $transaction_analytics['completed_transactions_count'] = $completed_transactions_count;
383 $transaction_analytics['incomplete_transactions_count'] = $incomplete_transactions_count;
384 $transaction_analytics['refunded_transactions_count'] = $refunded_transactions_count;
385
386 $transaction_analytics['total_transactions_amount'] = $total_transactions_amount;
387 $transaction_analytics['completed_transactions_amount'] = $completed_transactions_amount;
388 $transaction_analytics['incomplete_transactions_amount'] = $incomplete_transactions_amount;
389 $transaction_analytics['refunded_transactions_amount'] = $refunded_transactions_amount;
390
391 // Recent transactions
392
393 return $transaction_analytics;
394 }
395
396 /**
397 * Get transaction details from better payment table.
398 *
399 * @param string $args
400 * @return object
401 * @since 0.0.1
402 */
403 public static function get_transaction($id)
404 {
405 global $wpdb;
406
407 $table = self::get_table_name();
408 $transaction_id = (int) sanitize_text_field($id);
409
410 $item = $wpdb->get_row(
411 $wpdb->prepare(
412 "SELECT * FROM $table WHERE id = %d",
413 $transaction_id
414 )
415 );
416
417 return $item;
418 }
419
420 /**
421 * Get transaction details from better payment table.
422 *
423 * @param string $args
424 * @return object
425 * @since 0.0.1
426 */
427 public static function get_transactions_by_email($email)
428 {
429 global $wpdb;
430
431 $table = self::get_table_name();
432 $email = sanitize_text_field($email);
433
434 $item = $wpdb->get_results(
435 $wpdb->prepare(
436 "SELECT * FROM $table WHERE email = %s order by payment_date DESC",
437 $email
438 )
439 );
440
441 return $item;
442 }
443
444 /**
445 * Get analytics counts and amounts for a specific user via a single SQL aggregate query.
446 * Returns the same shape as get_transactions_analytics_dashboard() without loading all rows.
447 *
448 * @param string $email
449 * @return array
450 */
451 public static function get_user_analytics_by_email( $email ) {
452 global $wpdb;
453 $table = self::get_table_name();
454 $email = sanitize_text_field( $email );
455 $ffi_like = '%' . $wpdb->esc_like( '"' . $email . '"' ) . '%';
456
457 $row = $wpdb->get_row(
458 $wpdb->prepare(
459 "SELECT
460 COUNT(*) AS total_count,
461 SUM(CAST(amount AS DECIMAL(10,2))) AS total_amount,
462 SUM(CASE WHEN status IN ('paid','Completed','completed','success')
463 THEN 1 ELSE 0 END) AS completed_count,
464 SUM(CASE WHEN status IN ('paid','Completed','completed','success')
465 THEN CAST(amount AS DECIMAL(10,2)) ELSE 0 END) AS completed_amount,
466 SUM(CASE WHEN status IN ('pending','Pending','unpaid','incomplete','Incomplete') OR status IS NULL
467 THEN 1 ELSE 0 END) AS incomplete_count,
468 SUM(CASE WHEN status IN ('pending','Pending','unpaid','incomplete','Incomplete') OR status IS NULL
469 THEN CAST(amount AS DECIMAL(10,2)) ELSE 0 END) AS incomplete_amount,
470 SUM(CASE WHEN status = 'refunded' THEN 1 ELSE 0 END) AS refunded_count,
471 SUM(CASE WHEN status = 'refunded'
472 THEN CAST(amount AS DECIMAL(10,2)) ELSE 0 END) AS refunded_amount
473 FROM $table WHERE ( email = %s OR form_fields_info LIKE %s )",
474 $email,
475 $ffi_like
476 )
477 );
478
479 if ( ! $row ) {
480 $row = (object) array_fill_keys(
481 array( 'total_count', 'total_amount', 'completed_count', 'completed_amount', 'incomplete_count', 'incomplete_amount', 'refunded_count', 'refunded_amount' ),
482 0
483 );
484 }
485
486 return [
487 'total_transactions_count' => (int) $row->total_count,
488 'total_transactions_amount' => (float) $row->total_amount,
489 'completed_transactions_count' => (int) $row->completed_count,
490 'completed_transactions_amount' => (float) $row->completed_amount,
491 'incomplete_transactions_count' => (int) $row->incomplete_count,
492 'incomplete_transactions_amount' => (float) $row->incomplete_amount,
493 'refunded_transactions_count' => (int) $row->refunded_count,
494 'refunded_transactions_amount' => (float) $row->refunded_amount,
495 ];
496 }
497
498 /**
499 * Get paginated transactions for a specific user email.
500 *
501 * @param string $email
502 * @param array $args page, per_page, type ('transactions'|'subscriptions')
503 * @return array { transactions: array, total: int, pages: int }
504 */
505 public static function get_user_transactions_paginated( $email, $args = [] ) {
506 global $wpdb;
507
508 $defaults = [
509 'page' => 1,
510 'per_page' => 10,
511 'type' => 'transactions',
512 ];
513 $args = wp_parse_args( $args, $defaults );
514 $table = self::get_table_name();
515 $email = sanitize_text_field( $email );
516 $page = max( 1, intval( $args['page'] ) );
517 $per_page = max( 1, intval( $args['per_page'] ) );
518 $offset = ( $page - 1 ) * $per_page;
519
520 $type_where = '';
521 if ( 'subscriptions' === $args['type'] ) {
522 $type_where = " AND form_fields_info LIKE '%\"subscription_id\"%'";
523 }
524
525 // Match rows where the email column is set directly OR where the email
526 // appears as a quoted value inside the serialized form_fields_info blob.
527 // The initial payment_create() insert never writes the email column —
528 // only the payment-confirmed callback does — so many rows have the user's
529 // email exclusively inside form_fields_info.
530 $ffi_like = '%' . $wpdb->esc_like( '"' . $email . '"' ) . '%';
531
532 $email_where = 'email = %s OR form_fields_info LIKE %s';
533
534 $total = (int) $wpdb->get_var(
535 $wpdb->prepare(
536 "SELECT COUNT(id) FROM $table WHERE ( $email_where )" . $type_where,
537 $email,
538 $ffi_like
539 )
540 );
541
542 $transactions = $wpdb->get_results(
543 $wpdb->prepare(
544 "SELECT * FROM $table WHERE ( $email_where )" . $type_where . " ORDER BY payment_date DESC LIMIT %d OFFSET %d",
545 $email,
546 $ffi_like,
547 $per_page,
548 $offset
549 )
550 );
551
552 return [
553 'transactions' => $transactions,
554 'total' => $total,
555 'pages' => $total > 0 ? (int) ceil( $total / $per_page ) : 1,
556 'page' => $page,
557 'per_page' => $per_page,
558 ];
559 }
560
561 /**
562 * Delete a transaction
563 *
564 * @param int $id
565 *
566 * @return int|boolean
567 * @since 0.0.1
568 */
569 public static function delete_transaction($id)
570 {
571 global $wpdb;
572 $table = self::get_table_name();
573
574 return $wpdb->delete(
575 $table,
576 ['id' => $id],
577 ['%d']
578 );
579 }
580
581 /**
582 * Allowed transaction statuses
583 *
584 * @since 0.0.1
585 */
586 public function allowed_statuses($type = 'all', $version = 'v1'){
587 $helperObj = new Helper();
588 $statuses = $helperObj->get_statuses_by_transaction_type($type, $version);
589
590 return $statuses;
591 }
592
593 /**
594 * Allowed transaction statuses
595 *
596 * @since 0.0.1
597 */
598 public function allowed_statuses_types($version = 'v1'){
599 $helperObj = new Helper();
600 $statuses = $helperObj->get_transaction_types($version);
601
602 return $statuses;
603 }
604
605 /**
606 * Get transactions amount
607 *
608 * @return array
609 * @since 0.0.1
610 */
611 public function get_transactions_by_statuses($statuses = array(), $payment_date_from = '', $payment_date_to = '', $fetchNull=0) {
612 global $wpdb;
613
614 $table = DB::get_table_name();
615 $allowed_statuses = $this->allowed_statuses('all', 'v2');
616
617 $nullDataQuery = $fetchNull ? " OR status IS NULL " : '';
618
619 $statuses = is_array($statuses) && count($statuses) ? $statuses : $allowed_statuses;
620
621 #ToDo: Consider currency while calculating total amount
622 $statuses = array_map(function($status) use ($allowed_statuses) {
623 if(!in_array($status, $allowed_statuses)) {
624 return false;
625 }
626
627 return "'" . esc_sql($status) . "'";
628 }, $statuses);
629
630 $statuses = implode(',', $statuses);
631
632 if(!empty($payment_date_from) && !empty($payment_date_to)){
633 $amount = $wpdb->get_results(
634 "SELECT amount, payment_date, email FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . ") AND payment_date BETWEEN '" . esc_sql($payment_date_from) . "' AND '" . esc_sql($payment_date_to) . "'"
635 );
636 } else if(!empty($payment_date_from) && empty($payment_date_to)){
637 $amount = $wpdb->get_results(
638 "SELECT amount, payment_date, email FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . ") AND payment_date >= '" . esc_sql($payment_date_from) . "'"
639 );
640 } else {
641 $amount = $wpdb->get_results(
642 "SELECT amount, payment_date, email FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . " )"
643 );
644 }
645
646 return $amount;
647 }
648
649 /**
650 * Get transactions amount
651 *
652 * @return array
653 * @since 0.0.1
654 */
655 public function get_transactions_amount_by_statuses($statuses = array(), $payment_date_from = '', $payment_date_to = '', $count_or_amount = 'amount', $fetchNull=0, $filtered_transactions = array() ) {
656 global $wpdb;
657
658 $table = DB::get_table_name();
659 $allowed_statuses = $this->allowed_statuses('all', 'v2');
660
661 $nullDataQuery = $fetchNull ? " OR status IS NULL " : '';
662
663 $statuses = is_array($statuses) && count($statuses) ? $statuses : $allowed_statuses;
664 $statuses_original = $statuses;
665
666 #ToDo: Consider currency while calculating total amount
667 $statuses = array_map(function($status) use ($allowed_statuses) {
668 if(!in_array($status, $allowed_statuses)) {
669 return false;
670 }
671
672 return "'" . esc_sql($status) . "'";
673 }, $statuses);
674
675 $statuses = implode(',', $statuses);
676
677 if( is_array($filtered_transactions) && count($filtered_transactions) ){
678 $filtered_transactions_by_statuses = array();
679 $filtered_transactions_amount = 0;
680
681 foreach($filtered_transactions as $filtered_transaction) {
682 if( in_array($filtered_transaction->status, $statuses_original) ) {
683 $filtered_transactions_by_statuses[] = $filtered_transaction->id;
684 $filtered_transactions_amount += ! empty( $filtered_transaction->amount ) ? floatval( $filtered_transaction->amount ) : 0;
685 }
686 }
687
688 if($count_or_amount == 'count'){
689 $filtered_transactions_by_statuses_count = count($filtered_transactions_by_statuses);
690 return $filtered_transactions_by_statuses_count;
691 }
692
693 if($count_or_amount == 'amount'){
694 return $filtered_transactions_amount;
695 }
696 }
697
698 if($count_or_amount == 'count') {
699 $count = (int) $wpdb->get_var(
700 "SELECT count(id) FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . " )"
701 );
702
703 return $count;
704 }
705
706 if(!empty($payment_date_from) && !empty($payment_date_to)){
707 $amount = $wpdb->get_var(
708 "SELECT sum(amount) FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . ") AND payment_date BETWEEN '" . esc_sql($payment_date_from) . "' AND '" . esc_sql($payment_date_to) . "'"
709 );
710 }else {
711 $amount = $wpdb->get_var(
712 // #TODO Amount based on filtered transactions
713 "SELECT sum(amount) FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . ")"
714 );
715 }
716
717 return $amount;
718 }
719
720 public static function search_transactions($items, $search_text, $offset, $per_page){
721 $filtered_items = array();
722 $search_text = str_replace('%', '', $search_text);
723
724 if(count($items) > 0){
725 $filtered_items = array_filter($items, function($item) use ($search_text){
726 $form_fields_info = maybe_unserialize($item->form_fields_info);
727
728 if(
729 isset($form_fields_info['first_name']) && stripos($form_fields_info['first_name'], $search_text) !== false
730 || isset($form_fields_info['last_name']) && stripos($form_fields_info['last_name'], $search_text) !== false
731 || isset($form_fields_info['primary_first_name']) && stripos($form_fields_info['primary_first_name'], $search_text) !== false
732 || isset($form_fields_info['primary_last_name']) && stripos($form_fields_info['primary_last_name'], $search_text) !== false
733 || isset($form_fields_info['email']) && stripos($form_fields_info['email'], $search_text) !== false
734 || isset($form_fields_info['primary_email']) && stripos($form_fields_info['primary_email'], $search_text) !== false
735 ){
736 return true;
737 }
738
739 });
740
741 }
742
743 return $filtered_items;
744 }
745
746 public static function mark_as_completed($id)
747 {
748 global $wpdb;
749 $table = self::get_table_name();
750
751 $id = (int) sanitize_text_field($id);
752
753 if ($id > 0) {
754 return $wpdb->update($table, ['status' => 'Completed'], ['id' => $id]);
755 }
756
757 return false;
758 }
759 }
760