Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Plugin to handle email notifications and subscriptions from front and backends and also after moderation approval
<?php
/**
* Plugin Name: Ovni Submission Notifications
*
* Description: Sends email notifications when topics or replies are published. Also deals with moderation.
* Author: Tony Voss
 * Text Domain: Ovni-submission-notifications
* Version: 2.1
* Credits: Started by cloning the plugin pending-submission-notifcations by Razvan Horeanga
* Notes: This plugin handles all notifications except for publication of WP posts and comments, which are handled by WP.
* It does deal with notifying admin when a post is held for moderation.
* History:
0.1 16 Oct 2018 Clone taken from pending-submission-notifcations v1.2
1.0 14 Nov 2018 Development completed - going live
1.1 14 Nov 2018 Added auto-subscription of moderated topics
2.0 29 Jan 2019 Major rework to handle topic & reply noticiations in all cases
2.1 28 Aug 2020 Found sending subscriber emails missing from submission_notifications_send_email
**/
// Exit if file is called directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
require_once plugin_dir_path( __FILE__ ) . 'admin/admin.php';
// We will will take over notification of forum and topic subscribers, so do not want bbPress to do it.
remove_action( 'bbp_new_topic', 'bbp_notify_forum_subscribers', 11, 4 );
remove_action( 'bbp_new_reply', 'bbp_notify_topic_subscribers', 11, 5 );
// will act whenever a post changes status
add_action( 'transition_post_status', 'submission_notifications_send_email', 10, 3 );
/**
* Send out emails depending on who updates the status of the post.
*
* @param string $new_status New post status.
* @param string $old_status Old post status.
* @param WP_Post $post Post object.
*/
function submission_notifications_send_email( $new_status, $old_status, $post ) {
// error_log("Trace Point 0 $postID:" . $post->ID . "Statuses: ". $old_status . ">" . $new_status . " Post type: " . $post->post_type);
// Notify Admin that something has been held for moderation
if (( 'pending' === $new_status) && ('pending' != $old_status)) {
$submission_email = get_option( 'submission_notification_admin_email' );
$admins = ( empty( $submission_email ) ) ? get_option( 'admin_email' ) : $submission_email;
$edit_link = get_edit_post_link( $post->ID, '' );
$preview_link = get_permalink( $post->ID ) . '&preview=true';
$username = get_userdata( $post->post_author );
$username_last_edit = get_the_modified_author();
$subject = __( '[Ovni Owners] New ' . $post->post_type . ' pending review', 'submission-notifications' );
if ($post->post_type !== 'reply') $subject .= ': "' . $post->post_title . '"';
$message = __( 'A new ' . $post->post_type . ' is pending review.', 'submission-notifications' );
$message .= "\r\n\r\n";
$message .= __( 'Author', 'submission-notifications' ) . ': ' . $username->user_login . "\r\n";
$message .= __( 'Title', 'submission-notifications' ) . ': ' . $post->post_title . "\r\n";
$message .= __( 'Last edited by', 'submission-notifications' ) . ': ' . $username_last_edit . "\r\n";
$message .= __( 'Last edit date', 'submission-notifications' ) . ': ' . $post->post_modified ."\r\n";
$message .= __( 'Edit the submission', 'submission-notifications' ) . ': ' . $edit_link . "\r\n";
$message .= __( 'Preview the submission', 'submission-notifications' ) . ': ' . $preview_link;
if ('post' === $post->post_type) $message .= "\r\n\r\nConsider promoting author to site role of Author";
$result = wp_mail( $admins, $subject, $message );
}
elseif ( 'pending' === $old_status && 'publish' === $new_status) {
// Notify Contributor that moderator has published their post.
$username = get_userdata( $post->post_author );
$url = get_permalink( $post->ID );
$subject = __( '[Ovni Owners] Your submission is now live! ', 'submission-notifications' );
if ($post->post_type === 'reply') $message = "Your reply was just published ! \r\n\r\n";
else $message = '"' . $post->post_title . '" ' . __( 'was just published ', 'submission-notifications' ) . "! \r\n\r\n";
$message .= $url;
$result = wp_mail( $username->user_email, $subject, $message );
// now to email subscribers
if ($post->post_type === 'reply'){ // Reply approved
$reply_id = $post->ID;
$topic_id = bbp_get_reply_topic_id($reply_id);
$forum_id = bbp_get_topic_forum_id($topic_id);
bbp_notify_topic_subscribers( $reply_id, $topic_id, bbp_Get_reply_forum_id($reply_id) );
}
else { // Topic approved
$topic_id = $post->ID;
bbp_notify_forum_subscribers($topic_id , bbp_get_topic_forum_id($topic_id));
}
}
}
// Handle publications from backend
function ovni_topic_meta_save($topic_id, $forum_id){
$topic_author_id = bbp_get_topic_author_id($topic_id);
if (bbp_get_topic_status($topic_id) == 'publish'){
// error_log("New topic published from backend topic_id: " . $topic_id . " forum_id: " . $forum_id);
$result=bbp_add_user_topic_subscription($topic_author_id, $topic_id);
bbp_notify_forum_subscribers($topic_id, $forum_id );
}
}
add_action( 'bbp_topic_attributes_metabox_save', 'ovni_topic_meta_save', 10, 2 );
function ovni_reply_meta_save($reply_id, $topic_id, $forum_id, $reply_to){
$reply_author_id = bbp_get_reply_author_id($reply_id);
if (bbp_get_reply_status($reply_id) == 'publish'){
$result = bbp_add_user_topic_subscription($reply_author_id, $topic_id);
// error_log("Ovni reply meta save topic_id: " . strval($topic_id). " reply_author_id: " . strval($reply_author_id) . " sub result: ". ($result?"True":"False"));
// error_log("Subscriptions: ". print_r((array)bbp_get_user_subscribed_topic_ids( $reply_author_id ), true));
bbp_notify_topic_subscribers( $reply_id, $topic_id, $forum_id );
}
}
add_action('bbp_reply_attributes_metabox_save', 'ovni_reply_meta_save', 10, 4);
// Handle publications from frontend
function ovni_new_topic($topic_id, $forum_id, $arg3, $topic_author_id){
if (bbp_get_topic_status($topic_id) == 'publish'){
// error_log("New topic published from frontend topic_id: " . $topic_id . " forum_id: " . $forum_id);
bbp_add_user_topic_subscription($topic_author_id, $topic_id);
bbp_notify_forum_subscribers($topic_id, $forum_id );
}
}
add_action('bbp_new_topic', 'ovni_new_topic', 10, 4);
function ovni_new_reply($reply_id, $topic_id, $forum_id, $arg5, $reply_author_id, $arg7, $reply_to){
if (bbp_get_reply_status($reply_id) == 'publish'){
// error_log("New reply published from frontend topic_id: " . $topic_id . " forum_id: " . $forum_id);
bbp_add_user_topic_subscription($reply_author_id, $topic_id);
bbp_notify_topic_subscribers( $reply_id, $topic_id, $forum_id );
}
}
add_action('bbp_new_reply', 'ovni_new_reply', 10, 7);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment