PluginProbe
Magic Login – Magic Link & Passwordless Authentication for WordPress – Login Without Password / trunk
Magic Login – Magic Link & Passwordless Authentication for WordPress – Login Without Password vtrunk
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 trunk, at includes/block.php

215 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 \MagicLogin\Utils\localize_frontend_script();
127 }
128
129 $form_action = apply_filters( 'magic_login_login_block_form_action', '' );
130
131 $class = 'magic-login-login-block';
132 if ( ! empty( $args['align'] ) ) {
133 $class .= ' align' . $args['align'];
134 }
135
136 if ( ! empty( $args['className'] ) ) {
137 $class .= ' ' . esc_attr( $args['className'] );
138 }
139
140 if ( empty( $args['redirectTo'] ) ) {
141 $args['redirectTo'] = home_url( add_query_arg( null, null ) ); // phpcs:ignore
142 }
143
144 if ( ! defined( 'REST_REQUEST' ) && ! is_admin() && is_user_logged_in() && $args['hideLoggedIn'] ) { // already logged-in dont show
145 return;
146 }
147
148 ob_start();
149 $login_request = LoginManager::process_login_request();
150 if ( false === $login_request['show_form'] && ! $args['hideFormAfterSubmit'] ) {
151 $login_request['show_form'] = true;
152 }
153
154 ?>
155
156 <?php
157 $error_messages = '';
158 $login_errors = $login_request['errors'];
159 // error messages
160 if ( ! empty( $login_errors ) && is_wp_error( $login_errors ) && $login_errors->has_errors() ) {
161 foreach ( $login_errors->get_error_codes() as $code ) {
162 foreach ( $login_errors->get_error_messages( $code ) as $message ) {
163 $error_messages .= $message . "<br />\n";
164 }
165 }
166 }
167 ?>
168
169 <div id="magic-login-login-block" class="<?php echo esc_attr( $class ); ?>">
170 <?php if ( ! empty( $args['title'] ) ) : ?>
171 <h2 id="magic-login-block-title"><?php echo esc_html( $args['title'] ); ?></h2>
172 <?php endif; ?>
173 <div class="magic-login-form-header">
174 <?php
175 if ( ! empty( $error_messages ) ) :
176 printf( '<div class="magic_login_block_login_error">%s</div>', wp_kses_post( $error_messages ) );
177 endif;
178 ?>
179
180 <?php if ( $login_request['is_processed'] ) : ?>
181 <?php echo wp_kses_post( $login_request['info'] ); ?>
182 <?php endif; ?>
183
184 <?php if ( ! empty( $args['description'] ) && $login_request['show_form'] ) : ?>
185 <p class="magic-login-block-description"><?php echo esc_html( $args['description'] ); ?></p>
186 <?php endif; ?>
187 </div>
188 <?php if ( $login_request['code_login'] ) : ?>
189 <?php CodeLogin::code_form(); ?>
190 <?php elseif ( $login_request['show_form'] ) : ?>
191 <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' ); ?>">
192 <div class="magicloginform-inner">
193 <?php if ( ! empty( $args['loginLabel'] ) ) : ?>
194 <label for="user_login"><?php echo esc_html( $args['loginLabel'] ); ?></label>
195 <?php endif; ?>
196
197 <input type="text" name="log" id="user_login" class="input" value="" size="20" autocapitalize="off" autocomplete="username" required />
198 <?php do_action( 'magic_login_form' ); ?>
199 <?php if ( ! empty( $args['buttonLabel'] ) ) : ?>
200 <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'] ); ?>" />
201 <?php endif; ?>
202
203 <?php if ( ! empty( $args['redirectTo'] ) && ! $args['cancelRedirection'] && $add_redirection_field ) : ?>
204 <input type="hidden" name="redirect_to" value="<?php echo esc_url( $args['redirectTo'] ); ?>" />
205 <?php endif; ?>
206 <input type="hidden" name="testcookie" value="1" />
207 </div>
208 </form>
209 <?php endif; ?>
210 </div>
211
212 <?php
213 return ob_get_clean();
214 }
215