PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / trunk
JetBackup – Backup, Restore & Migrate vtrunk
3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / Settings / Notifications.php
backup / src / JetBackup / Settings Last commit date
.htaccess 1 year ago Automation.php 10 months ago General.php 3 months ago Integrations.php 4 months ago Logging.php 4 months ago Maintenance.php 4 months ago Notifications.php 4 months ago Performance.php 3 months ago Restore.php 1 year ago Security.php 23 hours ago Settings.php 4 months ago Updates.php 4 months ago index.html 1 year ago web.config 1 year ago
Notifications.php
137 lines
1 <?php
2
3 namespace JetBackup\Settings;
4
5 use JetBackup\Alert\Alert;
6 use JetBackup\Exception\DBException;
7 use JetBackup\Exception\FieldsValidationException;
8 use JetBackup\Wordpress\Wordpress;
9 use SleekDB\Exceptions\InvalidArgumentException;
10 use SleekDB\Exceptions\IOException;
11
12 if (!defined( '__JETBACKUP__')) die('Direct access is not allowed');
13
14 class Notifications extends Settings {
15
16 const SECTION = 'notifications';
17
18 const EMAILS = 'EMAILS';
19 const ALTERNATE_EMAIL = 'ALTERNATE_EMAIL';
20 const ADMIN_EMAIL = 'ADMIN_EMAIL';
21 const NOTIFICATION_LEVELS_FREQUENCY = 'NOTIFICATION_LEVELS_FREQUENCY';
22
23 const NOTIFICATION_FREQUENCY_DISABLED = 0;
24 const NOTIFICATION_FREQUENCY_REAL_TIME = 1;
25 const NOTIFICATION_FREQUENCY_DAILY = 2;
26
27 const NOTIFICATION_FREQUENCIES = [
28 self::NOTIFICATION_FREQUENCY_DISABLED,
29 self::NOTIFICATION_FREQUENCY_DAILY,
30 self::NOTIFICATION_FREQUENCY_REAL_TIME
31 ];
32
33 const NOTIFICATION_FREQUENCY_DEFAULTS = [
34 Alert::LEVEL_INFORMATION => self::NOTIFICATION_FREQUENCY_DISABLED,
35 Alert::LEVEL_WARNING => self::NOTIFICATION_FREQUENCY_DISABLED,
36 Alert::LEVEL_CRITICAL => self::NOTIFICATION_FREQUENCY_REAL_TIME,
37 ];
38
39 const NOTIFICATION_FREQUENCY_NAMES = [
40 self::NOTIFICATION_FREQUENCY_DISABLED => 'Disabled',
41 self::NOTIFICATION_FREQUENCY_REAL_TIME => 'Real Time',
42 self::NOTIFICATION_FREQUENCY_DAILY => 'Daily',
43
44 ];
45
46 /**
47 * @throws DBException
48 * @throws IOException
49 * @throws InvalidArgumentException
50 */
51 public function __construct() {
52 parent::__construct(self::SECTION);
53 }
54
55 /**
56 * @return bool
57 */
58 public function isEmailsEnabled():bool { return (bool) $this->get(self::EMAILS, true); }
59
60 /**
61 * @param bool $value
62 *
63 * @return void
64 */
65 public function setEmailsEnabled(bool $value):void { $this->set(self::EMAILS, $value); }
66
67 /**
68 * @return string
69 */
70 public function getAlternateEmail():string { return $this->get(self::ALTERNATE_EMAIL); }
71
72 /**
73 * @param $value
74 *
75 * @return void
76 */
77 public function setAlternateEmail($value):void { $this->set(self::ALTERNATE_EMAIL, $value); }
78 public function setAlertLevelFrequency(array $value):void { $this->set(self::NOTIFICATION_LEVELS_FREQUENCY, $value); }
79
80 public function getAlertLevelFrequency():array {return $this->get(self::NOTIFICATION_LEVELS_FREQUENCY, self::NOTIFICATION_FREQUENCY_DEFAULTS);}
81
82 /**
83 * @return array
84 */
85 public function getDisplay():array {
86
87 return [
88 self::EMAILS => $this->isEmailsEnabled() ? 1 : 0,
89 self::ALTERNATE_EMAIL => $this->getAlternateEmail(),
90 self::ADMIN_EMAIL => Wordpress::getBlogInfo('admin_email'),
91 self::NOTIFICATION_LEVELS_FREQUENCY => $this->getAlertLevelFrequency(),
92 ];
93 }
94
95 /**
96 * @return array
97 */
98 public function getDisplayCLI():array {
99
100 $alert_levels = [];
101 foreach($this->getAlertLevelFrequency() as $level => $frequency)
102 $alert_levels[] = Alert::LEVEL_NAMES[$level] . ' - ' . self::NOTIFICATION_FREQUENCY_NAMES[$frequency];
103
104 return [
105 'Emails' => $this->isEmailsEnabled() ? "Yes" : "No",
106 'Alternate Email' => $this->getAlternateEmail(),
107 'Admin Email' => Wordpress::getBlogInfo('admin_email'),
108 'Email Alert Levels' => implode(", ", $alert_levels),
109 ];
110 }
111
112 /**
113 * @throws FieldsValidationException
114 */
115 public function validateFields():void {
116
117 $changedFields = self::getChangedFields($this->getData(), (new Notifications())->getData());
118
119 if(in_array(self::NOTIFICATION_LEVELS_FREQUENCY, $changedFields)) {
120 $frequency = $this->getAlertLevelFrequency();
121
122 foreach ($frequency as $key => $value) {
123
124 if (!($key & Alert::LEVELS))
125 throw new FieldsValidationException("Invalid alert level key: $key");
126
127 if (!in_array($value, self::NOTIFICATION_FREQUENCIES))
128 throw new FieldsValidationException("Invalid frequency value for $key: $value");
129 }
130 }
131
132 if(in_array(self::ALTERNATE_EMAIL, $changedFields)) {
133 if ($this->getAlternateEmail() && !Wordpress::isEmail($this->getAlternateEmail()))
134 throw new FieldsValidationException("Email " . $this->getAlternateEmail() . " is not valid");
135 }
136 }
137 }