| @@ -4,8 +4,9 @@ | ||
| 4 | 4 | |
| 5 | 5 | use FluentBoards\App\Models\Board; |
| 6 | 6 | use FluentBoards\App\Models\Comment; |
| 7 | 7 | use FluentBoards\App\Models\Meta; |
| 8 | +use FluentBoards\App\Models\Relation; | |
| 8 | 9 | use FluentBoards\App\Models\Task; |
| 9 | 10 | use FluentBoards\App\Models\User; |
| 10 | 11 | use FluentBoards\App\Services\Constant; |
| 11 | 12 | use FluentBoards\App\Services\Helper; |
| @@ -13,9 +14,9 @@ | ||
| 13 | 14 | class ScheduleHandler |
| 14 | 15 | { |
| 15 | 16 | public function sendEmailForComment( |
| 16 | 17 | $commentId, |
| 17 | - $usersToSendEmail, | |
| 18 | + $recipientUserIds, | |
| 18 | 19 | $current_user_id |
| 19 | 20 | ) { |
| 20 | 21 | try { |
| 21 | 22 | $comment = Comment::find($commentId) ?? null; |
| @@ -21,9 +22,9 @@ | ||
| 21 | 22 | $comment = Comment::find($commentId) ?? null; |
| 22 | 23 | if ( ! $comment) { |
| 23 | 24 | return; |
| 24 | 25 | } |
| 25 | - if ('comment' != $comment->type) { | |
| 26 | + if (!in_array($comment->type, ['comment', 'reply'])) { | |
| 26 | 27 | return; |
| 27 | 28 | } |
| 28 | 29 | |
| 29 | 30 | $task = Task::findOrFail($comment->task_id); |
| @@ -29,12 +30,22 @@ | ||
| 29 | 30 | $task = Task::findOrFail($comment->task_id); |
| 30 | 31 | if ( ! $task) { |
| 31 | 32 | return; |
| 32 | 33 | } |
| 33 | -// $assignees = $task->assignees; | |
| 34 | + | |
| 35 | + $usersToSendEmail = $this->getEligibleCommentRecipientEmails( | |
| 36 | + $recipientUserIds, | |
| 37 | + $task->board_id | |
| 38 | + ); | |
| 39 | + | |
| 40 | + if (!$usersToSendEmail) { | |
| 41 | + return; | |
| 42 | + } | |
| 43 | + | |
| 34 | 44 | $board = $task->board; |
| 35 | - $page_url = fluent_boards_page_url(); | |
| 36 | 45 | |
| 46 | + $page_url = site_url('/') . '?redirect=to_task&taskId='.$task->id; | |
| 47 | + | |
| 37 | 48 | $user = $task->user($comment->created_by); |
| 38 | 49 | |
| 39 | 50 | $userData = $this->getUserData($current_user_id); |
| 40 | 51 | |
| @@ -50,12 +61,28 @@ | ||
| 50 | 61 | $boardLinkTag = '<a target="_blank" href="' |
| 51 | 62 | .htmlspecialchars($boardUrl).'">' |
| 52 | 63 | .htmlspecialchars($board->title).'</a>'; |
| 53 | 64 | |
| 65 | + // Consolidated translation with placeholders | |
| 66 | + // translators: %1$s is the task link, %2$s is the board link | |
| 67 | + $preparedBody = sprintf(__('commented on %1$s on %2$s board.', 'fluent-boards'), $taskLinkTag, $boardLinkTag); | |
| 68 | + $preHeader = __('New comment has been added on task','fluent-boards'); | |
| 69 | + $mailSubject = __('New comment has been added on task','fluent-boards'); | |
| 70 | + | |
| 71 | + | |
| 72 | + if ($comment->type == 'reply') | |
| 73 | + { | |
| 74 | + // translators: %1$s is the task link, %2$s is the board link | |
| 75 | + $preparedBody = sprintf(__('replied on your comment on %1$s on %2$s board.', 'fluent-boards'), $taskLinkTag, $boardLinkTag); | |
| 76 | + | |
| 77 | + $preHeader = __('A reply has been added to your comment on a task.','fluent-boards'); | |
| 78 | + $mailSubject = __('New Reply on Your Task Comment','fluent-boards'); | |
| 79 | + } | |
| 80 | + | |
| 54 | 81 | $data = [ |
| 55 | - 'body' => 'commented on '.$taskLinkTag.' on ' | |
| 56 | - .$boardLinkTag.' board.', | |
| 57 | - 'pre_header' => 'comment added in fluent task', | |
| 82 | + 'body' => $preparedBody, | |
| 83 | + 'comment_link' => $page_url, | |
| 84 | + 'pre_header' => $preHeader, | |
| 58 | 85 | 'show_footer' => true, |
| 59 | 86 | 'comment' => $comment->description, |
| 60 | 87 | 'userData' => $userData, |
| 61 | 88 | 'site_url' => site_url(), |
| @@ -62,9 +89,8 @@ | ||
| 62 | 89 | 'site_title' => get_bloginfo('name'), |
| 63 | 90 | 'site_logo' => fluent_boards_site_logo(), |
| 64 | 91 | ]; |
| 65 | 92 | |
| 66 | - $mailSubject = 'new comment added on task'; | |
| 67 | 93 | $message = Helper::loadView('emails.comment2', $data); |
| 68 | 94 | $headers = ['Content-Type: text/html; charset=UTF-8']; |
| 69 | 95 | |
| 70 | 96 | foreach ($usersToSendEmail as $email) { |
| @@ -74,8 +100,83 @@ | ||
| 74 | 100 | // do nothing // better to log here |
| 75 | 101 | } |
| 76 | 102 | } |
| 77 | 103 | |
| 104 | + public function sendEmailForMention($commentId, $recipientUserIds, $current_user_id) | |
| 105 | + { | |
| 106 | + try { | |
| 107 | + $comment = Comment::find($commentId) ?? null; | |
| 108 | + if ( ! $comment) { | |
| 109 | + return; | |
| 110 | + } | |
| 111 | + | |
| 112 | + if (!in_array($comment->type, ['comment', 'reply'])) { | |
| 113 | + return; | |
| 114 | + } | |
| 115 | + | |
| 116 | + $task = Task::findOrFail($comment->task_id); | |
| 117 | + if ( ! $task) { | |
| 118 | + return; | |
| 119 | + } | |
| 120 | + | |
| 121 | + $usersToSendEmail = $this->getEligibleCommentRecipientEmails( | |
| 122 | + $recipientUserIds, | |
| 123 | + $task->board_id | |
| 124 | + ); | |
| 125 | + if (!$usersToSendEmail) { | |
| 126 | + return; | |
| 127 | + } | |
| 128 | +// $assignees = $task->assignees; | |
| 129 | + $board = $task->board; | |
| 130 | + | |
| 131 | + $page_url = site_url('/') . '?redirect=to_task&taskId='.$task->id; | |
| 132 | + | |
| 133 | + $user = $task->user($comment->created_by); | |
| 134 | + | |
| 135 | + $userData = $this->getUserData($current_user_id); | |
| 136 | + | |
| 137 | + $boardUrl = $page_url.'boards/'.$board->id; | |
| 138 | + $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'.$task->id.'-' | |
| 139 | + .substr($task->title, 0, 10); | |
| 140 | + | |
| 141 | + $userLinkTag = '<strong>'.htmlspecialchars($user->display_name) | |
| 142 | + .'</strong>'; | |
| 143 | + $taskLinkTag = '<a target="_blank" href="' | |
| 144 | + .htmlspecialchars($taskUrl).'">' | |
| 145 | + .htmlspecialchars($task->title).'</a>'; | |
| 146 | + $boardLinkTag = '<a target="_blank" href="' | |
| 147 | + .htmlspecialchars($boardUrl).'">' | |
| 148 | + .htmlspecialchars($board->title).'</a>'; | |
| 149 | + | |
| 150 | + $commentLink = $taskUrl . '?comment='.$comment->id; | |
| 151 | + | |
| 152 | + // translators: %1$s is the task link, %2$s is the board link | |
| 153 | + $bodyText = sprintf(__('mentioned you in a comment on %1$s on %2$s board.', 'fluent-boards'), $taskLinkTag, $boardLinkTag); | |
| 154 | + | |
| 155 | + $data = [ | |
| 156 | + 'body' => $bodyText, | |
| 157 | + 'comment_link' => $page_url, | |
| 158 | + 'pre_header' => __('You are mentioned in a comment','fluent-boards'), | |
| 159 | + 'show_footer' => true, | |
| 160 | + 'comment' => $comment->description, | |
| 161 | + 'userData' => $userData, | |
| 162 | + 'site_url' => site_url(), | |
| 163 | + 'site_title' => get_bloginfo('name'), | |
| 164 | + 'site_logo' => fluent_boards_site_logo(), | |
| 165 | + ]; | |
| 166 | + | |
| 167 | + $mailSubject = __('You are mentioned in a comment','fluent-boards'); | |
| 168 | + $message = Helper::loadView('emails.comment2', $data); | |
| 169 | + $headers = ['Content-Type: text/html; charset=UTF-8']; | |
| 170 | + | |
| 171 | + foreach ($usersToSendEmail as $email) { | |
| 172 | + \wp_mail($email, $mailSubject, $message, $headers); | |
| 173 | + } | |
| 174 | + } catch (\Exception $e) { | |
| 175 | + // do nothing // better to log here | |
| 176 | + } | |
| 177 | + } | |
| 178 | + | |
| 78 | 179 | public function sendEmailForAddAssignee( |
| 79 | 180 | $taskId, |
| 80 | 181 | $newAssigneeId, |
| 81 | 182 | $current_user_id |
| @@ -99,12 +200,14 @@ | ||
| 99 | 200 | .$task->id.'-'.substr($task->title, 0, 10); |
| 100 | 201 | $taskLinkTag = '<a target="_blank" href="' |
| 101 | 202 | .htmlspecialchars($taskUrl).'">' |
| 102 | 203 | .htmlspecialchars($task->title).'</a>'; |
| 204 | + // translators: %1$s is the task link, %2$s is the board link | |
| 205 | + $bodyText = sprintf(__('has assigned you to task %1$s on %2$s board.', 'fluent-boards'), $taskLinkTag, $boardLinkTag); | |
| 206 | + | |
| 103 | 207 | $data = [ |
| 104 | - 'body' => 'has assigned you to task '.$taskLinkTag | |
| 105 | - .' on the board '.$boardLinkTag, | |
| 106 | - 'pre_header' => 'you have been assigned to task', | |
| 208 | + 'body' => $bodyText, | |
| 209 | + 'pre_header' => __('you have been assigned to task','fluent-boards'), | |
| 107 | 210 | 'show_footer' => true, |
| 108 | 211 | 'userData' => $userData, |
| 109 | 212 | 'site_url' => site_url(), |
| 110 | 213 | 'site_title' => get_bloginfo('name'), |
| @@ -119,14 +222,14 @@ | ||
| 119 | 222 | $taskLinkTag = '<a target="_blank" href="' |
| 120 | 223 | .htmlspecialchars($taskUrl).'">' |
| 121 | 224 | .htmlspecialchars($task->title).'</a>'; |
| 122 | 225 | |
| 226 | + // translators: %1$s is the subtask title, %2$s is the task link, %3$s is the board link | |
| 227 | + $bodyText = sprintf(__('has assigned you to subtask <strong>%1$s</strong> of task %2$s on the board %3$s', 'fluent-boards'), $task->title, $taskLinkTag, $boardLinkTag); | |
| 228 | + | |
| 123 | 229 | $data = [ |
| 124 | - 'body' => 'has assigned you to subtask <strong>' | |
| 125 | - .$task->title.'</strong> of task ' | |
| 126 | - .$taskLinkTag.' on the board ' | |
| 127 | - .$boardLinkTag, | |
| 128 | - 'pre_header' => 'you have been assigned to subtask', | |
| 230 | + 'body' => $bodyText, | |
| 231 | + 'pre_header' => __('you have been assigned to subtask','fluent-boards'), | |
| 129 | 232 | 'show_footer' => true, 'user' => $assignee, |
| 130 | 233 | 'userData' => $userData, |
| 131 | 234 | 'site_url' => site_url(), |
| 132 | 235 | 'site_title' => get_bloginfo('name'), |
| @@ -133,9 +236,9 @@ | ||
| 133 | 236 | 'site_logo' => fluent_boards_site_logo(), |
| 134 | 237 | ]; |
| 135 | 238 | } |
| 136 | 239 | |
| 137 | - $mailSubject = 'You have been assigned to task'; | |
| 240 | + $mailSubject = __('You have been assigned to task','fluent-boards'); | |
| 138 | 241 | $message = Helper::loadView('emails.assignee2', $data); |
| 139 | 242 | $headers = ['Content-Type: text/html; charset=UTF-8']; |
| 140 | 243 | |
| 141 | 244 | |
| @@ -141,9 +244,9 @@ | ||
| 141 | 244 | |
| 142 | 245 | \wp_mail($assignee->user_email, $mailSubject, $message, $headers); |
| 143 | 246 | |
| 144 | 247 | } catch (\Exception $e) { |
| 145 | - throw new \Exception('Error in sending mail to new assignees', 1); | |
| 248 | + throw new \Exception(esc_html__('Error in sending mail to new assignees', 'fluent-boards'), 1); | |
| 146 | 249 | } |
| 147 | 250 | } |
| 148 | 251 | |
| 149 | 252 | public function sendEmailForRemoveAssignee( |
| @@ -171,12 +274,14 @@ | ||
| 171 | 274 | .$task->id.'-'.substr($task->title, 0, 10); |
| 172 | 275 | $taskLinkTag = '<a target="_blank" href="' |
| 173 | 276 | .htmlspecialchars($taskUrl).'">' |
| 174 | 277 | .htmlspecialchars($task->title).'</a>'; |
| 278 | + // translators: %1$s is the task link, %2$s is the board link | |
| 279 | + $bodyText = sprintf(__('has removed you from task %1$s on the board %2$s', 'fluent-boards'), $taskLinkTag, $boardLinkTag); | |
| 280 | + | |
| 175 | 281 | $data = [ |
| 176 | - 'body' => 'has removed you from task '.$taskLinkTag | |
| 177 | - .' on the board '.$boardLinkTag, | |
| 178 | - 'pre_header' => 'you have been removed from task', | |
| 282 | + 'body' => $bodyText, | |
| 283 | + 'pre_header' => __('you have been removed from task','fluent-boards'), | |
| 179 | 284 | 'show_footer' => true, |
| 180 | 285 | 'userData' => $userData, |
| 181 | 286 | 'site_url' => site_url(), |
| 182 | 287 | 'site_title' => get_bloginfo('name'), |
| @@ -191,20 +296,20 @@ | ||
| 191 | 296 | $taskLinkTag = '<a target="_blank" href="' |
| 192 | 297 | .htmlspecialchars($taskUrl).'">' |
| 193 | 298 | .htmlspecialchars($task->title).'</a>'; |
| 194 | 299 | |
| 300 | + // translators: %1$s is the subtask title, %2$s is the task link, %3$s is the board link | |
| 301 | + $bodyText = sprintf(__('has removed you from subtask <strong>%1$s</strong> of task %2$s on the board %3$s', 'fluent-boards'), $task->title, $taskLinkTag, $boardLinkTag); | |
| 302 | + | |
| 195 | 303 | $data = [ |
| 196 | - 'body' => 'has removed you from subtask <strong>' | |
| 197 | - .$task->title.'</strong> of task ' | |
| 198 | - .$taskLinkTag.' on the board ' | |
| 199 | - .$boardLinkTag, | |
| 200 | - 'pre_header' => 'you have been removed from subtask', | |
| 304 | + 'body' => $bodyText, | |
| 305 | + 'pre_header' => __('you have been removed from subtask','fluent-boards'), | |
| 201 | 306 | 'show_footer' => true, |
| 202 | 307 | 'userData' => $userData, |
| 203 | 308 | ]; |
| 204 | 309 | } |
| 205 | 310 | |
| 206 | - $mailSubject = 'You have been removed from task'; | |
| 311 | + $mailSubject = __('You have been removed from task','fluent-boards'); | |
| 207 | 312 | $message = Helper::loadView('emails.assignee2', $data); |
| 208 | 313 | $headers = ['Content-Type: text/html; charset=UTF-8']; |
| 209 | 314 | |
| 210 | 315 | \wp_mail($assignee->user_email, $mailSubject, $message, $headers); |
| @@ -209,9 +314,9 @@ | ||
| 209 | 314 | |
| 210 | 315 | \wp_mail($assignee->user_email, $mailSubject, $message, $headers); |
| 211 | 316 | |
| 212 | 317 | } catch (\Exception $e) { |
| 213 | - throw new \Exception('Error in sending mail to new assignees', 1); | |
| 318 | + throw new \Exception(esc_html__('Error in sending mail to new assignees', 'fluent-boards'), 1); | |
| 214 | 319 | } |
| 215 | 320 | } |
| 216 | 321 | |
| 217 | 322 | |
| @@ -237,13 +342,14 @@ | ||
| 237 | 342 | $taskLinkTag = '<a target="_blank" href="' |
| 238 | 343 | .htmlspecialchars($taskUrl).'">' |
| 239 | 344 | .htmlspecialchars($task->title).'</a>'; |
| 240 | 345 | |
| 346 | + // translators: %1$s is the task link, %2$s is the stage title, %3$s is the board link | |
| 347 | + $bodyText = sprintf(__('has moved %1$s task to <strong>%2$s</strong> stage of board %3$s', 'fluent-boards'), $taskLinkTag, $task->stage->title, $boardLinkTag); | |
| 348 | + | |
| 241 | 349 | $data = [ |
| 242 | - 'body' => 'has moved '.$taskLinkTag.' task to <strong>' | |
| 243 | - .$task->stage->title | |
| 244 | - .'</strong> stage of board '.$boardLinkTag, | |
| 245 | - 'pre_header' => 'Task stage has been changed', | |
| 350 | + 'body' => $bodyText, | |
| 351 | + 'pre_header' => __('Task stage has been changed','fluent-boards'), | |
| 246 | 352 | 'show_footer' => true, |
| 247 | 353 | 'userData' => $userData, |
| 248 | 354 | 'site_url' => site_url(), |
| 249 | 355 | 'site_title' => get_bloginfo('name'), |
| @@ -250,9 +356,9 @@ | ||
| 250 | 356 | 'site_logo' => fluent_boards_site_logo(), |
| 251 | 357 | ]; |
| 252 | 358 | |
| 253 | 359 | |
| 254 | - $mailSubject = 'Task stage has been changed'; | |
| 360 | + $mailSubject = __('Task stage has been changed','fluent-boards'); | |
| 255 | 361 | $message = Helper::loadView('emails.assignee2', $data); |
| 256 | 362 | $headers = ['Content-Type: text/html; charset=UTF-8']; |
| 257 | 363 | |
| 258 | 364 | foreach ($newAssigneeEmails as $assignee_email) { |
| @@ -258,9 +364,9 @@ | ||
| 258 | 364 | foreach ($newAssigneeEmails as $assignee_email) { |
| 259 | 365 | \wp_mail($assignee_email, $mailSubject, $message, $headers); |
| 260 | 366 | } |
| 261 | 367 | } catch (\Exception $e) { |
| 262 | - throw new \Exception('Error in sending mail to new assignees', 1); | |
| 368 | + // Silent fail for email sending | |
| 263 | 369 | } |
| 264 | 370 | } |
| 265 | 371 | |
| 266 | 372 | public function sendEmailForDueDateUpdate( |
| @@ -285,13 +391,14 @@ | ||
| 285 | 391 | $taskLinkTag = '<a target="_blank" href="' |
| 286 | 392 | .htmlspecialchars($taskUrl).'">' |
| 287 | 393 | .htmlspecialchars($task->title).'</a>'; |
| 288 | 394 | |
| 395 | + // translators: %1$s is the task link, %2$s is the due date, %3$s is the board link | |
| 396 | + $bodyText = sprintf(__('has updated due date of %1$s task to <strong>%2$s</strong> of board %3$s', 'fluent-boards'), $taskLinkTag, $task->due_at, $boardLinkTag); | |
| 397 | + | |
| 289 | 398 | $data = [ |
| 290 | - 'body' => 'has updated due date of '.$taskLinkTag | |
| 291 | - .' task to <strong>'.$task->due_at | |
| 292 | - .'</strong> of board '.$boardLinkTag, | |
| 293 | - 'pre_header' => 'Task due date has been changed', | |
| 399 | + 'body' => $bodyText, | |
| 400 | + 'pre_header' => __('Task due date has been changed','fluent-boards'), | |
| 294 | 401 | 'show_footer' => true, |
| 295 | 402 | 'userData' => $userData, |
| 296 | 403 | 'site_url' => site_url(), |
| 297 | 404 | 'site_title' => get_bloginfo('name'), |
| @@ -298,9 +405,9 @@ | ||
| 298 | 405 | 'site_logo' => fluent_boards_site_logo(), |
| 299 | 406 | ]; |
| 300 | 407 | |
| 301 | 408 | |
| 302 | - $mailSubject = 'Task due date has been changed'; | |
| 409 | + $mailSubject = __('Task due date has been changed','fluent-boards'); | |
| 303 | 410 | $message = Helper::loadView('emails.assignee2', $data); |
| 304 | 411 | $headers = ['Content-Type: text/html; charset=UTF-8']; |
| 305 | 412 | |
| 306 | 413 | foreach ($newAssigneeEmails as $assignee_email) { |
| @@ -306,9 +413,9 @@ | ||
| 306 | 413 | foreach ($newAssigneeEmails as $assignee_email) { |
| 307 | 414 | \wp_mail($assignee_email, $mailSubject, $message, $headers); |
| 308 | 415 | } |
| 309 | 416 | } catch (\Exception $e) { |
| 310 | - throw new \Exception('Error in sending mail to new assignees', 1); | |
| 417 | + throw new \Exception(esc_html__('Error in sending mail to new assignees', 'fluent-boards'), 1); | |
| 311 | 418 | } |
| 312 | 419 | } |
| 313 | 420 | |
| 314 | 421 | public function sendEmailForArchivedTask( |
| @@ -333,12 +440,14 @@ | ||
| 333 | 440 | $taskLinkTag = '<a target="_blank" href="' |
| 334 | 441 | .htmlspecialchars($taskUrl).'">' |
| 335 | 442 | .htmlspecialchars($task->title).'</a>'; |
| 336 | 443 | |
| 444 | + // translators: %1$s is the task link, %2$s is the board link | |
| 445 | + $bodyText = sprintf(__('has archived %1$s task of board %2$s', 'fluent-boards'), $taskLinkTag, $boardLinkTag); | |
| 446 | + | |
| 337 | 447 | $data = [ |
| 338 | - 'body' => 'has archived '.$taskLinkTag.' task of board ' | |
| 339 | - .$boardLinkTag, | |
| 340 | - 'pre_header' => 'Task has been archived', | |
| 448 | + 'body' => $bodyText, | |
| 449 | + 'pre_header' => __('Task has been archived','fluent-boards'), | |
| 341 | 450 | 'show_footer' => true, |
| 342 | 451 | 'userData' => $userData, |
| 343 | 452 | 'site_url' => site_url(), |
| 344 | 453 | 'site_title' => get_bloginfo('name'), |
| @@ -345,9 +454,9 @@ | ||
| 345 | 454 | 'site_logo' => fluent_boards_site_logo(), |
| 346 | 455 | ]; |
| 347 | 456 | |
| 348 | 457 | |
| 349 | - $mailSubject = 'Task has been archived'; | |
| 458 | + $mailSubject = __('Task has been archived','fluent-boards'); | |
| 350 | 459 | $message = Helper::loadView('emails.assignee2', $data); |
| 351 | 460 | $headers = ['Content-Type: text/html; charset=UTF-8']; |
| 352 | 461 | |
| 353 | 462 | foreach ($newAssigneeEmails as $assignee_email) { |
| @@ -353,105 +462,123 @@ | ||
| 353 | 462 | foreach ($newAssigneeEmails as $assignee_email) { |
| 354 | 463 | \wp_mail($assignee_email, $mailSubject, $message, $headers); |
| 355 | 464 | } |
| 356 | 465 | } catch (\Exception $e) { |
| 357 | - throw new \Exception('Error in sending mail to new assignees', 1); | |
| 466 | + throw new \Exception(esc_html__('Error in sending mail to new assignees', 'fluent-boards'), 1); | |
| 358 | 467 | } |
| 359 | 468 | } |
| 360 | 469 | |
| 361 | - private function getUserData($userId) | |
| 470 | + /** | |
| 471 | + * Resolve current recipient emails after revalidating board access and preferences. | |
| 472 | + * | |
| 473 | + * Email values are accepted only for queued actions created before recipient IDs | |
| 474 | + * were introduced. They are mapped back to current WordPress users before checks. | |
| 475 | + * | |
| 476 | + * @param array $queuedRecipients | |
| 477 | + * @param int $boardId | |
| 478 | + * @return array | |
| 479 | + */ | |
| 480 | + private function getEligibleCommentRecipientEmails($queuedRecipients, $boardId) | |
| 362 | 481 | { |
| 363 | - $currentUser = User::findOrFail($userId); | |
| 364 | - $gravaterPhoto = fluent_boards_user_avatar($currentUser->user_email, | |
| 365 | - $currentUser->display_name); | |
| 482 | + $legacyEmails = []; | |
| 483 | + foreach ((array) $queuedRecipients as $queuedRecipient) { | |
| 484 | + if (is_string($queuedRecipient) && !ctype_digit($queuedRecipient)) { | |
| 485 | + $legacyEmail = sanitize_email($queuedRecipient); | |
| 486 | + if ($legacyEmail && is_email($legacyEmail)) { | |
| 487 | + $legacyEmails[strtolower($legacyEmail)] = $legacyEmail; | |
| 488 | + } | |
| 489 | + } | |
| 490 | + } | |
| 366 | 491 | |
| 367 | - return [ | |
| 368 | - 'display_name' => $currentUser->display_name, | |
| 369 | - 'photo' => $gravaterPhoto, | |
| 370 | - ]; | |
| 371 | - } | |
| 492 | + $legacyUserIdsByEmail = []; | |
| 493 | + if ($legacyEmails) { | |
| 494 | + $legacyUsers = User::whereIn('user_email', array_values($legacyEmails)) | |
| 495 | + ->get(['ID', 'user_email']); | |
| 496 | + foreach ($legacyUsers as $legacyUser) { | |
| 497 | + $legacyUserIdsByEmail[strtolower($legacyUser->user_email)] = absint($legacyUser->ID); | |
| 498 | + } | |
| 499 | + } | |
| 372 | 500 | |
| 373 | - public function sendInvitationViaEmail($boardId, $email, $current_user_id) | |
| 374 | - { | |
| 375 | - try { | |
| 376 | - $userData = $this->getUserData($current_user_id); | |
| 501 | + $recipientUserIds = []; | |
| 502 | + $seenRecipientUserIds = []; | |
| 503 | + foreach ((array) $queuedRecipients as $queuedRecipient) { | |
| 504 | + $recipientUserId = 0; | |
| 377 | 505 | |
| 378 | - $board = Board::findOrFail($boardId) ?? null; | |
| 379 | - if (!$board) { | |
| 380 | - return; | |
| 506 | + if (is_int($queuedRecipient) || (is_string($queuedRecipient) && ctype_digit($queuedRecipient))) { | |
| 507 | + $recipientUserId = absint($queuedRecipient); | |
| 508 | + } elseif (is_string($queuedRecipient)) { | |
| 509 | + $legacyEmail = sanitize_email($queuedRecipient); | |
| 510 | + if ($legacyEmail) { | |
| 511 | + $recipientUserId = $legacyUserIdsByEmail[strtolower($legacyEmail)] ?? 0; | |
| 512 | + } | |
| 381 | 513 | } |
| 382 | 514 | |
| 383 | - $page_url = fluent_boards_page_url(); | |
| 515 | + if (!$recipientUserId || isset($seenRecipientUserIds[$recipientUserId])) { | |
| 516 | + continue; | |
| 517 | + } | |
| 518 | + $seenRecipientUserIds[$recipientUserId] = true; | |
| 519 | + $recipientUserIds[] = $recipientUserId; | |
| 520 | + } | |
| 384 | 521 | |
| 385 | - $hashCode = $this->hashGenerate(20); | |
| 522 | + if (!$recipientUserIds) { | |
| 523 | + return []; | |
| 524 | + } | |
| 386 | 525 | |
| 387 | - $siteUrl = add_query_arg( array( | |
| 388 | - 'fbs' => 1, | |
| 389 | - 'invitation' => 'board', | |
| 390 | - 'email' => $email, | |
| 391 | - 'hash' => $hashCode, | |
| 392 | - 'bid' => $boardId | |
| 393 | - ), site_url('index.php') ); | |
| 526 | + $eligibleRecipientIds = []; | |
| 527 | + $boardRelations = Relation::where('object_type', Constant::OBJECT_TYPE_BOARD_USER) | |
| 528 | + ->where('object_id', absint($boardId)) | |
| 529 | + ->whereIn('foreign_id', $recipientUserIds) | |
| 530 | + ->get(['foreign_id', 'preferences']); | |
| 394 | 531 | |
| 395 | - $data = [ | |
| 396 | - 'body' => 'has invited you to join board: '. $board->title , | |
| 397 | - 'pre_header' => __('join board invitation in fluent boards', 'fluent-boards'), | |
| 398 | - 'btn_title' => __('Join Board', 'fluent-boards'), | |
| 399 | - 'show_footer' => true, | |
| 400 | - 'userData' => $userData, | |
| 401 | - 'boardLink' => $siteUrl, | |
| 402 | - 'site_url' => site_url(), | |
| 403 | - 'site_title' => get_bloginfo('name'), | |
| 404 | - 'site_logo' => fluent_boards_site_logo(), | |
| 405 | - ]; | |
| 532 | + foreach ($boardRelations as $boardRelation) { | |
| 533 | + $preferences = maybe_unserialize($boardRelation->preferences); | |
| 534 | + if (!is_array($preferences)) { | |
| 535 | + continue; | |
| 536 | + } | |
| 406 | 537 | |
| 407 | - $mailSubject = 'Invitation for joining board'; | |
| 408 | - $message = Helper::loadView('emails.invite-external', $data); | |
| 409 | - $headers = ['Content-Type: text/html; charset=UTF-8']; | |
| 538 | + if ( | |
| 539 | + !array_key_exists(Constant::BOARD_EMAIL_COMMENT, $preferences) || | |
| 540 | + $preferences[Constant::BOARD_EMAIL_COMMENT] | |
| 541 | + ) { | |
| 542 | + $eligibleRecipientIds[absint($boardRelation->foreign_id)] = true; | |
| 543 | + } | |
| 544 | + } | |
| 410 | 545 | |
| 411 | - $this->saveHashByEmail($boardId, $email, $hashCode); | |
| 546 | + if (!$eligibleRecipientIds) { | |
| 547 | + return []; | |
| 548 | + } | |
| 412 | 549 | |
| 413 | - \wp_mail($email, $mailSubject, $message, $headers); | |
| 414 | - } catch (\Exception $e) { | |
| 415 | - // do nothing // better to log here | |
| 550 | + $usersById = []; | |
| 551 | + $recipients = User::whereIn('ID', array_keys($eligibleRecipientIds)) | |
| 552 | + ->get(['ID', 'user_email']); | |
| 553 | + foreach ($recipients as $recipient) { | |
| 554 | + $usersById[absint($recipient->ID)] = $recipient; | |
| 416 | 555 | } |
| 417 | - } | |
| 418 | 556 | |
| 419 | - private function hashGenerate($chars) | |
| 420 | - { | |
| 421 | - $data = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz'; | |
| 422 | - return substr(str_shuffle($data), 0, $chars); | |
| 423 | - } | |
| 557 | + $recipientEmails = []; | |
| 558 | + foreach ($recipientUserIds as $recipientUserId) { | |
| 559 | + if (!isset($eligibleRecipientIds[$recipientUserId], $usersById[$recipientUserId])) { | |
| 560 | + continue; | |
| 561 | + } | |
| 424 | 562 | |
| 425 | - private function saveHashByEmail($boardId, $email, $hash) | |
| 426 | - { | |
| 427 | - $this->deleteHashCode($boardId, $email); | |
| 563 | + $email = sanitize_email($usersById[$recipientUserId]->user_email); | |
| 564 | + if ($email && is_email($email)) { | |
| 565 | + $recipientEmails[] = $email; | |
| 566 | + } | |
| 567 | + } | |
| 428 | 568 | |
| 429 | - $fbs_meta = new Meta(); | |
| 430 | - $fbs_meta->object_id = $boardId; | |
| 431 | - $fbs_meta->object_type = Constant::OBJECT_TYPE_BOARD; | |
| 432 | - $fbs_meta->key = Constant::BOARD_INVITATION; | |
| 433 | - $fbs_meta->value = ['email' => $email, 'hash' => $hash]; | |
| 434 | - $fbs_meta->save(); | |
| 569 | + return $recipientEmails; | |
| 435 | 570 | } |
| 436 | 571 | |
| 437 | - private function deleteHashCode($boardId, $email) | |
| 572 | + private function getUserData($userId) | |
| 438 | 573 | { |
| 439 | - $activeHashCodes = $this->getActiveHashCodes($boardId); | |
| 574 | + $currentUser = User::findOrFail($userId); | |
| 575 | + $gravaterPhoto = fluent_boards_user_avatar($currentUser->user_email, | |
| 576 | + $currentUser->display_name); | |
| 440 | 577 | |
| 441 | - foreach ($activeHashCodes as $savedHash) { | |
| 442 | - $value = maybe_unserialize($savedHash->value); | |
| 443 | - if($value['email'] == $email){ | |
| 444 | - Meta::where('id', $savedHash->id)->delete(); | |
| 445 | - } | |
| 446 | - } | |
| 447 | - } | |
| 448 | - | |
| 449 | - private function getActiveHashCodes($boardId) | |
| 450 | - { | |
| 451 | - return Meta::query()->where('object_id', $boardId) | |
| 452 | - ->where('object_type', Constant::OBJECT_TYPE_BOARD) | |
| 453 | - ->where('key', Constant::BOARD_INVITATION) | |
| 454 | - ->get(); | |
| 578 | + return [ | |
| 579 | + 'display_name' => $currentUser->display_name, | |
| 580 | + 'photo' => $gravaterPhoto, | |
| 581 | + ]; | |
| 455 | 582 | } |
| 456 | 583 | |
| 457 | 584 | } |