PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.7.7
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.7.7
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / Modules / Auth / Classes / InvitationService.php

InvitationService.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.7.7, at Modules/Auth/Classes/InvitationService.php

279 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Modules\Auth\Classes;
4
5
6 use FluentCommunity\App\App;
7 use FluentCommunity\App\Models\SpaceUserPivot;
8 use FluentCommunity\App\Services\Helper;
9 use FluentCommunity\App\Services\Libs\Mailer;
10 use FluentCommunity\Framework\Support\Arr;
11
12 class InvitationService
13 {
14 public static function registerNewUser($user_login, $user_email, $user_pass = '', $extraData = [])
15 {
16 $errors = new \WP_Error();
17
18 $sanitized_user_login = sanitize_user($user_login);
19
20 $user_email = apply_filters('user_registration_email', $user_email); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
21
22 // Check the username.
23 if ('' === $sanitized_user_login) {
24 $errors->add('empty_username', __('<strong>Error</strong>: Please enter a username.', 'fluent-community'));
25 } elseif (!validate_username($user_login)) {
26 $errors->add('invalid_username', __('<strong>Error</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.', 'fluent-community'));
27 $sanitized_user_login = '';
28 } elseif (username_exists($sanitized_user_login)) {
29 $errors->add('username_exists', __('<strong>Error</strong>: This username is already registered. Please choose another one.', 'fluent-community'));
30 } else {
31 /** This filter is documented in wp-includes/user.php */
32 $illegal_user_logins = (array)apply_filters('illegal_user_logins', array()); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
33 if (in_array(strtolower($sanitized_user_login), array_map('strtolower', $illegal_user_logins), true)) {
34 $errors->add('invalid_username', __('<strong>Error</strong>: Sorry, that username is not allowed.', 'fluent-community'));
35 }
36 }
37
38 // Check the email address.
39 if ('' === $user_email) {
40 $errors->add('empty_email', __('<strong>Error</strong>: Please type your email address.', 'fluent-community'));
41 } elseif (!is_email($user_email)) {
42 $errors->add('invalid_email', __('<strong>Error</strong>: The email address is not correct.', 'fluent-community'));
43 $user_email = '';
44 } elseif (email_exists($user_email)) {
45 $errors->add(
46 'email_exists',
47 __('<strong>Error:</strong> This email address is already registered. Please login or try resetting your password.', 'fluent-community')
48 );
49 }
50
51 do_action('register_post', $sanitized_user_login, $user_email, $errors); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
52
53 $errors = apply_filters('registration_errors', $errors, $sanitized_user_login, $user_email); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
54
55 if ($errors->has_errors()) {
56 return $errors;
57 }
58
59 if (!$user_pass) {
60 $user_pass = wp_generate_password(8, false);
61 }
62
63 $data = [
64 'user_login' => wp_slash($sanitized_user_login),
65 'user_email' => wp_slash($user_email),
66 'user_pass' => $user_pass
67 ];
68
69 if (!empty($extraData['first_name'])) {
70 $data['first_name'] = sanitize_text_field($extraData['first_name']);
71 }
72
73 if (!empty($extraData['last_name'])) {
74 $data['last_name'] = sanitize_text_field($extraData['last_name']);
75 }
76
77 if (!empty($extraData['full_name']) && empty($extraData['first_name']) && empty($extraData['last_name'])) {
78 $extraData['full_name'] = sanitize_text_field($extraData['full_name']);
79 // extract the names
80 $fullNameArray = explode(' ', $extraData['full_name']);
81 $data['first_name'] = array_shift($fullNameArray);
82 if ($fullNameArray) {
83 $data['last_name'] = implode(' ', $fullNameArray);
84 } else {
85 $data['last_name'] = '';
86 }
87 }
88
89 if (!empty($extraData['description'])) {
90 $data['description'] = sanitize_textarea_field($extraData['description']);
91 }
92
93 if (!empty($extraData['user_url']) && filter_var($extraData['user_url'], FILTER_VALIDATE_URL)) {
94 $data['user_url'] = sanitize_url($extraData['user_url']);
95 }
96
97 if (!empty($extraData['role'])) {
98 $data['role'] = $extraData['role'];
99 }
100
101 $user_id = wp_insert_user($data);
102
103 if (!$user_id || is_wp_error($user_id)) {
104 $errors->add('registerfail', __('<strong>Error</strong>: Could not register you. Please contact the site admin!', 'fluent-community')
105 );
106 return $errors;
107 }
108
109 if (!empty($_COOKIE['wp_lang'])) {
110 $wp_lang = sanitize_text_field(wp_unslash($_COOKIE['wp_lang']));
111 if (in_array($wp_lang, get_available_languages(), true)) {
112 update_user_meta($user_id, 'locale', $wp_lang); // Set user locale if defined on registration.
113 }
114 }
115
116 do_action('register_new_user', $user_id); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
117
118 return $user_id;
119 }
120
121 public static function makeLogin($user)
122 {
123 wp_clear_auth_cookie();
124 wp_set_current_user($user->ID, $user->user_login);
125 wp_set_auth_cookie($user->ID, true, is_ssl());
126
127 $user = get_user_by('ID', $user->ID);
128
129 if ($user) {
130 do_action('wp_login', $user->user_login, $user); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
131 }
132
133 return $user;
134 }
135
136 public static function invite($inviatationData)
137 {
138 // Validate the data
139 if (empty($inviatationData['email']) || !is_email($inviatationData['email'])) {
140 return new \WP_Error('email_required', __('Email is required', 'fluent-community'));
141 }
142
143 if (empty($inviatationData['user_id'])) {
144 return new \WP_Error('user_id_required', __('User ID is required', 'fluent-community'));
145 }
146
147 $spaceId = (int)Arr::get($inviatationData, 'space_id');
148
149 $inviteeUser = get_user_by('email', $inviatationData['email']);
150
151 if ($inviteeUser) {
152 if (empty($spaceId)) {
153 return new \WP_Error('user_exist', __('User already exists.', 'fluent-community'));
154 }
155
156 if ($spaceId) {
157 // Let's check if the user is already a member of the space
158 $spaceUser = SpaceUserPivot::where('user_id', $inviteeUser->ID)
159 ->where('space_id', $spaceId)
160 ->first();
161
162 if ($spaceUser) {
163 return new \WP_Error('user_exist', __('User already exists in the space.', 'fluent-community'));
164 }
165 }
166 }
167
168 // Check if the user already invited the same email
169 $exist = Invitation::where('message', $inviatationData['email'])
170 ->when($spaceId, function ($query) use ($spaceId) {
171 return $query->where('post_id', $spaceId);
172 })
173 ->where('user_id', $inviatationData['user_id'])
174 ->first();
175
176 if ($exist) {
177 return new \WP_Error('invitation_exist', __('You have already invited this user', 'fluent-community'));
178 }
179
180 $formattedData = array_filter([
181 'message' => $inviatationData['email'],
182 'user_id' => $inviatationData['user_id'],
183 'status' => 'pending',
184 'post_id' => $spaceId,
185 'message_rendered' => md5($inviatationData['email'] . '_' . time() . '_' . wp_generate_uuid4(10) . '_' . $inviatationData['user_id']),
186 'meta' => [
187 'invitee_name' => sanitize_text_field(Arr::get($inviatationData, 'invitee_name'))
188 ]
189 ]);
190
191 $inviation = Invitation::create($formattedData);
192
193 do_action('fluent_community/invitation_created', $inviation);
194
195 return $inviation;
196 }
197
198 public static function createLinkInvite($data)
199 {
200 $formattedData = array_filter([
201 'message' => '',
202 'user_id' => $data['user_id'],
203 'status' => 'active',
204 'post_id' => $data['space_id'],
205 'message_rendered' => md5($data['space_id'] . '_' . time() . '_' . wp_generate_uuid4(10) . '_' . $data['user_id']),
206 'meta' => Arr::only($data, ['title', 'limit', 'expire_date'])
207 ]);
208
209 $inviation = Invitation::create($formattedData);
210
211 do_action('fluent_community/invitation_link_created', $inviation);
212
213 return $inviation;
214 }
215
216 public static function sendInvitationEmail(Invitation $invitation)
217 {
218 $xProfile = $invitation->xprofile;
219 if (!$xProfile) {
220 return new \WP_Error('xprofile_not_found', __('XProfile not found', 'fluent-community'));
221 }
222
223 $portalSettings = Helper::generalSettings();
224
225 /* translators: %1$s is replaced by the name of the user, %2$s is replaced by the title of the site */
226 $subject = \sprintf(__('%1$s has invited you to join the %2$s', 'fluent-community'),
227 $xProfile->display_name,
228 Arr::get($portalSettings, 'site_title')
229 );
230
231 if ($invitation->post_id) {
232 $space = $invitation->space;
233 if ($space) {
234 $subject = \sprintf(
235 /* translators: %1$s is replaced by the name of the user, %2$s is replaced by the title of the space, %3$s is replaced by the title of the site */
236 __('%1$s has invited you to join the %2$s on %3$s', 'fluent-community'),
237 $xProfile->display_name,
238 $space->title,
239 Arr::get($portalSettings, 'site_title')
240 );
241 }
242 }
243
244 $invitation->reactions_count += 1;
245 $invitation->save();
246
247 $emailComposer = new \FluentCommunity\App\Services\Libs\EmailComposer();
248
249 $emailComposer->addBlock('html_content', self::getInviationEmailHtml($invitation));
250 $emailComposer->setDefaultLogo();
251 $emailComposer->setDefaultFooter();
252
253 $emailBody = $emailComposer->getHtml();
254
255 $mailer = new Mailer($invitation->message, $subject, $emailBody);
256
257 return $mailer->send();
258 }
259
260 private static function getInviationEmailHtml(Invitation $invitation)
261 {
262 $portalSettings = Helper::generalSettings();
263 $siteTitle = Arr::get($portalSettings, 'site_title');
264
265 if ($invitation->space) {
266 /* translators: %1$s is replaced by the title of the space, %2$s is replaced by the title of the site */
267 $siteTitle = \sprintf(__('%1$s on %2$s', 'fluent-community'), $invitation->space->title, $siteTitle);
268 }
269
270 return (string)App::make('view')->make('email.Default._invitation', [
271 'by_name' => $invitation->xprofile->display_name,
272 'by_email' => $invitation->user->user_email,
273 'site_title' => $siteTitle,
274 'access_url' => $invitation->getAccessUrl(),
275 'invitee_name' => Arr::get($invitation->meta, 'invitee_name', 'there')
276 ]);
277 }
278 }
279