Skip to content

Instantly share code, notes, and snippets.

@nmoinvaz
Last active October 11, 2018 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmoinvaz/cd6fb0f7988ac5b7c3fb7f0772d6d187 to your computer and use it in GitHub Desktop.
Save nmoinvaz/cd6fb0f7988ac5b7c3fb7f0772d6d187 to your computer and use it in GitHub Desktop.
Send emails to everybody else on a ticket
<?php
/**
* @package Awesome Support: Everybody Reply
* @author Nathan Moinvaziri <nathan@nathanm.com>
* @license MIT
* @link http://github.com/nmoinvaz/
* @copyright 2018 Nathan Moinvaziri
*
* @wordpress-plugin
* Plugin Name: Awesome Support: Everybody Reply
* Plugin URI: http://github.com/nmoinvaz/
* Description: This add-on for Awesome Support enables everybody to get reply email notifications on a ticket.
* Version: 1.2.0
* Author: Nathan Moinvaziri
* Author URI: http://github.com/nmoinvaz/
* Text Domain: as-eb-reply
* License: MIT
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
add_filter( 'wpas_email_notifications_email', 'aseall_set_notification_email', 12, 4 );
/**
* Set the recipient for the e-mail notification
*
* @since 0.2.5
*
* @param null|WP_User $email A email object
* @param string $case Case of the notification being sent out
* @param int $ticket_id ID of the ticket being processed
* @param int $post_id ID of the post being processed
*
* @return WP_User
*/
function aseall_set_notification_email( $email, $case, $ticket_id, $post_id ) {
if( 'private_note_created' === $case || 'satisfaction_survey' === $case ) {
return $email;
}
if ( $ticket_id == null || $post_id == null ) {
return $email;
}
$users = wpas_get_users_who_replied($ticket_id);
if (count($users) == 0) {
return $email;
}
$recipients = array();
// Copy original array of recipients
foreach ($email['recipient_email'] as $recipient) {
$recipients[] = $recipient['user_id'];
}
// Add new users to array of recipients if they don't already exist
foreach ($users as $user_id) {
if (!in_array($user_id, $recipients)) {
$recipients[] = $user_id;
}
}
// Only send emails to users who can edit ticket since note is private
if ( 'ticket_note' === $case ) {
foreach ($recipients as $user_id) {
// Apparently safe in php lol
if (!user_can( $user_id, 'edit_ticket' )) {
if (($key = array_search($user_id, $recipients)) !== false) {
unset($recipients[$key]);
}
}
}
}
// Rebuild array of recipients
$email['recipient_email'] = array();
foreach ($recipients as $user_id) {
$user = get_user_by( 'id', $user_id );
$email['recipient_email'][] = array( 'user_id' => $user->ID, 'email' => $user->user_email );
}
return $email;
}
/**
* Get all users who replied on a ticket
*
* @param integer $post_id ID of the post (ticket) to get the agents from
* @param array $args Additional arguments (see WP_Query)
* @return array|ids
*/
function wpas_get_users_who_replied( $post_id, $args = array() ) {
$users = array();
$ticket = get_post($post_id);
if ($ticket == null) {
return $users;
}
$defaults = array(
'post_parent' => $post_id,
'post_type' => array('ticket_reply', 'ticket_note'),
'post_status' => 'any',
'order' => wpas_get_option( 'replies_order', 'ASC' ),
'orderby' => 'date',
'posts_per_page' => - 1,
'no_found_rows' => true,
'cache_results' => false,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
);
$args = wp_parse_args( $args, $defaults );
$query = new WP_Query( $args );
if (!is_wp_error( $query )) {
foreach ($query->posts as $reply) {
if (!in_array($reply->post_author, $users) && $reply->post_author != $ticket->post_author) {
$users[] = $reply->post_author;
}
}
}
return $users;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment