-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathUser_Meta_Command.php
More file actions
349 lines (335 loc) · 10.4 KB
/
User_Meta_Command.php
File metadata and controls
349 lines (335 loc) · 10.4 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
use WP_CLI\CommandWithMeta;
use WP_CLI\Fetchers\User as UserFetcher;
/**
* Adds, updates, deletes, and lists 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 CommandWithMeta {
protected $meta_type = 'user';
private $fetcher;
public function __construct() {
$this->fetcher = new UserFetcher();
}
/**
* Lists 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
* ---
*
* [--orderby=<fields>]
* : Set orderby which field.
* ---
* default: id
* options:
* - id
* - meta_key
* - meta_value
* ---
*
* [--order=<order>]
* : Set ascending or descending order.
* ---
* default: asc
* options:
* - asc
* - desc
* ---
*
* [--unserialize]
* : Unserialize meta_value output.
*
* ## 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
*
* @param array{0: string} $args Positional arguments.
* @param array{keys?: string, fields?: string, format: 'table'|'csv'|'json'|'count'|'yaml', orderby: 'id'|'meta_key'|'meta_value', order: 'asc'|'desc', unserialize?: bool} $assoc_args Associative arguments.
*/
public function list_( $args, $assoc_args ) {
$args = $this->replace_login_with_user_id( $args );
parent::list_( $args, $assoc_args );
}
/**
* Gets 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.
*
* # Get the primary site of a user (for multisite)
* $ wp user meta get 2 primary_blog
* 3
*
* @param array{0: string, 1: string} $args Positional arguments.
* @param array{format: 'table'|'csv'|'json'|'yaml'} $assoc_args Associative arguments.
*/
public function get( $args, $assoc_args ) {
$args = $this->replace_login_with_user_id( $args );
parent::get( $args, $assoc_args );
}
/**
* Deletes 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.
*
* @param array<string> $args Positional arguments.
*/
public function delete( $args, $assoc_args ) {
$args = $this->replace_login_with_user_id( $args );
parent::delete( $args, $assoc_args );
}
/**
* Adds 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: plaintext
* options:
* - plaintext
* - json
* ---
*
* ## EXAMPLES
*
* # Add user meta
* $ wp user meta add 123 bio "Mary is an WordPress developer."
* Success: Added custom field.
*
* @param array<string> $args Positional arguments.
* @param array{format: 'plaintext'|'json'} $assoc_args Associative arguments.
*/
public function add( $args, $assoc_args ) {
$args = $this->replace_login_with_user_id( $args );
parent::add( $args, $assoc_args );
}
/**
* Updates 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: plaintext
* options:
* - plaintext
* - json
* ---
*
* ## EXAMPLES
*
* # Update user meta
* $ wp user meta update 123 bio "Mary is an awesome WordPress developer."
* Success: Updated custom field 'bio'.
*
* @alias set
*
* @param array<string> $args Positional arguments.
* @param array{format: 'plaintext'|'json'} $assoc_args Associative arguments.
*/
public function update( $args, $assoc_args ) {
$args = $this->replace_login_with_user_id( $args );
parent::update( $args, $assoc_args );
}
/**
* Wrapper method for add_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Metadata key to use.
* @param mixed $meta_value Metadata value. Must be serializable if
* non-scalar.
* @param bool $unique Optional, default is false. Whether the
* specified metadata key should be unique for the
* object. If true, and the object already has a
* value for the specified metadata key, no change
* will be made.
*
* @return int|false The meta ID on success, false on failure.
*/
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
return add_user_meta( $object_id, $meta_key, $meta_value, $unique );
}
/**
* Wrapper method for update_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Metadata key to use.
* @param mixed $meta_value Metadata value. Must be serializable if
* non-scalar.
* @param mixed $prev_value Optional. If specified, only update existing
* metadata entries with the specified value.
* Otherwise, update all entries.
*
* @return int|bool Meta ID if the key didn't exist, true on successful
* update, false on failure.
*/
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_user_meta( $object_id, $meta_key, $meta_value, $prev_value );
}
/**
* Wrapper method for get_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Optional. Metadata key. If not specified,
* retrieve all metadata for the specified object.
* @param bool $single Optional, default is false. If true, return only
* the first value of the specified meta_key. This
* parameter has no effect if meta_key is not
* specified.
*
* @return mixed Single metadata value, or array of values.
*
* @phpstan-return ($single is true ? string : $meta_key is "" ? array<array<string>> : array<string>)
*/
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
// @phpstan-ignore return.type
return get_user_meta( $object_id, $meta_key, $single );
}
/**
* Wrapper method for delete_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object metadata is for
* @param string $meta_key Metadata key
* @param mixed $meta_value Optional. Metadata value. Must be serializable
* if non-scalar. If specified, only delete
* metadata entries with this value. Otherwise,
* delete all entries with the specified meta_key.
* Pass `null, `false`, or an empty string to skip
* this check. For backward compatibility, it is
* not possible to pass an empty string to delete
* those entries with an empty string for a value.
*
* @return bool True on successful delete, false on failure.
*/
protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) {
return delete_user_meta( $object_id, $meta_key, $meta_value );
}
/**
* Replaces user_login value with user ID
* user meta is a special case that also supports user_login
*
* @template T of array
*
* @param T $args
* @return T
*/
private function replace_login_with_user_id( $args ) {
$user = $this->fetcher->get_check( $args[0] );
$args[0] = $user->ID;
// TODO: Improve method type eventually.
// Related: https://github.com/phpstan/phpstan/issues/8438.
// @phpstan-ignore return.type
return $args;
}
}