/**
  * Bulk import redirects from a CSV file matching the following structure:
  *
  * redirect_from_path,(redirect_to_post_id|redirect_to_path|redirect_to_url)
  *
  * @subcommand import-from-csv
  * @synopsis --csv=<path-to-csv>
  */
 function import_from_csv($args, $assoc_args)
 {
     define('WP_IMPORTING', true);
     if (empty($assoc_args['csv']) || !file_exists($assoc_args['csv'])) {
         WP_CLI::error("Invalid 'csv' file");
     }
     global $wpdb;
     if (($handle = fopen($assoc_args['csv'], "r")) !== FALSE) {
         while (($data = fgetcsv($handle, 2000, ",")) !== FALSE) {
             $row++;
             $redirect_from = $data[0];
             $redirect_to = $data[1];
             WP_CLI::line("Adding (CSV) redirect for {$redirect_from} to {$redirect_to}");
             WP_CLI::line("-- at {$row}");
             WPCOM_Legacy_Redirector::insert_legacy_redirect($redirect_from, $redirect_to);
             if (0 == $row % 100) {
                 if (function_exists('stop_the_insanity')) {
                     stop_the_insanity();
                 }
                 sleep(1);
             }
         }
         fclose($handle);
     }
 }
 /**
  * Remove corrupt Cron Control data resulting from initial plugin deployment
  *
  * @subcommand remove-all-plugin-data
  * @synopsis [--batch-size=<batch-size>] [--dry-run=<dry-run>]
  */
 public function purge($args, $assoc_args)
 {
     global $wpdb;
     // Are we actually destroying any data?
     $dry_run = true;
     if (isset($assoc_args['dry-run']) && 'false' === $assoc_args['dry-run']) {
         $dry_run = false;
     }
     // Provide some idea of what's going on
     \WP_CLI::line(__('CRON CONTROL', 'automattic-cron-control') . "\n");
     $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_type = %s;", 'a8c_cron_ctrl_event'));
     if (is_numeric($count)) {
         $count = (int) $count;
         \WP_CLI::line(sprintf(__('Found %s total items', 'automattic-cron-control'), number_format_i18n($count)) . "\n\n");
         \WP_CLI::confirm(__('Proceed?', 'automattic-cron-control'));
     } else {
         \WP_CLI::error(__('Something went wrong...aborting!', 'automattic-cron-control'));
     }
     // Should we really destroy all this data?
     if (!$dry_run) {
         \WP_CLI::line(__('This process will remove all CPT data for the Cron Control plugin', 'automattic-cron-control'));
         \WP_CLI::confirm(__('Proceed?', 'automattic-cron-control'));
         \WP_CLI::line("\n" . __('Starting...', 'automattic-cron-control') . "\n");
     }
     // Determine how many batches this will take
     if (isset($assoc_args['batch-size'])) {
         $page_size = max(1, min(absint($assoc_args['batch-size']), 500));
     } else {
         $page_size = 250;
     }
     \WP_CLI::line(sprintf(__('Processing in batches of %s', 'automattic-cron-control'), number_format_i18n($page_size)) . "\n\n");
     $pages = 1;
     $page = 1;
     if ($count > $page_size) {
         $pages = ceil($count / $page_size);
     }
     // Let's get on with it
     do {
         \WP_CLI::line("\n\n" . sprintf(__('Processing page %1$s of %2$s', 'automattic-cron-control'), number_format_i18n($page), number_format_i18n($pages)) . "\n");
         $items = $wpdb->get_results($wpdb->prepare("SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = %s LIMIT %d,%d", 'a8c_cron_ctrl_event', absint(($page - 1) * $page_size), $page_size));
         // Nothing more to do
         if (!is_array($items) || empty($items)) {
             \WP_CLI::line(__('No more items found!', 'automattic-cron-control'));
             break;
         }
         \WP_CLI::line(sprintf(__('Found %s items in this batch'), number_format_i18n(count($items))));
         foreach ($items as $item) {
             \WP_CLI::line("{$item->ID}, `{$item->post_title}`");
             if (!$dry_run) {
                 wp_delete_post($item->ID, true);
             }
         }
         // Some cleanup
         unset($items);
         stop_the_insanity();
         // Prepare for the next batch
         $page++;
         if ($page > $pages) {
             break;
         }
         // Don't rush into the next batch, unless we haven't done anything
         if (!$dry_run) {
             sleep(5);
         }
     } while (true);
     // Remove the now-stale cache when actively run
     if (!$dry_run) {
         wp_cache_delete('a8c_cron_ctrl_option');
         \WP_CLI::line("\n" . sprintf(__('Cleared the %s cache', 'automattic-cron-control'), 'Cron Control'));
     }
     // Fin
     \WP_CLI::success(__('All done.', 'automattic-cron-control'));
 }