class-abstract-migration.php
3 years ago
class-date-time-utils.php
3 years ago
class-debugging.php
3 years ago
class-generate-modal.php
3 years ago
class-migration.php
3 years ago
class-request-utils.php
3 years ago
class-settings-utils.php
3 years ago
class-user-utils.php
3 years ago
index.php
5 years ago
class-settings-utils.php
116 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Responsible for various settings manipulations. |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage utils |
| 7 | * @copyright 2023 WP White Security |
| 8 | * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 |
| 9 | * @link https://wordpress.org/plugins/wp-2fa/ |
| 10 | */ |
| 11 | |
| 12 | namespace WP2FA\Utils; |
| 13 | |
| 14 | use WP2FA\Admin\Helpers\WP_Helper; |
| 15 | |
| 16 | /** |
| 17 | * Utility class handling settings CRUD. |
| 18 | * |
| 19 | * @package WP2FA\Utils |
| 20 | * @since 1.7.0 |
| 21 | */ |
| 22 | class Settings_Utils { |
| 23 | |
| 24 | /** |
| 25 | * Creates a hash based on the passed settings array. |
| 26 | * |
| 27 | * @param array $settings - Settings array. |
| 28 | * |
| 29 | * @return string |
| 30 | */ |
| 31 | public static function create_settings_hash( array $settings ): string { |
| 32 | return md5( json_encode( $settings ) ); // phpcs:ignore |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Returns an option by given name |
| 37 | * |
| 38 | * @param string $setting_name - The name of the option. |
| 39 | * @param mixed $default_value - The default value if there is no one stored. |
| 40 | * |
| 41 | * @return mixed |
| 42 | * |
| 43 | * @since 2.0.0 |
| 44 | */ |
| 45 | public static function get_option( $setting_name, $default_value = false ) { |
| 46 | $prefixed_setting_name = self::setting_prefixer( $setting_name ); |
| 47 | return ( WP_Helper::is_multisite() ) ? get_network_option( null, $prefixed_setting_name, $default_value ) : get_option( $prefixed_setting_name, $default_value ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Updates an option by a given name with a given value |
| 52 | * |
| 53 | * @param string $setting_name - The name of the setting to update. |
| 54 | * @param mixed $new_value - The value to be stored. |
| 55 | * |
| 56 | * @return mixed |
| 57 | * |
| 58 | * @since 2.0.0 |
| 59 | */ |
| 60 | public static function update_option( $setting_name, $new_value ) { |
| 61 | $prefixed_setting_name = self::setting_prefixer( $setting_name ); |
| 62 | return ( WP_Helper::is_multisite() ) ? update_network_option( null, $prefixed_setting_name, $new_value ) : update_option( $prefixed_setting_name, $new_value, true ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Deletes an option by a given name |
| 67 | * |
| 68 | * @param string $setting_name - The name of the option to delete. |
| 69 | * |
| 70 | * @return mixed |
| 71 | * |
| 72 | * @since 2.0.0 |
| 73 | */ |
| 74 | public static function delete_option( $setting_name ) { |
| 75 | $prefixed_setting_name = self::setting_prefixer( $setting_name ); |
| 76 | return ( WP_Helper::is_multisite() ) ? delete_network_option( null, $prefixed_setting_name ) : delete_option( $prefixed_setting_name ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Created a prefixed setting name from supplied string. |
| 81 | * |
| 82 | * @param string $setting_name - The name of the setting. |
| 83 | * |
| 84 | * @return string |
| 85 | */ |
| 86 | private static function setting_prefixer( $setting_name ) { |
| 87 | // Ensure we have not already been passed a prefixed setting name. |
| 88 | return ( strpos( $setting_name, 'wp_2fa_' ) === 0 ) ? $setting_name : WP_2FA_PREFIX . $setting_name; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Converts a string (e.g. 'yes' or 'no') to a bool. |
| 93 | * |
| 94 | * @since 2.0.0 |
| 95 | * @param string $string String to convert. |
| 96 | * @return bool |
| 97 | */ |
| 98 | public static function string_to_bool( $string ) { |
| 99 | return is_bool( $string ) ? $string : ( 'yes' === $string || 1 === $string || 'true' === $string || '1' === $string || 'on' === $string || 'enable' === $string ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Converts a bool to a 'yes' or 'no'. |
| 104 | * |
| 105 | * @since 2.0.0 |
| 106 | * @param bool $bool String to convert. |
| 107 | * @return string |
| 108 | */ |
| 109 | public static function bool_to_string( $bool ) { |
| 110 | if ( ! is_bool( $bool ) ) { |
| 111 | $bool = self::string_to_bool( $bool ); |
| 112 | } |
| 113 | return true === $bool ? 'yes' : 'no'; |
| 114 | } |
| 115 | } |
| 116 |