PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.9.1
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.9.1
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 / PushNotification / PushNotificationModule.php

PushNotificationModule.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.9.1, at Modules/PushNotification/PushNotificationModule.php

191 lines 7.1 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\PushNotification;
4
5 use FluentCommunity\App\Functions\Utility;
6 use FluentCommunity\App\Services\Helper;
7 use FluentCommunity\App\Services\NotificationPref;
8 use FluentCommunity\App\Services\OnboardingService;
9 use FluentCommunity\Framework\Support\Arr;
10
11 class PushNotificationModule
12 {
13 const SETUP_NOT_INSTALLED = 'not_installed';
14 const SETUP_DISABLED = 'disabled';
15 const SETUP_INCOMPLETE = 'incomplete';
16 const SETUP_READY = 'ready';
17
18 const PROMPT_PLACEMENTS = ['feed_sidebar','profile_notification_prefs','profile_sidebar','notification_popover'];
19
20 const COMMENT_ACTIONS = [
21 'fluent_community/notification/comment/notifed_to_author',
22 'fluent_community/notification/comment/notifed_to_mentions',
23 'fluent_community/notification/comment/notifed_to_thread_commetenter',
24 'fluent_community/notification/comment/notifed_to_other_users'
25 ];
26
27 public static function isFluentNotifyActive()
28 {
29 return self::getSetupState() === self::SETUP_READY;
30 }
31
32 /**
33 * Which step of the FluentNotify setup is still outstanding, so the settings
34 * screen can say what to do rather than only that something is missing.
35 *
36 * @return string One of the SETUP_* constants.
37 */
38 public static function getSetupState()
39 {
40 if (!defined('FLUENT_NOTIFY_PLUGIN_VERSION')) {
41 return self::SETUP_NOT_INSTALLED;
42 }
43
44 if (!\FluentNotify\App\Services\Helper::isEnabled()) {
45 return self::SETUP_DISABLED;
46 }
47
48 if (!\FluentNotify\App\Services\Helper::isConfigComplete()) {
49 return self::SETUP_INCOMPLETE;
50 }
51
52 return self::SETUP_READY;
53 }
54
55 public static function getSettingsUrl()
56 {
57 // FluentNotify picks hash or history routing at runtime; the fragment is
58 // ignored under history routing, which lands on its dashboard instead.
59 return admin_url('admin.php?page=fluent-notify#/settings');
60 }
61
62 public static function isAvailable()
63 {
64 $pushEnabledInCommunity = Arr::get(Utility::getPushNotificationSettings(), 'push_enabled') === 'yes';
65
66 return self::isFluentNotifyActive() && $pushEnabledInCommunity;
67 }
68
69 public function register()
70 {
71 add_action('fluent_community/install_fluent_notify_plugin', function () {
72 OnboardingService::backgroundInstaller([
73 'name' => 'Fluent Notify',
74 'repo-slug' => 'fluent-notify',
75 'file' => 'fluent-notify.php'
76 ], 'https://fluentapi.wpmanageninja.com/addons/download/fluent-notify/1.0.0.zip');
77 });
78
79 if (!self::isAvailable()) return;
80
81 foreach (self::COMMENT_ACTIONS as $action) {
82 add_action($action, [$this, 'handleCommentNotification'], 10, 1);
83 }
84
85 // let's load the assets
86 add_filter('fluent_community/portal_data_vars', function ($vars) {
87 if (!is_user_logged_in()) {
88 return $vars;
89 }
90
91 $vars['js_files']['push_notification'] = [
92 'url' => \FluentNotify\App\Vite::getStaticSrcUrl('push_notification.js'),
93 'deps' => [],
94 ];
95 $vars['js_vars']['fluentNotifyPublic'] = \FluentNotify\App\Services\Helper::getPublicConfig();
96
97 return $vars;
98 });
99
100 add_filter('fluent_community/portal_vars', function ($vars) {
101 if (!is_user_logged_in()) {
102 return $vars;
103 }
104
105 $vars['has_push_notification'] = true;
106 $vars['push_prompt_placements'] = Arr::get(Utility::getPushNotificationSettings(), 'prompt_placements');
107
108 return $vars;
109 });
110
111 }
112
113 public function handleCommentNotification($eventData)
114 {
115 $key = Arr::get($eventData, 'key');
116 $comment = Arr::get($eventData, 'comment');
117 $feed = Arr::get($eventData, 'feed');
118
119 if (!$feed || !$comment) return;
120
121 // Hook event key => notification event in NotificationPref::NOTIFICATION_EVENTS.
122 $hookToEvent = [
123 'notifed_to_author' => 'comment',
124 'notifed_to_thread_commetenter' => 'reply',
125 'notifed_to_mentions' => 'mention',
126 'notifed_to_other_users' => 'co_comment'
127 ];
128
129 $userIds = NotificationPref::filterPushUserIds(
130 Arr::get($eventData, 'user_ids', []),
131 Arr::get($hookToEvent, $key, '')
132 );
133
134 if (!$userIds) return;
135
136 $xprofile = $comment->xprofile;
137
138 $notification = Arr::get($eventData, 'notification');
139 $content = Helper::getHumanExcerpt($comment->message_rendered ?: $comment->message, 100);
140 if (!$content) {
141 // The co-comment hook passes the notification as an array, the others as a model.
142 $fallback = is_array($notification) ? Arr::get($notification, 'content') : $notification->content;
143 $content = Helper::getHumanExcerpt($fallback, 100);
144 }
145
146 $commenter = $xprofile ? $xprofile->display_name : '' . __('Someone', 'fluent-community');
147 $feedTitle = $feed->title ? $feed->title : __('post', 'fluent-community');
148
149 // get the first name only
150 $commenterParts = explode(' ', $commenter);
151 if (count($commenterParts) > 0) {
152 $commenter = $commenterParts[0];
153 }
154
155 $title = '';
156 switch ($key):
157 case 'notifed_to_author':
158 /* translators: %1$s is the commenter name, %2$s is the post title */
159 $title = \sprintf(__('New comment by %1$s on: %2$s', 'fluent-community'), $commenter, $feedTitle);
160 break;
161 case 'notifed_to_mentions':
162 /* translators: %1$s is the commenter name, %2$s is the post title */
163 $title = \sprintf(__('%1$s mentioned you on: %2$s', 'fluent-community'), $commenter, $feedTitle);
164 break;
165 case 'notifed_to_other_users':
166 /* translators: %1$s is the commenter name, %2$s is the post title */
167 $title = \sprintf(__('%1$s also commented on: %2$s', 'fluent-community'), $commenter, $feedTitle);
168 break;
169 case 'notifed_to_thread_commetenter':
170 /* translators: %1$s is the commenter name, %2$s is the post title */
171 $title = \sprintf(__('%1$s replied to a comment on: %2$s', 'fluent-community'), $commenter, $feedTitle);
172 break;
173 default:
174 /* translators: %1$s is the commenter name, %2$s is the post title */
175 $content = \sprintf(__('Comment by %1$s on %2$s', 'fluent-community'), $commenter, $feedTitle);
176 endswitch;
177
178 $feedPermalik = $feed->getPermalink() . '?comment_id=' . $comment->id;
179
180 do_action('fluent_notify/schedule_notifications', $userIds, [
181 'user_ids' => $userIds,
182 'title' => $title,
183 'message' => $content,
184 'action_url' => $feedPermalik,
185 'icon' => $xprofile->avatar,
186 'source' => 'community_comment',
187 'source_id' => $comment->id,
188 ]);
189 }
190 }
191