Advertisement
nefi_c

custom duplicated image wordpress Christianto @ WPQuestions

Feb 13th, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.67 KB | None | 0 0
  1.  
  2. /* showing duplicated box */
  3. function print_duplicated_image_box( $post ) {
  4.     wp_nonce_field( 'remove_duplicated_nonce_action', 'remove_duplicated_nonce' ); ?>
  5.     <script type="text/javascript">
  6.     jQuery(document).ready(function(){
  7.         $('#scan-images-duplicated, #delete-images-duplicated').click(function(e){
  8.             e.preventDefault();
  9.             var order = $(this).hasClass('scanimages') ? 'scan' : 'delete_all',
  10.                 p_data = {
  11.                     action: 'galleryduplicate',
  12.                     command: order,
  13.                     postID: $('#post_ID').val(),
  14.                     rd_nonce: $('#remove_duplicated_nonce').val()
  15.                 },
  16.                 spinner = order == 'scan' ? $('#remove-duplicated-box .spinner:eq(0)'):$('#remove-duplicated-box .spinner:eq(1)'); 
  17.             spinner.show();
  18.             $.ajax({ url: ajaxurl, data: p_data,
  19.                 success: function(content){  
  20.                     if(order == 'scan'){
  21.                         $('#duplicate-image-result').html(content).css('visibility','visible');
  22.                     } else {
  23.                         $('#duplicate-notification').html(content).css('visibility','visible');
  24.                         setTimeout(function(){
  25.                             $('#duplicate-image-result').animate({height: 0}, 500, function(){$(this).empty().css('height','auto');});
  26.                             $('#duplicate-notification').empty().css({'visibility':'hidden'});
  27.                         },3000)
  28.                     }
  29.                     spinner.hide();
  30.                 }
  31.             });
  32.         });
  33.         $('body').on('click', '.delete-attachment', function(e){
  34.             e.preventDefault();
  35.             var deleted = $(this),
  36.                 p_data = {
  37.                     action: 'galleryduplicate',
  38.                     command: 'delete',
  39.                     imageid: deleted.attr('id'),
  40.                     postID: $('#post_ID').val(),       
  41.                     rd_nonce: $('#remove_duplicated_nonce').val()
  42.                 };
  43.             $('#remove-duplicated-box .spinner:eq(0)').show();
  44.             $.ajax({ url: ajaxurl, data: p_data,
  45.                 success: function(data){  
  46.                     if(data == 'success'){
  47.                         deleted.parent().remove();
  48.                     $('#remove-duplicated-box .spinner:eq(0)').hide();
  49.                     }
  50.                 }
  51.             });
  52.         })
  53.     });
  54.     </script>
  55.     <div class="submitbox" id="remove-duplicated-box" style="padding: 10px 0 0;clear: both;border-top: 1px solid whiteSmoke;margin-top: -2px;">
  56.         <input type="submit" class="button scanimages" id="scan-images-duplicated" value="Scan for Duplicate Images" style="float:right">
  57.         <span class="spinner" style="display: none;"></span>
  58.         <div id="duplicate-image-result" style="visibility: hidden; margin: 5px 0 10px; clear:both; float:left"></div>
  59.         <div class="clear" style="border-top: 1px solid #D9D9D9; border-bottom: 1px solid #fff; margin-bottom: 20px;"></div>
  60.         <input type="submit" class="button button-primary button-large" id="delete-images-duplicated" value="Delete Old Duplicate Images" style="float:right">
  61.         <span class="spinner" style="display: none;"></span>
  62.         <p id="duplicate-notification" style="visibility: hidden; height: 1em; margin: 0; padding: 5px 0; text-align: right; clear:both"></p>
  63.     </div>
  64. <?php
  65. }
  66. // add our custom box
  67. function my_duplicated_box() {
  68.     $posttype = array( 'post', 'page' );
  69.     foreach ($posttype as $type) {
  70.         add_meta_box(
  71.             'check_image_duplication',
  72.             __( 'Remove Duplicate Images'),
  73.             'print_duplicated_image_box',
  74.             $type,
  75.             'side',
  76.             'core'
  77.         );
  78.     }
  79. }
  80. add_action( 'add_meta_boxes', 'my_duplicated_box' );
  81.  
  82. function rename_latest_file($ID, $new_name, $postID){
  83.     $file = get_attached_file($ID);
  84.     $path = pathinfo($file);
  85.     $newfile = $path['dirname']."/".$new_name;
  86.  
  87.     rename($file, $newfile);    
  88.     // update_attached_file( $ID, $newfile );
  89.    
  90.     $wp_filetype = wp_check_filetype(basename($newfile), null );
  91.     $wp_upload_dir = wp_upload_dir();
  92.     $attachment = array(
  93.         'guid' => $wp_upload_dir['url'] . '/' . basename( $newfile ),
  94.         'post_mime_type' => $wp_filetype['type'],
  95.         'post_title' => preg_replace('/\.[^.]+$/', '', basename($newfile)),
  96.         'post_content' => '',
  97.         'post_status' => 'inherit'
  98.     );
  99.    
  100.     $attach_id = wp_insert_attachment( $attachment, $newfile, $postID );
  101.  
  102.     require_once(ABSPATH . 'wp-admin/includes/image.php');
  103.     $attach_data = wp_generate_attachment_metadata( $attach_id, $newfile );
  104.     wp_update_attachment_metadata( $attach_id, $attach_data );
  105.    
  106.     wp_delete_attachment( $ID, false );
  107. }
  108.  
  109. /* check image gallery duplicate function */
  110. function check_gallery_duplication(){
  111.     if(!wp_verify_nonce( $_GET['rd_nonce'],  'remove_duplicated_nonce_action' ))
  112.         die;
  113.        
  114.     $postID = $_GET['postID'];
  115.     $command = $_GET['command'];
  116.    
  117.     $originalname = $imagedata = $similar = $latestID = $duplicate = array();
  118.     $attachments = get_children( array('post_parent' => $postID, 'post_type' => 'attachment', 'post_mime_type' =>'image', 'orderby' => 'post_date') );
  119.    
  120.     // get all original name
  121.     foreach($attachments as $image){
  122.         if(!in_array( $image->post_title,$originalname ))
  123.             $originalname[] = $image->post_title;
  124.     }
  125.    
  126.     foreach($attachments as $image){
  127.         $filename = basename(wp_get_attachment_url($image->ID));
  128.         foreach($originalname as $key){
  129.             preg_match('/'.$key.'.(jpg|png|gif)/i', $filename, $ori);
  130.             if(!empty($ori)){
  131.                 $imagedata[$key.'.'.$ori[1]][] = array('value' => 0, 'extension' => $ori[1], 'filename' => $filename, 'ID' => $image->ID );
  132.                 $similar[$key.'.'.$ori[1]][] = 0;
  133.             } else {
  134.                 preg_match('/'.$key.'(\d+).(jpg|png|gif)/i', $filename, $rev);
  135.                 if(!empty($rev)){
  136.                     $imagedata[$key.'.'.$rev[2]][] = array('value' => (int) $rev[1], 'extension' => $rev[2], 'filename' => $filename, 'ID' => $image->ID);
  137.                     $similar[$key.'.'.$rev[2]][] = $rev[1];
  138.                 }
  139.             }
  140.         }
  141.     }
  142.  
  143.     // return only previous image, not latest (largest number of image identifier)
  144.     foreach($imagedata as $key => $image){
  145.         $latest = max($similar[$key]);
  146.         foreach($image as $data){  
  147.             if($data['value'] != $latest)
  148.                 $duplicate[$key][] = array( 'ID' => $data['ID'], 'filename' => $data['filename'] );
  149.             else
  150.                 $latestID[$key] = $data['ID'];
  151.         }
  152.     }
  153.    
  154.     if($command == 'scan'){
  155.         if(!empty($duplicate)){
  156.             foreach($duplicate as $key => $result){
  157.                 echo '<p>'.$key.' <br/><strong>'.count($result).' Duplicates</strong> <a id="'.$key.'" class="delete-attachment" href="#">Delete</a></p>';
  158.             }
  159.         } else {
  160.             echo '<p>No Duplicated Images found </p>';
  161.         }
  162.     } elseif($command == 'delete'){
  163.         if(!empty($duplicate)){
  164.             $image_name = $_GET['imageid'];
  165.             foreach($duplicate[$image_name] as $image){
  166.                 wp_delete_attachment( $image['ID'], false );
  167.             }
  168.             rename_latest_file( $latestID[$image_name], $image_name, $postID);
  169.             echo 'success';
  170.         }
  171.     } else if($command == 'delete_all'){
  172.         foreach($duplicate as $key => $data){
  173.             foreach($data as $image){
  174.                 wp_delete_attachment( $image['ID'], false );
  175.             }
  176.             rename_latest_file( $latestID[$key], $key, $postID);
  177.         }
  178.         echo 'Images succesfully deleted!';
  179.     }
  180.  
  181.     die;
  182. }
  183. add_action('wp_ajax_galleryduplicate', 'check_gallery_duplication');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement