PluginProbe
Magic Login – Magic Link & Passwordless Authentication for WordPress – Login Without Password / 2.1
Magic Login – Magic Link & Passwordless Authentication for WordPress – Login Without Password v2.1
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.1, at includes/shortcode.php

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