feat(03-03): create notification class with admin alert on job submission
- Created DDHH_JM_Notifications class with email notification system - Hook into transition_post_status to detect new pending job submissions - Send German email to admin with job details and edit link - Email includes: title, author, org, location, type, date, edit link - Only trigger on new → pending transition to avoid spam on updates - Log errors if wp_mail() fails (common in Local WP) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
112
includes/class-notifications.php
Normal file
112
includes/class-notifications.php
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Email notification system for job submissions
|
||||||
|
*
|
||||||
|
* @package DDHH_Job_Manager
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Exit if accessed directly.
|
||||||
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles email notifications for job submissions
|
||||||
|
*/
|
||||||
|
class DDHH_JM_Notifications {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup notification hooks
|
||||||
|
*/
|
||||||
|
public static function setup_hooks() {
|
||||||
|
// Hook into post status transitions to detect new pending job submissions
|
||||||
|
add_action( 'transition_post_status', array( __CLASS__, 'send_admin_new_job_notification' ), 10, 3 );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send admin notification when a new job is submitted for moderation
|
||||||
|
*
|
||||||
|
* @param string $new_status New post status.
|
||||||
|
* @param string $old_status Old post status.
|
||||||
|
* @param WP_Post $post Post object.
|
||||||
|
*/
|
||||||
|
public static function send_admin_new_job_notification( $new_status, $old_status, $post ) {
|
||||||
|
// Only trigger on job_offer posts transitioning to pending
|
||||||
|
if ( 'job_offer' !== $post->post_type ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only send notification on initial submission to pending
|
||||||
|
// Avoid sending on every save when already pending
|
||||||
|
if ( 'pending' !== $new_status || 'pending' === $old_status ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get admin email
|
||||||
|
$admin_email = get_option( 'admin_email' );
|
||||||
|
if ( ! $admin_email ) {
|
||||||
|
error_log( 'DDHH Job Manager: Cannot send admin notification - admin_email option not set' );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare email data
|
||||||
|
$job_title = $post->post_title;
|
||||||
|
$author = get_userdata( $post->post_author );
|
||||||
|
$author_name = $author ? $author->display_name : 'Unbekannt';
|
||||||
|
$author_org = get_user_meta( $post->post_author, 'ddhh_org_name', true );
|
||||||
|
if ( empty( $author_org ) ) {
|
||||||
|
$author_org = 'Nicht angegeben';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get ACF fields
|
||||||
|
$job_location = get_field( 'job_location', $post->ID );
|
||||||
|
$job_type = get_field( 'job_type', $post->ID );
|
||||||
|
|
||||||
|
// Get submission date
|
||||||
|
$submission_date = get_the_date( 'd.m.Y H:i', $post->ID );
|
||||||
|
|
||||||
|
// Get edit link
|
||||||
|
$edit_link = get_edit_post_link( $post->ID, '' );
|
||||||
|
|
||||||
|
// Build email subject
|
||||||
|
$subject = sprintf( 'Neues Stellenangebot zur Prüfung: %s', $job_title );
|
||||||
|
|
||||||
|
// Build email body
|
||||||
|
$body = sprintf(
|
||||||
|
"Ein neues Stellenangebot wurde eingereicht und wartet auf Ihre Prüfung.\n\n" .
|
||||||
|
"Titel: %s\n" .
|
||||||
|
"Anbieter: %s (%s)\n" .
|
||||||
|
"Standort: %s\n" .
|
||||||
|
"Art: %s\n" .
|
||||||
|
"Eingereicht am: %s\n\n" .
|
||||||
|
"Prüfen Sie das Stellenangebot hier:\n%s\n\n" .
|
||||||
|
"---\n" .
|
||||||
|
"Diese E-Mail wurde automatisch gesendet.",
|
||||||
|
$job_title,
|
||||||
|
$author_name,
|
||||||
|
$author_org,
|
||||||
|
$job_location ? $job_location : 'Nicht angegeben',
|
||||||
|
$job_type ? $job_type : 'Nicht angegeben',
|
||||||
|
$submission_date,
|
||||||
|
$edit_link
|
||||||
|
);
|
||||||
|
|
||||||
|
// Set email headers
|
||||||
|
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
|
||||||
|
|
||||||
|
// Convert plain text to HTML with line breaks
|
||||||
|
$html_body = nl2br( esc_html( $body ) );
|
||||||
|
|
||||||
|
// Send email
|
||||||
|
$sent = wp_mail( $admin_email, $subject, $html_body, $headers );
|
||||||
|
|
||||||
|
// Log if email fails
|
||||||
|
if ( ! $sent ) {
|
||||||
|
error_log(
|
||||||
|
sprintf(
|
||||||
|
'DDHH Job Manager: Failed to send admin notification for job #%d "%s". Email may be disabled in Local WP environment.',
|
||||||
|
$post->ID,
|
||||||
|
$job_title
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user