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