Files
Digital-Dabei-Hamburg-Job-M…/migrations/completed/2026-01-15-migrate-job-entries.php
Viktor Miller 737f3d6fe9 feat(07-01): implement complete job management workflow
Adds comprehensive job submission, editing, and deactivation functionality with proper form handling and permissions. Includes administrator capabilities for job_offer management and fixed dashboard navigation.

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

194 lines
4.9 KiB
PHP

<?php
/**
* One-time migration script to convert Formidable job submission entries to job_offer posts
*
* Run this file once via: wp eval-file migrate-job-entries.php
*
* @package DDHH_Job_Manager
*/
// Make sure we're running in WordPress context
if ( ! defined( 'ABSPATH' ) ) {
// Try to load WordPress
$wp_load = dirname( __FILE__ ) . '/../../../wp-load.php';
if ( file_exists( $wp_load ) ) {
require_once $wp_load;
} else {
die( 'Could not find WordPress. Please run via WP-CLI: wp eval-file migrate-job-entries.php' );
}
}
// Check if Formidable is available
if ( ! class_exists( 'FrmForm' ) || ! class_exists( 'FrmEntry' ) || ! class_exists( 'FrmField' ) ) {
die( "Error: Formidable Forms is not active.\n" );
}
// Get the job submission form
$form = FrmForm::getOne( 'job_submission' );
if ( ! $form ) {
die( "Error: Job submission form not found.\n" );
}
$form_id = $form->id;
echo "Found job submission form with ID: {$form_id}\n";
// Get all entries for this form using global $wpdb
global $wpdb;
$entry_table = $wpdb->prefix . 'frm_items';
$entry_ids = $wpdb->get_col( $wpdb->prepare(
"SELECT id FROM {$entry_table} WHERE form_id = %d ORDER BY created_at ASC",
$form_id
) );
if ( empty( $entry_ids ) ) {
die( "No entries found to migrate.\n" );
}
// Load full entry objects
$entries = array();
foreach ( $entry_ids as $entry_id ) {
$entry = FrmEntry::getOne( $entry_id );
if ( $entry ) {
$entries[] = $entry;
}
}
echo "Found " . count( $entries ) . " entries to migrate.\n\n";
$migrated = 0;
$skipped = 0;
$errors = 0;
foreach ( $entries as $entry ) {
echo "Processing entry #{$entry->id}...\n";
// Get full entry with meta data
$full_entry = FrmEntry::getOne( $entry->id, true );
if ( ! $full_entry ) {
echo " ERROR: Could not load entry data\n";
$errors++;
continue;
}
// Check if this entry has already been migrated by checking if a post exists
// We'll check if there's a post with the same author and created around the same time
$entry_user_id = absint( $entry->user_id );
if ( empty( $entry_user_id ) ) {
echo " SKIPPED: Entry has no user_id\n";
$skipped++;
continue;
}
// Extract field values
$job_title = '';
$job_description = '';
$job_location = '';
$job_type = '';
$job_deadline = '';
$job_contact_email = '';
$job_logo = '';
foreach ( $full_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 ) ) {
echo " SKIPPED: Missing job title\n";
$skipped++;
continue;
}
// Check if a post with this title and author already exists
$existing_post = get_posts( array(
'post_type' => 'job_offer',
'post_status' => 'any',
'author' => $entry_user_id,
'title' => $job_title,
'numberposts' => 1,
) );
if ( ! empty( $existing_post ) ) {
echo " SKIPPED: Post already exists (ID: {$existing_post[0]->ID})\n";
$skipped++;
continue;
}
// Create job_offer post
$post_data = array(
'post_title' => $job_title,
'post_content' => $job_description,
'post_type' => 'job_offer',
'post_status' => 'pending',
'post_author' => $entry_user_id,
'post_date' => $entry->created_at,
);
$post_id = wp_insert_post( $post_data, true );
// Check for errors
if ( is_wp_error( $post_id ) ) {
echo " ERROR: Failed to create post - " . $post_id->get_error_message() . "\n";
$errors++;
continue;
}
// Save custom fields
if ( ! empty( $job_location ) ) {
update_post_meta( $post_id, 'job_location', $job_location );
}
if ( ! empty( $job_type ) ) {
update_post_meta( $post_id, 'job_type', $job_type );
}
if ( ! empty( $job_contact_email ) ) {
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 ) );
}
// Store reference to original entry
update_post_meta( $post_id, '_migrated_from_entry_id', $entry->id );
echo " SUCCESS: Created post #{$post_id} - \"{$job_title}\"\n";
$migrated++;
}
echo "\n=== Migration Complete ===\n";
echo "Migrated: {$migrated}\n";
echo "Skipped: {$skipped}\n";
echo "Errors: {$errors}\n";
echo "Total: " . count( $entries ) . "\n";