Guest User

Untitled

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