| 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 PROMPT_PLACEMENTS = ['feed_sidebar','profile_notification_prefs','profile_sidebar','notification_popover']; |
| 14 |
|
| 15 |
const COMMENT_ACTIONS = [ |
| 16 |
'fluent_community/notification/comment/notifed_to_author', |
| 17 |
'fluent_community/notification/comment/notifed_to_mentions', |
| 18 |
'fluent_community/notification/comment/notifed_to_thread_commetenter', |
| 19 |
'fluent_community/notification/comment/notifed_to_other_users' |
| 20 |
]; |
| 21 |
|
| 22 |
public static function isFluentNotifyActive() |
| 23 |
{ |
| 24 |
if (!defined('FLUENT_NOTIFY_PLUGIN_VERSION')) { |
| 25 |
return false; |
| 26 |
} |
| 27 |
|
| 28 |
return \FluentNotify\App\Services\Helper::isEnabled() |
| 29 |
&& \FluentNotify\App\Services\Helper::isConfigComplete(); |
| 30 |
} |
| 31 |
|
| 32 |
public static function isAvailable() |
| 33 |
{ |
| 34 |
$pushEnabledInCommunity = Arr::get(Utility::getPushNotificationSettings(), 'push_enabled') === 'yes'; |
| 35 |
|
| 36 |
return self::isFluentNotifyActive() && $pushEnabledInCommunity; |
| 37 |
} |
| 38 |
|
| 39 |
public function register() |
| 40 |
{ |
| 41 |
add_action('fluent_community/install_fluent_notify_plugin', function () { |
| 42 |
OnboardingService::backgroundInstaller([ |
| 43 |
'name' => 'Fluent Notify', |
| 44 |
'repo-slug' => 'fluent-notify', |
| 45 |
'file' => 'fluent-notify.php' |
| 46 |
], 'https://fluentapi.wpmanageninja.com/addons/download/fluent-notify/1.0.0.zip'); |
| 47 |
}); |
| 48 |
|
| 49 |
if (!self::isAvailable()) return; |
| 50 |
|
| 51 |
foreach (self::COMMENT_ACTIONS as $action) { |
| 52 |
add_action($action, [$this, 'handleCommentNotification'], 10, 1); |
| 53 |
} |
| 54 |
|
| 55 |
// let's load the assets |
| 56 |
add_filter('fluent_community/portal_data_vars', function ($vars) { |
| 57 |
if (!is_user_logged_in()) { |
| 58 |
return $vars; |
| 59 |
} |
| 60 |
|
| 61 |
$vars['js_files']['push_notification'] = [ |
| 62 |
'url' => \FluentNotify\App\Vite::getStaticSrcUrl('push_notification.js'), |
| 63 |
'deps' => [], |
| 64 |
]; |
| 65 |
$vars['js_vars']['fluentNotifyPublic'] = \FluentNotify\App\Services\Helper::getPublicConfig(); |
| 66 |
|
| 67 |
return $vars; |
| 68 |
}); |
| 69 |
|
| 70 |
add_filter('fluent_community/portal_vars', function ($vars) { |
| 71 |
if (!is_user_logged_in()) { |
| 72 |
return $vars; |
| 73 |
} |
| 74 |
|
| 75 |
$vars['has_push_notification'] = true; |
| 76 |
$vars['push_prompt_placements'] = Arr::get(Utility::getPushNotificationSettings(), 'prompt_placements'); |
| 77 |
|
| 78 |
return $vars; |
| 79 |
}); |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
public function handleCommentNotification($eventData) |
| 84 |
{ |
| 85 |
$key = Arr::get($eventData, 'key'); |
| 86 |
$comment = Arr::get($eventData, 'comment'); |
| 87 |
$feed = Arr::get($eventData, 'feed'); |
| 88 |
|
| 89 |
if (!$feed || !$comment) return; |
| 90 |
|
| 91 |
// Hook event key => notification event in NotificationPref::NOTIFICATION_EVENTS. |
| 92 |
$hookToEvent = [ |
| 93 |
'notifed_to_author' => 'comment', |
| 94 |
'notifed_to_thread_commetenter' => 'reply', |
| 95 |
'notifed_to_mentions' => 'mention', |
| 96 |
'notifed_to_other_users' => 'co_comment' |
| 97 |
]; |
| 98 |
|
| 99 |
$userIds = NotificationPref::filterPushUserIds( |
| 100 |
Arr::get($eventData, 'user_ids', []), |
| 101 |
Arr::get($hookToEvent, $key, '') |
| 102 |
); |
| 103 |
|
| 104 |
if (!$userIds) return; |
| 105 |
|
| 106 |
$xprofile = $comment->xprofile; |
| 107 |
|
| 108 |
$notification = Arr::get($eventData, 'notification'); |
| 109 |
$content = Helper::getHumanExcerpt($comment->message_rendered ?: $comment->message, 100); |
| 110 |
if (!$content) { |
| 111 |
// The co-comment hook passes the notification as an array, the others as a model. |
| 112 |
$fallback = is_array($notification) ? Arr::get($notification, 'content') : $notification->content; |
| 113 |
$content = Helper::getHumanExcerpt($fallback, 100); |
| 114 |
} |
| 115 |
|
| 116 |
$commenter = $xprofile ? $xprofile->display_name : '' . __('Someone', 'fluent-community'); |
| 117 |
$feedTitle = $feed->title ? $feed->title : __('post', 'fluent-community'); |
| 118 |
|
| 119 |
// get the first name only |
| 120 |
$commenterParts = explode(' ', $commenter); |
| 121 |
if (count($commenterParts) > 0) { |
| 122 |
$commenter = $commenterParts[0]; |
| 123 |
} |
| 124 |
|
| 125 |
$title = ''; |
| 126 |
switch ($key): |
| 127 |
case 'notifed_to_author': |
| 128 |
/* translators: %1$s is the commenter name, %2$s is the post title */ |
| 129 |
$title = \sprintf(__('New comment by %1$s on: %2$s', 'fluent-community'), $commenter, $feedTitle); |
| 130 |
break; |
| 131 |
case 'notifed_to_mentions': |
| 132 |
/* translators: %1$s is the commenter name, %2$s is the post title */ |
| 133 |
$title = \sprintf(__('%1$s mentioned you on: %2$s', 'fluent-community'), $commenter, $feedTitle); |
| 134 |
break; |
| 135 |
case 'notifed_to_other_users': |
| 136 |
/* translators: %1$s is the commenter name, %2$s is the post title */ |
| 137 |
$title = \sprintf(__('%1$s also commented on: %2$s', 'fluent-community'), $commenter, $feedTitle); |
| 138 |
break; |
| 139 |
case 'notifed_to_thread_commetenter': |
| 140 |
/* translators: %1$s is the commenter name, %2$s is the post title */ |
| 141 |
$title = \sprintf(__('%1$s replied to a comment on: %2$s', 'fluent-community'), $commenter, $feedTitle); |
| 142 |
break; |
| 143 |
default: |
| 144 |
/* translators: %1$s is the commenter name, %2$s is the post title */ |
| 145 |
$content = \sprintf(__('Comment by %1$s on %2$s', 'fluent-community'), $commenter, $feedTitle); |
| 146 |
endswitch; |
| 147 |
|
| 148 |
$feedPermalik = $feed->getPermalink() . '?comment_id=' . $comment->id; |
| 149 |
|
| 150 |
do_action('fluent_notify/schedule_notifications', $userIds, [ |
| 151 |
'user_ids' => $userIds, |
| 152 |
'title' => $title, |
| 153 |
'message' => $content, |
| 154 |
'action_url' => $feedPermalik, |
| 155 |
'icon' => $xprofile->avatar, |
| 156 |
'source' => 'community_comment', |
| 157 |
'source_id' => $comment->id, |
| 158 |
]); |
| 159 |
} |
| 160 |
} |
| 161 |
|