PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.7.7
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.7.7
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / app / Services / Libs / DailyDigest.php

DailyDigest.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.7.7, at app/Services/Libs/DailyDigest.php

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