-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathUser_Meta_Command.php
More file actions
211 lines (202 loc) · 5.19 KB
/
User_Meta_Command.php
File metadata and controls
211 lines (202 loc) · 5.19 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Manage user custom fields.
*
* ## EXAMPLES
*
* # Add user meta
* $ wp user meta add 123 bio "Mary is an WordPress developer."
* Success: Added custom field.
*
* # List user meta
* $ wp user meta list 123 --keys=nickname,description,wp_capabilities
* +---------+-----------------+--------------------------------+
* | user_id | meta_key | meta_value |
* +---------+-----------------+--------------------------------+
* | 123 | nickname | supervisor |
* | 123 | description | Mary is a WordPress developer. |
* | 123 | wp_capabilities | {"administrator":true} |
* +---------+-----------------+--------------------------------+
*
* # Update user meta
* $ wp user meta update 123 bio "Mary is an awesome WordPress developer."
* Success: Updated custom field 'bio'.
*
* # Delete user meta
* $ wp user meta delete 123 bio
* Success: Deleted custom field.
*/
class User_Meta_Command extends \WP_CLI\CommandWithMeta {
protected $meta_type = 'user';
public function __construct() {
$this->fetcher = new \WP_CLI\Fetchers\User;
}
/**
* List all metadata associated with a user.
*
* ## OPTIONS
*
* <user>
* : The user login, user email, or user ID of the user to get metadata for.
*
* [--keys=<keys>]
* : Limit output to metadata of specific keys.
*
* [--fields=<fields>]
* : Limit the output to specific row fields. Defaults to id,meta_key,meta_value.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - count
* - yaml
* ---
*
* ## EXAMPLES
*
* # List user meta
* $ wp user meta list 123 --keys=nickname,description,wp_capabilities
* +---------+-----------------+--------------------------------+
* | user_id | meta_key | meta_value |
* +---------+-----------------+--------------------------------+
* | 123 | nickname | supervisor |
* | 123 | description | Mary is a WordPress developer. |
* | 123 | wp_capabilities | {"administrator":true} |
* +---------+-----------------+--------------------------------+
*
* @subcommand list
*/
public function list_( $args, $assoc_args ) {
$args = $this->replace_login_with_user_id( $args );
parent::list_( $args, $assoc_args );
}
/**
* Get meta field value.
*
* ## OPTIONS
*
* <user>
* : The user login, user email, or user ID of the user to get metadata for.
*
* <key>
* : The metadata key.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* ---
*
* ## EXAMPLES
*
* # Get user meta
* $ wp user meta get 123 bio
* Mary is an WordPress developer.
*/
public function get( $args, $assoc_args ) {
$args = $this->replace_login_with_user_id( $args );
parent::get( $args, $assoc_args );
}
/**
* Delete a meta field.
*
* ## OPTIONS
*
* <user>
* : The user login, user email, or user ID of the user to delete metadata from.
*
* <key>
* : The metadata key.
*
* [<value>]
* : The value to delete. If omitted, all rows with key will deleted.
*
* ## EXAMPLES
*
* # Delete user meta
* $ wp user meta delete 123 bio
* Success: Deleted custom field.
*/
public function delete( $args, $assoc_args ) {
$args = $this->replace_login_with_user_id( $args );
parent::delete( $args, $assoc_args );
}
/**
* Add a meta field.
*
* ## OPTIONS
*
* <user>
* : The user login, user email, or user ID of the user to add metadata for.
*
* <key>
* : The metadata key.
*
* <value>
* : The new metadata value.
*
* [--format=<format>]
* : The serialization format for the value. Default is plaintext.
*
* ## EXAMPLES
*
* # Add user meta
* $ wp user meta add 123 bio "Mary is an WordPress developer."
* Success: Added custom field.
*/
public function add( $args, $assoc_args ) {
$args = $this->replace_login_with_user_id( $args );
parent::add( $args, $assoc_args );
}
/**
* Update a meta field.
*
* ## OPTIONS
*
* <user>
* : The user login, user email, or user ID of the user to update metadata for.
*
* <key>
* : The metadata key.
*
* <value>
* : The new metadata value.
*
* [--format=<format>]
* : The serialization format for the value. Default is plaintext.
*
* ## EXAMPLES
*
* # Update user meta
* $ wp user meta update 123 bio "Mary is an awesome WordPress developer."
* Success: Updated custom field 'bio'.
*
* @alias set
*/
public function update( $args, $assoc_args ) {
$args = $this->replace_login_with_user_id( $args );
parent::update( $args, $assoc_args );
}
/**
* Replace user_login value with user ID
* user meta is a special case that also supports user_login
*
* @param array
* @return array
*/
private function replace_login_with_user_id( $args ) {
$user = $this->fetcher->get_check( $args[0] );
$args[0] = $user->ID;
return $args;
}
}