0, 'post_types' => array( 'post', 'page' ), 'exclude_ids' => array(), 'site_description' => '', 'max_posts' => 500, 'generate_full' => 0, 'auto_rebuild' => 1, 'use_summaries' => 1, 'manual_sections' => '', 'curation_mode' => 'auto', // 'auto' (top by AILM Rank) | 'manual' (explicit list). 'selected_posts' => array(), // Ordered post IDs for manual mode. 'pinned_pages' => array(), // Ordered post IDs rendered first (both modes). // H2 heading for the pinned section. Stored empty by default and // translated at render time — never persist a translated string. 'pinned_title' => '', 'autopublish' => 0, // 1 = cron writes the live file directly (legacy behaviour). ); } /** * Get all settings (merged with defaults). * * @return array */ public static function get_all() { if ( null === self::$settings ) { $raw = get_option( self::OPTION_KEY, array() ); if ( ! is_array( $raw ) ) { $raw = array(); } self::$settings = wp_parse_args( $raw, self::defaults() ); } return self::$settings; } /** * Get a single setting. * * @param string $key Setting key. * @param mixed $default Fallback value. * @return mixed */ public static function get( $key, $default = null ) { $all = self::get_all(); if ( ! isset( $all[ $key ] ) ) { return $default; } return $all[ $key ]; } /** * Update settings after sanitization. * * @param array $raw Unsanitized input (e.g. $_POST). * @return array Sanitized settings that were stored. */ public static function update_from_input( array $raw ) { $settings = self::sanitize( $raw ); update_option( self::OPTION_KEY, $settings, false ); self::$settings = null; return $settings; } /** * Sanitize raw settings input. * * @param array $raw Unsanitized input. * @return array Sanitized settings. */ public static function sanitize( array $raw ) { $defaults = self::defaults(); // Post types: whitelist against public post types. $public_types = get_post_types( array( 'public' => true ), 'names' ); $raw_types = isset( $raw['post_types'] ) && is_array( $raw['post_types'] ) ? $raw['post_types'] : array(); $post_types = array_values( array_intersect( array_map( 'sanitize_key', array_map( 'wp_unslash', $raw_types ) ), $public_types ) ); $post_types = array_unique( $post_types ); if ( empty( $post_types ) ) { $post_types = $defaults['post_types']; } // Excluded post IDs: positive integers only. // Accept both a string ("1, 2, 3" — admin UI / $_POST) and an array // (typed callers), then normalize to a flat list of parts. $exclude_ids = array(); $parts = array(); if ( isset( $raw['exclude_ids'] ) ) { if ( is_string( $raw['exclude_ids'] ) ) { $split = preg_split( '/[\s,]+/', trim( (string) wp_unslash( $raw['exclude_ids'] ) ) ); $parts = is_array( $split ) ? $split : array(); } elseif ( is_array( $raw['exclude_ids'] ) ) { $parts = (array) wp_unslash( $raw['exclude_ids'] ); } } foreach ( $parts as $part ) { if ( is_string( $part ) && '' === trim( $part ) ) { continue; } // Strict int cast: negative values are dropped, not abs'd // (consistent with selected_posts below). $id = (int) wp_unslash( $part ); if ( $id > 0 ) { $exclude_ids[] = $id; } } $exclude_ids = array_values( array_unique( $exclude_ids ) ); // Max posts: 1..500 (v1 hard cap). $max_posts = isset( $raw['max_posts'] ) ? absint( wp_unslash( $raw['max_posts'] ) ) : $defaults['max_posts']; $max_posts = max( 1, min( 500, $max_posts ) ); // Site description: single line of plain text. $site_description = ''; if ( isset( $raw['site_description'] ) ) { $site_description = sanitize_text_field( wp_unslash( $raw['site_description'] ) ); } // Manual sections: multi-line plain markdown, sanitized conservatively. $manual_sections = ''; if ( isset( $raw['manual_sections'] ) ) { $manual_sections = wp_unslash( (string) $raw['manual_sections'] ); // Strip tags but keep markdown-safe characters; remove null bytes. $manual_sections = str_replace( "\0", '', $manual_sections ); $manual_sections = preg_replace( '/<\s*\/?\s*(script|style|iframe)[^>]*>/i', '', $manual_sections ); $manual_sections = trim( $manual_sections ); } // Curation mode: enum. $curation_mode = isset( $raw['curation_mode'] ) ? sanitize_key( wp_unslash( $raw['curation_mode'] ) ) : 'auto'; if ( ! in_array( $curation_mode, array( 'auto', 'manual' ), true ) ) { $curation_mode = 'auto'; } // Selected posts (manual mode): ordered positive ints, capped at 500. $selected_posts = array(); if ( isset( $raw['selected_posts'] ) && is_array( $raw['selected_posts'] ) ) { foreach ( $raw['selected_posts'] as $pid ) { // Strict int cast: negative values are dropped, not abs'd. $pid = (int) wp_unslash( $pid ); if ( $pid > 0 && ! in_array( $pid, $selected_posts, true ) ) { $selected_posts[] = $pid; } if ( count( $selected_posts ) >= 500 ) { break; } } } // Pinned pages: ordered positive ints rendered first, capped at PINNED_MAX. $pinned_pages = array(); if ( isset( $raw['pinned_pages'] ) && is_array( $raw['pinned_pages'] ) ) { foreach ( $raw['pinned_pages'] as $pid ) { // Strict int cast: negative values are dropped, not abs'd. $pid = (int) wp_unslash( $pid ); if ( $pid > 0 && ! in_array( $pid, $pinned_pages, true ) ) { $pinned_pages[] = $pid; } if ( count( $pinned_pages ) >= self::PINNED_MAX ) { break; } } } // Pinned section heading: single line of plain text. Empty is a valid // value — the renderer then falls back to a translated default. $pinned_title = ''; if ( isset( $raw['pinned_title'] ) ) { $pinned_title = trim( sanitize_text_field( wp_unslash( $raw['pinned_title'] ) ) ); } return array( 'enabled' => empty( $raw['enabled'] ) ? 0 : 1, 'post_types' => $post_types, 'exclude_ids' => $exclude_ids, 'site_description' => $site_description, 'max_posts' => $max_posts, 'generate_full' => empty( $raw['generate_full'] ) ? 0 : 1, 'auto_rebuild' => empty( $raw['auto_rebuild'] ) ? 0 : 1, 'use_summaries' => empty( $raw['use_summaries'] ) ? 0 : 1, 'manual_sections' => $manual_sections, 'curation_mode' => $curation_mode, 'selected_posts' => $selected_posts, 'pinned_pages' => $pinned_pages, 'pinned_title' => $pinned_title, 'autopublish' => empty( $raw['autopublish'] ) ? 0 : 1, ); } /** * Whether the module is enabled and files should be served. * * @return bool */ public static function is_enabled() { return (bool) self::get( 'enabled' ); } /** * Enable or disable the module (persists immediately). * * Used by the Rebuild button so a click always produces a file — * pressing "Rebuild" is an explicit intent to have llms.txt. * * @param bool $enabled Whether to enable. * @return void */ public static function set_enabled( $enabled ) { $all = self::get_all(); $all['enabled'] = $enabled ? 1 : 0; update_option( self::OPTION_KEY, $all, false ); self::$settings = null; } }