Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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'),
- postID: $('#post_ID').val(),
- 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( 'post', 'page' );
- 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);
- $newfile = $path['dirname']."/".$new_name;
- 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'];
- $originalname = $imagedata = $similar = $latestID = $duplicate = array();
- $attachments = get_children( array('post_parent' => $postID, 'post_type' => 'attachment', 'post_mime_type' =>'image', 'orderby' => 'post_date') );
- // get all original name
- foreach($attachments as $image){
- if(!in_array( $image->post_title,$originalname ))
- $originalname[] = $image->post_title;
- }
- foreach($attachments as $image){
- $filename = basename(wp_get_attachment_url($image->ID));
- foreach($originalname as $key){
- preg_match('/'.$key.'.(jpg|png|gif)/i', $filename, $ori);
- if(!empty($ori)){
- $imagedata[$key.'.'.$ori[1]][] = array('value' => 0, 'extension' => $ori[1], 'filename' => $filename, 'ID' => $image->ID );
- $similar[$key.'.'.$ori[1]][] = 0;
- } else {
- preg_match('/'.$key.'(\d+).(jpg|png|gif)/i', $filename, $rev);
- if(!empty($rev)){
- $imagedata[$key.'.'.$rev[2]][] = array('value' => (int) $rev[1], 'extension' => $rev[2], 'filename' => $filename, 'ID' => $image->ID);
- $similar[$key.'.'.$rev[2]][] = $rev[1];
- }
- }
- }
- }
- // return only previous image, not latest (largest number of image identifier)
- foreach($imagedata as $key => $image){
- $latest = max($similar[$key]);
- foreach($image as $data){
- if($data['value'] != $latest)
- $duplicate[$key][] = array( 'ID' => $data['ID'], 'filename' => $data['filename'] );
- else
- $latestID[$key] = $data['ID'];
- }
- }
- 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');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement