PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.5.4
Fluent Support – Helpdesk & Customer Support Ticket System v1.5.4
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Hooks / Handlers / AuthHandler.php

AuthHandler.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.5.4, at app/Hooks/Handlers/AuthHandler.php

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