| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite\Admin; |
| 4 |
|
| 5 |
use Better_Payment\Lite\Controller; |
| 6 |
|
| 7 |
/** |
| 8 |
* Exit if accessed directly |
| 9 |
*/ |
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Settings persistence helpers. |
| 16 |
* |
| 17 |
* This class used to render the PHP admin — the settings page, the transactions |
| 18 |
* list and single view, the analytics page — and register their AJAX handlers. |
| 19 |
* The admin is React now (Admin\ReactAdmin + API\AdminAPI) and this class was |
| 20 |
* never instantiated, so all of that was unreachable and has been removed. What |
| 21 |
* remains are the three static helpers live code still calls: the quick-setup |
| 22 |
* wizard (Setup_Wizard::save_setup_wizard_data) and the Elementor bootstrap in |
| 23 |
* better-payment.php. |
| 24 |
* |
| 25 |
* @since 0.0.1 |
| 26 |
*/ |
| 27 |
class Settings extends Controller |
| 28 |
{ |
| 29 |
/** |
| 30 |
* This function is responsible for |
| 31 |
* save all settings data, including checking the disable field to prevent |
| 32 |
* users manipulation. |
| 33 |
* |
| 34 |
* @param array $values |
| 35 |
* @return void |
| 36 |
* @since 0.0.1 |
| 37 |
*/ |
| 38 |
public static function save_settings($posted_fields = []) |
| 39 |
{ |
| 40 |
$saved_settings = DB::get_settings(); |
| 41 |
$default_settings = DB::default_settings(); |
| 42 |
|
| 43 |
$saved_settings = wp_parse_args($default_settings, $saved_settings); // newly added default values to merge |
| 44 |
|
| 45 |
$data = []; |
| 46 |
$new_posted_fields = []; |
| 47 |
|
| 48 |
if (!empty($posted_fields)) { |
| 49 |
foreach ($posted_fields as $posted_field) { |
| 50 |
$new_posted_fields[$posted_field['name']] = $posted_field['value']; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
if (count($new_posted_fields)) { |
| 55 |
|
| 56 |
foreach ($saved_settings as $saved_settings_item_key => $saved_settings_item_value) { |
| 57 |
if (isset($new_posted_fields[$saved_settings_item_key]) && $new_posted_fields[$saved_settings_item_key] != '') { |
| 58 |
$data[$saved_settings_item_key] = self::sanitize_field($saved_settings_item_key, $new_posted_fields[$saved_settings_item_key]); |
| 59 |
} else { |
| 60 |
$data[$saved_settings_item_key] = $saved_settings_item_value; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
if (count($data)) { |
| 66 |
DB::update_settings($data); |
| 67 |
} |
| 68 |
|
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Save default settings |
| 74 |
* |
| 75 |
* @since 0.0.1 |
| 76 |
*/ |
| 77 |
public static function save_default_settings($reset = 0) |
| 78 |
{ |
| 79 |
$default_settings = DB::default_settings(); |
| 80 |
$saved_settings = DB::get_settings(); |
| 81 |
|
| 82 |
//Do nothing : if already saved && not reset |
| 83 |
if (!empty($saved_settings) && (!$reset)) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
return DB::update_settings($default_settings); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* This function is responsible for the data sanitization |
| 92 |
* |
| 93 |
* @param array $field |
| 94 |
* @param string|array $value |
| 95 |
* @return string|array |
| 96 |
* @since 0.0.1 |
| 97 |
*/ |
| 98 |
public static function sanitize_field($field, $value) |
| 99 |
{ |
| 100 |
if (isset($field['sanitize']) && !empty($field['sanitize'])) { |
| 101 |
if (function_exists($field['sanitize'])) { |
| 102 |
$value = call_user_func($field['sanitize'], $value); |
| 103 |
} |
| 104 |
return $value; |
| 105 |
} |
| 106 |
|
| 107 |
if (is_array($field) && isset($field['type'])) { |
| 108 |
switch ($field['type']) { |
| 109 |
case 'text': |
| 110 |
$value = sanitize_text_field($value); |
| 111 |
break; |
| 112 |
case 'textarea': |
| 113 |
$value = sanitize_textarea_field($value); |
| 114 |
break; |
| 115 |
case 'email': |
| 116 |
$value = sanitize_email($value); |
| 117 |
break; |
| 118 |
default: |
| 119 |
return $value; |
| 120 |
break; |
| 121 |
} |
| 122 |
} else { |
| 123 |
$value = sanitize_text_field($value); |
| 124 |
} |
| 125 |
|
| 126 |
return $value; |
| 127 |
} |
| 128 |
} |
| 129 |
|