PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.7.1
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.7.1
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 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Notifications / Notifications.php
wp-staging / Notifications Last commit date
Interfaces 2 years ago Transporter 8 months ago Notifications.php 1 year ago NotificationsProvider.php 2 years ago
Notifications.php
136 lines
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 * @var string
14 */
15 const OPTION_BACKUP_SCHEDULE_REPORT_EMAIL = 'wpstg_backup_schedules_report_email';
16
17 /**
18 * @var bool
19 */
20 const DISABLE_FOOTER_MESSAGE = false;
21
22 /**
23 * @var bool
24 */
25 const ENABLE_FOOTER_MESSAGE = true;
26
27 /**
28 * @var string
29 */
30 const OPTION_SEND_EMAIL_AS_HTML = 'wpstg_send_email_as_html';
31
32 /**
33 * @var object
34 */
35 private $transporter;
36
37 /**
38 * @var NotificationsProvider
39 */
40 private $notificationProvider;
41
42 /**
43 * @param NotificationsProvider $notificationProvider
44 */
45 public function __construct(NotificationsProvider $notificationProvider)
46 {
47 $this->notificationProvider = $notificationProvider;
48 $this->getTransporter();
49 }
50
51 /**
52 * @return void
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 * @param string $to Recipient
67 * @param string $subject Subject
68 * @param string $message Content
69 * @param string $from (Optional) Sender
70 * @param array $attachments (Optional) Attachments
71 * @param bool $isAddFooterMessage (Optional) Enable footer message
72 * @return bool
73 */
74 public function sendEmail(string $to, string $subject, string $message, string $from = '', array $attachments = [], bool $isAddFooterMessage = self::ENABLE_FOOTER_MESSAGE): bool
75 {
76 if (empty($this->transporter->emailNotification) || !is_object($this->transporter->emailNotification)) {
77 return false;
78 }
79
80 $this->transporter->emailNotification->setSender($from)
81 ->setRecipient($to)
82 ->setSubject($subject)
83 ->setAttachment($attachments)
84 ->setIsAddFooterMessage($isAddFooterMessage);
85
86 return $this->transporter->emailNotification->send($message);
87 }
88
89 /**
90 * @param string $webhook Slack Webhook
91 * @param string $title title
92 * @param string $message Content
93 * @param bool $isAddFooterMessage (Optional) Enable footer message
94 * @return bool
95 */
96 public function sendSlack(string $webhook, string $title, string $message, bool $isAddFooterMessage = self::ENABLE_FOOTER_MESSAGE): bool
97 {
98 if (empty($this->transporter->slackNotification) || !is_object($this->transporter->slackNotification)) {
99 return false;
100 }
101
102 $this->transporter->slackNotification->setWebhook($webhook)
103 ->setTitle($title)
104 ->setIsAddFooterMessage($isAddFooterMessage);
105
106 return $this->transporter->slackNotification->send($message);
107 }
108
109 /**
110 * @param string $to Recipient
111 * @param string $subject Subject
112 * @param string $message Content
113 * @param string $from (Optional) Sender
114 * @param array $details (Optional) Details
115 * @param array $attachments (Optional) Attachments
116 * @return bool
117 */
118 public function sendEmailAsHTML(string $to, string $subject, string $message = '', string $from = '', array $details = [], array $attachments = []): bool
119 {
120 if (empty($this->transporter->emailNotification) || !is_object($this->transporter->emailNotification)) {
121 return false;
122 }
123
124 $this->transporter->emailNotification->setUseHtml(true);
125 $templateEngine = WPStaging::make(TemplateEngine::class);
126 $emailTemplate = EmailTemplateBuilder::create($templateEngine)
127 ->setTitle($subject)
128 ->setRecipient($to)
129 ->setMessage($message)
130 ->setDetails($details)
131 ->generate();
132
133 return $this->sendEmail($to, $subject, $emailTemplate, $from, $attachments, false);
134 }
135 }
136