1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
<?php
use iThemesSecurity\User_Groups;
abstract class ITSEC_Settings { protected $settings;
public function __construct() { $this->load();
add_action( 'itsec-lib-clear-caches', array( $this, 'load' ), 0 ); }
abstract public function get_id(); abstract public function get_defaults(); protected function after_save() {}
protected function handle_settings_changes( $old_settings ) { $user_group_settings = ITSEC_Modules::get_container()->get( User_Groups\Settings_Registry::class );
foreach ( $user_group_settings->get_settings() as $user_group_setting ) { if ( $user_group_setting->get_module() !== $this->get_id() ) { continue; }
$current = ITSEC_Lib::array_get( $this->settings, $user_group_setting->get_setting() ); $previous = ITSEC_Lib::array_get( $old_settings, $user_group_setting->get_setting() );
if ( $previous !== $current ) { ITSEC_Response::add_store_dispatch( 'ithemes-security/user-groups', 'fetchGroupsSettings' ); break; } } }
public function export() { return $this->settings; }
public function import( $settings ) { $this->set_all( $settings ); }
public function get( $name, $default = null ) { if ( isset( $this->settings[$name] ) ) { return $this->settings[$name]; }
return $default; }
public function get_all() { return $this->settings; }
public function set( $name, $value ) { $settings = $this->settings; $settings[$name] = $value;
return $this->set_all( $settings ); }
public function set_all( $settings ) { $retval = array( 'old_settings' => $this->settings, 'new_settings' => $this->settings, 'errors' => array(), 'messages' => array(), 'saved' => false, );
$validator = ITSEC_Modules::get_validator( $this->get_id() );
if ( is_null( $validator ) ) { $retval['errors'][] = new WP_Error( 'itsec-settings-missing-validator-for-' . $this->get_id(), sprintf( __( 'The data validator for %1$s is missing. Data for the module cannot be saved without the validator. This error could indicate a bad install of iThemes Security. Please remove the plugin and reinstall it. If this message persists, please contact support and send them this error message.', 'better-wp-security' ), $this->get_id() ) ); } else { $validator->validate( $settings );
$retval['errors'] = $validator->get_errors(); $retval['messages'] = $validator->get_messages();
if ( $validator->can_save() ) { $this->settings = $validator->get_settings();
ITSEC_Storage::set( $this->get_id(), $this->settings ); $this->after_save(); $this->handle_settings_changes( $retval['old_settings'] );
$retval['new_settings'] = $this->settings; $retval['saved'] = true;
do_action( 'itsec-settings-updated', $this->get_id() ); } else { ITSEC_Response::set_success( false ); } }
ITSEC_Response::add_errors( $retval['errors'] ); ITSEC_Response::add_messages( $retval['messages'] );
return $retval; }
public function load() { $this->settings = ITSEC_Storage::get( $this->get_id() ); $defaults = $this->get_defaults();
if ( ! is_array( $this->settings ) ) { $this->settings = array(); }
$this->settings = array_merge( $defaults, $this->settings ); } }
|