Files
Digital-Dabei-Hamburg-Job-M…/includes/class-formidable.php
Viktor Miller 0c9ebb9e89 fix(07-01): fix job type dropdown not pre-populating on edit
Improved JavaScript to use case-insensitive matching for select fields, so 'vollzeit' matches 'Vollzeit'. Also normalized all job_type values to lowercase when saving to ensure consistency.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-17 21:27:49 +09:00

1429 lines
38 KiB
PHP

<?php
/**
* Formidable Forms integration for provider registration
*
* @package DDHH_Job_Manager
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* Handles Formidable Forms integration for provider registration
*/
class DDHH_JM_Formidable {
/**
* Form ID for provider registration
*
* @var int|null
*/
private static $registration_form_id = null;
/**
* Form ID for job submission
*
* @var int|null
*/
private static $job_submission_form_id = null;
/**
* Form ID for job edit
*
* @var int|null
*/
private static $job_edit_form_id = null;
/**
* Form ID for job deactivation
*
* @var int|null
*/
private static $job_deactivation_form_id = null;
/**
* Form ID for job application
*
* @var int|null
*/
private static $job_application_form_id = null;
/**
* Get the registration form ID
*
* @return int|null Form ID or null if not found
*/
public static function get_registration_form_id() {
if ( null !== self::$registration_form_id ) {
return self::$registration_form_id;
}
// Look up form by key
$form = FrmForm::getOne( 'provider_registration' );
if ( $form ) {
self::$registration_form_id = $form->id;
return self::$registration_form_id;
}
return null;
}
/**
* Get the job submission form ID
*
* @return int|null Form ID or null if not found
*/
public static function get_job_submission_form_id() {
if ( null !== self::$job_submission_form_id ) {
return self::$job_submission_form_id;
}
// Look up form by key
$form = FrmForm::getOne( 'job_submission' );
if ( $form ) {
self::$job_submission_form_id = $form->id;
return self::$job_submission_form_id;
}
return null;
}
/**
* Get the job edit form ID
*
* @return int|null Form ID or null if not found
*/
public static function get_job_edit_form_id() {
if ( null !== self::$job_edit_form_id ) {
return self::$job_edit_form_id;
}
// Look up form by key
$form = FrmForm::getOne( 'job_edit' );
if ( $form ) {
self::$job_edit_form_id = $form->id;
return self::$job_edit_form_id;
}
return null;
}
/**
* Get the job deactivation form ID
*
* @return int|null Form ID or null if not found
*/
public static function get_job_deactivation_form_id() {
if ( null !== self::$job_deactivation_form_id ) {
return self::$job_deactivation_form_id;
}
// Look up form by key
$form = FrmForm::getOne( 'job_deactivation' );
if ( $form ) {
self::$job_deactivation_form_id = $form->id;
return self::$job_deactivation_form_id;
}
return null;
}
/**
* Get the job application form ID
*
* @return int|null Form ID or null if not found
*/
public static function get_job_application_form_id() {
if ( null !== self::$job_application_form_id ) {
return self::$job_application_form_id;
}
// Look up form by key
$form = FrmForm::getOne( 'job_application' );
if ( $form ) {
self::$job_application_form_id = $form->id;
return self::$job_application_form_id;
}
return null;
}
/**
* Setup registration hooks
*/
public static function setup_registration_hooks() {
// Create forms on plugin activation if they don't exist
add_action( 'init', array( __CLASS__, 'create_registration_form' ), 11 );
add_action( 'init', array( __CLASS__, 'create_job_submission_form' ), 11 );
add_action( 'init', array( __CLASS__, 'create_job_edit_form' ), 11 );
add_action( 'init', array( __CLASS__, 'create_job_deactivation_form' ), 11 );
add_action( 'init', array( __CLASS__, 'create_job_application_form' ), 11 );
// Hook into Formidable form submission
add_action( 'frm_after_create_entry', array( __CLASS__, 'handle_registration_submission' ), 30, 2 );
add_action( 'frm_after_create_entry', array( __CLASS__, 'handle_job_submission' ), 30, 2 );
add_action( 'frm_after_create_entry', array( __CLASS__, 'handle_job_edit_submission' ), 30, 2 );
add_action( 'frm_after_create_entry', array( __CLASS__, 'handle_job_deactivation_submission' ), 30, 2 );
// Add JavaScript redirect for registration form
add_action( 'wp_footer', array( __CLASS__, 'add_registration_redirect_script' ) );
// Hook into Formidable form validation for ownership check
add_filter( 'frm_validate_entry', array( __CLASS__, 'validate_job_ownership' ), 10, 2 );
add_filter( 'frm_validate_entry', array( __CLASS__, 'validate_job_deactivation_ownership' ), 10, 2 );
// Hook to pre-populate edit form fields
add_filter( 'frm_get_default_value', array( __CLASS__, 'prepopulate_edit_form_fields' ), 10, 3 );
}
/**
* Create the registration form programmatically if it doesn't exist
*/
public static function create_registration_form() {
// Check if Formidable is active
if ( ! class_exists( 'FrmForm' ) ) {
return;
}
// Check if form already exists
$existing_form = FrmForm::getOne( 'provider_registration' );
if ( $existing_form ) {
self::$registration_form_id = $existing_form->id;
return;
}
// Create form
$form_values = array(
'name' => 'Provider Registration',
'form_key' => 'provider_registration',
'description' => 'Anbieter-Registrierung für Digital Dabei Hamburg',
'status' => 'published',
'options' => array(
'submit_value' => 'Registrieren',
'success_msg' => 'Registrierung erfolgreich! Sie werden weitergeleitet...',
'redirect_url' => home_url( '/anbieter-dashboard/' ),
),
);
$form_id = FrmForm::create( $form_values );
if ( ! $form_id ) {
return;
}
self::$registration_form_id = $form_id;
// Create form fields
$fields = array(
array(
'name' => 'Organisationsname',
'field_key' => 'organization_name',
'type' => 'text',
'required' => '1',
'form_id' => $form_id,
'field_order' => 1,
),
array(
'name' => 'Ansprechperson',
'field_key' => 'contact_person',
'type' => 'text',
'required' => '1',
'form_id' => $form_id,
'field_order' => 2,
),
array(
'name' => 'E-Mail',
'field_key' => 'email',
'type' => 'email',
'required' => '1',
'form_id' => $form_id,
'field_order' => 3,
'field_options' => array(
'unique' => '1',
),
),
array(
'name' => 'Passwort',
'field_key' => 'password',
'type' => 'password',
'required' => '1',
'form_id' => $form_id,
'field_order' => 4,
'field_options' => array(
'minlength' => 8,
),
),
array(
'name' => 'Passwort bestätigen',
'field_key' => 'password_confirm',
'type' => 'password',
'required' => '1',
'form_id' => $form_id,
'field_order' => 5,
'field_options' => array(
'minlength' => 8,
),
),
);
foreach ( $fields as $field ) {
FrmField::create( $field );
}
}
/**
* Add JavaScript to redirect after successful registration
*/
public static function add_registration_redirect_script() {
// Only add on login page
if ( ! is_page( 'anbieter-login' ) ) {
return;
}
$redirect_url = home_url( '/anbieter-dashboard/' );
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
var ddhhRedirectTriggered = false;
function triggerRedirect() {
if (ddhhRedirectTriggered) return;
ddhhRedirectTriggered = true;
console.log('DDHH: Redirecting to dashboard in 1.5s...');
setTimeout(function() {
window.location.href = '<?php echo esc_js( $redirect_url ); ?>';
}, 1500);
}
// Watch for Formidable success messages
var checkInterval = setInterval(function() {
if (ddhhRedirectTriggered) {
clearInterval(checkInterval);
return;
}
var successMsg = $('.frm_message, .frm-message').filter(function() {
return $(this).text().indexOf('Registrierung erfolgreich') !== -1;
});
if (successMsg.length > 0 && successMsg.is(':visible')) {
clearInterval(checkInterval);
triggerRedirect();
}
}, 200);
});
</script>
<?php
}
/**
* Handle registration form submission
*
* @param int $entry_id Entry ID.
* @param int $form_id Form ID.
*/
public static function handle_registration_submission( $entry_id, $form_id ) {
error_log('DDHH Registration: Hook fired for form ' . $form_id . ' (' . gettype($form_id) . '), entry ' . $entry_id);
// Only process our registration form (use loose comparison for type coercion)
if ( $form_id != self::get_registration_form_id() ) {
error_log('DDHH Registration: Wrong form ID. Expected ' . self::get_registration_form_id() . ' (' . gettype(self::get_registration_form_id()) . ')');
return;
}
error_log('DDHH Registration: Processing registration form');
// Get entry data
$entry = FrmEntry::getOne( $entry_id, true );
if ( ! $entry ) {
error_log('DDHH Registration: Failed to get entry');
return;
}
error_log('DDHH Registration: Entry retrieved successfully');
// Extract field values
$email = '';
$password = '';
$password_confirm = '';
$organization_name = '';
$contact_person = '';
foreach ( $entry->metas as $field_id => $value ) {
$field = FrmField::getOne( $field_id );
if ( ! $field ) {
continue;
}
error_log('DDHH Registration: Found field ' . $field->field_key . ' with value: ' . json_encode($value));
switch ( $field->field_key ) {
case 'email':
$email = sanitize_email( $value );
break;
case 'password':
$password = $value;
break;
case 'password_confirm':
$password_confirm = $value;
break;
case 'organization_name':
$organization_name = sanitize_text_field( $value );
break;
case 'contact_person':
$contact_person = sanitize_text_field( $value );
break;
}
}
error_log('DDHH Registration: email=' . $email . ', org=' . $organization_name . ', contact=' . $contact_person);
// Validate required fields
if ( empty( $email ) || empty( $password ) || empty( $organization_name ) || empty( $contact_person ) ) {
error_log('DDHH Registration: Validation failed - missing required fields');
return;
}
// Validate passwords match
error_log('DDHH Registration: Comparing passwords - pwd=' . json_encode($password) . ', confirm=' . json_encode($password_confirm));
if ( $password !== $password_confirm ) {
error_log('DDHH Registration: Passwords do not match (strict comparison)');
return;
}
// Check if user already exists
if ( email_exists( $email ) ) {
error_log('DDHH Registration: Email already exists');
return;
}
error_log('DDHH Registration: Validation passed, creating user...');
// Create username from email (part before @)
$username = sanitize_user( strstr( $email, '@', true ) );
// Ensure username is unique
$username_base = $username;
$counter = 1;
while ( username_exists( $username ) ) {
$username = $username_base . $counter;
$counter++;
}
// Create WordPress user
$user_id = wp_insert_user(
array(
'user_login' => $username,
'user_email' => $email,
'user_pass' => $password,
'role' => 'ddhh_provider',
'display_name' => $contact_person,
'first_name' => $contact_person,
)
);
// Check for errors
if ( is_wp_error( $user_id ) ) {
error_log('DDHH Registration: wp_insert_user failed - ' . $user_id->get_error_message());
return;
}
error_log('DDHH Registration: User created successfully with ID ' . $user_id);
// Store organization name as user meta
update_user_meta( $user_id, 'ddhh_org_name', $organization_name );
// Auto-login the user
wp_set_auth_cookie( $user_id, true );
wp_set_current_user( $user_id );
error_log('DDHH Registration: User logged in, triggering redirect...');
// Optionally redirect (handled by JavaScript or form settings)
do_action( 'ddhh_provider_registered', $user_id, $organization_name );
}
/**
* Handle job submission form entry
*
* @param int $entry_id Entry ID.
* @param int $form_id Form ID.
*/
public static function handle_job_submission( $entry_id, $form_id ) {
// Only process our job submission form
if ( $form_id != self::get_job_submission_form_id() ) {
return;
}
error_log( 'DDHH Job Submission: Processing form entry ' . $entry_id );
// Get entry data
$entry = FrmEntry::getOne( $entry_id, true );
if ( ! $entry ) {
error_log( 'DDHH Job Submission: Failed to get entry' );
return;
}
// Extract field values
$job_title = '';
$job_description = '';
$job_location = '';
$job_type = '';
$job_deadline = '';
$job_contact_email = '';
$job_logo = '';
foreach ( $entry->metas as $field_id => $value ) {
$field = FrmField::getOne( $field_id );
if ( ! $field ) {
continue;
}
switch ( $field->field_key ) {
case 'job_title':
$job_title = sanitize_text_field( $value );
break;
case 'job_description':
$job_description = wp_kses_post( $value );
break;
case 'job_location':
$job_location = sanitize_text_field( $value );
break;
case 'job_type':
$job_type = sanitize_text_field( $value );
break;
case 'job_deadline':
$job_deadline = sanitize_text_field( $value );
break;
case 'job_contact_email':
$job_contact_email = sanitize_email( $value );
break;
case 'job_logo':
$job_logo = $value; // File ID
break;
}
}
// Validate required fields
if ( empty( $job_title ) || empty( $job_description ) || empty( $job_location ) || empty( $job_type ) || empty( $job_contact_email ) ) {
error_log( 'DDHH Job Submission: Validation failed - missing required fields' );
return;
}
// Create job_offer post
$post_data = array(
'post_title' => $job_title,
'post_content' => $job_description,
'post_type' => 'job_offer',
'post_status' => 'pending',
'post_author' => get_current_user_id(),
);
$post_id = wp_insert_post( $post_data );
// Check for errors
if ( is_wp_error( $post_id ) ) {
error_log( 'DDHH Job Submission: wp_insert_post failed - ' . $post_id->get_error_message() );
return;
}
error_log( 'DDHH Job Submission: Post created successfully with ID ' . $post_id );
// Save custom fields
update_post_meta( $post_id, 'job_location', $job_location );
update_post_meta( $post_id, 'job_type', $job_type );
update_post_meta( $post_id, 'job_contact_email', $job_contact_email );
if ( ! empty( $job_deadline ) ) {
update_post_meta( $post_id, 'job_deadline', $job_deadline );
}
// Handle logo upload if present
if ( ! empty( $job_logo ) && is_numeric( $job_logo ) ) {
set_post_thumbnail( $post_id, absint( $job_logo ) );
error_log( 'DDHH Job Submission: Logo set as featured image' );
}
error_log( 'DDHH Job Submission: Job offer created successfully' );
do_action( 'ddhh_job_submitted', $post_id, $entry_id );
}
/**
* Handle job edit form submission
*
* @param int $entry_id Entry ID.
* @param int $form_id Form ID.
*/
public static function handle_job_edit_submission( $entry_id, $form_id ) {
// Only process our job edit form
if ( $form_id != self::get_job_edit_form_id() ) {
return;
}
// Check if we have a job_id to update
if ( ! isset( $_GET['job_id'] ) ) {
error_log( 'DDHH Job Edit: No job_id provided' );
return;
}
$post_id = absint( $_GET['job_id'] );
error_log( 'DDHH Job Edit: Processing form entry ' . $entry_id . ' for post ' . $post_id );
// Verify post exists and user owns it
$post = get_post( $post_id );
if ( ! $post || 'job_offer' !== $post->post_type ) {
error_log( 'DDHH Job Edit: Invalid post' );
return;
}
if ( absint( $post->post_author ) !== get_current_user_id() ) {
error_log( 'DDHH Job Edit: User does not own this post' );
return;
}
// Get entry data
$entry = FrmEntry::getOne( $entry_id, true );
if ( ! $entry ) {
error_log( 'DDHH Job Edit: Failed to get entry' );
return;
}
// Extract field values (accounting for "2" suffix)
$job_title = '';
$job_description = '';
$job_location = '';
$job_type = '';
$job_deadline = '';
$job_contact_email = '';
$job_logo = '';
foreach ( $entry->metas as $field_id => $value ) {
$field = FrmField::getOne( $field_id );
if ( ! $field ) {
continue;
}
switch ( $field->field_key ) {
case 'job_title':
case 'job_title2':
$job_title = sanitize_text_field( $value );
break;
case 'job_description':
case 'job_description2':
$job_description = wp_kses_post( $value );
break;
case 'job_location':
case 'job_location2':
$job_location = sanitize_text_field( $value );
break;
case 'job_type':
case 'job_type2':
$job_type = strtolower( sanitize_text_field( $value ) );
break;
case 'job_deadline':
case 'job_deadline2':
$job_deadline = sanitize_text_field( $value );
break;
case 'job_contact_email':
case 'job_contact_email2':
$job_contact_email = sanitize_email( $value );
break;
case 'job_logo':
case 'job_logo2':
$job_logo = $value; // File ID
break;
}
}
// Validate required fields
if ( empty( $job_title ) || empty( $job_description ) || empty( $job_location ) || empty( $job_type ) || empty( $job_contact_email ) ) {
error_log( 'DDHH Job Edit: Validation failed - missing required fields' );
return;
}
// Update job_offer post
$post_data = array(
'ID' => $post_id,
'post_title' => $job_title,
'post_content' => $job_description,
'post_status' => 'pending', // Reset to pending for admin review
);
$result = wp_update_post( $post_data );
// Check for errors
if ( is_wp_error( $result ) ) {
error_log( 'DDHH Job Edit: wp_update_post failed - ' . $result->get_error_message() );
return;
}
error_log( 'DDHH Job Edit: Post updated successfully with ID ' . $post_id );
// Update custom fields
update_post_meta( $post_id, 'job_location', $job_location );
update_post_meta( $post_id, 'job_type', $job_type );
update_post_meta( $post_id, 'job_contact_email', $job_contact_email );
if ( ! empty( $job_deadline ) ) {
update_post_meta( $post_id, 'job_deadline', $job_deadline );
}
// Handle logo upload if present
if ( ! empty( $job_logo ) && is_numeric( $job_logo ) ) {
set_post_thumbnail( $post_id, absint( $job_logo ) );
error_log( 'DDHH Job Edit: Logo updated' );
}
error_log( 'DDHH Job Edit: Job offer updated successfully' );
do_action( 'ddhh_job_edited', $post_id, $entry_id );
}
/**
* Handle job deactivation form submission
*
* @param int $entry_id Entry ID.
* @param int $form_id Form ID.
*/
public static function handle_job_deactivation_submission( $entry_id, $form_id ) {
// Only process our job deactivation form
if ( $form_id != self::get_job_deactivation_form_id() ) {
return;
}
// Check if we have a job_id to deactivate
if ( ! isset( $_GET['job_id'] ) ) {
error_log( 'DDHH Job Deactivation: No job_id provided' );
return;
}
$post_id = absint( $_GET['job_id'] );
error_log( 'DDHH Job Deactivation: Processing form entry ' . $entry_id . ' for post ' . $post_id );
// Verify post exists and user owns it
$post = get_post( $post_id );
if ( ! $post || 'job_offer' !== $post->post_type ) {
error_log( 'DDHH Job Deactivation: Invalid post' );
return;
}
if ( absint( $post->post_author ) !== get_current_user_id() ) {
error_log( 'DDHH Job Deactivation: User does not own this post' );
return;
}
// Get entry data
$entry = FrmEntry::getOne( $entry_id, true );
if ( ! $entry ) {
error_log( 'DDHH Job Deactivation: Failed to get entry' );
return;
}
// Extract deactivation reason
$deactivation_reason = '';
foreach ( $entry->metas as $field_id => $value ) {
$field = FrmField::getOne( $field_id );
if ( ! $field ) {
continue;
}
if ( 'deactivation_reason' === $field->field_key ) {
$deactivation_reason = sanitize_textarea_field( $value );
break;
}
}
// Update post status to draft (deactivated)
$result = wp_update_post( array(
'ID' => $post_id,
'post_status' => 'draft',
) );
// Check for errors
if ( is_wp_error( $result ) ) {
error_log( 'DDHH Job Deactivation: wp_update_post failed - ' . $result->get_error_message() );
return;
}
error_log( 'DDHH Job Deactivation: Post status updated to draft for post ' . $post_id );
// Save deactivation reason (using ACF field name)
if ( ! empty( $deactivation_reason ) ) {
update_post_meta( $post_id, 'job_deactivation_reason', $deactivation_reason );
update_post_meta( $post_id, 'job_deactivation_date', current_time( 'mysql' ) );
error_log( 'DDHH Job Deactivation: Saved deactivation reason' );
}
error_log( 'DDHH Job Deactivation: Job deactivated successfully' );
do_action( 'ddhh_job_deactivated', $post_id, $entry_id );
}
/**
* Create the job submission form programmatically if it doesn't exist
*/
public static function create_job_submission_form() {
// Check if Formidable is active
if ( ! class_exists( 'FrmForm' ) || ! class_exists( 'FrmFormActionsController' ) ) {
return;
}
// Check if form already exists
$existing_form = FrmForm::getOne( 'job_submission' );
if ( $existing_form ) {
self::$job_submission_form_id = $existing_form->id;
return;
}
// Create form
$form_values = array(
'name' => 'Stellenangebot einreichen',
'form_key' => 'job_submission',
'description' => 'Alle Felder mit * sind Pflichtfelder. Ihr Angebot wird vor Veröffentlichung geprüft.',
'status' => 'published',
'options' => array(
'submit_value' => 'Stellenangebot einreichen',
'success_msg' => 'Ihr Stellenangebot wurde zur Prüfung eingereicht!',
'success_action' => 'redirect',
'success_url' => home_url( '/anbieter-dashboard/' ),
),
);
$form_id = FrmForm::create( $form_values );
if ( ! $form_id ) {
return;
}
self::$job_submission_form_id = $form_id;
// Create form fields
$fields_data = array(
array(
'name' => 'Stellentitel',
'field_key' => 'job_title',
'type' => 'text',
'required' => '1',
'form_id' => $form_id,
'field_order' => 1,
),
array(
'name' => 'Stellenbeschreibung',
'field_key' => 'job_description',
'type' => 'textarea',
'required' => '1',
'form_id' => $form_id,
'field_order' => 2,
),
array(
'name' => 'Standort',
'field_key' => 'job_location',
'type' => 'text',
'required' => '1',
'form_id' => $form_id,
'field_order' => 3,
),
array(
'name' => 'Art',
'field_key' => 'job_type',
'type' => 'select',
'required' => '1',
'form_id' => $form_id,
'field_order' => 4,
'field_options' => array(
'options' => array(
'Vollzeit' => 'Vollzeit',
'Teilzeit' => 'Teilzeit',
'Ehrenamt' => 'Ehrenamt',
),
),
),
array(
'name' => 'Bewerbungsfrist',
'field_key' => 'job_deadline',
'type' => 'date',
'required' => '0',
'form_id' => $form_id,
'field_order' => 5,
'field_options' => array(
'format' => 'd.m.Y',
),
),
array(
'name' => 'Kontakt-E-Mail',
'field_key' => 'job_contact_email',
'type' => 'email',
'required' => '1',
'form_id' => $form_id,
'field_order' => 6,
),
array(
'name' => 'Logo',
'field_key' => 'job_logo',
'type' => 'file',
'required' => '0',
'form_id' => $form_id,
'field_order' => 7,
'field_options' => array(
'restrict' => '1',
'allowed_types' => 'image/jpeg,image/png',
'max_size' => '2',
),
),
);
// Store field IDs for form action mapping
$field_ids = array();
foreach ( $fields_data as $field ) {
$field_id = FrmField::create( $field );
if ( $field_id ) {
$field_ids[ $field['field_key'] ] = $field_id;
}
}
// Create the Create Post action
if ( ! empty( $field_ids ) ) {
$action_values = array(
'menu_order' => 1,
'post_status' => 'published',
'post_content' => array(
'post_type' => 'job_offer',
'post_status' => 'pending',
'post_title' => $field_ids['job_title'],
'post_content' => $field_ids['job_description'],
'post_author' => 'current_user',
'post_custom_fields' => array(
array(
'meta_name' => 'job_location',
'field_id' => $field_ids['job_location'],
),
array(
'meta_name' => 'job_type',
'field_id' => $field_ids['job_type'],
),
array(
'meta_name' => 'job_deadline',
'field_id' => $field_ids['job_deadline'],
),
array(
'meta_name' => 'job_contact_email',
'field_id' => $field_ids['job_contact_email'],
),
array(
'meta_name' => 'job_logo',
'field_id' => $field_ids['job_logo'],
),
),
),
);
// Create the form action using the proper API
FrmFormActionsController::create_action(
$form_id,
array(
'post_excerpt' => 'wppost',
'post_content' => $action_values,
'menu_order' => 1,
)
);
}
}
/**
* Create the job edit form programmatically if it doesn't exist
*/
public static function create_job_edit_form() {
// Check if Formidable is active
if ( ! class_exists( 'FrmForm' ) || ! class_exists( 'FrmFormActionsController' ) ) {
return;
}
// Check if form already exists
$existing_form = FrmForm::getOne( 'job_edit' );
if ( $existing_form ) {
self::$job_edit_form_id = $existing_form->id;
return;
}
// Create form
$form_values = array(
'name' => 'Stellenangebot bearbeiten',
'form_key' => 'job_edit',
'description' => 'Bearbeiten Sie Ihr Stellenangebot. Alle Felder mit * sind Pflichtfelder.',
'status' => 'published',
'options' => array(
'submit_value' => 'Änderungen speichern',
'success_msg' => 'Ihre Änderungen wurden gespeichert!',
'success_action' => 'redirect',
'success_url' => home_url( '/anbieter-dashboard/' ),
),
);
$form_id = FrmForm::create( $form_values );
if ( ! $form_id ) {
return;
}
self::$job_edit_form_id = $form_id;
// Create form fields (identical to submission form)
$fields_data = array(
array(
'name' => 'Stellentitel',
'field_key' => 'job_title',
'type' => 'text',
'required' => '1',
'form_id' => $form_id,
'field_order' => 1,
),
array(
'name' => 'Stellenbeschreibung',
'field_key' => 'job_description',
'type' => 'textarea',
'required' => '1',
'form_id' => $form_id,
'field_order' => 2,
),
array(
'name' => 'Standort',
'field_key' => 'job_location',
'type' => 'text',
'required' => '1',
'form_id' => $form_id,
'field_order' => 3,
),
array(
'name' => 'Art',
'field_key' => 'job_type',
'type' => 'select',
'required' => '1',
'form_id' => $form_id,
'field_order' => 4,
'field_options' => array(
'options' => array(
'Vollzeit' => 'Vollzeit',
'Teilzeit' => 'Teilzeit',
'Ehrenamt' => 'Ehrenamt',
),
),
),
array(
'name' => 'Bewerbungsfrist',
'field_key' => 'job_deadline',
'type' => 'date',
'required' => '0',
'form_id' => $form_id,
'field_order' => 5,
'field_options' => array(
'format' => 'd.m.Y',
),
),
array(
'name' => 'Kontakt-E-Mail',
'field_key' => 'job_contact_email',
'type' => 'email',
'required' => '1',
'form_id' => $form_id,
'field_order' => 6,
),
array(
'name' => 'Logo',
'field_key' => 'job_logo',
'type' => 'file',
'required' => '0',
'form_id' => $form_id,
'field_order' => 7,
'field_options' => array(
'restrict' => '1',
'allowed_types' => 'image/jpeg,image/png',
'max_size' => '2',
),
),
);
// Store field IDs for form action mapping
$field_ids = array();
foreach ( $fields_data as $field ) {
$field_id = FrmField::create( $field );
if ( $field_id ) {
$field_ids[ $field['field_key'] ] = $field_id;
}
}
// Create the Update Post action
if ( ! empty( $field_ids ) ) {
$action_values = array(
'menu_order' => 1,
'post_status' => 'published',
'post_content' => array(
'post_type' => 'job_offer',
'post_status' => 'pending',
'post_title' => $field_ids['job_title'],
'post_content' => $field_ids['job_description'],
'post_id' => 'id_param',
'post_custom_fields' => array(
array(
'meta_name' => 'job_location',
'field_id' => $field_ids['job_location'],
),
array(
'meta_name' => 'job_type',
'field_id' => $field_ids['job_type'],
),
array(
'meta_name' => 'job_deadline',
'field_id' => $field_ids['job_deadline'],
),
array(
'meta_name' => 'job_contact_email',
'field_id' => $field_ids['job_contact_email'],
),
array(
'meta_name' => 'job_logo',
'field_id' => $field_ids['job_logo'],
),
),
),
);
// Create the form action using the proper API
FrmFormActionsController::create_action(
$form_id,
array(
'post_excerpt' => 'wppost',
'post_content' => $action_values,
'menu_order' => 1,
)
);
}
}
/**
* Pre-populate edit form fields with existing post data
*
* @param mixed $default_value The default value.
* @param object $field The field object.
* @param bool $dynamic_default Whether to use dynamic default.
* @return mixed The modified default value.
*/
public static function prepopulate_edit_form_fields( $default_value, $field, $dynamic_default ) {
// Only process for the job edit form
if ( absint( $field->form_id ) !== self::get_job_edit_form_id() ) {
return $default_value;
}
// Check if we're editing - look for job_id in URL
if ( ! isset( $_GET['job_id'] ) ) {
return $default_value;
}
$post_id = absint( $_GET['job_id'] );
$post = get_post( $post_id );
if ( ! $post || 'job_offer' !== $post->post_type ) {
return $default_value;
}
// Map field keys to post data (handle both with and without "2" suffix)
switch ( $field->field_key ) {
case 'job_title':
case 'job_title2':
return $post->post_title;
case 'job_description':
case 'job_description2':
return $post->post_content;
case 'job_location':
case 'job_location2':
$value = get_post_meta( $post_id, 'job_location', true );
return $value ? $value : $default_value;
case 'job_type':
case 'job_type2':
$value = get_post_meta( $post_id, 'job_type', true );
return $value ? $value : $default_value;
case 'job_deadline':
case 'job_deadline2':
$value = get_post_meta( $post_id, 'job_deadline', true );
return $value ? $value : $default_value;
case 'job_contact_email':
case 'job_contact_email2':
$value = get_post_meta( $post_id, 'job_contact_email', true );
return $value ? $value : $default_value;
case 'job_logo':
case 'job_logo2':
$value = get_post_thumbnail_id( $post_id );
return $value ? $value : $default_value;
}
return $default_value;
}
/**
* Validate job ownership before allowing edit
*
* @param array $errors Validation errors.
* @param array $values Form values.
* @return array Modified errors.
*/
public static function validate_job_ownership( $errors, $values ) {
// Only validate for the job edit form
if ( absint( $values['form_id'] ) !== self::get_job_edit_form_id() ) {
return $errors;
}
// Check if job_id parameter exists
if ( ! isset( $_GET['job_id'] ) ) {
$errors[''] = 'Keine Stellenangebot-ID angegeben.';
return $errors;
}
$job_id = absint( $_GET['job_id'] );
// Verify post exists and is a job_offer
$post = get_post( $job_id );
if ( ! $post || 'job_offer' !== $post->post_type ) {
$errors[''] = 'Ungültige Stellenangebot-ID.';
return $errors;
}
// Verify post author matches current user
$current_user_id = get_current_user_id();
if ( absint( $post->post_author ) !== $current_user_id ) {
$errors[''] = 'Sie haben keine Berechtigung, dieses Stellenangebot zu bearbeiten.';
return $errors;
}
return $errors;
}
/**
* Create the job deactivation form programmatically if it doesn't exist
*/
public static function create_job_deactivation_form() {
// Check if Formidable is active
if ( ! class_exists( 'FrmForm' ) || ! class_exists( 'FrmFormActionsController' ) ) {
return;
}
// Check if form already exists
$existing_form = FrmForm::getOne( 'job_deactivation' );
if ( $existing_form ) {
self::$job_deactivation_form_id = $existing_form->id;
return;
}
// Create form
$form_values = array(
'name' => 'Stellenangebot deaktivieren',
'form_key' => 'job_deactivation',
'description' => '',
'status' => 'published',
'options' => array(
'submit_value' => 'Stellenangebot deaktivieren',
'success_msg' => 'Ihr Stellenangebot wurde deaktiviert.',
'success_action' => 'redirect',
'success_url' => home_url( '/anbieter-dashboard/' ),
),
);
$form_id = FrmForm::create( $form_values );
if ( ! $form_id ) {
return;
}
self::$job_deactivation_form_id = $form_id;
// Create form fields
$fields_data = array(
array(
'name' => 'Grund für Deaktivierung',
'description' => 'Bitte geben Sie an, warum Sie dieses Stellenangebot deaktivieren möchten',
'field_key' => 'deactivation_reason',
'type' => 'textarea',
'required' => '1',
'form_id' => $form_id,
'field_order' => 1,
),
array(
'name' => 'Job ID',
'field_key' => 'job_id',
'type' => 'hidden',
'required' => '0',
'form_id' => $form_id,
'field_order' => 2,
),
);
// Store field IDs for form action mapping
$field_ids = array();
foreach ( $fields_data as $field ) {
$field_id = FrmField::create( $field );
if ( $field_id ) {
$field_ids[ $field['field_key'] ] = $field_id;
}
}
// Create the Update Post action
if ( ! empty( $field_ids ) ) {
$action_values = array(
'menu_order' => 1,
'post_status' => 'published',
'post_content' => array(
'post_type' => 'job_offer',
'post_status' => 'draft',
'post_id' => 'id_param',
'post_custom_fields' => array(
array(
'meta_name' => 'job_deactivation_reason',
'field_id' => $field_ids['deactivation_reason'],
),
),
),
);
// Create the form action using the proper API
FrmFormActionsController::create_action(
$form_id,
array(
'post_excerpt' => 'wppost',
'post_content' => $action_values,
'menu_order' => 1,
)
);
}
}
/**
* Validate job ownership before allowing deactivation
*
* @param array $errors Validation errors.
* @param array $values Form values.
* @return array Modified errors.
*/
public static function validate_job_deactivation_ownership( $errors, $values ) {
// Only validate for the job deactivation form
if ( absint( $values['form_id'] ) !== self::get_job_deactivation_form_id() ) {
return $errors;
}
// Check if job_id parameter exists
if ( ! isset( $_GET['job_id'] ) ) {
$errors[''] = 'Keine Stellenangebot-ID angegeben.';
return $errors;
}
$job_id = absint( $_GET['job_id'] );
// Verify post exists and is a job_offer
$post = get_post( $job_id );
if ( ! $post || 'job_offer' !== $post->post_type ) {
$errors[''] = 'Ungültige Stellenangebot-ID.';
return $errors;
}
// Verify post author matches current user
$current_user_id = get_current_user_id();
if ( absint( $post->post_author ) !== $current_user_id ) {
$errors[''] = 'Sie haben keine Berechtigung, dieses Stellenangebot zu deaktivieren.';
return $errors;
}
return $errors;
}
/**
* Create the job application form programmatically if it doesn't exist
*/
public static function create_job_application_form() {
// Check if Formidable is active
if ( ! class_exists( 'FrmForm' ) ) {
return;
}
// Check if form already exists
$existing_form = FrmForm::getOne( 'job_application' );
if ( $existing_form ) {
self::$job_application_form_id = $existing_form->id;
return;
}
// Create form
$form_values = array(
'name' => 'Jetzt bewerben',
'form_key' => 'job_application',
'description' => '',
'status' => 'published',
'options' => array(
'submit_value' => 'Bewerbung absenden',
'success_msg' => 'Ihre Bewerbung wurde versendet. Der Anbieter wird sich bei Ihnen melden.',
),
);
$form_id = FrmForm::create( $form_values );
if ( ! $form_id ) {
return;
}
self::$job_application_form_id = $form_id;
// Get current user email if logged in
$current_user = wp_get_current_user();
$default_email = '';
if ( $current_user && $current_user->ID ) {
$default_email = $current_user->user_email;
}
// Create form fields
$fields_data = array(
array(
'name' => 'Name',
'field_key' => 'applicant_name',
'type' => 'text',
'required' => '1',
'form_id' => $form_id,
'field_order' => 1,
),
array(
'name' => 'E-Mail',
'field_key' => 'applicant_email',
'type' => 'email',
'required' => '1',
'form_id' => $form_id,
'field_order' => 2,
'default_value' => $default_email,
),
array(
'name' => 'Nachricht',
'description' => 'Warum interessieren Sie sich für diese Stelle?',
'field_key' => 'applicant_message',
'type' => 'textarea',
'required' => '1',
'form_id' => $form_id,
'field_order' => 3,
),
array(
'name' => 'Job ID',
'field_key' => 'job_id',
'type' => 'hidden',
'required' => '0',
'form_id' => $form_id,
'field_order' => 4,
),
);
foreach ( $fields_data as $field ) {
FrmField::create( $field );
}
}
}