wp plugin refacotr
This commit is contained in:
@@ -105,6 +105,13 @@ class Igny8Admin {
|
||||
'sanitize_callback' => array($this, 'sanitize_modules'),
|
||||
'default' => array_keys(igny8_get_available_modules())
|
||||
));
|
||||
|
||||
// Default post status for IGNY8 published content
|
||||
register_setting('igny8_bridge_controls', 'igny8_default_post_status', array(
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => array($this, 'sanitize_post_status'),
|
||||
'default' => 'draft'
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -617,6 +624,17 @@ class Igny8Admin {
|
||||
|
||||
return !empty($clean) ? $clean : $supported;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize post status
|
||||
*
|
||||
* @param string $value Post status value
|
||||
* @return string Sanitized post status
|
||||
*/
|
||||
public function sanitize_post_status($value) {
|
||||
$allowed = array('draft', 'publish');
|
||||
return in_array($value, $allowed, true) ? $value : 'draft';
|
||||
}
|
||||
}
|
||||
|
||||
// Register AJAX handlers
|
||||
|
||||
@@ -58,6 +58,7 @@ $pending_links = array_filter($link_queue, function($item) {
|
||||
return $item['status'] === 'pending';
|
||||
});
|
||||
$webhook_logs = igny8_get_webhook_logs(array('limit' => 10));
|
||||
$default_post_status = get_option('igny8_default_post_status', 'draft');
|
||||
|
||||
?>
|
||||
|
||||
@@ -325,6 +326,34 @@ $webhook_logs = igny8_get_webhook_logs(array('limit' => 10));
|
||||
<span class="description"><?php _e('Allow editors to update content in WordPress and sync back to IGNY8.', 'igny8-bridge'); ?></span>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="automation-block">
|
||||
<h3><?php _e('Default Post Status for IGNY8 Content', 'igny8-bridge'); ?></h3>
|
||||
<label>
|
||||
<input
|
||||
type="radio"
|
||||
name="igny8_default_post_status"
|
||||
value="draft"
|
||||
<?php checked($default_post_status, 'draft'); ?>
|
||||
/>
|
||||
<strong><?php _e('Draft', 'igny8-bridge'); ?></strong>
|
||||
<span class="description"><?php _e('Save content as draft for review before publishing.', 'igny8-bridge'); ?></span>
|
||||
</label>
|
||||
<br />
|
||||
<label>
|
||||
<input
|
||||
type="radio"
|
||||
name="igny8_default_post_status"
|
||||
value="publish"
|
||||
<?php checked($default_post_status, 'publish'); ?>
|
||||
/>
|
||||
<strong><?php _e('Publish', 'igny8-bridge'); ?></strong>
|
||||
<span class="description"><?php _e('Publish content immediately when received from IGNY8.', 'igny8-bridge'); ?></span>
|
||||
</label>
|
||||
<p class="description" style="margin-top: 8px;">
|
||||
<?php _e('Choose whether content published from IGNY8 should be saved as draft or published immediately in WordPress.', 'igny8-bridge'); ?>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="automation-column-right">
|
||||
|
||||
@@ -594,20 +594,30 @@ class Igny8RestAPI {
|
||||
}
|
||||
|
||||
// Create WordPress post
|
||||
$post_id = igny8_create_wordpress_post_from_task($content_data);
|
||||
$result = igny8_create_wordpress_post_from_task($content_data);
|
||||
|
||||
if (is_wp_error($post_id)) {
|
||||
if (is_wp_error($result)) {
|
||||
return $this->build_unified_response(
|
||||
false,
|
||||
null,
|
||||
'Failed to create WordPress post: ' . $post_id->get_error_message(),
|
||||
'Failed to create WordPress post: ' . $result->get_error_message(),
|
||||
'post_creation_failed',
|
||||
null,
|
||||
500
|
||||
);
|
||||
}
|
||||
|
||||
// Return success response
|
||||
// Handle new return format (array with post_id and term_ids)
|
||||
if (is_array($result) && isset($result['post_id'])) {
|
||||
$post_id = $result['post_id'];
|
||||
$term_ids = $result['term_ids'] ?? array();
|
||||
} else {
|
||||
// Legacy format (just post_id)
|
||||
$post_id = $result;
|
||||
$term_ids = array();
|
||||
}
|
||||
|
||||
// Return success response with term_ids
|
||||
return $this->build_unified_response(
|
||||
true,
|
||||
array(
|
||||
@@ -615,7 +625,8 @@ class Igny8RestAPI {
|
||||
'post_url' => get_permalink($post_id),
|
||||
'post_status' => get_post_status($post_id),
|
||||
'content_id' => $content_id,
|
||||
'task_id' => $task_id
|
||||
'task_id' => $task_id,
|
||||
'term_ids' => $term_ids
|
||||
),
|
||||
'Content successfully published to WordPress',
|
||||
null,
|
||||
|
||||
@@ -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