PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.7.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.7.0
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.7.0, at Modules/Auth/Classes/InvitationHandler.php

163 lines 5.8 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 if ($space) {
43 $role = 'member';
44 if ($space->type == 'course') {
45 $role = 'student';
46 }
47 $isNew = Helper::addToSpace($space, $user->ID, $role);
48 if ($isNew && !$invitation->message) {
49 $invitation->reactions_count = $invitation->reactions_count + 1;
50 $invitation->save();
51 }
52
53 $redirecctUrl = $space->getPermalink();
54 }
55 }
56
57 if ($invitation->message) {
58 $invitation->status = 'accepted';
59 $invitation->save();
60 }
61
62 return $redirecctUrl;
63 }, 10, 3);
64 }
65
66 public function addShortcode($atts)
67 {
68 return 'Hello world!';
69 }
70
71 public function showCommunityOnBoard($invitation, $frameData)
72 {
73 $user = Helper::getCurrentUser();
74 $user->syncXProfile(false);
75 $frameData['title'] = __('Accept Invitation', 'fluent-community');
76
77 $space = BaseSpace::withoutGlobalScopes()->find($invitation->post_id);
78
79 $spaceName = $space ? $space->title : __('the community', 'fluent-community');
80
81 /* 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 */
82 $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'),
83 $user->display_name,
84 $invitation->xprofile ? $invitation->xprofile->display_name : __('Someone', 'fluent-community'),
85 '<b>' . $spaceName . '</b>'
86 );
87
88 $frameData['invitation_token'] = $invitation->message_rendered;
89
90 App::make('view')->render('auth.logged_in_accept', $frameData);
91 }
92
93 public function acceptInvitationAjax()
94 {
95 if (!check_ajax_referer('fcom_accept_invitation', '_fcom_accept_invitation_nonce', false)) {
96 wp_send_json([
97 'message' => __('Invalid request', 'fluent-community')
98 ], 403);
99 }
100
101 $token = isset($_POST['invitation_token']) ? sanitize_text_field(wp_unslash($_POST['invitation_token'])) : '';
102 $user = Helper::getCurrentUser();
103
104 $redirectUrl = $this->handleInvitationLogin(null, $user, $token);
105
106 if (is_wp_error($redirectUrl)) {
107 wp_send_json([
108 'message' => $redirectUrl->get_error_message()
109 ], 422);
110 }
111
112 wp_send_json([
113 'redirect_url' => $redirectUrl,
114 'message' => __('Invitation accepted successfully. Please wait...', 'fluent-community')
115 ]);
116 }
117
118 public function handleInvitationLogin($url, $user, $token)
119 {
120 $userModel = User::find($user->ID);
121
122 $invitation = Invitation::where('message_rendered', $token)
123 ->whereIn('status', ['pending', 'active'])
124 ->first();
125
126 if (!$userModel || !$invitation || !$invitation->isValid() || ($invitation->message && $invitation->message != $userModel->user_email)) {
127 return new \WP_Error('invalid_invitation', __('Invalid invitation token. Please try again', 'fluent-community'));
128 }
129
130 $userModel->syncXProfile(true);
131
132 $space = null;
133 if ($invitation->post_id) {
134 $space = BaseSpace::withoutGlobalScopes()->find($invitation->post_id);
135 if ($space) {
136 $role = 'member';
137 if ($space->type == 'course') {
138 $role = 'student';
139 }
140 $isNew = Helper::addToSpace($space, $userModel->ID, $role);
141
142 if ($isNew && !$invitation->message) {
143 $invitation->reactions_count = $invitation->reactions_count + 1;
144 $invitation->save();
145 }
146 }
147 }
148
149 if ($invitation->message) {
150 $invitation->status = 'accepted';
151 $invitation->save();
152 }
153
154 $redirectUrl = Helper::baseUrl();
155 if ($space) {
156 $redirectUrl = $space->getPermalink();
157 }
158
159 return $redirectUrl;
160 }
161
162 }
163