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

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