PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 0.0.2
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v0.0.2
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 / admin / includes / class-better-payment-settings.php

class-better-payment-settings.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 0.0.2, at admin/includes/class-better-payment-settings.php

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