PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.10.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.10.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 / app / Hooks / Handlers / ActivityMonitorHandler.php

ActivityMonitorHandler.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.10.0, at app/Hooks/Handlers/ActivityMonitorHandler.php

132 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\App\Hooks\Handlers;
4
5 use FluentCommunity\App\Functions\Utility;
6 use FluentCommunity\App\Models\Activity;
7 use FluentCommunity\App\Models\XProfile;
8 use FluentCommunity\App\Services\Helper;
9 use FluentCommunity\Framework\Support\Arr;
10
11 class ActivityMonitorHandler
12 {
13 public function register()
14 {
15 add_action('fluent_community/comment_added', [$this, 'handleNewCommentEvent'], 10, 2);
16 add_action('fluent_community/feed/created', [$this, 'handleFeedCreated'], 10, 1);
17
18 add_action('fluent_community/track_activity', [$this, 'trackActivity'], 10);
19
20 add_action('profile_update', function ($userId) {
21 if (Utility::getPrivacySetting('enable_user_sync') === 'no') {
22 return;
23 }
24
25 $user = get_user_by('ID', $userId);
26 $xprofile = XProfile::where('user_id', $userId)->first();
27 if (!$user || !$xprofile) {
28 return;
29 }
30 $firstName = $user->first_name;
31 $lastName = $user->last_name;
32 $displayName = trim($firstName . ' ' . $lastName);
33 if ($displayName && $xprofile->display_name != $displayName) {
34 $xprofile->display_name = $displayName;
35 $xprofile->save();
36 }
37 });
38
39 add_action('wp_ajax_fluent_community_renew_nonce', [$this, 'renewNonce']);
40
41 // Logged-out: without this, WP returns "0"/200 and the SPA loops on a stale nonce.
42 add_action('wp_ajax_nopriv_fluent_community_renew_nonce', function () {
43 wp_send_json(['message' => __('Your session has expired. Please log in again.', 'fluent-community')], 401);
44 });
45 }
46
47 public function renewNonce()
48 {
49 $ajax_nonce = Arr::get($_REQUEST, 'ajax_nonce'); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
50
51 if (!wp_verify_nonce($ajax_nonce, 'fluent_community_ajax_nonce')) {
52 wp_send_json(['message' => __('Invalid nonce', 'fluent-community')], 400);
53 }
54
55 $currentProfile = Helper::getCurrentProfile();
56 if (!$currentProfile) {
57 wp_send_json(['message' => __('Invalid user', 'fluent-community')], 401);
58 }
59
60 wp_send_json([
61 'rest_nonce' => wp_create_nonce('wp_rest'),
62 'ajax_nonce' => wp_create_nonce('fluent_community_ajax_nonce'),
63 ], 200);
64 }
65
66
67 public function handleNewCommentEvent($comment, $feed)
68 {
69 $isPublic = 1;
70 $space = $feed->space_id ? $feed->space : null;
71
72 if ($space) {
73 $isPublic = $space->privacy == 'public';
74 }
75
76 $data = [
77 'user_id' => $comment->user_id,
78 'feed_id' => $feed->id,
79 'space_id' => $feed->space_id,
80 'related_id' => $comment->id,
81 'action_name' => 'comment_added',
82 'is_public' => $isPublic,
83 ];
84
85 Activity::create($data);
86
87 do_action('fluent_community/track_activity');
88 }
89
90 public function handleFeedCreated($feed)
91 {
92 $isPublic = 1;
93 $space = $feed->space_id ? $feed->space : null;
94
95 if ($space) {
96 $isPublic = $space->privacy == 'public';
97 }
98
99 $data = [
100 'user_id' => $feed->user_id,
101 'feed_id' => $feed->id,
102 'space_id' => $feed->space_id,
103 'action_name' => 'feed_published',
104 'is_public' => $isPublic,
105 ];
106
107 Activity::create($data);
108
109 do_action('fluent_community/track_activity');
110 }
111
112 public function trackActivity()
113 {
114 $currentProfile = Helper::getCurrentProfile();
115
116 if (!$currentProfile) {
117 return false;
118 }
119
120 // Debounce: skip the write if last_activity was updated within the last 5 minutes.
121 // The ticker polls every ~45-75s per session; without this each poll issues an
122 // xprofile UPDATE, saturating the DB at scale.
123 $throttleSeconds = apply_filters('fluent_community/track_activity_throttle_seconds', 300);
124 if ($currentProfile->last_activity && (current_time('timestamp') - strtotime($currentProfile->last_activity)) < $throttleSeconds) {
125 return false;
126 }
127
128 $currentProfile->last_activity = current_time('mysql');
129 $currentProfile->save();
130 }
131 }
132