PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 0.0.6
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v0.0.6
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.6, at includes/Admin/Better_Payment_DB.php

525 lines 19.7 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, $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 $items = $wpdb->get_results(
250 $wpdb->prepare(
251 "SELECT * FROM $table WHERE 1
252 $whereQuery
253 ORDER BY $order_by $order
254 LIMIT %d, %d
255 ",
256 $offset,
257 $per_page
258 )
259 );
260
261 }
262
263 return $items;
264 }
265
266 /**
267 * Get the count of total transactions
268 *
269 * @return int
270 * @since 0.0.1
271 */
272 public static function get_transaction_count($args = '', $version = 'v1', $fetchNull = 0, $transaction_type = '')
273 {
274 global $wpdb;
275 $table = self::get_table_name();
276
277 $count = 0;
278
279 if( !empty($transaction_type) ){
280 $count = self::get_transactions($args, 1, $version, $fetchNull, $transaction_type);
281 return $count;
282 }
283
284 if($args !== ''){
285 $count = self::get_transactions($args, 1, $version, $fetchNull);
286 }else {
287 $count = $wpdb->get_var(
288 "SELECT count(id) FROM $table"
289 );
290 }
291
292 return $count;
293 }
294
295 /**
296 * Get transactions analytics
297 *
298 * @return int
299 * @since 0.0.1
300 */
301 public static function get_transactions_analytics($filtered_transactions = array())
302 {
303 global $wpdb;
304 $transaction_analytics = [];
305
306 $table = self::get_table_name();
307 $better_payment_db_obj = new Better_Payment_DB();
308
309 $total_transactions = $better_payment_db_obj->get_transactions_amount_by_statuses(array(), '', '', 'count', 1, $filtered_transactions );
310
311 $completed_statuses = $better_payment_db_obj->allowed_statuses('completed', 'v2');
312 $completed_transactions = $better_payment_db_obj->get_transactions_amount_by_statuses( $completed_statuses, '', '', 'count', 0, $filtered_transactions );
313
314 $incomplete_statuses = $better_payment_db_obj->allowed_statuses('incomplete', 'v2');
315 $incomplete_transactions = $better_payment_db_obj->get_transactions_amount_by_statuses( $incomplete_statuses, '', '', 'count', 1, $filtered_transactions );
316
317 $transaction_analytics['total_transactions'] = $total_transactions;
318 $transaction_analytics['completed_transactions'] = $completed_transactions;
319 $transaction_analytics['incomplete_transactions'] = $incomplete_transactions;
320
321 return $transaction_analytics;
322 }
323
324 /**
325 * Get transaction details from better payment table.
326 *
327 * @param string $args
328 * @return object
329 * @since 0.0.1
330 */
331 public static function get_transaction($id)
332 {
333 global $wpdb;
334
335 $table = self::get_table_name();
336 $transaction_id = (int) sanitize_text_field($id);
337
338 $item = $wpdb->get_row(
339 $wpdb->prepare(
340 "SELECT * FROM $table WHERE id = %d",
341 $transaction_id
342 )
343 );
344
345 return $item;
346 }
347
348 /**
349 * Delete a transaction
350 *
351 * @param int $id
352 *
353 * @return int|boolean
354 * @since 0.0.1
355 */
356 public static function delete_transaction($id)
357 {
358 global $wpdb;
359 $table = self::get_table_name();
360
361 return $wpdb->delete(
362 $table,
363 ['id' => $id],
364 ['%d']
365 );
366 }
367
368 /**
369 * Allowed transaction statuses
370 *
371 * @since 0.0.1
372 */
373 public function allowed_statuses($type = 'all', $version = 'v1'){
374 $helperObj = new Better_Payment_Helper();
375 $statuses = $helperObj->get_statuses_by_transaction_type($type, $version);
376
377 return $statuses;
378 }
379
380 /**
381 * Allowed transaction statuses
382 *
383 * @since 0.0.1
384 */
385 public function allowed_statuses_types($version = 'v1'){
386 $helperObj = new Better_Payment_Helper();
387 $statuses = $helperObj->get_transaction_types($version);
388
389 return $statuses;
390 }
391
392 /**
393 * Get transactions amount
394 *
395 * @return array
396 * @since 0.0.1
397 */
398 public function get_transactions_by_statuses($statuses = array(), $payment_date_from = '', $payment_date_to = '', $fetchNull=0) {
399 global $wpdb;
400
401 $table = Better_Payment_DB::get_table_name();
402 $allowed_statuses = $this->allowed_statuses('all', 'v2');
403
404 $nullDataQuery = $fetchNull ? " OR status IS NULL " : '';
405
406 $statuses = is_array($statuses) && count($statuses) ? $statuses : $allowed_statuses;
407
408 #ToDo: Consider currency while calculating total amount
409 $statuses = array_map(function($status) use ($allowed_statuses) {
410 if(!in_array($status, $allowed_statuses)) {
411 return false;
412 }
413
414 return "'" . esc_sql($status) . "'";
415 }, $statuses);
416
417 $statuses = implode(',', $statuses);
418
419 if(!empty($payment_date_from) && !empty($payment_date_to)){
420 $amount = $wpdb->get_results(
421 "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) . "'"
422 );
423 } else if(!empty($payment_date_from) && empty($payment_date_to)){
424 $amount = $wpdb->get_results(
425 "SELECT amount, payment_date FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . ") AND payment_date >= '" . esc_sql($payment_date_from) . "'"
426 );
427 } else {
428 $amount = $wpdb->get_results(
429 "SELECT amount, payment_date FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . " )"
430 );
431 }
432
433 return $amount;
434 }
435
436 /**
437 * Get transactions amount
438 *
439 * @return array
440 * @since 0.0.1
441 */
442 public function get_transactions_amount_by_statuses($statuses = array(), $payment_date_from = '', $payment_date_to = '', $count_or_amount = 'amount', $fetchNull=0, $filtered_transactions = array() ) {
443 global $wpdb;
444
445 $table = Better_Payment_DB::get_table_name();
446 $allowed_statuses = $this->allowed_statuses('all', 'v2');
447
448 $nullDataQuery = $fetchNull ? " OR status IS NULL " : '';
449
450 $statuses = is_array($statuses) && count($statuses) ? $statuses : $allowed_statuses;
451 $statuses_original = $statuses;
452
453 #ToDo: Consider currency while calculating total amount
454 $statuses = array_map(function($status) use ($allowed_statuses) {
455 if(!in_array($status, $allowed_statuses)) {
456 return false;
457 }
458
459 return "'" . esc_sql($status) . "'";
460 }, $statuses);
461
462 $statuses = implode(',', $statuses);
463
464 if( is_array($filtered_transactions) && count($filtered_transactions) ){
465 $filtered_transactions_by_statuses = array();
466
467 foreach($filtered_transactions as $filtered_transaction) {
468 if( in_array($filtered_transaction->status, $statuses_original) ) {
469 $filtered_transactions_by_statuses[] = $filtered_transaction->id;
470 }
471 }
472
473 if($count_or_amount == 'count'){
474 $filtered_transactions_by_statuses_count = count($filtered_transactions_by_statuses);
475 return $filtered_transactions_by_statuses_count;
476 }
477 }
478 if($count_or_amount == 'count') {
479 $count = (int) $wpdb->get_var(
480 "SELECT count(id) FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . " )"
481 );
482
483 return $count;
484 }
485
486 if(!empty($payment_date_from) && !empty($payment_date_to)){
487 $amount = $wpdb->get_var(
488 "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) . "'"
489 );
490 }else {
491 $amount = $wpdb->get_var(
492 "SELECT sum(amount) FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . ")"
493 );
494 }
495
496 return $amount;
497 }
498
499 public static function search_transactions($items, $search_text, $offset, $per_page){
500 $filtered_items = array();
501 $search_text = str_replace('%', '', $search_text);
502
503 if(count($items) > 0){
504 $filtered_items = array_filter($items, function($item) use ($search_text){
505 $form_fields_info = maybe_unserialize($item->form_fields_info);
506
507 if(
508 isset($form_fields_info['first_name']) && stripos($form_fields_info['first_name'], $search_text) !== false
509 || isset($form_fields_info['last_name']) && stripos($form_fields_info['last_name'], $search_text) !== false
510 || isset($form_fields_info['primary_first_name']) && stripos($form_fields_info['primary_first_name'], $search_text) !== false
511 || isset($form_fields_info['primary_last_name']) && stripos($form_fields_info['primary_last_name'], $search_text) !== false
512 || isset($form_fields_info['email']) && stripos($form_fields_info['email'], $search_text) !== false
513 || isset($form_fields_info['primary_email']) && stripos($form_fields_info['primary_email'], $search_text) !== false
514 ){
515 return true;
516 }
517
518 });
519
520 }
521
522 return $filtered_items;
523 }
524 }
525