Skip to content

Instantly share code, notes, and snippets.

@batonac
Last active August 29, 2023 20:12
Show Gist options
  • Save batonac/26db07c24e77fb27459e1bd9c6f59426 to your computer and use it in GitHub Desktop.
Save batonac/26db07c24e77fb27459e1bd9c6f59426 to your computer and use it in GitHub Desktop.
Media Deduper Pro CLI commands
<?php
/*
Plugin Name: Media Deduper Pro CLI
Plugin URI: https://avu.nu/
Description: Provides WP-CLI commands for Media Deduper Pro.
Version: 1.0
Author: Avunu LLC
Author URI: https://avu.nu/
*/
if (!class_exists('WP_CLI')) {
return;
}
/**
* Commands for the Media Deduper Pro Plugin.
*/
class Media_Deduper_Pro_Command extends WP_CLI_Command
{
/**
* Run the media deduplication process.
*/
public function run_indexer()
{
// Fetch all media post IDs that are not already indexed
$media_query_args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => -1,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'mdd_hash',
'compare' => 'NOT EXISTS' // This will only get media without an 'mdd_hash' meta key.
),
),
);
$media_query = new WP_Query($media_query_args);
$media_ids = $media_query->posts;
// check if the number of media items is greater than 0
if (count($media_ids) == 0) {
WP_CLI::success("No media items to process.");
return;
}
// Provide feedback
WP_CLI::log("Found " . count($media_ids) . " media items to process.");
// Initialize the MDD Pro class
$media_deduper_pro = new Media_Deduper_Pro();
// Progress bar
$progress = WP_CLI\Utils\make_progress_bar("Processing", count($media_ids));
// Loop through media IDs and process
foreach ($media_ids as $media_id) {
$result = $media_deduper_pro->calc_media_meta($media_id);
if (is_wp_error($result)) {
WP_CLI::warning($result->get_error_message());
} elseif ($result !== true) {
WP_CLI::warning("Unexpected result for media ID: " . $media_id);
}
// Update progress
$progress->tick();
}
// Finish the progress bar
$progress->finish();
WP_CLI::success("Finished processing all media items.");
}
/**
* Run the smart bulk delete process.
*/
public function smart_bulk_delete()
{
// Initialize your Media_Deduper_Pro instance if not already done.
$media_deduper = new Media_Deduper_Pro();
// Check if bulk processing is already running.
if ($media_deduper->is_bulk_processing()) {
WP_CLI::error(__('There is another Bulk Process already running. Please wait until that process is complete or stop it!', 'media-deduper'));
return;
}
// Fetch duplicate IDs.
$attachments = $this->get_duplicate_ids();
$total_count = count($attachments);
// If there are no duplicates.
if ($total_count < 1) {
WP_CLI::error(__('There are no duplicates on this site!', 'media-deduper'));
return;
}
// Provide feedback
WP_CLI::log("Found " . $total_count . " duplicate media items to process.");
// Progress bar
$progress = WP_CLI\Utils\make_progress_bar("Processing", $total_count);
// Loop through media IDs and process
foreach ($attachments as $attachment) {
// Delete the duplicate.
$media_deduper->bulk_delete_unused_item($attachment);
// Update progress
$progress->tick();
}
// Finish the progress bar
$progress->finish();
// Provide feedback
WP_CLI::success("Finished processing all duplicate media items.");
}
/**
* Retrieves an array of post ids that have duplicate hashes,
* returning a single ID for each group of duplicates.
*
* @return array
*/
private function get_duplicate_ids()
{
global $wpdb;
$sql = "SELECT DISTINCT p.post_id
FROM $wpdb->postmeta AS p
JOIN (
SELECT count(*) AS dupe_count, meta_value
FROM $wpdb->postmeta
WHERE meta_key = 'mdd_hash'
AND meta_value != '" . Media_Deduper_Pro::NOT_FOUND_HASH . "'
GROUP BY meta_value
HAVING dupe_count > 1
) AS p2
ON p.meta_value = p2.meta_value
GROUP BY p.meta_value;";
// debug, write the query to a file
file_put_contents( dirname( __FILE__ ) . '/query.txt', $sql );
$duplicate_ids = $wpdb->get_col($sql);
// If the query returns an empty array, replace it with an array containing a single 0.
if (!count($duplicate_ids)) {
$duplicate_ids = array('0');
}
return $duplicate_ids;
}
}
WP_CLI::add_command('media-deduper-pro', 'Media_Deduper_Pro_Command');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment