$user->display_name,
'{{user_email}}' => $user->user_email,
'{{post_title}}' => $post->post_title,
'{{post_url}}' => get_permalink( $post_id ),
'{{post_id}}' => $post_id,
'{{form_name}}' => $form->form_name,
'{{submission_date}}' => current_time( 'mysql' ),
'{{site_name}}' => get_bloginfo( 'name' ),
'{{site_url}}' => home_url(),
);
// Replace variables in subject and message.
$subject = $this->replace_variables(
$templates['submission_received']['subject'],
$variables
);
$message = $this->replace_variables(
$templates['submission_received']['message'],
$variables
);
// Add HTML wrapper.
$message = $this->wrap_html( $message );
// Set headers for HTML email.
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: ' . get_bloginfo( 'name' ) . ' <' . get_option( 'admin_email' ) . '>',
);
return wp_mail( $user->user_email, $subject, $message, $headers );
}
/**
* Send post published email to user
*
* @param int $user_id User ID.
* @param int $post_id Post ID.
* @param object $form Form object (unused but kept for API consistency).
* @return bool Success.
*/
public function send_post_published( $user_id, $post_id, $form ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- $form kept for API consistency.
$templates = get_option( 'affcf_email_templates', array() );
if ( empty( $templates['post_published']['enabled'] ) ) {
return false;
}
$user = get_userdata( $user_id );
if ( ! $user ) {
return false;
}
$post = get_post( $post_id );
if ( ! $post ) {
return false;
}
// Prepare variables.
$variables = array(
'{{username}}' => $user->display_name,
'{{post_title}}' => $post->post_title,
'{{post_url}}' => get_permalink( $post_id ),
'{{site_name}}' => get_bloginfo( 'name' ),
);
// Replace variables.
$subject = $this->replace_variables(
$templates['post_published']['subject'],
$variables
);
$message = $this->replace_variables(
$templates['post_published']['message'],
$variables
);
// Add HTML wrapper.
$message = $this->wrap_html( $message );
// Set headers.
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: ' . get_bloginfo( 'name' ) . ' <' . get_option( 'admin_email' ) . '>',
);
return wp_mail( $user->user_email, $subject, $message, $headers );
}
/**
* Send post rejected email to user
*
* @param int $user_id User ID.
* @param int $post_id Post ID.
* @param string $reason Rejection reason.
* @param object $form Form object (unused but kept for API consistency).
* @return bool Success.
*/
public function send_post_rejected( $user_id, $post_id, $reason, $form ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- $form kept for API consistency.
$templates = get_option( 'affcf_email_templates', array() );
if ( empty( $templates['post_rejected']['enabled'] ) ) {
return false;
}
$user = get_userdata( $user_id );
if ( ! $user ) {
return false;
}
$post = get_post( $post_id );
if ( ! $post ) {
return false;
}
// Get edit link.
$pages = get_option( 'affcf_default_pages', array() );
$edit_page_id = ! empty( $pages['custom_edit_page_id'] )
? $pages['custom_edit_page_id']
: ( $pages['edit_page_id'] ?? 0 );
$edit_link = $edit_page_id
? add_query_arg( 'post_id', $post_id, get_permalink( $edit_page_id ) )
: '';
// Prepare variables.
$variables = array(
'{{username}}' => $user->display_name,
'{{post_title}}' => $post->post_title,
'{{rejection_reason}}' => $reason,
'{{edit_link}}' => $edit_link,
'{{site_name}}' => get_bloginfo( 'name' ),
);
// Replace variables.
$subject = $this->replace_variables(
$templates['post_rejected']['subject'],
$variables
);
$message = $this->replace_variables(
$templates['post_rejected']['message'],
$variables
);
// Add HTML wrapper.
$message = $this->wrap_html( $message );
// Set headers.
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: ' . get_bloginfo( 'name' ) . ' <' . get_option( 'admin_email' ) . '>',
);
return wp_mail( $user->user_email, $subject, $message, $headers );
}
/**
* Send admin notification email
*
* @param int $post_id Post ID.
* @param object $form Form object.
* @return bool Success.
*/
public function send_admin_notification( $post_id, $form ) {
$templates = get_option( 'affcf_email_templates', array() );
if ( empty( $templates['admin_notification']['enabled'] ) ) {
return false;
}
$post = get_post( $post_id );
if ( ! $post ) {
return false;
}
$user = get_userdata( $post->post_author );
if ( ! $user ) {
return false;
}
// Prepare variables.
$variables = array(
'{{username}}' => $user->display_name,
'{{user_email}}' => $user->user_email,
'{{post_title}}' => $post->post_title,
'{{form_name}}' => $form->form_name,
'{{submission_date}}' => current_time( 'mysql' ),
'{{admin_edit_link}}' => admin_url( 'post.php?post=' . $post_id . '&action=edit' ),
'{{site_name}}' => get_bloginfo( 'name' ),
);
// Replace variables.
$subject = $this->replace_variables(
$templates['admin_notification']['subject'],
$variables
);
$message = $this->replace_variables(
$templates['admin_notification']['message'],
$variables
);
// Add HTML wrapper.
$message = $this->wrap_html( $message );
// Set headers.
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: ' . get_bloginfo( 'name' ) . ' <' . get_option( 'admin_email' ) . '>',
);
// Send to admin.
return wp_mail( get_option( 'admin_email' ), $subject, $message, $headers );
}
/**
* Replace variables in template
*
* @param string $template Template string.
* @param array $variables Variables to replace.
* @return string Processed template.
*/
private function replace_variables( $template, $variables ) {
return str_replace(
array_keys( $variables ),
array_values( $variables ),
$template
);
}
/**
* Wrap message in HTML template
*
* @param string $content Email content.
* @return string HTML email.
*/
private function wrap_html( $content ) {
// Convert line breaks to
tags.
$content = nl2br( $content );
ob_start();
?>