PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.5.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.5.0
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.5.0, at app/Services/Libs/DailyDigest.php

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