class-abstract-migration.php
11 hours ago
class-date-time-utils.php
11 hours ago
class-debugging.php
11 hours ago
class-generate-modal.php
11 hours ago
class-migration.php
11 hours ago
class-request-utils.php
11 hours ago
class-settings-utils.php
11 hours ago
class-upgrade-guard.php
11 hours ago
class-user-utils.php
11 hours ago
class-validator.php
11 hours ago
class-white-label.php
11 hours ago
index.php
11 hours ago
class-white-label.php
221 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Responsible for white labeling functionality. |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage white-label |
| 7 | * |
| 8 | * @copyright 2026 Melapress |
| 9 | * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 |
| 10 | * |
| 11 | * @see https://wordpress.org/plugins/wp-2fa/ |
| 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\WP2FA; |
| 21 | use WP2FA\Admin\Controllers\Methods; |
| 22 | |
| 23 | if ( ! class_exists( '\WP2FA\Utils\White_Label' ) ) { |
| 24 | /** |
| 25 | * Utility class for white labeling. |
| 26 | * |
| 27 | * @package WP2FA\Utils |
| 28 | * |
| 29 | * @since 2.8.0 |
| 30 | */ |
| 31 | class White_Label { |
| 32 | |
| 33 | /** |
| 34 | * Local static cache for plugins settings. |
| 35 | * |
| 36 | * @var array |
| 37 | * |
| 38 | * @since 2.8.0 |
| 39 | */ |
| 40 | private static $plugin_settings = array(); |
| 41 | |
| 42 | /** |
| 43 | * Class cache to store the default white label settings. |
| 44 | * |
| 45 | * @var array |
| 46 | * |
| 47 | * @since 3.0.0 |
| 48 | */ |
| 49 | private static $default_settings = array(); |
| 50 | |
| 51 | /** |
| 52 | * Inits the plugin related classes and settings |
| 53 | * |
| 54 | * @return void |
| 55 | * |
| 56 | * @since 2.8.0 |
| 57 | */ |
| 58 | public static function init() { |
| 59 | |
| 60 | \add_filter( WP_2FA_PREFIX . 'default_settings', array( __CLASS__, 'merge_settings' ) ); |
| 61 | |
| 62 | \add_filter( WP_2FA_PREFIX . 'whitelabel_settings', array( __CLASS__, 'fill_settings_array' ) ); |
| 63 | |
| 64 | \add_filter( 'login_headerurl', array( __CLASS__, 'set_logo_url' ) ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Changes the default logo URL to the white label's custom URL. |
| 69 | * |
| 70 | * @param string $logo_url - The current logo URL. |
| 71 | * |
| 72 | * @return string |
| 73 | * |
| 74 | * @since 3.0.0 |
| 75 | */ |
| 76 | public static function set_logo_url( $logo_url ) { |
| 77 | $custom_url = esc_url( WP2FA::get_wp2fa_white_label_setting( 'logo-code-page-url' ) ); |
| 78 | |
| 79 | if ( ! empty( $custom_url ) ) { |
| 80 | // Change the header title as well. |
| 81 | \add_filter( |
| 82 | 'login_headertext', |
| 83 | function() use ( $custom_url ) { |
| 84 | return esc_html( $custom_url ); |
| 85 | } |
| 86 | ); |
| 87 | |
| 88 | return $custom_url; |
| 89 | } |
| 90 | |
| 91 | return $logo_url; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Merges the default settings with the stored settings. |
| 96 | * |
| 97 | * @param array $settings_array - Collected settings. |
| 98 | * |
| 99 | * @since 3.0.0 |
| 100 | */ |
| 101 | public static function fill_settings_array( $settings_array ) { |
| 102 | $settings_array = array_merge( self::get_default_settings(), $settings_array ); |
| 103 | |
| 104 | return $settings_array; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Util function to grab white label settings or apply defaults if no settings are saved into the db. |
| 109 | * |
| 110 | * @param string $setting_name Settings to grab value of. |
| 111 | * @param boolean $get_default_on_empty return default setting value if current one is empty. |
| 112 | * |
| 113 | * @return string|array Settings value or default value. |
| 114 | * |
| 115 | * @since 2.8.0 |
| 116 | */ |
| 117 | public static function get_setting( $setting_name = '', $get_default_on_empty = false ) { |
| 118 | $default_settings = self::get_default_settings( array() ); |
| 119 | |
| 120 | $white_label_setting = self::$plugin_settings[ WP_2FA_WHITE_LABEL_SETTINGS_NAME ]; |
| 121 | |
| 122 | // If we have no setting name, return them all. |
| 123 | if ( empty( $setting_name ) ) { |
| 124 | return $white_label_setting; |
| 125 | } |
| 126 | |
| 127 | // First lets check if any options have been saved. |
| 128 | $apply_defaults = empty( $white_label_setting ); |
| 129 | |
| 130 | if ( $apply_defaults ) { |
| 131 | return isset( $default_settings[ $setting_name ] ) ? $default_settings[ $setting_name ] : ''; |
| 132 | } elseif ( ! isset( $white_label_setting[ $setting_name ] ) ) { |
| 133 | if ( true === $get_default_on_empty ) { |
| 134 | if ( isset( $default_settings[ $setting_name ] ) ) { |
| 135 | return $default_settings[ $setting_name ]; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return ''; |
| 140 | } else { |
| 141 | return $white_label_setting[ $setting_name ]; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Array with all the plugin default settings. |
| 147 | * |
| 148 | * @return array |
| 149 | * |
| 150 | * @since 2.8.0 |
| 151 | */ |
| 152 | public static function get_default_settings() { |
| 153 | |
| 154 | if ( empty( self::$default_settings ) ) { |
| 155 | self::$default_settings = array( |
| 156 | 'default-text-code-page' => '<p>' . \esc_html__( 'Please enter the two-factor authentication (2FA) verification code below to login. Depending on your 2FA setup, you can get the code from the 2FA app or it was sent to you by email.', 'wp-2fa' ) . '</p><p><strong>' . \esc_html__( 'Note: if you are supposed to receive an email but did not receive any, please click the Resend Code button to request another code.', 'wp-2fa' ) . '</strong></p>', |
| 157 | 'default-text-pw-reset-code-page' => '<p>' . \esc_html__( 'You have been sent a one-time code via email. Please enter the code below and then click Get New Password to proceed with the password reset.', 'wp-2fa' ) . '</p><br><p><strong>' . \esc_html__( 'Note: If you have not received the code please click the button Resend Code. If you still do not get the code after pressing the button, please contact the website\'s administrator.', 'wp-2fa' ) . '</strong></p>', |
| 158 | 'default-2fa-required-notice' => '<p>' . \esc_html__( 'This website\'s administrator requires you to enable two-factor authentication (2FA) {grace_period_remaining}.', 'wp-2fa' ) . '</p><br><p>' . \esc_html__( 'Failing to configure 2FA within this time period will result in a locked account. For more information, please contact your website administrator.', 'wp-2fa' ) . '</p>', |
| 159 | 'default-2fa-resetup-required-notice' => '<p>' . \esc_html__( 'This website\'s administrator requires you to enable two-factor authentication (2FA) {grace_period_remaining}.', 'wp-2fa' ) . '</p><br><p>' . \esc_html__( 'Failing to configure 2FA within this time period will result in a locked account. For more information, please contact your website administrator.', 'wp-2fa' ) . '</p>', |
| 160 | |
| 161 | 'custom-text-app-code-page' => '<p>' . \esc_html__( 'Please enter the two-factor authentication (2FA) verification code below to login. Depending on your 2FA setup, you can get the code from the 2FA app or it was sent to you by email.', 'wp-2fa' ) . '</p><p><strong>' . \esc_html__( 'Note: if you are supposed to receive an email but did not receive any, please click the Resend Code button to request another code.', 'wp-2fa' ) . '</strong></p>', |
| 162 | 'custom-text-email-code-page' => '<p>' . \esc_html__( 'Please enter the two-factor authentication (2FA) verification code below to login. Depending on your 2FA setup, you can get the code from the 2FA app or it was sent to you by email.', 'wp-2fa' ) . '</p><p><strong>' . \esc_html__( 'Note: if you are supposed to receive an email but did not receive any, please click the Resend Code button to request another code.', 'wp-2fa' ) . '</strong></p>', |
| 163 | |
| 164 | 'default-backup-code-page' => \esc_html__( 'Enter a backup verification code.', 'wp-2fa' ), |
| 165 | 'enable_wizard_styling' => 'enable_wizard_styling', |
| 166 | 'show_help_text' => 'show_help_text', |
| 167 | 'enable_wizard_logo' => '', |
| 168 | 'hide_page_generated_by' => false, |
| 169 | 'enable_welcome' => '', |
| 170 | 'welcome' => '', |
| 171 | 'method_selection' => '<h3>' . \esc_html__( 'Choose the 2FA method', 'wp-2fa' ) . '</h3>' . Methods::get_number_of_methods_text(), |
| 172 | // 'method_selection_single' => '<h3>' . \esc_html__( 'Choose the 2FA method', 'wp-2fa' ) . '</h3><p>' . \esc_html__( 'Only the below 2FA method is allowed on this website:', 'wp-2fa' ) . '</p>', |
| 173 | |
| 174 | 'no_further_action' => '<h3>' . \esc_html__( 'Congratulations! You are all set.', 'wp-2fa' ), |
| 175 | 'wp-2fa_required_intro' => '<h3>' . \esc_html__( 'You are required to configure 2FA.', 'wp-2fa' ) . '</h3><p>' . \esc_html__( 'In order to keep this site - and your details secure, this website’s administrator requires you to enable 2FA authentication to continue.', 'wp-2fa' ) . '</p><p>' . \esc_html__( 'Two factor authentication ensures only you have access to your account by creating an added layer of security when logging in -', 'wp-2fa' ) . ' <a href="https://melapress.com/wordpress-2fa/?&utm_source=plugin&utm_medium=wp2fa&utm_campaign=learn_more" target="_blank" rel="noopener">' . \esc_html__( 'Learn more', 'wp-2fa' ) . '</a></p>', |
| 176 | 'wp-2fa_wizard_cancel' => '<h3>' . \esc_html__( 'Are you sure?', 'wp-2fa' ) . '</h3><p>' . \esc_html__( 'Any unsaved changes will be lost!', 'wp-2fa' ) . '</p>', |
| 177 | '2fa_wizard_logout' => '<h3>' . \esc_html__( 'Are you sure?', 'wp-2fa' ) . '</h3><p>' . \esc_html__( 'That will terminate your session and log you out of the WP.', 'wp-2fa' ) . '</p>', |
| 178 | |
| 179 | 'custom_css' => '', |
| 180 | 'login_custom_css' => '', |
| 181 | 'logo-code-page' => '', |
| 182 | 'logo-code-page-url' => '', |
| 183 | 'disable_login_css' => '', |
| 184 | 'login-to-view-area' => '<p>' . \esc_html__( 'You must be logged in to view this page. {login_url}', 'wp-2fa' ) . '</p>', |
| 185 | |
| 186 | 'user-profile-form-preamble-title' => \esc_html__( 'Two-factor authentication settings', 'wp-2fa' ), |
| 187 | 'user-profile-form-preamble-desc' => \esc_html__( 'Add two-factor authentication to strengthen the security of your user account.', 'wp-2fa' ), |
| 188 | 'use_custom_2fa_message' => 'use-defaults', |
| 189 | ); |
| 190 | |
| 191 | /** |
| 192 | * Gives the ability to filter the default settings array of the plugin |
| 193 | * |
| 194 | * @param array $settings - The array with all the default settings. |
| 195 | * |
| 196 | * @since 2.0.0 |
| 197 | */ |
| 198 | self::$default_settings = \apply_filters( WP_2FA_PREFIX . 'white_label_default_settings', self::$default_settings ); |
| 199 | } |
| 200 | |
| 201 | return self::$default_settings; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Array with all the plugin default settings. |
| 206 | * |
| 207 | * @param array $settings - Currently collected settings. |
| 208 | * |
| 209 | * @return array |
| 210 | * |
| 211 | * @since 3.0.0 |
| 212 | */ |
| 213 | public static function merge_settings( $settings ) { |
| 214 | |
| 215 | $settings[ WP_2FA_WHITE_LABEL_SETTINGS_NAME ] = self::get_default_settings(); |
| 216 | |
| 217 | return $settings; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 |