| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite\Blocks; |
| 4 |
|
| 5 |
class Settings |
| 6 |
{ |
| 7 |
|
| 8 |
private static $instance; |
| 9 |
|
| 10 |
public static function get_instance() |
| 11 |
{ |
| 12 |
if ( null === static::$instance ) { |
| 13 |
static::$instance = new static(); |
| 14 |
} |
| 15 |
return static::$instance; |
| 16 |
} |
| 17 |
|
| 18 |
public static function get( $key, $default = false ) |
| 19 |
{ |
| 20 |
return get_option( $key, $default ); |
| 21 |
} |
| 22 |
|
| 23 |
public static function save( $key, $value = '' ) |
| 24 |
{ |
| 25 |
return update_option( $key, $value ); |
| 26 |
} |
| 27 |
|
| 28 |
public static function save_eb_settings( $key, $value = '' ) |
| 29 |
{ |
| 30 |
$settings = get_option( 'eb_settings', [ ] ); |
| 31 |
$prev_value = null; |
| 32 |
|
| 33 |
// If 'all' is passed as the key, replace the entire settings array |
| 34 |
if ( $key === 'all' && ! empty( $value ) && is_string( $value ) ) { |
| 35 |
$prev_value = $settings; |
| 36 |
$settings = json_decode( wp_unslash( $value ), true ); |
| 37 |
|
| 38 |
// Ensure 'unfilteredFile' is only enabled by admins (when saving all settings at once) |
| 39 |
if ( array_key_exists( 'unfilteredFile', $settings ) ) { |
| 40 |
$requested = $settings['unfilteredFile']; |
| 41 |
$truthy = ( $requested === true || $requested === 'true' || $requested === 1 || $requested === '1' ); |
| 42 |
if ( current_user_can( 'activate_plugins' ) && $truthy ) { |
| 43 |
// Store as string 'true' for compatibility with consumers |
| 44 |
$settings['unfilteredFile'] = 'true'; |
| 45 |
} else { |
| 46 |
// For non-admins or falsy values, do not allow enabling this flag |
| 47 |
unset( $settings['unfilteredFile'] ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
// Fires after saving all settings |
| 53 |
do_action( "eb_after_save_all_settings", $settings, $prev_value ); |
| 54 |
|
| 55 |
return update_option( 'eb_settings', $settings ); |
| 56 |
} |
| 57 |
|
| 58 |
if ( isset( $settings[ $key ] ) ) { |
| 59 |
$prev_value = $settings[ $key ]; |
| 60 |
} |
| 61 |
|
| 62 |
// Enforce admin-only true for 'unfilteredFile' when saving a single key |
| 63 |
if ( $key === 'unfilteredFile' ) { |
| 64 |
$truthy = ( $value === true || $value === 'true' || $value === 1 || $value === '1' ); |
| 65 |
if ( current_user_can( 'activate_plugins' ) && $truthy ) { |
| 66 |
// Store as string 'true' for compatibility with consumers |
| 67 |
$value = 'true'; |
| 68 |
} else { |
| 69 |
// Non-admins or falsy values should not enable this flag |
| 70 |
$value = ''; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
if ( empty( $value ) ) { |
| 75 |
unset( $settings[ $key ] ); |
| 76 |
} else { |
| 77 |
$settings[ $key ] = $value; |
| 78 |
} |
| 79 |
/** |
| 80 |
* Fires after save a specific settings key |
| 81 |
* |
| 82 |
* @since 4.5.0 |
| 83 |
* @param mixed $value current value of settings |
| 84 |
* @param mixed $prev_value previous value of settings |
| 85 |
*/ |
| 86 |
do_action( "eb_after_save_{$key}_settings", $value, $prev_value ); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
return update_option( 'eb_settings', $settings ); |
| 91 |
} |
| 92 |
/** |
| 93 |
* Get Essential Blocks settings in a way that mirrors save_eb_settings() |
| 94 |
* |
| 95 |
* - Reads the single 'eb_settings' option (array) |
| 96 |
* - When a $key is provided, returns that entry or $default if not set |
| 97 |
* - Normalizes critical flags like 'unfilteredFile' to match how we save them |
| 98 |
* (string 'true' for enabled, '' for disabled) |
| 99 |
* - Provides a legacy fallback for previously stored single options |
| 100 |
* |
| 101 |
* @param string $key Optional settings key to fetch |
| 102 |
* @param mixed $default Default value when key not found |
| 103 |
* @return mixed Array of all settings or the value for the provided key |
| 104 |
*/ |
| 105 |
public static function get_eb_settings( $key = '', $default = false ) |
| 106 |
{ |
| 107 |
$settings = get_option( 'eb_settings', [] ); |
| 108 |
|
| 109 |
// If a specific key is requested |
| 110 |
if ( strlen( $key ) > 0 ) { |
| 111 |
if ( isset( $settings[ $key ] ) ) { |
| 112 |
$value = $settings[ $key ]; |
| 113 |
|
| 114 |
return $value; |
| 115 |
} |
| 116 |
|
| 117 |
return $default; |
| 118 |
} |
| 119 |
|
| 120 |
// If no key was requested, normalize the full array for consumers |
| 121 |
if ( array_key_exists( 'unfilteredFile', $settings ) ) { |
| 122 |
$value = $settings['unfilteredFile']; |
| 123 |
$truthy = ( $value === true || $value === 'true' || $value === 1 || $value === '1' ); |
| 124 |
$settings['unfilteredFile'] = $truthy ? 'true' : ''; |
| 125 |
} |
| 126 |
|
| 127 |
return $settings; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
public static function save_eb_write_with_ai( $value ) |
| 132 |
{ |
| 133 |
return update_option( 'eb_write_with_ai', $value ); |
| 134 |
} |
| 135 |
|
| 136 |
public static function reset_eb_settings( $key ) |
| 137 |
{ |
| 138 |
$settings = get_option( 'eb_settings', [ ] ); |
| 139 |
$prev_value = null; |
| 140 |
if ( isset( $settings[ $key ] ) ) { |
| 141 |
$prev_value = $settings[ $key ]; |
| 142 |
unset( $settings[ $key ] ); |
| 143 |
} |
| 144 |
/** |
| 145 |
* Fires after reset a specific settings key |
| 146 |
* |
| 147 |
* @since 4.5.0 |
| 148 |
* @param mixed $value current value of settings |
| 149 |
* @param mixed $prev_value previous value of settings |
| 150 |
*/ |
| 151 |
do_action( "eb_after_reset_{$key}_settings", $prev_value ); |
| 152 |
return update_option( 'eb_settings', $settings ); |
| 153 |
} |
| 154 |
|
| 155 |
public static function set_transient( $key, $value, $expiration = null ) |
| 156 |
{ |
| 157 |
if ( $expiration === null ) { |
| 158 |
$expiration = HOUR_IN_SECONDS * 6; |
| 159 |
} |
| 160 |
return set_transient( $key, $value, $expiration ); |
| 161 |
} |
| 162 |
|
| 163 |
public static function get_transient( $key ) |
| 164 |
{ |
| 165 |
return get_transient( $key ); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Summary of save_integration |
| 170 |
* @param mixed $type |
| 171 |
* @param mixed $data |
| 172 |
* @return bool |
| 173 |
*/ |
| 174 |
public static function save_integration( $type, $data = null ) |
| 175 |
{ |
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Summary of save_blocks_option |
| 181 |
* @param mixed $data |
| 182 |
* @return bool |
| 183 |
*/ |
| 184 |
public static function save_blocks_option( $data = [ ] ) |
| 185 |
{ |
| 186 |
/** |
| 187 |
* Sanitize Data |
| 188 |
*/ |
| 189 |
return update_option( 'essential_all_blocks', $data ); |
| 190 |
} |
| 191 |
} |