diff --git a/includes/class-notifications.php b/includes/class-notifications.php index 5a006f8..3cb1457 100644 --- a/includes/class-notifications.php +++ b/includes/class-notifications.php @@ -22,6 +22,9 @@ class DDHH_JM_Notifications { // Hook into post status transitions to detect job deactivations add_action( 'transition_post_status', array( __CLASS__, 'send_admin_job_deactivation_notification' ), 10, 3 ); + + // Hook into Formidable form submissions to send application notifications + add_action( 'frm_after_create_entry', array( __CLASS__, 'send_provider_application_notification' ), 30, 2 ); } /** @@ -209,4 +212,127 @@ class DDHH_JM_Notifications { ); } } + + /** + * Send provider notification when a mentor applies to their job + * + * @param int $entry_id Entry ID. + * @param int $form_id Form ID. + */ + public static function send_provider_application_notification( $entry_id, $form_id ) { + // Only process job application form submissions + $application_form_id = DDHH_JM_Formidable::get_job_application_form_id(); + if ( $form_id !== $application_form_id ) { + return; + } + + // Get entry data + $entry = FrmEntry::getOne( $entry_id, true ); + if ( ! $entry ) { + return; + } + + // Extract field values + $applicant_name = ''; + $applicant_email = ''; + $applicant_message = ''; + $job_id = 0; + + foreach ( $entry->metas as $field_id => $value ) { + $field = FrmField::getOne( $field_id ); + if ( ! $field ) { + continue; + } + + switch ( $field->field_key ) { + case 'applicant_name': + $applicant_name = sanitize_text_field( $value ); + break; + case 'applicant_email': + $applicant_email = sanitize_email( $value ); + break; + case 'applicant_message': + $applicant_message = sanitize_textarea_field( $value ); + break; + case 'job_id': + $job_id = absint( $value ); + break; + } + } + + // Validate required fields + if ( empty( $applicant_name ) || empty( $applicant_email ) || empty( $applicant_message ) || empty( $job_id ) ) { + error_log( 'DDHH Job Manager: Missing required fields in job application submission' ); + return; + } + + // Get job post + $post = get_post( $job_id ); + if ( ! $post || 'job_offer' !== $post->post_type ) { + error_log( sprintf( 'DDHH Job Manager: Invalid job_id %d in application submission', $job_id ) ); + return; + } + + // Get job details + $job_title = $post->post_title; + $job_location = get_field( 'job_location', $job_id ); + $job_type = get_field( 'job_type', $job_id ); + + // Get provider contact email from ACF field + $provider_email = get_field( 'job_contact_email', $job_id ); + if ( empty( $provider_email ) ) { + error_log( + sprintf( + 'DDHH Job Manager: Cannot send application notification for job #%d - job_contact_email not set', + $job_id + ) + ); + return; + } + + // Build email subject + $subject = sprintf( 'Neue Bewerbung für: %s', $job_title ); + + // Build email body + $body = sprintf( + "Sie haben eine neue Bewerbung für Ihr Stellenangebot erhalten.\n\n" . + "Stelle: %s\n" . + "Standort: %s\n" . + "Art: %s\n\n" . + "--- Bewerber ---\n" . + "Name: %s\n" . + "E-Mail: %s\n\n" . + "Nachricht:\n%s\n\n" . + "---\n" . + "Bitte antworten Sie direkt an %s.\n\n" . + "Diese E-Mail wurde automatisch gesendet.", + $job_title, + $job_location ? $job_location : 'Nicht angegeben', + $job_type ? $job_type : 'Nicht angegeben', + $applicant_name, + $applicant_email, + $applicant_message, + $applicant_email + ); + + // 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( $provider_email, $subject, $html_body, $headers ); + + // Log if email fails + if ( ! $sent ) { + error_log( + sprintf( + 'DDHH Job Manager: Failed to send application notification for job #%d "%s". Email may be disabled in Local WP environment.', + $job_id, + $job_title + ) + ); + } + } }