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