PluginProbe
Magic Login – Magic Link & Passwordless Authentication for WordPress – Login Without Password / 2.4
Magic Login – Magic Link & Passwordless Authentication for WordPress – Login Without Password v2.4
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 / block.php

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

214 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Block
4 *
5 * @package MagicLogin
6 */
7
8 namespace MagicLogin\Block;
9
10 // phpcs:disable WordPress.WP.I18n.MissingTranslatorsComment
11 use MagicLogin\CodeLogin;
12 use MagicLogin\LoginManager;
13 use function MagicLogin\Core\script_url;
14 use function MagicLogin\Login\process_login_request;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit; // Exit if accessed directly.
18 }
19 /**
20 * Default setup routine
21 *
22 * @return void
23 */
24 function setup() {
25 add_action( 'init', __NAMESPACE__ . '\\register_blocks' );
26 }
27
28 /**
29 * Register blocks
30 *
31 * @since 1.2
32 */
33 function register_blocks() {
34 if ( ! function_exists( 'register_block_type' ) ) {
35 return;
36 }
37
38 wp_register_script(
39 'magic-login-block-editor',
40 script_url( 'block-editor', 'admin' ),
41 [
42 'wp-i18n',
43 'wp-components',
44 'wp-element',
45 'wp-server-side-render',
46 ],
47 MAGIC_LOGIN_VERSION,
48 true
49 );
50
51 wp_set_script_translations(
52 'magic-login-block-editor',
53 'magic-login',
54 plugin_dir_path( MAGIC_LOGIN_PLUGIN_FILE ) . 'languages'
55 );
56
57 $deps = is_admin() ? [ 'wp-edit-blocks' ] : [];
58
59 wp_register_style(
60 'magic-login-login-block',
61 MAGIC_LOGIN_URL . 'dist/css/login-block-style.css',
62 $deps,
63 MAGIC_LOGIN_VERSION
64 );
65
66 register_block_type(
67 'magic-login/login-block',
68 [
69 'attributes' => [
70 'title' => [
71 'type' => 'string',
72 'default' => __( 'Login with Email', 'magic-login' ),
73 ],
74 'description' => [
75 'type' => 'string',
76 'default' => __( 'Please enter your username or email address. You will receive an email message to log in.', 'magic-login' ),
77 ],
78 'loginLabel' => [
79 'type' => 'string',
80 'default' => __( 'Username or Email Address', 'magic-login' ),
81 ],
82 'buttonLabel' => [
83 'type' => 'string',
84 'default' => __( 'Send me the link', 'magic-login' ),
85 ],
86 'redirectTo' => [
87 'type' => 'string',
88 ],
89 'hideLoggedIn' => [
90 'type' => 'boolean',
91 'default' => true,
92 ],
93 'hideFormAfterSubmit' => [
94 'type' => 'boolean',
95 'default' => true,
96 ],
97 'cancelRedirection' => [
98 'type' => 'boolean',
99 'default' => false,
100 ],
101 ],
102 'editor_script' => 'magic-login-block-editor',
103 'editor_style' => 'magic-login-login-block',
104 'style' => 'magic-login-login-block',
105 'render_callback' => __NAMESPACE__ . '\\render_login_block',
106 ]
107 );
108 }
109
110
111 /**
112 * Render Login Block
113 *
114 * @param array $args Block Attributes
115 *
116 * @return false|string|void
117 * @since 1.2
118 */
119 function render_login_block( $args ) {
120
121 $settings = \MagicLogin\Utils\get_settings();
122 $add_redirection_field = empty( $settings['enable_login_redirection'] ) || empty( $settings['enforce_redirection_rules'] );
123
124 if ( $settings['enable_ajax'] ) {
125 wp_enqueue_script( 'magic-login-frontend', MAGIC_LOGIN_URL . 'dist/js/frontend.js', [ 'jquery' ], MAGIC_LOGIN_VERSION, true );
126 }
127
128 $form_action = apply_filters( 'magic_login_login_block_form_action', '' );
129
130 $class = 'magic-login-login-block';
131 if ( ! empty( $args['align'] ) ) {
132 $class .= ' align' . $args['align'];
133 }
134
135 if ( ! empty( $args['className'] ) ) {
136 $class .= ' ' . esc_attr( $args['className'] );
137 }
138
139 if ( empty( $args['redirectTo'] ) ) {
140 $args['redirectTo'] = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // phpcs:ignore
141 }
142
143 if ( ! defined( 'REST_REQUEST' ) && ! is_admin() && is_user_logged_in() && $args['hideLoggedIn'] ) { // already logged-in dont show
144 return;
145 }
146
147 ob_start();
148 $login_request = LoginManager::process_login_request();
149 if ( false === $login_request['show_form'] && ! $args['hideFormAfterSubmit'] ) {
150 $login_request['show_form'] = true;
151 }
152
153 ?>
154
155 <?php
156 $error_messages = '';
157 $login_errors = $login_request['errors'];
158 // error messages
159 if ( ! empty( $login_errors ) && is_wp_error( $login_errors ) && $login_errors->has_errors() ) {
160 foreach ( $login_errors->get_error_codes() as $code ) {
161 foreach ( $login_errors->get_error_messages( $code ) as $message ) {
162 $error_messages .= $message . "<br />\n";
163 }
164 }
165 }
166 ?>
167
168 <div id="magic-login-login-block" class="<?php echo esc_attr( $class ); ?>">
169 <?php if ( ! empty( $args['title'] ) ) : ?>
170 <h2 id="magic-login-block-title"><?php echo esc_html( $args['title'] ); ?></h2>
171 <?php endif; ?>
172 <div class="magic-login-form-header">
173 <?php
174 if ( ! empty( $error_messages ) ) :
175 printf( '<div class="magic_login_block_login_error">%s</div>', wp_kses_post( $error_messages ) );
176 endif;
177 ?>
178
179 <?php if ( $login_request['is_processed'] ) : ?>
180 <?php echo wp_kses_post( $login_request['info'] ); ?>
181 <?php endif; ?>
182
183 <?php if ( ! empty( $args['description'] ) && $login_request['show_form'] ) : ?>
184 <p class="magic-login-block-description"><?php echo esc_html( $args['description'] ); ?></p>
185 <?php endif; ?>
186 </div>
187 <?php if ( $login_request['code_login'] ) : ?>
188 <?php CodeLogin::code_form(); ?>
189 <?php elseif ( $login_request['show_form'] ) : ?>
190 <form name="magicloginform" class="block-login-form" id="magicloginform" action="<?php echo esc_url( $form_action ); ?>" method="post" autocomplete="off" data-ajax-url="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" data-ajax-spinner="<?php echo esc_url( get_admin_url() . 'images/spinner.gif' ); ?>" data-ajax-sending-msg="<?php esc_attr_e( 'Sending...', 'magic-login' ); ?>">
191 <div class="magicloginform-inner">
192 <?php if ( ! empty( $args['loginLabel'] ) ) : ?>
193 <label for="user_login"><?php echo esc_html( $args['loginLabel'] ); ?></label>
194 <?php endif; ?>
195
196 <input type="text" name="log" id="user_login" class="input" value="" size="20" autocapitalize="off" autocomplete="username" required />
197 <?php do_action( 'magic_login_form' ); ?>
198 <?php if ( ! empty( $args['buttonLabel'] ) ) : ?>
199 <input type="submit" name="wp-submit" id="wp-submit" class="magic-login-submit button button-primary button-large" value="<?php echo esc_attr( $args['buttonLabel'] ); ?>" />
200 <?php endif; ?>
201
202 <?php if ( ! empty( $args['redirectTo'] ) && ! $args['cancelRedirection'] && $add_redirection_field ) : ?>
203 <input type="hidden" name="redirect_to" value="<?php echo esc_url( $args['redirectTo'] ); ?>" />
204 <?php endif; ?>
205 <input type="hidden" name="testcookie" value="1" />
206 </div>
207 </form>
208 <?php endif; ?>
209 </div>
210
211 <?php
212 return ob_get_clean();
213 }
214