Files
igny8/igy8-wp-plugin/sync/igny8-to-wp.php
alorig 8296685fbd asd
2025-11-22 19:46:34 +05:00

808 lines
27 KiB
PHP

<?php
/**
* IGNY8 → WordPress Synchronization
*
* Handles creating WordPress posts from IGNY8 content
*
* @package Igny8Bridge
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
/**
* Determine WordPress post type for IGNY8 task
*
* @param array $content_data Task data
* @return string
*/
function igny8_resolve_post_type_for_task($content_data) {
$content_type = $content_data['content_type'] ?? $content_data['post_type'] ?? 'post';
$post_type_map = array(
'post' => 'post',
'page' => 'page',
'product' => 'product',
'article' => 'post',
'blog' => 'post'
);
$post_type = isset($post_type_map[$content_type]) ? $post_type_map[$content_type] : $content_type;
$post_type = apply_filters('igny8_post_type_for_task', $post_type, $content_data);
if (!post_type_exists($post_type)) {
$post_type = 'post';
}
return $post_type;
}
/**
* Cache writer brief for a task
*
* @param int $task_id IGNY8 task ID
* @param int $post_id WordPress post ID
* @param Igny8API|null $api Optional API client
*/
function igny8_cache_task_brief($task_id, $post_id, $api = null) {
if (!$task_id || !$post_id) {
return;
}
$api = $api ?: new Igny8API();
if (!$api->is_authenticated()) {
return;
}
$response = $api->get("/writer/tasks/{$task_id}/brief/");
if ($response && !empty($response['success']) && !empty($response['data'])) {
update_post_meta($post_id, '_igny8_task_brief', $response['data']);
update_post_meta($post_id, '_igny8_brief_cached_at', current_time('mysql'));
}
}
/**
* Create WordPress post from IGNY8 task/content
*
* @param array $content_data Content data from IGNY8
* @param array $allowed_post_types Post types allowed to be created automatically
* @return int|WP_Error WordPress post ID or error
*/
function igny8_create_wordpress_post_from_task($content_data, $allowed_post_types = array()) {
$api = new Igny8API();
if (!$api->is_authenticated()) {
return new WP_Error('igny8_not_authenticated', 'IGNY8 API not authenticated');
}
$post_type = igny8_resolve_post_type_for_task($content_data);
if (!empty($allowed_post_types) && !in_array($post_type, $allowed_post_types, true)) {
return new WP_Error('igny8_post_type_disabled', sprintf('Post type %s is disabled for automation', $post_type));
}
// Prepare post data
$post_data = array(
'post_title' => $content_data['title'] ?? 'Untitled',
'post_content' => $content_data['content'] ?? '',
'post_status' => igny8_map_igny8_status_to_wp($content_data['status'] ?? 'draft'),
'post_type' => $post_type,
'meta_input' => array()
);
// Add IGNY8 meta
if (!empty($content_data['task_id'])) {
$post_data['meta_input']['_igny8_task_id'] = $content_data['task_id'];
}
if (!empty($content_data['content_id'])) {
$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'];
}
if (!empty($content_data['sector_id'])) {
$post_data['meta_input']['_igny8_sector_id'] = $content_data['sector_id'];
}
if (!empty($content_data['keyword_ids'])) {
$post_data['meta_input']['_igny8_keyword_ids'] = $content_data['keyword_ids'];
}
// Create post
$post_id = wp_insert_post($post_data);
if (is_wp_error($post_id)) {
error_log("IGNY8: Failed to create WordPress post: " . $post_id->get_error_message());
return $post_id;
}
// Assign taxonomies if cluster/sector IDs exist
if (!empty($content_data['cluster_id'])) {
// Find cluster term
$cluster_terms = get_terms(array(
'taxonomy' => 'igny8_clusters',
'meta_key' => '_igny8_cluster_id',
'meta_value' => $content_data['cluster_id'],
'hide_empty' => false
));
if (!is_wp_error($cluster_terms) && !empty($cluster_terms)) {
wp_set_post_terms($post_id, array($cluster_terms[0]->term_id), 'igny8_clusters');
}
}
if (!empty($content_data['sector_id'])) {
// Find sector term
$sector_terms = get_terms(array(
'taxonomy' => 'igny8_sectors',
'meta_key' => '_igny8_sector_id',
'meta_value' => $content_data['sector_id'],
'hide_empty' => false
));
if (!is_wp_error($sector_terms) && !empty($sector_terms)) {
wp_set_post_terms($post_id, array($sector_terms[0]->term_id), 'igny8_sectors');
}
}
// Handle categories
if (!empty($content_data['categories'])) {
$category_ids = igny8_process_categories($content_data['categories'], $post_id);
if (!empty($category_ids)) {
wp_set_post_terms($post_id, $category_ids, 'category');
}
}
// Handle tags
if (!empty($content_data['tags'])) {
$tag_ids = igny8_process_tags($content_data['tags'], $post_id);
if (!empty($tag_ids)) {
wp_set_post_terms($post_id, $tag_ids, 'post_tag');
}
}
// Handle featured image
if (!empty($content_data['featured_image'])) {
igny8_set_featured_image($post_id, $content_data['featured_image']);
}
// Handle image gallery (1-5 images)
if (!empty($content_data['gallery_images'])) {
igny8_set_image_gallery($post_id, $content_data['gallery_images']);
}
// Handle meta title and meta description (SEO)
if (!empty($content_data['meta_title'])) {
update_post_meta($post_id, '_yoast_wpseo_title', $content_data['meta_title']);
update_post_meta($post_id, '_seopress_titles_title', $content_data['meta_title']);
update_post_meta($post_id, '_aioseo_title', $content_data['meta_title']);
// Generic meta
update_post_meta($post_id, '_igny8_meta_title', $content_data['meta_title']);
}
if (!empty($content_data['meta_description'])) {
update_post_meta($post_id, '_yoast_wpseo_metadesc', $content_data['meta_description']);
update_post_meta($post_id, '_seopress_titles_desc', $content_data['meta_description']);
update_post_meta($post_id, '_aioseo_description', $content_data['meta_description']);
// Generic meta
update_post_meta($post_id, '_igny8_meta_description', $content_data['meta_description']);
}
// 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);
// 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']) {
error_log("IGNY8: Updated task {$content_data['task_id']} with WordPress post {$post_id} (status: {$wp_status})");
} else {
error_log("IGNY8: Failed to update task: " . ($response['error'] ?? 'Unknown error'));
}
}
// 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']);
}
return $post_id;
}
/**
* Map IGNY8 task status to WordPress post status
*
* @param string $igny8_status IGNY8 task status
* @return string WordPress post status
*/
function igny8_map_igny8_status_to_wp($igny8_status) {
$status_map = array(
'completed' => 'publish',
'draft' => 'draft',
'pending' => 'pending',
'scheduled' => 'future',
'archived' => 'trash'
);
return isset($status_map[$igny8_status]) ? $status_map[$igny8_status] : 'draft';
}
/**
* Sync IGNY8 tasks to WordPress posts
* Fetches tasks from IGNY8 and creates/updates WordPress posts
*
* @param array $filters Optional filters (status, cluster_id, etc.)
* @return array Sync results
*/
function igny8_sync_igny8_tasks_to_wp($filters = array()) {
// Skip if connection is disabled
if (!igny8_is_connection_enabled()) {
return array('success' => false, 'error' => 'Connection disabled', 'disabled' => true);
}
$api = new Igny8API();
if (!$api->is_authenticated()) {
return array('success' => false, 'error' => 'Not authenticated');
}
if (function_exists('igny8_is_module_enabled') && !igny8_is_module_enabled('writer')) {
return array('success' => true, 'created' => 0, 'updated' => 0, 'failed' => 0, 'skipped' => 0, 'total' => 0, 'disabled' => true);
}
$site_id = get_option('igny8_site_id');
if (!$site_id) {
return array('success' => false, 'error' => 'Site ID not configured');
}
$enabled_post_types = function_exists('igny8_get_enabled_post_types') ? igny8_get_enabled_post_types() : array('post', 'page');
// Build endpoint with filters
$endpoint = '/writer/tasks/';
$query_params = array();
$query_params[] = 'site_id=' . intval($site_id);
if (!empty($filters['status'])) {
$query_params[] = 'status=' . urlencode($filters['status']);
}
if (!empty($filters['cluster_id'])) {
$query_params[] = 'cluster_id=' . intval($filters['cluster_id']);
}
if (!empty($query_params)) {
$endpoint .= '?' . implode('&', $query_params);
}
// Get tasks from IGNY8
$response = $api->get($endpoint);
if (!$response['success']) {
return array('success' => false, 'error' => $response['error'] ?? 'Unknown error');
}
$tasks = $response['data']['results'] ?? $response['data'] ?? $response['results'] ?? array();
$created = 0;
$updated = 0;
$failed = 0;
$skipped = 0;
foreach ($tasks as $task) {
// Check if post already exists
$existing_posts = get_posts(array(
'meta_key' => '_igny8_task_id',
'meta_value' => $task['id'],
'post_type' => 'any',
'posts_per_page' => 1
));
if (!empty($existing_posts)) {
// Update existing post
$post_id = $existing_posts[0]->ID;
$update_data = array(
'ID' => $post_id,
'post_title' => $task['title'] ?? get_the_title($post_id),
'post_status' => igny8_map_igny8_status_to_wp($task['status'] ?? 'draft')
);
if (!empty($task['content'])) {
$update_data['post_content'] = $task['content'];
}
$result = wp_update_post($update_data);
// Update categories, tags, images, and meta
if ($result && !is_wp_error($result)) {
// Update categories
if (!empty($task['categories'])) {
$category_ids = igny8_process_categories($task['categories'], $post_id);
if (!empty($category_ids)) {
wp_set_post_terms($post_id, $category_ids, 'category');
}
}
// Update tags
if (!empty($task['tags'])) {
$tag_ids = igny8_process_tags($task['tags'], $post_id);
if (!empty($tag_ids)) {
wp_set_post_terms($post_id, $tag_ids, 'post_tag');
}
}
// Update featured image
if (!empty($task['featured_image']) || !empty($task['featured_media'])) {
igny8_set_featured_image($post_id, $task['featured_image'] ?? $task['featured_media']);
}
// Update gallery
if (!empty($task['gallery_images']) || !empty($task['images'])) {
igny8_set_image_gallery($post_id, $task['gallery_images'] ?? $task['images']);
}
// Update meta title and description
if (!empty($task['meta_title']) || !empty($task['seo_title'])) {
$meta_title = $task['meta_title'] ?? $task['seo_title'];
update_post_meta($post_id, '_yoast_wpseo_title', $meta_title);
update_post_meta($post_id, '_seopress_titles_title', $meta_title);
update_post_meta($post_id, '_aioseo_title', $meta_title);
update_post_meta($post_id, '_igny8_meta_title', $meta_title);
}
if (!empty($task['meta_description']) || !empty($task['seo_description'])) {
$meta_desc = $task['meta_description'] ?? $task['seo_description'];
update_post_meta($post_id, '_yoast_wpseo_metadesc', $meta_desc);
update_post_meta($post_id, '_seopress_titles_desc', $meta_desc);
update_post_meta($post_id, '_aioseo_description', $meta_desc);
update_post_meta($post_id, '_igny8_meta_description', $meta_desc);
}
}
if ($result && !is_wp_error($result)) {
igny8_cache_task_brief($task['id'], $post_id, $api);
$updated++;
} else {
$failed++;
}
} else {
// Create new post
$task_post_type = igny8_resolve_post_type_for_task($task);
if (!empty($enabled_post_types) && !in_array($task_post_type, $enabled_post_types, true)) {
$skipped++;
continue;
}
$content_data = array(
'task_id' => $task['id'],
'title' => $task['title'] ?? 'Untitled',
'content' => $task['content'] ?? '',
'status' => $task['status'] ?? 'draft',
'cluster_id' => $task['cluster_id'] ?? null,
'sector_id' => $task['sector_id'] ?? null,
'keyword_ids' => $task['keyword_ids'] ?? array(),
'content_type' => $task['content_type'] ?? $task['post_type'] ?? 'post',
'post_type' => $task['post_type'] ?? null, // Keep for backward compatibility
'categories' => $task['categories'] ?? array(),
'tags' => $task['tags'] ?? array(),
'featured_image' => $task['featured_image'] ?? $task['featured_media'] ?? null,
'gallery_images' => $task['gallery_images'] ?? $task['images'] ?? array(),
'meta_title' => $task['meta_title'] ?? $task['seo_title'] ?? null,
'meta_description' => $task['meta_description'] ?? $task['seo_description'] ?? null
);
$post_id = igny8_create_wordpress_post_from_task($content_data, $enabled_post_types);
if (is_wp_error($post_id)) {
if ($post_id->get_error_code() === 'igny8_post_type_disabled') {
$skipped++;
} else {
$failed++;
}
} elseif ($post_id) {
igny8_cache_task_brief($task['id'], $post_id, $api);
$created++;
} else {
$failed++;
}
}
}
return array(
'success' => true,
'created' => $created,
'updated' => $updated,
'failed' => $failed,
'skipped' => $skipped,
'total' => count($tasks)
);
}
/**
* Handle webhook from IGNY8 (when content is published from IGNY8)
* This can be called via REST API endpoint or scheduled sync
*
* @param array $webhook_data Webhook data from IGNY8
* @return int|false WordPress post ID or false on failure
*/
function igny8_handle_igny8_webhook($webhook_data) {
if (empty($webhook_data['task_id'])) {
return false;
}
$api = new Igny8API();
// Get full task data from IGNY8
$task_response = $api->get("/writer/tasks/{$webhook_data['task_id']}/");
if (!$task_response['success']) {
return false;
}
$task = $task_response['data'];
// Prepare content data
$content_data = array(
'task_id' => $task['id'],
'content_id' => $task['content_id'] ?? null,
'title' => $task['title'] ?? 'Untitled',
'content' => $task['content'] ?? '',
'status' => $task['status'] ?? 'draft',
'cluster_id' => $task['cluster_id'] ?? null,
'sector_id' => $task['sector_id'] ?? null,
'keyword_ids' => $task['keyword_ids'] ?? array(),
'post_type' => $task['post_type'] ?? 'post'
);
return igny8_create_wordpress_post_from_task($content_data);
}
/**
* Process categories from IGNY8
*
* @param array $categories Category data (IDs, names, or slugs)
* @param int $post_id Post ID
* @return array Category term IDs
*/
function igny8_process_categories($categories, $post_id) {
$category_ids = array();
foreach ($categories as $category) {
$term_id = null;
// If it's an ID
if (is_numeric($category)) {
$term = get_term($category, 'category');
if ($term && !is_wp_error($term)) {
$term_id = $term->term_id;
}
}
// If it's an array with name/slug
elseif (is_array($category)) {
$name = $category['name'] ?? $category['slug'] ?? null;
$slug = $category['slug'] ?? sanitize_title($name);
if ($name) {
// Try to find existing term
$term = get_term_by('slug', $slug, 'category');
if (!$term) {
$term = get_term_by('name', $name, 'category');
}
// Create if doesn't exist
if (!$term || is_wp_error($term)) {
$term_result = wp_insert_term($name, 'category', array('slug' => $slug));
if (!is_wp_error($term_result)) {
$term_id = $term_result['term_id'];
}
} else {
$term_id = $term->term_id;
}
}
}
// If it's a string (name or slug)
elseif (is_string($category)) {
$term = get_term_by('slug', $category, 'category');
if (!$term) {
$term = get_term_by('name', $category, 'category');
}
if ($term && !is_wp_error($term)) {
$term_id = $term->term_id;
} else {
// Create new category
$term_result = wp_insert_term($category, 'category');
if (!is_wp_error($term_result)) {
$term_id = $term_result['term_id'];
}
}
}
if ($term_id) {
$category_ids[] = $term_id;
}
}
return array_unique($category_ids);
}
/**
* Process tags from IGNY8
*
* @param array $tags Tag data (IDs, names, or slugs)
* @param int $post_id Post ID
* @return array Tag term IDs
*/
function igny8_process_tags($tags, $post_id) {
$tag_ids = array();
foreach ($tags as $tag) {
$term_id = null;
// If it's an ID
if (is_numeric($tag)) {
$term = get_term($tag, 'post_tag');
if ($term && !is_wp_error($term)) {
$term_id = $term->term_id;
}
}
// If it's an array with name/slug
elseif (is_array($tag)) {
$name = $tag['name'] ?? $tag['slug'] ?? null;
$slug = $tag['slug'] ?? sanitize_title($name);
if ($name) {
// Try to find existing term
$term = get_term_by('slug', $slug, 'post_tag');
if (!$term) {
$term = get_term_by('name', $name, 'post_tag');
}
// Create if doesn't exist
if (!$term || is_wp_error($term)) {
$term_result = wp_insert_term($name, 'post_tag', array('slug' => $slug));
if (!is_wp_error($term_result)) {
$term_id = $term_result['term_id'];
}
} else {
$term_id = $term->term_id;
}
}
}
// If it's a string (name or slug)
elseif (is_string($tag)) {
$term = get_term_by('slug', $tag, 'post_tag');
if (!$term) {
$term = get_term_by('name', $tag, 'post_tag');
}
if ($term && !is_wp_error($term)) {
$term_id = $term->term_id;
} else {
// Create new tag
$term_result = wp_insert_term($tag, 'post_tag');
if (!is_wp_error($term_result)) {
$term_id = $term_result['term_id'];
}
}
}
if ($term_id) {
$tag_ids[] = $term_id;
}
}
return array_unique($tag_ids);
}
/**
* Set featured image for post
*
* @param int $post_id Post ID
* @param string|array $image_data Image URL or array with image data
* @return int|false Attachment ID or false on failure
*/
function igny8_set_featured_image($post_id, $image_data) {
$image_url = is_array($image_data) ? ($image_data['url'] ?? $image_data['src'] ?? '') : $image_data;
if (empty($image_url)) {
return false;
}
// Check if image already exists
$attachment_id = igny8_get_attachment_by_url($image_url);
if (!$attachment_id) {
// Download and attach image
$attachment_id = igny8_import_image($image_url, $post_id);
}
if ($attachment_id) {
set_post_thumbnail($post_id, $attachment_id);
return $attachment_id;
}
return false;
}
/**
* Set image gallery for post (1-5 images)
*
* @param int $post_id Post ID
* @param array $gallery_images Array of image URLs or image data
* @return array Attachment IDs
*/
function igny8_set_image_gallery($post_id, $gallery_images) {
$attachment_ids = array();
// Limit to 5 images
$gallery_images = array_slice($gallery_images, 0, 5);
foreach ($gallery_images as $image_data) {
$image_url = is_array($image_data) ? ($image_data['url'] ?? $image_data['src'] ?? '') : $image_data;
if (empty($image_url)) {
continue;
}
// Check if image already exists
$attachment_id = igny8_get_attachment_by_url($image_url);
if (!$attachment_id) {
// Download and attach image
$attachment_id = igny8_import_image($image_url, $post_id);
}
if ($attachment_id) {
$attachment_ids[] = $attachment_id;
}
}
// Store gallery as post meta (WordPress native)
if (!empty($attachment_ids)) {
update_post_meta($post_id, '_igny8_gallery_images', $attachment_ids);
// Also store in format compatible with plugins
update_post_meta($post_id, '_product_image_gallery', implode(',', $attachment_ids)); // WooCommerce
update_post_meta($post_id, '_gallery_images', $attachment_ids); // Generic
}
return $attachment_ids;
}
/**
* Get attachment ID by image URL
*
* @param string $image_url Image URL
* @return int|false Attachment ID or false
*/
function igny8_get_attachment_by_url($image_url) {
global $wpdb;
$attachment_id = $wpdb->get_var($wpdb->prepare(
"SELECT ID FROM {$wpdb->posts} WHERE guid = %s AND post_type = 'attachment'",
$image_url
));
return $attachment_id ? intval($attachment_id) : false;
}
/**
* Import image from URL and attach to post
*
* @param string $image_url Image URL
* @param int $post_id Post ID to attach to
* @return int|false Attachment ID or false on failure
*/
function igny8_import_image($image_url, $post_id) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
// Download image
$tmp = download_url($image_url);
if (is_wp_error($tmp)) {
error_log("IGNY8: Failed to download image {$image_url}: " . $tmp->get_error_message());
return false;
}
// Get file extension
$file_array = array(
'name' => basename(parse_url($image_url, PHP_URL_PATH)),
'tmp_name' => $tmp
);
// Upload to WordPress media library
$attachment_id = media_handle_sideload($file_array, $post_id);
// Clean up temp file
@unlink($tmp);
if (is_wp_error($attachment_id)) {
error_log("IGNY8: Failed to import image {$image_url}: " . $attachment_id->get_error_message());
return false;
}
return $attachment_id;
}
/**
* Scheduled sync from IGNY8 to WordPress
* Fetches new/updated tasks from IGNY8 and creates/updates WordPress posts
*/
function igny8_cron_sync_from_igny8() {
// Skip if connection is disabled
if (!igny8_is_connection_enabled()) {
error_log('IGNY8: Connection disabled, skipping sync from IGNY8');
return;
}
$site_id = get_option('igny8_site_id');
if (!$site_id) {
error_log('IGNY8: Site ID not set, skipping sync from IGNY8');
return;
}
if (function_exists('igny8_is_module_enabled') && !igny8_is_module_enabled('writer')) {
error_log('IGNY8: Writer module disabled, skipping sync from IGNY8');
return;
}
// Get last sync time
$last_sync = get_option('igny8_last_sync_from_igny8', 0);
// Sync only completed/published tasks
$filters = array(
'status' => 'completed'
);
// If we have a last sync time, we could filter by updated date
// For now, sync all completed tasks (API should handle deduplication)
$result = igny8_sync_igny8_tasks_to_wp($filters);
if ($result['success']) {
update_option('igny8_last_sync_from_igny8', time());
update_option('igny8_last_writer_sync', current_time('timestamp'));
error_log(sprintf(
'IGNY8: Synced from IGNY8 - Created %d posts, updated %d posts, %d failed, %d skipped',
$result['created'],
$result['updated'],
$result['failed'],
$result['skipped'] ?? 0
));
} else {
error_log('IGNY8: Failed to sync from IGNY8: ' . ($result['error'] ?? 'Unknown error'));
}
}