admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('igny8_post_editor_nonce'), )); } /** * Render Planner Brief meta box */ public function render_planner_brief_box($post) { $task_id = get_post_meta($post->ID, '_igny8_task_id', true); $brief = get_post_meta($post->ID, '_igny8_task_brief', true); $brief_cached_at = get_post_meta($post->ID, '_igny8_brief_cached_at', true); $cluster_id = get_post_meta($post->ID, '_igny8_cluster_id', true); if (!$task_id && !$cluster_id) { echo '

'; _e('This post is not linked to an IGNY8 task or cluster.', 'igny8-bridge'); echo '

'; return; } wp_nonce_field('igny8_post_editor_nonce', 'igny8_post_editor_nonce'); ?>

'; foreach ($keywords as $keyword) { echo '' . esc_html(trim($keyword)) . ''; } echo ''; ?>

ID, '_igny8_task_id', true); $optimizer_job_id = get_post_meta($post->ID, '_igny8_optimizer_job_id', true); $optimizer_status = get_post_meta($post->ID, '_igny8_optimizer_status', true); if (!$task_id) { echo '

'; _e('This post is not linked to an IGNY8 task.', 'igny8-bridge'); echo '

'; return; } wp_nonce_field('igny8_post_editor_nonce', 'igny8_post_editor_nonce'); ?>

'Unauthorized')); } if (!igny8_is_connection_enabled()) { wp_send_json_error(array('message' => 'Connection is disabled. Enable sync operations first.')); } $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; $task_id = isset($_POST['task_id']) ? intval($_POST['task_id']) : 0; if (!$post_id || !$task_id) { wp_send_json_error(array('message' => 'Invalid post ID or task ID')); } $api = new Igny8API(); if (!$api->is_authenticated()) { wp_send_json_error(array('message' => 'Not authenticated')); } // Try to fetch from Planner first $response = $api->get("/planner/tasks/{$task_id}/brief/"); if (!$response['success']) { // Fallback to Writer brief $response = $api->get("/writer/tasks/{$task_id}/brief/"); } if ($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')); wp_send_json_success(array( 'message' => 'Brief fetched successfully', 'brief' => $response['data'] )); } else { wp_send_json_error(array( 'message' => 'Failed to fetch brief: ' . ($response['error'] ?? 'Unknown error') )); } } /** * Refresh Planner task (AJAX handler) */ public static function refresh_planner_task() { check_ajax_referer('igny8_post_editor_nonce', 'nonce'); if (!current_user_can('edit_posts')) { wp_send_json_error(array('message' => 'Unauthorized')); } if (!igny8_is_connection_enabled()) { wp_send_json_error(array('message' => 'Connection is disabled. Enable sync operations first.')); } $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; $task_id = isset($_POST['task_id']) ? intval($_POST['task_id']) : 0; if (!$post_id || !$task_id) { wp_send_json_error(array('message' => 'Invalid post ID or task ID')); } $api = new Igny8API(); if (!$api->is_authenticated()) { wp_send_json_error(array('message' => 'Not authenticated')); } $response = $api->post("/planner/tasks/{$task_id}/refresh/", array( 'wordpress_post_id' => $post_id, 'reason' => 'reoptimize', 'notes' => 'Requested refresh from WordPress editor' )); if ($response['success']) { wp_send_json_success(array( 'message' => 'Refresh requested successfully', 'data' => $response['data'] )); } else { wp_send_json_error(array( 'message' => 'Failed to request refresh: ' . ($response['error'] ?? 'Unknown error') )); } } /** * Create Optimizer job (AJAX handler) */ public static function create_optimizer_job() { check_ajax_referer('igny8_post_editor_nonce', 'nonce'); if (!current_user_can('edit_posts')) { wp_send_json_error(array('message' => 'Unauthorized')); } if (!igny8_is_connection_enabled()) { wp_send_json_error(array('message' => 'Connection is disabled. Enable sync operations first.')); } $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; $task_id = isset($_POST['task_id']) ? intval($_POST['task_id']) : 0; $job_type = isset($_POST['job_type']) ? sanitize_text_field($_POST['job_type']) : 'audit'; $priority = isset($_POST['priority']) ? sanitize_text_field($_POST['priority']) : 'normal'; if (!$post_id || !$task_id) { wp_send_json_error(array('message' => 'Invalid post ID or task ID')); } $api = new Igny8API(); if (!$api->is_authenticated()) { wp_send_json_error(array('message' => 'Not authenticated')); } $response = $api->post("/optimizer/jobs/", array( 'post_id' => $post_id, 'task_id' => $task_id, 'job_type' => $job_type, 'priority' => $priority )); if ($response['success'] && !empty($response['data'])) { $job_id = $response['data']['id'] ?? $response['data']['job_id'] ?? null; if ($job_id) { update_post_meta($post_id, '_igny8_optimizer_job_id', $job_id); update_post_meta($post_id, '_igny8_optimizer_status', $response['data']['status'] ?? 'pending'); update_post_meta($post_id, '_igny8_optimizer_job_created_at', current_time('mysql')); } wp_send_json_success(array( 'message' => 'Optimizer job created successfully', 'job_id' => $job_id, 'data' => $response['data'] )); } else { wp_send_json_error(array( 'message' => 'Failed to create optimizer job: ' . ($response['error'] ?? 'Unknown error') )); } } /** * Get Optimizer job status (AJAX handler) */ public static function get_optimizer_status() { check_ajax_referer('igny8_post_editor_nonce', 'nonce'); if (!current_user_can('edit_posts')) { wp_send_json_error(array('message' => 'Unauthorized')); } if (!igny8_is_connection_enabled()) { wp_send_json_error(array('message' => 'Connection is disabled. Enable sync operations first.')); } $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; $job_id = isset($_POST['job_id']) ? intval($_POST['job_id']) : 0; if (!$post_id || !$job_id) { wp_send_json_error(array('message' => 'Invalid post ID or job ID')); } $api = new Igny8API(); if (!$api->is_authenticated()) { wp_send_json_error(array('message' => 'Not authenticated')); } $response = $api->get("/optimizer/jobs/{$job_id}/"); if ($response['success'] && !empty($response['data'])) { $status = $response['data']['status'] ?? 'unknown'; update_post_meta($post_id, '_igny8_optimizer_status', $status); if (!empty($response['data']['score_changes'])) { update_post_meta($post_id, '_igny8_optimizer_score_changes', $response['data']['score_changes']); } if (!empty($response['data']['recommendations'])) { update_post_meta($post_id, '_igny8_optimizer_recommendations', $response['data']['recommendations']); } wp_send_json_success(array( 'message' => 'Status retrieved successfully', 'status' => $status, 'data' => $response['data'] )); } else { wp_send_json_error(array( 'message' => 'Failed to get status: ' . ($response['error'] ?? 'Unknown error') )); } } } // Initialize new Igny8PostMetaBoxes();