PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.10.01
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.10.01
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 / InvitationHandler.php

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

165 lines 5.9 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 use FluentCommunity\App\App;
6 use FluentCommunity\App\Models\BaseSpace;
7 use FluentCommunity\App\Models\User;
8 use FluentCommunity\App\Services\Helper;
9
10 class InvitationHandler
11 {
12 public function register()
13 {
14 add_filter('fluent_community/auth/invitation', function ($invitation, $token) {
15 return Invitation::where('message_rendered', $token)
16 ->whereIn('status', ['pending', 'active'])
17 ->first();
18 }, 10, 2);
19
20 add_action('fluent_community/auth/show_invitation_for_user', [$this, 'showCommunityOnBoard'], 10, 2);
21 add_action('wp_ajax_fcom_user_accept_invitation', [$this, 'acceptInvitationAjax']);
22 add_filter('fluent_community/auth/after_login_with_invitation', [$this, 'handleInvitationLogin'], 10, 3);
23 add_filter('fluent_community/auth/after_signup_redirect_url', function ($redirecctUrl, $user, $postedData) {
24 if (empty($postedData['invitation_token'])) {
25 return $redirecctUrl;
26 }
27
28 $invitation = Invitation::where('message_rendered', $postedData['invitation_token'])
29 ->whereIn('status', ['pending', 'active'])
30 ->first();
31
32 if (!$invitation || !$user || !$invitation->isValid()) {
33 return $redirecctUrl;
34 }
35
36 if ($invitation->message && $invitation->message != $user->user_email) {
37 return $redirecctUrl;
38 }
39
40 if ($invitation->post_id) {
41 $space = BaseSpace::withoutGlobalScopes()->find($invitation->post_id);
42 /** @var BaseSpace|null $space */
43 if ($space) {
44 $role = 'member';
45 if ($space->type == 'course') {
46 $role = 'student';
47 }
48 $isNew = Helper::addToSpace($space, $user->ID, $role);
49 if ($isNew && !$invitation->message) {
50 $invitation->reactions_count = $invitation->reactions_count + 1;
51 $invitation->save();
52 }
53
54 $redirecctUrl = $space->getPermalink();
55 }
56 }
57
58 if ($invitation->message) {
59 $invitation->status = 'accepted';
60 $invitation->save();
61 }
62
63 return $redirecctUrl;
64 }, 10, 3);
65 }
66
67 public function addShortcode($atts)
68 {
69 return 'Hello world!';
70 }
71
72 public function showCommunityOnBoard($invitation, $frameData)
73 {
74 $user = Helper::getCurrentUser();
75 $user->syncXProfile(false);
76 $frameData['title'] = __('Accept Invitation', 'fluent-community');
77
78 $space = BaseSpace::withoutGlobalScopes()->find($invitation->post_id);
79
80 $spaceName = $space ? $space->title : __('the community', 'fluent-community');
81
82 /* translators: %1$s is replaced by the name of the user, %2$s is replaced by the name of the inviter, %3$s is replaced by the name of the space */
83 $frameData['description'] = \sprintf(__('Welcome back %1$s. %2$s has invited you to join in %3$s. Please click the button below to continue.', 'fluent-community'),
84 $user->display_name,
85 $invitation->xprofile ? $invitation->xprofile->display_name : __('Someone', 'fluent-community'),
86 '<b>' . $spaceName . '</b>'
87 );
88
89 $frameData['invitation_token'] = $invitation->message_rendered;
90
91 App::make('view')->render('auth.logged_in_accept', $frameData);
92 }
93
94 public function acceptInvitationAjax()
95 {
96 if (!check_ajax_referer('fcom_accept_invitation', '_fcom_accept_invitation_nonce', false)) {
97 wp_send_json([
98 'message' => __('Invalid request', 'fluent-community')
99 ], 403);
100 }
101
102 $token = isset($_POST['invitation_token']) ? sanitize_text_field(wp_unslash($_POST['invitation_token'])) : '';
103 $user = Helper::getCurrentUser();
104
105 $redirectUrl = $this->handleInvitationLogin(null, $user, $token);
106
107 if (is_wp_error($redirectUrl)) {
108 wp_send_json([
109 'message' => $redirectUrl->get_error_message()
110 ], 422);
111 }
112
113 wp_send_json([
114 'redirect_url' => $redirectUrl,
115 'message' => __('Invitation accepted successfully. Please wait...', 'fluent-community')
116 ]);
117 }
118
119 public function handleInvitationLogin($url, $user, $token)
120 {
121 $userModel = User::find($user->ID);
122
123 $invitation = Invitation::where('message_rendered', $token)
124 ->whereIn('status', ['pending', 'active'])
125 ->first();
126
127 if (!$userModel || !$invitation || !$invitation->isValid() || ($invitation->message && $invitation->message != $userModel->user_email)) {
128 return new \WP_Error('invalid_invitation', __('Invalid invitation token. Please try again', 'fluent-community'));
129 }
130
131 $userModel->syncXProfile(true);
132
133 $space = null;
134 if ($invitation->post_id) {
135 $space = BaseSpace::withoutGlobalScopes()->find($invitation->post_id);
136 /** @var BaseSpace|null $space */
137 if ($space) {
138 $role = 'member';
139 if ($space->type == 'course') {
140 $role = 'student';
141 }
142 $isNew = Helper::addToSpace($space, $userModel->ID, $role);
143
144 if ($isNew && !$invitation->message) {
145 $invitation->reactions_count = $invitation->reactions_count + 1;
146 $invitation->save();
147 }
148 }
149 }
150
151 if ($invitation->message) {
152 $invitation->status = 'accepted';
153 $invitation->save();
154 }
155
156 $redirectUrl = Helper::baseUrl();
157 if ($space) {
158 $redirectUrl = $space->getPermalink();
159 }
160
161 return $redirectUrl;
162 }
163
164 }
165