| 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 |
->where('status', 'pending') |
| 17 |
->first(); |
| 18 |
}, 10, 2); |
| 19 |
|
| 20 |
add_action('fluent_community/auth/show_inviration_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 |
|
| 25 |
if (empty($postedData['invitation_token'])) { |
| 26 |
return $redirecctUrl; |
| 27 |
} |
| 28 |
|
| 29 |
$invitation = Invitation::where('message_rendered', $postedData['invitation_token']) |
| 30 |
->where('status', 'pending') |
| 31 |
->first(); |
| 32 |
|
| 33 |
if (!$invitation || !$user || $invitation->message != $user->user_email) { |
| 34 |
return $redirecctUrl; |
| 35 |
} |
| 36 |
|
| 37 |
if ($invitation->post_id) { |
| 38 |
$space = BaseSpace::find($invitation->post_id); |
| 39 |
if ($space) { |
| 40 |
$role = 'member'; |
| 41 |
if ($space->type == 'course') { |
| 42 |
$role = 'student'; |
| 43 |
} |
| 44 |
Helper::addToSpace($space, $user->ID, $role); |
| 45 |
} |
| 46 |
|
| 47 |
$redirecctUrl = $space->getPermalink(); |
| 48 |
} |
| 49 |
|
| 50 |
$invitation->status = 'accepted'; |
| 51 |
$invitation->save(); |
| 52 |
|
| 53 |
return $redirecctUrl; |
| 54 |
}, 10, 3); |
| 55 |
} |
| 56 |
|
| 57 |
public function addShortcode($atts) |
| 58 |
{ |
| 59 |
return 'Hello world!'; |
| 60 |
} |
| 61 |
|
| 62 |
public function showCommunityOnBoard($invitation, $frameData) |
| 63 |
{ |
| 64 |
$user = Helper::getCurrentUser(); |
| 65 |
$user->syncXProfile(false); |
| 66 |
$frameData['title'] = ''; |
| 67 |
$frameData['description'] = \sprintf(__('Welcome back %1$s. %2$s has been invited you to join the community. Please click the button bellow to continue.', 'fluent-community'), $user->display_name, $invitation->xprofile->display_name); |
| 68 |
$frameData['invitation_token'] = $invitation->message_rendered; |
| 69 |
|
| 70 |
App::make('view')->render('auth.logged_in_accept', $frameData); |
| 71 |
} |
| 72 |
|
| 73 |
public function acceptInvitationAjax() |
| 74 |
{ |
| 75 |
$token = sanitize_text_field($_POST['invitation_token']); |
| 76 |
$user = Helper::getCurrentUser(); |
| 77 |
|
| 78 |
$redirectUrl = $this->handleInvitationLogin(null, $user, $token); |
| 79 |
|
| 80 |
if (is_wp_error($redirectUrl)) { |
| 81 |
wp_send_json([ |
| 82 |
'message' => $redirectUrl->get_error_message() |
| 83 |
], 422); |
| 84 |
} |
| 85 |
|
| 86 |
wp_send_json([ |
| 87 |
'redirect_url' => $redirectUrl, |
| 88 |
'message' => __('Invitation accepted successfully. Please wait...', 'fluent-community') |
| 89 |
]); |
| 90 |
} |
| 91 |
|
| 92 |
public function handleInvitationLogin($url, $user, $token) |
| 93 |
{ |
| 94 |
$userModel = User::find($user->ID); |
| 95 |
|
| 96 |
$invitation = Invitation::where('message_rendered', $token) |
| 97 |
->where('status', 'pending') |
| 98 |
->first(); |
| 99 |
|
| 100 |
|
| 101 |
if (!$userModel || !$invitation || $invitation->message != $userModel->user_email) { |
| 102 |
return new \WP_Error('invalid_invitation', __('Invalid invitation token. Please try again', 'fluent-community')); |
| 103 |
} |
| 104 |
|
| 105 |
$userModel->syncXProfile(true); |
| 106 |
|
| 107 |
$space = null; |
| 108 |
if ($invitation->post_id) { |
| 109 |
$space = BaseSpace::find($invitation->post_id); |
| 110 |
if ($space) { |
| 111 |
$role = 'member'; |
| 112 |
if ($space->type == 'course') { |
| 113 |
$role = 'student'; |
| 114 |
} |
| 115 |
Helper::addToSpace($space, $userModel->ID, $role); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
$invitation->status = 'accepted'; |
| 120 |
$invitation->save(); |
| 121 |
|
| 122 |
$redirectUrl = Helper::baseUrl(); |
| 123 |
if ($space) { |
| 124 |
$redirectUrl = $space->getPermalink(); |
| 125 |
} |
| 126 |
|
| 127 |
return $redirectUrl; |
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
|