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
|
|
|
|
8
|
|
|
class User_Meta_Container extends Container { |
9
|
|
|
protected $user_id; |
10
|
|
|
|
11
|
|
|
public $settings = array( |
12
|
|
|
'show_on' => array( |
13
|
|
|
'role' => array() |
14
|
|
|
) |
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Create a new user meta container |
19
|
|
|
* |
20
|
|
|
* @param string $title Unique title of the container |
21
|
|
|
**/ |
22
|
1 |
|
public function __construct( $title ) { |
23
|
1 |
|
parent::__construct( $title ); |
24
|
|
|
|
25
|
|
|
if ( ! $this->get_datastore() ) { |
26
|
|
|
$this->set_datastore( new User_Meta_Datastore() ); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Bind attach() and save() to the appropriate WordPress actions. |
32
|
|
|
**/ |
33
|
|
|
public function init() { |
34
|
|
|
add_action( 'admin_init', array( $this, '_attach' ) ); |
35
|
|
|
add_action( 'profile_update', array( $this, '_save' ), 10, 1 ); |
36
|
|
|
add_action( 'user_register', array( $this, '_save' ), 10, 1 ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Perform save operation after successful is_valid_save() check. |
41
|
|
|
* The call is propagated to all fields in the container. |
42
|
|
|
* |
43
|
|
|
* @param int $user_id ID of the user against which save() is ran |
44
|
|
|
**/ |
45
|
|
View Code Duplication |
public function save( $user_id ) { |
|
|
|
|
46
|
|
|
// Unhook action to garantee single save |
47
|
|
|
remove_action( 'profile_update', array( $this, '_save' ) ); |
48
|
|
|
|
49
|
|
|
$this->set_user_id( $user_id ); |
50
|
|
|
|
51
|
|
|
foreach ( $this->fields as $field ) { |
52
|
|
|
$field->set_value_from_input(); |
53
|
|
|
$field->save(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
do_action( 'carbon_after_save_user_meta', $user_id ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Checks whether the current request is valid |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
**/ |
64
|
|
|
public function is_valid_save( $user_id = 0 ) { |
65
|
|
|
if ( ! isset( $_REQUEST[ $this->get_nonce_name() ] ) || ! wp_verify_nonce( $_REQUEST[ $this->get_nonce_name() ], $this->get_nonce_name() ) ) { // Input var okay. |
|
|
|
|
66
|
|
|
return false; |
67
|
|
|
} else if ( ! $this->is_valid_attach() ) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->is_valid_save_conditions( $user_id ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Perform checks whether the current save() request is valid |
76
|
|
|
* |
77
|
|
|
* @param int $user_id ID of the user against which save() is ran |
78
|
|
|
* @return bool |
79
|
|
|
**/ |
80
|
|
|
public function is_valid_save_conditions( $user_id ) { |
81
|
|
|
$valid = true; |
82
|
|
|
$user = get_userdata( $user_id ); |
83
|
|
|
|
84
|
|
|
if ( empty( $user->roles ) ) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Check user role |
89
|
|
|
if ( ! empty( $this->settings['show_on']['role'] ) ) { |
90
|
|
|
$allowed_roles = (array) $this->settings['show_on']['role']; |
91
|
|
|
|
92
|
|
|
// array_shift removed the returned role from the $user_profile->roles |
93
|
|
|
// $roles_to_shift prevents changing of the $user_profile->roles variable |
94
|
|
|
$roles_to_shift = $user->roles; |
95
|
|
|
$profile_role = array_shift( $roles_to_shift ); |
96
|
|
|
if ( ! in_array( $profile_role, $allowed_roles ) ) { |
97
|
|
|
$valid = false; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $valid; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Show the container only on users who have the $role role. |
106
|
|
|
* |
107
|
|
|
* @param string $role |
108
|
|
|
* @return object $this |
109
|
|
|
**/ |
110
|
|
|
public function show_on_user_role( $role ) { |
111
|
|
|
$this->settings['show_on']['role'] = (array) $role; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Add the container to the user |
118
|
|
|
**/ |
119
|
|
|
public function attach() { |
120
|
|
|
add_action( 'show_user_profile', array( $this, 'render' ), 10, 1 ); |
121
|
|
|
add_action( 'edit_user_profile', array( $this, 'render' ), 10, 1 ); |
122
|
|
|
add_action( 'user_new_form', array( $this, 'render' ), 10, 1 ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Whether we're on the user profile page |
127
|
|
|
**/ |
128
|
|
|
public function is_profile_page() { |
129
|
|
|
global $pagenow; |
130
|
|
|
|
131
|
|
|
return $pagenow === 'profile.php' || $pagenow === 'user-new.php' || $pagenow === 'user-edit.php'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Perform checks whether the container should be attached during the current request |
136
|
|
|
* |
137
|
|
|
* @return bool True if the container is allowed to be attached |
138
|
|
|
**/ |
139
|
|
|
public function is_valid_attach() { |
140
|
|
|
if ( ! current_user_can( 'edit_users' ) || ! $this->is_profile_page() ) { |
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return true; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Output the container markup |
149
|
|
|
**/ |
150
|
|
|
public function render( $user_profile = null ) { |
151
|
|
|
$profile_role = ''; |
152
|
|
|
|
153
|
|
|
if ( is_object( $user_profile ) ) { |
154
|
|
|
$this->set_user_id( $user_profile->ID ); |
155
|
|
|
|
156
|
|
|
// array_shift removed the returned role from the $user_profile->roles |
157
|
|
|
// $roles_to_shift prevents changing of the $user_profile->roles variable |
158
|
|
|
$roles_to_shift = $user_profile->roles; |
159
|
|
|
$profile_role = array_shift( $roles_to_shift ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
include \Carbon_Fields\DIR . '/templates/Container/user_meta.php'; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Set the user ID the container will operate with. |
167
|
|
|
* |
168
|
|
|
* @param int $user_id |
169
|
|
|
**/ |
170
|
|
|
public function set_user_id( $user_id ) { |
171
|
|
|
$this->user_id = $user_id; |
172
|
|
|
$this->store->set_id( $user_id ); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.