PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 0.0.4
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v0.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 / Better_Payment_DB.php

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

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