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