← All changes
|
Modules/PushNotification/PushNotificationModule.php
+66
-14
2.9.0
→
2.10.0
View file →
| @@ -4,13 +4,18 @@ | ||
| 4 | 4 | |
| 5 | 5 | use FluentCommunity\App\Functions\Utility; |
| 6 | 6 | use FluentCommunity\App\Services\Helper; |
| 7 | 7 | use FluentCommunity\App\Services\NotificationPref; |
| 8 | -use FluentCommunity\App\Services\OnboardingService; | |
| 9 | 8 | use FluentCommunity\Framework\Support\Arr; |
| 10 | 9 | |
| 11 | 10 | class PushNotificationModule |
| 12 | 11 | { |
| 12 | + const SETUP_NOT_INSTALLED = 'not_installed'; | |
| 13 | + const SETUP_INACTIVE = 'inactive'; | |
| 14 | + const SETUP_DISABLED = 'disabled'; | |
| 15 | + const SETUP_INCOMPLETE = 'incomplete'; | |
| 16 | + const SETUP_READY = 'ready'; | |
| 17 | + | |
| 13 | 18 | const PROMPT_PLACEMENTS = ['feed_sidebar','profile_notification_prefs','profile_sidebar','notification_popover']; |
| 14 | 19 | |
| 15 | 20 | const COMMENT_ACTIONS = [ |
| 16 | 21 | 'fluent_community/notification/comment/notifed_to_author', |
| @@ -18,18 +23,51 @@ | ||
| 18 | 23 | 'fluent_community/notification/comment/notifed_to_thread_commetenter', |
| 19 | 24 | 'fluent_community/notification/comment/notifed_to_other_users' |
| 20 | 25 | ]; |
| 21 | 26 | |
| 27 | + const PUSHED_ACTIONS = ['comment_added', 'child_comment_added', 'mention_added']; | |
| 28 | + | |
| 22 | 29 | public static function isFluentNotifyActive() |
| 23 | 30 | { |
| 31 | + return self::getSetupState() === self::SETUP_READY; | |
| 32 | + } | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * Which step of the FluentNotify setup is still outstanding, so the settings | |
| 36 | + * screen can say what to do rather than only that something is missing. | |
| 37 | + * | |
| 38 | + * @return string One of the SETUP_* constants. | |
| 39 | + */ | |
| 40 | + public static function getSetupState() | |
| 41 | + { | |
| 24 | 42 | if (!defined('FLUENT_NOTIFY_PLUGIN_VERSION')) { |
| 25 | - return false; | |
| 43 | + // The files can be there while the plugin is deactivated, which needs | |
| 44 | + // activating rather than another download. | |
| 45 | + if (file_exists(WP_PLUGIN_DIR . '/fluent-notify/fluent-notify.php')) { | |
| 46 | + return self::SETUP_INACTIVE; | |
| 47 | + } | |
| 48 | + | |
| 49 | + return self::SETUP_NOT_INSTALLED; | |
| 26 | 50 | } |
| 27 | 51 | |
| 28 | - return \FluentNotify\App\Services\Helper::isEnabled() | |
| 29 | - && \FluentNotify\App\Services\Helper::isConfigComplete(); | |
| 52 | + if (!\FluentNotify\App\Services\Helper::isEnabled()) { | |
| 53 | + return self::SETUP_DISABLED; | |
| 54 | + } | |
| 55 | + | |
| 56 | + if (!\FluentNotify\App\Services\Helper::isConfigComplete()) { | |
| 57 | + return self::SETUP_INCOMPLETE; | |
| 58 | + } | |
| 59 | + | |
| 60 | + return self::SETUP_READY; | |
| 30 | 61 | } |
| 31 | 62 | |
| 63 | + public static function getSettingsUrl() | |
| 64 | + { | |
| 65 | + // FluentNotify picks hash or history routing at runtime; the fragment is | |
| 66 | + // ignored under history routing, which lands on its dashboard instead. | |
| 67 | + return admin_url('admin.php?page=fluent-notify#/settings'); | |
| 68 | + } | |
| 69 | + | |
| 32 | 70 | public static function isAvailable() |
| 33 | 71 | { |
| 34 | 72 | $pushEnabledInCommunity = Arr::get(Utility::getPushNotificationSettings(), 'push_enabled') === 'yes'; |
| 35 | 73 | |
| @@ -35,18 +73,28 @@ | ||
| 35 | 73 | |
| 36 | 74 | return self::isFluentNotifyActive() && $pushEnabledInCommunity; |
| 37 | 75 | } |
| 38 | 76 | |
| 77 | + public static function getPushedCommentIds($userId, $commentIds) | |
| 78 | + { | |
| 79 | + if (!$userId || !$commentIds || !class_exists('\FluentNotify\App\Models\NotificationLog')) { | |
| 80 | + return []; | |
| 81 | + } | |
| 82 | + | |
| 83 | + $pushed = \FluentNotify\App\Models\NotificationLog::query() | |
| 84 | + ->join('fn_subscriptions', 'fn_subscriptions.id', '=', 'fn_notifications.subscription_id') | |
| 85 | + ->where('fn_subscriptions.user_id', $userId) | |
| 86 | + ->where('fn_notifications.source', 'community_comment') | |
| 87 | + ->whereIn('fn_notifications.source_id', $commentIds) | |
| 88 | + ->whereNotIn('fn_notifications.status', ['fcm_failed', 'fcm_skipped']) | |
| 89 | + ->pluck('fn_notifications.source_id') | |
| 90 | + ->toArray(); | |
| 91 | + | |
| 92 | + return array_values(array_unique(array_map('intval', $pushed))); | |
| 93 | + } | |
| 94 | + | |
| 39 | 95 | public function register() |
| 40 | 96 | { |
| 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 | 97 | if (!self::isAvailable()) return; |
| 50 | 98 | |
| 51 | 99 | foreach (self::COMMENT_ACTIONS as $action) { |
| 52 | 100 | add_action($action, [$this, 'handleCommentNotification'], 10, 1); |
| @@ -144,15 +192,19 @@ | ||
| 144 | 192 | /* translators: %1$s is the commenter name, %2$s is the post title */ |
| 145 | 193 | $content = \sprintf(__('Comment by %1$s on %2$s', 'fluent-community'), $commenter, $feedTitle); |
| 146 | 194 | endswitch; |
| 147 | 195 | |
| 148 | - $feedPermalik = $feed->getPermalink() . '?comment_id=' . $comment->id; | |
| 196 | + $actionUrl = add_query_arg([ | |
| 197 | + 'comment_id' => $comment->id, | |
| 198 | + 'fcom_pn' => 'true', | |
| 199 | + 'feed_id' => $feed->id, | |
| 200 | + ], $feed->getPermalink()); | |
| 149 | 201 | |
| 150 | 202 | do_action('fluent_notify/schedule_notifications', $userIds, [ |
| 151 | 203 | 'user_ids' => $userIds, |
| 152 | 204 | 'title' => $title, |
| 153 | 205 | 'message' => $content, |
| 154 | - 'action_url' => $feedPermalik, | |
| 206 | + 'action_url' => $actionUrl, | |
| 155 | 207 | 'icon' => $xprofile->avatar, |
| 156 | 208 | 'source' => 'community_comment', |
| 157 | 209 | 'source_id' => $comment->id, |
| 158 | 210 | ]); |