wp plugin refacotr
This commit is contained in:
@@ -115,7 +115,7 @@ function igny8_create_wordpress_post_from_task($content_data, $allowed_post_type
|
||||
'post_title' => sanitize_text_field($content_data['title'] ?? 'Untitled'),
|
||||
'post_content' => wp_kses_post($content_html),
|
||||
'post_excerpt' => sanitize_text_field($excerpt),
|
||||
'post_status' => igny8_map_igny8_status_to_wp($content_data['status'] ?? 'draft'),
|
||||
'post_status' => get_option('igny8_default_post_status', 'draft'),
|
||||
'post_type' => $post_type,
|
||||
'post_author' => $author_id,
|
||||
'meta_input' => array()
|
||||
@@ -136,10 +136,6 @@ function igny8_create_wordpress_post_from_task($content_data, $allowed_post_type
|
||||
$post_data['meta_input']['_igny8_content_id'] = $content_data['content_id'];
|
||||
}
|
||||
|
||||
if (!empty($content_data['cluster_id'])) {
|
||||
$post_data['meta_input']['_igny8_cluster_id'] = $content_data['cluster_id'];
|
||||
}
|
||||
|
||||
// Stage 1: New meta fields
|
||||
if (!empty($content_data['content_type'])) {
|
||||
$post_data['meta_input']['_igny8_content_type'] = $content_data['content_type'];
|
||||
@@ -154,12 +150,19 @@ function igny8_create_wordpress_post_from_task($content_data, $allowed_post_type
|
||||
$post_data['meta_input']['_igny8_source'] = $content_data['source'];
|
||||
}
|
||||
|
||||
if (!empty($content_data['sector_id'])) {
|
||||
$post_data['meta_input']['_igny8_sector_id'] = $content_data['sector_id'];
|
||||
// Store keywords as custom fields
|
||||
if (!empty($content_data['primary_keyword'])) {
|
||||
$post_data['meta_input']['_igny8_primary_keyword'] = $content_data['primary_keyword'];
|
||||
}
|
||||
|
||||
if (!empty($content_data['keyword_ids'])) {
|
||||
$post_data['meta_input']['_igny8_keyword_ids'] = $content_data['keyword_ids'];
|
||||
if (!empty($content_data['secondary_keywords'])) {
|
||||
// Store as JSON for editing in meta box
|
||||
$keywords = is_array($content_data['secondary_keywords'])
|
||||
? $content_data['secondary_keywords']
|
||||
: json_decode($content_data['secondary_keywords'], true);
|
||||
if (is_array($keywords)) {
|
||||
$post_data['meta_input']['_igny8_secondary_keywords'] = json_encode($keywords);
|
||||
}
|
||||
}
|
||||
|
||||
// Create post
|
||||
@@ -182,9 +185,11 @@ function igny8_create_wordpress_post_from_task($content_data, $allowed_post_type
|
||||
// Import and process content images
|
||||
igny8_import_content_images($post_id, $content_html);
|
||||
|
||||
// Assign taxonomies if cluster/sector IDs exist
|
||||
// Assign cluster taxonomy (create term if doesn't exist)
|
||||
if (!empty($content_data['cluster_id'])) {
|
||||
// Find cluster term
|
||||
$cluster_name = $content_data['cluster_name'] ?? 'Cluster ' . $content_data['cluster_id'];
|
||||
|
||||
// Try to find existing term by IGNY8 cluster_id
|
||||
$cluster_terms = get_terms(array(
|
||||
'taxonomy' => 'igny8_clusters',
|
||||
'meta_key' => '_igny8_cluster_id',
|
||||
@@ -194,11 +199,28 @@ function igny8_create_wordpress_post_from_task($content_data, $allowed_post_type
|
||||
|
||||
if (!is_wp_error($cluster_terms) && !empty($cluster_terms)) {
|
||||
wp_set_post_terms($post_id, array($cluster_terms[0]->term_id), 'igny8_clusters');
|
||||
Igny8_Logger::info("{$log_prefix} ✅ Assigned existing cluster term: {$cluster_name}");
|
||||
} else {
|
||||
// Create new cluster term
|
||||
$term_result = wp_insert_term($cluster_name, 'igny8_clusters', array(
|
||||
'slug' => sanitize_title($cluster_name)
|
||||
));
|
||||
|
||||
if (!is_wp_error($term_result)) {
|
||||
update_term_meta($term_result['term_id'], '_igny8_cluster_id', $content_data['cluster_id']);
|
||||
wp_set_post_terms($post_id, array($term_result['term_id']), 'igny8_clusters');
|
||||
Igny8_Logger::info("{$log_prefix} ✅ Created and assigned new cluster term: {$cluster_name}");
|
||||
} else {
|
||||
Igny8_Logger::error("{$log_prefix} ❌ Failed to create cluster term: {$term_result->get_error_message()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Assign sector taxonomy (create term if doesn't exist)
|
||||
if (!empty($content_data['sector_id'])) {
|
||||
// Find sector term
|
||||
$sector_name = $content_data['sector_name'] ?? 'Sector ' . $content_data['sector_id'];
|
||||
|
||||
// Try to find existing term by IGNY8 sector_id
|
||||
$sector_terms = get_terms(array(
|
||||
'taxonomy' => 'igny8_sectors',
|
||||
'meta_key' => '_igny8_sector_id',
|
||||
@@ -208,6 +230,20 @@ function igny8_create_wordpress_post_from_task($content_data, $allowed_post_type
|
||||
|
||||
if (!is_wp_error($sector_terms) && !empty($sector_terms)) {
|
||||
wp_set_post_terms($post_id, array($sector_terms[0]->term_id), 'igny8_sectors');
|
||||
Igny8_Logger::info("{$log_prefix} ✅ Assigned existing sector term: {$sector_name}");
|
||||
} else {
|
||||
// Create new sector term
|
||||
$term_result = wp_insert_term($sector_name, 'igny8_sectors', array(
|
||||
'slug' => sanitize_title($sector_name)
|
||||
));
|
||||
|
||||
if (!is_wp_error($term_result)) {
|
||||
update_term_meta($term_result['term_id'], '_igny8_sector_id', $content_data['sector_id']);
|
||||
wp_set_post_terms($post_id, array($term_result['term_id']), 'igny8_sectors');
|
||||
Igny8_Logger::info("{$log_prefix} ✅ Created and assigned new sector term: {$sector_name}");
|
||||
} else {
|
||||
Igny8_Logger::error("{$log_prefix} ❌ Failed to create sector term: {$term_result->get_error_message()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -287,60 +323,57 @@ function igny8_create_wordpress_post_from_task($content_data, $allowed_post_type
|
||||
// Handle gallery images
|
||||
if (!empty($content_data['gallery_images'])) {
|
||||
Igny8_Logger::info("{$log_prefix} STEP: Setting gallery with " . count($content_data['gallery_images']) . " images");
|
||||
igny8_set_gallery_images($post_id, $content_data['gallery_images']);
|
||||
igny8_set_image_gallery($post_id, $content_data['gallery_images']);
|
||||
}
|
||||
|
||||
// Get the actual WordPress post status (after creation)
|
||||
$created_post = get_post($post_id);
|
||||
$wp_status = $created_post ? $created_post->post_status : 'draft';
|
||||
|
||||
// Store WordPress status in meta for IGNY8 to read
|
||||
update_post_meta($post_id, '_igny8_wordpress_status', $wp_status);
|
||||
// Collect term IDs for all taxonomies
|
||||
$term_ids = array(
|
||||
'categories' => array(),
|
||||
'tags' => array(),
|
||||
'igny8_clusters' => array(),
|
||||
'igny8_sectors' => array()
|
||||
);
|
||||
|
||||
// Map WordPress status back to IGNY8 status
|
||||
$igny8_status = igny8_map_wp_status_to_igny8($wp_status);
|
||||
|
||||
// Update IGNY8 task with WordPress post ID, URL, and status
|
||||
if (!empty($content_data['task_id'])) {
|
||||
$update_data = array(
|
||||
'assigned_post_id' => $post_id,
|
||||
'post_url' => get_permalink($post_id),
|
||||
'wordpress_status' => $wp_status, // WordPress actual status (publish/pending/draft)
|
||||
'status' => $igny8_status, // IGNY8 mapped status (completed/pending/draft)
|
||||
'synced_at' => current_time('mysql'),
|
||||
'post_type' => $post_type, // WordPress post type
|
||||
'content_type' => $content_type // IGNY8 content type
|
||||
);
|
||||
|
||||
// Include content_id if provided
|
||||
if (!empty($content_data['content_id'])) {
|
||||
$update_data['content_id'] = $content_data['content_id'];
|
||||
}
|
||||
|
||||
$response = $api->put("/writer/tasks/{$content_data['task_id']}/", $update_data);
|
||||
|
||||
if ($response['success']) {
|
||||
Igny8_Logger::info("{$log_prefix} ✅ Updated IGNY8 task {$content_data['task_id']} with WordPress post {$post_id} (status: {$wp_status})");
|
||||
} else {
|
||||
Igny8_Logger::error("{$log_prefix} ❌ Failed to update IGNY8 task: " . ($response['error'] ?? 'Unknown error'));
|
||||
}
|
||||
// Get assigned category IDs
|
||||
$category_terms = wp_get_post_terms($post_id, 'category', array('fields' => 'ids'));
|
||||
if (!is_wp_error($category_terms)) {
|
||||
$term_ids['categories'] = $category_terms;
|
||||
}
|
||||
|
||||
// Store content_id if provided (for IGNY8 to query)
|
||||
if (!empty($content_data['content_id'])) {
|
||||
update_post_meta($post_id, '_igny8_content_id', $content_data['content_id']);
|
||||
// Get assigned tag IDs
|
||||
$tag_terms = wp_get_post_terms($post_id, 'post_tag', array('fields' => 'ids'));
|
||||
if (!is_wp_error($tag_terms)) {
|
||||
$term_ids['tags'] = $tag_terms;
|
||||
}
|
||||
|
||||
// Send status webhook to IGNY8
|
||||
igny8_send_status_webhook($post_id, $content_data, $wp_status);
|
||||
// Get assigned cluster IDs
|
||||
$cluster_terms = wp_get_post_terms($post_id, 'igny8_clusters', array('fields' => 'ids'));
|
||||
if (!is_wp_error($cluster_terms)) {
|
||||
$term_ids['igny8_clusters'] = $cluster_terms;
|
||||
}
|
||||
|
||||
// Get assigned sector IDs
|
||||
$sector_terms = wp_get_post_terms($post_id, 'igny8_sectors', array('fields' => 'ids'));
|
||||
if (!is_wp_error($sector_terms)) {
|
||||
$term_ids['igny8_sectors'] = $sector_terms;
|
||||
}
|
||||
|
||||
Igny8_Logger::separator("{$log_prefix} 🎉 POST CREATION COMPLETED SUCCESSFULLY");
|
||||
Igny8_Logger::info("{$log_prefix} WordPress Post ID: {$post_id}");
|
||||
Igny8_Logger::info("{$log_prefix} Post URL: " . get_permalink($post_id));
|
||||
Igny8_Logger::info("{$log_prefix} Post Status: {$wp_status}");
|
||||
Igny8_Logger::info("{$log_prefix} Term IDs: " . json_encode($term_ids));
|
||||
Igny8_Logger::separator();
|
||||
|
||||
return $post_id;
|
||||
// Return array with post_id and term_ids for REST API response
|
||||
return array(
|
||||
'post_id' => $post_id,
|
||||
'term_ids' => $term_ids
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user