Skip to content

Instantly share code, notes, and snippets.

@radist2s
Last active December 24, 2015 07:39
Show Gist options
  • Save radist2s/6765130 to your computer and use it in GitHub Desktop.
Save radist2s/6765130 to your computer and use it in GitHub Desktop.
Wordpress Vkontakte new comment email notifier
<?php
/**
* Theme Native Code
**/
add_action('wp_ajax_vk_comment_mail', 'vk_comment_mail');
add_action('wp_ajax_nopriv_vk_comment_mail', 'vk_comment_mail');
function vk_comment_mail()
{
if (!isset($_POST['last_comment'])){
return;
}
$api_secret = 'VK_APP_SECRET_CODE'; //Change this string
$comment_text = htmlspecialchars_decode($_POST['last_comment'], ENT_NOQUOTES);//Yes, decode text before sign checking. Why? Because! (under my umbrella...)
$comment_encoding = mb_detect_encoding($comment_text, 'utf-8,cp1251');
$comment_encoded = iconv($comment_encoding, 'cp1251//TRANSLIT', $comment_text);
if (md5($api_secret . $_POST['date'] . $_POST['num'] . $comment_encoded) !== $_POST['sign'])
{
status_header(403);
echo print_r($_POST, true), PHP_EOL;
die('Invalid Sign');
}
$mail_text = '';
$mail_text .= $comment_text . PHP_EOL;
$mail_text .= '<br /><br />' . $_SERVER['HTTP_REFERER'];
//Comment text is a HTML (<br />, <p />, etc)
add_filter('wp_mail_content_type', function(){
return 'text/html';
});
$responce = wp_mail('moderator-email@example.com', 'New Comment on site ' . get_bloginfo('name'), $mail_text);
exit;
}
;(function(){
/**
* Openapi code in body:
* VK.init({apiId: 12345678, onlyWidgets: true});
* VK.Widgets.Comments('vk_comments', {limit: 10, width: '610', attach: '*'})
**/
VK.Observer.subscribe('widgets.comments.new_comment', notify_comment)
function notify_comment(num, last_comment, date, sign) {
jQuery.ajax({
type: 'post',
url: (window.siteurl || '') + '/wp-admin/admin-ajax.php',
data: {
action: 'vk_comment_mail',
num: num,
last_comment: last_comment,
date: date,
sign: sign
}
})
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment