PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 0.0.3
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v0.0.3
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.3, at includes/Admin/Better_Payment_Settings.php

565 lines 18.6 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 use function Better_Payment\Lite\Classes\better_payment_dd;
9
10 /**
11 * Exit if accessed directly
12 */
13 if (!defined('ABSPATH')) {
14 exit;
15 }
16
17 /**
18 * The plugin settings class
19 *
20 * @since 0.0.1
21 */
22 class Better_Payment_Settings extends Controller
23 {
24 var $page_slug_prefix = '';
25 /**
26 * @var int|string
27 */
28
29 protected $file_version;
30
31 /**
32 * Constructor
33 *
34 * @since 0.0.1
35 */
36 public function __construct($page_slug_prefix = 'better-payment')
37 {
38 $this->page_slug_prefix = $page_slug_prefix;
39 $this->file_version = defined('WP_DEBUG') && WP_DEBUG ? time() : BETTER_PAYMENT_VERSION;
40
41 add_action('admin_menu', [$this, 'register_menu']);
42 add_action('wp_ajax_better_payment_settings_action', [$this, 'save_settings_ajax']);
43
44 //Transactions page: actions
45 add_action('wp_ajax_better-payment-delete-transaction', [$this, 'admin_transaction_delete']);
46 add_action('wp_ajax_better-payment-filter-transaction', [$this, 'admin_transaction_filter']);
47 add_action('wp_ajax_better-payment-view-transaction', [$this, 'admin_transaction_view']);
48
49 //EL Editor Style
50 add_action('elementor/editor/before_enqueue_scripts', [$this, 'editor_enqueue_scripts']);
51 }
52
53 /**
54 * Enqueue editor scripts
55 *
56 * @since 0.0.1
57 */
58 public function editor_enqueue_scripts()
59 {
60 // bp icon font
61 wp_enqueue_style(
62 'bp-icon-admin-editor',
63 BETTER_PAYMENT_ASSETS . '/icon/style.min.css',
64 false
65 );
66 }
67
68 /**
69 * Register menu
70 *
71 * @since 0.0.1
72 */
73 public function register_menu()
74 {
75 add_menu_page(
76 __('Better Payment', 'better-payment'),
77 __('Better Payment', 'better-payment'),
78 'manage_options',
79 $this->page_slug_prefix . '-settings',
80 [$this, 'render_better_payment_admin_pages'],
81 BETTER_PAYMENT_ASSETS . '/img/better-payment-icon-white-small.png',
82 64
83 );
84
85 add_submenu_page(
86 $this->page_slug_prefix . '-settings',
87 __('Settings', 'better-payment'),
88 __('Settings', 'better-payment'),
89 'manage_options',
90 $this->page_slug_prefix . '-settings',
91 [$this, 'render_better_payment_admin_pages'],
92 64
93 );
94
95 $settings = apply_filters( 'better_payment/admin/get_submenu_page_list', array(
96 $this->page_slug_prefix . '-transactions' => array(
97 'title' => __('Transactions', 'better-payment'),
98 'capability' => 'manage_options',
99 'callback' => [$this, 'render_better_payment_admin_pages'],
100 ),
101 ), $this->page_slug_prefix );
102
103 foreach( $settings as $slug => $setting ) {
104 $cap = isset( $setting['capability'] ) ? $setting['capability'] : 'manage_options';
105 add_submenu_page( $this->page_slug_prefix . '-settings', $setting['title'], $setting['title'], $cap, $slug, $setting['callback'] );
106 }
107 }
108
109 /**
110 * Admin pages
111 *
112 * @since 0.0.1
113 */
114 public function render_better_payment_admin_pages()
115 {
116 $bp_admin_saved_settings = Better_Payment_DB::get_settings();
117 $bp_admin_all_transactions_count = Better_Payment_DB::get_transaction_count();
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
418 $bp_admin_all_transactions_count = Better_Payment_DB::get_transaction_count($args, 'v2', $fetchNull);
419 $bp_admin_all_transactions_paginations = self::bp_paginate_links($args['paged'], $args['per_page'], $bp_admin_all_transactions_count );
420 $paginations_showing_entities_html = self::bp_paginate_showing_entities_html($args['paged'], $args['per_page'], $bp_admin_all_transactions_count );
421
422 include_once BETTER_PAYMENT_ADMIN_VIEWS_PATH . "/template-transaction-list.php";
423 exit;
424 }
425
426 /**
427 * Paging links
428 *
429 * @since 0.0.1
430 */
431 public static function bp_paginate_links($current_page, $per_page, $total_entry_count)
432 {
433 $pagination_html = '';
434
435 $current_page = intval($current_page);
436 $per_page = intval($per_page);
437 $total_entry_count = intval($total_entry_count);
438
439 $total_page_count = ceil($total_entry_count / $per_page);
440
441 $previous_page = $current_page - 1;
442 $next_page = $current_page + 1;
443
444 $prev_button_disabled = (1 == $current_page) ? 'disabled' : '';
445 $next_button_disabled = $total_page_count == $current_page ? 'disabled' : '';
446
447 $prev_button_link = ($current_page > 1) ? admin_url("admin.php?page=better-payment-transactions&per_page={$per_page}&paged={$previous_page}") : "#";
448 $next_button_link = ($current_page < $total_page_count) ? admin_url("admin.php?page=better-payment-transactions&per_page={$per_page}&paged={$next_page}") : "#";
449
450 $showing_entry_from = $per_page * ($current_page - 1) + 1;
451
452 $pagination_html .= "<ul class=''>";
453
454 //Showing entries content
455 if ($showing_entry_from > $total_entry_count) {
456 return '';
457 }
458
459 if ( $total_page_count == 1 ) {
460 return '';
461 }
462
463 //Previous button
464 if($current_page > 1){
465 $pagination_html .= "<li><a href='$prev_button_link' $prev_button_disabled class='pagination-previous'><i class='bp-icon bp-caret-left'></i></a></li>";
466 }
467
468 //Pagination links
469 for ($i = 1; $i <= $total_page_count; $i++) {
470 $is_current_class = ($i == $current_page) ? 'is-current active' : '';
471 $page_link = admin_url("admin.php?page=better-payment-transactions&per_page={$per_page}&paged={$i}");
472
473 $pagination_html .= "
474 <li>
475 <a href=\"$page_link\" class=\"pagination-link $is_current_class \" aria-label=\"Page $i\"> $i </a>
476 </li>
477 ";
478 }
479
480 //Next button
481 if($current_page < $total_page_count){
482 $pagination_html .= "<li><a href='$next_button_link' class='pagination-next' $next_button_disabled><i class='bp-icon bp-caret-right'></i></a></li>";
483 }
484
485 $pagination_html .= "</ul>";
486
487 return $pagination_html;
488 }
489
490 /**
491 * Pagination showing entities html
492 *
493 * @since 0.0.1
494 */
495 public static function bp_paginate_showing_entities_html($current_page, $per_page, $total_entry_count)
496 {
497 $pagination_showing_entries_html = '';
498
499 $current_page = intval($current_page);
500 $per_page = intval($per_page);
501 $total_entry_count = intval($total_entry_count);
502
503 $total_page_count = ceil($total_entry_count / $per_page);
504
505 $showing_entry_from = $per_page * ($current_page - 1) + 1;
506 $showing_entry_to = $per_page * $current_page;
507
508 //Showing entries content
509 if ($total_page_count == 1 || $current_page == $total_page_count) {
510 $showing_entry_to = $total_entry_count;
511 }
512
513 $pagination_showing_entries_html = __("Showing {$showing_entry_from}-{$showing_entry_to} of $total_entry_count entries", "better-payment");
514
515 if ($showing_entry_from > $total_entry_count) {
516 return '';
517 }
518
519 if ($total_page_count == 1) {
520 return $pagination_showing_entries_html;
521 }
522
523 return $pagination_showing_entries_html;
524 }
525
526 /**
527 * This function is responsible for the data sanitization
528 *
529 * @param array $field
530 * @param string|array $value
531 * @return string|array
532 * @since 0.0.1
533 */
534 public static function sanitize_field($field, $value)
535 {
536 if (isset($field['sanitize']) && !empty($field['sanitize'])) {
537 if (function_exists($field['sanitize'])) {
538 $value = call_user_func($field['sanitize'], $value);
539 }
540 return $value;
541 }
542
543 if (is_array($field) && isset($field['type'])) {
544 switch ($field['type']) {
545 case 'text':
546 $value = sanitize_text_field($value);
547 break;
548 case 'textarea':
549 $value = sanitize_textarea_field($value);
550 break;
551 case 'email':
552 $value = sanitize_email($value);
553 break;
554 default:
555 return $value;
556 break;
557 }
558 } else {
559 $value = sanitize_text_field($value);
560 }
561
562 return $value;
563 }
564 }
565