PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.9.2
WP 2FA – Two-factor authentication for WordPress v2.9.2
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 10 months ago class-date-time-utils.php 10 months ago class-debugging.php 10 months ago class-generate-modal.php 10 months ago class-migration.php 10 months ago class-request-utils.php 10 months ago class-settings-utils.php 10 months ago class-user-utils.php 10 months ago class-validator.php 10 months ago class-white-label.php 10 months ago index.php 10 months ago
class-settings-utils.php
164 lines
1 <?php
2 /**
3 * Responsible for various settings manipulations.
4 *
5 * @package wp2fa
6 * @subpackage utils
7 * @copyright 2025 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 use WP2FA\Admin\Helpers\WP_Helper;
19 use WP2FA\Admin\Controllers\Settings;
20 use WP2FA\Extensions\RoleSettings\Role_Settings_Controller;
21
22 if ( ! class_exists( '\WP2FA\Utils\Settings_Utils' ) ) {
23
24 /**
25 * Utility class handling settings CRUD.
26 *
27 * @package WP2FA\Utils
28 *
29 * @since 1.7.0
30 */
31 class Settings_Utils {
32
33 /**
34 * Creates a hash based on the passed settings array.
35 *
36 * @param array $settings - Settings array.
37 *
38 * @return string
39 *
40 * @since 3.0.0
41 */
42 public static function create_settings_hash( array $settings ): string {
43 return md5( json_encode( $settings ) );
44 }
45
46 /**
47 * Returns an option by given name
48 *
49 * @param string $setting_name - The name of the option.
50 * @param mixed $default_value - The default value if there is no one stored.
51 *
52 * @return mixed
53 *
54 * @since 2.0.0
55 */
56 public static function get_option( $setting_name, $default_value = false ) {
57 $setting_name = sanitize_key( $setting_name );
58 $prefixed_setting_name = self::setting_prefixer( $setting_name );
59
60 return ( WP_Helper::is_multisite() ) ? get_network_option( null, $prefixed_setting_name, $default_value ) : get_option( $prefixed_setting_name, $default_value );
61 }
62
63 /**
64 * Updates an option by a given name with a given value
65 *
66 * @param string $setting_name - The name of the setting to update.
67 * @param mixed $new_value - The value to be stored.
68 *
69 * @return mixed
70 *
71 * @since 2.0.0
72 */
73 public static function update_option( $setting_name, $new_value ) {
74 $setting_name = \sanitize_key( $setting_name );
75 $new_value = $new_value;
76 $prefixed_setting_name = self::setting_prefixer( $setting_name );
77
78 return ( WP_Helper::is_multisite() ) ? \update_network_option( null, $prefixed_setting_name, $new_value ) : \update_option( $prefixed_setting_name, $new_value, true );
79 }
80
81 /**
82 * Deletes an option by a given name
83 *
84 * @param string $setting_name - The name of the option to delete.
85 *
86 * @return mixed
87 *
88 * @since 2.0.0
89 */
90 public static function delete_option( $setting_name ) {
91 $setting_name = sanitize_key( $setting_name );
92 $prefixed_setting_name = self::setting_prefixer( $setting_name );
93
94 return ( WP_Helper::is_multisite() ) ? \delete_network_option( null, $prefixed_setting_name ) : \delete_option( $prefixed_setting_name );
95 }
96
97 /**
98 * Created a prefixed setting name from supplied string.
99 *
100 * @param string $setting_name - The name of the setting.
101 *
102 * @return string
103 *
104 * @since 3.0.0
105 */
106 private static function setting_prefixer( $setting_name ) {
107 // Ensure we have not already been passed a prefixed setting name.
108 return ( strpos( $setting_name, 'wp_2fa_' ) === 0 ) ? $setting_name : WP_2FA_PREFIX . $setting_name;
109 }
110
111 /**
112 * Converts a string (e.g. 'yes' or 'no') to a bool.
113 *
114 * @param string $string String to convert.
115 *
116 * @return bool
117 *
118 * @since 2.0.0
119 */
120 public static function string_to_bool( $string ) {
121 $string = sanitize_text_field( $string );
122 return is_bool( $string ) ? $string : ( 'yes' === $string || 1 === $string || 'true' === $string || '1' === $string || 'on' === $string || 'enable' === $string );
123 }
124
125 /**
126 * Converts a bool to a 'yes' or 'no'.
127 *
128 * @param bool $bool String to convert.
129 *
130 * @return string
131 *
132 * @since 2.0.0
133 */
134 public static function bool_to_string( $bool ) {
135 if ( ! is_bool( $bool ) ) {
136 $bool = self::string_to_bool( $bool );
137 }
138 return true === $bool ? 'yes' : 'no';
139 }
140
141 /**
142 * Gets a setting for a specific role. If no role is specified it will fall back to the default.
143 *
144 * @param string|null $role - The name of the role.
145 * @param string $setting_name - The name of the setting.
146 * @param bool $default - Set the default on empty.
147 *
148 * @return mixed
149 *
150 * @since 3.0.0
151 */
152 public static function get_setting_role( ?string $role, string $setting_name, bool $default = false ) {
153 $role = sanitize_key( $role );
154 $setting_name = sanitize_key( $setting_name );
155
156 if ( class_exists( Role_Settings_Controller::class, false ) ) {
157 return Role_Settings_Controller::get_setting( $role, $setting_name, $default );
158 }
159
160 return Settings::get_role_or_default_setting( $setting_name, null, $role, $default );
161 }
162 }
163 }
164