Skip to content

Instantly share code, notes, and snippets.

@joshmoto
Created February 13, 2013 16:32
Show Gist options
  • Save joshmoto/4945849 to your computer and use it in GitHub Desktop.
Save joshmoto/4945849 to your computer and use it in GitHub Desktop.
functions.php
// SEND EMAIL WHAT SHOW ON CUSTOM BOX
function print_my_custom_notification_box( $post ) {
wp_nonce_field( 'my_custom_notification_box_nonce_action', 'my_custom_notification_box_nonce_field' ); ?>
<script type="text/javascript">
jquery(document).ready(function($){
$('#mynotification-box-button').click(function(e){
e.preventDefault();
$('#mynotification-box .spinner').show();
$.ajax({ url: ajaxurl, data: { action: 'sendnotification', postID: $('#post_ID').val(), my_notification_nonce: $('#my_custom_notification_box_nonce_field').val() },
success: function(content){
$('#mynotification').text(content).css('visibility','visible');
setTimeout(function(){
$('#mynotification').empty().css({'visibility':'hidden'});
},3000)
$('#mynotification-box .spinner').hide();
}
});
});
});
</script>
<div class="submitbox" id="mynotification-box" style="padding: 10px 0 0; clear: both; border-top: 1px solid whiteSmoke; margin-top: -2px;">
<input type="submit" class="button button-primary button-large" id="mynotification-box-button" accesskey="n" value="Send Email Notification" style="float:right">
<span class="spinner" style="display: none;"></span>
<div class="clear"></div>
<p id="mynotification" style="visibility: hidden; height: 1em; margin: 5px 0 10px; text-align: right;"></p>
</div>
<?php
}
// SEND EMAIL ADD OUR CUSTOM BOX
function my_custom_notification_box() {
$posttype = array( 'individual' );
foreach ($posttype as $type) {
add_meta_box(
'post_email_notification',
__( 'Email Notification'),
'print_my_custom_notification_box',
$type,
'side',
'core'
);
}
}
add_action( 'add_meta_boxes', 'my_custom_notification_box' );
// SEND EMAIL FUNCTION
function send_mypost_notification(){
if(!wp_verify_nonce( $_GET['my_notification_nonce'], 'my_custom_notification_box_nonce_action' ))
die;
$postID = $_GET['postID'];
$firstname = get_post_meta($postID, 'first_name', true);
$surname = get_post_meta($postID, 'surname', true);
$email = get_post_meta($postID, 'login_email', true);
$subject = get_bloginfo('name').' Notification';
$headers = 'From: '.get_bloginfo('name').' <'.get_bloginfo('admin_email').'>' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= 'Hello '.$firstname.',<br/><br/>';
$message .= 'Your page has been updated.<br/><br/>';
$message .= '<a href="'.get_permalink($postID).'" target="_blank">Click here to view your page.</a><br/><br/>';
$message .= 'From the team<br/>';
$message .= '<a href="'.get_bloginfo('url').'" target="_blank">'.get_bloginfo('url').'</a>';
$message .= '</body></html>';
$send_success = wp_mail( $email, $subject, $message, $headers, $attachments );
if($send_success)
echo 'Notification is successfully sent';
else
echo 'Notification failed to sent';
die;
}
add_action('wp_ajax_sendnotification', 'send_mypost_notification');
// YOUR CODE
function get_file_name($str){
return substr($str,strrpos($str, '/')+1,strlen($str));
}
function remove_duplicate_attachments($post_id){
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post_id
);
$attachments = get_posts( $args );
if ($attachments) {
foreach ($attachments as $attachment) {
$arr[$attachment->ID] = get_file_name($attachment->guid);
}
$arr_a = $arr;
}
$media_arr = array();
$num_of_attach_to_detect = 20;
if( !empty( $arr ) ){
foreach( $arr as $key => $val ){
$filename = substr( $val , 0,strpos( $val ,'.' ) );
$ext = ltrim( strchr( $val, '.' ), '.' );
$current = $key;
$media_arr_n = array();
for( $i=1; $i <= $num_of_attach_to_detect; $i++ ){
$filename_new = $filename.$i.'.'.$ext;
foreach( $arr_a as $k => $v ){
if( $filename_new == $v )
$media_arr_n[] = $k;
}
}
if( !empty( $media_arr_n ) ){
$count = count( $media_arr_n );
$media_arr_n[ $count-1 ] = $current;
$media_arr = array_merge( $media_arr , $media_arr_n );
}
}
if( !empty( $media_arr ) ){
$media_arr = array_unique( $media_arr );
foreach( $media_arr as $attachmentid ){
wp_delete_attachment( $attachmentid );
}
return 1;
}
}
return 0;
}
function func_remove_duplicate_images( $post ) {
wp_nonce_field( 'remove_duplicate_images_nonce_action', 'remove_duplicate_images_nonce_field' ); ?>
<script type="text/javascript">
jQuery(document).ready(function($){
$('#remove-images-button').click(function(e){
e.preventDefault();
$('#mynotification-box .spinner').show();
$.ajax({ url: ajaxurl, data: { action: 'remove_duplicate_images_from_post', postID: $('#post_ID').val(), remove_duplicate_images_nonce: $('#remove_duplicate_images_nonce_field').val() },
success: function(content){
$('#mynotification').text(content).css('visibility','visible');
setTimeout(function(){
$('#mynotification').empty().css({'visibility':'hidden'});
},3000)
$('#mynotification-box .spinner').hide();
}
});
});
});
</script>
<div class="submitbox" id="mynotification-box" style="padding: 10px 0 0;clear: both;border-top: 1px solid whiteSmoke;margin-top: -2px;">
<div style="border-bottom: 1px solid #CCCCCC;float: right;margin-bottom: 18px;padding: 4px 0 22px;width: 100%; display:none;">
<input class="button tagadd" type="button" value="Scan for Duplicate Images" style="float:right">
</div>
<input type="submit" class="button button-primary button-large" id="remove-images-button" accesskey="n" value="Delete Old Duplicate Images" style="float:right">
<span class="spinner" style="display: none;"></span>
<div class="clear"></div>
<p id="mynotification" style="visibility: hidden; height: 1em; margin: 5px 0 10px; text-align: right;"></p>
</div>
<?php
}
add_action("wp_ajax_remove_duplicate_images_from_post", "remove_duplicate_images_from_post");
add_action("wp_ajax_nopriv_remove_duplicate_images_from_post", "remove_duplicate_images_from_post");
function remove_duplicate_images_from_post(){
$postID = $_REQUEST['postID'];
if(!empty( $postID ) ){
$ret_val = remove_duplicate_attachments( $postID );
if( $ret_val == 1 )
echo 'Images successfully deleted!';
else
echo 'No Duplicates Detected!';
}
else
echo 'Not Allowed';
die;
}
function remove_duplicate_images() {
$posttype = array( 'post', 'page', 'individual' );
foreach ($posttype as $type) {
add_meta_box(
'post_email_notification',
__( 'Remove Duplicate Images'),
'func_remove_duplicate_images',
$type,
'side',
'core'
);
}
}
add_action( 'add_meta_boxes', 'remove_duplicate_images' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment