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_Settings.php

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

573 lines 19.4 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 use Better_Payment\Lite\Controller;
7
8 /**
9 * Exit if accessed directly
10 */
11 if (!defined('ABSPATH')) {
12 exit;
13 }
14
15 /**
16 * The plugin settings class
17 *
18 * @since 0.0.1
19 */
20 class Better_Payment_Settings extends Controller
21 {
22 var $page_slug_prefix = '';
23 /**
24 * @var int|string
25 */
26
27 protected $file_version;
28
29 /**
30 * Constructor
31 *
32 * @since 0.0.1
33 */
34 public function __construct($page_slug_prefix = 'better-payment')
35 {
36 $this->page_slug_prefix = $page_slug_prefix;
37 $this->file_version = defined('WP_DEBUG') && WP_DEBUG ? time() : BETTER_PAYMENT_VERSION;
38
39 add_action('admin_menu', [$this, 'register_menu']);
40 add_action('wp_ajax_better_payment_settings_action', [$this, 'save_settings_ajax']);
41
42 //Transactions page: actions
43 add_action('wp_ajax_better-payment-delete-transaction', [$this, 'admin_transaction_delete']);
44 add_action('wp_ajax_better-payment-filter-transaction', [$this, 'admin_transaction_filter']);
45 add_action('wp_ajax_better-payment-view-transaction', [$this, 'admin_transaction_view']);
46
47 //EL Editor Style
48 add_action('elementor/editor/before_enqueue_scripts', [$this, 'editor_enqueue_scripts']);
49 }
50
51 /**
52 * Enqueue editor scripts
53 *
54 * @since 0.0.1
55 */
56 public function editor_enqueue_scripts()
57 {
58 // bp icon font
59 wp_enqueue_style(
60 'bp-icon-admin-editor',
61 BETTER_PAYMENT_ASSETS . '/icon/style.min.css',
62 false
63 );
64 }
65
66 /**
67 * Register menu
68 *
69 * @since 0.0.1
70 */
71 public function register_menu()
72 {
73 add_menu_page(
74 __('Better Payment', 'better-payment'),
75 __('Better Payment', 'better-payment'),
76 'manage_options',
77 $this->page_slug_prefix . '-settings',
78 [$this, 'render_better_payment_admin_pages'],
79 BETTER_PAYMENT_ASSETS . '/img/better-payment-icon-white-small.png',
80 64
81 );
82
83 add_submenu_page(
84 $this->page_slug_prefix . '-settings',
85 __('Settings', 'better-payment'),
86 __('Settings', 'better-payment'),
87 'manage_options',
88 $this->page_slug_prefix . '-settings',
89 [$this, 'render_better_payment_admin_pages'],
90 64
91 );
92
93 $settings = apply_filters( 'better_payment/admin/get_submenu_page_list', array(
94 $this->page_slug_prefix . '-transactions' => array(
95 'title' => __('Transactions', 'better-payment'),
96 'capability' => 'manage_options',
97 'callback' => [$this, 'render_better_payment_admin_pages'],
98 ),
99 ), $this->page_slug_prefix );
100
101 foreach( $settings as $slug => $setting ) {
102 $cap = isset( $setting['capability'] ) ? $setting['capability'] : 'manage_options';
103 add_submenu_page( $this->page_slug_prefix . '-settings', $setting['title'], $setting['title'], $cap, $slug, $setting['callback'] );
104 }
105 }
106
107 /**
108 * Admin pages
109 *
110 * @since 0.0.1
111 */
112 public function render_better_payment_admin_pages()
113 {
114 $bp_admin_saved_settings = Better_Payment_DB::get_settings();
115 $bp_admin_all_transactions_count = Better_Payment_DB::get_transaction_count();
116 $bp_admin_completed_transactions_count = Better_Payment_DB::get_transaction_count('', 'v2', 0, 'completed');
117 $bp_admin_incomplete_transactions_count = Better_Payment_DB::get_transaction_count('', 'v2', 1, 'incomplete');
118
119 $template = !empty($_GET['page']) ? sanitize_text_field($_GET['page']) : 'better-payment-settings';
120
121 $menuObj = new Menu();
122 $menu_slugs = $menuObj->better_payment_menu_slugs();
123 $template = in_array($template, $menu_slugs) ? $template : 'better-payment-settings';
124
125 $transaction_pagination_args = [];
126 $bp_admin_all_transactions_paginations = '';
127 $viewMode = 0;
128
129 if ($template == 'better-payment-transactions') {
130 //Transactions Page
131 $action = isset($_GET['action']) ? sanitize_text_field($_GET['action']) : 'list';
132 $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
133 $template = 'better-payment-transaction';
134
135 switch ($action) {
136 case 'edit':
137 $template .= '-edit';
138 break;
139
140 case 'view':
141 $viewMode = 1;
142 $template .= '-view';
143 break;
144
145 default:
146 $template .= '-list';
147 break;
148 }
149
150 $ui_postfix = '';
151 $template .= $ui_postfix;
152
153 //Pagination
154 $transaction_pagination_paged = isset($_GET['paged']) && intval($_GET['paged']) > 1 ? intval($_GET['paged']) : 1;
155 $transaction_pagination_order = isset($_GET['order']) ? sanitize_text_field($_GET['order']) : 'DESC';
156 $transaction_pagination_orderby = isset($_GET['orderby']) ? sanitize_text_field($_GET['orderby']) : 'id';
157 $transaction_pagination_per_page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 20;
158 $transaction_pagination_source = isset($_GET['source']) ? sanitize_text_field($_GET['source']) : '';
159 $transaction_pagination_status = isset($_GET['status']) ? sanitize_text_field($_GET['status']) : 'all';
160
161 if ($transaction_pagination_paged) {
162 $transaction_pagination_args = [
163 'number' => $transaction_pagination_per_page,
164 'offset' => ($transaction_pagination_paged - 1) * $transaction_pagination_per_page,
165 'orderby' => $transaction_pagination_orderby,
166 'order' => $transaction_pagination_order,
167 'status' => $transaction_pagination_status
168 ];
169 }
170 $bp_admin_all_transactions_paginations = self::bp_paginate_links($transaction_pagination_paged, $transaction_pagination_per_page, $bp_admin_all_transactions_count);
171 $paginations_showing_entities_html = self::bp_paginate_showing_entities_html(intval($transaction_pagination_paged), intval($transaction_pagination_per_page), intval($bp_admin_all_transactions_count));
172
173 $fetchNullIfIncomplete = $transaction_pagination_status === 'incomplete' ? 1 : 0;
174 $bp_admin_all_transactions = Better_Payment_DB::get_transactions($transaction_pagination_args, 0, 'v2', $fetchNullIfIncomplete);
175 }
176
177 $bp_admin_all_transactions_analytics = Better_Payment_DB::get_transactions_analytics();
178
179 //Transaction details/view page
180 if($viewMode){
181 $transaction_id = isset($_REQUEST['id']) ? (int) sanitize_text_field($_REQUEST['id']) : 0;
182 if($transaction_id == 0){
183 _e('Record not found!', 'better-payment');
184 exit;
185 }
186 $bp_admin_transaction = Better_Payment_DB::get_transaction($transaction_id);
187
188 //Fetch referer content
189 $better_payment_helper = new Better_Payment_Helper();
190 $bp_admin_transaction_form_fields = is_object($bp_admin_transaction) && isset($bp_admin_transaction) ? maybe_unserialize($bp_admin_transaction->form_fields_info) : [];
191 $referer_page_id = !empty($bp_admin_transaction_form_fields['referer_page_id']) ? $bp_admin_transaction_form_fields['referer_page_id'] : '';
192 $referer_widget_id = !empty($bp_admin_transaction_form_fields['referer_widget_id']) ? $bp_admin_transaction_form_fields['referer_widget_id'] : '';
193
194 $bp_transaction_referer_content = '';
195
196 if($referer_page_id != '' && $referer_widget_id != ''){
197 $bp_transaction_referer_content = $better_payment_helper->get_better_payment_widget_settings($referer_page_id, $referer_widget_id);
198 }
199 }
200
201 include_once BETTER_PAYMENT_ADMIN_VIEWS_PATH . '/' . "$template.php";
202 }
203
204 /**
205 * Settings save ajax
206 *
207 * @since 0.0.1
208 */
209 public static function save_settings_ajax()
210 {
211 /**
212 * Verify the Nonce
213 */
214 if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'better_payment_admin_nonce')) {
215 return;
216 }
217
218 if (isset($_POST['form_data']) && $_POST['reset_button'] == 'true') {
219 self::save_default_settings(1);
220 wp_send_json_success("success");
221 } else if (isset($_POST['form_data'])) {
222 self::save_settings($_POST['form_data']);
223 wp_send_json_success("success");
224 } else {
225 wp_send_json_error("error");
226 }
227
228 die;
229 }
230
231 /**
232 * This function is responsible for
233 * save all settings data, including checking the disable field to prevent
234 * users manipulation.
235 *
236 * @param array $values
237 * @return void
238 * @since 0.0.1
239 */
240 public static function save_settings($posted_fields = [])
241 {
242 $saved_settings = Better_Payment_DB::get_settings();
243 $default_settings = Better_Payment_DB::default_settings();
244
245 $saved_settings = wp_parse_args($default_settings, $saved_settings); // newly added default values to merge
246
247 $data = [];
248 $new_posted_fields = [];
249
250 if (!empty($posted_fields)) {
251 foreach ($posted_fields as $posted_field) {
252 $new_posted_fields[$posted_field['name']] = $posted_field['value'];
253 }
254 }
255
256 if (count($new_posted_fields)) {
257
258 foreach ($saved_settings as $saved_settings_item_key => $saved_settings_item_value) {
259 if (isset($new_posted_fields[$saved_settings_item_key]) && $new_posted_fields[$saved_settings_item_key] != '') {
260 $data[$saved_settings_item_key] = self::sanitize_field($saved_settings_item_key, $new_posted_fields[$saved_settings_item_key]);
261 } else {
262 $data[$saved_settings_item_key] = $saved_settings_item_value;
263 }
264 }
265 }
266
267 if (count($data)) {
268 Better_Payment_DB::update_settings($data);
269 }
270
271 return;
272 }
273
274 /**
275 * Save default settings
276 *
277 * @since 0.0.1
278 */
279 public static function save_default_settings($reset = 0)
280 {
281 $default_settings = Better_Payment_DB::default_settings();
282 $saved_settings = Better_Payment_DB::get_settings();
283
284 //Do nothing : if already saved && not reset
285 if (!empty($saved_settings) && (!$reset)) {
286 return false;
287 }
288
289 return Better_Payment_DB::update_settings($default_settings);
290 }
291
292 /**
293 * transaction template
294 *
295 * @since 0.0.1
296 */
297 public function admin_transaction_view()
298 {
299 $response = [];
300
301 if (!wp_verify_nonce($_REQUEST['nonce'], 'better_payment_admin_nonce')) {
302 $response['message'] = __("Access Denied!", 'better-payment');
303 wp_send_json_error($response);
304 }
305
306 if (!current_user_can('manage_options')) {
307 $response['message'] = __("Access Denied!", 'better-payment');
308 wp_send_json_error($response);
309 }
310
311 $transaction_id = isset($_REQUEST['id']) ? (int) sanitize_text_field($_REQUEST['id']) : 0;
312
313 $bp_admin_transaction = '';
314
315 if($transaction_id == 0){
316 return __('Record not found!', 'better-payment');
317 }
318
319 $bp_admin_transaction = Better_Payment_DB::get_transaction($transaction_id);
320
321 include_once BETTER_PAYMENT_ADMIN_VIEWS_PATH . "/template-transaction-view.php";
322 exit;
323 }
324
325 /**
326 * Transaction delete
327 *
328 * @since 0.0.1
329 */
330 public function admin_transaction_delete()
331 {
332 $response = [];
333
334 if (!wp_verify_nonce($_REQUEST['nonce'], 'better_payment_admin_nonce')) {
335 $response['message'] = __("Access Denied!", 'better-payment');
336 wp_send_json_error($response);
337 }
338
339 if (!current_user_can('manage_options')) {
340 $response['message'] = __("Access Denied!", 'better-payment');
341 wp_send_json_error($response);
342 }
343
344 $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
345
346 if (Better_Payment_DB::delete_transaction($id)) {
347 $response['message'] = __("Deleted successfully", 'better-payment');
348
349 $current_page = sanitize_text_field($_REQUEST['currentPage']);
350 $per_page = sanitize_text_field($_REQUEST['perPage']);
351 $total_entry_count = sanitize_text_field($_REQUEST['totalEntryCount']);
352
353 $response['pagination_showing_entities_html'] = self::bp_paginate_showing_entities_html(intval($current_page), intval($per_page), intval($total_entry_count) - 1);
354
355 wp_send_json_success($response);
356 } else {
357 $response['message'] = __("Something went wrong!", 'better-payment');
358 wp_send_json_error( $response );
359 }
360 }
361
362 /**
363 * Transaction filter
364 *
365 * @since 0.0.1
366 */
367 public function admin_transaction_filter()
368 {
369 $response = [];
370
371 if (!wp_verify_nonce($_REQUEST['nonce'], 'better_payment_admin_nonce')) {
372 $response['message'] = __("Access Denied!", 'better-payment');
373 wp_send_json_error($response);
374 }
375
376 if (!current_user_can('manage_options')) {
377 $response['message'] = __("Access Denied!", 'better-payment');
378 wp_send_json_error($response);
379 }
380
381 $filter_search_text = ( isset($_REQUEST['filterFormData']['search_text']) && !empty($_REQUEST['filterFormData']['search_text']) ) ? sanitize_text_field( $_REQUEST['filterFormData']['search_text'] ) : '';
382 $filter_payment_date_from = ( isset($_REQUEST['filterFormData']['payment_date_from']) && !empty($_REQUEST['filterFormData']['payment_date_from']) ) ? sanitize_text_field( $_REQUEST['filterFormData']['payment_date_from'] ) : '';
383 $filter_payment_date_to = ( isset($_REQUEST['filterFormData']['payment_date_to']) && !empty($_REQUEST['filterFormData']['payment_date_to']) ) ? sanitize_text_field( $_REQUEST['filterFormData']['payment_date_to'] ) : '';
384 $filter_order_by = ( isset($_REQUEST['filterFormData']['order_by']) && !empty($_REQUEST['filterFormData']['order_by']) ) ? sanitize_text_field( $_REQUEST['filterFormData']['order_by'] ) : 'id';
385 $filter_order = ( isset($_REQUEST['filterFormData']['order']) && !empty($_REQUEST['filterFormData']['order']) ) ? sanitize_text_field( $_REQUEST['filterFormData']['order'] ) : 'DESC';
386 $filter_status = ( isset($_REQUEST['filterFormData']['status']) && !empty($_REQUEST['filterFormData']['status']) ) ? sanitize_text_field( $_REQUEST['filterFormData']['status'] ) : 'all';
387 $filter_source = ( isset($_REQUEST['filterFormData']['source']) && !empty($_REQUEST['filterFormData']['source']) ) ? sanitize_text_field( $_REQUEST['filterFormData']['source'] ) : '';
388 $filter_paged = ( isset($_REQUEST['filterFormData']['paged']) && !empty($_REQUEST['filterFormData']['paged']) ) ? intval( $_REQUEST['filterFormData']['paged'] ) : 1;
389 $filter_per_page = ( isset($_REQUEST['filterFormData']['per_page']) && !empty($_REQUEST['filterFormData']['per_page']) ) ? intval( $_REQUEST['filterFormData']['per_page'] ) : 20;
390
391 $args = array(
392 'search_text' => $filter_search_text,
393 'payment_date_from' => $filter_payment_date_from,
394 'payment_date_to' => $filter_payment_date_to,
395 'order_by' => $filter_order_by,
396 'order' => $filter_order,
397 'status' => $filter_status,
398 'source' => $filter_source,
399 'paged' => $filter_paged,
400 'per_page' => $filter_per_page,
401 );
402
403 if( !empty($args['payment_date_from']) ){
404 $args['payment_date_from'] = date('Y-m-d H:i:s',strtotime($args['payment_date_from']));
405 }
406 if( !empty($args['payment_date_to']) ){
407 $args['payment_date_to'] = date('Y-m-d H:i:s',strtotime($args['payment_date_to']));
408 }
409
410 $args['offset'] = ( $args['paged'] - 1) * $args['per_page'];
411
412 $fetchNullIfIncomplete = !empty($args['status']) && 'incomplete' === $args['status'] ? 1 : 0;
413 $bp_admin_all_transactions = Better_Payment_DB::get_transactions($args, 0, 'v2', $fetchNullIfIncomplete);
414
415 //Pagination
416 $fetchNull = !empty($args['status']) && 'incomplete' === $args['status'] ? 1 : 0;
417 $only_incompleted_transactions = !empty($args['status']) && 'incomplete' === $args['status'] ? 1 : 0;
418 $only_completed_transactions = !empty($args['status']) && 'completed' === $args['status'] ? 1 : 0;
419
420 $bp_admin_all_transactions_count = Better_Payment_DB::get_transaction_count($args, 'v2', $fetchNull);
421 $bp_admin_all_transactions_paginations = self::bp_paginate_links($args['paged'], $args['per_page'], $bp_admin_all_transactions_count );
422 $paginations_showing_entities_html = self::bp_paginate_showing_entities_html($args['paged'], $args['per_page'], $bp_admin_all_transactions_count );
423
424 $bp_admin_completed_transactions_count = Better_Payment_DB::get_transaction_count($args, 'v2', 0, 'completed');
425 $bp_admin_incomplete_transactions_count = Better_Payment_DB::get_transaction_count($args, 'v2', 1, 'incomplete');
426
427 $bp_admin_completed_transactions_count = $only_incompleted_transactions ? 0 : $bp_admin_completed_transactions_count;
428 $bp_admin_incomplete_transactions_count = $only_completed_transactions ? 0 : $bp_admin_incomplete_transactions_count;
429
430 include_once BETTER_PAYMENT_ADMIN_VIEWS_PATH . "/template-transaction-list.php";
431 exit;
432 }
433
434 /**
435 * Paging links
436 *
437 * @since 0.0.1
438 */
439 public static function bp_paginate_links($current_page, $per_page, $total_entry_count)
440 {
441 $pagination_html = '';
442
443 $current_page = intval($current_page);
444 $per_page = intval($per_page);
445 $total_entry_count = intval($total_entry_count);
446
447 $total_page_count = ceil($total_entry_count / $per_page);
448
449 $previous_page = $current_page - 1;
450 $next_page = $current_page + 1;
451
452 $prev_button_disabled = (1 == $current_page) ? 'disabled' : '';
453 $next_button_disabled = $total_page_count == $current_page ? 'disabled' : '';
454
455 $prev_button_link = ($current_page > 1) ? admin_url("admin.php?page=better-payment-transactions&per_page={$per_page}&paged={$previous_page}") : "#";
456 $next_button_link = ($current_page < $total_page_count) ? admin_url("admin.php?page=better-payment-transactions&per_page={$per_page}&paged={$next_page}") : "#";
457
458 $showing_entry_from = $per_page * ($current_page - 1) + 1;
459
460 $pagination_html .= "<ul class=''>";
461
462 //Showing entries content
463 if ($showing_entry_from > $total_entry_count) {
464 return '';
465 }
466
467 if ( $total_page_count == 1 ) {
468 return '';
469 }
470
471 //Previous button
472 if($current_page > 1){
473 $pagination_html .= "<li><a href='$prev_button_link' $prev_button_disabled class='pagination-previous'><i class='bp-icon bp-caret-left'></i></a></li>";
474 }
475
476 //Pagination links
477 for ($i = 1; $i <= $total_page_count; $i++) {
478 $is_current_class = ($i == $current_page) ? 'is-current active' : '';
479 $page_link = admin_url("admin.php?page=better-payment-transactions&per_page={$per_page}&paged={$i}");
480
481 $pagination_html .= "
482 <li>
483 <a href=\"$page_link\" class=\"pagination-link $is_current_class \" aria-label=\"Page $i\"> $i </a>
484 </li>
485 ";
486 }
487
488 //Next button
489 if($current_page < $total_page_count){
490 $pagination_html .= "<li><a href='$next_button_link' class='pagination-next' $next_button_disabled><i class='bp-icon bp-caret-right'></i></a></li>";
491 }
492
493 $pagination_html .= "</ul>";
494
495 return $pagination_html;
496 }
497
498 /**
499 * Pagination showing entities html
500 *
501 * @since 0.0.1
502 */
503 public static function bp_paginate_showing_entities_html($current_page, $per_page, $total_entry_count)
504 {
505 $pagination_showing_entries_html = '';
506
507 $current_page = intval($current_page);
508 $per_page = intval($per_page);
509 $total_entry_count = intval($total_entry_count);
510
511 $total_page_count = ceil($total_entry_count / $per_page);
512
513 $showing_entry_from = $per_page * ($current_page - 1) + 1;
514 $showing_entry_to = $per_page * $current_page;
515
516 //Showing entries content
517 if ($total_page_count == 1 || $current_page == $total_page_count) {
518 $showing_entry_to = $total_entry_count;
519 }
520
521 $pagination_showing_entries_html = __("Showing {$showing_entry_from}-{$showing_entry_to} of $total_entry_count entries", "better-payment");
522
523 if ($showing_entry_from > $total_entry_count) {
524 return '';
525 }
526
527 if ($total_page_count == 1) {
528 return $pagination_showing_entries_html;
529 }
530
531 return $pagination_showing_entries_html;
532 }
533
534 /**
535 * This function is responsible for the data sanitization
536 *
537 * @param array $field
538 * @param string|array $value
539 * @return string|array
540 * @since 0.0.1
541 */
542 public static function sanitize_field($field, $value)
543 {
544 if (isset($field['sanitize']) && !empty($field['sanitize'])) {
545 if (function_exists($field['sanitize'])) {
546 $value = call_user_func($field['sanitize'], $value);
547 }
548 return $value;
549 }
550
551 if (is_array($field) && isset($field['type'])) {
552 switch ($field['type']) {
553 case 'text':
554 $value = sanitize_text_field($value);
555 break;
556 case 'textarea':
557 $value = sanitize_textarea_field($value);
558 break;
559 case 'email':
560 $value = sanitize_email($value);
561 break;
562 default:
563 return $value;
564 break;
565 }
566 } else {
567 $value = sanitize_text_field($value);
568 }
569
570 return $value;
571 }
572 }
573