PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 1.0.4
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v1.0.4
2.4.1 2.4.0 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 All 68 releases
better-payment / includes / Admin / DB.php

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

531 lines 20.2 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' => esc_html__('yes', 'better-payment'),
39 'better_payment_settings_general_general_stripe' => esc_html__('yes', 'better-payment'),
40 'better_payment_settings_general_general_paystack' => esc_html__('', 'better-payment'),
41 'better_payment_settings_general_general_email' => esc_html__('yes', 'better-payment'),
42 'better_payment_settings_general_general_currency' => esc_html__('USD', 'better-payment'),
43
44 'better_payment_settings_general_email_to' => esc_html__('', 'better-payment'),
45 'better_payment_settings_general_email_subject' => esc_html__('', 'better-payment'),
46 'better_payment_settings_general_email_message_admin' => esc_html__('', 'better-payment'),
47 'better_payment_settings_general_email_from_email' => esc_html__('', 'better-payment'),
48 'better_payment_settings_general_email_from_name' => esc_html__('', 'better-payment'),
49 'better_payment_settings_general_email_reply_to' => esc_html__('', 'better-payment'),
50 'better_payment_settings_general_email_cc' => esc_html__('', 'better-payment'),
51 'better_payment_settings_general_email_bcc' => esc_html__('', 'better-payment'),
52 'better_payment_settings_general_email_send_as' => esc_html__('html', 'better-payment'),
53
54 'better_payment_settings_general_email_subject_customer' => esc_html__('', 'better-payment'),
55 'better_payment_settings_general_email_message_customer' => esc_html__('', 'better-payment'),
56 'better_payment_settings_general_email_from_email_customer' => esc_html__('', 'better-payment'),
57 'better_payment_settings_general_email_from_name_customer' => esc_html__('', 'better-payment'),
58 'better_payment_settings_general_email_reply_to_customer' => esc_html__('', 'better-payment'),
59 'better_payment_settings_general_email_cc_customer' => esc_html__('', 'better-payment'),
60 'better_payment_settings_general_email_bcc_customer' => esc_html__('', 'better-payment'),
61 'better_payment_settings_general_email_send_as_customer' => esc_html__('html', 'better-payment'),
62
63 'better_payment_settings_payment_paypal_live_mode' => esc_html__('no', 'better-payment'),
64 'better_payment_settings_payment_paypal_email' => esc_html__('', 'better-payment'),
65 'better_payment_settings_opt_in' => esc_html__('', 'better-payment'),
66 'better_payment_settings_payment_paypal_live_client_id' => esc_html__('', 'better-payment'),
67 'better_payment_settings_payment_paypal_test_client_id' => esc_html__('', 'better-payment'),
68 'better_payment_settings_payment_paypal_live_secret' => esc_html__('', 'better-payment'),
69 'better_payment_settings_payment_paypal_test_secret' => esc_html__('', 'better-payment'),
70 'better_payment_settings_payment_stripe_live_mode' => esc_html__('no', 'better-payment'),
71 'better_payment_settings_payment_stripe_live_public' => esc_html__('', 'better-payment'),
72 'better_payment_settings_payment_stripe_live_secret' => esc_html__('', 'better-payment'),
73 'better_payment_settings_payment_stripe_test_public' => esc_html__('', 'better-payment'),
74 'better_payment_settings_payment_stripe_test_secret' => esc_html__('', 'better-payment'),
75 'better_payment_settings_payment_paystack_live_mode' => esc_html__('no', 'better-payment'),
76 'better_payment_settings_payment_paystack_live_public' => esc_html__('', 'better-payment'),
77 'better_payment_settings_payment_paystack_live_secret' => esc_html__('', 'better-payment'),
78 'better_payment_settings_payment_paystack_test_public' => esc_html__('', 'better-payment'),
79 'better_payment_settings_payment_paystack_test_secret' => esc_html__('', 'better-payment'),
80 ));
81 }
82 /**
83 * Get all settings value from options table.
84 *
85 * @param string $name
86 * @return array
87 * @since 0.0.1
88 */
89 public static function get_settings($name = '')
90 {
91 $settings = get_option('better_payment_settings', true);
92 $default = self::default_settings();
93 if (!empty($name) && isset($settings[$name])) {
94 return $settings[$name];
95 }
96
97 if (!empty($name) && !isset($settings[$name]) && isset($default[$name])) {
98 return $default[$name];
99 }
100
101 if (!empty($name) && !isset($settings[$name]) && !isset($default[$name])) {
102 return '';
103 }
104
105 return is_array($settings) ? $settings : [];
106 }
107 /**
108 * Update settings
109 * @param array $value
110 * @return boolean
111 * @since 0.0.1
112 */
113 public static function update_settings($value, $key = '')
114 {
115 if (!empty($key)) {
116 return update_option($key, $value);
117 }
118 return update_option('better_payment_settings', $value);
119 }
120
121 /**
122 * Get all transactions from better payment table.
123 *
124 * @param string $args
125 * @return array
126 * @since 0.0.1
127 */
128 public static function get_transactions($args = [], $count_only=0, $version = 'v1', $fetchNull = 0, $transaction_type = '')
129 {
130 global $wpdb;
131 $items = '';
132
133 $defaults = array(
134 'search_text' => '',
135 'payment_date_from' => '',
136 'payment_date_to' => '',
137 'order_by' => 'id',
138 'order' => 'DESC',
139 'source' => '',
140 'paged' => 1,
141 'per_page' => 20,
142 'offset' => 0,
143 'status' => 'all',
144 );
145
146 $allowed_order_by = array(
147 'id',
148 'payment_date',
149 'email',
150 'amount'
151 );
152
153 $allowed_order = array(
154 'DESC',
155 'ASC'
156 );
157
158 $better_payment_db_obj = new DB();
159 $better_payment_helper_obj = new Helper();
160
161 $allowed_statuses = $better_payment_db_obj->allowed_statuses('all', 'v2');
162 $allowed_statuses_types = $better_payment_db_obj->allowed_statuses_types('v2');
163
164 $args = wp_parse_args($args, $defaults);
165 $table = self::get_table_name();
166
167 $search_text = sanitize_text_field($args['search_text']);
168 $search_text = empty($search_text) ? '' : "%$search_text%";
169
170 $order_by = in_array($args['order_by'], $allowed_order_by) ? sanitize_text_field($args['order_by']) : $defaults['order_by'];
171 $order = in_array($args['order'], $allowed_order) ? sanitize_text_field($args['order']) : $defaults['order'];
172 $offset = $args['offset'] > 0 ? intval($args['offset']) : $defaults['offset'];
173 $per_page = $args['per_page'] > 0 ? intval($args['per_page']) : $defaults['per_page'];
174
175 if($args['status'] == 'null') {
176 $status = sanitize_text_field($args['status']);
177 }else {
178 $allowed_statuses_by_version = ($version == 'v1') ? $allowed_statuses : $allowed_statuses_types;
179 $status = in_array($args['status'], $allowed_statuses_by_version) ? sanitize_text_field($args['status']) : $defaults['status'];
180 }
181
182 $statuses_by_transaction_type = array();
183
184 if( !empty($transaction_type) ){
185 $statuses_by_types = $better_payment_helper_obj->transaction_statuses_with_type_v2();
186 $statuses_by_transaction_type = isset($statuses_by_types[$transaction_type]['statuses']) ? $statuses_by_types[$transaction_type]['statuses'] : $defaults['status'];
187 $status = '';
188 }
189
190 $whereQuery = '';
191
192 $valid_search_text = $search_text != '' && strlen($search_text) >= 2;
193 if($valid_search_text){
194 $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 );
195 }
196
197 if( $args['source'] != '' ){
198 $whereQuery .= $wpdb->prepare(" AND source = %s", $args['source']);
199 }
200
201 if( $status != 'all' ){
202 if($status == 'null'){
203 $whereQuery .= esc_sql(" AND status is NULL");
204 }else {
205 //version 2
206 if('v1' === $version){
207 $whereQuery .= $wpdb->prepare( " AND status = %s OR status = %s", $status, strtolower($status) );
208 } else {
209
210 if($transaction_type){
211 $statuses_by_type = $statuses_by_transaction_type;
212 }else {
213 $statuses_by_type = $better_payment_db_obj->allowed_statuses($status, 'v2');
214 }
215
216 $statuses = array_map(function($status) {
217 return "'" . esc_sql($status) . "'";
218 }, $statuses_by_type);
219
220 $statuses = implode(',', $statuses);
221
222 $nullDataQuery = $fetchNull ? " OR status IS NULL " : '';
223 $whereQuery .= " AND ( status IN ( $statuses ) " . esc_sql($nullDataQuery) . " ) ";
224 }
225 }
226 }
227
228 if( $args['payment_date_from'] != '' && $args['payment_date_to'] != ''){
229 $whereQuery .= $wpdb->prepare(" AND payment_date BETWEEN %s AND %s", $args['payment_date_from'], $args['payment_date_to']);
230 }
231
232 if($count_only === 1){
233 if($valid_search_text){
234 $items = $wpdb->get_var(
235 "SELECT count(id) FROM $table WHERE 1
236 $whereQuery
237 ORDER BY $order_by $order
238 "
239 );
240 }else {
241 $items = $wpdb->get_var(
242 $wpdb->prepare(
243 "SELECT count(id) FROM $table WHERE 1
244 $whereQuery
245 ORDER BY $order_by $order
246 LIMIT %d, %d
247 ",
248 $offset,
249 $per_page
250 )
251 );
252 }
253
254 }else {
255 $items = $wpdb->get_results(
256 $wpdb->prepare(
257 "SELECT * FROM $table WHERE 1
258 $whereQuery
259 ORDER BY $order_by $order
260 LIMIT %d, %d
261 ",
262 $offset,
263 $per_page
264 )
265 );
266
267 }
268
269 return $items;
270 }
271
272 /**
273 * Get the count of total transactions
274 *
275 * @return int
276 * @since 0.0.1
277 */
278 public static function get_transaction_count($args = '', $version = 'v1', $fetchNull = 0, $transaction_type = '')
279 {
280 global $wpdb;
281 $table = self::get_table_name();
282
283 $count = 0;
284
285 if( !empty($transaction_type) ){
286 $count = self::get_transactions($args, 1, $version, $fetchNull, $transaction_type);
287 return $count;
288 }
289
290 if($args !== ''){
291 $count = self::get_transactions($args, 1, $version, $fetchNull);
292 }else {
293 $count = $wpdb->get_var(
294 "SELECT count(id) FROM $table"
295 );
296 }
297
298 return $count;
299 }
300
301 /**
302 * Get transactions analytics
303 *
304 * @return int
305 * @since 0.0.1
306 */
307 public static function get_transactions_analytics($filtered_transactions = array())
308 {
309 global $wpdb;
310 $transaction_analytics = [];
311
312 $table = self::get_table_name();
313 $better_payment_db_obj = new DB();
314
315 $total_transactions = $better_payment_db_obj->get_transactions_amount_by_statuses(array(), '', '', 'count', 1, $filtered_transactions );
316
317 $completed_statuses = $better_payment_db_obj->allowed_statuses('completed', 'v2');
318 $completed_transactions = $better_payment_db_obj->get_transactions_amount_by_statuses( $completed_statuses, '', '', 'count', 0, $filtered_transactions );
319
320 $incomplete_statuses = $better_payment_db_obj->allowed_statuses('incomplete', 'v2');
321 $incomplete_transactions = $better_payment_db_obj->get_transactions_amount_by_statuses( $incomplete_statuses, '', '', 'count', 1, $filtered_transactions );
322
323 $transaction_analytics['total_transactions'] = $total_transactions;
324 $transaction_analytics['completed_transactions'] = $completed_transactions;
325 $transaction_analytics['incomplete_transactions'] = $incomplete_transactions;
326
327 return $transaction_analytics;
328 }
329
330 /**
331 * Get transaction details from better payment table.
332 *
333 * @param string $args
334 * @return object
335 * @since 0.0.1
336 */
337 public static function get_transaction($id)
338 {
339 global $wpdb;
340
341 $table = self::get_table_name();
342 $transaction_id = (int) sanitize_text_field($id);
343
344 $item = $wpdb->get_row(
345 $wpdb->prepare(
346 "SELECT * FROM $table WHERE id = %d",
347 $transaction_id
348 )
349 );
350
351 return $item;
352 }
353
354 /**
355 * Delete a transaction
356 *
357 * @param int $id
358 *
359 * @return int|boolean
360 * @since 0.0.1
361 */
362 public static function delete_transaction($id)
363 {
364 global $wpdb;
365 $table = self::get_table_name();
366
367 return $wpdb->delete(
368 $table,
369 ['id' => $id],
370 ['%d']
371 );
372 }
373
374 /**
375 * Allowed transaction statuses
376 *
377 * @since 0.0.1
378 */
379 public function allowed_statuses($type = 'all', $version = 'v1'){
380 $helperObj = new Helper();
381 $statuses = $helperObj->get_statuses_by_transaction_type($type, $version);
382
383 return $statuses;
384 }
385
386 /**
387 * Allowed transaction statuses
388 *
389 * @since 0.0.1
390 */
391 public function allowed_statuses_types($version = 'v1'){
392 $helperObj = new Helper();
393 $statuses = $helperObj->get_transaction_types($version);
394
395 return $statuses;
396 }
397
398 /**
399 * Get transactions amount
400 *
401 * @return array
402 * @since 0.0.1
403 */
404 public function get_transactions_by_statuses($statuses = array(), $payment_date_from = '', $payment_date_to = '', $fetchNull=0) {
405 global $wpdb;
406
407 $table = DB::get_table_name();
408 $allowed_statuses = $this->allowed_statuses('all', 'v2');
409
410 $nullDataQuery = $fetchNull ? " OR status IS NULL " : '';
411
412 $statuses = is_array($statuses) && count($statuses) ? $statuses : $allowed_statuses;
413
414 #ToDo: Consider currency while calculating total amount
415 $statuses = array_map(function($status) use ($allowed_statuses) {
416 if(!in_array($status, $allowed_statuses)) {
417 return false;
418 }
419
420 return "'" . esc_sql($status) . "'";
421 }, $statuses);
422
423 $statuses = implode(',', $statuses);
424
425 if(!empty($payment_date_from) && !empty($payment_date_to)){
426 $amount = $wpdb->get_results(
427 "SELECT amount, payment_date FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . ") AND payment_date BETWEEN '" . esc_sql($payment_date_from) . "' AND '" . esc_sql($payment_date_to) . "'"
428 );
429 } else if(!empty($payment_date_from) && empty($payment_date_to)){
430 $amount = $wpdb->get_results(
431 "SELECT amount, payment_date FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . ") AND payment_date >= '" . esc_sql($payment_date_from) . "'"
432 );
433 } else {
434 $amount = $wpdb->get_results(
435 "SELECT amount, payment_date FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . " )"
436 );
437 }
438
439 return $amount;
440 }
441
442 /**
443 * Get transactions amount
444 *
445 * @return array
446 * @since 0.0.1
447 */
448 public function get_transactions_amount_by_statuses($statuses = array(), $payment_date_from = '', $payment_date_to = '', $count_or_amount = 'amount', $fetchNull=0, $filtered_transactions = array() ) {
449 global $wpdb;
450
451 $table = DB::get_table_name();
452 $allowed_statuses = $this->allowed_statuses('all', 'v2');
453
454 $nullDataQuery = $fetchNull ? " OR status IS NULL " : '';
455
456 $statuses = is_array($statuses) && count($statuses) ? $statuses : $allowed_statuses;
457 $statuses_original = $statuses;
458
459 #ToDo: Consider currency while calculating total amount
460 $statuses = array_map(function($status) use ($allowed_statuses) {
461 if(!in_array($status, $allowed_statuses)) {
462 return false;
463 }
464
465 return "'" . esc_sql($status) . "'";
466 }, $statuses);
467
468 $statuses = implode(',', $statuses);
469
470 if( is_array($filtered_transactions) && count($filtered_transactions) ){
471 $filtered_transactions_by_statuses = array();
472
473 foreach($filtered_transactions as $filtered_transaction) {
474 if( in_array($filtered_transaction->status, $statuses_original) ) {
475 $filtered_transactions_by_statuses[] = $filtered_transaction->id;
476 }
477 }
478
479 if($count_or_amount == 'count'){
480 $filtered_transactions_by_statuses_count = count($filtered_transactions_by_statuses);
481 return $filtered_transactions_by_statuses_count;
482 }
483 }
484 if($count_or_amount == 'count') {
485 $count = (int) $wpdb->get_var(
486 "SELECT count(id) FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . " )"
487 );
488
489 return $count;
490 }
491
492 if(!empty($payment_date_from) && !empty($payment_date_to)){
493 $amount = $wpdb->get_var(
494 "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) . "'"
495 );
496 }else {
497 $amount = $wpdb->get_var(
498 "SELECT sum(amount) FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . ")"
499 );
500 }
501
502 return $amount;
503 }
504
505 public static function search_transactions($items, $search_text, $offset, $per_page){
506 $filtered_items = array();
507 $search_text = str_replace('%', '', $search_text);
508
509 if(count($items) > 0){
510 $filtered_items = array_filter($items, function($item) use ($search_text){
511 $form_fields_info = maybe_unserialize($item->form_fields_info);
512
513 if(
514 isset($form_fields_info['first_name']) && stripos($form_fields_info['first_name'], $search_text) !== false
515 || isset($form_fields_info['last_name']) && stripos($form_fields_info['last_name'], $search_text) !== false
516 || isset($form_fields_info['primary_first_name']) && stripos($form_fields_info['primary_first_name'], $search_text) !== false
517 || isset($form_fields_info['primary_last_name']) && stripos($form_fields_info['primary_last_name'], $search_text) !== false
518 || isset($form_fields_info['email']) && stripos($form_fields_info['email'], $search_text) !== false
519 || isset($form_fields_info['primary_email']) && stripos($form_fields_info['primary_email'], $search_text) !== false
520 ){
521 return true;
522 }
523
524 });
525
526 }
527
528 return $filtered_items;
529 }
530 }
531