Skip to content

Instantly share code, notes, and snippets.

@mlbd
Created September 3, 2025 21:31
Show Gist options
  • Save mlbd/2f25beb3a32f68eb69baa32b886c63c1 to your computer and use it in GitHub Desktop.
Save mlbd/2f25beb3a32f68eb69baa32b886c63c1 to your computer and use it in GitHub Desktop.
Per-assignee approval email that is sent immediately after an assignment is completed.
// [NEW] Per-assignee approval email
add_action( 'owf_individual_signoff_approved', 'send_individual_signoff_email', 10, 7 );
/**
* [NEW] Send "assignment-style" email when an individual assignee approves a review step.
* Reuses the Assignment Email template via get_step_mail_content(), without needing the next action_id.
*
* @param int $post_id
* @param int $action_history_id Current step action_history_id
* @param int $actor_id Approver user ID
* @param array $review_data Includes 'next_assign_actors' (json) and 'step_id' (proposed next step)
* @param int $current_step_id
* @param int $next_step_id Proposed next step id
*/
function send_individual_signoff_email( $post_id, $action_history_id, $actor_id, $review_data, $current_step_id, $next_step_id, $sign_off_comments ) {
$post_id = intval( $post_id );
$action_history_id = intval( $action_history_id );
$actor_id = intval( $actor_id );
$current_step_id = intval( $current_step_id );
$next_step_id = intval( $next_step_id );
$post = get_post( $post_id );
if ( ! $post ) {
return;
}
// Respect Assignment Emails on/off toggle.
$email_settings = get_option( 'oasiswf_email_settings' );
if ( ! is_array( $email_settings ) || ! isset( $email_settings['assignment_emails'] ) || $email_settings['assignment_emails'] !== 'yes' ) {
return;
}
$current_user_id = get_current_user_id();
$ow_email_settings_helper = new OW_Email_Settings_Helper();
$ow_email = new OW_Email();
// Candidate next assignees for this approval (may be empty).
$candidate_to_users = array();
if ( isset( $review_data['next_assign_actors'] ) && ! empty( $review_data['next_assign_actors'] ) ) {
$decoded = json_decode( $review_data['next_assign_actors'], true );
if ( is_array( $decoded ) ) {
$candidate_to_users = array_filter( array_map( 'intval', $decoded ) );
}
}
// Fallback: if we cannot determine next assignees yet, notify the post author.
if ( empty( $candidate_to_users ) ) {
$candidate_to_users = array( intval( $post->post_author ) );
}
// Use proposed next step id if present, otherwise fall back to current step id.
$step_for_template = $next_step_id > 0 ? $next_step_id : $current_step_id;
$review_data = array(
"comment" => $sign_off_comments,
"task_actor_id" => $actor_id
);
// Use the same comment accessor as assignment emails.
$comment = $ow_email->get_step_comment_content( $action_history_id, $review_data );
$post_id_for_params = $post_id; // clarity
foreach ( $candidate_to_users as $to_user_id ) {
$to_user_id = intval( $to_user_id );
if ( $to_user_id === $current_user_id ) {
continue; // mirror send_step_email: don't email the actor themselves
}
// Build the standard assignment email content using existing helper.
$mails = $ow_email->get_step_mail_content( $action_history_id, $step_for_template, $to_user_id, $post_id_for_params );
if ( ! $mails || empty( $mails->assign_subject ) || empty( $mails->assign_content ) ) {
continue;
}
$mail_content = $mails->assign_content . $comment;
$mail_content = apply_filters( "oasiswf_custom_email_content", $mail_content, $post_id_for_params, $action_history_id );
$email_cc = isset( $mails->assign_cc ) ? json_decode( json_encode( $mails->assign_cc ), true ) : array();
$email_bcc = isset( $mails->assign_bcc ) ? json_decode( json_encode( $mails->assign_bcc ), true ) : array();
$email_recipient_params = array(
"post_id" => $post_id_for_params,
"email_cc" => $email_cc,
"email_bcc" => $email_bcc
);
$recipients = $ow_email_settings_helper->get_cc_recipients( $email_recipient_params );
$cc_users = apply_filters( 'ow_assignment_cc_users', $recipients["email_cc"] );
$bcc_users = apply_filters( 'ow_assignment_bcc_users', $recipients["email_bcc"] );
// Send the email.
$ow_email->send_mail( $to_user_id, $mails->assign_subject, $mail_content, '', $cc_users, $bcc_users );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment