-
-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathPodsAPI_CLI_Command.php
More file actions
511 lines (417 loc) · 12.1 KB
/
PodsAPI_CLI_Command.php
File metadata and controls
511 lines (417 loc) · 12.1 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
<?php
/**
* Implements PodsAPI command for WP-CLI
*/
class PodsAPI_CLI_Command extends WP_CLI_Command {
/**
* Add a pod.
*
* ## OPTIONS
*
* --name=<name>
* : The pod name, the default type is post_type.
*
* [--<field>=<value>]
* : The field => value pair(s) to save.
*
* ## EXAMPLES
*
* wp pods-legacy-api add-pod --name=book
* wp pods-legacy-api add-pod --name=book --type=post_type
* wp pods-legacy-api add-pod --name=book --type=post_type --label=Books --singular_label=Book
* wp pods-legacy-api add-pod --name=genre --type=taxonomy --label=Genres --singular_label=Genre
*
* @subcommand add-pod
*
* @param $args
* @param $assoc_args
*/
public function add_pod( $args, $assoc_args ) {
// Don't allow id to be set.
if ( isset( $assoc_args['id'] ) ) {
unset( $assoc_args['id'] );
}
$api = pods_api();
$id = 0;
try {
$id = $api->save_pod( $assoc_args );
} catch ( Exception $exception ) {
WP_CLI::error( sprintf( __( 'Error saving pod: %s', 'pods' ), $exception->getMessage() ) );
}
if ( 0 < $id ) {
WP_CLI::success( __( 'Pod added.', 'pods' ) );
WP_CLI::line( sprintf( __( 'New ID: %s', 'pods' ), $id ) );
} else {
WP_CLI::error( __( 'Pod not added.', 'pods' ) );
}
}
/**
* Save a pod.
*
* ## OPTIONS
*
* --name=<name>
* : The pod name.
*
* [--<field>=<value>]
* : The field => value pair(s) to save.
*
* ## EXAMPLES
*
* wp pods-legacy-api save-pod --name=book --type=post_type
* wp pods-legacy-api save-pod --name=book --type=post_type --label=Books --singular_label=Book
* wp pods-legacy-api save-pod --name=genre --type=taxonomy --label=Genres --singular_label=Genre
*
* @subcommand save-pod
*
* @param $args
* @param $assoc_args
*/
public function save_pod( $args, $assoc_args ) {
// Don't allow id to be set.
if ( isset( $assoc_args['id'] ) ) {
unset( $assoc_args['id'] );
}
$api = pods_api();
$id = 0;
try {
$pod = $api->load_pod( $assoc_args['name'] );
if ( ! $pod ) {
WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['name'] ) );
}
$id = $api->save_pod( $assoc_args );
} catch ( Exception $exception ) {
WP_CLI::error( sprintf( __( 'Error saving pod: %s', 'pods' ), $exception->getMessage() ) );
}
if ( 0 < $id ) {
WP_CLI::success( __( 'Pod saved.', 'pods' ) );
WP_CLI::line( sprintf( __( 'ID: %s', 'pods' ), $id ) );
} else {
WP_CLI::error( __( 'Pod not saved.', 'pods' ) );
}
}
/**
* Duplicate a pod.
*
* ## OPTIONS
*
* --name=<name>
* : The pod name.
*
* [--new_name=<new_name>]
* : The new pod name (defaults to a unique non-conflicting name).
*
* [--<field>=<value>]
* : The field => value pair(s) to save.
*
* ## EXAMPLES
*
* wp pods-legacy-api duplicate-pod --name=book
* wp pods-legacy-api duplicate-pod --name=book --new_name=book2
* wp pods-legacy-api duplicate-pod --name=book --new_name=book2 --label="Books Two" --singular_label="Book Two"
*
* @subcommand duplicate-pod
*
* @param $args
* @param $assoc_args
*/
public function duplicate_pod( $args, $assoc_args ) {
// Don't allow id to be set.
if ( isset( $assoc_args['id'] ) ) {
unset( $assoc_args['id'] );
}
$api = pods_api();
$id = 0;
try {
$pod = $api->load_pod( $assoc_args['name'] );
if ( ! $pod ) {
WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['name'] ) );
}
$id = $api->duplicate_pod( $assoc_args );
} catch ( Exception $exception ) {
WP_CLI::error( sprintf( __( 'Error duplicating pod: %s', 'pods' ), $exception->getMessage() ) );
}
if ( 0 < $id ) {
WP_CLI::success( __( 'Pod duplicated.', 'pods' ) );
WP_CLI::line( sprintf( __( 'New ID: %s', 'pods' ), $id ) );
} else {
WP_CLI::error( __( 'Pod not duplicated.', 'pods' ) );
}
}
/**
* Reset a pod which will delete all pod items.
*
* ## OPTIONS
*
* --name=<name>
* : The pod name.
*
* ## EXAMPLES
*
* wp pods-legacy-api reset-pod --name=book
*
* @subcommand reset-pod
*
* @param $args
* @param $assoc_args
*/
public function reset_pod( $args, $assoc_args ) {
$api = pods_api();
$reset = false;
try {
$pod = $api->load_pod( $assoc_args['name'] );
if ( ! $pod ) {
WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['name'] ) );
}
$reset = $api->reset_pod( $assoc_args );
} catch ( Exception $exception ) {
WP_CLI::error( sprintf( __( 'Error resetting pod: %s', 'pods' ), $exception->getMessage() ) );
}
if ( $reset ) {
WP_CLI::success( __( 'Pod content reset.', 'pods' ) );
} else {
WP_CLI::error( __( 'Pod content not reset.', 'pods' ) );
}
}
/**
* Delete a pod, which will NOT delete all pod items by default.
*
* ## OPTIONS
*
* --name=<name>
* : The pod name.
*
* [--delete-all]
* : Delete all pod content for the pod.
*
* ## EXAMPLES
*
* wp pods-legacy-api delete-pod --name=book
* wp pods-legacy-api delete-pod --name=book --delete_all
*
* @subcommand delete-pod
*
* @param $args
* @param $assoc_args
*/
public function delete_pod( $args, $assoc_args ) {
$api = pods_api();
// Handle prettified arg name
if ( ! empty( $assoc_args['delete-all'] ) ) {
$assoc_args['delete_all'] = true;
unset( $assoc_args['delete-all'] );
}
$deleted = false;
try {
$pod = $api->load_pod( $assoc_args['name'] );
if ( ! $pod ) {
WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['name'] ) );
}
$deleted = $api->delete_pod( $assoc_args );
} catch ( Exception $exception ) {
WP_CLI::error( sprintf( __( 'Error deleting pod: %s', 'pods' ), $exception->getMessage() ) );
}
if ( $deleted ) {
WP_CLI::success( __( 'Pod deleted.', 'pods' ) );
} else {
WP_CLI::error( __( 'Pod not deleted.', 'pods' ) );
}
}
/**
* Activate a component.
*
* ## OPTIONS
*
* --component=<component>
* : The component identifier.
*
* ## EXAMPLES
*
* wp pods-legacy-api activate-component --component=templates
*
* @subcommand activate-component
*
* @param $args
* @param $assoc_args
*/
public function activate_component( $args, $assoc_args ) {
if ( ! class_exists( 'PodsInit' ) ) {
WP_CLI::error( __( 'PodsInit not available', 'pods' ) );
return;
}
$component = $assoc_args['component'];
$active = PodsInit::$components->is_component_active( $component );
if ( $active ) {
WP_CLI::error( sprintf( __( 'Component %s is already active.', 'pods' ), $component ) );
} else {
PodsInit::$components->activate_component( $component );
WP_CLI::success( __( 'Component activated.', 'pods' ) );
}
}
/**
* Deactivate a component.
*
* ## OPTIONS
*
* --component=<component>
* : The component identifier.
*
* ## EXAMPLES
*
* wp pods-legacy-api deactivate-component --component=templates
*
* @subcommand deactivate-component
*
* @param $args
* @param $assoc_args
*/
public function deactivate_component( $args, $assoc_args ) {
if ( ! class_exists( 'PodsInit' ) ) {
WP_CLI::error( __( 'PodsInit not available', 'pods' ) );
return;
}
$component = $assoc_args['component'];
$active = PodsInit::$components->is_component_active( $component );
if ( ! $active ) {
WP_CLI::error( sprintf( __( 'Component %s is not active.', 'pods' ), $component ) );
} else {
PodsInit::$components->deactivate_component( $component );
WP_CLI::success( __( 'Component deactivated.', 'pods' ) );
}
}
/**
* Clear the Pods cache.
*
* ## EXAMPLES
*
* wp pods-legacy-api clear-cache
*
* @subcommand clear-cache
*/
public function cache_clear() {
pods_api()->cache_flush_pods();
WP_CLI::success( __( 'Pods cache cleared', 'pods' ) );
}
/**
* Export a Pods Package to a file.
*
* ## OPTIONS
*
* --file=<file>
* : The file to save to including path (defaults to current path).
*
* [--pods=<pods>]
* : A comma-separated list of Pods IDs to export (default is all Pods).
*
* [--templates=<templates>]
* : A comma-separated list of Pod Template IDs to export (default is all Templates).
*
* [--pages=<pages>]
* : A comma-separated list of Pod Page IDs to export (default is all Pod Pages).
*
* ## EXAMPLES
*
* wp pods-legacy-api export-pod --file="pods-package.json"
* wp pods-legacy-api export-pod --file="pods-package.json" --pods="book,genre"
* wp pods-legacy-api export-pod --file="/path/to/pods-package.json" --pods="book,genre"
* wp pods-legacy-api export-pod --templates="book-single,book-list" --file="pods-package.json"
* wp pods-legacy-api export-pod --pod-pages="books,books/*" --file="pods-package.json"
* wp pods-legacy-api export-pod --pods="book,genre" --templates="book-single,book-list" --pod-pages="books,books/*" --file="pods-package.json"
*
* @subcommand export-pod
*/
public function export_pod( $args, $assoc_args ) {
if ( ! PodsInit::$components->is_component_active( 'migrate-packages' ) ) {
WP_CLI::error( sprintf( __( 'Migrate Package is not activated. Try activating it: %s', 'pods' ), 'wp pods-legacy-api activate-component --component=migrate-packages' ) );
}
$params = array(
'pods' => true,
);
if ( PodsInit::$components->is_component_active( 'templates' ) ) {
$params['templates'] = true;
}
if ( PodsInit::$components->is_component_active( 'pages' ) ) {
$params['pages'] = true;
}
$file = $assoc_args['file'];
unset( $assoc_args['file'] );
$params = array_merge( $params, $assoc_args );
$data = false;
try {
$data = Pods_Migrate_Packages::export( $params );
} catch ( Exception $exception ) {
WP_CLI::error( sprintf( __( 'Error exporting Pods Package: %s', 'pods' ), $exception->getMessage() ) );
}
if ( ! empty( $data ) ) {
// Load PodsMigrate class file for use.
pods_migrate();
// Only JSON format is supported for export.
if ( false === strpos( $file, '.json' ) ) {
$file .= '.json';
}
$export_file = PodsMigrate::export_data_to_file( $file, $data, true );
if ( $export_file ) {
WP_CLI::success( sprintf( __( 'Pods Package exported: %s', 'pods' ), $export_file ) );
} else {
WP_CLI::error( __( 'Pods Package not exported.', 'pods' ) );
}
} else {
WP_CLI::error( __( 'No Pods Package data found.', 'pods' ) );
}
}
/**
* Import a Pods Package from a file.
*
* ## OPTIONS
*
* --file=<file>
* : The file to save to including path (defaults to current path).
*
* [--replace]
* : Overwrite imported items if they already exist (defaults to false).
*
* ## EXAMPLES
*
* wp pods-legacy-api import-pod --file="pods-package.json"
* wp pods-legacy-api import-pod --file="/path/to/pods-package.json"
* wp pods-legacy-api import-pod --file="pods-package.json" --replace
*
* @subcommand import-pod
*/
public function import_pod( $args, $assoc_args ) {
if ( ! PodsInit::$components->is_component_active( 'migrate-packages' ) ) {
WP_CLI::error( sprintf( __( 'Migrate Package is not activated. Try activating it: %s', 'pods' ), 'wp pods-legacy-api activate-component --component=migrate-packages' ) );
}
$replace = false;
if ( ! empty( $assoc_args['replace'] ) ) {
$replace = true;
}
$file = $assoc_args['file'];
$imported = false;
try {
// Load PodsMigrate class file for use.
pods_migrate();
// Only JSON format is supported for import.
if ( false === strpos( $file, '.json' ) ) {
WP_CLI::error( sprintf( __( 'Invalid file format, the file must use the .json extension: %s', 'pods' ), $file ) );
}
$data = PodsMigrate::get_data_from_file( $file, true );
if ( empty( $data ) ) {
WP_CLI::error( __( 'No Pods Package data found.', 'pods' ) );
}
$imported = Pods_Migrate_Packages::import( $data, $replace );
} catch ( Exception $exception ) {
WP_CLI::error( sprintf( __( 'Error exporting Pods Package: %s', 'pods' ), $exception->getMessage() ) );
}
if ( ! empty( $imported ) ) {
WP_CLI::success( __( 'Pods Package imported.', 'pods' ) );
} else {
WP_CLI::error( __( 'Pods Package not imported.', 'pods' ) );
}
}
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_hook( 'after_wp_load', function() {
WP_CLI::add_command( 'pods-legacy-api', 'PodsAPI_CLI_Command' );
} );
}