Last active
August 29, 2015 14:16
-
-
Save trepmal/70bd27828f23fa551b8e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( ! defined( 'WP_CLI' ) ) return; | |
/** | |
* Media Fix | |
*/ | |
class Media_Fix extends WP_CLI_Command { | |
/** | |
* Media fix | |
* | |
* ## OPTIONS | |
* | |
* [--dry-run] | |
* : Dry run. Only tell which images aren't found. | |
* | |
* ## EXAMPLES | |
* | |
* wp media fix | |
*/ | |
function __invoke( $args, $assoc_args ) { | |
$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; | |
} | |
$upload_dir = wp_upload_dir(); | |
$this->basedir = trailingslashit( $upload_dir['basedir'] ); | |
$this->not_found = 0; | |
$this->regenerated = 0; | |
foreach ( $images->posts as $id ) { | |
$this->_process_fix( $id, $assoc_args ); | |
} | |
WP_CLI::line( "Images not found: {$this->not_found}" ); | |
WP_CLI::line( "Images regenerated: {$this->regenerated}" ); | |
} | |
private function _process_fix( $id, $assoc_args ) { | |
$img_md = wp_get_attachment_metadata( $id ); | |
if ( ! is_array( $img_md ) ) { | |
WP_CLI::warning( "Metadata for $id not an array" ); | |
return; | |
} | |
if ( ! isset( $img_md['file'] ) ) { | |
WP_CLI::warning( "No file name for $id" ); | |
return; | |
} | |
$primary = $img_md['file']; | |
if ( ! isset( $img_md['sizes'] ) ) { | |
WP_CLI::warning( "No sizes set for $id ($primary)" ); | |
return; | |
} | |
$sizes = wp_list_pluck( $img_md['sizes'], 'file' ); | |
$segments = explode('/', $primary); | |
$directory = ''; | |
if ( count( $segments ) > 1 ) { | |
$directory = trailingslashit( implode('/', array_slice( $segments, 0, count( $segments ) - 1 ) ) ); | |
} | |
// build a nice single array to work with | |
$full = $this->basedir . $primary; | |
// $paths = array(); | |
// foreach ( $sizes as $sizename => $file ) { | |
// $paths[ $sizename ] = $this->basedir . $directory . $file; | |
// } | |
// WP_CLI::line( WP_CLI::colorize( "%p[$id]%n $full" ) ); | |
if ( ! file_exists( $full ) ) { | |
WP_CLI::line( WP_CLI::colorize( "%p[$id]%n $full" ) ); | |
WP_CLI::warning( "Full-size image not found, can't regenerate :("); | |
$this->not_found++; | |
return; | |
} | |
$warn = false; | |
// for each image.... | |
foreach ( $sizes as $size => $file ) { | |
// does it exist? | |
if ( ! file_exists( $this->basedir . $directory . $file ) ) { | |
if ( isset( $assoc_args['dry-run'] ) ) { | |
if ( ! $warn ) { WP_CLI::line( WP_CLI::colorize( "%p[$id]%n $full" ) ); $warn = true; } | |
WP_CLI::warning( "{$this->basedir}$directory$file not found!"); | |
$this->not_found++; | |
} else { | |
if ( ! $warn ) { WP_CLI::line( WP_CLI::colorize( "%p[$id]%n $full" ) ); $warn = true; } | |
WP_CLI::warning( "{$this->basedir}$directory$file not found! Regenerating..."); | |
$this->not_found++; | |
// try to create it | |
$new_meta = wp_generate_attachment_metadata( $id, $full ); | |
if ( ! file_exists( $this->basedir . $directory . $file ) ) { | |
WP_CLI::warning( "Could not generate missing image" ); | |
} else { | |
$this->regenerated++; | |
WP_CLI::success( "$file regenerated!" ); | |
} | |
} | |
} | |
} | |
} | |
} | |
WP_CLI::add_command( 'media fix', 'Media_Fix' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment