Create a gist now

Instantly share code, notes, and snippets.

Use this class to create a plugin that will send an email when a post goes into pending review status. It should be noted that auto-save has to be turned off, if not multiple emails will be sent. The class handles turning off auto-save.
class PendingReviewNotifier
{
private $_email_sent_flag = false;
public function run()
{
// add notification action to save_post hook
add_action('save_post', array(&$this, 'send_pending_review_notice'));
// add disable autosave hook
add_action('wp_print_scripts', array(&$this, 'disableAutoSave'));
}
public function disableAutoSave(){
wp_deregister_script('autosave');
}
public function send_pending_review_notice($id) {
// the save_post hook is called twice on every post save, so I'm getting two emails
// I need to set a flag to check if an email has already been sent
if(empty($this->_email_sent_flag)) {
$post_status = null;
if(!empty($_POST['post_status'])) {
$post_status = $_POST['post_status'];
}
$user_id = null;
if(!empty($_POST['user_ID'])) {
$user_id = (integer)$_POST['user_ID'];
}
$user_data = null;
if(!empty($user_id) && is_int($user_id)) {
$user_data = get_userdata($user_id);
}
//verify post is not a revision
if ( !wp_is_post_revision( $post_id ) && !empty($post_status) && $post_status == 'pending') {
$message = 'A post was placed in pending review status on blog: ' . get_bloginfo('name').PHP_EOL.PHP_EOL;
$message .= "Post Title: " . $_POST['post_title'] . PHP_EOL.PHP_EOL;
// Choose one of the following options to show the author's name
$message .= "Post Author: " . $user_data->display_name . '('.$user_data->user_email.')'. PHP_EOL.PHP_EOL;
//$message .= "Link: " . get_permalink($id);
$subject = "Post in pending preview status: " .$_POST['post_title'];
$recipient = get_bloginfo('admin_email');
wp_mail( $recipient, $subject, $message );
// set the sent email flag
$this->_email_sent_flag = true;
}
}
}
}
// start
$pending_review_notifier = new PendingReviewNotifier();
$pending_review_notifier->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment