Created
February 13, 2013 12:05
-
-
Save joshmoto/4944150 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
/* showing duplicated box */ | |
function print_duplicated_image_box( $post ) { | |
wp_nonce_field( 'remove_duplicated_nonce_action', 'remove_duplicated_nonce' ); ?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($){ | |
$('#scan-images-duplicated, #delete-images-duplicated').click(function(e){ | |
e.preventDefault(); | |
var order = $(this).hasClass('scanimages') ? 'scan' : 'delete_all', | |
p_data = { | |
action: 'galleryduplicate', | |
command: order, | |
postID: $('#post_ID').val(), | |
rd_nonce: $('#remove_duplicated_nonce').val() | |
}, | |
spinner = order == 'scan' ? $('#remove-duplicated-box .spinner:eq(0)'):$('#remove-duplicated-box .spinner:eq(1)'); | |
spinner.show(); | |
$.ajax({ url: ajaxurl, data: p_data, | |
success: function(content){ | |
if(order == 'scan'){ | |
$('#duplicate-image-result').html(content).css('visibility','visible'); | |
} else { | |
$('#duplicate-notification').html(content).css('visibility','visible'); | |
setTimeout(function(){ | |
$('#duplicate-image-result').animate({height: 0}, 500, function(){$(this).empty().css('height','auto');}); | |
$('#duplicate-notification').empty().css({'visibility':'hidden'}); | |
},3000) | |
} | |
spinner.hide(); | |
} | |
}); | |
}); | |
$('body').on('click', '.delete-attachment', function(e){ | |
e.preventDefault(); | |
var deleted = $(this), | |
p_data = { | |
action: 'galleryduplicate', | |
command: 'delete', | |
imageid: deleted.attr('id'), | |
rd_nonce: $('#remove_duplicated_nonce').val() | |
}; | |
$('#remove-duplicated-box .spinner:eq(0)').show(); | |
$.ajax({ url: ajaxurl, data: p_data, | |
success: function(data){ | |
if(data == 'success'){ | |
deleted.parent().remove(); | |
$('#remove-duplicated-box .spinner:eq(0)').hide(); | |
} | |
} | |
}); | |
}) | |
}); | |
</script> | |
<div class="submitbox" id="remove-duplicated-box" style="padding: 10px 0 0;clear: both;border-top: 1px solid whiteSmoke;margin-top: -2px;"> | |
<input type="submit" class="button scanimages" id="scan-images-duplicated" value="Scan for Duplicate Images" style="float:right"> | |
<span class="spinner" style="display: none;"></span> | |
<div id="duplicate-image-result" style="visibility: hidden; margin: 5px 0 10px; clear:both; float:left"></div> | |
<div class="clear" style="border-top: 1px solid #D9D9D9; border-bottom: 1px solid #fff; margin-bottom: 20px;"></div> | |
<input type="submit" class="button button-primary button-large" id="delete-images-duplicated" value="Delete Old Duplicate Images" style="float:right"> | |
<span class="spinner" style="display: none;"></span> | |
<p id="duplicate-notification" style="visibility: hidden; height: 1em; margin: 0; padding: 5px 0; text-align: right; clear:both"></p> | |
</div> | |
<?php | |
} | |
// add our custom box | |
function my_duplicated_box() { | |
$posttype = array( 'individual' ); | |
foreach ($posttype as $type) { | |
add_meta_box( | |
'check_image_duplication', | |
__( 'Remove Duplicate Images'), | |
'print_duplicated_image_box', | |
$type, | |
'side', | |
'core' | |
); | |
} | |
} | |
add_action( 'add_meta_boxes', 'my_duplicated_box' ); | |
function rename_latest_file($ID, $new_name, $postID){ | |
$file = get_attached_file($ID); | |
$path = pathinfo($file); | |
$newfilename = $new_name; | |
$newfile = $path['dirname']."/".$newfilename; | |
rename($file, $newfile); | |
// update_attached_file( $ID, $newfile ); | |
$wp_filetype = wp_check_filetype(basename($newfile), null ); | |
$wp_upload_dir = wp_upload_dir(); | |
$attachment = array( | |
'guid' => $wp_upload_dir['url'] . '/' . basename( $newfile ), | |
'post_mime_type' => $wp_filetype['type'], | |
'post_title' => preg_replace('/\.[^.]+$/', '', basename($newfile)), | |
'post_content' => '', | |
'post_status' => 'inherit' | |
); | |
$attach_id = wp_insert_attachment( $attachment, $newfile, $postID ); | |
require_once(ABSPATH . 'wp-admin/includes/image.php'); | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $newfile ); | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
wp_delete_attachment( $ID, false ); | |
} | |
/* check image gallery duplicate function */ | |
function check_gallery_duplication(){ | |
if(!wp_verify_nonce( $_GET['rd_nonce'], 'remove_duplicated_nonce_action' )) | |
die; | |
$postID = $_GET['postID']; | |
$command = $_GET['command']; | |
$imagename = $similar = $latestID = $duplicate = array(); | |
$attachments = get_children( array('post_parent' => $postID, 'post_type' => 'attachment', 'post_mime_type' =>'image', 'orderby' => 'post_date') ); | |
// get all filename and associative ID | |
foreach($attachments as $image){ | |
$filename = basename(wp_get_attachment_url($image->ID)); | |
preg_match('/(^[a-zA-Z0-9_-]{1,})_(\d+).(jpg|png|gif)/i', $filename, $match); | |
if($match[2]){ | |
$imagename[$match[1]][] = array($match[2], $match[3], $image->ID, $filename); | |
$similar[$match[1]][] = $match[2]; | |
} | |
} | |
// return only previous image, not latest (largest number of image identifier) | |
foreach($imagename as $key => $data){ | |
$latest = max($similar[$key]); | |
foreach($data as $id){ | |
if($key.'_'.$latest.'.'.$id[1] != $id[3]) | |
$duplicate[$key.'_1.'.$id[1]][] = array( 'ID' => $id[2], 'filename' => $id[3] ); | |
else | |
$latestID[$key.'_1.'.$id[1]] = $id[2]; | |
} | |
} | |
//Kint::dump($latestID); | |
if($command == 'scan'){ | |
if(!empty($duplicate)){ | |
foreach($duplicate as $key => $result){ | |
echo '<p>'.$key.' <br/><strong>'.count($result).' Duplicates</strong> <a id="'.$key.'" class="delete-attachment" href="#">Delete</a></p>'; | |
} | |
} else { | |
echo '<p>No Duplicated Images found </p>'; | |
} | |
} elseif($command == 'delete'){ | |
if(!empty($duplicate)){ | |
$image_name = $_GET['imageid']; | |
foreach($duplicate[$image_name] as $image){ | |
wp_delete_attachment( $image['ID'], false ); | |
} | |
rename_latest_file( $latestID[$image_name], $image_name, $postID); | |
echo 'success'; | |
} | |
} else if($command == 'delete_all'){ | |
foreach($duplicate as $key => $data){ | |
foreach($data as $image){ | |
wp_delete_attachment( $image['ID'], false ); | |
} | |
rename_latest_file( $latestID[$key], $key, $postID); | |
} | |
echo 'Images succesfully deleted!'; | |
} | |
die; | |
} | |
add_action('wp_ajax_galleryduplicate', 'check_gallery_duplication'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment