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