Completed
Push — 2.x ( 7c2045...bb72bf )
by Scott Kingsley
12s
created

PodsAPI_CLI_Command::duplicate_pod()   B

Complexity

Conditions 5
Paths 24

Size

Total Lines 31
Code Lines 17

Duplication

Lines 31
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 24
nop 2
dl 31
loc 31
rs 8.439
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 5 and the first side effect is on line 485.

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.

Loading history...
2
/**
3
 * Implements PodsAPI command for WP-CLI
4
 */
5
class PodsAPI_CLI_Command extends WP_CLI_Command {
6
7
	/**
8
	 * Add a pod.
9
	 *
10
	 * ## OPTIONS
11
	 *
12
	 * --name=<name>
13
	 * : The pod name, the default type is post_type.
14
	 *
15
	 * [--<field>=<value>]
16
	 * : The field => value pair(s) to save.
17
	 *
18
	 * ## EXAMPLES
19
	 *
20
	 * wp pods-api add-pod --name=book
21
	 * wp pods-api add-pod --name=book --type=post_type
22
	 * wp pods-api add-pod --name=book --type=post_type --label=Books --singular_label=Book
23
	 * wp pods-api add-pod --name=genre --type=taxonomy --label=Genres --singular_label=Genre
24
	 *
25
	 * @subcommand add-pod
26
	 */
27
	public function add_pod( $args, $assoc_args ) {
28
29
		// Don't allow id to be set.
30
		if ( isset( $assoc_args['id'] ) ) {
31
			unset( $assoc_args['id'] );
32
		}
33
34
		$api = pods_api();
35
36
		$id = 0;
37
38
		try {
39
			$id = $api->save_pod( $assoc_args );
40
		} catch ( Exception $e ) {
41
			WP_CLI::error( sprintf( __( 'Error saving pod: %s', 'pods' ), $e->getMessage() ) );
42
		}
43
44
		if ( 0 < $id ) {
45
			WP_CLI::success( __( 'Pod added.', 'pods' ) );
46
			WP_CLI::line( sprintf( __( 'New ID: %s', 'pods' ), $id ) );
47
		} else {
48
			WP_CLI::error( __( 'Pod not added.', 'pods' ) );
49
		}
50
51
	}
52
53
	/**
54
	 * Save a pod.
55
	 *
56
	 * ## OPTIONS
57
	 *
58
	 * --name=<name>
59
	 * : The pod name.
60
	 *
61
	 * [--<field>=<value>]
62
	 * : The field => value pair(s) to save.
63
	 *
64
	 * ## EXAMPLES
65
	 *
66
	 * wp pods-api save-pod --name=book --type=post_type
67
	 * wp pods-api save-pod --name=book --type=post_type --label=Books --singular_label=Book
68
	 * wp pods-api save-pod --name=genre --type=taxonomy --label=Genres --singular_label=Genre
69
	 *
70
	 * @subcommand save-pod
71
	 */
72 View Code Duplication
	public function save_pod( $args, $assoc_args ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
74
		// Don't allow id to be set.
75
		if ( isset( $assoc_args['id'] ) ) {
76
			unset( $assoc_args['id'] );
77
		}
78
79
		$api = pods_api();
80
81
		$id = 0;
82
83
		try {
84
			$pod = $api->load_pod( $assoc_args['name'] );
85
86
			if ( ! $pod ) {
87
				WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['name'] ) );
88
			}
89
90
			$id = $api->save_pod( $assoc_args );
91
		} catch ( Exception $e ) {
92
			WP_CLI::error( sprintf( __( 'Error saving pod: %s', 'pods' ), $e->getMessage() ) );
93
		}
94
95
		if ( 0 < $id ) {
96
			WP_CLI::success( __( 'Pod saved.', 'pods' ) );
97
			WP_CLI::line( sprintf( __( 'ID: %s', 'pods' ), $id ) );
98
		} else {
99
			WP_CLI::error( __( 'Pod not saved.', 'pods' ) );
100
		}
101
102
	}
103
104
	/**
105
	 * Duplicate a pod.
106
	 *
107
	 * ## OPTIONS
108
	 *
109
	 * --name=<name>
110
	 * : The pod name.
111
	 *
112
	 * [--new_name=<new_name>]
113
	 * : The new pod name (defaults to a unique non-conflicting name).
114
	 *
115
	 * [--<field>=<value>]
116
	 * : The field => value pair(s) to save.
117
	 *
118
	 * ## EXAMPLES
119
	 *
120
	 * wp pods-api duplicate-pod --name=book
121
	 * wp pods-api duplicate-pod --name=book --new_name=book2
122
	 * wp pods-api duplicate-pod --name=book --new_name=book2 --label="Books Two" --singular_label="Book Two"
123
	 *
124
	 * @subcommand duplicate-pod
125
	 */
126 View Code Duplication
	public function duplicate_pod( $args, $assoc_args ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
128
		// Don't allow id to be set.
129
		if ( isset( $assoc_args['id'] ) ) {
130
			unset( $assoc_args['id'] );
131
		}
132
133
		$api = pods_api();
134
135
		$id = 0;
136
137
		try {
138
			$pod = $api->load_pod( $assoc_args['name'] );
139
140
			if ( ! $pod ) {
141
				WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['name'] ) );
142
			}
143
144
			$id = $api->duplicate_pod( $assoc_args );
145
		} catch ( Exception $e ) {
146
			WP_CLI::error( sprintf( __( 'Error duplicating pod: %s', 'pods' ), $e->getMessage() ) );
147
		}
148
149
		if ( 0 < $id ) {
150
			WP_CLI::success( __( 'Pod duplicated.', 'pods' ) );
151
			WP_CLI::line( sprintf( __( 'New ID: %s', 'pods' ), $id ) );
152
		} else {
153
			WP_CLI::error( __( 'Pod not duplicated.', 'pods' ) );
154
		}
155
156
	}
157
158
	/**
159
	 * Reset a pod which will delete all pod items.
160
	 *
161
	 * ## OPTIONS
162
	 *
163
	 * --name=<name>
164
	 * : The pod name.
165
	 *
166
	 * ## EXAMPLES
167
	 *
168
	 * wp pods-api reset-pod --name=book
169
	 *
170
	 * @subcommand reset-pod
171
	 */
172
	public function reset_pod( $args, $assoc_args ) {
173
174
		$api = pods_api();
175
176
		$reset = false;
177
178
		try {
179
			$pod = $api->load_pod( $assoc_args['name'] );
180
181
			if ( ! $pod ) {
182
				WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['name'] ) );
183
			}
184
185
			$reset = $api->reset_pod( $assoc_args );
186
		} catch ( Exception $e ) {
187
			WP_CLI::error( sprintf( __( 'Error resetting pod: %s', 'pods' ), $e->getMessage() ) );
188
		}
189
190
		if ( $reset ) {
191
			WP_CLI::success( __( 'Pod content reset.', 'pods' ) );
192
		} else {
193
			WP_CLI::error( __( 'Pod content not reset.', 'pods' ) );
194
		}
195
196
	}
197
198
	/**
199
	 * Delete a pod, which will NOT delete all pod items by default.
200
	 *
201
	 * ## OPTIONS
202
	 *
203
	 * --name=<name>
204
	 * : The pod name.
205
	 *
206
	 * [--delete-all]
207
	 * : Delete all pod content for the pod.
208
	 *
209
	 * ## EXAMPLES
210
	 *
211
	 * wp pods-api delete-pod --name=book
212
	 * wp pods-api delete-pod --name=book --delete_all
213
	 *
214
	 * @subcommand delete-pod
215
	 */
216
	public function delete_pod( $args, $assoc_args ) {
217
218
		$api = pods_api();
219
220
		// Handle prettified arg name
221
		if ( ! empty( $assoc_args['delete-all'] ) ) {
222
			$assoc_args['delete_all'] = true;
223
224
			unset( $assoc_args['delete-all'] );
225
		}
226
227
		$deleted = false;
228
229
		try {
230
			$pod = $api->load_pod( $assoc_args['name'] );
231
232
			if ( ! $pod ) {
233
				WP_CLI::error( sprintf( __( 'Pod "%s" does not exist.', 'pods' ), $assoc_args['name'] ) );
234
			}
235
236
			$deleted = $api->delete_pod( $assoc_args );
237
		} catch ( Exception $e ) {
238
			WP_CLI::error( sprintf( __( 'Error deleting pod: %s', 'pods' ), $e->getMessage() ) );
239
		}
240
241
		if ( $deleted ) {
242
			WP_CLI::success( __( 'Pod deleted.', 'pods' ) );
243
		} else {
244
			WP_CLI::error( __( 'Pod not deleted.', 'pods' ) );
245
		}
246
247
	}
248
249
	/**
250
	 * Activate a component.
251
	 *
252
	 * ## OPTIONS
253
	 *
254
	 * --component=<component>
255
	 * : The component identifier.
256
	 *
257
	 * ## EXAMPLES
258
	 *
259
	 * wp pods-api activate-component --component=templates
260
	 *
261
	 * @subcommand activate-component
262
	 */
263 View Code Duplication
	public function activate_component( $args, $assoc_args ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
264
265
		if ( ! class_exists( 'PodsInit' ) ) {
266
			WP_CLI::error( __( 'PodsInit not available', 'pods' ) );
267
268
			return;
269
		}
270
271
		$component = $assoc_args['component'];
272
273
		$active = PodsInit::$components->is_component_active( $component );
0 ignored issues
show
Bug introduced by
The property components cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
274
275
		if ( $active ) {
276
			WP_CLI::error( sprintf( __( 'Component %s is already active.', 'pods' ), $component ) );
277
		} else {
278
			PodsInit::$components->activate_component( $component );
0 ignored issues
show
Bug introduced by
The property components cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
279
280
			WP_CLI::success( __( 'Component activated.', 'pods' ) );
281
		}
282
283
	}
284
285
	/**
286
	 * Deactivate a component.
287
	 *
288
	 * ## OPTIONS
289
	 *
290
	 * --component=<component>
291
	 * : The component identifier.
292
	 *
293
	 * ## EXAMPLES
294
	 *
295
	 * wp pods-api deactivate-component --component=templates
296
	 *
297
	 * @subcommand deactivate-component
298
	 */
299 View Code Duplication
	public function deactivate_component( $args, $assoc_args ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 );
0 ignored issues
show
Bug introduced by
The property components cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
310
311
		if ( ! $active ) {
312
			WP_CLI::error( sprintf( __( 'Component %s is not active.', 'pods' ), $component ) );
313
		} else {
314
			PodsInit::$components->deactivate_component( $component );
0 ignored issues
show
Bug introduced by
The property components cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
315
316
			WP_CLI::success( __( 'Component deactivated.', 'pods' ) );
317
		}
318
319
	}
320
321
	/**
322
	 * Clear the Pods cache.
323
	 *
324
	 * ## EXAMPLES
325
	 *
326
	 * wp pods-api clear-cache
327
	 *
328
	 * @subcommand clear-cache
329
	 */
330
	public function cache_clear() {
331
332
		pods_api()->cache_flush_pods();
333
334
		WP_CLI::success( __( 'Pods cache cleared', 'pods' ) );
335
336
	}
337
338
	/**
339
	 * Export a Pods Package to a file.
340
	 *
341
	 * ## OPTIONS
342
	 *
343
	 * --file=<file>
344
	 * : The file to save to including path (defaults to current path).
345
	 *
346
	 * [--pods=<pods>]
347
	 * : A comma-separated list of Pods IDs to export (default is all Pods).
348
	 *
349
	 * [--templates=<templates>]
350
	 * : A comma-separated list of Pod Template IDs to export (default is all Templates).
351
	 *
352
	 * [--pages=<pages>]
353
	 * : A comma-separated list of Pod Page IDs to export (default is all Pod Pages).
354
	 *
355
	 * ## EXAMPLES
356
	 *
357
	 * wp pods-api export-pod --file="pods-package.json"
358
	 * wp pods-api export-pod --file="pods-package.json" --pods="book,genre"
359
	 * wp pods-api export-pod --file="/path/to/pods-package.json" --pods="book,genre"
360
	 * wp pods-api export-pod --templates="book-single,book-list" --file="pods-package.json"
361
	 * wp pods-api export-pod --pod-pages="books,books/*" --file="pods-package.json"
362
	 * wp pods-api export-pod --pods="book,genre" --templates="book-single,book-list" --pod-pages="books,books/*" --file="pods-package.json"
363
	 *
364
	 * @subcommand export-pod
365
	 */
366
	public function export_pod( $args, $assoc_args ) {
367
368
		if ( ! PodsInit::$components->is_component_active( 'migrate-packages' ) ) {
0 ignored issues
show
Bug introduced by
The property components cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
369
			WP_CLI::error( sprintf( __( 'Migrate Package is not activated. Try activating it: %s', 'pods' ), 'wp pods-api activate-component --component=migrate-packages' ) );
370
		}
371
372
		$params = array(
373
			'pods' => true,
374
		);
375
376
		if ( PodsInit::$components->is_component_active( 'templates' ) ) {
0 ignored issues
show
Bug introduced by
The property components cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
377
			$params['templates'] = true;
378
		}
379
380
		if ( PodsInit::$components->is_component_active( 'pages' ) ) {
0 ignored issues
show
Bug introduced by
The property components cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
381
			$params['pages'] = true;
382
		}
383
384
		$file = $assoc_args['file'];
385
386
		unset( $assoc_args['file'] );
387
388
		$params = array_merge( $params, $assoc_args );
389
390
		$data = false;
391
392
		try {
393
			$data = Pods_Migrate_Packages::export( $params );
394
		} catch ( Exception $e ) {
395
			WP_CLI::error( sprintf( __( 'Error exporting Pods Package: %s', 'pods' ), $e->getMessage() ) );
396
		}
397
398
		if ( ! empty( $data ) ) {
399
			// Load PodsMigrate class file for use.
400
			pods_migrate();
401
402
			// Only JSON format is supported for export.
403
			if ( false === strpos( $file, '.json' ) ) {
404
				$file .= '.json';
405
			}
406
407
			$export_file = PodsMigrate::export_data_to_file( $file, $data, true );
408
409
			if ( $export_file ) {
410
				WP_CLI::success( sprintf( __( 'Pods Package exported: %s', 'pods' ), $export_file ) );
411
			} else {
412
				WP_CLI::error( __( 'Pods Package not exported.', 'pods' ) );
413
			}
414
		} else {
415
			WP_CLI::error( __( 'No Pods Package data found.', 'pods' ) );
416
		}
417
418
	}
419
420
	/**
421
	 * Import a Pods Package from a file.
422
	 *
423
	 * ## OPTIONS
424
	 *
425
	 * --file=<file>
426
	 * : The file to save to including path (defaults to current path).
427
	 *
428
	 * [--replace]
429
	 * : Overwrite imported items if they already exist (defaults to false).
430
	 *
431
	 * ## EXAMPLES
432
	 *
433
	 * wp pods-api import-pod --file="pods-package.json"
434
	 * wp pods-api import-pod --file="/path/to/pods-package.json"
435
	 * wp pods-api import-pod --file="pods-package.json" --replace
436
	 *
437
	 * @subcommand import-pod
438
	 */
439
	function import_pod( $args, $assoc_args ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
440
441
		if ( ! PodsInit::$components->is_component_active( 'migrate-packages' ) ) {
0 ignored issues
show
Bug introduced by
The property components cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
442
			WP_CLI::error( sprintf( __( 'Migrate Package is not activated. Try activating it: %s', 'pods' ), 'wp pods-api activate-component --component=migrate-packages' ) );
443
		}
444
445
		$replace = false;
446
447
		if ( ! empty( $assoc_args['replace'] ) ) {
448
			$replace = true;
449
		}
450
451
		$file = $assoc_args['file'];
452
453
		$imported = false;
454
455
		try {
456
			// Load PodsMigrate class file for use.
457
			pods_migrate();
458
459
			// Only JSON format is supported for import.
460
			if ( false === strpos( $file, '.json' ) ) {
461
				WP_CLI::error( sprintf( __( 'Invalid file format, the file must use the .json extension: %s', 'pods' ), $file ) );
462
			}
463
464
			$data = PodsMigrate::get_data_from_file( $file, true );
465
466
			if ( empty( $data ) ) {
467
				WP_CLI::error( __( 'No Pods Package data found.', 'pods' ) );
468
			}
469
470
			$imported = Pods_Migrate_Packages::import( $data, $replace );
471
		} catch ( Exception $e ) {
472
			WP_CLI::error( sprintf( __( 'Error exporting Pods Package: %s', 'pods' ), $e->getMessage() ) );
473
		}
474
475
		if ( ! empty( $imported ) ) {
476
			WP_CLI::success( __( 'Pods Package imported.', 'pods' ) );
477
		} else {
478
			WP_CLI::error( __( 'Pods Package not imported.', 'pods' ) );
479
		}
480
481
	}
482
483
}
484
485
WP_CLI::add_command( 'pods-api', 'PodsAPI_CLI_Command' );
486