/**
* 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)));
}
}