From 4a1c05c421ea91a09eb8f6a5f52b4c31ee5f4e85 Mon Sep 17 00:00:00 2001 From: Viktor Miller Date: Wed, 14 Jan 2026 20:07:08 +0900 Subject: [PATCH] feat(04-02): add deactivation notification method to notifications class - Added send_admin_job_deactivation_notification() method - Hooks transition_post_status with guards: draft status AND old publish - Extracts deactivation reason from ACF field job_deactivation_reason - Email subject: "Stellenangebot deaktiviert: {job_title}" - Email includes job details, provider info, and deactivation reason - Sends to admin_email with edit link for review - Error logging for missing admin_email or wp_mail failure Co-Authored-By: Claude Sonnet 4.5 --- includes/class-notifications.php | 100 +++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/includes/class-notifications.php b/includes/class-notifications.php index 449f203..5a006f8 100644 --- a/includes/class-notifications.php +++ b/includes/class-notifications.php @@ -19,6 +19,9 @@ class DDHH_JM_Notifications { 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 post status transitions to detect job deactivations + add_action( 'transition_post_status', array( __CLASS__, 'send_admin_job_deactivation_notification' ), 10, 3 ); } /** @@ -109,4 +112,101 @@ class DDHH_JM_Notifications { ); } } + + /** + * Send admin notification when a job is deactivated by provider + * + * @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_job_deactivation_notification( $new_status, $old_status, $post ) { + // Only trigger on job_offer posts transitioning from publish to draft + if ( 'job_offer' !== $post->post_type ) { + return; + } + + // Only send notification when published job becomes draft (deactivation) + // Avoid notification on draft saves or initial draft creation + if ( 'draft' !== $new_status || 'publish' !== $old_status ) { + return; + } + + // Get admin email + $admin_email = get_option( 'admin_email' ); + if ( ! $admin_email ) { + error_log( 'DDHH Job Manager: Cannot send deactivation 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 deactivation reason from ACF field + $deactivation_reason = get_field( 'job_deactivation_reason', $post->ID ); + if ( empty( $deactivation_reason ) ) { + $deactivation_reason = 'Kein Grund angegeben'; + } + + // Get deactivation date + $deactivation_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 deaktiviert: %s', $job_title ); + + // Build email body + $body = sprintf( + "Ein Stellenangebot wurde vom Anbieter deaktiviert.\n\n" . + "Titel: %s\n" . + "Anbieter: %s (%s)\n" . + "Standort: %s\n" . + "Art: %s\n" . + "Deaktiviert am: %s\n\n" . + "Grund für Deaktivierung:\n%s\n\n" . + "Stelle ansehen:\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', + $deactivation_date, + $deactivation_reason ? $deactivation_reason : 'Nicht angegeben', + $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 deactivation notification for job #%d "%s". Email may be disabled in Local WP environment.', + $post->ID, + $job_title + ) + ); + } + } }