user = $user;
$this->lastSent = $user->getUserMeta('_fcom_last_digest_sent');
$this->fromDate = current_time('mysql', strtotime('-' . $days . ' days'));
$this->toDate = current_time('mysql');
}
public function send()
{
if (!$this->willSend()) {
return false;
}
update_user_meta($this->user->ID, '_fcom_last_digest_sent', current_time('mysql'));
$emailBody = $this->getEmailBody();
if (!$emailBody) {
return false;
}
$eamilSubject = $this->getEmailSubject();
$mailer = new Mailer('', $eamilSubject, $emailBody);
$mailer->to($this->user->user_email, $this->user->display_name);
$mailer->send();
return true;
}
public function getEmailBody()
{
$settings = Helper::generalSettings();
$emailComposer = new \FluentCommunity\App\Services\Libs\EmailComposer();
$headingHtml = \sprintf(
/* translators: %s is replaced by the title of the site */
'
' . __('This week on %s', 'fluent-community') . '
',
Arr::get($settings, 'site_title')
);
/* translators: %1$s is replaced by the start date of the week, %2$s is replaced by the end date of the week */
$headingHtml .= '' . sprintf(__('%1$s - %2$s', 'fluent-community'), date_i18n('F d', strtotime('-7 days')), date_i18n('F d, Y')) . '
';
$emailComposer->addBlock('html_content', $headingHtml);
$emailComposer->addBlock('paragraph', __('Here are some of the most popular posts and notifications you may have missed from the last week 🔥.', 'fluent-community'));
$popularPostsHtml = $this->getPoppularPostsHtml();
// Let's add popular posts here
$emailComposer->addBlock('html_content', $popularPostsHtml);
$notificationsHtml = $this->getNotificationsHtml();
if (!$notificationsHtml && !$popularPostsHtml) {
return '';
}
if ($notificationsHtml) {
$emailComposer->addBlock('html_content', $notificationsHtml);
if ($this->displayCount < count($this->unreadNotifications)) {
$emailComposer->addBlock('button', __('View all the notifications', 'fluent-community'), [
'link' => ProfileHelper::signUserUrlWithAuthHash(Helper::baseUrl('notifications/'), $this->user->ID)
]);
}
}
$emailComposer->setDefaultLogo();
$emailComposer->setDefaultFooter();
$emailBody = $emailComposer->getHtml();
$emailBody = str_replace([
'##email_notification_url##'
], [
ProfileHelper::getSignedNotificationPrefUrl($this->user->ID)
], $emailBody);
$hooksSections = apply_filters('fluent_community/digest_notification/email_sections', [
'before_content' => '',
'after_content' => ''
], $this->user);
if(!empty($hooksSections['before_content'])) {
$emailBody = str_replace('', $hooksSections['before_content'], $emailBody);
}
if(!empty($hooksSections['after_content'])) {
$emailBody = str_replace('', $hooksSections['after_content'], $emailBody);
}
return apply_filters('fluent_community/digest_email_body', $emailBody, $this->user);
}
public function willSend($hours = 20)
{
if (!$this->lastSent) {
return true;
}
return (current_time('timestamp') - strtotime($this->lastSent)) > ($hours * 3600);
}
public function getEmailSubject()
{
$settings = Helper::generalSettings();
$emailSubject = \sprintf(
/* translators: %1$s is replaced by the name of the user, %2$s is replaced by the title of the site */
__('Hey %1$s, here\'s your weekly digest from %2$s', 'fluent-community'),
$this->user->display_name,
Arr::get($settings, 'site_title')
);
$notificationCount = count($this->getUnreadNotifications());
if ($notificationCount > 0) {
/* translators: %d is replaced by the number of unread notifications for digest email subject */
$emailSubject .= ' ' . \sprintf(__('(🔔%d)', 'fluent-community'), $notificationCount);
}
return apply_filters('fluent_community/digest_email_subject', $emailSubject, $this->user, $notificationCount);
}
public function getUnreadNotifications()
{
if ($this->unreadNotifications) {
return $this->unreadNotifications;
}
$this->unreadNotifications = Notification::whereHas('subscribers', function ($query) {
return $query->where('user_id', $this->user->ID)
->where('is_read', 0)
->when($this->lastSent, function ($q) {
$q->where('created_at', '>=', $this->lastSent);
});
})
->limit(10)
->with(['subscriber', 'xprofile'])
->orderBy('updated_at', 'DESC')
->get();
return $this->unreadNotifications;
}
public function getNotificationsHtml()
{
$notifications = $this->getUnreadNotifications();
if ($notifications->isEmpty()) {
return '';
}
$notifications = $notifications->take($this->displayCount);
$totalCount = count($notifications);
ob_start();
?>
$notification):
$permalink = ProfileHelper::signUserUrlWithAuthHash(Helper::getUrlByJsRoute($notification->route), $this->user->ID);
$isLast = $totalCount == ($index + 1);
?>
|
|
user->ID)
->where('status', 'published')
->with(['xprofile' => function ($q) {
$q->select(ProfileHelper::getXProfilePublicFields());
}, 'space'])
->whereHas('xprofile', function ($q) {
$q->where('status', 'active');
})
->where('created_at', '>=', gmdate('Y-m-d H:i:s', strtotime('-' . $days . ' days')))
->orderByRaw('(reactions_count + (comments_count * 2)) DESC')
->limit($count)
->get();
if ($trendingPosts->isEmpty()) {
return '';
}
$html = '' . __('Trending Posts', 'fluent-community') . '
';
$blockStart = '';
foreach ($trendingPosts as $post) {
$permalink = ProfileHelper::signUserUrlWithAuthHash($post->getPermalink(), $this->user->ID);
$html .= $blockStart;
$html .= (string)App::make('view')->make('email.Default._user_post_content', [
'user_name' => $post->xprofile->display_name,
'user_avatar' => $post->xprofile->avatar,
'content' => Helper::getHumanExcerpt(CustomSanitizer::unslashMarkdown($post->message), 200),
'title' => $post->title,
'permalink' => $permalink,
'space_name' => $post->space ? $post->space->title : '',
'hide_publish' => 'yes',
'show_read_more' => 'yes',
'linkColor' => Utility::getThemeColor(),
'timestamp' => human_time_diff(strtotime($post->created_at), current_time('timestamp'))
]);
$html .= $blockEnd;
}
return $html;
}
}