| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Board; |
| 6 |
use FluentBoards\App\Models\Comment; |
| 7 |
use FluentBoards\App\Models\Meta; |
| 8 |
use FluentBoards\App\Models\Task; |
| 9 |
use FluentBoards\App\Models\User; |
| 10 |
use FluentBoards\App\Services\Constant; |
| 11 |
use FluentBoards\App\Services\Helper; |
| 12 |
|
| 13 |
class ScheduleHandler |
| 14 |
{ |
| 15 |
public function sendEmailForComment( |
| 16 |
$commentId, |
| 17 |
$usersToSendEmail, |
| 18 |
$current_user_id |
| 19 |
) { |
| 20 |
try { |
| 21 |
$comment = Comment::find($commentId) ?? null; |
| 22 |
if ( ! $comment) { |
| 23 |
return; |
| 24 |
} |
| 25 |
if (!in_array($comment->type, ['comment', 'reply'])) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
$task = Task::findOrFail($comment->task_id); |
| 30 |
if ( ! $task) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
$board = $task->board; |
| 35 |
|
| 36 |
$page_url = site_url('/') . '?redirect=to_task&taskId='.$task->id; |
| 37 |
|
| 38 |
$user = $task->user($comment->created_by); |
| 39 |
|
| 40 |
$userData = $this->getUserData($current_user_id); |
| 41 |
|
| 42 |
$boardUrl = $page_url.'boards/'.$board->id; |
| 43 |
$taskUrl = $page_url.'boards/'.$board->id.'/tasks/'.$task->id.'-' |
| 44 |
.substr($task->title, 0, 10); |
| 45 |
|
| 46 |
$userLinkTag = '<strong>'.htmlspecialchars($user->display_name) |
| 47 |
.'</strong>'; |
| 48 |
$taskLinkTag = '<a target="_blank" href="' |
| 49 |
.htmlspecialchars($taskUrl).'">' |
| 50 |
.htmlspecialchars($task->title).'</a>'; |
| 51 |
$boardLinkTag = '<a target="_blank" href="' |
| 52 |
.htmlspecialchars($boardUrl).'">' |
| 53 |
.htmlspecialchars($board->title).'</a>'; |
| 54 |
|
| 55 |
// Consolidated translation with placeholders |
| 56 |
// translators: %1$s is the task link, %2$s is the board link |
| 57 |
$preparedBody = sprintf(__('commented on %1$s on %2$s board.', 'fluent-boards'), $taskLinkTag, $boardLinkTag); |
| 58 |
$preHeader = __('New comment has been added on task','fluent-boards'); |
| 59 |
$mailSubject = __('New comment has been added on task','fluent-boards'); |
| 60 |
|
| 61 |
|
| 62 |
if ($comment->type == 'reply') |
| 63 |
{ |
| 64 |
// translators: %1$s is the task link, %2$s is the board link |
| 65 |
$preparedBody = sprintf(__('replied on your comment on %1$s on %2$s board.', 'fluent-boards'), $taskLinkTag, $boardLinkTag); |
| 66 |
|
| 67 |
$preHeader = __('A reply has been added to your comment on a task.','fluent-boards'); |
| 68 |
$mailSubject = __('New Reply on Your Task Comment','fluent-boards'); |
| 69 |
} |
| 70 |
|
| 71 |
$data = [ |
| 72 |
'body' => $preparedBody, |
| 73 |
'comment_link' => $page_url, |
| 74 |
'pre_header' => $preHeader, |
| 75 |
'show_footer' => true, |
| 76 |
'comment' => $comment->description, |
| 77 |
'userData' => $userData, |
| 78 |
'site_url' => site_url(), |
| 79 |
'site_title' => get_bloginfo('name'), |
| 80 |
'site_logo' => fluent_boards_site_logo(), |
| 81 |
]; |
| 82 |
|
| 83 |
$message = Helper::loadView('emails.comment2', $data); |
| 84 |
$headers = ['Content-Type: text/html; charset=UTF-8']; |
| 85 |
|
| 86 |
foreach ($usersToSendEmail as $email) { |
| 87 |
\wp_mail($email, $mailSubject, $message, $headers); |
| 88 |
} |
| 89 |
} catch (\Exception $e) { |
| 90 |
// do nothing // better to log here |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
public function sendEmailForMention($commentId, $usersToSendEmail, $current_user_id) |
| 95 |
{ |
| 96 |
try { |
| 97 |
$comment = Comment::find($commentId) ?? null; |
| 98 |
if ( ! $comment) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
if (!in_array($comment->type, ['comment', 'reply'])) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
$task = Task::findOrFail($comment->task_id); |
| 107 |
if ( ! $task) { |
| 108 |
return; |
| 109 |
} |
| 110 |
// $assignees = $task->assignees; |
| 111 |
$board = $task->board; |
| 112 |
|
| 113 |
$page_url = site_url('/') . '?redirect=to_task&taskId='.$task->id; |
| 114 |
|
| 115 |
$user = $task->user($comment->created_by); |
| 116 |
|
| 117 |
$userData = $this->getUserData($current_user_id); |
| 118 |
|
| 119 |
$boardUrl = $page_url.'boards/'.$board->id; |
| 120 |
$taskUrl = $page_url.'boards/'.$board->id.'/tasks/'.$task->id.'-' |
| 121 |
.substr($task->title, 0, 10); |
| 122 |
|
| 123 |
$userLinkTag = '<strong>'.htmlspecialchars($user->display_name) |
| 124 |
.'</strong>'; |
| 125 |
$taskLinkTag = '<a target="_blank" href="' |
| 126 |
.htmlspecialchars($taskUrl).'">' |
| 127 |
.htmlspecialchars($task->title).'</a>'; |
| 128 |
$boardLinkTag = '<a target="_blank" href="' |
| 129 |
.htmlspecialchars($boardUrl).'">' |
| 130 |
.htmlspecialchars($board->title).'</a>'; |
| 131 |
|
| 132 |
$commentLink = $taskUrl . '?comment='.$comment->id; |
| 133 |
|
| 134 |
// translators: %1$s is the task link, %2$s is the board link |
| 135 |
$bodyText = sprintf(__('mentioned you in a comment on %1$s on %2$s board.', 'fluent-boards'), $taskLinkTag, $boardLinkTag); |
| 136 |
|
| 137 |
$data = [ |
| 138 |
'body' => $bodyText, |
| 139 |
'comment_link' => $page_url, |
| 140 |
'pre_header' => __('You are mentioned in a comment','fluent-boards'), |
| 141 |
'show_footer' => true, |
| 142 |
'comment' => $comment->description, |
| 143 |
'userData' => $userData, |
| 144 |
'site_url' => site_url(), |
| 145 |
'site_title' => get_bloginfo('name'), |
| 146 |
'site_logo' => fluent_boards_site_logo(), |
| 147 |
]; |
| 148 |
|
| 149 |
$mailSubject = __('You are mentioned in a comment','fluent-boards'); |
| 150 |
$message = Helper::loadView('emails.comment2', $data); |
| 151 |
$headers = ['Content-Type: text/html; charset=UTF-8']; |
| 152 |
|
| 153 |
foreach ($usersToSendEmail as $email) { |
| 154 |
\wp_mail($email, $mailSubject, $message, $headers); |
| 155 |
} |
| 156 |
} catch (\Exception $e) { |
| 157 |
// do nothing // better to log here |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
public function sendEmailForAddAssignee( |
| 162 |
$taskId, |
| 163 |
$newAssigneeId, |
| 164 |
$current_user_id |
| 165 |
) { |
| 166 |
try { |
| 167 |
$task = Task::findOrFail($taskId); |
| 168 |
$board = $task->board; |
| 169 |
$assignee = User::findOrFail($newAssigneeId); |
| 170 |
|
| 171 |
$userData = $this->getUserData($current_user_id); |
| 172 |
|
| 173 |
$page_url = fluent_boards_page_url(); |
| 174 |
$boardUrl = $page_url.'boards/'.$board->id; |
| 175 |
$boardLinkTag = '<a target="_blank" href="' |
| 176 |
.htmlspecialchars($boardUrl).'">' |
| 177 |
.htmlspecialchars($board->title).'</a>'; |
| 178 |
|
| 179 |
if (null == $task->parent_id) { |
| 180 |
// this is a task |
| 181 |
$taskUrl = $page_url.'boards/'.$board->id.'/tasks/' |
| 182 |
.$task->id.'-'.substr($task->title, 0, 10); |
| 183 |
$taskLinkTag = '<a target="_blank" href="' |
| 184 |
.htmlspecialchars($taskUrl).'">' |
| 185 |
.htmlspecialchars($task->title).'</a>'; |
| 186 |
// translators: %1$s is the task link, %2$s is the board link |
| 187 |
$bodyText = sprintf(__('has assigned you to task %1$s on %2$s board.', 'fluent-boards'), $taskLinkTag, $boardLinkTag); |
| 188 |
|
| 189 |
$data = [ |
| 190 |
'body' => $bodyText, |
| 191 |
'pre_header' => __('you have been assigned to task','fluent-boards'), |
| 192 |
'show_footer' => true, |
| 193 |
'userData' => $userData, |
| 194 |
'site_url' => site_url(), |
| 195 |
'site_title' => get_bloginfo('name'), |
| 196 |
'site_logo' => fluent_boards_site_logo(), |
| 197 |
]; |
| 198 |
} else { |
| 199 |
// this is a subtask |
| 200 |
$task = $task->parentTask($task->parent_id); |
| 201 |
|
| 202 |
$taskUrl = $page_url.'boards/'.$board->id.'/tasks/' |
| 203 |
.$task->id.'-'.substr($task->title, 0, 10); |
| 204 |
$taskLinkTag = '<a target="_blank" href="' |
| 205 |
.htmlspecialchars($taskUrl).'">' |
| 206 |
.htmlspecialchars($task->title).'</a>'; |
| 207 |
|
| 208 |
// translators: %1$s is the subtask title, %2$s is the task link, %3$s is the board link |
| 209 |
$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); |
| 210 |
|
| 211 |
$data = [ |
| 212 |
'body' => $bodyText, |
| 213 |
'pre_header' => __('you have been assigned to subtask','fluent-boards'), |
| 214 |
'show_footer' => true, 'user' => $assignee, |
| 215 |
'userData' => $userData, |
| 216 |
'site_url' => site_url(), |
| 217 |
'site_title' => get_bloginfo('name'), |
| 218 |
'site_logo' => fluent_boards_site_logo(), |
| 219 |
]; |
| 220 |
} |
| 221 |
|
| 222 |
$mailSubject = __('You have been assigned to task','fluent-boards'); |
| 223 |
$message = Helper::loadView('emails.assignee2', $data); |
| 224 |
$headers = ['Content-Type: text/html; charset=UTF-8']; |
| 225 |
|
| 226 |
|
| 227 |
\wp_mail($assignee->user_email, $mailSubject, $message, $headers); |
| 228 |
|
| 229 |
} catch (\Exception $e) { |
| 230 |
throw new \Exception(esc_html__('Error in sending mail to new assignees', 'fluent-boards'), 1); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
public function sendEmailForRemoveAssignee( |
| 235 |
$taskId, |
| 236 |
$newAssigneeId, |
| 237 |
$current_user_id |
| 238 |
) { |
| 239 |
try { |
| 240 |
$task = Task::findOrFail($taskId); |
| 241 |
$board = $task->board; |
| 242 |
$assignee = User::findOrFail($newAssigneeId); |
| 243 |
|
| 244 |
$userData = $this->getUserData($current_user_id); |
| 245 |
|
| 246 |
$page_url = fluent_boards_page_url(); |
| 247 |
|
| 248 |
$boardUrl = $page_url.'boards/'.$board->id; |
| 249 |
$boardLinkTag = '<a target="_blank" href="' |
| 250 |
.htmlspecialchars($boardUrl).'">' |
| 251 |
.htmlspecialchars($board->title).'</a>'; |
| 252 |
|
| 253 |
if (null == $task->parent_id) { |
| 254 |
// this is a task |
| 255 |
$taskUrl = $page_url.'boards/'.$board->id.'/tasks/' |
| 256 |
.$task->id.'-'.substr($task->title, 0, 10); |
| 257 |
$taskLinkTag = '<a target="_blank" href="' |
| 258 |
.htmlspecialchars($taskUrl).'">' |
| 259 |
.htmlspecialchars($task->title).'</a>'; |
| 260 |
// translators: %1$s is the task link, %2$s is the board link |
| 261 |
$bodyText = sprintf(__('has removed you from task %1$s on the board %2$s', 'fluent-boards'), $taskLinkTag, $boardLinkTag); |
| 262 |
|
| 263 |
$data = [ |
| 264 |
'body' => $bodyText, |
| 265 |
'pre_header' => __('you have been removed from task','fluent-boards'), |
| 266 |
'show_footer' => true, |
| 267 |
'userData' => $userData, |
| 268 |
'site_url' => site_url(), |
| 269 |
'site_title' => get_bloginfo('name'), |
| 270 |
'site_logo' => fluent_boards_site_logo(), |
| 271 |
]; |
| 272 |
} else { |
| 273 |
// this is a subtask |
| 274 |
$task = $task->parentTask($task->parent_id); |
| 275 |
|
| 276 |
$taskUrl = $page_url.'boards/'.$board->id.'/tasks/' |
| 277 |
.$task->id.'-'.substr($task->title, 0, 10); |
| 278 |
$taskLinkTag = '<a target="_blank" href="' |
| 279 |
.htmlspecialchars($taskUrl).'">' |
| 280 |
.htmlspecialchars($task->title).'</a>'; |
| 281 |
|
| 282 |
// translators: %1$s is the subtask title, %2$s is the task link, %3$s is the board link |
| 283 |
$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); |
| 284 |
|
| 285 |
$data = [ |
| 286 |
'body' => $bodyText, |
| 287 |
'pre_header' => __('you have been removed from subtask','fluent-boards'), |
| 288 |
'show_footer' => true, |
| 289 |
'userData' => $userData, |
| 290 |
]; |
| 291 |
} |
| 292 |
|
| 293 |
$mailSubject = __('You have been removed from task','fluent-boards'); |
| 294 |
$message = Helper::loadView('emails.assignee2', $data); |
| 295 |
$headers = ['Content-Type: text/html; charset=UTF-8']; |
| 296 |
|
| 297 |
\wp_mail($assignee->user_email, $mailSubject, $message, $headers); |
| 298 |
|
| 299 |
} catch (\Exception $e) { |
| 300 |
throw new \Exception(esc_html__('Error in sending mail to new assignees', 'fluent-boards'), 1); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
public function sendEmailForChangeStage( |
| 306 |
$taskId, |
| 307 |
$newAssigneeEmails, |
| 308 |
$current_user_id |
| 309 |
) { |
| 310 |
try { |
| 311 |
$task = Task::findOrFail($taskId); |
| 312 |
$board = $task->board; |
| 313 |
|
| 314 |
$userData = $this->getUserData($current_user_id); |
| 315 |
|
| 316 |
$page_url = fluent_boards_page_url(); |
| 317 |
$boardUrl = $page_url.'boards/'.$board->id; |
| 318 |
$boardLinkTag = '<a target="_blank" href="' |
| 319 |
.htmlspecialchars($boardUrl).'">' |
| 320 |
.htmlspecialchars($board->title).'</a>'; |
| 321 |
|
| 322 |
$taskUrl = $page_url.'boards/'.$board->id.'/tasks/'.$task->id |
| 323 |
.'-'.substr($task->title, 0, 10); |
| 324 |
$taskLinkTag = '<a target="_blank" href="' |
| 325 |
.htmlspecialchars($taskUrl).'">' |
| 326 |
.htmlspecialchars($task->title).'</a>'; |
| 327 |
|
| 328 |
// translators: %1$s is the task link, %2$s is the stage title, %3$s is the board link |
| 329 |
$bodyText = sprintf(__('has moved %1$s task to <strong>%2$s</strong> stage of board %3$s', 'fluent-boards'), $taskLinkTag, $task->stage->title, $boardLinkTag); |
| 330 |
|
| 331 |
$data = [ |
| 332 |
'body' => $bodyText, |
| 333 |
'pre_header' => __('Task stage has been changed','fluent-boards'), |
| 334 |
'show_footer' => true, |
| 335 |
'userData' => $userData, |
| 336 |
'site_url' => site_url(), |
| 337 |
'site_title' => get_bloginfo('name'), |
| 338 |
'site_logo' => fluent_boards_site_logo(), |
| 339 |
]; |
| 340 |
|
| 341 |
|
| 342 |
$mailSubject = __('Task stage has been changed','fluent-boards'); |
| 343 |
$message = Helper::loadView('emails.assignee2', $data); |
| 344 |
$headers = ['Content-Type: text/html; charset=UTF-8']; |
| 345 |
|
| 346 |
foreach ($newAssigneeEmails as $assignee_email) { |
| 347 |
\wp_mail($assignee_email, $mailSubject, $message, $headers); |
| 348 |
} |
| 349 |
} catch (\Exception $e) { |
| 350 |
// Silent fail for email sending |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
public function sendEmailForDueDateUpdate( |
| 355 |
$taskId, |
| 356 |
$newAssigneeEmails, |
| 357 |
$current_user_id |
| 358 |
) { |
| 359 |
try { |
| 360 |
$task = Task::findOrFail($taskId); |
| 361 |
$board = $task->board; |
| 362 |
|
| 363 |
$userData = $this->getUserData($current_user_id); |
| 364 |
|
| 365 |
$page_url = fluent_boards_page_url(); |
| 366 |
$boardUrl = $page_url.'boards/'.$board->id; |
| 367 |
$boardLinkTag = '<a target="_blank" href="' |
| 368 |
.htmlspecialchars($boardUrl).'">' |
| 369 |
.htmlspecialchars($board->title).'</a>'; |
| 370 |
|
| 371 |
$taskUrl = $page_url.'boards/'.$board->id.'/tasks/'.$task->id |
| 372 |
.'-'.substr($task->title, 0, 10); |
| 373 |
$taskLinkTag = '<a target="_blank" href="' |
| 374 |
.htmlspecialchars($taskUrl).'">' |
| 375 |
.htmlspecialchars($task->title).'</a>'; |
| 376 |
|
| 377 |
// translators: %1$s is the task link, %2$s is the due date, %3$s is the board link |
| 378 |
$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); |
| 379 |
|
| 380 |
$data = [ |
| 381 |
'body' => $bodyText, |
| 382 |
'pre_header' => __('Task due date has been changed','fluent-boards'), |
| 383 |
'show_footer' => true, |
| 384 |
'userData' => $userData, |
| 385 |
'site_url' => site_url(), |
| 386 |
'site_title' => get_bloginfo('name'), |
| 387 |
'site_logo' => fluent_boards_site_logo(), |
| 388 |
]; |
| 389 |
|
| 390 |
|
| 391 |
$mailSubject = __('Task due date has been changed','fluent-boards'); |
| 392 |
$message = Helper::loadView('emails.assignee2', $data); |
| 393 |
$headers = ['Content-Type: text/html; charset=UTF-8']; |
| 394 |
|
| 395 |
foreach ($newAssigneeEmails as $assignee_email) { |
| 396 |
\wp_mail($assignee_email, $mailSubject, $message, $headers); |
| 397 |
} |
| 398 |
} catch (\Exception $e) { |
| 399 |
throw new \Exception(esc_html__('Error in sending mail to new assignees', 'fluent-boards'), 1); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
public function sendEmailForArchivedTask( |
| 404 |
$taskId, |
| 405 |
$newAssigneeEmails, |
| 406 |
$current_user_id |
| 407 |
) { |
| 408 |
try { |
| 409 |
$task = Task::findOrFail($taskId); |
| 410 |
$board = $task->board; |
| 411 |
|
| 412 |
$userData = $this->getUserData($current_user_id); |
| 413 |
|
| 414 |
$page_url = fluent_boards_page_url(); |
| 415 |
$boardUrl = $page_url.'boards/'.$board->id; |
| 416 |
$boardLinkTag = '<a target="_blank" href="' |
| 417 |
.htmlspecialchars($boardUrl).'">' |
| 418 |
.htmlspecialchars($board->title).'</a>'; |
| 419 |
|
| 420 |
$taskUrl = $page_url.'boards/'.$board->id.'/tasks/'.$task->id |
| 421 |
.'-'.substr($task->title, 0, 10); |
| 422 |
$taskLinkTag = '<a target="_blank" href="' |
| 423 |
.htmlspecialchars($taskUrl).'">' |
| 424 |
.htmlspecialchars($task->title).'</a>'; |
| 425 |
|
| 426 |
// translators: %1$s is the task link, %2$s is the board link |
| 427 |
$bodyText = sprintf(__('has archived %1$s task of board %2$s', 'fluent-boards'), $taskLinkTag, $boardLinkTag); |
| 428 |
|
| 429 |
$data = [ |
| 430 |
'body' => $bodyText, |
| 431 |
'pre_header' => __('Task has been archived','fluent-boards'), |
| 432 |
'show_footer' => true, |
| 433 |
'userData' => $userData, |
| 434 |
'site_url' => site_url(), |
| 435 |
'site_title' => get_bloginfo('name'), |
| 436 |
'site_logo' => fluent_boards_site_logo(), |
| 437 |
]; |
| 438 |
|
| 439 |
|
| 440 |
$mailSubject = __('Task has been archived','fluent-boards'); |
| 441 |
$message = Helper::loadView('emails.assignee2', $data); |
| 442 |
$headers = ['Content-Type: text/html; charset=UTF-8']; |
| 443 |
|
| 444 |
foreach ($newAssigneeEmails as $assignee_email) { |
| 445 |
\wp_mail($assignee_email, $mailSubject, $message, $headers); |
| 446 |
} |
| 447 |
} catch (\Exception $e) { |
| 448 |
throw new \Exception(esc_html__('Error in sending mail to new assignees', 'fluent-boards'), 1); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
private function getUserData($userId) |
| 453 |
{ |
| 454 |
$currentUser = User::findOrFail($userId); |
| 455 |
$gravaterPhoto = fluent_boards_user_avatar($currentUser->user_email, |
| 456 |
$currentUser->display_name); |
| 457 |
|
| 458 |
return [ |
| 459 |
'display_name' => $currentUser->display_name, |
| 460 |
'photo' => $gravaterPhoto, |
| 461 |
]; |
| 462 |
} |
| 463 |
|
| 464 |
} |
| 465 |
|