Skip to content

Instantly share code, notes, and snippets.

@peterjohnhunt
Created September 29, 2022 00:43
Show Gist options
  • Select an option

  • Save peterjohnhunt/ac15299b3e915d957e707970938cbd55 to your computer and use it in GitHub Desktop.

Select an option

Save peterjohnhunt/ac15299b3e915d957e707970938cbd55 to your computer and use it in GitHub Desktop.
<?php
/**
* Import Specs CLI
*
* @subcommand specs
*
* ## EXAMPLES
*
* wp import file
*
* @when after_wp_load
*/
function file( $args, $assoc_args ) {
list( $filename ) = $args;
$fields = $assoc_args['fields'] ?? 'ID';
$format = $assoc_args['format'] ?? 'table';
$test_run = $assoc_args['test-run'] ?? false;
$dry_run = $assoc_args['dry-run'] ?? false;
if ( ! file_exists( $filename ) ) {
\WP_CLI::error( "Missing file: {$filename}" );
}
$rows = new \WP_CLI\Iterators\CSV($filename);
$items = [];
foreach ($rows as $index => $row) {
$items[] = [
'ID' => $row['ID']
];
if ( $test_run ) break;
}
\WP_CLI\Utils\format_items( $format, $items, $fields );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment