class-ajax-helper.php
5 days ago
class-classes-helper.php
5 days ago
class-email-templates.php
5 days ago
class-file-writer.php
5 days ago
class-methods-helper.php
5 days ago
class-php-helper.php
5 days ago
class-sms-templates.php
5 days ago
class-user-helper.php
5 days ago
class-wp-helper.php
5 days ago
index.php
5 days ago
class-sms-templates.php
179 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Responsible for the User's operations. |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage helpers |
| 7 | * |
| 8 | * @since 3.1.1.2 |
| 9 | * |
| 10 | * @copyright 2026 Melapress |
| 11 | * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 |
| 12 | * |
| 13 | * @see https://wordpress.org/plugins/wp-2fa/ |
| 14 | */ |
| 15 | |
| 16 | declare(strict_types=1); |
| 17 | |
| 18 | namespace WP2FA\Admin\Helpers; |
| 19 | |
| 20 | use WP2FA\WP2FA; |
| 21 | use WP2FA\Utils\Settings_Utils; |
| 22 | |
| 23 | // Exit if accessed directly. |
| 24 | if ( ! defined( 'ABSPATH' ) ) { |
| 25 | exit; |
| 26 | } |
| 27 | |
| 28 | if ( ! class_exists( '\WP2FA\Admin\Helpers\SMS_Templates' ) ) { |
| 29 | /** |
| 30 | * Responsible for the SMS templates. |
| 31 | */ |
| 32 | class SMS_Templates { |
| 33 | |
| 34 | /** |
| 35 | * Local static cache for SMS template settings. |
| 36 | * |
| 37 | * @var array |
| 38 | */ |
| 39 | protected static $wp_2fa_sms_templates = null; |
| 40 | |
| 41 | /** |
| 42 | * Function to return the SMS templates. |
| 43 | * |
| 44 | * @param string $sms_template Optional parameter to return only a specific SMS template. |
| 45 | * |
| 46 | * @return array Array with the SMS templates. |
| 47 | * |
| 48 | * @since 3.1.1.2 |
| 49 | */ |
| 50 | public static function get_sms_templates( $sms_template = '' ): array { |
| 51 | // Mapping of the SMS names to the templates. We must unify that. |
| 52 | $internal_mapping = array( |
| 53 | 'default-twilio-registration-text' => array( |
| 54 | 'body' => 'default-twilio-registration-text', |
| 55 | 'title' => __( 'SMS registration text', 'wp-2fa' ), |
| 56 | 'description' => __( 'This is the template for the SMS message sent during the 2FA setup for users to confirm their mobile number.', 'wp-2fa' ), |
| 57 | ), |
| 58 | 'default-twilio-code-text' => array( |
| 59 | 'body' => 'default-twilio-code-text', |
| 60 | 'title' => __( 'SMS code text', 'wp-2fa' ), |
| 61 | 'description' => __( 'This is the template for the SMS message with the one-time code sent to users when authenticating.', 'wp-2fa' ), |
| 62 | ), |
| 63 | ); |
| 64 | |
| 65 | if ( isset( $sms_template ) && ! empty( $sms_template ) && array_key_exists( $sms_template, $internal_mapping ) ) { |
| 66 | return array( |
| 67 | 'body' => self::get_wp2fa_sms_templates( $internal_mapping[ $sms_template ]['body'] ), |
| 68 | 'title' => $internal_mapping[ $sms_template ]['title'], |
| 69 | 'description' => $internal_mapping[ $sms_template ]['description'], |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | foreach ( $internal_mapping as $template_key => $template_data ) { |
| 74 | |
| 75 | $template = self::get_wp2fa_sms_templates( $template_key ); |
| 76 | if ( \is_array( $template ) && isset( $template['body'] ) && ! empty( $template['body'] ) ) { |
| 77 | $body = (string) $template['body']; |
| 78 | } else { |
| 79 | $body = (string) WP2FA::get_wp2fa_white_label_setting( $template_key, true ); |
| 80 | } |
| 81 | |
| 82 | $internal_mapping[ $template_key ]['body'] = $body; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Add an option for external providers to implement their own sms template settings for the settings tab. |
| 87 | * |
| 88 | * The structure of the array should be the same as the internal mapping, with the addition of the 'title' and 'description' keys for each template, which will be used in the settings page to display the template name and description. |
| 89 | * - key - unique identifier for the template, used for filtering and retrieving the template. |
| 90 | * - body - the setting name for the sms body in the database. |
| 91 | * - title - the title of the sms template, used for display purposes. |
| 92 | * - description - the description of the sms template, used for display purposes. |
| 93 | * |
| 94 | * @param array $result - The array with all the sms templates. |
| 95 | * |
| 96 | * @since 3.1.1.2 |
| 97 | */ |
| 98 | $result = \apply_filters( WP_2FA_PREFIX . 'sms_templates', $internal_mapping ); |
| 99 | |
| 100 | return $result; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Util function to grab SMS settings or apply defaults if no settings are saved into the db. |
| 105 | * |
| 106 | * @param string $setting_name Settings to grab value of. |
| 107 | * |
| 108 | * @since 2.0.0 |
| 109 | */ |
| 110 | public static function get_wp2fa_sms_templates( $setting_name = '' ) { |
| 111 | |
| 112 | if ( is_null( self::$wp_2fa_sms_templates ) ) { |
| 113 | self::$wp_2fa_sms_templates = Settings_Utils::get_option( WP_2FA_EMAIL_SETTINGS_NAME ); |
| 114 | |
| 115 | if ( false === self::$wp_2fa_sms_templates || ! is_array( self::$wp_2fa_sms_templates ) ) { |
| 116 | self::$wp_2fa_sms_templates = self::get_sms_templates(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // If we have no setting name, return what ever is saved. |
| 121 | if ( empty( $setting_name ) ) { |
| 122 | return self::$wp_2fa_sms_templates; |
| 123 | } |
| 124 | |
| 125 | // If we have a saved setting, return it. |
| 126 | if ( $setting_name && isset( self::$wp_2fa_sms_templates[ $setting_name ] ) ) { |
| 127 | return self::$wp_2fa_sms_templates[ $setting_name ]; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get the SMS template tags with their replacements. |
| 133 | * |
| 134 | * @param bool $only_tags - Whether to return only the tags or the tags with their replacements. |
| 135 | * @param int|string $user_id User id, if its needed. |
| 136 | * @param string $token Login code, if its needed.. |
| 137 | * |
| 138 | * @return array The SMS template tags with their replacements or only the tags. |
| 139 | * |
| 140 | * @since 3.1.1.2 |
| 141 | */ |
| 142 | public static function get_sms_template_tags( bool $only_tags = false, $user_id = '', $token = '' ) { |
| 143 | |
| 144 | if ( $only_tags ) { |
| 145 | return array( |
| 146 | '{site_url}', |
| 147 | '{site_name}', |
| 148 | '{user_login_name}', |
| 149 | '{user_login_code}', |
| 150 | '{domain}', |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | // Setup user data. |
| 155 | if ( isset( $user_id ) && ! empty( $user_id ) ) { |
| 156 | $user = \get_userdata( intval( $user_id ) ); |
| 157 | } else { |
| 158 | $user = \wp_get_current_user(); |
| 159 | } |
| 160 | |
| 161 | // Setup token. |
| 162 | $token = trim( (string) $token ); |
| 163 | if ( isset( $token ) && ! empty( $token ) ) { |
| 164 | $login_code = $token; |
| 165 | } else { |
| 166 | $login_code = ''; |
| 167 | } |
| 168 | |
| 169 | return array( |
| 170 | '{site_url}' => \esc_url( \get_bloginfo( 'url' ) ), |
| 171 | '{site_name}' => \sanitize_text_field( \get_bloginfo( 'name' ) ), |
| 172 | '{user_login_name}' => \sanitize_text_field( $user->user_login ), |
| 173 | '{user_login_code}' => $login_code, |
| 174 | '{domain}' => \esc_url( WP_Helper::wp_get_high_level_domain( \get_bloginfo( 'url' ) ) ), |
| 175 | ); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 |