diff --git a/inc/optimus_cli.class.php b/inc/optimus_cli.class.php index 1abbe86..16e9155 100644 --- a/inc/optimus_cli.class.php +++ b/inc/optimus_cli.class.php @@ -57,28 +57,28 @@ public static function optimize( $args, $assoc_args ) { * Checks if all the registered files really have the optimized version. * Note: only works with convert to webp is enabled. */ - public static function syncMissing () { - $options = Optimus::get_options(); - if ($options['webp_convert'] == 0) { - WP_CLI::error('sync-missing command only syncs webp missing images. Looks like you don\'t have this setting enabled.', TRUE); - } - - // Retrieves the post ids with optimized assets registered in the DB. - $posts = Optimus_Management::bulk_optimized_assets(); - - foreach ($posts as $key => $post) { - $assets = Optimus_Request::get_files_paths($post['ID']); - foreach($assets as $asset_path) { - if (stream_resolve_include_path($asset_path) === FALSE) { - $metadata = wp_get_attachment_metadata($post['ID']); - // Unregister the optimus flags when the file is not longer there. - unset($metadata['optimus']); - update_post_meta($post['ID'], '_wp_attachment_metadata', $metadata); - // Registries works at post level. - break; - } + public static function syncMissingWebp () { + $options = Optimus::get_options(); + if ($options['webp_convert'] == 0) { + WP_CLI::error('sync-missing command only syncs webp missing images. Looks like you don\'t have this setting enabled.', TRUE); + } + + // Retrieves the post ids with optimized assets registered in the DB. + $posts = Optimus_Management::bulk_optimized_assets(); + + foreach ($posts as $key => $post) { + $assets = Optimus_Request::get_files_paths($post['ID']); + foreach($assets as $asset_path) { + if (stream_resolve_include_path($asset_path) === FALSE) { + $metadata = wp_get_attachment_metadata($post['ID']); + // Unregister the optimus flags when the file is not longer there. + unset($metadata['optimus']); + update_post_meta($post['ID'], '_wp_attachment_metadata', $metadata); + // Registries works at post level. + break; + } + } } - } } private static function _optimize_image($img) { @@ -130,8 +130,8 @@ public static function add_commands() { ), )); - $cmd_syncmissing = function() { self::syncMissing(); }; - WP_CLI::add_command( 'optimus sync-missing', $cmd_syncmissing, array( + $cmd_syncmissing = function() { self::syncMissingWebp(); }; + WP_CLI::add_command( 'optimus webp sync', $cmd_syncmissing, array( 'shortdesc' => 'Detects missing optimized assets.', )); } diff --git a/inc/optimus_request.class.php b/inc/optimus_request.class.php index 9263b34..6486a30 100644 --- a/inc/optimus_request.class.php +++ b/inc/optimus_request.class.php @@ -314,7 +314,9 @@ public static function optimize_upload_images($upload_data, $attachment_id) { * Gets all the files paths of the optimized images. */ public static function get_files_paths($post_id) { - $metadata = wp_get_attachment_metadata($post_id); + if ( empty ( $metadata = wp_get_attachment_metadata( $post_id ) ) ) { + return array(); + } $files = self::_get_files($metadata, $post_id); $post_files = array();