PluginProbe
Magic Login – Magic Link & Passwordless Authentication for WordPress – Login Without Password / 2.6.3
Magic Login – Magic Link & Passwordless Authentication for WordPress – Login Without Password v2.6.3
2.8.1 2.1 2.1.1 2.1.2 2.1.3 2.2 2.3 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.4 2.4.1 2.4.2 2.5 2.5.1 2.6 2.6.1 2.6.2 2.6.3 2.7 2.7.1 2.8 trunk All 48 releases
magic-login / includes / shortcode.php

shortcode.php in Magic Login – Magic Link & Passwordless Authentication for WordPress – Login Without Password 2.6.3, at includes/shortcode.php

174 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Shortcode functionality
4 *
5 * @package MagicLogin
6 */
7
8 namespace MagicLogin\Shortcode;
9
10 use MagicLogin\CodeLogin;
11 use MagicLogin\LoginManager;
12 use function MagicLogin\Core\style_url;
13 use function MagicLogin\Login\process_login_request;
14
15 // phpcs:disable WordPress.WhiteSpace.PrecisionAlignment.Found
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit; // Exit if accessed directly.
18 }
19
20 /**
21 * Default setup routine
22 *
23 * @return void
24 * @since 1.1
25 */
26 function setup() {
27 add_shortcode( 'magic_login_form', __NAMESPACE__ . '\\shortcode_login_form' );
28 add_filter( 'magic_login_redirect', __NAMESPACE__ . '\\maybe_shortcode_redirect', 99, 2 );
29 }
30
31 /**
32 * This form needs to be compatible with various themes as much as possible
33 * not like the form in login.php which designed to fit on the standard login screen
34 *
35 * @param array $shortcode_atts Shortcode Attributes
36 *
37 * @return string|void
38 */
39 function shortcode_login_form( $shortcode_atts ) {
40 $atts = shortcode_atts(
41 [
42 'redirect_to' => home_url( add_query_arg( null, null ) ), // phpcs:ignore
43 'hide_logged_in' => true,
44 'error_message' => '',
45 'info_message' => '',
46 'success_message' => '',
47 'label' => '',
48 'button_text' => '',
49 'class' => '',
50 ],
51 $shortcode_atts,
52 'magic_login_form'
53 );
54
55 // accept on/off yes/no true/false
56 $hide_logged_in = filter_var( $atts['hide_logged_in'], FILTER_VALIDATE_BOOLEAN );
57
58 if ( is_user_logged_in() && $hide_logged_in && ! is_preview() ) {
59 return;
60 }
61
62 ob_start();
63
64 wp_enqueue_style(
65 'magic_login_shortcode',
66 style_url( 'shortcode-style', 'shortcode' ),
67 [],
68 MAGIC_LOGIN_VERSION
69 );
70
71 $settings = \MagicLogin\Utils\get_settings();
72
73 if ( $settings['enable_ajax'] ) {
74 wp_enqueue_script( 'magic-login-frontend', MAGIC_LOGIN_URL . 'dist/js/frontend.js', [ 'jquery' ], MAGIC_LOGIN_VERSION, true );
75 }
76
77 $add_redirection_field = empty( $settings['enable_login_redirection'] ) || empty( $settings['enforce_redirection_rules'] );
78
79 $form_action = apply_filters( 'magic_login_shortcode_form_action', '' );
80
81 $login_request = LoginManager::process_login_request( $atts );
82 $button_text = ! empty( $atts['button_text'] ) ? $atts['button_text'] : esc_html__( 'Send me the link', 'magic-login' );
83 ?>
84 <div id="magic-login-shortcode">
85 <div class="magic-login-form-header">
86 <?php
87 $login_errors = $login_request['errors'];
88
89 // error messages
90 if ( ! empty( $login_errors ) && is_wp_error( $login_errors ) && $login_errors->has_errors() ) {
91 $error_messages = '';
92
93 foreach ( $login_errors->get_error_codes() as $code ) {
94 foreach ( $login_errors->get_error_messages( $code ) as $message ) {
95 $error_messages .= $message . "<br />\n";
96 }
97 }
98
99 if ( ! empty( $error_messages ) ) {
100 printf( '<div id="login_error">%s</div>', wp_kses_post( $error_messages ) );
101 }
102 }
103
104 // display info messages
105 if ( ! empty( $login_request['info'] ) ) {
106 echo wp_kses_post( $login_request['info'] );
107 }
108 ?>
109 </div>
110 <?php if ( $login_request['code_login'] ) : ?>
111 <?php CodeLogin::code_form(); ?>
112 <?php elseif ( $login_request['show_form'] ) : ?>
113 <form name="magicloginform"
114 class="magic-login-inline-login-form <?php echo esc_attr( $atts['class'] ); ?>"
115 id="magicloginform"
116 action="<?php echo esc_url( $form_action ); ?>"
117 method="post"
118 autocomplete="off"
119 data-ajax-url="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>"
120 data-ajax-spinner="<?php echo esc_url( get_admin_url() . 'images/spinner.gif' ); ?>"
121 data-ajax-sending-msg="<?php esc_attr_e( 'Sending...', 'magic-login' ); ?>"
122 >
123
124 <?php if ( ! empty( $atts['label'] ) ) : ?>
125 <label for="user_login"><?php echo wp_kses_post( $atts['label'] ); ?></label>
126 <?php elseif ( defined( 'MAGIC_LOGIN_USERNAME_ONLY' ) && MAGIC_LOGIN_USERNAME_ONLY ) : ?>
127 <label for="user_login"><?php esc_html_e( 'Username', 'magic-login' ); ?></label>
128 <?php else : ?>
129 <label for="user_login"><?php esc_html_e( 'Username or Email Address', 'magic-login' ); ?></label>
130 <?php endif; ?>
131 <input type="text" name="log" id="user_login" class="input" value="" size="20" autocapitalize="off" autocomplete="username" required />
132 <?php
133
134 /**
135 * Fires following the 'email' field in the login form.
136 *
137 * @since 1.0
138 */
139 do_action( 'magic_login_form' );
140
141 ?>
142 <?php if ( $add_redirection_field ) : ?>
143 <input type="hidden" name="redirect_to" value="<?php echo esc_url( $atts['redirect_to'] ); ?>" />
144 <?php endif; ?>
145 <input type="hidden" name="testcookie" value="1" />
146 <?php if ( $settings['enable_ajax'] ) : ?>
147 <input type="hidden" name="messages[info]" value="<?php echo esc_attr( $atts['info_message'] ); ?>" />
148 <input type="hidden" name="messages[error]" value="<?php echo esc_attr( $atts['error_message'] ); ?>" />
149 <input type="hidden" name="messages[success]" value="<?php echo esc_attr( $atts['success_message'] ); ?>" />
150 <?php endif; ?>
151 <input type="submit" name="wp-submit" id="wp-submit" class="magic-login-submit button button-primary button-large" value="<?php echo esc_attr( $button_text ); ?>" />
152 </form>
153 <?php endif; ?>
154 </div>
155 <?php
156 return ob_get_clean();
157 }
158
159 /**
160 * Get magic link redirect url after login
161 *
162 * @param string $redirect_url default redirection url
163 * @param \WP_User $user User object
164 *
165 * @return mixed
166 */
167 function maybe_shortcode_redirect( $redirect_url, $user ) {
168 if ( isset( $_REQUEST['redirect_to'] ) && $_REQUEST['redirect_to'] ) { // phpcs:ignore
169 $redirect_url = esc_url_raw( $_REQUEST['redirect_to'] ); // phpcs:ignore
170 }
171
172 return $redirect_url;
173 }
174