class-settings.php
252 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Responsible for the plugin settings iterations |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage trusted-devices |
| 7 | */ |
| 8 | |
| 9 | namespace WP2FA\Admin\Controllers; |
| 10 | |
| 11 | use WP2FA\WP2FA; |
| 12 | use WP2FA\Admin\User; |
| 13 | |
| 14 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 15 | |
| 16 | /** |
| 17 | * WP2FA Settings controller |
| 18 | */ |
| 19 | class Settings { |
| 20 | |
| 21 | /** |
| 22 | * The name of the WP2FA WP admin settings page |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | private static $settings_page_name = 'wp-2fa-policies'; |
| 27 | |
| 28 | /** |
| 29 | * The link to the WP admin settings page |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | private static $settings_page_link = ''; |
| 34 | |
| 35 | /** |
| 36 | * The name of the WP2FA WP admin setup page |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | private static $setup_page_name = 'wp-2fa-setup'; |
| 41 | |
| 42 | /** |
| 43 | * The link to the WP admin setup page |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
| 47 | private static $setup_page_link = ''; |
| 48 | |
| 49 | /** |
| 50 | * The link to the custom settings page (if one is presented) |
| 51 | * |
| 52 | * @var string |
| 53 | */ |
| 54 | private static $custom_setup_page_link = null; |
| 55 | |
| 56 | /** |
| 57 | * Array with all the backup methods available |
| 58 | * |
| 59 | * @var array |
| 60 | * |
| 61 | * @since 2.0.0 |
| 62 | */ |
| 63 | private static $backup_methods = null; |
| 64 | |
| 65 | /** |
| 66 | * Returns the link to the WP admin settings page, based on the current WP install |
| 67 | * |
| 68 | * @return string |
| 69 | */ |
| 70 | public static function get_settings_page_link() { |
| 71 | if ( '' === self::$settings_page_link ) { |
| 72 | if ( WP2FA::is_this_multisite() ) { |
| 73 | self::$settings_page_link = add_query_arg( 'page', self::$settings_page_name, network_admin_url( 'admin.php' ) ); |
| 74 | } else { |
| 75 | self::$settings_page_link = add_query_arg( 'page', self::$settings_page_name, admin_url( 'admin.php' ) ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return self::$settings_page_link; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the link to the WP admin settings page, based on the current WP install |
| 84 | * |
| 85 | * @return string |
| 86 | */ |
| 87 | public static function get_setup_page_link() { |
| 88 | if ( '' === self::$setup_page_link ) { |
| 89 | if ( WP2FA::is_this_multisite() ) { |
| 90 | self::$setup_page_link = add_query_arg( 'show', self::$setup_page_name, network_admin_url( 'profile.php' ) ); |
| 91 | } else { |
| 92 | self::$setup_page_link = add_query_arg( 'show', self::$setup_page_name, admin_url( 'profile.php' ) ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return self::$setup_page_link; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Extracts the custom settings page URL |
| 101 | * |
| 102 | * @param mixed $user - User for which to extract the setting, null, WP_User or user id - @see get_role_or_default_setting method of this class. |
| 103 | * |
| 104 | * @return string |
| 105 | */ |
| 106 | public static function get_custom_page_link( $user = null ): string { |
| 107 | if ( null === self::$custom_setup_page_link ) { |
| 108 | self::$custom_setup_page_link = self::get_role_or_default_setting( 'custom-user-page-id', $user ); |
| 109 | |
| 110 | if ( ! empty( self::$custom_setup_page_link ) ) { |
| 111 | $custom_slug = ''; |
| 112 | if ( WP2FA::is_this_multisite() ) { |
| 113 | switch_to_blog( get_main_site_id() ); |
| 114 | |
| 115 | $custom_slug = get_post_field( 'post_name', get_post( self::$custom_setup_page_link ) ); |
| 116 | self::$custom_setup_page_link = trailingslashit( get_site_url() ) . $custom_slug; |
| 117 | |
| 118 | restore_current_blog(); |
| 119 | } else { |
| 120 | $custom_slug = get_post_field( 'post_name', get_post( self::$custom_setup_page_link ) ); |
| 121 | self::$custom_setup_page_link = trailingslashit( get_site_url() ) . $custom_slug; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return self::$custom_setup_page_link; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Check all the roles for given setting |
| 131 | * |
| 132 | * @param string $setting_name - The name of the setting to check for. |
| 133 | * |
| 134 | * @return boolean |
| 135 | * |
| 136 | * @since 2.0.0 |
| 137 | */ |
| 138 | public static function check_setting_in_all_roles( string $setting_name ): bool { |
| 139 | global $wp_roles; |
| 140 | |
| 141 | $roles = $wp_roles->get_names(); |
| 142 | |
| 143 | foreach ( $roles as $role => $value ) { |
| 144 | if ( ! empty( WP2FA::get_wp2fa_setting( $setting_name, null, null, $role ) ) ) { |
| 145 | return true; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Return setting specific for the given role or default setting (based on user) |
| 154 | * |
| 155 | * @param string $setting_name - The name of the setting. |
| 156 | * @param mixed $user - \WP_User or any string or null - if string the current user will be used, if null global plugin setting will be used. |
| 157 | * @param mixed $role - The name of the role (or null). |
| 158 | * @param boolean $get_default_on_empty - Get default setting on empty setting value. |
| 159 | * @param boolean $get_default_value - Extracts default value. |
| 160 | * |
| 161 | * @return mixed |
| 162 | * |
| 163 | * @since 2.0.0 |
| 164 | */ |
| 165 | public static function get_role_or_default_setting( string $setting_name, $user = null, $role = null, $get_default_on_empty = false, $get_default_value = false ) { |
| 166 | /** |
| 167 | * No user specified - get the default settings |
| 168 | */ |
| 169 | if ( null === $user ) { |
| 170 | return WP2FA::get_wp2fa_setting( $setting_name, $get_default_on_empty, $get_default_value ); |
| 171 | } |
| 172 | /** |
| 173 | * There is an User - extract the role |
| 174 | */ |
| 175 | if ( $user instanceof \WP_User ) { |
| 176 | if ( null === $role ) { |
| 177 | $role = reset( $user->roles ); |
| 178 | } |
| 179 | return WP2FA::get_wp2fa_setting( $setting_name, $get_default_on_empty, $get_default_value, $role ); |
| 180 | } |
| 181 | |
| 182 | // Extract user by an ID. |
| 183 | if ( is_int( $user ) ) { |
| 184 | if ( null === $role ) { |
| 185 | $role = reset( ( new \WP_User( $user ) )->roles ); |
| 186 | } |
| 187 | return WP2FA::get_wp2fa_setting( $setting_name, $get_default_on_empty, $get_default_value, $role ); |
| 188 | } |
| 189 | /** |
| 190 | * Current user - lets extract the role |
| 191 | */ |
| 192 | if ( null === $role ) { |
| 193 | /** |
| 194 | * No logged in current user, ergo no roles - fall back to defaults |
| 195 | */ |
| 196 | if ( 0 === User::get_instance()->getUser()->ID ) { |
| 197 | return WP2FA::get_wp2fa_setting( $setting_name, $get_default_on_empty, $get_default_value ); |
| 198 | } |
| 199 | |
| 200 | $role = reset( User::get_instance()->getUser()->roles ); |
| 201 | } |
| 202 | return WP2FA::get_wp2fa_setting( $setting_name, $get_default_on_empty, $get_default_value, $role ); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Returns all the backup methods currently supported |
| 207 | * |
| 208 | * @return array |
| 209 | * |
| 210 | * @since 2.0.0 |
| 211 | */ |
| 212 | public static function get_backup_methods(): array { |
| 213 | |
| 214 | if ( null === self::$backup_methods ) { |
| 215 | |
| 216 | /** |
| 217 | * Gives the ability to add additional backup methods |
| 218 | * |
| 219 | * @param array The array with all the backup methods currently supported. |
| 220 | * |
| 221 | * @since 2.0.0 |
| 222 | */ |
| 223 | self::$backup_methods = apply_filters( WP_2FA_PREFIX . 'backup_methods_list', array() ); |
| 224 | } |
| 225 | |
| 226 | return self::$backup_methods; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Get backup methods enabled for user based on its role |
| 231 | * |
| 232 | * @param \WP_User $user - The WP user which we must check. |
| 233 | * |
| 234 | * @return array |
| 235 | * |
| 236 | * @since 2.0.0 |
| 237 | */ |
| 238 | public static function get_enabled_backup_methods_for_user_role( \WP_User $user ): array { |
| 239 | $backup_methods = self::get_backup_methods(); |
| 240 | |
| 241 | /** |
| 242 | * Extensions could change the enabled backup methods array. |
| 243 | * |
| 244 | * @param array - Backup methods array. |
| 245 | * @param \WP_User - The user to check for. |
| 246 | * |
| 247 | * @since 2.0.0 |
| 248 | */ |
| 249 | return apply_filters( 'wp_2fa_backup_methods_enabled', $backup_methods, $user ); |
| 250 | } |
| 251 | } |
| 252 |