AbstractMigration.php
4 years ago
DateTimeUtils.php
4 years ago
Debugging.php
5 years ago
GenerateModal.php
4 years ago
Migration.php
4 years ago
RequestUtils.php
4 years ago
SettingsUtils.php
4 years ago
UserUtils.php
4 years ago
index.php
5 years ago
SettingsUtils.php
76 lines
| 1 | <?php |
| 2 | namespace WP2FA\Utils; |
| 3 | |
| 4 | use WP2FA\WP2FA as WP2FA; |
| 5 | |
| 6 | /** |
| 7 | * Utility class handling settings CRUD. |
| 8 | * |
| 9 | * @package WP2FA\Utils |
| 10 | * @since 1.7.0 |
| 11 | */ |
| 12 | class SettingsUtils { |
| 13 | |
| 14 | /** |
| 15 | * Creates a hash based on the passed settings array. |
| 16 | * |
| 17 | * @param array $settings - Settings array. |
| 18 | * |
| 19 | * @return string |
| 20 | */ |
| 21 | public static function create_settings_hash( array $settings ): string { |
| 22 | return md5( json_encode( $settings ) ); |
| 23 | } |
| 24 | |
| 25 | public static function get_option( $setting_name, $default_value = false ) { |
| 26 | $prefixed_setting_name = self::setting_prefixer( $setting_name ); |
| 27 | return ( WP2FA::is_this_multisite() ) ? get_network_option( null, $prefixed_setting_name, $default_value ) : get_option( $prefixed_setting_name, $default_value ); |
| 28 | } |
| 29 | |
| 30 | public static function update_option( $setting_name, $new_value ) { |
| 31 | $prefixed_setting_name = self::setting_prefixer( $setting_name ); |
| 32 | return ( WP2FA::is_this_multisite() ) ? update_network_option( null, $prefixed_setting_name, $new_value ) : update_option( $prefixed_setting_name, $new_value, true ); |
| 33 | } |
| 34 | |
| 35 | public static function delete_option( $setting_name ) { |
| 36 | $prefixed_setting_name = self::setting_prefixer( $setting_name ); |
| 37 | return ( WP2FA::is_this_multisite() ) ? delete_network_option( null, $prefixed_setting_name ) : delete_option( $prefixed_setting_name ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Created a prefixed setting name from supplied string. |
| 42 | * |
| 43 | * @param string $setting_name |
| 44 | * @return string |
| 45 | */ |
| 46 | private static function setting_prefixer( $setting_name ) { |
| 47 | // Ensure we have not already been passed a prefixed setting name. |
| 48 | return ( strpos( $setting_name, 'wp_2fa_' ) === 0 ) ? $setting_name : WP_2FA_PREFIX . $setting_name; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Converts a string (e.g. 'yes' or 'no') to a bool. |
| 53 | * |
| 54 | * @since 2.0.0 |
| 55 | * @param string $string String to convert. |
| 56 | * @return bool |
| 57 | */ |
| 58 | public static function string_to_bool( $string ) { |
| 59 | return is_bool( $string ) ? $string : ( 'yes' === $string || 1 === $string || 'true' === $string || '1' === $string || 'on' === $string || 'enable' === $string); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Converts a bool to a 'yes' or 'no'. |
| 64 | * |
| 65 | * @since 2.0.0 |
| 66 | * @param bool $bool String to convert. |
| 67 | * @return string |
| 68 | */ |
| 69 | public static function bool_to_string( $bool ) { |
| 70 | if ( ! is_bool( $bool ) ) { |
| 71 | $bool = self::string_to_bool( $bool ); |
| 72 | } |
| 73 | return true === $bool ? 'yes' : 'no'; |
| 74 | } |
| 75 | } |
| 76 |