|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Implements Pods command for WP-CLI |
|
5
|
|
|
*/ |
|
6
|
|
|
class Pods_CLI_Command extends WP_CLI_Command { |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Add a pod item. |
|
10
|
|
|
* |
|
11
|
|
|
* ## OPTIONS |
|
12
|
|
|
* |
|
13
|
|
|
* --pod=<pod> |
|
14
|
|
|
* : The pod name. |
|
15
|
|
|
* |
|
16
|
|
|
* --<field>=<value> |
|
17
|
|
|
* : The field => value pair(s) to save. |
|
18
|
|
|
* |
|
19
|
|
|
* ## EXAMPLES |
|
20
|
|
|
* |
|
21
|
|
|
* wp pods add --pod=my_pod --my_field_name1=Value --my_field_name2="Another Value" |
|
22
|
|
|
*/ |
|
23
|
|
|
public function add( $args, $assoc_args ) { |
|
24
|
|
|
|
|
25
|
|
|
$pod_name = $assoc_args['pod']; |
|
26
|
|
|
|
|
27
|
|
|
unset( $assoc_args['pod'] ); |
|
28
|
|
|
|
|
29
|
|
|
$pod = pods( $pod_name, null, false ); |
|
30
|
|
|
|
|
31
|
|
|
if ( $pod->valid() ) { |
|
32
|
|
|
WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['pod'] ) ); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
View Code Duplication |
if ( ! empty( $assoc_args ) ) { |
|
36
|
|
|
$id = 0; |
|
37
|
|
|
|
|
38
|
|
|
try { |
|
39
|
|
|
$id = $pod->add( $assoc_args ); |
|
40
|
|
|
} catch ( Exception $e ) { |
|
41
|
|
|
WP_CLI::error( sprintf( __( 'Error saving pod item: %s', 'pods' ), $e->getMessage() ) ); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ( 0 < $id ) { |
|
45
|
|
|
WP_CLI::success( __( 'Pod item added.', 'pods' ) ); |
|
46
|
|
|
WP_CLI::line( sprintf( __( 'New ID: %s', 'pods' ), $id ) ); |
|
47
|
|
|
} else { |
|
48
|
|
|
WP_CLI::error( __( 'Pod item not added.', 'pods' ) ); |
|
49
|
|
|
} |
|
50
|
|
|
} else { |
|
51
|
|
|
WP_CLI::error( __( 'No data sent for saving.', 'pods' ) ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Save a pod item. |
|
58
|
|
|
* |
|
59
|
|
|
* ## OPTIONS |
|
60
|
|
|
* |
|
61
|
|
|
* --pod=<pod> |
|
62
|
|
|
* : The pod name. |
|
63
|
|
|
* |
|
64
|
|
|
* [--item=<item>] |
|
65
|
|
|
* : The item to save for, it is not used for a settings pod. |
|
66
|
|
|
* |
|
67
|
|
|
* --<field>=<value> |
|
68
|
|
|
* : The field => value pair(s) to save. |
|
69
|
|
|
* |
|
70
|
|
|
* ## EXAMPLES |
|
71
|
|
|
* |
|
72
|
|
|
* wp pods save --pod=my_pod --item=123 --my_field_name1=Value2 --my_field_name2="Another Value2" |
|
73
|
|
|
* wp pods save --pod=my_settings_pod --my_option_field_name1=Value --my_option_field_name2="Another Value2" |
|
74
|
|
|
*/ |
|
75
|
|
|
public function save( $args, $assoc_args ) { |
|
76
|
|
|
|
|
77
|
|
|
$pod_name = $assoc_args['pod']; |
|
78
|
|
|
$item = pods_v( 'item', $assoc_args ); |
|
79
|
|
|
|
|
80
|
|
|
unset( $assoc_args['pod'] ); |
|
81
|
|
|
|
|
82
|
|
|
if ( null !== $item ) { |
|
83
|
|
|
unset( $assoc_args['item'] ); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$pod = pods( $pod_name, $item, false ); |
|
87
|
|
|
|
|
88
|
|
|
if ( $pod->valid() ) { |
|
89
|
|
|
WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['pod'] ) ); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
View Code Duplication |
if ( null !== $item && $pod->exists() ) { |
|
93
|
|
|
WP_CLI::error( sprintf( __( 'Pod "%s" item "%s" does not exist.', 'pods' ), $assoc_args['pod'], $assoc_args['item'] ) ); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
View Code Duplication |
if ( ! empty( $assoc_args ) ) { |
|
97
|
|
|
$id = 0; |
|
98
|
|
|
|
|
99
|
|
|
try { |
|
100
|
|
|
$id = $pod->save( $assoc_args ); |
|
101
|
|
|
} catch ( Exception $e ) { |
|
102
|
|
|
WP_CLI::error( sprintf( __( 'Error saving pod item: %s', 'pods' ), $e->getMessage() ) ); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if ( 0 < $id ) { |
|
106
|
|
|
WP_CLI::success( __( 'Pod item saved.', 'pods' ) ); |
|
107
|
|
|
WP_CLI::line( sprintf( __( 'ID: %s', 'pods' ), $id ) ); |
|
108
|
|
|
} else { |
|
109
|
|
|
WP_CLI::error( __( 'Pod item not saved.', 'pods' ) ); |
|
110
|
|
|
} |
|
111
|
|
|
} else { |
|
112
|
|
|
WP_CLI::error( __( 'No data sent for saving.', 'pods' ) ); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Duplicate a pod item. |
|
119
|
|
|
* |
|
120
|
|
|
* ## OPTIONS |
|
121
|
|
|
* |
|
122
|
|
|
* --pod=<pod> |
|
123
|
|
|
* : The pod name. |
|
124
|
|
|
* |
|
125
|
|
|
* --item=<item> |
|
126
|
|
|
* : The pod item to delete. |
|
127
|
|
|
* |
|
128
|
|
|
* ## EXAMPLES |
|
129
|
|
|
* |
|
130
|
|
|
* wp pods duplicate --pod=my_pod --item=123 |
|
131
|
|
|
*/ |
|
132
|
|
|
public function duplicate( $args, $assoc_args ) { |
|
133
|
|
|
|
|
134
|
|
|
$pod = pods( $assoc_args['pod'], $assoc_args['item'], false ); |
|
135
|
|
|
|
|
136
|
|
|
if ( $pod->valid() ) { |
|
137
|
|
|
WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['pod'] ) ); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
View Code Duplication |
if ( $pod->exists() ) { |
|
141
|
|
|
WP_CLI::error( sprintf( __( 'Pod "%s" item "%s" does not exist.', 'pods' ), $assoc_args['pod'], $assoc_args['item'] ) ); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$id = 0; |
|
145
|
|
|
|
|
146
|
|
|
try { |
|
147
|
|
|
$id = $pod->duplicate( $assoc_args ); |
|
148
|
|
|
} catch ( Exception $e ) { |
|
149
|
|
|
WP_CLI::error( sprintf( __( 'Error saving pod item: %s', 'pods' ), $e->getMessage() ) ); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if ( 0 < $id ) { |
|
153
|
|
|
WP_CLI::success( __( 'Pod item duplicated.', 'pods' ) ); |
|
154
|
|
|
WP_CLI::line( sprintf( __( 'New ID: %s', 'pods' ), $id ) ); |
|
155
|
|
|
} else { |
|
156
|
|
|
WP_CLI::error( __( 'Pod item not duplicated.', 'pods' ) ); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Delete a pod item. |
|
163
|
|
|
* |
|
164
|
|
|
* ## OPTIONS |
|
165
|
|
|
* |
|
166
|
|
|
* --pod=<pod> |
|
167
|
|
|
* : The pod name. |
|
168
|
|
|
* |
|
169
|
|
|
* --item=<item> |
|
170
|
|
|
* : The pod item to delete. |
|
171
|
|
|
* |
|
172
|
|
|
* ## EXAMPLES |
|
173
|
|
|
* |
|
174
|
|
|
* wp pods delete --pod=my_pod --item=123 |
|
175
|
|
|
*/ |
|
176
|
|
|
public function delete( $args, $assoc_args ) { |
|
177
|
|
|
|
|
178
|
|
|
$pod = pods( $assoc_args['pod'], $assoc_args['item'], false ); |
|
179
|
|
|
|
|
180
|
|
|
if ( $pod->valid() ) { |
|
181
|
|
|
WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['pod'] ) ); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
View Code Duplication |
if ( $pod->exists() ) { |
|
185
|
|
|
WP_CLI::error( sprintf( __( 'Pod "%s" item "%s" does not exist.', 'pods' ), $assoc_args['pod'], $assoc_args['item'] ) ); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$deleted = false; |
|
189
|
|
|
|
|
190
|
|
|
try { |
|
191
|
|
|
$deleted = $pod->delete(); |
|
192
|
|
|
} catch ( Exception $e ) { |
|
193
|
|
|
WP_CLI::error( sprintf( __( 'Error saving pod item: %s', 'pods' ), $e->getMessage() ) ); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
if ( $deleted ) { |
|
197
|
|
|
WP_CLI::success( __( 'Pod item deleted.', 'pods' ) ); |
|
198
|
|
|
} else { |
|
199
|
|
|
WP_CLI::error( __( 'Pod item not deleted.', 'pods' ) ); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Export a single pod item to a file. |
|
206
|
|
|
* |
|
207
|
|
|
* ## OPTIONS |
|
208
|
|
|
* |
|
209
|
|
|
* --pod=<pod> |
|
210
|
|
|
* : The pod name. |
|
211
|
|
|
* |
|
212
|
|
|
* --file=<file> |
|
213
|
|
|
* : The file to save to including path (defaults to current path). |
|
214
|
|
|
* |
|
215
|
|
|
* [--item=<item>] |
|
216
|
|
|
* : The item to save for, it is not used for a settings pod. |
|
217
|
|
|
* |
|
218
|
|
|
* [--fields=<fields>] |
|
219
|
|
|
* : The comma-separated list of fields to export (defaults to all fields). |
|
220
|
|
|
* |
|
221
|
|
|
* [--depth=<depth>] |
|
222
|
|
|
* : The depth of related objects to recursively export (default is 1 level deep, only returns IDs for related objects). |
|
223
|
|
|
* |
|
224
|
|
|
* ## EXAMPLES |
|
225
|
|
|
* |
|
226
|
|
|
* wp pods export-item --pod=my_pod --item=123 --file="item-data.json" |
|
227
|
|
|
* wp pods export-item --pod=my_pod --item=123 --file="/path/to/item-data.json" |
|
228
|
|
|
* wp pods export-item --pod=my_pod --item=123 --file="item-data.json" --fields="ID,post_title,post_content,my_field_name1,my_field_name2" |
|
229
|
|
|
* wp pods export-item --pod=my_pod --item=123 --file="item-data.json" --depth=2 |
|
230
|
|
|
* |
|
231
|
|
|
* @subcommand export-item |
|
232
|
|
|
*/ |
|
233
|
|
|
public function export_item( $args, $assoc_args ) { |
|
234
|
|
|
|
|
235
|
|
|
$pod_name = $assoc_args['pod']; |
|
236
|
|
|
$item = pods_v( 'item', $assoc_args ); |
|
237
|
|
|
|
|
238
|
|
|
unset( $assoc_args['pod'] ); |
|
239
|
|
|
|
|
240
|
|
|
if ( null !== $item ) { |
|
241
|
|
|
unset( $assoc_args['item'] ); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
$pod = pods( $pod_name, $item, false ); |
|
245
|
|
|
|
|
246
|
|
|
if ( $pod->valid() ) { |
|
247
|
|
|
WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['pod'] ) ); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
View Code Duplication |
if ( null !== $item && $pod->exists() ) { |
|
251
|
|
|
WP_CLI::error( sprintf( __( 'Pod "%s" item "%s" does not exist.', 'pods' ), $assoc_args['pod'], $assoc_args['item'] ) ); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
$params = array( |
|
255
|
|
|
'fields' => pods_v( 'fields', $assoc_args, null, true ), |
|
256
|
|
|
'depth' => (int) pods_v( 'depth', $assoc_args, 1, true ), |
|
257
|
|
|
); |
|
258
|
|
|
|
|
259
|
|
|
$data = false; |
|
260
|
|
|
|
|
261
|
|
|
try { |
|
262
|
|
|
$data = $pod->export( $params ); |
|
263
|
|
|
} catch ( Exception $e ) { |
|
264
|
|
|
WP_CLI::error( sprintf( __( 'Error exporting pod item: %s', 'pods' ), $e->getMessage() ) ); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
View Code Duplication |
if ( ! empty( $data ) ) { |
|
268
|
|
|
// Load PodsMigrate class file for use. |
|
269
|
|
|
pods_migrate(); |
|
270
|
|
|
|
|
271
|
|
|
$file = $assoc_args['file']; |
|
272
|
|
|
|
|
273
|
|
|
$export_file = PodsMigrate::export_data_to_file( $file, $data, true ); |
|
274
|
|
|
|
|
275
|
|
|
if ( $export_file ) { |
|
276
|
|
|
WP_CLI::success( sprintf( __( 'Pod item exported: %s', 'pods' ), $export_file ) ); |
|
277
|
|
|
} else { |
|
278
|
|
|
WP_CLI::error( __( 'Pod item not exported.', 'pods' ) ); |
|
279
|
|
|
} |
|
280
|
|
|
} else { |
|
281
|
|
|
WP_CLI::error( __( 'No export data found.', 'pods' ) ); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Export all pod items to a file. |
|
288
|
|
|
* |
|
289
|
|
|
* ## OPTIONS |
|
290
|
|
|
* |
|
291
|
|
|
* --pod=<pod> |
|
292
|
|
|
* : The pod name. |
|
293
|
|
|
* |
|
294
|
|
|
* --file=<file> |
|
295
|
|
|
* : The file to save to including path (defaults to current path). |
|
296
|
|
|
* |
|
297
|
|
|
* [--fields=<fields>] |
|
298
|
|
|
* : The comma-separated list of fields to export (defaults to all fields). |
|
299
|
|
|
* |
|
300
|
|
|
* [--depth=<depth>] |
|
301
|
|
|
* : The depth of related objects to recursively export (default is 1 level deep, only returns IDs for related objects). |
|
302
|
|
|
* |
|
303
|
|
|
* [--params=<params>] |
|
304
|
|
|
* : The params to pass into the Pods::find() call, provided in arg1=A&arg2=B or JSON format (default is limit=-1). |
|
305
|
|
|
* |
|
306
|
|
|
* ## EXAMPLES |
|
307
|
|
|
* |
|
308
|
|
|
* wp pods export --pod=my_pod --file="items.json" |
|
309
|
|
|
* wp pods export --pod=my_pod --file="/path/to/items.json" |
|
310
|
|
|
* wp pods export --pod=my_pod --file="items.json" --fields="ID,post_title,post_content,my_field_name1,my_field_name2" |
|
311
|
|
|
* wp pods export --pod=my_pod --file="items.json" --depth=2 |
|
312
|
|
|
* wp pods export --pod=my_pod --file="items.json" --params="{\"limit\":10,\"orderby\":\"t.ID DESC\"}" |
|
313
|
|
|
* wp pods export --pod=my_pod --file="items.json" --params="limit=10&orderby=t.ID DESC" |
|
314
|
|
|
*/ |
|
315
|
|
|
public function export( $args, $assoc_args ) { |
|
316
|
|
|
|
|
317
|
|
|
$pod_name = $assoc_args['pod']; |
|
318
|
|
|
|
|
319
|
|
|
unset( $assoc_args['pod'] ); |
|
320
|
|
|
|
|
321
|
|
|
$pod = pods( $pod_name, null, false ); |
|
322
|
|
|
|
|
323
|
|
|
if ( $pod->valid() ) { |
|
324
|
|
|
WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['pod'] ) ); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
$params = array( |
|
328
|
|
|
'fields' => pods_v( 'fields', $assoc_args, null, true ), |
|
329
|
|
|
'depth' => (int) pods_v( 'depth', $assoc_args, 1, true ), |
|
330
|
|
|
); |
|
331
|
|
|
|
|
332
|
|
|
// Handle custom find() params. |
|
333
|
|
|
$find_params = pods_v( 'params', $assoc_args, null, true ); |
|
334
|
|
|
|
|
335
|
|
|
if ( is_string( $find_params ) ) { |
|
336
|
|
|
$params['params'] = array(); |
|
337
|
|
|
|
|
338
|
|
|
if ( false !== strpos( $params['params'], '{' ) ) { |
|
339
|
|
|
// Pull the find params from JSON format. |
|
340
|
|
|
$params['params'] = json_decode( $params['params'], true ); |
|
341
|
|
|
} else { |
|
342
|
|
|
// Pull the find params from string argument format. |
|
343
|
|
|
wp_parse_str( $find_params, $params['params'] ); |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
$data = false; |
|
348
|
|
|
|
|
349
|
|
|
try { |
|
350
|
|
|
$data = $pod->export_data( $params ); |
|
351
|
|
|
} catch ( Exception $e ) { |
|
352
|
|
|
WP_CLI::error( sprintf( __( 'Error exporting pod items: %s', 'pods' ), $e->getMessage() ) ); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
View Code Duplication |
if ( ! empty( $data ) ) { |
|
356
|
|
|
// Load PodsMigrate class file for use. |
|
357
|
|
|
pods_migrate(); |
|
358
|
|
|
|
|
359
|
|
|
$file = $assoc_args['file']; |
|
360
|
|
|
|
|
361
|
|
|
$export_file = PodsMigrate::export_data_to_file( $file, $data ); |
|
362
|
|
|
|
|
363
|
|
|
if ( $export_file ) { |
|
364
|
|
|
WP_CLI::success( sprintf( __( 'Pod items exported: %s', 'pods' ), $export_file ) ); |
|
365
|
|
|
} else { |
|
366
|
|
|
WP_CLI::error( __( 'Pod items not exported.', 'pods' ) ); |
|
367
|
|
|
} |
|
368
|
|
|
} else { |
|
369
|
|
|
WP_CLI::error( __( 'No pod item export data found.', 'pods' ) ); |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
WP_CLI::add_command( 'pods', 'Pods_CLI_Command' ); |
|
377
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.