| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentSupport\App\App; |
| 6 |
use FluentSupport\App\Services\Helper; |
| 7 |
use FluentSupport\Framework\Support\Arr; |
| 8 |
|
| 9 |
class AuthHandler |
| 10 |
{ |
| 11 |
|
| 12 |
public function init() |
| 13 |
{ |
| 14 |
add_shortcode('fluent_support_login', array($this, 'loginForm')); |
| 15 |
add_shortcode('fluent_support_signup', array($this, 'registrationForm')); |
| 16 |
add_shortcode('fluent_support_auth', array($this, 'authForm')); |
| 17 |
} |
| 18 |
|
| 19 |
public function loginForm($attributes) |
| 20 |
{ |
| 21 |
$attributes = $this->getShortcodes($attributes); |
| 22 |
$this->handleAlreadyLoggedIn($attributes); |
| 23 |
|
| 24 |
$app = App::getInstance(); |
| 25 |
$assets = $app['url.assets']; |
| 26 |
wp_enqueue_style('fluent_support_login_style', $assets . 'admin/css/all_public.css'); |
| 27 |
wp_enqueue_script('fluent_support_login_helper', $assets . 'portal/js/login_helper.js'); |
| 28 |
$return = '<div class="fst_login_wrapper">'; |
| 29 |
|
| 30 |
$loginArgs = apply_filters('fluent_support/login_form_args', [ |
| 31 |
'echo' => false, |
| 32 |
'redirect' => Helper::getPortalBaseUrl(), |
| 33 |
'remember' => true, |
| 34 |
'value_remember' => true |
| 35 |
]); |
| 36 |
|
| 37 |
$return .= wp_login_form($loginArgs); |
| 38 |
|
| 39 |
if ($attributes['show-signup'] == 'true') { |
| 40 |
$return .= '<p style="text-align: center">' |
| 41 |
. __('Not registered?', 'fluent-support') |
| 42 |
. ' <a href="#" id="fs_show_signup">' |
| 43 |
. __('Create an Account', 'fluent-support') |
| 44 |
. '</a></p>'; |
| 45 |
} |
| 46 |
|
| 47 |
$return .= '</div>'; |
| 48 |
return $return; |
| 49 |
} |
| 50 |
|
| 51 |
public function registrationForm($attributes) |
| 52 |
{ |
| 53 |
$attributes = $this->getShortcodes($attributes); |
| 54 |
$this->handleAlreadyLoggedIn($attributes); |
| 55 |
|
| 56 |
$app = App::getInstance(); |
| 57 |
$assets = $app['url.assets']; |
| 58 |
wp_enqueue_style('fluent_support_login_style', $assets . 'admin/css/all_public.css'); |
| 59 |
wp_enqueue_script('fluent_support_login_helper', $assets . 'portal/js/login_helper.js'); |
| 60 |
|
| 61 |
wp_localize_script('fluent_support_login_helper', 'fluentSupportPublic', [ |
| 62 |
'signup' => rest_url($app->config->get('app.rest_namespace') . '/' . $app->config->get('app.rest_version')) . '/signup', |
| 63 |
'nonce' => wp_create_nonce('wp_rest'), |
| 64 |
'hide' => $attributes['hide'] |
| 65 |
]); |
| 66 |
|
| 67 |
$registrationFields = static::getSignupFields(); |
| 68 |
|
| 69 |
$hide = $attributes['hide'] == 'true' ? 'hide' : ''; |
| 70 |
$registrationForm = '<div class="fst_registration_wrapper ' . $hide . '"><form id="fstRegistrationForm" class="fs_registration_form" method="post" name="fs_registration_form">'; |
| 71 |
|
| 72 |
foreach ($registrationFields as $fieldName => $registrationField) { |
| 73 |
$registrationForm .= $this->renderField($fieldName, $registrationField); |
| 74 |
} |
| 75 |
|
| 76 |
$registrationForm .= '<input type="hidden" name="__redirect_to" value="' . $attributes['redirect-to'] . '">'; |
| 77 |
$registrationForm .= '<button type="submit" id="fst_submit">' . $this->submitBtnLoadingSvg() . '<span>' . __('Signup', 'fluent-support') . '</span></button>'; |
| 78 |
|
| 79 |
$registrationForm .= '</form>'; |
| 80 |
|
| 81 |
if ($hide) { |
| 82 |
$registrationForm .= '<p style="text-align: center">' |
| 83 |
. __('Already have an account?', 'fluent-support') |
| 84 |
. ' <a href="#" id="fs_show_login">' |
| 85 |
. __('Login', 'fluent-support') |
| 86 |
. '</a></p>'; |
| 87 |
} |
| 88 |
|
| 89 |
$registrationForm .= '</div>'; |
| 90 |
|
| 91 |
return $registrationForm; |
| 92 |
} |
| 93 |
|
| 94 |
public function authForm($attributes) |
| 95 |
{ |
| 96 |
$authForm = '<div class="fst_auth_wrapper">'; |
| 97 |
|
| 98 |
$authForm .= do_shortcode('[fluent_support_login show-signup=true]'); |
| 99 |
$authForm .= do_shortcode('[fluent_support_signup hide=true]'); |
| 100 |
|
| 101 |
$authForm .= '</div>'; |
| 102 |
|
| 103 |
return $authForm; |
| 104 |
} |
| 105 |
|
| 106 |
private function renderField($fieldName, $field) |
| 107 |
{ |
| 108 |
$fieldType = Arr::get($field, 'type'); |
| 109 |
$isRequired = Arr::get($field, 'required'); |
| 110 |
$isRequired = $isRequired ? 'is-required' : ''; |
| 111 |
|
| 112 |
$textTypes = ['text', 'email', 'password']; |
| 113 |
|
| 114 |
$html = '<div class="fst_field_group fst_field_' . $fieldName . '">'; |
| 115 |
if ($label = Arr::get($field, 'label')) { |
| 116 |
$html .= '<div class="fst_field_label ' . $isRequired . '"><label for="' . Arr::get($field, 'id') . '">' . $label . '</label></div>'; |
| 117 |
} |
| 118 |
|
| 119 |
if (in_array($fieldType, $textTypes)) { |
| 120 |
|
| 121 |
$inputAtts = array_filter([ |
| 122 |
'type' => esc_attr($fieldType), |
| 123 |
'id' => esc_attr(Arr::get($field, 'id')), |
| 124 |
'placeholder' => esc_attr(Arr::get($field, 'placeholder')), |
| 125 |
'name' => esc_attr($fieldName) |
| 126 |
]); |
| 127 |
|
| 128 |
$atts = ''; |
| 129 |
|
| 130 |
foreach ($inputAtts as $attKey => $att) { |
| 131 |
$atts .= $attKey . '="' . $att . '" '; |
| 132 |
} |
| 133 |
|
| 134 |
if (Arr::get($field, 'required')) { |
| 135 |
$atts .= 'required'; |
| 136 |
} |
| 137 |
|
| 138 |
$html .= '<div class="fs_input_wrap"><input ' . $atts . '/></div>'; |
| 139 |
} else { |
| 140 |
return ''; |
| 141 |
} |
| 142 |
|
| 143 |
return $html . '</div>'; |
| 144 |
} |
| 145 |
|
| 146 |
public static function getSignupFields() |
| 147 |
{ |
| 148 |
return apply_filters('fluent_support/registration_form_fields', [ |
| 149 |
'first_name' => [ |
| 150 |
'required' => true, |
| 151 |
'type' => 'text', |
| 152 |
'label' => __('First name', 'fluent-support'), |
| 153 |
'id' => 'fst_first_name', |
| 154 |
'placeholder' => __('First name', 'fluent-support') |
| 155 |
], |
| 156 |
'last_name' => [ |
| 157 |
'type' => 'text', |
| 158 |
'label' => __('Last Name', 'fluent-support'), |
| 159 |
'id' => 'fst_last_name', |
| 160 |
'placeholder' => __('Last name', 'fluent-support') |
| 161 |
], |
| 162 |
'username' => [ |
| 163 |
'required' => true, |
| 164 |
'type' => 'text', |
| 165 |
'label' => __('Username', 'fluent-support'), |
| 166 |
'id' => 'fst_username', |
| 167 |
'placeholder' => __('Username', 'fluent-support') |
| 168 |
], |
| 169 |
'email' => [ |
| 170 |
'required' => true, |
| 171 |
'type' => 'email', |
| 172 |
'label' => __('Email Address', 'fluent-support'), |
| 173 |
'id' => 'fst_email', |
| 174 |
'placeholder' => __('Your Email Address', 'fluent-support') |
| 175 |
], |
| 176 |
'password' => [ |
| 177 |
'required' => true, |
| 178 |
'type' => 'password', |
| 179 |
'label' => __('Password', 'fluent-support'), |
| 180 |
'id' => 'fst_password', |
| 181 |
'placeholder' => __('Password', 'fluent-support') |
| 182 |
] |
| 183 |
]); |
| 184 |
} |
| 185 |
|
| 186 |
protected function submitBtnLoadingSvg() |
| 187 |
{ |
| 188 |
$loadingIcon = '<svg version="1.1" id="loader-1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" |
| 189 |
width="40px" height="20px" viewBox="0 0 50 50" style="enable-background:new 0 0 50 50;" xml:space="preserve"> |
| 190 |
<path fill="#000" d="M43.935,25.145c0-10.318-8.364-18.683-18.683-18.683c-10.318,0-18.683,8.365-18.683,18.683h4.068c0-8.071,6.543-14.615,14.615-14.615c8.072,0,14.615,6.543,14.615,14.615H43.935z"> |
| 191 |
<animateTransform attributeType="xml" |
| 192 |
attributeName="transform" |
| 193 |
type="rotate" |
| 194 |
from="0 25 25" |
| 195 |
to="360 25 25" |
| 196 |
dur="0.6s" |
| 197 |
repeatCount="indefinite"/> |
| 198 |
</path> |
| 199 |
</svg>'; |
| 200 |
|
| 201 |
return apply_filters('fluent_support/signup_loading_icon', $loadingIcon); |
| 202 |
} |
| 203 |
|
| 204 |
protected function getShortcodes($attributes) |
| 205 |
{ |
| 206 |
$shortCodeDefaults = apply_filters('fluent_support/auth_shortcode_defaults', [ |
| 207 |
'auto-redirect' => false, |
| 208 |
'redirect-to' => Helper::getPortalBaseUrl(), |
| 209 |
'hide' => false, |
| 210 |
'show-signup' => false |
| 211 |
]); |
| 212 |
|
| 213 |
return shortcode_atts($shortCodeDefaults, $attributes); |
| 214 |
} |
| 215 |
|
| 216 |
protected function handleAlreadyLoggedIn($attributes) |
| 217 |
{ |
| 218 |
if (get_current_user_id() && !wp_is_json_request() && is_singular()) { |
| 219 |
if ($attributes['auto-redirect'] === 'true') { |
| 220 |
$redirect = $attributes['redirect-to']; |
| 221 |
?> |
| 222 |
<script type="text/javascript"> |
| 223 |
document.addEventListener("DOMContentLoaded", function () { |
| 224 |
var redirect = "<?php echo $redirect; ?>"; |
| 225 |
window.location.replace(redirect); |
| 226 |
}); |
| 227 |
</script> |
| 228 |
<?php |
| 229 |
} |
| 230 |
|
| 231 |
die(); |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|