*/ public function status(): array { $state = get_option( self::STATE ); if ( ! is_array( $state ) ) { return array( 'state' => 'idle' ); } return $this->view( $state ); } /** * Start over: collect every page the two files will carry. * * @param bool $overwrite Rewrite summaries that already exist. * @return array */ public function start( bool $overwrite ): array { $settings = $this->options->get( 'llms_txt', Generator::defaults() ); $ids = array(); foreach ( array_map( 'absint', (array) ( $settings['key_pages'] ?? array() ) ) as $id ) { if ( $id > 0 ) { $ids[] = $id; } } $limit = max( (int) ( $settings['max_items'] ?? 50 ), (int) ( $settings['full_max_items'] ?? 30 ) ); foreach ( (array) ( $settings['post_types'] ?? array( 'page', 'post' ) ) as $type ) { foreach ( get_posts( array( 'post_type' => $type, 'post_status' => 'publish', 'posts_per_page' => $limit, 'fields' => 'ids', 'orderby' => 'page' === $type ? 'menu_order' : 'date', 'order' => 'page' === $type ? 'ASC' : 'DESC', 'suppress_filters' => false, 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query 'relation' => 'OR', array( 'key' => '_airanksuite_exclude', 'compare' => 'NOT EXISTS' ), array( 'key' => '_airanksuite_exclude', 'value' => '1', 'compare' => '!=' ), ), ) ) as $id ) { $ids[] = (int) $id; } } $ids = array_values( array_unique( $ids ) ); $state = array( 'state' => 'running', 'ids' => $ids, 'offset' => 0, 'written' => 0, 'kept' => 0, 'failed' => 0, 'ai' => $this->ai_ready(), 'overwrite' => $overwrite, 'current' => '', 'started' => time(), 'errors' => array(), ); update_option( self::STATE, $state, false ); return $this->view( $state ); } /** * One slice: a few pages, then the site lead and the files at the end. * * @return array */ public function tick(): array { $state = get_option( self::STATE ); if ( ! is_array( $state ) || 'running' !== ( $state['state'] ?? '' ) ) { return $this->status(); } $ids = (array) $state['ids']; $total = count( $ids ); $end = min( $total, (int) $state['offset'] + self::SLICE ); for ( $i = (int) $state['offset']; $i < $end; $i++ ) { $post = get_post( (int) $ids[ $i ] ); if ( ! $post instanceof WP_Post ) { continue; } $state['current'] = get_the_title( $post ); $existing = (string) get_post_meta( $post->ID, '_airanksuite_ai_summary', true ); if ( '' !== trim( $existing ) && empty( $state['overwrite'] ) ) { ++$state['kept']; continue; } $summary = $this->summarise( $post, (bool) $state['ai'] ); if ( '' === $summary ) { ++$state['failed']; $state['errors'][] = get_the_title( $post ); continue; } update_post_meta( $post->ID, '_airanksuite_ai_summary', $summary ); ++$state['written']; } $state['offset'] = $end; if ( $end >= $total ) { $this->finish( $state ); $state['state'] = 'done'; $state['ended'] = time(); } update_option( self::STATE, $state, false ); return $this->view( $state ); } /** @return array */ public function cancel(): array { $state = get_option( self::STATE ); if ( is_array( $state ) ) { $state['state'] = 'cancelled'; update_option( self::STATE, $state, false ); return $this->view( $state ); } return $this->status(); } /** * The site's own lead, then both files rebuilt. * * @param array $state Job state. */ private function finish( array &$state ): void { $settings = $this->options->get( 'llms_txt', Generator::defaults() ); if ( '' === trim( (string) ( $settings['summary'] ?? '' ) ) ) { $lead = $this->site_lead( (array) $state['ids'], (bool) $state['ai'] ); if ( '' !== $lead ) { $this->options->set( 'llms_txt', array( 'summary' => $lead ), Generator::defaults() ); $state['lead'] = $lead; } } delete_transient( LlmsTxtModule::CACHE_KEY ); delete_transient( LlmsTxtModule::CACHE_KEY_FULL ); } /** * A summary for one page: the model's when there is one, the page's own * first sentences otherwise. */ private function summarise( WP_Post $post, bool $ai ): string { if ( $ai ) { try { $generator = \AiRankSuite\Kernel::service( \AiRankSuite\Modules\Ai\Services\Generator::class ); $result = $generator->describe( $post ); if ( ! $result instanceof \WP_Error && '' !== trim( (string) ( $result['summary'] ?? '' ) ) ) { return trim( (string) $result['summary'] ); } } catch ( \Throwable $e ) { // Fall through to the text of the page itself. unset( $e ); } } return self::first_sentences( Html::text( Html::without_private( Html::rendered( $post ) ) ), 2, 320 ); } /** * Two sentences about the whole site, from its name, tagline and the pages just summarised. * * @param array $ids Pages in the job. */ private function site_lead( array $ids, bool $ai ): string { $name = get_bloginfo( 'name' ); $tagline = get_bloginfo( 'description' ); $pages = array(); foreach ( array_slice( $ids, 0, 12 ) as $id ) { $post = get_post( (int) $id ); if ( ! $post instanceof WP_Post ) { continue; } $pages[] = get_the_title( $post ) . ': ' . (string) get_post_meta( $post->ID, '_airanksuite_ai_summary', true ); } if ( $ai ) { try { $client = \AiRankSuite\Kernel::service( \AiRankSuite\Modules\Ai\Services\AiClient::class ); $text = $client->for( 'meta' )->complete( 'You write the one-paragraph lead of a website\'s llms.txt: two factual sentences that say what the site is, who it is for and what it offers. Same language as the pages. No marketing adjectives, no exclamation marks, no quotation marks.', "Site name: {$name}\nTagline: {$tagline}\n\nPages:\n" . implode( "\n", $pages ), 200 ); if ( ! $text instanceof \WP_Error && '' !== trim( (string) $text ) ) { return trim( sanitize_textarea_field( (string) $text ) ); } } catch ( \Throwable $e ) { unset( $e ); } } return '' !== $tagline ? $tagline : ''; } /** * Whether the AI engine can be asked at all. */ private function ai_ready(): bool { if ( ! class_exists( \AiRankSuite\Modules\Ai\Services\AiClient::class ) ) { return false; } try { return (bool) \AiRankSuite\Kernel::service( \AiRankSuite\Modules\Ai\Services\AiClient::class )->has_key(); } catch ( \Throwable $e ) { return false; } } /** * The first sentences of a text, cut at a sentence end and a length. */ public static function first_sentences( string $text, int $count, int $max ): string { $text = trim( (string) preg_replace( '/\s+/', ' ', $text ) ); if ( '' === $text ) { return ''; } $parts = preg_split( '/(?<=[.!?])\s+(?=[A-ZÄÖÜ0-9„"(])/u', $text ) ?: array( $text ); $out = trim( implode( ' ', array_slice( $parts, 0, $count ) ) ); if ( mb_strlen( $out ) > $max ) { $out = rtrim( mb_substr( $out, 0, $max - 1 ) ) . '…'; } return $out; } /** * What the browser draws. * * @param array $state Job state. * @return array */ private function view( array $state ): array { $total = count( (array) ( $state['ids'] ?? array() ) ); return array( 'state' => (string) ( $state['state'] ?? 'idle' ), 'total' => $total, 'done' => min( $total, (int) ( $state['offset'] ?? 0 ) ), 'written' => (int) ( $state['written'] ?? 0 ), 'kept' => (int) ( $state['kept'] ?? 0 ), 'failed' => (int) ( $state['failed'] ?? 0 ), 'ai' => (bool) ( $state['ai'] ?? false ), 'current' => (string) ( $state['current'] ?? '' ), 'lead' => (string) ( $state['lead'] ?? '' ), 'errors' => array_slice( (array) ( $state['errors'] ?? array() ), 0, 10 ), ); } }