Guest User

Untitled

a guest
Apr 21st, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. // SEND EMAIL WHAT SHOW ON CUSTOM BOX
  2.  
  3. function print_my_custom_notification_box( $post ) {
  4. wp_nonce_field( 'my_custom_notification_box_nonce_action', 'my_custom_notification_box_nonce_field' ); ?>
  5. <script type="text/javascript">
  6. jquery(document).ready(function($){
  7. $('#mynotification-box-button').click(function(e){
  8. e.preventDefault();
  9. $('#mynotification-box .spinner').show();
  10. $.ajax({ url: ajaxurl, data: { action: 'sendnotification', postID: $('#post_ID').val(), my_notification_nonce: $('#my_custom_notification_box_nonce_field').val() },
  11. success: function(content){
  12. $('#mynotification').text(content).css('visibility','visible');
  13. setTimeout(function(){
  14. $('#mynotification').empty().css({'visibility':'hidden'});
  15. },3000)
  16. $('#mynotification-box .spinner').hide();
  17. }
  18. });
  19. });
  20. });
  21. </script>
  22. <div class="submitbox" id="mynotification-box" style="padding: 10px 0 0; clear: both; border-top: 1px solid whiteSmoke; margin-top: -2px;">
  23. <input type="submit" class="button button-primary button-large" id="mynotification-box-button" accesskey="n" value="Send Email Notification" style="float:right">
  24. <span class="spinner" style="display: none;"></span>
  25. <div class="clear"></div>
  26. <p id="mynotification" style="visibility: hidden; height: 1em; margin: 5px 0 10px; text-align: right;"></p>
  27. </div>
  28. <?php
  29. }
  30.  
  31. // SEND EMAIL ADD OUR CUSTOM BOX
  32. function my_custom_notification_box() {
  33. $posttype = array( 'individual' );
  34. foreach ($posttype as $type) {
  35. add_meta_box(
  36. 'post_email_notification',
  37. __( 'Email Notification'),
  38. 'print_my_custom_notification_box',
  39. $type,
  40. 'side',
  41. 'core'
  42. );
  43. }
  44. }
  45. add_action( 'add_meta_boxes', 'my_custom_notification_box' );
  46.  
  47. // SEND EMAIL FUNCTION
  48. function send_mypost_notification(){
  49. if(!wp_verify_nonce( $_GET['my_notification_nonce'], 'my_custom_notification_box_nonce_action' ))
  50. die;
  51.  
  52. $postID = $_GET['postID'];
  53.  
  54. $firstname = get_post_meta($postID, 'first_name', true);
  55. $surname = get_post_meta($postID, 'surname', true);
  56. $email = get_post_meta($postID, 'login_email', true);
  57.  
  58. $subject = get_bloginfo('name').' Notification';
  59.  
  60. $headers = 'From: '.get_bloginfo('name').' <'.get_bloginfo('admin_email').'>' . "\r\n";
  61. $headers .= "MIME-Version: 1.0\r\n";
  62. $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
  63.  
  64. $message = '<html><body>';
  65. $message .= 'Hello '.$firstname.',<br/><br/>';
  66. $message .= 'Your page has been updated.<br/><br/>';
  67. $message .= '<a href="'.get_permalink($postID).'" target="_blank">Click here to view your page.</a><br/><br/>';
  68. $message .= 'From the team<br/>';
  69. $message .= '<a href="'.get_bloginfo('url').'" target="_blank">'.get_bloginfo('url').'</a>';
  70. $message .= '</body></html>';
  71.  
  72. $send_success = wp_mail( $email, $subject, $message, $headers, $attachments );
  73.  
  74. if($send_success)
  75. echo 'Notification is successfully sent';
  76. else
  77. echo 'Notification failed to sent';
  78. die;
  79. }
  80. add_action('wp_ajax_sendnotification', 'send_mypost_notification');
  81.  
  82.  
  83.  
  84.  
  85. // YOUR CODE
  86.  
  87. function get_file_name($str){
  88. return substr($str,strrpos($str, '/')+1,strlen($str));
  89. }
  90.  
  91. function remove_duplicate_attachments($post_id){
  92.  
  93. $args = array(
  94. 'post_type' => 'attachment',
  95. 'numberposts' => -1,
  96. 'post_status' => null,
  97. 'post_parent' => $post_id
  98. );
  99. $attachments = get_posts( $args );
  100.  
  101.  
  102. if ($attachments) {
  103. foreach ($attachments as $attachment) {
  104. $arr[$attachment->ID] = get_file_name($attachment->guid);
  105. }
  106.  
  107. $arr_a = $arr;
  108. }
  109.  
  110.  
  111. $media_arr = array();
  112. $num_of_attach_to_detect = 20;
  113.  
  114. if( !empty( $arr ) ){
  115.  
  116. foreach( $arr as $key => $val ){
  117.  
  118. $filename = substr( $val , 0,strpos( $val ,'.' ) );
  119. $ext = ltrim( strchr( $val, '.' ), '.' );
  120.  
  121. $current = $key;
  122. $media_arr_n = array();
  123. for( $i=1; $i <= $num_of_attach_to_detect; $i++ ){
  124.  
  125. $filename_new = $filename.$i.'.'.$ext;
  126. foreach( $arr_a as $k => $v ){
  127. if( $filename_new == $v )
  128. $media_arr_n[] = $k;
  129. }
  130. }
  131.  
  132. if( !empty( $media_arr_n ) ){
  133. $count = count( $media_arr_n );
  134. $media_arr_n[ $count-1 ] = $current;
  135. $media_arr = array_merge( $media_arr , $media_arr_n );
  136. }
  137. }
  138.  
  139.  
  140.  
  141. if( !empty( $media_arr ) ){
  142. $media_arr = array_unique( $media_arr );
  143. foreach( $media_arr as $attachmentid ){
  144. wp_delete_attachment( $attachmentid );
  145. }
  146.  
  147. return 1;
  148. }
  149.  
  150. }
  151. return 0;
  152. }
  153.  
  154.  
  155. function func_remove_duplicate_images( $post ) {
  156. wp_nonce_field( 'remove_duplicate_images_nonce_action', 'remove_duplicate_images_nonce_field' ); ?>
  157. <script type="text/javascript">
  158. jQuery(document).ready(function($){
  159. $('#remove-images-button').click(function(e){
  160. e.preventDefault();
  161. $('#mynotification-box .spinner').show();
  162. $.ajax({ url: ajaxurl, data: { action: 'remove_duplicate_images_from_post', postID: $('#post_ID').val(), remove_duplicate_images_nonce: $('#remove_duplicate_images_nonce_field').val() },
  163. success: function(content){
  164. $('#mynotification').text(content).css('visibility','visible');
  165. setTimeout(function(){
  166. $('#mynotification').empty().css({'visibility':'hidden'});
  167. },3000)
  168. $('#mynotification-box .spinner').hide();
  169. }
  170. });
  171. });
  172. });
  173. </script>
  174. <div class="submitbox" id="mynotification-box" style="padding: 10px 0 0;clear: both;border-top: 1px solid whiteSmoke;margin-top: -2px;">
  175. <div style="border-bottom: 1px solid #CCCCCC;float: right;margin-bottom: 18px;padding: 4px 0 22px;width: 100%; display:none;">
  176. <input class="button tagadd" type="button" value="Scan for Duplicate Images" style="float:right">
  177. </div>
  178.  
  179. <input type="submit" class="button button-primary button-large" id="remove-images-button" accesskey="n" value="Delete Old Duplicate Images" style="float:right">
  180. <span class="spinner" style="display: none;"></span>
  181. <div class="clear"></div>
  182. <p id="mynotification" style="visibility: hidden; height: 1em; margin: 5px 0 10px; text-align: right;"></p>
  183. </div>
  184. <?php
  185. }
  186.  
  187. add_action("wp_ajax_remove_duplicate_images_from_post", "remove_duplicate_images_from_post");
  188. add_action("wp_ajax_nopriv_remove_duplicate_images_from_post", "remove_duplicate_images_from_post");
  189.  
  190. function remove_duplicate_images_from_post(){
  191. $postID = $_REQUEST['postID'];
  192.  
  193. if(!empty( $postID ) ){
  194. $ret_val = remove_duplicate_attachments( $postID );
  195. if( $ret_val == 1 )
  196. echo 'Images successfully deleted!';
  197. else
  198. echo 'No Duplicates Detected!';
  199.  
  200. }
  201. else
  202. echo 'Not Allowed';
  203. die;
  204. }
  205.  
  206.  
  207. function remove_duplicate_images() {
  208. $posttype = array( 'post', 'page', 'individual' );
  209. foreach ($posttype as $type) {
  210. add_meta_box(
  211. 'post_email_notification',
  212. __( 'Remove Duplicate Images'),
  213. 'func_remove_duplicate_images',
  214. $type,
  215. 'side',
  216. 'core'
  217. );
  218. }
  219. }
  220. add_action( 'add_meta_boxes', 'remove_duplicate_images' );
Add Comment
Please, Sign In to add comment