: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
use Smush\Core\Media\Media_Item_Cache;
use Smush\Core\Media\Media_Item_Optimizer;
if ( ! defined( 'WPINC' ) ) {
* Reduce image file sizes, improve performance and boost your SEO using the free WPMU DEV Smush API.
class CLI extends WP_CLI_Command {
* : Optimize single image, batch or all images.
* : Attachment ID to compress.
* # Smush single image with ID = 10.
* $ wp smush compress --type=single --image=10
* # Smush first 5 images.
* $ wp smush compress --type=batch --image=5
* @param array $args All the positional arguments.
* @param array $assoc_args All the arguments defined like --key=value or --flag or --no-flag.
public function compress( $args, $assoc_args ) {
$type = $assoc_args['type'];
$image = $assoc_args['image'];
/* translators: %d - image ID */
$msg = sprintf( __( 'Smushing image ID: %d', 'wp-smushit' ), absint( $image ) );
$this->smush( $msg, array( $image ) );
/* translators: %d - number of images */
$msg = sprintf( __( 'Smushing first %d images', 'wp-smushit' ), absint( $image ) );
$this->smush_all( $msg, $image );
$this->smush_all( __( 'Smushing all images', 'wp-smushit' ) );
* List unoptimized images.
* : Limit number of images to get.
* # Get all unoptimized images.
* # Get the first 100 images that are not optimized.
* @param array $args All the positional arguments.
public function _list( $args ) {
if ( ! empty( $args ) ) {
$response = WP_CLI::launch_self(
array( '--meta_compare=NOT EXISTS' ),
'post_type' => 'attachment',
'fields' => 'ID, guid, post_mime_type',
'meta_key' => 'wp-smpro-smush-data',
'posts_per_page' => (int) $count,
$images = json_decode( $response->stdout );
if ( empty( $images ) ) {
WP_CLI::success( __( 'No uncompressed images found', 'wp-smushit' ) );
WP_CLI::success( __( 'Unsmushed images:', 'wp-smushit' ) );
WP_CLI\Utils\format_items( 'table', $images, array( 'ID', 'guid', 'post_mime_type' ) );
* : Attachment ID to restore.
* # Restore all images that have backups.
* # Restore single image with ID = 10.
* $ wp smush restore --id=10
* @param array $args All the positional arguments.
* @param array $assoc_args All the arguments defined like --key=value or --flag or --no-flag.
public function restore( $args, $assoc_args ) {
$this->restore_image( absint( $id ) );
* @param string $msg Message for progress bar status.
* @param array $images Attachment IDs.
private function smush( $msg = '', $images = array() ) {
$progress = WP_CLI\Utils\make_progress_bar( $msg, count( $images ) + 1 );
$core = WP_Smush::get_instance()->core();
// We need to initialize the database module (maybe all other modules as well?).
Settings::get_instance()->init();
$unsmushed_attachments = $core->get_unsmushed_attachments();
$attachment_id = array_pop( $images );
// Skip if already Smushed.
$should_convert = $core->mod->webp->should_be_converted( $attachment_id );
if ( ! in_array( (int) $attachment_id, $unsmushed_attachments, true ) && ! $should_convert ) {
/* translators: %d - attachment ID */
$errors[] = sprintf( __( 'Image (ID: %d) already compressed', 'wp-smushit' ), $attachment_id );
$status = $core->mod->smush->smush_single( $attachment_id, true );
if ( is_array( $status ) && isset( $status['error'] ) ) {
/* translators: %1$d - attachment ID, %2$s - error. */
$errors[] = sprintf( __( 'Error compressing image (ID: %1$d). %2$s', 'wp-smushit' ), $attachment_id, $status['error'] );
if ( ! empty( $errors ) ) {
foreach ( $errors as $error ) {
WP_CLI::warning( $error );
WP_CLI::success( __( 'Image compressed', 'wp-smushit' ) );
* Smush all uncompressed images.
* @param string $msg Message for progress bar status.
* @param int $batch Compress only this number of images.
private function smush_all( $msg, $batch = 0 ) {
$attachments = WP_Smush::get_instance()->core()->get_unsmushed_attachments();
$attachments = array_slice( $attachments, 0, $batch );
$progress = WP_CLI\Utils\make_progress_bar( $msg, count( $attachments ) );
foreach ( $attachments as $attachment_id ) {
WP_Smush::get_instance()->core()->mod->smush->smush_single( $attachment_id, true );
WP_CLI::success( __( 'All images compressed', 'wp-smushit' ) );
* @param int $id Image ID to restore. Default: 0 - restores all images.
private function restore_image( $id = 0 ) {
$core = WP_Smush::get_instance()->core();
$attachments = $core->get_smushed_attachments();
if ( empty( $attachments ) ) {
WP_CLI::success( __( 'No images available to restore', 'wp-smushit' ) );
if ( ! in_array( (string) $id, $attachments, true ) ) {
WP_CLI::warning( __( 'Image with defined ID not found', 'wp-smushit' ) );
$attachments = array( $id );
$progress = WP_CLI\Utils\make_progress_bar( __( 'Restoring images', 'wp-smushit' ), count( $attachments ) );
foreach ( $attachments as $attachment_id ) {
if ( ! $core->mod->backup->backup_exists( $attachment_id ) ) {
$warning_text = sprintf(/* translators: %d - attachment ID */
esc_html__( 'Image %d cannot be restored', 'wp-smushit' ),
WP_CLI::warning( $warning_text );
$media_item = Media_Item_Cache::get_instance()->get( $attachment_id );
$optimizer = new Media_Item_Optimizer( $media_item );
$restored = $optimizer->restore();
WP_CLI::error( __( 'There were issues restoring some images', 'wp-smushit' ) );
WP_CLI::success( __( 'All images restored', 'wp-smushit' ) );