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 / block.php

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

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