1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Container; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Datastore\Meta_Datastore; |
6
|
|
|
use Carbon_Fields\Datastore\User_Meta_Datastore; |
7
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
8
|
|
|
|
9
|
|
|
class User_Meta_Container extends Container { |
10
|
|
|
protected $user_id; |
11
|
|
|
|
12
|
|
|
public $settings = array( |
13
|
|
|
'show_on' => array( |
14
|
|
|
'role' => array(), |
15
|
|
|
), |
16
|
|
|
'show_for' => array( |
17
|
|
|
'relation' => 'AND', |
18
|
|
|
'edit_users', |
19
|
|
|
), |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Create a new user meta container |
24
|
|
|
* |
25
|
|
|
* @param string $title Unique title of the container |
26
|
|
|
**/ |
27
|
|
|
public function __construct( $title ) { |
28
|
|
|
parent::__construct( $title ); |
29
|
|
|
|
30
|
|
|
if ( ! $this->get_datastore() ) { |
31
|
|
|
$this->set_datastore( new User_Meta_Datastore(), $this->has_default_datastore() ); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Bind attach() and save() to the appropriate WordPress actions. |
37
|
|
|
**/ |
38
|
|
|
public function init() { |
39
|
|
|
add_action( 'admin_init', array( $this, '_attach' ) ); |
40
|
|
|
add_action( 'profile_update', array( $this, '_save' ), 10, 1 ); |
41
|
|
|
add_action( 'user_register', array( $this, '_save' ), 10, 1 ); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Add the container to the user |
46
|
|
|
**/ |
47
|
|
|
public function attach() { |
48
|
|
|
add_action( 'show_user_profile', array( $this, 'render' ), 10, 1 ); |
49
|
|
|
add_action( 'edit_user_profile', array( $this, 'render' ), 10, 1 ); |
50
|
|
|
add_action( 'user_new_form', array( $this, 'render' ), 10, 1 ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Whether we're on the user profile page |
55
|
|
|
**/ |
56
|
|
|
public function is_profile_page() { |
57
|
|
|
global $pagenow; |
58
|
|
|
|
59
|
|
|
return $pagenow === 'profile.php' || $pagenow === 'user-new.php' || $pagenow === 'user-edit.php'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Perform checks whether the container should be seen for the currently logged in user |
64
|
|
|
* |
65
|
|
|
* @return bool True if the current user is allowed to see the container |
66
|
|
|
**/ |
67
|
|
|
public function is_valid_show_for() { |
68
|
|
|
$show_for = $this->settings['show_for']; |
69
|
|
|
|
70
|
|
|
$relation = $show_for['relation']; |
71
|
|
|
unset( $show_for['relation'] ); |
72
|
|
|
|
73
|
|
|
$validated_capabilities_count = 0; |
|
|
|
|
74
|
|
|
foreach ( $show_for as $capability ) { |
75
|
|
|
if ( current_user_can( $capability ) ) { |
76
|
|
|
$validated_capabilities_count++; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* When the relation is AND all capabilities must be evaluated to true |
82
|
|
|
* When the relation is OR at least 1 must be evaluated to true |
83
|
|
|
*/ |
84
|
|
|
$min_valid_capabilities_count = $relation === 'AND' ? count( $show_for ) : 1; |
|
|
|
|
85
|
|
|
|
86
|
|
|
return apply_filters( 'carbon_container_user_meta_is_valid_show_for', $validated_capabilities_count >= $min_valid_capabilities_count, $this ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Perform checks whether the container should be attached during the current request |
91
|
|
|
* |
92
|
|
|
* @return bool True if the container is allowed to be attached |
93
|
|
|
**/ |
94
|
|
|
public function is_valid_attach() { |
95
|
|
|
if ( ! $this->is_profile_page() || ! $this->is_valid_show_for() ) { |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Perform checks whether the current save() request is valid |
104
|
|
|
* |
105
|
|
|
* @param int $user_id ID of the user against which save() is ran |
106
|
|
|
* @return bool |
107
|
|
|
**/ |
108
|
|
|
public function is_valid_save_conditions( $user_id ) { |
109
|
|
|
$valid = true; |
110
|
|
|
$user = get_userdata( $user_id ); |
111
|
|
|
|
112
|
|
|
if ( empty( $user->roles ) ) { |
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Check user role |
117
|
|
|
if ( ! empty( $this->settings['show_on']['role'] ) ) { |
118
|
|
|
$allowed_roles = (array) $this->settings['show_on']['role']; |
119
|
|
|
|
120
|
|
|
// array_shift removed the returned role from the $user_profile->roles |
121
|
|
|
// $roles_to_shift prevents changing of the $user_profile->roles variable |
122
|
|
|
$roles_to_shift = $user->roles; |
123
|
|
|
$profile_role = array_shift( $roles_to_shift ); |
124
|
|
|
if ( ! in_array( $profile_role, $allowed_roles ) ) { |
125
|
|
|
$valid = false; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $valid; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Checks whether the current request is valid |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
**/ |
137
|
|
|
public function is_valid_save( $user_id = 0 ) { |
138
|
|
View Code Duplication |
if ( ! isset( $_REQUEST[ $this->get_nonce_name() ] ) || ! wp_verify_nonce( $_REQUEST[ $this->get_nonce_name() ], $this->get_nonce_name() ) ) { |
|
|
|
|
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ( ! $this->is_valid_attach() ) { |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $this->is_valid_save_conditions( $user_id ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Perform save operation after successful is_valid_save() check. |
151
|
|
|
* The call is propagated to all fields in the container. |
152
|
|
|
* |
153
|
|
|
* @param int $user_id ID of the user against which save() is ran |
154
|
|
|
**/ |
155
|
|
View Code Duplication |
public function save( $user_id ) { |
|
|
|
|
156
|
|
|
// Unhook action to garantee single save |
157
|
|
|
remove_action( 'profile_update', array( $this, '_save' ) ); |
158
|
|
|
|
159
|
|
|
$this->set_user_id( $user_id ); |
160
|
|
|
|
161
|
|
|
foreach ( $this->fields as $field ) { |
162
|
|
|
$field->set_value_from_input(); |
163
|
|
|
$field->save(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
do_action( 'carbon_after_save_user_meta', $user_id ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Output the container markup |
171
|
|
|
**/ |
172
|
|
|
public function render( $user_profile = null ) { |
173
|
|
|
$profile_role = ''; |
174
|
|
|
|
175
|
|
|
if ( is_object( $user_profile ) ) { |
176
|
|
|
$this->set_user_id( $user_profile->ID ); |
177
|
|
|
|
178
|
|
|
// array_shift removed the returned role from the $user_profile->roles |
179
|
|
|
// $roles_to_shift prevents changing of the $user_profile->roles variable |
180
|
|
|
$roles_to_shift = $user_profile->roles; |
181
|
|
|
$profile_role = array_shift( $roles_to_shift ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
include \Carbon_Fields\DIR . '/templates/Container/user_meta.php'; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Set the user ID the container will operate with. |
189
|
|
|
* |
190
|
|
|
* @param int $user_id |
191
|
|
|
**/ |
192
|
|
|
public function set_user_id( $user_id ) { |
193
|
|
|
$this->user_id = $user_id; |
194
|
|
|
$this->get_datastore()->set_id( $user_id ); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Show the container only on users who have the $role role. |
199
|
|
|
* |
200
|
|
|
* @param string $role |
201
|
|
|
* @return object $this |
202
|
|
|
**/ |
203
|
|
|
public function show_on_user_role( $role ) { |
204
|
|
|
$this->settings['show_on']['role'] = (array) $role; |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Show the container only for users who have either capabilities or roles setup |
211
|
|
|
* |
212
|
|
|
* @param array $show_for |
213
|
|
|
* @return object $this |
214
|
|
|
**/ |
215
|
|
|
public function show_for( $show_for ) { |
216
|
|
|
$this->settings['show_for'] = $this->parse_show_for( $show_for ); |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Validate and parse the show_for logic rules. |
223
|
|
|
* |
224
|
|
|
* @param array $rules |
|
|
|
|
225
|
|
|
* @return array |
226
|
|
|
*/ |
227
|
|
|
protected function parse_show_for( $show_for ) { |
228
|
|
|
if ( ! is_array( $show_for ) ) { |
229
|
|
|
Incorrect_Syntax_Exception::raise( 'Show for argument should be an array.' ); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$allowed_relations = array( 'AND', 'OR' ); |
233
|
|
|
|
234
|
|
|
$parsed_show_for = array( |
235
|
|
|
'relation' => 'AND', |
236
|
|
|
); |
237
|
|
|
|
238
|
|
|
foreach ( $show_for as $key => $rule ) { |
239
|
|
|
// Check if we have a relation key |
240
|
|
View Code Duplication |
if ( $key === 'relation' ) { |
|
|
|
|
241
|
|
|
$relation = strtoupper( $rule ); |
242
|
|
|
|
243
|
|
|
if ( ! in_array( $relation, $allowed_relations ) ) { |
244
|
|
|
Incorrect_Syntax_Exception::raise( 'Invalid relation type ' . $rule . '. ' . |
245
|
|
|
'The rule should be one of the following: "' . implode( '", "', $allowed_relations ) . '"' ); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$parsed_show_for['relation'] = $relation; |
249
|
|
|
continue; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
// Check if the rule is valid |
253
|
|
|
if ( ! is_string( $rule ) || empty( $rule ) ) { |
254
|
|
|
Incorrect_Syntax_Exception::raise( 'Invalid show_for logic rule format. ' . |
255
|
|
|
'The rule should be a string, containing an user capability/role.' ); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$parsed_show_for[] = $rule; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $parsed_show_for; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: