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