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