| @@ -12,8 +12,9 @@ | ||
| 12 | 12 | use FluentCommunity\App\Models\XProfile; |
| 13 | 13 | use FluentCommunity\App\Services\Helper; |
| 14 | 14 | use FluentCommunity\App\Services\Libs\DailyDigest; |
| 15 | 15 | use FluentCommunity\App\Services\Libs\Mailer; |
| 16 | +use FluentCommunity\App\Services\FeedsHelper; | |
| 16 | 17 | use FluentCommunity\App\Services\NotificationPref; |
| 17 | 18 | use FluentCommunity\App\Services\ProfileHelper; |
| 18 | 19 | use FluentCommunity\Framework\Support\Arr; |
| 19 | 20 | |
| @@ -35,10 +36,21 @@ | ||
| 35 | 36 | add_action('fluent_community/email_notify_users_everyone_tag', [$this, 'emailNotifyUsersForEveryoneTag'], 10, 2); |
| 36 | 37 | |
| 37 | 38 | add_action('fluent_community_send_daily_digest', [$this, 'maybeSendDailyDigest'], 10); |
| 38 | 39 | add_action('fluent_community/space/join_requested', [$this, 'handleCommunityJoinRequest'], 10, 2); |
| 40 | + | |
| 41 | + add_action('fluent_community/send_new_user_notification', [$this, 'sendNewUserNotificationAsync'], 10, 1); | |
| 39 | 42 | } |
| 40 | 43 | |
| 44 | + public function sendNewUserNotificationAsync($userId) | |
| 45 | + { | |
| 46 | + $userId = (int) $userId; | |
| 47 | + if (!$userId || !get_userdata($userId)) { | |
| 48 | + return; | |
| 49 | + } | |
| 50 | + wp_new_user_notification($userId, null, 'user'); | |
| 51 | + } | |
| 52 | + | |
| 41 | 53 | public function handleSpaceFeedCreated($feed) |
| 42 | 54 | { |
| 43 | 55 | if (did_action('fluent_community/feed/scheduling_everyone_tag')) { |
| 44 | 56 | return; |
| @@ -48,22 +60,23 @@ | ||
| 48 | 60 | if (!$space) { |
| 49 | 61 | return false; |
| 50 | 62 | } |
| 51 | 63 | |
| 52 | - $types = ['np_by_member_mail']; | |
| 64 | + $events = ['np_by_member']; | |
| 53 | 65 | $spaceRole = $feed->user->getSpaceRole($feed->space); |
| 54 | 66 | if (in_array($spaceRole, ['admin', 'moderator'])) { |
| 55 | - $types[] = 'np_by_admin_mail'; | |
| 67 | + $events[] = 'np_by_admin'; | |
| 56 | 68 | } |
| 57 | 69 | |
| 58 | - $hasSubscribers = User::query()->where(function ($query) use ($types, $space, $feed) { | |
| 59 | - $query->whereHas('notificationSubscriptions', function ($query) use ($types, $space) { | |
| 60 | - $query->whereIn('notification_type', $types) | |
| 70 | + $hasSubscribers = User::query()->where(function ($query) use ($events, $space, $feed) { | |
| 71 | + $query->whereHas('notificationPreferences', function ($query) use ($events, $space) { | |
| 72 | + $query->where('channel', 'mail') | |
| 73 | + ->whereIn('event_key', $events) | |
| 61 | 74 | ->where('object_id', $space->id) |
| 62 | - ->where('is_read', 1); | |
| 75 | + ->where('value', 1); | |
| 63 | 76 | }); |
| 64 | 77 | |
| 65 | - do_action_ref_array('fluent_community/space_feed/email_notify_sub_query', [&$query, $feed, $space, $types]); | |
| 78 | + do_action_ref_array('fluent_community/space_feed/email_notify_sub_query', [&$query, $feed, $space, $events]); | |
| 66 | 79 | |
| 67 | 80 | return $query; |
| 68 | 81 | })->exists(); |
| 69 | 82 | |
| @@ -95,20 +108,21 @@ | ||
| 95 | 108 | if (!$space || !$feed->user) { |
| 96 | 109 | return; |
| 97 | 110 | } |
| 98 | 111 | |
| 99 | - $types = ['np_by_member_mail']; | |
| 112 | + $events = ['np_by_member']; | |
| 100 | 113 | $spaceRole = $feed->user->getSpaceRole($feed->space); |
| 101 | 114 | if (in_array($spaceRole, ['admin', 'moderator'])) { |
| 102 | - $types[] = 'np_by_admin_mail'; | |
| 115 | + $events[] = 'np_by_admin'; | |
| 103 | 116 | } |
| 104 | 117 | |
| 105 | 118 | $lastSendUserId = (int)$feed->getCustomMeta('_last_email_user_id', 0); |
| 106 | - $usersQuery = User::query()->where(function ($query) use ($types, $space, $feed) { | |
| 107 | - $query->whereHas('notificationSubscriptions', function ($query) use ($types, $space) { | |
| 108 | - $query->whereIn('notification_type', $types) | |
| 119 | + $usersQuery = User::query()->where(function ($query) use ($events, $space, $feed) { | |
| 120 | + $query->whereHas('notificationPreferences', function ($query) use ($events, $space) { | |
| 121 | + $query->where('channel', 'mail') | |
| 122 | + ->whereIn('event_key', $events) | |
| 109 | 123 | ->where('object_id', $space->id) |
| 110 | - ->where('is_read', 1); | |
| 124 | + ->where('value', 1); | |
| 111 | 125 | }); |
| 112 | 126 | |
| 113 | 127 | $mentionedUserIds = Arr::get($feed->meta, 'mentioned_user_ids', []); |
| 114 | 128 | |
| @@ -115,9 +129,9 @@ | ||
| 115 | 129 | if ($mentionedUserIds) { |
| 116 | 130 | $query->orWhereIn('ID', $mentionedUserIds); |
| 117 | 131 | } |
| 118 | 132 | |
| 119 | - do_action_ref_array('fluent_community/space_feed/email_notify_sub_query', [&$query, $feed, $space, $types]); | |
| 133 | + do_action_ref_array('fluent_community/space_feed/email_notify_sub_query', [&$query, $feed, $space, $events]); | |
| 120 | 134 | |
| 121 | 135 | return $query; |
| 122 | 136 | }) |
| 123 | 137 | ->whereHas('space_pivot', function ($query) use ($space) { |
| @@ -138,12 +152,14 @@ | ||
| 138 | 152 | if ($users->isEmpty()) { |
| 139 | 153 | return; // It's done |
| 140 | 154 | } |
| 141 | 155 | |
| 156 | + $undeliverableEmails = Helper::getUndeliverableEmails($users->pluck('user_email')->toArray()); | |
| 157 | + | |
| 142 | 158 | $emailSubject = \sprintf( |
| 143 | 159 | /* translators: %1$s is the author name and %2$s is the post excerpt (max 30 chars) */ |
| 144 | 160 | __('New Post By %1$s: %2$s', 'fluent-community'), |
| 145 | - $feed->user->getDisplayName(), | |
| 161 | + $feed->user->getPublicDisplayName(), | |
| 146 | 162 | $feed->getHumanExcerpt(30) |
| 147 | 163 | ); |
| 148 | 164 | |
| 149 | 165 | $emailBody = $feed->getFeedHtml(true); |
| @@ -163,8 +179,12 @@ | ||
| 163 | 179 | if ($user->ID == $feed->user_id) { |
| 164 | 180 | continue; |
| 165 | 181 | } |
| 166 | 182 | |
| 183 | + if ($undeliverableEmails && in_array(strtolower($user->user_email), $undeliverableEmails)) { | |
| 184 | + continue; | |
| 185 | + } | |
| 186 | + | |
| 167 | 187 | $newEmailBody = str_replace([ |
| 168 | 188 | '##feed_permalink##', |
| 169 | 189 | '##email_notification_url##' |
| 170 | 190 | ], [ |
| @@ -225,10 +245,12 @@ | ||
| 225 | 245 | } else { |
| 226 | 246 | $globalCommentStatus = $this->isEnabled('com_my_post_mail'); |
| 227 | 247 | } |
| 228 | 248 | |
| 249 | + $authorId = FeedsHelper::getNotificationAuthorId($feed); | |
| 250 | + | |
| 229 | 251 | $notificationUserIds = []; |
| 230 | - if ($comment->user_id != $feed->user_id && NotificationPref::willGetCommentEmail($feed->user_id, $globalCommentStatus)) { | |
| 252 | + if ($comment->user_id != $authorId && NotificationPref::willGetNotification($authorId, 'comment', 'mail', $globalCommentStatus)) { | |
| 231 | 253 | as_schedule_single_action(time(), 'fluent_community/comment_added_async', [$comment->id, 0], 'fluent-community'); |
| 232 | 254 | return true; |
| 233 | 255 | } |
| 234 | 256 | |
| @@ -234,9 +256,9 @@ | ||
| 234 | 256 | |
| 235 | 257 | if ($comment->parent_id) { |
| 236 | 258 | $notificationUserIds = $comment->getCommentParentUserIds(); |
| 237 | 259 | $notificationUserIds = array_filter($notificationUserIds, function ($userId) use ($globalCommentStatus) { |
| 238 | - return NotificationPref::willGetCommentReplyEmail($userId, $globalCommentStatus); | |
| 260 | + return NotificationPref::willGetNotification($userId, 'reply', 'mail', $globalCommentStatus); | |
| 239 | 261 | }); |
| 240 | 262 | } |
| 241 | 263 | |
| 242 | 264 | if (!$notificationUserIds) { |
| @@ -269,21 +291,22 @@ | ||
| 269 | 291 | } |
| 270 | 292 | |
| 271 | 293 | $notificationUserIds = $comment->getCommentParentUserIds($lastUserId); |
| 272 | 294 | $notificationUserIds = array_filter($notificationUserIds, function ($userId) use ($globalCommentStatus) { |
| 273 | - return NotificationPref::willGetCommentReplyEmail($userId, $globalCommentStatus); | |
| 295 | + return NotificationPref::willGetNotification($userId, 'reply', 'mail', $globalCommentStatus); | |
| 274 | 296 | }); |
| 275 | 297 | |
| 276 | 298 | $notificationUserIds = array_diff($notificationUserIds, [$comment->user_id]); |
| 277 | - if ($comment->user_id != $feed->user_id && NotificationPref::willGetCommentEmail($feed->user_id, $globalCommentStatus)) { | |
| 299 | + $authorId = FeedsHelper::getNotificationAuthorId($feed); | |
| 300 | + if ($comment->user_id != $authorId && NotificationPref::willGetNotification($authorId, 'comment', 'mail', $globalCommentStatus)) { | |
| 278 | 301 | // Add at the first |
| 279 | - $notificationUserIds[] = $feed->user_id; | |
| 302 | + $notificationUserIds[] = $authorId; | |
| 280 | 303 | } |
| 281 | 304 | |
| 282 | 305 | // the mentioned user ids |
| 283 | 306 | if ($mentionedUserIds = Arr::get($comment->meta, 'mentioned_user_ids', [])) { |
| 284 | 307 | foreach ($mentionedUserIds as $mentionedUserId) { |
| 285 | - if (NotificationPref::willGetMentionEmail($mentionedUserId, $this->isEnabled('mention_mail'))) { | |
| 308 | + if (NotificationPref::willGetNotification($mentionedUserId, 'mention', 'mail', $this->isEnabled('mention_mail'))) { | |
| 286 | 309 | $notificationUserIds[] = $mentionedUserId; |
| 287 | 310 | } |
| 288 | 311 | } |
| 289 | 312 | } |
| @@ -307,8 +330,10 @@ | ||
| 307 | 330 | if ($users->isEmpty()) { |
| 308 | 331 | return; // it's done |
| 309 | 332 | } |
| 310 | 333 | |
| 334 | + $undeliverableEmails = Helper::getUndeliverableEmails($users->pluck('user_email')->toArray()); | |
| 335 | + | |
| 311 | 336 | $emailBody = $comment->getCommentHtml(true); |
| 312 | 337 | $emailSubject = $comment->getEmailSubject($feed); |
| 313 | 338 | |
| 314 | 339 | $feedPermalik = $feed->getPermalink() . '?comment_id=' . $comment->id; |
| @@ -321,8 +346,12 @@ | ||
| 321 | 346 | if ($user->ID == $comment->user_id) { |
| 322 | 347 | continue; |
| 323 | 348 | } |
| 324 | 349 | |
| 350 | + if ($undeliverableEmails && in_array(strtolower($user->user_email), $undeliverableEmails)) { | |
| 351 | + continue; | |
| 352 | + } | |
| 353 | + | |
| 325 | 354 | $newEmailBody = str_replace([ |
| 326 | 355 | '##feed_permalink##', |
| 327 | 356 | '##email_notification_url##' |
| 328 | 357 | ], [ |
| @@ -395,11 +424,12 @@ | ||
| 395 | 424 | if (!$notification) { |
| 396 | 425 | return true; |
| 397 | 426 | } |
| 398 | 427 | |
| 399 | - $users = User::whereDoesntHave('notificationSubscriptions', function ($query) { | |
| 400 | - $query->where('notification_type', 'mention_mail') | |
| 401 | - ->where('is_read', 0); | |
| 428 | + $users = User::whereDoesntHave('notificationPreferences', function ($query) { | |
| 429 | + $query->where('channel', 'mail') | |
| 430 | + ->where('event_key', 'mention') | |
| 431 | + ->where('value', 0); | |
| 402 | 432 | }) |
| 403 | 433 | ->whereHas('space_pivot', function ($query) use ($feed) { |
| 404 | 434 | $query->where('space_id', $feed->space_id) |
| 405 | 435 | ->where('status', 'active'); |
| @@ -417,8 +447,10 @@ | ||
| 417 | 447 | if ($users->isEmpty()) { |
| 418 | 448 | return true; // it's done |
| 419 | 449 | } |
| 420 | 450 | |
| 451 | + $undeliverableEmails = Helper::getUndeliverableEmails($users->pluck('user_email')->toArray()); | |
| 452 | + | |
| 421 | 453 | $author = $feed->user; |
| 422 | 454 | |
| 423 | 455 | $emailSubject = \sprintf( |
| 424 | 456 | /* translators: for admin post to send email all space members: %1$s is the feed title, %2$s is the author name and %3$s space name */ |
| @@ -423,9 +455,9 @@ | ||
| 423 | 455 | $emailSubject = \sprintf( |
| 424 | 456 | /* translators: for admin post to send email all space members: %1$s is the feed title, %2$s is the author name and %3$s space name */ |
| 425 | 457 | __('%1$s - %2$s [%3$s]', 'fluent-community'), |
| 426 | 458 | $feed->getHumanExcerpt(30), |
| 427 | - $author->display_name, | |
| 459 | + $author->getPublicDisplayName(), | |
| 428 | 460 | $feed->space->title |
| 429 | 461 | ); |
| 430 | 462 | |
| 431 | 463 | $emailBody = $feed->getFeedHtml(true); |
| @@ -439,8 +471,12 @@ | ||
| 439 | 471 | if ($user->ID == $author->ID) { |
| 440 | 472 | continue; |
| 441 | 473 | } |
| 442 | 474 | |
| 475 | + if ($undeliverableEmails && in_array(strtolower($user->user_email), $undeliverableEmails)) { | |
| 476 | + continue; | |
| 477 | + } | |
| 478 | + | |
| 443 | 479 | $newEmailBody = str_replace([ |
| 444 | 480 | '##feed_permalink##', |
| 445 | 481 | '##email_notification_url##' |
| 446 | 482 | ], [ |
| @@ -510,11 +546,12 @@ | ||
| 510 | 546 | $globalEnabled = Arr::get($settings, 'digest_email_status') === 'yes'; |
| 511 | 547 | $lastSentUserId = Utility::getOption('last_digest_sent_user_id'); |
| 512 | 548 | |
| 513 | 549 | if ($globalEnabled) { |
| 514 | - $users = User::whereDoesntHave('notification_records', function ($query) { | |
| 515 | - $query->where('notification_type', 'digest_mail') | |
| 516 | - ->where('is_read', 0); | |
| 550 | + $users = User::whereDoesntHave('notificationPreferences', function ($query) { | |
| 551 | + $query->where('channel', 'mail') | |
| 552 | + ->where('event_key', 'digest') | |
| 553 | + ->where('value', 0); | |
| 517 | 554 | }) |
| 518 | 555 | ->whereHas('xprofile', function ($query) { |
| 519 | 556 | $query->where('status', 'active'); |
| 520 | 557 | }) |
| @@ -524,11 +561,12 @@ | ||
| 524 | 561 | ->limit(100) |
| 525 | 562 | ->orderBy('ID', 'ASC') |
| 526 | 563 | ->get(); |
| 527 | 564 | } else { |
| 528 | - $users = User::whereHas('notification_records', function ($query) { | |
| 529 | - $query->where('notification_type', 'digest_mail') | |
| 530 | - ->where('is_read', 1); | |
| 565 | + $users = User::whereHas('notificationPreferences', function ($query) { | |
| 566 | + $query->where('channel', 'mail') | |
| 567 | + ->where('event_key', 'digest') | |
| 568 | + ->where('value', 1); | |
| 531 | 569 | }) |
| 532 | 570 | ->whereHas('xprofile', function ($query) { |
| 533 | 571 | $query->where('status', 'active'); |
| 534 | 572 | }) |
| @@ -546,8 +584,10 @@ | ||
| 546 | 584 | Utility::updateOption('last_digest_sent_user_id', 0); |
| 547 | 585 | return false; |
| 548 | 586 | } |
| 549 | 587 | |
| 588 | + $undeliverableEmails = Helper::getUndeliverableEmails($users->pluck('user_email')->toArray()); | |
| 589 | + | |
| 550 | 590 | $startAt = microtime(true); |
| 551 | 591 | $maxSendPerSecond = 10; |
| 552 | 592 | $sentCount = 0; |
| 553 | 593 | |
| @@ -552,8 +592,13 @@ | ||
| 552 | 592 | $sentCount = 0; |
| 553 | 593 | |
| 554 | 594 | foreach ($users as $user) { |
| 555 | 595 | Utility::updateOption('last_digest_sent_user_id', $user->ID); |
| 596 | + | |
| 597 | + if ($undeliverableEmails && in_array(strtolower($user->user_email), $undeliverableEmails)) { | |
| 598 | + continue; | |
| 599 | + } | |
| 600 | + | |
| 556 | 601 | $emailDigest = new DailyDigest($user); |
| 557 | 602 | if ($emailDigest->send()) { |
| 558 | 603 | $sentCount++; |
| 559 | 604 | } |
| @@ -629,9 +674,9 @@ | ||
| 629 | 674 | $modUserId = $moderatorsUserIds[0]; |
| 630 | 675 | |
| 631 | 676 | $moderator = get_user_by('ID', $modUserId); |
| 632 | 677 | |
| 633 | - if (!$moderator || !$moderator->user_email) { | |
| 678 | + if (!$moderator || !$moderator->user_email || Helper::isUndeliverableEmail($moderator->user_email)) { | |
| 634 | 679 | return; |
| 635 | 680 | } |
| 636 | 681 | |
| 637 | 682 | $mailer->to($moderator->user_email, $moderator->display_name); |
| @@ -645,13 +690,19 @@ | ||
| 645 | 690 | |
| 646 | 691 | $mailer = new Mailer('', $emailSubject, $emailBody); |
| 647 | 692 | |
| 648 | 693 | $users = User::whereIn('ID', $chunk)->get(); |
| 694 | + $undeliverableEmails = Helper::getUndeliverableEmails($users->pluck('user_email')->toArray()); | |
| 695 | + | |
| 649 | 696 | $first = null; |
| 650 | 697 | foreach ($users as $user) { |
| 651 | 698 | if (!$user || !$user->user_email) { |
| 652 | 699 | continue; |
| 653 | 700 | } |
| 701 | + | |
| 702 | + if ($undeliverableEmails && in_array(strtolower($user->user_email), $undeliverableEmails)) { | |
| 703 | + continue; | |
| 704 | + } | |
| 654 | 705 | if (!$first) { |
| 655 | 706 | $mailer->to($user->user_email, $user->display_name); |
| 656 | 707 | $first = $user; |
| 657 | 708 | continue; |
| @@ -673,12 +724,13 @@ | ||
| 673 | 724 | $unreadCount = Notification::byStatus('unread', $userId)->count(); |
| 674 | 725 | $unreadMessages = apply_filters('fluent_messaging/get_unread_message_count', 0, $userId); |
| 675 | 726 | |
| 676 | 727 | $html = ''; |
| 728 | + $linkColor = Utility::getThemeColor(); | |
| 677 | 729 | if ($unreadCount) { |
| 678 | 730 | $notificationUrl = ProfileHelper::signUserUrlWithAuthHash(Helper::baseUrl('notifications'), $userId); |
| 679 | 731 | /* translators: %d is replaced by the number of unread notifications */ |
| 680 | - $html = '<a style="text-decoration: none;" href="' . $notificationUrl . '">' . sprintf(__('🔔 %d Unread Notifications.', 'fluent-community'), $unreadCount) . '</a>'; | |
| 732 | + $html = '<a style="text-decoration: none; color: ' . esc_attr($linkColor) . ';" href="' . $notificationUrl . '">' . sprintf(__('🔔 %d Unread Notifications', 'fluent-community'), $unreadCount) . '</a>'; | |
| 681 | 733 | if ($unreadMessages) { |
| 682 | 734 | $html .= '<span style="margin: 0 10px;"> | </span>'; |
| 683 | 735 | } |
| 684 | 736 | } |
| @@ -685,9 +737,9 @@ | ||
| 685 | 737 | |
| 686 | 738 | if ($unreadMessages) { |
| 687 | 739 | $chatUrl = ProfileHelper::signUserUrlWithAuthHash(Helper::baseUrl('chat'), $userId); |
| 688 | 740 | /* translators: %d is replaced by the number of unread messages */ |
| 689 | - $html .= '<a style="text-decoration: none;" href="' . $chatUrl . '">' . sprintf(__('✉️ %d Unread Messages', 'fluent-community'), $unreadMessages) . '</a>'; | |
| 741 | + $html .= '<a style="text-decoration: none; color: ' . esc_attr($linkColor) . ';" href="' . $chatUrl . '">' . sprintf(__('✉️ %d Unread Messages', 'fluent-community'), $unreadMessages) . '</a>'; | |
| 690 | 742 | } |
| 691 | 743 | |
| 692 | 744 | return $html; |
| 693 | 745 | } |