WP_CLI::confirm PHP Method

confirm() public static method

If 'y' is provided to the question, the script execution continues. If 'n' or any other response is provided to the question, script exits. # wp db drop asks for confirmation before dropping the database. WP_CLI::confirm( "Are you sure you want to drop the database?", $assoc_args );
public static confirm ( string $question, array $assoc_args = [] )
$question string Question to display before the prompt.
$assoc_args array Skips prompt if 'yes' is provided.
    public static function confirm($question, $assoc_args = array())
    {
        if (!\WP_CLI\Utils\get_flag_value($assoc_args, 'yes')) {
            fwrite(STDOUT, $question . " [y/n] ");
            $answer = strtolower(trim(fgets(STDIN)));
            if ('y' != $answer) {
                exit;
            }
        }
    }

Usage Example

Example #1
0
 /**
  * Regenerate thumbnail(s).
  *
  * ## OPTIONS
  *
  * [<attachment-id>...]
  * : One or more IDs of the attachments to regenerate.
  *
  * [--skip-delete]
  * : Skip deletion of the original thumbnails. If your thumbnails are linked from sources outside your control, it's likely best to leave them around. Defaults to false.
  *
  * [--only-missing]
  * : Only generate thumbnails for images missing image sizes.
  *
  * [--yes]
  * : Answer yes to the confirmation message.
  *
  * ## EXAMPLES
  *
  *     # re-generate all thumbnails, without confirmation
  *     wp media regenerate --yes
  *
  *     # re-generate all thumbnails that have IDs between 1000 and 2000
  *     seq 1000 2000 | xargs wp media regenerate
  */
 function regenerate($args, $assoc_args = array())
 {
     if (empty($args)) {
         WP_CLI::confirm('Do you really want to regenerate all images?', $assoc_args);
     }
     $skip_delete = \WP_CLI\Utils\get_flag_value($assoc_args, 'skip-delete');
     $only_missing = \WP_CLI\Utils\get_flag_value($assoc_args, 'only-missing');
     if ($only_missing) {
         $skip_delete = true;
     }
     $query_args = array('post_type' => 'attachment', 'post__in' => $args, 'post_mime_type' => 'image', 'post_status' => 'any', 'posts_per_page' => -1, 'fields' => 'ids');
     $images = new WP_Query($query_args);
     $count = $images->post_count;
     if (!$count) {
         WP_CLI::warning('No images found.');
         return;
     }
     WP_CLI::log(sprintf('Found %1$d %2$s to regenerate.', $count, _n('image', 'images', $count)));
     $errored = false;
     foreach ($images->posts as $id) {
         if (!$this->_process_regeneration($id, $skip_delete, $only_missing)) {
             $errored = true;
         }
     }
     if ($errored) {
         WP_CLI::log(_n('An error occurred with image regeneration.', 'An error occurred regenerating one or more images.', $count));
     } else {
         WP_CLI::success(sprintf('Finished regenerating %1$s.', _n('the image', 'all images', $count)));
     }
 }
All Usage Examples Of WP_CLI::confirm