Skip to content

Instantly share code, notes, and snippets.

@paulschreiber
Created May 20, 2022 23:06
Show Gist options
  • Save paulschreiber/a8109ee4afa76f45708c8b980159c9bd to your computer and use it in GitHub Desktop.
Save paulschreiber/a8109ee4afa76f45708c8b980159c9bd to your computer and use it in GitHub Desktop.
<?php
public function delete_thumbnails( $args, $assoc_args ) {
global $wpdb;
if ( isset( $assoc_args['dry-run'] ) && 'false' === $assoc_args['dry-run'] ) {
$dry_run = false;
} else {
$dry_run = true;
WP_CLI::line( '!!! Doing a dry-run, no thumbnails will be deleted.' );
}
$total_rows = 0;
$cache_key = 'full-size-images-hash-' . get_option( 'template' );
$full_size_images = wp_cache_get( $cache_key );
if ( false === $full_size_images ) {
$full_size_images = [];
$result = $wpdb->get_results( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key='_wp_attachment_metadata'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQLPlaceholders.UnnecessaryPrepare
foreach ( $result as $row ) {
$data = unserialize( $row->meta_value ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize
if ( ! isset( $data['file'] ) ) {
continue;
}
$file_path = $data['file'];
$full_size_images[ $file_path ] = 1;
}
wp_cache_set( $cache_key, $full_size_images, false, 300 );
}
$uploads_directory = wp_get_upload_dir()['basedir'];
$years = array_diff( scandir( $uploads_directory ), [ '..', '.', '.DS_Store' ] );
foreach ( $years as $year ) {
// skip unknown folders
if ( ! is_numeric( $year ) ) {
continue;
}
$year_directory = $uploads_directory . '/' . $year;
foreach ( [ '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12' ] as $month ) {
$month_directory = $year_directory . '/' . $month;
// not all months have uploads
if ( ! file_exists( $month_directory ) ) {
continue;
}
$images = array_diff( scandir( $month_directory ), [ '..', '.', '.DS_Store' ] );
foreach ( $images as $image ) {
$path = $year . '/' . $month . '/' . $image;
$full_path = $month_directory . '/' . $image;
if ( ! isset( $full_size_images[ $path ] ) ) {
$total_rows++;
if ( ! $dry_run ) {
WP_CLI::line( "Deleting $full_path" );
unlink( $full_path ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_unlink
} else {
WP_CLI::line( "Found: $full_path" );
}
}
}
}
}
// Print a success message
if ( $total_rows ) {
if ( $dry_run ) {
WP_CLI::success( "Done. Found $total_rows files." );
} else {
WP_CLI::success( "Done. Deleted $total_rows files." );
}
} else {
WP_CLI::success( 'Done. No files needed deleting.' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment