PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.97
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.97
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 1.0.97, at Modules/Auth/Classes/InvitationService.php

253 lines 9.4 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);
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());
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);
52
53 $errors = apply_filters('registration_errors', $errors, $sanitized_user_login, $user_email);
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($_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);
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);
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 sendInvitationEmail(Invitation $invitation)
199 {
200 $xProfile = $invitation->xprofile;
201 if (!$xProfile) {
202 return new \WP_Error('xprofile_not_found', __('XProfile not found', 'fluent-community'));
203 }
204
205 $portalSettings = Helper::generalSettings();
206
207 $subject = \sprintf(
208 __('%1$s has invited you to join the %2$s', 'fluent-community'),
209 $xProfile->display_name,
210 Arr::get($portalSettings, 'site_title')
211 );
212
213 if ($invitation->post_id) {
214 $space = $invitation->space;
215 if ($space) {
216 $subject = \sprintf(
217 __('%1$s has invited you to join the %2$s on %3$s', 'fluent-community'),
218 $xProfile->display_name,
219 $space->title,
220 Arr::get($portalSettings, 'site_title')
221 );
222 }
223 }
224
225 $emailBody = self::getInviationEmailHtml($invitation);
226
227 $invitation->reactions_count += 1;
228 $invitation->save();
229
230 $mailer = new Mailer($invitation->message, $subject, $emailBody);
231
232 return $mailer->send();
233 }
234
235 private static function getInviationEmailHtml(Invitation $invitation)
236 {
237 $portalSettings = Helper::generalSettings();
238 $siteTitle = Arr::get($portalSettings, 'site_title');
239
240 if ($invitation->space) {
241 $siteTitle = \sprintf(__('%1$s on %2$s', 'fluent-community'), $invitation->space->title, $siteTitle);
242 }
243
244 return (string)App::make('view')->make('email.Default._invitation', [
245 'by_name' => $invitation->xprofile->display_name,
246 'by_email' => $invitation->user->user_email,
247 'site_title' => $siteTitle,
248 'access_url' => $invitation->getAccessUrl(),
249 'invitee_name' => Arr::get($invitation->meta, 'invitee_name', 'there')
250 ]);
251 }
252 }
253