| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Services\Libs; |
| 4 |
|
| 5 |
use FluentCommunity\App\App; |
| 6 |
use FluentCommunity\App\Models\Feed; |
| 7 |
use FluentCommunity\App\Models\Notification; |
| 8 |
use FluentCommunity\App\Models\User; |
| 9 |
use FluentCommunity\App\Services\CustomSanitizer; |
| 10 |
use FluentCommunity\App\Services\Helper; |
| 11 |
use FluentCommunity\App\Services\ProfileHelper; |
| 12 |
use FluentCommunity\Framework\Support\Arr; |
| 13 |
|
| 14 |
class DailyDigest |
| 15 |
{ |
| 16 |
private $user; |
| 17 |
|
| 18 |
private $unreadNotifications = null; |
| 19 |
|
| 20 |
public $lastSent = null; |
| 21 |
|
| 22 |
public $displayCount = 7; |
| 23 |
|
| 24 |
private $fromDate = null; |
| 25 |
|
| 26 |
private $toDate = null; |
| 27 |
|
| 28 |
public function __construct(User $user, $days = 7) |
| 29 |
{ |
| 30 |
$this->user = $user; |
| 31 |
$this->lastSent = $user->getUserMeta('_fcom_last_digest_sent'); |
| 32 |
|
| 33 |
$this->fromDate = current_time('mysql', strtotime('-' . $days . ' days')); |
| 34 |
$this->toDate = current_time('mysql'); |
| 35 |
} |
| 36 |
|
| 37 |
public function send() |
| 38 |
{ |
| 39 |
if (!$this->willSend()) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
update_user_meta($this->user->ID, '_fcom_last_digest_sent', current_time('mysql')); |
| 44 |
$emailBody = $this->getEmailBody(); |
| 45 |
if (!$emailBody) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
$eamilSubject = $this->getEmailSubject(); |
| 50 |
$mailer = new Mailer('', $eamilSubject, $emailBody); |
| 51 |
$mailer->to($this->user->user_email, $this->user->display_name); |
| 52 |
$mailer->send(); |
| 53 |
|
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
public function getEmailBody() |
| 58 |
{ |
| 59 |
$settings = Helper::generalSettings(); |
| 60 |
$emailComposer = new \FluentCommunity\App\Services\Libs\EmailComposer(); |
| 61 |
|
| 62 |
$headingHtml = \sprintf( |
| 63 |
'<h2 style="font-family: Arial, sans-serif; font-size: 20px; font-weight: bold; margin: 0; margin-bottom: 5px;">' . __('This week on %s', 'fluent-community') . '</h2>', |
| 64 |
Arr::get($settings, 'site_title') |
| 65 |
); |
| 66 |
|
| 67 |
$headingHtml .= '<p style="font-family: Arial, sans-serif; font-size: 12px; font-weight: normal; margin: 0; margin-bottom: 16px;">' . date('F d', strtotime($this->fromDate)) . ' - ' . date('F d, Y') . '</p>'; |
| 68 |
$emailComposer->addBlock('html_content', $headingHtml); |
| 69 |
$emailComposer->addBlock('paragraph', __('Here are some of the most popular posts, notifications you may have missed from the last week. 🔥.', 'fluent-community')); |
| 70 |
|
| 71 |
$popularPostsHtml = $this->getPoppularPostsHtml(); |
| 72 |
|
| 73 |
// Let's add popular posts here |
| 74 |
$emailComposer->addBlock('html_content', $popularPostsHtml); |
| 75 |
|
| 76 |
$notificationsHtml = $this->getNotificationsHtml(); |
| 77 |
if (!$notificationsHtml && !$popularPostsHtml) { |
| 78 |
return ''; |
| 79 |
} |
| 80 |
|
| 81 |
if ($notificationsHtml) { |
| 82 |
$emailComposer->addBlock('html_content', $notificationsHtml); |
| 83 |
if ($this->displayCount < count($this->unreadNotifications)) { |
| 84 |
$emailComposer->addBlock('button', __('View all the notifications', 'fluent-community'), [ |
| 85 |
'link' => ProfileHelper::signUserUrlWithAuthHash(Helper::baseUrl('notifications/'), $this->user->ID) |
| 86 |
]); |
| 87 |
} |
| 88 |
|
| 89 |
} |
| 90 |
$emailComposer->setLogo(\FluentCommunity\Framework\Support\Arr::get($settings, 'logo')); |
| 91 |
|
| 92 |
$emailComposer->setDefaultFooter(); |
| 93 |
|
| 94 |
$emailBody = $emailComposer->getHtml(); |
| 95 |
|
| 96 |
return str_replace([ |
| 97 |
'##email_notification_url##' |
| 98 |
], [ |
| 99 |
ProfileHelper::getSignedNotificationPrefUrl($this->user->ID) |
| 100 |
], $emailBody); |
| 101 |
} |
| 102 |
|
| 103 |
public function willSend($hours = 20) |
| 104 |
{ |
| 105 |
if (!$this->lastSent) { |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
return (current_time('timestamp') - strtotime($this->lastSent)) > ($hours * 3600); |
| 110 |
} |
| 111 |
|
| 112 |
public function getEmailSubject() |
| 113 |
{ |
| 114 |
$settings = Helper::generalSettings(); |
| 115 |
|
| 116 |
$emailSubject = \sprintf( |
| 117 |
__('Hey %1$s, here\'s your weekly digest from %2$s', 'fluent-community'), |
| 118 |
$this->user->display_name, |
| 119 |
Arr::get($settings, 'site_title') |
| 120 |
); |
| 121 |
|
| 122 |
$notificationCount = count($this->getUnreadNotifications()); |
| 123 |
|
| 124 |
if ($notificationCount > 0) { |
| 125 |
// translators: %d is the number of unread notifications for digest email subject |
| 126 |
$emailSubject .= ' ' . \sprintf(__('(🔔%d)', 'fluent-community'), $notificationCount); |
| 127 |
} |
| 128 |
|
| 129 |
return apply_filters('fluent_community/digest_email_subject', $emailSubject, $this->user, $notificationCount); |
| 130 |
} |
| 131 |
|
| 132 |
public function getUnreadNotifications() |
| 133 |
{ |
| 134 |
if ($this->unreadNotifications) { |
| 135 |
return $this->unreadNotifications; |
| 136 |
} |
| 137 |
|
| 138 |
$this->unreadNotifications = Notification::whereHas('subscribers', function ($query) { |
| 139 |
return $query->where('user_id', $this->user->ID) |
| 140 |
->where('is_read', 0) |
| 141 |
->when($this->lastSent, function ($q) { |
| 142 |
$q->where('created_at', '>=', $this->lastSent); |
| 143 |
}); |
| 144 |
}) |
| 145 |
->limit(10) |
| 146 |
->with(['subscriber', 'xprofile']) |
| 147 |
->orderBy('updated_at', 'DESC') |
| 148 |
->get(); |
| 149 |
|
| 150 |
return $this->unreadNotifications; |
| 151 |
} |
| 152 |
|
| 153 |
public function getNotificationsHtml() |
| 154 |
{ |
| 155 |
$notifications = $this->getUnreadNotifications(); |
| 156 |
|
| 157 |
if ($notifications->isEmpty()) { |
| 158 |
return ''; |
| 159 |
} |
| 160 |
|
| 161 |
$notifications = $notifications->take($this->displayCount); |
| 162 |
|
| 163 |
$totalCount = count($notifications); |
| 164 |
|
| 165 |
ob_start(); |
| 166 |
?> |
| 167 |
<h4 style="font-family: Arial, sans-serif; font-size: 18px; font-weight: bold; margin: 20px 0 5px; margin-bottom: 5px;""><?php esc_html_e('Unread Notifications', 'fluent-community'); ?></h4> |
| 168 |
<table style="background: #f8f8f8;border-radius: 5px;margin: 10px 0 20px;border: 1px solid #dedede;" |
| 169 |
width="100%" |
| 170 |
cellspacing="0" cellpadding="0" border="0"> |
| 171 |
<?php |
| 172 |
foreach ($notifications as $index => $notification): |
| 173 |
$permalink = ProfileHelper::signUserUrlWithAuthHash(Helper::getUrlByJsRoute($notification->route), $this->user->ID); |
| 174 |
$isLast = $totalCount == ($index + 1); |
| 175 |
?> |
| 176 |
<tr> |
| 177 |
<td valign="top" align="left"> |
| 178 |
<table class="fcom_each_item" |
| 179 |
style="padding: 10px;<?php echo $isLast ? '' : 'border-bottom: 1px solid #dedede;'; ?>" |
| 180 |
cellspacing="0" cellpadding="0" border="0"> |
| 181 |
<tr> |
| 182 |
<td valign="top" style="border-radius: 50%; padding: 4px; vertical-align: top;"> |
| 183 |
<a href="<?php echo esc_url($permalink); ?>"> |
| 184 |
<img alt="<?php echo esc_html($notification->xprofile); ?>" |
| 185 |
src="<?php echo esc_url($notification->xprofile->avatar); ?>" width="32" |
| 186 |
height="32" style="border-radius: 50%; display: block;"> |
| 187 |
</a> |
| 188 |
</td> |
| 189 |
<td style="font-family: Arial, sans-serif; font-size: 16px;color: #3c434a; padding-left: 10px; vertical-align: middle;"> |
| 190 |
<a style="color: #3c434a; text-decoration: none;" target="_blank" |
| 191 |
href="<?php echo esc_url($permalink); ?>"> |
| 192 |
<?php echo wp_kses_post($notification->content); ?> |
| 193 |
</a> |
| 194 |
<p style="font-family: Arial, sans-serif; font-size: 12px; font-weight: normal; margin: 0; margin-top: 5px;"> |
| 195 |
<?php echo sprintf(__('%s ago', 'fluent-community'), human_time_diff(strtotime($notification->created_at), current_time('timestamp'))); ?> |
| 196 |
</p> |
| 197 |
</td> |
| 198 |
</tr> |
| 199 |
</table> |
| 200 |
</td> |
| 201 |
</tr> |
| 202 |
<?php endforeach; ?> |
| 203 |
</table> |
| 204 |
<?php |
| 205 |
return ob_get_clean(); |
| 206 |
} |
| 207 |
|
| 208 |
public function getPoppularPostsHtml($count = 5, $days = 7) |
| 209 |
{ |
| 210 |
$trendingPosts = Feed::byUserAccess($this->user->ID) |
| 211 |
->where('status', 'published') |
| 212 |
->with(['xprofile' => function ($q) { |
| 213 |
$q->select(ProfileHelper::getXProfilePublicFields()); |
| 214 |
}, 'space']) |
| 215 |
->whereHas('xprofile', function ($q) { |
| 216 |
$q->where('status', 'active'); |
| 217 |
}) |
| 218 |
->where('created_at', '>=', gmdate('Y-m-d H:i:s', strtotime('-' . $days . ' days'))) |
| 219 |
->orderByRaw('(reactions_count + (comments_count * 2)) DESC') |
| 220 |
->limit($count) |
| 221 |
->get(); |
| 222 |
|
| 223 |
if ($trendingPosts->isEmpty()) { |
| 224 |
return ''; |
| 225 |
} |
| 226 |
|
| 227 |
$html = '<h4 style="font-family: Arial, sans-serif; border-bottom: 2px solid #e7e7e5; font-size: 18px; font-weight: bold; padding-bottom: 10px; margin: 20px 0 10px;">' . __('Trending Posts', 'fluent-community') . '</h4>'; |
| 228 |
|
| 229 |
$blockStart = '<table style="background-color: #f7f7f7; margin: 10px 0 5px; padding: 15px 15px 0; border-radius: 5px;" bgcolor="#f7f7f7" width="100%" style="margin-top: 0px;" cellspacing="0" cellpadding="0" border="0"><tr><td>'; |
| 230 |
|
| 231 |
$blockEnd = '</td></tr></table>'; |
| 232 |
|
| 233 |
foreach ($trendingPosts as $post) { |
| 234 |
$permalink = ProfileHelper::signUserUrlWithAuthHash($post->getPermalink(), $this->user->ID); |
| 235 |
|
| 236 |
$html .= $blockStart; |
| 237 |
$html .= (string)App::make('view')->make('email.Default._user_post_content', [ |
| 238 |
'user_name' => $post->xprofile->display_name, |
| 239 |
'user_avatar' => $post->xprofile->avatar, |
| 240 |
'content' => Helper::getHumanExcerpt(CustomSanitizer::unslashMarkdown($post->message), 200), |
| 241 |
'title' => $post->title, |
| 242 |
'permalink' => $permalink, |
| 243 |
'space_name' => $post->space ? $post->space->title : '', |
| 244 |
'hide_publish' => 'yes', |
| 245 |
'show_read_more' => 'yes', |
| 246 |
'timestamp' => human_time_diff(strtotime($post->created_at), current_time('timestamp')) |
| 247 |
]); |
| 248 |
|
| 249 |
$html .= $blockEnd; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
return $html; |
| 254 |
|
| 255 |
} |
| 256 |
} |
| 257 |
|