PluginProbe
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.12.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.12.0
4.12.0 4.11.2 4.11.1 4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 All 67 releases
wp-staging / Notifications / Notifications.php
Notifications.php
139 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPStaging\Notifications;
4
5 use WPStaging\Core\WPStaging;
6 use WPStaging\Framework\TemplateEngine\TemplateEngine;
7 use WPStaging\Notifications\NotificationsProvider;
8 use WPStaging\Notifications\Transporter\EmailTemplateBuilder;
9
10 class Notifications
11 {
12
13
14
15 const OPTION_BACKUP_SCHEDULE_REPORT_EMAIL = 'wpstg_backup_schedules_report_email';
16
17
18
19
20 const DISABLE_FOOTER_MESSAGE = false;
21
22
23
24
25 const ENABLE_FOOTER_MESSAGE = true;
26
27
28
29
30 const OPTION_SEND_EMAIL_AS_HTML = 'wpstg_send_email_as_html';
31
32
33
34
35 private $transporter;
36
37
38
39
40 private $notificationProvider;
41
42
43
44
45 public function __construct(NotificationsProvider $notificationProvider)
46 {
47 $this->notificationProvider = $notificationProvider;
48 $this->getTransporter();
49 }
50
51
52
53
54 private function getTransporter()
55 {
56 $providers = $this->notificationProvider->getProviders();
57
58 $this->transporter = new \stdClass();
59 foreach ($providers as $provider) {
60 $providerName = lcfirst(basename(str_replace('\\', '/', $provider)));
61 $this->transporter->{$providerName} = WPStaging::make($provider);
62 }
63 }
64
65
66
67
68
69
70
71
72
73
74
75 public function sendEmail(string $to, string $subject, string $message, string $from = '', array $attachments = [], bool $isAddFooterMessage = self::ENABLE_FOOTER_MESSAGE, string $replyTo = ''): bool
76 {
77 if (empty($this->transporter->emailNotification) || !is_object($this->transporter->emailNotification)) {
78 return false;
79 }
80
81 $this->transporter->emailNotification->setSender($from)
82 ->setReplyTo($replyTo)
83 ->setRecipient($to)
84 ->setSubject($subject)
85 ->setAttachment($attachments)
86 ->setIsAddFooterMessage($isAddFooterMessage);
87
88 return $this->transporter->emailNotification->send($message);
89 }
90
91
92
93
94
95
96
97
98 public function sendSlack(string $webhook, string $title, string $message, bool $isAddFooterMessage = self::ENABLE_FOOTER_MESSAGE): bool
99 {
100 if (empty($this->transporter->slackNotification) || !is_object($this->transporter->slackNotification)) {
101 return false;
102 }
103
104 $this->transporter->slackNotification->setWebhook($webhook)
105 ->setTitle($title)
106 ->setIsAddFooterMessage($isAddFooterMessage);
107
108 return $this->transporter->slackNotification->send($message);
109 }
110
111
112
113
114
115
116
117
118
119
120
121 public function sendEmailAsHTML(string $to, string $subject, string $message = '', string $from = '', array $details = [], array $attachments = [], string $replyTo = ''): bool
122 {
123 if (empty($this->transporter->emailNotification) || !is_object($this->transporter->emailNotification)) {
124 return false;
125 }
126
127 $this->transporter->emailNotification->setUseHtml(true);
128 $templateEngine = WPStaging::make(TemplateEngine::class);
129 $emailTemplate = EmailTemplateBuilder::create($templateEngine)
130 ->setTitle($subject)
131 ->setRecipient($to)
132 ->setMessage($message)
133 ->setDetails($details)
134 ->generate();
135
136 return $this->sendEmail($to, $subject, $emailTemplate, $from, $attachments, false, $replyTo);
137 }
138 }
139