PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.8
Pods – Custom Content Types and Fields v3.3.8
trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / classes / cli / PodsAPI_CLI_Command.php
pods / classes / cli Last commit date
PodsAPI_CLI_Command.php 4 months ago Pods_CLI_Command.php 4 months ago
PodsAPI_CLI_Command.php
533 lines
1 <?php
2
3 // Don't load directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 die( '-1' );
6 }
7
8 /**
9 * Implements PodsAPI command for WP-CLI
10 */
11 class PodsAPI_CLI_Command extends WP_CLI_Command {
12
13 /**
14 * Add a pod.
15 *
16 * ## OPTIONS
17 *
18 * --name=<name>
19 * : The pod name, the default type is post_type.
20 *
21 * [--<field>=<value>]
22 * : The field => value pair(s) to save.
23 *
24 * ## EXAMPLES
25 *
26 * wp pods-legacy-api add-pod --name=book
27 * wp pods-legacy-api add-pod --name=book --type=post_type
28 * wp pods-legacy-api add-pod --name=book --type=post_type --label=Books --singular_label=Book
29 * wp pods-legacy-api add-pod --name=genre --type=taxonomy --label=Genres --singular_label=Genre
30 *
31 * @subcommand add-pod
32 *
33 * @param $args
34 * @param $assoc_args
35 */
36 public function add_pod( $args, $assoc_args ) {
37
38 // Don't allow id to be set.
39 if ( isset( $assoc_args['id'] ) ) {
40 unset( $assoc_args['id'] );
41 }
42
43 $api = pods_api();
44
45 $id = 0;
46
47 try {
48 $id = $api->save_pod( $assoc_args );
49 } catch ( Exception $exception ) {
50 // translators: %s is the error message.
51 WP_CLI::error( sprintf( __( 'Error saving pod: %s', 'pods' ), $exception->getMessage() ) );
52 }
53
54 if ( 0 < $id ) {
55 WP_CLI::success( __( 'Pod added.', 'pods' ) );
56 // translators: %s is the new pod ID.
57 WP_CLI::line( sprintf( __( 'New ID: %s', 'pods' ), $id ) );
58 } else {
59 WP_CLI::error( __( 'Pod not added.', 'pods' ) );
60 }
61
62 }
63
64 /**
65 * Save a pod.
66 *
67 * ## OPTIONS
68 *
69 * --name=<name>
70 * : The pod name.
71 *
72 * [--<field>=<value>]
73 * : The field => value pair(s) to save.
74 *
75 * ## EXAMPLES
76 *
77 * wp pods-legacy-api save-pod --name=book --type=post_type
78 * wp pods-legacy-api save-pod --name=book --type=post_type --label=Books --singular_label=Book
79 * wp pods-legacy-api save-pod --name=genre --type=taxonomy --label=Genres --singular_label=Genre
80 *
81 * @subcommand save-pod
82 *
83 * @param $args
84 * @param $assoc_args
85 */
86 public function save_pod( $args, $assoc_args ) {
87
88 // Don't allow id to be set.
89 if ( isset( $assoc_args['id'] ) ) {
90 unset( $assoc_args['id'] );
91 }
92
93 $api = pods_api();
94
95 $id = 0;
96
97 try {
98 $pod = $api->load_pod( $assoc_args['name'] );
99
100 if ( ! $pod ) {
101 // translators: %s is the pod name.
102 WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['name'] ) );
103 }
104
105 $id = $api->save_pod( $assoc_args );
106 } catch ( Exception $exception ) {
107 // translators: %s is the error message.
108 WP_CLI::error( sprintf( __( 'Error saving pod: %s', 'pods' ), $exception->getMessage() ) );
109 }
110
111 if ( 0 < $id ) {
112 WP_CLI::success( __( 'Pod saved.', 'pods' ) );
113 // translators: %s is the pod ID.
114 WP_CLI::line( sprintf( __( 'ID: %s', 'pods' ), $id ) );
115 } else {
116 WP_CLI::error( __( 'Pod not saved.', 'pods' ) );
117 }
118
119 }
120
121 /**
122 * Duplicate a pod.
123 *
124 * ## OPTIONS
125 *
126 * --name=<name>
127 * : The pod name.
128 *
129 * [--new_name=<new_name>]
130 * : The new pod name (defaults to a unique non-conflicting name).
131 *
132 * [--<field>=<value>]
133 * : The field => value pair(s) to save.
134 *
135 * ## EXAMPLES
136 *
137 * wp pods-legacy-api duplicate-pod --name=book
138 * wp pods-legacy-api duplicate-pod --name=book --new_name=book2
139 * wp pods-legacy-api duplicate-pod --name=book --new_name=book2 --label="Books Two" --singular_label="Book Two"
140 *
141 * @subcommand duplicate-pod
142 *
143 * @param $args
144 * @param $assoc_args
145 */
146 public function duplicate_pod( $args, $assoc_args ) {
147
148 // Don't allow id to be set.
149 if ( isset( $assoc_args['id'] ) ) {
150 unset( $assoc_args['id'] );
151 }
152
153 $api = pods_api();
154
155 $id = 0;
156
157 try {
158 $pod = $api->load_pod( $assoc_args['name'] );
159
160 if ( ! $pod ) {
161 // translators: %s is the pod name.
162 WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['name'] ) );
163 }
164
165 $id = $api->duplicate_pod( $assoc_args );
166 } catch ( Exception $exception ) {
167 // translators: %s is the error message.
168 WP_CLI::error( sprintf( __( 'Error duplicating pod: %s', 'pods' ), $exception->getMessage() ) );
169 }
170
171 if ( 0 < $id ) {
172 WP_CLI::success( __( 'Pod duplicated.', 'pods' ) );
173 // translators: %s is the new pod ID.
174 WP_CLI::line( sprintf( __( 'New ID: %s', 'pods' ), $id ) );
175 } else {
176 WP_CLI::error( __( 'Pod not duplicated.', 'pods' ) );
177 }
178
179 }
180
181 /**
182 * Reset a pod which will delete all pod items.
183 *
184 * ## OPTIONS
185 *
186 * --name=<name>
187 * : The pod name.
188 *
189 * ## EXAMPLES
190 *
191 * wp pods-legacy-api reset-pod --name=book
192 *
193 * @subcommand reset-pod
194 *
195 * @param $args
196 * @param $assoc_args
197 */
198 public function reset_pod( $args, $assoc_args ) {
199
200 $api = pods_api();
201
202 $reset = false;
203
204 try {
205 $pod = $api->load_pod( $assoc_args['name'] );
206
207 if ( ! $pod ) {
208 // translators: %s is the pod name.
209 WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['name'] ) );
210 }
211
212 $reset = $api->reset_pod( $assoc_args );
213 } catch ( Exception $exception ) {
214 // translators: %s is the error message.
215 WP_CLI::error( sprintf( __( 'Error resetting pod: %s', 'pods' ), $exception->getMessage() ) );
216 }
217
218 if ( $reset ) {
219 WP_CLI::success( __( 'Pod content reset.', 'pods' ) );
220 } else {
221 WP_CLI::error( __( 'Pod content not reset.', 'pods' ) );
222 }
223
224 }
225
226 /**
227 * Delete a pod, which will NOT delete all pod items by default.
228 *
229 * ## OPTIONS
230 *
231 * --name=<name>
232 * : The pod name.
233 *
234 * [--delete-all]
235 * : Delete all pod content for the pod.
236 *
237 * ## EXAMPLES
238 *
239 * wp pods-legacy-api delete-pod --name=book
240 * wp pods-legacy-api delete-pod --name=book --delete_all
241 *
242 * @subcommand delete-pod
243 *
244 * @param $args
245 * @param $assoc_args
246 */
247 public function delete_pod( $args, $assoc_args ) {
248
249 $api = pods_api();
250
251 // Handle prettified arg name
252 if ( ! empty( $assoc_args['delete-all'] ) ) {
253 $assoc_args['delete_all'] = true;
254
255 unset( $assoc_args['delete-all'] );
256 }
257
258 $deleted = false;
259
260 try {
261 $pod = $api->load_pod( $assoc_args['name'] );
262
263 if ( ! $pod ) {
264 // translators: %s is the pod name.
265 WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['name'] ) );
266 }
267
268 $deleted = $api->delete_pod( $assoc_args );
269 } catch ( Exception $exception ) {
270 // translators: %s is the error message.
271 WP_CLI::error( sprintf( __( 'Error deleting pod: %s', 'pods' ), $exception->getMessage() ) );
272 }
273
274 if ( $deleted ) {
275 WP_CLI::success( __( 'Pod deleted.', 'pods' ) );
276 } else {
277 WP_CLI::error( __( 'Pod not deleted.', 'pods' ) );
278 }
279
280 }
281
282 /**
283 * Activate a component.
284 *
285 * ## OPTIONS
286 *
287 * --component=<component>
288 * : The component identifier.
289 *
290 * ## EXAMPLES
291 *
292 * wp pods-legacy-api activate-component --component=templates
293 *
294 * @subcommand activate-component
295 *
296 * @param $args
297 * @param $assoc_args
298 */
299 public function activate_component( $args, $assoc_args ) {
300
301 if ( ! class_exists( 'PodsInit' ) ) {
302 WP_CLI::error( __( 'PodsInit not available', 'pods' ) );
303
304 return;
305 }
306
307 $component = $assoc_args['component'];
308
309 $active = PodsInit::$components->is_component_active( $component );
310
311 if ( $active ) {
312 // translators: %s is the component name.
313 WP_CLI::error( sprintf( __( 'Component %s is already active.', 'pods' ), $component ) );
314 } else {
315 PodsInit::$components->activate_component( $component );
316
317 WP_CLI::success( __( 'Component activated.', 'pods' ) );
318 }
319
320 }
321
322 /**
323 * Deactivate a component.
324 *
325 * ## OPTIONS
326 *
327 * --component=<component>
328 * : The component identifier.
329 *
330 * ## EXAMPLES
331 *
332 * wp pods-legacy-api deactivate-component --component=templates
333 *
334 * @subcommand deactivate-component
335 *
336 * @param $args
337 * @param $assoc_args
338 */
339 public function deactivate_component( $args, $assoc_args ) {
340
341 if ( ! class_exists( 'PodsInit' ) ) {
342 WP_CLI::error( __( 'PodsInit not available', 'pods' ) );
343
344 return;
345 }
346
347 $component = $assoc_args['component'];
348
349 $active = PodsInit::$components->is_component_active( $component );
350
351 if ( ! $active ) {
352 // translators: %s is the component name.
353 WP_CLI::error( sprintf( __( 'Component %s is not active.', 'pods' ), $component ) );
354 } else {
355 PodsInit::$components->deactivate_component( $component );
356
357 WP_CLI::success( __( 'Component deactivated.', 'pods' ) );
358 }
359
360 }
361
362 /**
363 * Clear the Pods cache.
364 *
365 * ## EXAMPLES
366 *
367 * wp pods-legacy-api clear-cache
368 *
369 * @subcommand clear-cache
370 */
371 public function cache_clear() {
372
373 pods_api()->cache_flush_pods();
374
375 WP_CLI::success( __( 'Pods cache cleared', 'pods' ) );
376
377 }
378
379 /**
380 * Export a Pods Package to a file.
381 *
382 * ## OPTIONS
383 *
384 * --file=<file>
385 * : The file to save to including path (defaults to current path).
386 *
387 * [--pods=<pods>]
388 * : A comma-separated list of Pods IDs to export (default is all Pods).
389 *
390 * [--templates=<templates>]
391 * : A comma-separated list of Pod Template IDs to export (default is all Templates).
392 *
393 * [--pages=<pages>]
394 * : A comma-separated list of Pod Page IDs to export (default is all Pod Pages).
395 *
396 * ## EXAMPLES
397 *
398 * wp pods-legacy-api export-pod --file="pods-package.json"
399 * wp pods-legacy-api export-pod --file="pods-package.json" --pods="book,genre"
400 * wp pods-legacy-api export-pod --file="/path/to/pods-package.json" --pods="book,genre"
401 * wp pods-legacy-api export-pod --templates="book-single,book-list" --file="pods-package.json"
402 * wp pods-legacy-api export-pod --pod-pages="books,books/*" --file="pods-package.json"
403 * wp pods-legacy-api export-pod --pods="book,genre" --templates="book-single,book-list" --pod-pages="books,books/*" --file="pods-package.json"
404 *
405 * @subcommand export-pod
406 */
407 public function export_pod( $args, $assoc_args ) {
408
409 if ( ! PodsInit::$components->is_component_active( 'migrate-packages' ) ) {
410 // translators: %s is the WP-CLI command to activate the component.
411 WP_CLI::error( sprintf( __( 'Migrate Package is not activated. Try activating it: %s', 'pods' ), 'wp pods-legacy-api activate-component --component=migrate-packages' ) );
412 }
413
414 $params = [
415 'pods' => true,
416 ];
417
418 if ( PodsInit::$components->is_component_active( 'templates' ) ) {
419 $params['templates'] = true;
420 }
421
422 if ( PodsInit::$components->is_component_active( 'pages' ) ) {
423 $params['pages'] = true;
424 }
425
426 $file = (string) $assoc_args['file'];
427
428 unset( $assoc_args['file'] );
429
430 $params = array_merge( $params, $assoc_args );
431
432 $data = false;
433
434 try {
435 $data = Pods_Migrate_Packages::export( $params );
436 } catch ( Exception $exception ) {
437 // translators: %s is the error message.
438 WP_CLI::error( sprintf( __( 'Error exporting Pods Package: %s', 'pods' ), $exception->getMessage() ) );
439 }
440
441 if ( ! empty( $data ) ) {
442 // Load PodsMigrate class file for use.
443 pods_migrate();
444
445 // Only JSON format is supported for export.
446 if ( false === strpos( $file, '.json' ) ) {
447 $file .= '.json';
448 }
449
450 $export_file = PodsMigrate::export_data_to_file( $file, $data, true );
451
452 if ( $export_file ) {
453 // translators: %s is the file path.
454 WP_CLI::success( sprintf( __( 'Pods Package exported: %s', 'pods' ), $export_file ) );
455 } else {
456 WP_CLI::error( __( 'Pods Package not exported.', 'pods' ) );
457 }
458 } else {
459 WP_CLI::error( __( 'No Pods Package data found.', 'pods' ) );
460 }
461
462 }
463
464 /**
465 * Import a Pods Package from a file.
466 *
467 * ## OPTIONS
468 *
469 * --file=<file>
470 * : The file to save to including path (defaults to current path).
471 *
472 * [--replace]
473 * : Overwrite imported items if they already exist (defaults to false).
474 *
475 * ## EXAMPLES
476 *
477 * wp pods-legacy-api import-pod --file="pods-package.json"
478 * wp pods-legacy-api import-pod --file="/path/to/pods-package.json"
479 * wp pods-legacy-api import-pod --file="pods-package.json" --replace
480 *
481 * @subcommand import-pod
482 */
483 public function import_pod( $args, $assoc_args ) {
484
485 if ( ! PodsInit::$components->is_component_active( 'migrate-packages' ) ) {
486 // translators: %s is the WP-CLI command to activate the component.
487 WP_CLI::error( sprintf( __( 'Migrate Package is not activated. Try activating it: %s', 'pods' ), 'wp pods-legacy-api activate-component --component=migrate-packages' ) );
488 }
489
490 $replace = false;
491
492 if ( ! empty( $assoc_args['replace'] ) ) {
493 $replace = true;
494 }
495
496 $file = (string) $assoc_args['file'];
497
498 $imported = false;
499
500 try {
501 // Load PodsMigrate class file for use.
502 pods_migrate();
503
504 // Only JSON format is supported for import.
505 if ( false === strpos( $file, '.json' ) ) {
506 // translators: %s is the file name.
507 WP_CLI::error( sprintf( __( 'Invalid file format, the file must use the .json extension: %s', 'pods' ), $file ) );
508 }
509
510 $data = PodsMigrate::get_data_from_file( $file, true );
511
512 if ( empty( $data ) ) {
513 WP_CLI::error( __( 'No Pods Package data found.', 'pods' ) );
514 }
515
516 $imported = Pods_Migrate_Packages::import( $data, $replace );
517 } catch ( Exception $exception ) {
518 // translators: %s is the error message.
519 WP_CLI::error( sprintf( __( 'Error exporting Pods Package: %s', 'pods' ), $exception->getMessage() ) );
520 }
521
522 if ( ! empty( $imported ) ) {
523 WP_CLI::success( __( 'Pods Package imported.', 'pods' ) );
524 } else {
525 WP_CLI::error( __( 'Pods Package not imported.', 'pods' ) );
526 }
527
528 }
529
530 }
531
532 WP_CLI::add_command( 'pods-legacy-api', 'PodsAPI_CLI_Command' );
533