PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 4.0.0
WP 2FA – Two-factor authentication for WordPress v4.0.0
4.0.0 1.7.1 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.8.0 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0 3.0.1 3.1.0 3.1.1 3.1.1.2 trunk 1.2.0 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.7.0
wp-2fa / includes / classes / Utils / class-settings-utils.php
wp-2fa / includes / classes / Utils Last commit date
class-abstract-migration.php 23 hours ago class-date-time-utils.php 23 hours ago class-debugging.php 23 hours ago class-generate-modal.php 23 hours ago class-migration.php 23 hours ago class-request-utils.php 23 hours ago class-settings-utils.php 23 hours ago class-upgrade-guard.php 23 hours ago class-user-utils.php 23 hours ago class-validator.php 23 hours ago class-white-label.php 23 hours ago index.php 23 hours ago
class-settings-utils.php
166 lines
1 <?php
2 /**
3 * Responsible for various settings manipulations.
4 *
5 * @package wp2fa
6 * @subpackage utils
7 * @copyright 2026 Melapress
8 * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
9 * @link https://wordpress.org/plugins/wp-2fa/
10 *
11 * @since 1.7.0
12 */
13
14 declare(strict_types=1);
15
16 namespace WP2FA\Utils;
17
18 defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
19
20 use WP2FA\Admin\Helpers\WP_Helper;
21 use WP2FA\Admin\Controllers\Settings;
22 use WP2FA\Extensions\RoleSettings\Role_Settings_Controller;
23
24 if ( ! class_exists( '\WP2FA\Utils\Settings_Utils' ) ) {
25
26 /**
27 * Utility class handling settings CRUD.
28 *
29 * @package WP2FA\Utils
30 *
31 * @since 1.7.0
32 */
33 class Settings_Utils {
34
35 /**
36 * Creates a hash based on the passed settings array.
37 *
38 * @param array $settings - Settings array.
39 *
40 * @return string
41 *
42 * @since 3.0.0
43 */
44 public static function create_settings_hash( array $settings ): string {
45 return md5( json_encode( $settings ) );
46 }
47
48 /**
49 * Returns an option by given name
50 *
51 * @param string $setting_name - The name of the option.
52 * @param mixed $default_value - The default value if there is no one stored.
53 *
54 * @return mixed
55 *
56 * @since 2.0.0
57 */
58 public static function get_option( $setting_name, $default_value = false ) {
59 $setting_name = sanitize_key( $setting_name );
60 $prefixed_setting_name = self::setting_prefixer( $setting_name );
61
62 return ( WP_Helper::is_multisite() ) ? get_network_option( null, $prefixed_setting_name, $default_value ) : get_option( $prefixed_setting_name, $default_value );
63 }
64
65 /**
66 * Updates an option by a given name with a given value
67 *
68 * @param string $setting_name - The name of the setting to update.
69 * @param mixed $new_value - The value to be stored.
70 *
71 * @return mixed
72 *
73 * @since 2.0.0
74 */
75 public static function update_option( $setting_name, $new_value ) {
76 $setting_name = \sanitize_key( $setting_name );
77 $new_value = $new_value;
78 $prefixed_setting_name = self::setting_prefixer( $setting_name );
79
80 return ( WP_Helper::is_multisite() ) ? \update_network_option( null, $prefixed_setting_name, $new_value ) : \update_option( $prefixed_setting_name, $new_value, false );
81 }
82
83 /**
84 * Deletes an option by a given name
85 *
86 * @param string $setting_name - The name of the option to delete.
87 *
88 * @return mixed
89 *
90 * @since 2.0.0
91 */
92 public static function delete_option( $setting_name ) {
93 $setting_name = sanitize_key( $setting_name );
94 $prefixed_setting_name = self::setting_prefixer( $setting_name );
95
96 return ( WP_Helper::is_multisite() ) ? \delete_network_option( null, $prefixed_setting_name ) : \delete_option( $prefixed_setting_name );
97 }
98
99 /**
100 * Created a prefixed setting name from supplied string.
101 *
102 * @param string $setting_name - The name of the setting.
103 *
104 * @return string
105 *
106 * @since 3.0.0
107 */
108 private static function setting_prefixer( $setting_name ) {
109 // Ensure we have not already been passed a prefixed setting name.
110 return ( strpos( $setting_name, 'wp_2fa_' ) === 0 ) ? $setting_name : WP_2FA_PREFIX . $setting_name;
111 }
112
113 /**
114 * Converts a string (e.g. 'yes' or 'no') to a bool.
115 *
116 * @param string $string String to convert.
117 *
118 * @return bool
119 *
120 * @since 2.0.0
121 */
122 public static function string_to_bool( $string ) {
123 $string = sanitize_text_field( $string );
124 return is_bool( $string ) ? $string : ( 'yes' === $string || 1 === $string || 'true' === $string || '1' === $string || 'on' === $string || 'enable' === $string );
125 }
126
127 /**
128 * Converts a bool to a 'yes' or 'no'.
129 *
130 * @param bool $bool String to convert.
131 *
132 * @return string
133 *
134 * @since 2.0.0
135 */
136 public static function bool_to_string( $bool ) {
137 if ( ! is_bool( $bool ) ) {
138 $bool = self::string_to_bool( $bool );
139 }
140 return true === $bool ? 'yes' : 'no';
141 }
142
143 /**
144 * Gets a setting for a specific role. If no role is specified it will fall back to the default.
145 *
146 * @param string|null $role - The name of the role.
147 * @param string $setting_name - The name of the setting.
148 * @param bool $default - Set the default on empty.
149 *
150 * @return mixed
151 *
152 * @since 3.0.0
153 */
154 public static function get_setting_role( ?string $role, string $setting_name, bool $default = false ) {
155 $role = \sanitize_key( $role );
156 $setting_name = \sanitize_key( $setting_name );
157
158 if ( class_exists( Role_Settings_Controller::class, false ) ) {
159 return Role_Settings_Controller::get_setting( $role, $setting_name, $default );
160 }
161
162 return Settings::get_role_or_default_setting( $setting_name, null, $role, $default );
163 }
164 }
165 }
166