resolve_category( $article['category_hint'], $default_cat ); $post_data = array( 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt, 'post_status' => $post_status, 'post_author' => $post_author, 'post_type' => 'post', 'post_category' => $category_id ? array( $category_id ) : array(), ); // Schedule for future publishing if a date is provided and status is 'publish'. if ( $scheduled_date && $post_status === 'publish' ) { $post_data['post_status'] = 'future'; $post_data['post_date'] = get_date_from_gmt( $scheduled_date ); $post_data['post_date_gmt'] = $scheduled_date; } $post_id = wp_insert_post( $post_data, true ); if ( is_wp_error( $post_id ) ) { Logger::error( __( 'Could not create post: ', 'ai-content-autopilot' ) . $post_id->get_error_message() ); return null; } // Set featured image. if ( $image_id ) { set_post_thumbnail( $post_id, $image_id ); } // Mark as seen. $this->mark_as_seen( $news_item, $post_id ); // Add to internal links index. $links = new InternalLinks(); $links->add_article( $post_id, $title, get_permalink( $post_id ), $content ); $actual_status = $scheduled_date && $post_status === 'publish' ? 'future' : $post_status; $schedule_info = $scheduled_date && $post_status === 'publish' /* translators: %s: scheduled date */ ? sprintf( __( ', scheduled: %s', 'ai-content-autopilot' ), get_date_from_gmt( $scheduled_date, 'j. M H:i' ) ) : ''; Logger::info( sprintf( /* translators: 1: post title, 2: post ID, 3: post status, 4: schedule info */ __( 'Post created: "%1$s" (ID: %2$d, status: %3$s%4$s)', 'ai-content-autopilot' ), $title, $post_id, $actual_status, $schedule_info ) ); return $post_id; } /** * Mark a news item as seen in the database. */ private function mark_as_seen( array $news_item, $post_id ) { global $wpdb; $table = $wpdb->prefix . 'aica_seen_articles'; $wpdb->insert( $table, array( 'hash' => $news_item['hash'], 'title' => $news_item['title'], 'url' => $news_item['url'], 'post_id' => $post_id, 'created_at' => current_time( 'mysql' ), ), array( '%s', '%s', '%s', '%d', '%s' ) ); } /** * Resolve category from AI hint or use default. * * @param string $hint Category suggestion from AI. * @param int $default_id Default category ID. * @return int Category ID. */ private function resolve_category( $hint, $default_id ) { if ( empty( $hint ) ) { return $default_id; } $hint = sanitize_text_field( $hint ); // Check if category already exists. $term = term_exists( $hint, 'category' ); if ( $term ) { return (int) $term['term_id']; } // Create the category. $result = wp_insert_term( $hint, 'category' ); if ( ! is_wp_error( $result ) ) { return (int) $result['term_id']; } return $default_id; } }