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

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

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