url */ public static function internal_links( $topic, $limit = 10, $exclude = 0 ) { $out = array(); $exclude = (int) $exclude; // One extra row instead of an exclusion clause: the excluded post is skipped here. $query = new WP_Query( array( 's' => wp_trim_words( $topic, 6, '' ), 'post_type' => array( 'post', 'page' ), 'post_status' => 'publish', 'posts_per_page' => $limit + 1, 'no_found_rows' => true, ) ); $recent = count( $query->posts ) < $limit + 1 ? get_posts( array( 'numberposts' => $limit + 1, 'post_status' => 'publish', ) ) : array(); foreach ( array_merge( $query->posts, $recent ) as $p ) { if ( count( $out ) >= $limit ) { break; } if ( (int) $p->ID !== $exclude ) { $out[ get_the_title( $p ) ] = get_permalink( $p ); } } return $out; } /** * Format link list for a prompt. * * @param array $links title => url. * @return string */ private static function links_text( array $links ) { $lines = array(); foreach ( $links as $t => $u ) { $lines[] = '- ' . $t . ' → ' . $u; } return $lines ? implode( "\n", $lines ) : '(none)'; } /** * Plain-text excerpt of a post for prompts. * * @param WP_Post $post Post. * @param int $words Max words. * @return string */ public static function post_text( WP_Post $post, $words = 2500 ) { $html = Aeoora_Content::render_html( $post, 'content' ); $md = Aeoora_Html_To_Markdown::convert( $html, get_permalink( $post ) ); return wp_trim_words( $md, $words, ' […]' ); } /* ------------------------------------------------------- blog writer */ /** * Step 1: outline (+ title, meta, answer capsule, takeaways, FAQ). * * @param array $in Input. * @return array|WP_Error */ public static function outline( array $in ) { $in = self::writer_input( $in ); $links = self::internal_links( $in['topic'], 8 ); $prompt = "Plan an AEO-optimised article.\n\n" . 'Topic / primary question: ' . $in['topic'] . "\n" . 'Focus phrase: ' . ( $in['keyword'] ? $in['keyword'] : $in['topic'] ) . "\n" . 'Secondary keywords: ' . ( $in['keywords'] ? $in['keywords'] : '(choose naturally)' ) . "\n" . 'Audience and intent: ' . ( $in['audience'] ? $in['audience'] : 'general readers' ) . '; intent = ' . $in['intent'] . "\n" . 'Target length: about ' . $in['words'] . " words\n" . 'First-hand experience to include: ' . ( $in['experience'] ? $in['experience'] : '(none provided — add [EXPERIENCE NEEDED] placeholders where it would help)' ) . "\n" . 'Verified facts with sources (ONLY these may be stated as facts): ' . ( $in['facts'] ? $in['facts'] : '(none — use [STAT NEEDED] placeholders)' ) . "\n" . "Internal pages available for links:\n" . self::links_text( $links ) . "\n\n" . "Return JSON with exactly these keys:\n" . "{\n \"title\": \"≤60 chars, main entity in the first 40 chars\",\n \"meta_description\": \"140–160 chars, core answer in the first 120\",\n \"slug\": \"short-url-slug\",\n \"entity\": \"main entity name used consistently\",\n \"answer\": \"40–60 word paragraph that directly answers the primary question and defines the entity (no links)\",\n \"takeaways\": [\"3–5 standalone facts, ≤25 words each\"],\n \"sections\": [{\"heading\": \"H2 text — about half phrased as real user questions\", \"brief\": \"what this section must answer, 1–2 sentences\", \"format\": \"paragraphs|list|steps|table|comparison\"}],\n \"faq\": [{\"q\": \"real follow-up question\", \"a\": \"30–60 word self-contained answer\"}],\n \"image_suggestions\": [{\"placement\": \"after which section\", \"alt\": \"descriptive alt text\"}]\n}\n" . 'Use ' . max( 3, min( 12, (int) round( $in['words'] / 250 ) ) ) . ' sections covering the likely follow-up questions (definition, how it works, cost/time, steps, comparison/alternatives, pros/cons, mistakes) and end with a short "Bottom line" section. 3–6 FAQ items.'; $res = Aeoora_Ai::complete( array( 'system' => self::system( 'You plan articles that get cited by AI answer engines.' ), 'prompt' => $prompt, 'json' => true, 'provider' => $in['provider'], 'model' => $in['model'], ) ); if ( is_wp_error( $res ) ) { return $res; } $data = $res['data']; if ( empty( $data['sections'] ) || ! is_array( $data['sections'] ) ) { return new WP_Error( 'aeoora_ai_bad_json', __( 'The AI’s plan came back without any sections. Try again, or choose a shorter length.', 'aeoora' ) ); } $data['links'] = $links; $data['_ai'] = array( 'provider' => $res['provider'], 'label' => isset( $res['provider_label'] ) ? $res['provider_label'] : $res['provider'], 'model' => $res['model'], ); return self::normalize_outline( $data ); } /** * Step 2: write one section as HTML. * * @param array $in Input: outline, index, plus writer inputs. * @return array|WP_Error { html } */ public static function section( array $in ) { $w = self::writer_input( $in ); $outline = isset( $in['outline'] ) && is_array( $in['outline'] ) ? self::normalize_outline( $in['outline'] ) : array(); $i = isset( $in['index'] ) ? (int) $in['index'] : 0; if ( empty( $outline['sections'][ $i ] ) ) { return new WP_Error( 'aeoora_bad_section', __( 'Unknown section.', 'aeoora' ) ); } $sec = $outline['sections'][ $i ]; $all = array(); foreach ( $outline['sections'] as $n => $s ) { $all[] = ( $n + 1 ) . '. ' . $s['heading'] . ( $n === $i ? ' ← WRITE THIS ONE' : '' ); } $count = max( 1, count( $outline['sections'] ) ); $target = max( 120, (int) round( ( $w['words'] - 150 ) / $count ) ); $links = isset( $outline['links'] ) && is_array( $outline['links'] ) ? $outline['links'] : array(); $prompt = 'Article title: ' . $outline['title'] . "\n" . 'Main entity: ' . $outline['entity'] . "\n" . 'Answer already given in the intro: ' . $outline['answer'] . "\n\n" . "Full outline:\n" . implode( "\n", $all ) . "\n\n" . 'Write ONLY section ' . ( $i + 1 ) . ': "' . $sec['heading'] . '" (about ' . $target . " words).\n" . 'What it must cover: ' . $sec['brief'] . "\n" . 'Preferred format: ' . $sec['format'] . "\n" . 'Verified facts you may use: ' . ( $w['facts'] ? $w['facts'] : '(none — use [STAT NEEDED: …] placeholders)' ) . "\n" . 'First-hand experience notes: ' . ( $w['experience'] ? $w['experience'] : '(none)' ) . "\n" . "Internal pages you may link to with descriptive anchor text (max 2 in this section, only if relevant):\n" . self::links_text( $links ) . "\n\n" . "Rules: start with

containing the heading exactly; the first paragraph answers the heading in ≤50 words and names its subject (never start with It/This/They); then detail in short paragraphs; use