get_or_create_category($item->category, $default_category); // Determine post status - scheduled date takes priority $post_status = $default_status; $post_date = null; if (!empty($item->scheduled_date)) { $post_status = 'future'; $post_date = $item->scheduled_date; } $post_data = array( 'post_title' => $item->title, 'post_content' => wp_kses_post($content), 'post_status' => $post_status, 'post_author' => get_current_user_id(), 'post_category' => array($category_id), 'post_type' => 'post' ); if ($post_date) { $post_data['post_date'] = $post_date; $post_data['post_date_gmt'] = get_gmt_from_date($post_date); } $post_id = wp_insert_post($post_data, true); if (is_wp_error($post_id)) { return $post_id; } if (!empty($item->keywords)) { $tags = array_map('trim', explode(',', $item->keywords)); wp_set_post_tags($post_id, $tags, false); } update_post_meta($post_id, '_aigh_generated', 1); update_post_meta($post_id, '_aigh_plan_item_id', $item->id); update_post_meta($post_id, '_aigh_generated_date', current_time('mysql')); if ($post_date) { update_post_meta($post_id, '_aigh_scheduled_date', $post_date); } return $post_id; } private function get_or_create_category($category_name, $default_category_id) { if (empty($category_name)) { return $default_category_id; } $category = get_term_by('name', $category_name, 'category'); if ($category) { return $category->term_id; } $new_category = wp_insert_term($category_name, 'category'); if (is_wp_error($new_category)) { return $default_category_id; } return $new_category['term_id']; } public function schedule_article($post_id, $scheduled_date) { $post_data = array( 'ID' => $post_id, 'post_status' => 'future', 'post_date' => $scheduled_date ); return wp_update_post($post_data, true); } public function update_article($post_id, $content) { $post_data = array( 'ID' => $post_id, 'post_content' => wp_kses_post($content) ); return wp_update_post($post_data, true); } }