forked from betterangels/better-angels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-buoy-user-settings.php
185 lines (171 loc) · 4.65 KB
/
class-buoy-user-settings.php
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Buoy User Settings
*
* @package WordPress\Plugin\WP_Buoy_Plugin\WP_Buoy_User\WP_Buoy_User_Settings
*
* @copyright Copyright (c) 2015-2016 by Meitar "maymay" Moscovitz
*
* @license https://www.gnu.org/licenses/gpl-3.0.en.html
*/
if (!defined('ABSPATH')) { exit; } // Disallow direct HTTP access.
/**
* Manages user-specific settings for a given user.
*/
class WP_Buoy_User_Settings {
/**
* The user associated with these settings.
*
* @var WP_User
*/
private $user;
/**
* User's current settings.
*
* @var array
*/
private $options;
/**
* List of default values for user profile settings.
*
* @todo Possible values should not be part of default values.
*
* @var array
*/
private $_defaults = array(
// option name => default/possible values
'crisis_message' => '',
'default_team' => false,
'gender_pronoun_possessive' => '',
'installer_dismissed' => false,
'phone_number' => '',
'public_responder' => false,
'sms_provider' => array(
'',
'AT&T',
'Alltel',
'Boost Mobile',
'Cricket',
'Metro PCS',
'Nextel',
'Ptel',
'Qwest',
'Sprint',
'Suncom',
'T-Mobile',
'Tracfone',
'U.S. Cellular',
'Verizon',
'Virgin Mobile'
)
);
/**
* Constructor.
*
* @param int|WP_User $user
*
* @return WP_Buoy_User_Settings
*/
public function __construct ($user) {
if (is_numeric($user)) {
$user = get_userdata($user);
}
$this->user = $user;
$this->options = $this->get_options();
}
/**
* Gets a user setting default, or all defaults.
*
* @param string $key
*
* @return mixed
*/
public function get_defaults ($key = null) {
return (null === $key) ? $this->_defaults : $this->_defaults[$key];
}
/**
* Retrieves user's Buoy settings from the WordPress database.
*
* @return array
*/
private function get_options () {
$opts = array();
foreach ($this->_defaults as $k => $v) {
$opts[$k] = get_user_meta($this->user->ID, WP_Buoy_Plugin::$prefix . '_' . $k, true);
}
return $opts;
}
/**
* Retrieves a user option.
*
* @param string $key
* @param mixed $default The value to return if the option doesn't exist.
*
* @return mixed The current option value, or the $default parameter if the option doesn't exist.
*/
public function get ($key, $default = null) {
return ($this->has($key)) ? $this->options[$key] : $default;
}
/**
* Sets a user option.
*
* Returns the current instance for chaining.
*
* @param string $key
* @param mixed $value
*
* @return WP_Buoy_User_Settings
*/
public function set ($key, $value) {
$this->options[$key] = $value;
return $this;
}
/**
* Checks whether a given option name is set.
*
* @param string $key The option name.
*
* @return bool
*/
public function has ($key) {
return isset($this->options[$key]);
}
/**
* Saves the current options to the database in user meta fields.
*
* If an option doesn't exist in the $options array, after it was
* deleted with WP_Buoy_User_Settings::delete(), for instance, it
* will be deleted from the database, too.
*
* Multiple user meta fields are used so that the WP database is
* more easily queryable to allow, for example, finding all users
* whose phone company is Verizon. This aids in debugging issues.
*
* @uses WP_Buoy_User_Settings::$options
*
* @return WP_Buoy_User_Settings
*/
public function save () {
foreach ($this->_defaults as $k => $v) {
if ($this->has($k)) {
update_user_meta($this->user->ID, WP_Buoy_Plugin::$prefix . '_' . $k, $this->get($k));
} else {
delete_user_meta($this->user->ID, WP_Buoy_Plugin::$prefix . '_' . $k);
}
}
return $this;
}
/**
* Removes an option from the current instance's $options array.
*
* Returns the current instance for chaining.
*
* @param string $key
*
* @return WP_Buoy_User_Settings
*/
public function delete ($key) {
unset($this->options[$key]);
return $this;
}
}