Skip to content

Commit

Permalink
Fixed broken command when no metadata available.
Browse files Browse the repository at this point in the history
  • Loading branch information
juliquiron committed May 9, 2019
1 parent 817e2a7 commit ec1a26c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
46 changes: 23 additions & 23 deletions inc/optimus_cli.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.',
));
}
Expand Down
4 changes: 3 additions & 1 deletion inc/optimus_request.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit ec1a26c

Please sign in to comment.