Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/fluent-smtp/app/Models/Settings.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + namespace FluentMail\App\Models;
4 +
5 + use FluentMail\Includes\Support\Arr;
6 + use FluentMail\App\Services\Mailer\Manager;
7 + use FluentMail\App\Models\Traits\SendTestEmailTrait;
8 +
9 + class Settings
10 + {
11 + use SendTestEmailTrait;
12 +
13 + protected $optionName = FLUENTMAIL . '-settings';
14 +
15 + public function get()
16 + {
17 + return fluentMailGetSettings();
18 + }
19 +
20 + public function getSettings()
21 + {
22 + return $this->get();
23 + }
24 +
25 + public function store($inputs)
26 + {
27 + $settings = $this->getSettings();
28 + $mappings = $this->getMappings($settings);
29 + $connections = $this->getConnections($settings);
30 + $email = Arr::get($inputs, 'connection.sender_email');
31 +
32 + $key = $inputs['connection_key'];
33 +
34 + if (isset($connections[$key])) {
35 + $mappings = array_filter($mappings, function ($mappingKey) use ($key) {
36 + return $mappingKey != $key;
37 + });
38 + unset($connections[$key]);
39 + }
40 +
41 + $primaryEmails = [];
42 + foreach ($connections as $connection) {
43 + $primaryEmails[] = $connection['provider_settings']['sender_email'];
44 + }
45 +
46 + $uniqueKey = $this->generateUniqueKey($email);
47 +
48 + $extraMappings = $inputs['valid_senders'];
49 +
50 + foreach ($extraMappings as $emailIndex => $email) {
51 + if (in_array($email, $primaryEmails)) {
52 + unset($extraMappings[$emailIndex]);
53 + }
54 + }
55 +
56 + $extraMappings[] = $email;
57 + $extraMappings = array_unique($extraMappings);
58 + $extraMappings = array_fill_keys($extraMappings, $uniqueKey);
59 +
60 + $mappings = array_merge($mappings, $extraMappings);
61 +
62 + $providers = fluentMail(Manager::class)->getConfig('providers');
63 +
64 + $title = $providers[$inputs['connection']['provider']]['title'];
65 +
66 + $connections[$uniqueKey] = [
67 + 'title' => $title,
68 + 'provider_settings' => $inputs['connection']
69 + ];
70 +
71 + $settings['mappings'] = $mappings;
72 +
73 + $settings['connections'] = $connections;
74 +
75 + if ($settings['mappings'] && $settings['connections']) {
76 + $validMappings = array_keys(Arr::get($settings, 'connections', []));
77 +
78 + $settings['mappings'] = array_filter($settings['mappings'], function ($key) use ($validMappings) {
79 + return in_array($key, $validMappings);
80 + });
81 + }
82 +
83 + $misc = $this->getMisc();
84 +
85 + if (!$misc) {
86 + $misc = [
87 + 'log_emails' => 'yes',
88 + 'log_saved_interval_days' => '14',
89 + 'disable_fluentcrm_logs' => 'no',
90 + 'default_connection' => ''
91 + ];
92 + }
93 +
94 + if (empty($misc['default_connection']) || $misc['default_connection'] == $key) {
95 + $misc['default_connection'] = $uniqueKey;
96 + $settings['misc'] = $misc;
97 + }
98 +
99 + fluentMailSetSettings($settings);
100 +
101 + return $settings;
102 + }
103 +
104 + public function generateUniqueKey($email)
105 + {
106 + return md5($email);
107 + }
108 +
109 + public function saveGlobalSettings($data)
110 + {
111 + return fluentMailSetSettings($data);
112 + }
113 +
114 + public function delete($key)
115 + {
116 + $settings = $this->getSettings();
117 +
118 +
119 + $mappings = $settings['mappings'];
120 + $connections = $settings['connections'];
121 +
122 + unset($connections[$key]);
123 +
124 + foreach ($mappings as $mapKey => $mapValue) {
125 + if ($mapValue == $key) {
126 + unset($mappings[$mapKey]);
127 + }
128 + }
129 +
130 + $settings['mappings'] = $mappings;
131 + $settings['connections'] = $connections;
132 +
133 + if (Arr::get($settings, 'misc.default_connection') == $key) {
134 + $default = Arr::get($settings, 'mappings', []);
135 + $default = reset($default);
136 + Arr::set($settings, 'misc.default_connection', $default ?: '');
137 + }
138 +
139 + if (Arr::get($settings, 'misc.fallback_connection') == $key) {
140 + Arr::set($settings, 'misc.fallback_connection', '');
141 + }
142 +
143 + fluentMailSetSettings($settings);
144 +
145 + return $settings;
146 + }
147 +
148 + public function getDefaults()
149 + {
150 + $url = str_replace(
151 + ['http://', 'http://www.', 'www.'],
152 + '',
153 + get_bloginfo('wpurl')
154 + );
155 +
156 + return [
157 + 'sender_name' => $url,
158 + 'sender_email' => get_option('admin_email')
159 + ];
160 + }
161 +
162 + public function getVerifiedEmails()
163 + {
164 + $optionName = FLUENTMAIL . '-ses-verified-emails';
165 +
166 + return get_option($optionName, []);
167 + }
168 +
169 + public function saveVerifiedEmails($verifiedEmails)
170 + {
171 + $optionName = FLUENTMAIL . '-ses-verified-emails';
172 + $emails = get_option($optionName, []);
173 + update_option($optionName, array_unique(array_merge(
174 + $emails, $verifiedEmails
175 + )));
176 + }
177 +
178 + public function getConnections($settings = null)
179 + {
180 + $settings = $settings ?: $this->getSettings();
181 +
182 + return Arr::get($settings, 'connections', []);
183 + }
184 +
185 + public function getMappings($settings = null)
186 + {
187 + $settings = $settings ?: $this->getSettings();
188 +
189 + return Arr::get($settings, 'mappings', []);
190 + }
191 +
192 + public function getMisc($settings = null)
193 + {
194 + $settings = $settings ?: $this->getSettings();
195 +
196 + return Arr::get($settings, 'misc', []);
197 + }
198 +
199 + public function getConnection($email)
200 + {
201 + $settings = $this->getSettings();
202 + $mappings = $this->getMappings($settings);
203 + $connections = $this->getConnections($settings);
204 +
205 + if (isset($mappings[$email])) {
206 + if (isset($connections[$mappings[$email]])) {
207 + return $connections[$mappings[$email]];
208 + }
209 + }
210 +
211 + return [];
212 + }
213 +
214 + public function updateMiscSettings($misc)
215 + {
216 + $settings = $this->get();
217 + $settings['misc'] = $misc;
218 + $this->saveGlobalSettings($settings);
219 + }
220 +
221 + public function updateConnection($fromEmail, $connection)
222 + {
223 + $key = $this->generateUniqueKey($fromEmail);
224 + $settings = $this->getSettings();
225 + $settings['connections'][$key]['provider_settings'] = $connection;
226 + $this->saveGlobalSettings($settings);
227 + }
228 +
229 + public function notificationSettings()
230 + {
231 + $defaults = [
232 + 'enabled' => 'no',
233 + 'notify_email' => '{site_admin}',
234 + 'notify_days' => ['Mon'],
235 + 'active_channel' => [],
236 + 'telegram' => [
237 + 'status' => 'no',
238 + 'token' => ''
239 + ],
240 + 'slack' => [
241 + 'status' => 'no',
242 + 'token' => '',
243 + 'webhook_url' => ''
244 + ],
245 + 'discord' => [
246 + 'status' => 'no',
247 + 'channel_name' => '',
248 + 'webhook_url' => ''
249 + ],
250 + ];
251 +
252 + $settings = get_option('_fluent_smtp_notify_settings', []);
253 +
254 + $settings = wp_parse_args($settings, $defaults);
255 +
256 + if (!is_array($settings['active_channel'])) {
257 + $settings['active_channel'] = array_filter([$settings['active_channel']]);
258 + }
259 +
260 + return $settings;
261 + }
262 +
263 + public function getAvailableNotificationChannels()
264 + {
265 + $manager = new \FluentMail\App\Services\Notification\Manager();
266 + return $manager->getAllChannels();
267 + }
268 + }
269 +