| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Modules\PushNotification; |
| 4 |
|
| 5 |
use FluentCommunity\App\Services\Helper; |
| 6 |
use FluentCommunity\Framework\Support\Arr; |
| 7 |
|
| 8 |
class PushNotificationModule |
| 9 |
{ |
| 10 |
public function register() |
| 11 |
{ |
| 12 |
if (!defined('FLUENT_NOTIFY_PLUGIN_VERSION') || !\FluentNotify\App\Services\Helper::isEnabled()) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
$commentActions = [ |
| 17 |
'fluent_community/notification/comment/notifed_to_author', |
| 18 |
// 'fluent_community/notification/comment/notifed_to_author', |
| 19 |
'fluent_community/notification/comment/notifed_to_mentions', |
| 20 |
//'fluent_community/notification/comment/notifed_to_other_users', |
| 21 |
'fluent_community/notification/comment/notifed_to_thread_commetenter', |
| 22 |
]; |
| 23 |
foreach ($commentActions as $action) { |
| 24 |
add_action($action, [ $this, 'handleCommentNotification' ], 10, 1); |
| 25 |
} |
| 26 |
|
| 27 |
// let's load the assets |
| 28 |
add_filter('fluent_community/portal_data_vars', function ($vars) { |
| 29 |
if (!is_user_logged_in()) { |
| 30 |
return $vars; |
| 31 |
} |
| 32 |
|
| 33 |
$vars['js_files']['push_notification'] = [ |
| 34 |
'url' => \FluentNotify\App\Vite::getStaticSrcUrl('push_notification.js'), |
| 35 |
'deps' => [], |
| 36 |
]; |
| 37 |
$vars['js_vars']['fluentNotifyPublic'] = \FluentNotify\App\Services\Helper::getPublicConfig(); |
| 38 |
|
| 39 |
return $vars; |
| 40 |
}); |
| 41 |
|
| 42 |
add_filter('fluent_community/portal_vars', function ($vars) { |
| 43 |
if (!is_user_logged_in()) { |
| 44 |
return $vars; |
| 45 |
} |
| 46 |
|
| 47 |
$vars['has_push_notification'] = true; |
| 48 |
|
| 49 |
return $vars; |
| 50 |
}); |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
public function handleCommentNotification($eventData) |
| 55 |
{ |
| 56 |
$key = Arr::get($eventData, 'key'); |
| 57 |
$comment = Arr::get($eventData, 'comment'); |
| 58 |
$feed = Arr::get($eventData, 'feed'); |
| 59 |
|
| 60 |
$xprofile = $comment->xprofile; |
| 61 |
|
| 62 |
if (!$feed || !$comment) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
$notification = Arr::get($eventData, 'notification'); |
| 67 |
$content = Helper::getHumanExcerpt($comment->message, 100); |
| 68 |
if (!$content) { |
| 69 |
$content = Helper::getHumanExcerpt($notification->content, 100); |
| 70 |
} |
| 71 |
|
| 72 |
$commenter = $xprofile ? $xprofile->display_name : '' . __('Someone', 'fluent-community'); |
| 73 |
$feedTitle = $feed->title ? $feed->title : __('post', 'fluent-community'); |
| 74 |
|
| 75 |
// get the first name only |
| 76 |
$commenterParts = explode(' ', $commenter); |
| 77 |
if (count($commenterParts) > 0) { |
| 78 |
$commenter = $commenterParts[0]; |
| 79 |
} |
| 80 |
|
| 81 |
$title = ''; |
| 82 |
switch ($key): |
| 83 |
case 'notifed_to_author': |
| 84 |
/* translators: %1$s is the commenter name, %2$s is the post title */ |
| 85 |
$title = \sprintf(__('💬 by %1$s: %2$s', 'fluent-community'), $commenter, $feedTitle); |
| 86 |
break; |
| 87 |
case 'notifed_to_mentions': |
| 88 |
/* translators: %1$s is the commenter name, %2$s is the post title */ |
| 89 |
$title = \sprintf(__('%1$s mentioned you on: %2$s', 'fluent-community'), $commenter, $feedTitle); |
| 90 |
break; |
| 91 |
case 'notifed_to_other_users': |
| 92 |
/* translators: %1$s is the commenter name, %2$s is the post title */ |
| 93 |
$title = \sprintf(__('New comment by %1$s on: %2$s', 'fluent-community'), $commenter, $feedTitle); |
| 94 |
break; |
| 95 |
case 'notifed_to_thread_commetenter': |
| 96 |
/* translators: %1$s is the commenter name, %2$s is the post title */ |
| 97 |
$title = \sprintf(__('%1$s replied to a comment on: %2$s', 'fluent-community'), $commenter, $feedTitle); |
| 98 |
break; |
| 99 |
default: |
| 100 |
/* translators: %1$s is the commenter name, %2$s is the post title */ |
| 101 |
$content = \sprintf(__('Comment by %1$s on %2$s', 'fluent-community'), $commenter, $feedTitle); |
| 102 |
endswitch; |
| 103 |
|
| 104 |
$userIds = Arr::get($eventData, 'user_ids', []); |
| 105 |
$feedPermalik = $feed->getPermalink() . '?comment_id=' . $comment->id; |
| 106 |
|
| 107 |
do_action('fluent_notify/schedule_notifications', $userIds, [ |
| 108 |
'user_ids' => $userIds, |
| 109 |
'title' => $title, |
| 110 |
'message' => $content, |
| 111 |
'action_url' => $feedPermalik, |
| 112 |
'icon' => $xprofile->avatar, |
| 113 |
'source' => 'community_comment', |
| 114 |
'source_id' => $comment->id, |
| 115 |
]); |
| 116 |
} |
| 117 |
} |
| 118 |
|