$value) { $new_columns[$key] = $value; if ($key === 'title') { $new_columns['igny8_source'] = __('Source', 'igny8-bridge'); $new_columns['igny8_sectors'] = __('Sectors', 'igny8-bridge'); $new_columns['igny8_clusters'] = __('Clusters', 'igny8-bridge'); } } return $new_columns; } /** * Render column content * * @param string $column_name Column name * @param int $post_id Post ID */ public function render_column_content($column_name, $post_id) { switch ($column_name) { case 'igny8_source': $this->render_source_column($post_id); break; case 'igny8_sectors': $this->render_sectors_column($post_id); break; case 'igny8_clusters': $this->render_clusters_column($post_id); break; } } /** * Render source column * * @param int $post_id Post ID */ private function render_source_column($post_id) { $task_id = get_post_meta($post_id, '_igny8_task_id', true); if ($task_id) { echo ''; echo esc_html__('IGNY8', 'igny8-bridge'); echo ''; } else { echo ''; echo esc_html__('WP', 'igny8-bridge'); echo ''; } } /** * Render sectors column * * @param int $post_id Post ID */ private function render_sectors_column($post_id) { $sectors = wp_get_post_terms($post_id, 'igny8_sectors', array('fields' => 'names')); if (!empty($sectors) && !is_wp_error($sectors)) { echo '
'; foreach ($sectors as $sector) { echo '' . esc_html($sector) . ''; } echo '
'; } else { echo ''; } } /** * Render clusters column * * @param int $post_id Post ID */ private function render_clusters_column($post_id) { $clusters = wp_get_post_terms($post_id, 'igny8_clusters', array('fields' => 'names')); if (!empty($clusters) && !is_wp_error($clusters)) { echo '
'; foreach ($clusters as $cluster) { echo '' . esc_html($cluster) . ''; } echo '
'; } else { echo ''; } } /** * Make columns sortable * * @param array $columns Sortable columns * @return array Modified columns */ public function make_columns_sortable($columns) { $columns['igny8_source'] = 'igny8_source'; return $columns; } /** * Add row actions * * @param array $actions Existing actions * @param WP_Post $post Post object * @return array Modified actions */ public function add_row_actions($actions, $post) { // Only add for published posts if ($post->post_status !== 'publish') { return $actions; } // Check if already synced to IGNY8 $task_id = get_post_meta($post->ID, '_igny8_task_id', true); if ($task_id) { // Already synced - show update action $actions['igny8_update'] = sprintf( '%s', '#', $post->ID, __('Update in IGNY8', 'igny8-bridge') ); } else { // Not synced - show send action $actions['igny8_send'] = sprintf( '%s', '#', $post->ID, __('Send to IGNY8', 'igny8-bridge') ); } return $actions; } /** * Send post to IGNY8 (AJAX handler) */ public static function send_to_igny8() { check_ajax_referer('igny8_admin_nonce', 'nonce'); if (!current_user_can('edit_posts')) { wp_send_json_error(array('message' => 'Unauthorized')); } $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; $action = isset($_POST['action_type']) ? sanitize_text_field($_POST['action_type']) : 'send'; if (!$post_id) { wp_send_json_error(array('message' => 'Invalid post ID')); } $post = get_post($post_id); if (!$post) { wp_send_json_error(array('message' => 'Post not found')); } if (!igny8_is_connection_enabled()) { wp_send_json_error(array('message' => 'Connection is disabled. Enable sync operations first.')); } $api = new Igny8API(); if (!$api->is_authenticated()) { wp_send_json_error(array('message' => 'Not authenticated with IGNY8')); } $site_id = get_option('igny8_site_id'); if (!$site_id) { wp_send_json_error(array('message' => 'Site ID not set')); } // Prepare post data for IGNY8 $post_data = array( 'title' => $post->post_title, 'content' => $post->post_content, 'excerpt' => $post->post_excerpt, 'status' => $post->post_status === 'publish' ? 'completed' : 'draft', 'post_type' => $post->post_type, 'url' => get_permalink($post_id), 'wordpress_post_id' => $post_id ); // Get categories $categories = wp_get_post_categories($post_id, array('fields' => 'names')); if (!empty($categories)) { $post_data['categories'] = $categories; } // Get tags $tags = wp_get_post_tags($post_id, array('fields' => 'names')); if (!empty($tags)) { $post_data['tags'] = $tags; } // Get featured image $featured_image_id = get_post_thumbnail_id($post_id); if ($featured_image_id) { $post_data['featured_image'] = wp_get_attachment_image_url($featured_image_id, 'full'); } // Get sectors and clusters $sectors = wp_get_post_terms($post_id, 'igny8_sectors', array('fields' => 'ids')); $clusters = wp_get_post_terms($post_id, 'igny8_clusters', array('fields' => 'ids')); if (!empty($sectors)) { // Get IGNY8 sector IDs from term meta $igny8_sector_ids = array(); foreach ($sectors as $term_id) { $igny8_sector_id = get_term_meta($term_id, '_igny8_sector_id', true); if ($igny8_sector_id) { $igny8_sector_ids[] = $igny8_sector_id; } } if (!empty($igny8_sector_ids)) { $post_data['sector_id'] = $igny8_sector_ids[0]; // Use first sector } } if (!empty($clusters)) { // Get IGNY8 cluster IDs from term meta $igny8_cluster_ids = array(); foreach ($clusters as $term_id) { $igny8_cluster_id = get_term_meta($term_id, '_igny8_cluster_id', true); if ($igny8_cluster_id) { $igny8_cluster_ids[] = $igny8_cluster_id; } } if (!empty($igny8_cluster_ids)) { $post_data['cluster_id'] = $igny8_cluster_ids[0]; // Use first cluster } } // Check if post already has task ID $existing_task_id = get_post_meta($post_id, '_igny8_task_id', true); if ($existing_task_id && $action === 'update') { // Update existing task $response = $api->put("/writer/tasks/{$existing_task_id}/", $post_data); } else { // Create new task $response = $api->post("/writer/tasks/", $post_data); } if ($response['success']) { $task_id = $response['data']['id'] ?? $existing_task_id; // Store task ID update_post_meta($post_id, '_igny8_task_id', $task_id); update_post_meta($post_id, '_igny8_last_synced', current_time('mysql')); wp_send_json_success(array( 'message' => $action === 'update' ? 'Post updated in IGNY8' : 'Post sent to IGNY8', 'task_id' => $task_id )); } else { wp_send_json_error(array( 'message' => 'Failed to send to IGNY8: ' . ($response['error'] ?? 'Unknown error') )); } } } // Initialize new Igny8AdminColumns(); // Register AJAX handler add_action('wp_ajax_igny8_send_to_igny8', array('Igny8AdminColumns', 'send_to_igny8'));