PluginProbe
WP Offload SES Lite / 1.3
WP Offload SES Lite v1.3
1.8.2 trunk 0.1.2 0.2.1 0.7.2.1 0.8.2 1.0 1.1 1.2 1.2.1 1.2.2 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
wp-ses / classes / Settings.php

Settings.php in WP Offload SES Lite 1.3, at classes/Settings.php

502 lines 13.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handle settings for WP Offload SES
4 *
5 * @author Delicious Brains
6 * @package WP Offload SES
7 */
8
9 namespace DeliciousBrains\WP_Offload_SES;
10
11 use DeliciousBrains\WP_Offload_SES\WP_Offload_SES;
12 use DeliciousBrains\WP_Offload_SES\Utils;
13
14 /**
15 * Class Settings
16 *
17 * @since 1.0.0
18 */
19 class Settings {
20
21 /**
22 * The settings key used in the database.
23 *
24 * @var string
25 */
26 private $settings_key;
27
28 /**
29 * The settings constant used in defines.
30 *
31 * @var string
32 */
33 private $settings_constant;
34
35 /**
36 * The settings array.
37 *
38 * @var array
39 */
40 private $settings;
41
42 /**
43 * Settings that have been defined via constants.
44 *
45 * @var array
46 */
47 private $defined_settings;
48
49 /**
50 * Construct the Settings class.
51 *
52 * @param string $settings_key The settings key used in the database.
53 * @param string $settings_constant The settings constant used in defines.
54 */
55 public function __construct( $settings_key, $settings_constant ) {
56 $this->settings_key = $settings_key;
57 $this->settings_constant = $settings_constant;
58 }
59
60 /**
61 * Get the plugin's settings array
62 *
63 * @param bool $force Get the settings fresh.
64 *
65 * @return array
66 */
67 public function get_settings( $force = false ) {
68 if ( is_null( $this->settings ) || $force ) {
69 if ( is_multisite() ) {
70 $network_settings = get_site_option( $this->settings_key, array() );
71
72 if ( Utils::is_network_admin() ) {
73 $this->settings = $this->filter_settings( $network_settings );
74 } else {
75 $subsite_settings = get_option( $this->settings_key, array() );
76 $this->settings = $this->filter_settings( $network_settings, $subsite_settings );
77 }
78 } else {
79 $this->settings = $this->filter_settings( get_option( $this->settings_key, array() ) );
80 }
81 }
82
83 return $this->settings;
84 }
85
86 /**
87 * Helper function for getting the network-level settings.
88 *
89 * @return array
90 */
91 public function get_network_settings() {
92 return $this->filter_settings( get_site_option( $this->settings_key, array() ) );
93 }
94
95 /**
96 * Get all settings that have been defined via constant for the plugin
97 *
98 * @param bool $force Get the settings fresh.
99 *
100 * @return array
101 */
102 public function get_defined_settings( $force = false ) {
103 if ( is_null( $this->defined_settings ) || $force ) {
104 $this->defined_settings = array();
105
106 $unserialized = array();
107
108 if ( defined( $this->settings_constant ) ) {
109 $unserialized = maybe_unserialize( constant( $this->settings_constant ) );
110 $unserialized = is_array( $unserialized ) ? $unserialized : array();
111 }
112
113 foreach ( $unserialized as $key => $value ) {
114 if ( ! in_array( $key, $this->get_settings_whitelist() ) ) {
115 continue;
116 }
117
118 if ( is_bool( $value ) || is_null( $value ) ) {
119 $value = (int) $value;
120 }
121
122 if ( is_numeric( $value ) ) {
123 $value = strval( $value );
124 } else {
125 $value = $this->sanitize_setting( $key, $value );
126 }
127
128 $this->defined_settings[ $key ] = $value;
129 }
130
131 $this->defined_settings = array_merge( $this->defined_settings, $this->get_wpses_defined_settings() );
132
133 $this->listen_for_settings_constant_changes();
134
135 // Normalize the defined settings before saving, so we can detect when a real change happens.
136 ksort( $this->defined_settings );
137 update_site_option( 'wposes_constant_' . $this->settings_constant, $this->defined_settings );
138 }
139
140 return $this->defined_settings;
141 }
142
143 /**
144 * Gets any settings defined using WP SES constants.
145 *
146 * @return array
147 */
148 public function get_wpses_defined_settings() {
149 $constants = array(
150 'aws-access-key-id' => 'WP_SES_ACCESS_KEY',
151 'aws-secret-access-key' => 'WP_SES_SECRET_KEY',
152 'default-email' => 'WP_SES_FROM',
153 'return-path' => 'WP_SES_RETURNPATH',
154 'reply-to' => 'WP_SES_REPLYTO',
155 'send-via-ses' => 'WP_SES_AUTOACTIVATE',
156 'region' => 'WP_SES_ENDPOINT',
157 );
158
159 foreach ( $constants as $key => $constant ) {
160 if ( ! defined( $constant ) ) {
161 unset( $constants[ $key ] );
162 continue;
163 }
164
165 $value = constant( $constant );
166
167 if ( 'region' === $key ) {
168 $value = explode( '.', $value );
169 $region = $value[1];
170 $value = $region;
171 }
172
173 $constants[ $key ] = $value;
174 }
175
176 return $constants;
177 }
178
179 /**
180 * Gets a single setting that has been defined in the plugin settings constant
181 *
182 * @param string $key The key of the setting to get.
183 * @param mixed $default Default to use if not found.
184 *
185 * @return mixed
186 */
187 public function get_defined_setting( $key, $default = '' ) {
188 $defined_settings = $this->get_defined_settings();
189 $setting = isset( $defined_settings[ $key ] ) ? $defined_settings[ $key ] : $default;
190
191 return $setting;
192 }
193
194 /**
195 * Subscribe to changes of the site option used to store the constant-defined settings.
196 */
197 protected function listen_for_settings_constant_changes() {
198 if ( ! has_action( 'update_site_option_' . 'wposes_constant_' . $this->settings_constant, array(
199 $this,
200 'settings_constant_changed',
201 ) ) ) {
202 add_action( 'add_site_option_' . 'wposes_constant_' . $this->settings_constant, array(
203 $this,
204 'settings_constant_added',
205 ), 10, 3 );
206 add_action( 'update_site_option_' . 'wposes_constant_' . $this->settings_constant, array(
207 $this,
208 'settings_constant_changed',
209 ), 10, 4 );
210 }
211 }
212
213 /**
214 * Translate a settings constant option addition into a change.
215 *
216 * @param string $option Name of the option.
217 * @param mixed $value Value the option is being initialized with.
218 * @param int $network_id ID of the network.
219 */
220 public function settings_constant_added( $option, $value, $network_id ) {
221 $db_settings = get_site_option( $this->settings_key, array() );
222 $this->settings_constant_changed( $option, $value, $db_settings, $network_id );
223 }
224
225 /**
226 * Callback for announcing when settings-defined values change.
227 *
228 * @param string $option Name of the option.
229 * @param mixed $new_settings Current value of the option.
230 * @param mixed $old_settings Old value of the option.
231 * @param int $network_id ID of the network.
232 */
233 public function settings_constant_changed( $option, $new_settings, $old_settings, $network_id ) {
234 $old_settings = $old_settings ?: array();
235
236 foreach ( $this->get_settings_whitelist() as $setting ) {
237 $old_value = isset( $old_settings[ $setting ] ) ? $old_settings[ $setting ] : null;
238 $new_value = isset( $new_settings[ $setting ] ) ? $new_settings[ $setting ] : null;
239
240 if ( $old_value !== $new_value ) {
241 /**
242 * Setting-specific hook for setting change.
243 *
244 * @param mixed $new_value
245 * @param mixed $old_value
246 * @param string $setting
247 */
248 do_action( 'wposes_constant_' . $this->settings_constant . '_changed_' . $setting, $new_value, $old_value, $setting );
249
250 /**
251 * Generic hook for setting change.
252 *
253 * @param mixed $new_value
254 * @param mixed $old_value
255 * @param string $setting
256 */
257 do_action( 'wposes_constant_' . $this->settings_constant . '_changed', $new_value, $old_value, $setting );
258 }
259 }
260 }
261
262 /**
263 * Filter the plugin settings array
264 *
265 * @param array $settings The single site/network admin settings to filter.
266 * @param array $subsite_settings Optional settings for a subsite.
267 *
268 * @return array $settings
269 */
270 public function filter_settings( $settings, $subsite_settings = array() ) {
271 $defined_settings = $this->get_defined_settings();
272
273 // Maybe add subsite settings.
274 if ( is_multisite() && ! Utils::is_network_admin() ) {
275 $subsite_settings_enabled = false;
276
277 if ( isset( $settings['enable-subsite-settings'] ) ) {
278 $subsite_settings_enabled = $settings['enable-subsite-settings'];
279 }
280
281 if ( isset( $defined_settings['enable-subsite-settings'] ) ) {
282 $subsite_settings_enabled = $defined_settings['enable-subsite-settings'];
283 }
284
285 if ( $subsite_settings_enabled ) {
286 $settings = array_merge( $settings, $subsite_settings );
287 }
288 }
289
290 if ( empty( $defined_settings ) ) {
291 return $settings;
292 }
293
294 foreach ( $defined_settings as $key => $value ) {
295 $settings[ $key ] = $value;
296 }
297
298 return $settings;
299 }
300
301 /**
302 * Get the whitelisted settings for the plugin.
303 *
304 * @param array $settings_whitelist Default whitelist.
305 *
306 * @return array
307 */
308 public function get_settings_whitelist( $settings_whitelist = array() ) {
309 $settings_whitelist = array(
310 'send-via-ses',
311 'region',
312 'default-email',
313 'default-email-name',
314 'reply-to',
315 'return-path',
316 'log-duration',
317 'completed-setup',
318 'enable-open-tracking',
319 'enable-click-tracking',
320 'enable-subsite-settings',
321 'override-network-settings',
322 'enable-health-report',
323 'health-report-frequency',
324 'health-report-recipients',
325 'health-report-custom-recipients',
326 );
327 return apply_filters( 'wposes_settings_whitelist', $settings_whitelist );
328 }
329
330 /**
331 * List of settings that should skip full sanitize.
332 *
333 * @param array $skip_sanitize_settings Settings to skip santitization.
334 *
335 * @return array
336 */
337 public function get_skip_sanitize_settings( $skip_sanitize_settings = array() ) {
338 return apply_filters( 'wposes_skip_sanitize_settings', $skip_sanitize_settings );
339 }
340
341 /**
342 * Sanitize a setting value, maybe.
343 *
344 * @param string $key Setting to sanitize.
345 * @param mixed $value Value of setting to sanitize.
346 *
347 * @return string
348 */
349 public function sanitize_setting( $key, $value ) {
350 $skip_sanitize = $this->get_skip_sanitize_settings();
351 if ( in_array( $key, $skip_sanitize ) ) {
352 $value = wp_strip_all_tags( $value );
353 } else {
354 $value = sanitize_text_field( $value );
355 }
356
357 return $value;
358 }
359
360 /**
361 * Get a specific setting
362 *
363 * @param string $key The key of the setting to get.
364 * @param string $default The default value if not found.
365 *
366 * @return string
367 */
368 public function get_setting( $key, $default = '' ) {
369 $this->get_settings();
370 if ( isset( $this->settings[ $key ] ) ) {
371 $setting = $this->settings[ $key ];
372 } else {
373 $setting = $default;
374 }
375
376 return apply_filters( 'wposes_get_setting', $setting, $key );
377 }
378
379 /**
380 * Gets arguements used to render a setting view.
381 *
382 * @param string $key Key of the setting.
383 *
384 * @return array
385 */
386 public function get_setting_args( $key ) {
387 /** @var WP_Offload_SES $wp_offload_ses */
388 global $wp_offload_ses;
389
390 $is_defined = $this->get_defined_setting( $key, false );
391
392 $args = array(
393 'key' => $key,
394 'disabled' => false,
395 'disabled_attr' => '',
396 'tr_class' => str_replace( '_', '-', $wp_offload_ses->get_plugin_prefix() . '-' . $key . '-container' ),
397 'setting_msg' => '',
398 'is_defined' => false,
399 );
400
401 if ( false !== $is_defined ) {
402 $args['is_defined'] = true;
403 $args['disabled'] = true;
404 $args['disabled_attr'] = 'disabled="disabled"';
405 $args['tr_class'] .= ' wposes-defined-setting';
406 $args['setting_msg'] = '<span class="wposes-defined-in-config">' . __( 'defined in wp-config.php', 'wp-offload-ses' ) . '</span>';
407 }
408
409 return $args;
410 }
411
412 /**
413 * Delete a setting
414 *
415 * @param string $key The key of the setting to delete.
416 */
417 public function remove_setting( $key ) {
418 $this->get_settings();
419 if ( isset( $this->settings[ $key ] ) ) {
420 unset( $this->settings[ $key ] );
421 }
422 }
423
424 /**
425 * Set up any default settings, after the plugin
426 * has loaded and any migration/upgrade routines have run.
427 *
428 * @return bool
429 */
430 public function set_default_settings() {
431 global $wp_offload_ses;
432
433 // Set up weekly health reports for lite.
434 if ( ! $wp_offload_ses->is_pro() && ( ! is_multisite() || Utils::is_network_admin() ) ) {
435 if ( '' === $this->get_setting( 'enable-health-report', '' ) ) {
436 $this->set_setting( 'enable-health-report', true );
437 $this->set_setting( 'health-report-recipients', 'site-admins' );
438 $this->set_setting( 'health-report-frequency', 'weekly' );
439 }
440 }
441
442 // Set the default log duration.
443 if ( ! $this->get_setting( 'log-duration', false ) ) {
444 $this->set_setting( 'log-duration', 90 );
445 }
446
447 $this->save_settings();
448
449 return true;
450 }
451
452 /**
453 * Set a setting
454 *
455 * @param string $key The key of the setting to set.
456 * @param mixed $value The value of the setting to set.
457 */
458 public function set_setting( $key, $value ) {
459 $this->get_settings();
460
461 $this->settings[ $key ] = $value;
462 }
463
464 /**
465 * Bulk set the settings array
466 *
467 * @param array $settings The settings to set.
468 */
469 public function set_settings( $settings ) {
470 $this->settings = $settings;
471 }
472
473 /**
474 * Save the settings to the database
475 */
476 public function save_settings() {
477 if ( is_array( $this->settings ) ) {
478 ksort( $this->settings );
479 }
480
481 $this->update_site_option( $this->settings_key, $this->settings );
482 }
483
484 /**
485 * Update site option.
486 *
487 * @param string $option The key of the option to update.
488 * @param mixed $value The value of the option.
489 * @param bool $autoload If it should autoload.
490 *
491 * @return bool
492 */
493 public function update_site_option( $option, $value, $autoload = true ) {
494 if ( is_multisite() && Utils::is_network_admin() ) {
495 return update_site_option( $option, $value );
496 }
497
498 return update_option( $option, $value, $autoload );
499 }
500
501 }
502