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

125 lines 3.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\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', function () {
40
41 $ajax_nonce = Arr::get($_REQUEST, 'ajax_nonce'); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
42
43 if (!wp_verify_nonce($ajax_nonce, 'fluent_community_ajax_nonce')) {
44 wp_send_json(['message' => 'Invalid nonce'], 400);
45 }
46
47 $currentProfile = Helper::getCurrentProfile();
48 if (!$currentProfile) {
49 wp_send_json(['message' => 'Invalid user'], 400);
50 }
51
52 wp_send_json([
53 'rest_nonce' => wp_create_nonce('wp_rest'),
54 'ajax_nonce' => wp_create_nonce('fluent_community_ajax_nonce'),
55 ], 200);
56 });
57 }
58
59
60 public function handleNewCommentEvent($comment, $feed)
61 {
62 $isPublic = 1;
63 $space = $feed->space_id ? $feed->space : null;
64
65 if ($space) {
66 $isPublic = $space->privacy == 'public';
67 }
68
69 $data = [
70 'user_id' => $comment->user_id,
71 'feed_id' => $feed->id,
72 'space_id' => $feed->space_id,
73 'related_id' => $comment->id,
74 'action_name' => 'comment_added',
75 'is_public' => $isPublic,
76 ];
77
78 Activity::create($data);
79
80 do_action('fluent_community/track_activity');
81 }
82
83 public function handleFeedCreated($feed)
84 {
85 $isPublic = 1;
86 $space = $feed->space_id ? $feed->space : null;
87
88 if ($space) {
89 $isPublic = $space->privacy == 'public';
90 }
91
92 $data = [
93 'user_id' => $feed->user_id,
94 'feed_id' => $feed->id,
95 'space_id' => $feed->space_id,
96 'action_name' => 'feed_published',
97 'is_public' => $isPublic,
98 ];
99
100 Activity::create($data);
101
102 do_action('fluent_community/track_activity');
103 }
104
105 public function trackActivity()
106 {
107 $currentProfile = Helper::getCurrentProfile();
108
109 if (!$currentProfile) {
110 return false;
111 }
112
113 // Debounce: skip the write if last_activity was updated within the last 5 minutes.
114 // The ticker polls every ~45-75s per session; without this each poll issues an
115 // xprofile UPDATE, saturating the DB at scale.
116 $throttleSeconds = apply_filters('fluent_community/track_activity_throttle_seconds', 300);
117 if ($currentProfile->last_activity && (current_time('timestamp') - strtotime($currentProfile->last_activity)) < $throttleSeconds) {
118 return false;
119 }
120
121 $currentProfile->last_activity = current_time('mysql');
122 $currentProfile->save();
123 }
124 }
125