PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.2.1
WP 2FA – Two-factor authentication for WordPress v2.2.1
4.1.0 4.0.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 / Shortcodes / class-shortcodes.php
wp-2fa / includes / classes / Shortcodes Last commit date
class-shortcodes.php 4 years ago index.php 5 years ago
class-shortcodes.php
207 lines
1 <?php
2 /**
3 * Responsible for rendering the short codes.
4 *
5 * @package wp2fa
6 * @subpackage short-codes
7 * @copyright 2021 WP White Security
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\Shortcodes;
13
14 use \WP2FA\WP2FA as WP2FA;
15 use \WP2FA\Core as Core;
16 use \WP2FA\Admin\User_Profile as User_Profile;
17 use \WP2FA\Admin\User_Notices as User_Notices;
18 use WP2FA\Admin\Controllers\Settings;
19
20 /**
21 * Class for rendering shortcodes.
22 */
23 class Shortcodes {
24
25 /**
26 * Constructor.
27 */
28 public function __construct() {
29 add_shortcode( 'wp-2fa-setup-form', array( $this, 'user_setup_2fa_form' ) );
30 add_shortcode( 'wp-2fa-setup-notice', array( $this, 'user_setup_2fa_notice' ) );
31 add_action( 'wp_enqueue_scripts', array( $this, 'register_2fa_shortcode_scripts' ) );
32 }
33
34 /**
35 * Register scripts and styles.
36 */
37 public function register_2fa_shortcode_scripts() {
38 // Add our front end stuff, which we only want to load when the shortcode is present.
39 wp_register_script( 'wp_2fa_frontend_scripts', Core\script_url( 'wp-2fa', 'admin' ), array( 'jquery', 'wp_2fa_micro_modals' ), WP_2FA_VERSION, true );
40 wp_register_script( 'wp_2fa_micro_modals', Core\script_url( 'micromodal', 'admin' ), array(), WP_2FA_VERSION, true );
41 wp_register_style( 'wp_2fa_styles', Core\style_url( 'styles', 'frontend' ), array(), WP_2FA_VERSION );
42
43 $data_array = array(
44 'ajaxURL' => admin_url( 'admin-ajax.php' ),
45 'roles' => WP2FA::wp_2fa_get_roles(),
46 'nonce' => wp_create_nonce( 'wp-2fa-settings-nonce' ),
47 'codesPreamble' => esc_html__( 'These are the 2FA backup codes for the user', 'wp-2fa' ),
48 'readyText' => esc_html__( 'I\'m ready', 'wp-2fa' ),
49 'codeReSentText' => esc_html__( 'New code sent', 'wp-2fa' ),
50 'allDoneHeading' => esc_html__( 'All done.', 'wp-2fa' ),
51 'allDoneText' => esc_html__( 'Your login just got more secure.', 'wp-2fa' ),
52 'closeWizard' => esc_html__( 'Close Wizard', 'wp-2fa' ),
53 'invalidEmail' => esc_html__( 'Please use a valid email address', 'wp-2fa' ),
54 );
55 wp_localize_script( 'wp_2fa_frontend_scripts', 'wp2faData', $data_array );
56
57 /**
58 * Fires when the FE shortcode scripts are registered.
59 *
60 * @param bool $shortcodes - True if called from the short codes method.
61 *
62 * @since 2.2.0
63 */
64 \do_action( WP_2FA_PREFIX . 'shortcode_scripts', true );
65 }
66
67 /**
68 * Output setup form.
69 *
70 * @param array $atts - Array with the attributes passed to shortcode.
71 *
72 * @return string
73 */
74 public function user_setup_2fa_form( $atts ) {
75
76 /** Shortcode redirect_after is supported, with which the user can override all other settings */
77 extract( // phpcs:ignore
78 shortcode_atts(
79 array(
80 'show_preamble' => 'true',
81 'redirect_after' => '',
82 ),
83 $atts
84 )
85 );
86
87 if ( is_user_logged_in() ) {
88 wp_enqueue_script( 'wp_2fa_frontend_scripts' );
89 wp_enqueue_style( 'wp_2fa_styles' );
90
91 $data_array = array(
92 'ajaxURL' => admin_url( 'admin-ajax.php' ),
93 'roles' => WP2FA::wp_2fa_get_roles(),
94 'nonce' => wp_create_nonce( 'wp-2fa-settings-nonce' ),
95 'codesPreamble' => esc_html__( 'These are the 2FA backup codes for the user', 'wp-2fa' ),
96 'readyText' => esc_html__( 'I\'m ready', 'wp-2fa' ),
97 'codeReSentText' => esc_html__( 'New code sent', 'wp-2fa' ),
98 'allDoneHeading' => esc_html__( 'All done.', 'wp-2fa' ),
99 'allDoneText' => esc_html__( 'Your login just got more secure.', 'wp-2fa' ),
100 'closeWizard' => esc_html__( 'Close Wizard', 'wp-2fa' ),
101 );
102 wp_localize_script( 'wp_2fa_frontend_scripts', 'wp2faData', $data_array );
103
104 $data_array = array(
105 'ajaxURL' => admin_url( 'admin-ajax.php' ),
106 'nonce' => wp_create_nonce( 'wp2fa-verify-wizard-page' ),
107 'codesPreamble' => esc_html__( 'These are the 2FA backup codes for the user', 'wp-2fa' ),
108 'readyText' => esc_html__( 'I\'m ready', 'wp-2fa' ),
109 'codeReSentText' => esc_html__( 'New code sent', 'wp-2fa' ),
110 'invalidEmail' => esc_html__( 'Please use a valid email address', 'wp-2fa' ),
111 );
112
113 $role = array_key_first( WP2FA::wp_2fa_get_roles() );
114 $redirect_page = Settings::get_role_or_default_setting( 'redirect-user-custom-page-global', 'current', $role );
115 $data_array['redirectToUrl'] = ( '' !== trim( $redirect_page ) ) ? \trailingslashit( get_site_url() ) . $redirect_page : '';
116 // Check and override if custom redirect page is selected and custom redirect is set.
117 if (
118 'yes' === Settings::get_role_or_default_setting( 'create-custom-user-page', 'current', $role ) ||
119 'yes' === Settings::get_role_or_default_setting( 'create-custom-user-page' ) ) {
120 if (
121 '' !== trim( Settings::get_role_or_default_setting( 'redirect-user-custom-page', 'current', $role ) ) ||
122 '' !== trim( Settings::get_role_or_default_setting( 'redirect-user-custom-page' ) ) ) {
123 if ( 'yes' === Settings::get_role_or_default_setting( 'create-custom-user-page', 'current', $role ) ) {
124 $data_array['redirectToUrl'] = trailingslashit( get_site_url() ) . Settings::get_role_or_default_setting( 'redirect-user-custom-page', 'current', $role );
125 } else {
126 $data_array['redirectToUrl'] = trailingslashit( get_site_url() ) . Settings::get_role_or_default_setting( 'redirect-user-custom-page' );
127 }
128 }
129 }
130
131 // Check for shortcode parameter - if one is present use it to redirect the user - highest priority.
132 if ( isset( $redirect_after ) && ! empty( $redirect_after ) ) {
133
134 $data_array['redirectToUrl'] = trailingslashit( get_site_url() ) . \urlencode( $redirect_after );
135 } elseif ( isset( $_GET['return'] ) && ! empty( $_GET['return'] ) ) {
136
137 $data_array['redirectToUrl'] = trailingslashit( get_site_url() ) . strip_tags( \urlencode( $_GET['return'] ) ); // phpcs:ignore
138 }
139 wp_localize_script( 'wp_2fa_frontend_scripts', 'wp2faWizardData', $data_array );
140
141 $forms = new User_Profile();
142 ob_start();
143 echo '<form id="your-profile" class="wp-2fa-configuration-form">';
144 $forms->inline_2fa_profile_form( 'output_shortcode', $show_preamble );
145 echo '</form>';
146 $content = ob_get_contents();
147 ob_end_clean();
148 return $content;
149 } elseif ( ! is_admin() && ! is_user_logged_in() ) {
150 $new_page_id = WP2FA::get_wp2fa_setting( 'custom-user-page-id' );
151 $redirect_to = ! empty( $new_page_id ) ? get_permalink( $new_page_id ) : get_home_url();
152 ob_start();
153 echo '<p>' . esc_html__( 'You must be logged in to view this page.', 'wp-2fa' ) . ' <a href="' . esc_url( wp_login_url( $redirect_to ) ) . '">' . esc_html__( 'Login here.', 'wp-2fa' ) . '</a></p>';
154 $content = ob_get_contents();
155 ob_end_clean();
156 return $content;
157 }
158 }
159
160 /**
161 * Output setup nag.
162 *
163 * @param array $atts - Array with the attributes passed to shortcode.
164 *
165 * @return string
166 */
167 public function user_setup_2fa_notice( $atts ) {
168 extract( // phpcs:ignore
169 shortcode_atts(
170 array(
171 'configure_2fa_url' => '',
172 ),
173 $atts
174 )
175 );
176 $notice = new User_Notices();
177
178 if ( ! is_admin() && is_user_logged_in() ) {
179 wp_enqueue_script( 'wp_2fa_micro_modals' );
180 wp_enqueue_script( 'wp_2fa_frontend_scripts' );
181 wp_enqueue_style( 'wp_2fa_styles' );
182
183 $data_array = array(
184 'ajaxURL' => admin_url( 'admin-ajax.php' ),
185 'roles' => WP2FA::wp_2fa_get_roles(),
186 'nonce' => wp_create_nonce( 'wp-2fa-settings-nonce' ),
187 'codesPreamble' => esc_html__( 'These are the 2FA backup codes for the user', 'wp-2fa' ),
188 'readyText' => esc_html__( 'I\'m ready', 'wp-2fa' ),
189 'codeReSentText' => esc_html__( 'New code sent', 'wp-2fa' ),
190 'allDoneHeading' => esc_html__( 'All done.', 'wp-2fa' ),
191 'allDoneText' => esc_html__( 'Your login just got more secure.', 'wp-2fa' ),
192 'closeWizard' => esc_html__( 'Close Wizard', 'wp-2fa' ),
193 );
194 wp_localize_script( 'wp_2fa_frontend_scripts', 'wp2faData', $data_array );
195
196 ob_start();
197 $notice->user_setup_2fa_nag( 'output_shortcode', $configure_2fa_url );
198 $content = ob_get_contents();
199 ob_end_clean();
200
201 return $content;
202 }
203
204 return '';
205 }
206 }
207