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