fix(07-01): fix timing issue in job notification emails

Moved job submission notifications from transition_post_status hook to custom ddhh_job_submitted/ddhh_job_edited hooks. This ensures metadata is saved before the email is sent, so location and type fields are populated correctly instead of showing "Nicht angegeben".

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-17 19:56:53 +09:00
parent 737f3d6fe9
commit 90525cd0ea

View File

@@ -17,8 +17,11 @@ 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 );
// Hook into job submission to send admin notification (after metadata is saved)
add_action( 'ddhh_job_submitted', array( __CLASS__, 'send_admin_new_job_notification_after_submit' ), 10, 2 );
// Hook into job edit to send admin notification (after metadata is saved)
add_action( 'ddhh_job_edited', array( __CLASS__, 'send_admin_job_edit_notification_after_submit' ), 10, 2 );
// Hook into post status transitions to detect job deactivations
add_action( 'transition_post_status', array( __CLASS__, 'send_admin_job_deactivation_notification' ), 10, 3 );
@@ -31,21 +34,14 @@ class DDHH_JM_Notifications {
}
/**
* Send admin notification when a new job is submitted for moderation
* Send admin notification when a new job is submitted (called after metadata is saved)
*
* @param string $new_status New post status.
* @param string $old_status Old post status.
* @param WP_Post $post Post object.
* @param int $post_id Post ID.
* @param int $entry_id Entry ID.
*/
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 ) {
public static function send_admin_new_job_notification_after_submit( $post_id, $entry_id ) {
$post = get_post( $post_id );
if ( ! $post || 'job_offer' !== $post->post_type ) {
return;
}
@@ -65,9 +61,9 @@ class DDHH_JM_Notifications {
$author_org = 'Nicht angegeben';
}
// Get ACF fields
$job_location = get_field( 'job_location', $post->ID );
$job_type = get_field( 'job_type', $post->ID );
// Get post meta fields
$job_location = get_post_meta( $post->ID, 'job_location', true );
$job_type = get_post_meta( $post->ID, 'job_type', true );
// Get submission date
$submission_date = get_the_date( 'd.m.Y H:i', $post->ID );
@@ -119,6 +115,88 @@ class DDHH_JM_Notifications {
}
}
/**
* Send admin notification when a job is edited (called after metadata is saved)
*
* @param int $post_id Post ID.
* @param int $entry_id Entry ID.
*/
public static function send_admin_job_edit_notification_after_submit( $post_id, $entry_id ) {
$post = get_post( $post_id );
if ( ! $post || 'job_offer' !== $post->post_type ) {
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 post meta fields
$job_location = get_post_meta( $post->ID, 'job_location', true );
$job_type = get_post_meta( $post->ID, 'job_type', true );
// Get submission date
$submission_date = current_time( 'd.m.Y H:i' );
// Get edit link
$edit_link = get_edit_post_link( $post->ID, '' );
// Build email subject
$subject = sprintf( 'Stellenangebot bearbeitet und wartet auf Prüfung: %s', $job_title );
// Build email body
$body = sprintf(
"Ein Stellenangebot wurde bearbeitet und wartet auf erneute Prüfung.\n\n" .
"Titel: %s\n" .
"Anbieter: %s (%s)\n" .
"Standort: %s\n" .
"Art: %s\n" .
"Bearbeitet 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 edit notification for job #%d "%s". Email may be disabled in Local WP environment.',
$post->ID,
$job_title
)
);
}
}
/**
* Send admin notification when a job is deactivated by provider
*
@@ -154,12 +232,12 @@ class DDHH_JM_Notifications {
$author_org = 'Nicht angegeben';
}
// Get ACF fields
$job_location = get_field( 'job_location', $post->ID );
$job_type = get_field( 'job_type', $post->ID );
// Get post meta fields
$job_location = get_post_meta( $post->ID, 'job_location', true );
$job_type = get_post_meta( $post->ID, 'job_type', true );
// Get deactivation reason from ACF field
$deactivation_reason = get_field( 'job_deactivation_reason', $post->ID );
// Get deactivation reason from post meta
$deactivation_reason = get_post_meta( $post->ID, 'job_deactivation_reason', true );
if ( empty( $deactivation_reason ) ) {
$deactivation_reason = 'Kein Grund angegeben';
}
@@ -278,11 +356,11 @@ class DDHH_JM_Notifications {
// Get job details
$job_title = $post->post_title;
$job_location = get_field( 'job_location', $job_id );
$job_type = get_field( 'job_type', $job_id );
$job_location = get_post_meta( $job_id, 'job_location', true );
$job_type = get_post_meta( $job_id, 'job_type', true );
// Get provider contact email from ACF field
$provider_email = get_field( 'job_contact_email', $job_id );
// Get provider contact email from post meta
$provider_email = get_post_meta( $job_id, 'job_contact_email', true );
if ( empty( $provider_email ) ) {
error_log(
sprintf(