Skip to content

Instantly share code, notes, and snippets.

@c-nefi
Forked from joshmoto/functions.php
Last active December 13, 2015 18:18
Show Gist options
  • Save c-nefi/4953885 to your computer and use it in GitHub Desktop.
Save c-nefi/4953885 to your computer and use it in GitHub Desktop.
// 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
}
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' );
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');
// REMOVE DUPLICATE IMAGES
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('individual');
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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment