[ __CLASS__, 'sanitize_settings' ], ] ); } public static function sanitize_settings( array $input ): array { $clean = []; $clean['mode'] = in_array( $input['mode'] ?? '', [ 'free', 'pro' ] ) ? $input['mode'] : 'free'; $clean['pro_key'] = sanitize_text_field( $input['pro_key'] ?? '' ); $clean['auto_update'] = ! empty( $input['auto_update'] ); $clean['include_products'] = ! empty( $input['include_products'] ); $clean['include_posts'] = ! empty( $input['include_posts'] ); $clean['include_pages'] = ! empty( $input['include_pages'] ); $clean['max_products'] = min( 500, max( 5, (int) ( $input['max_products'] ?? 50 ) ) ); $clean['max_posts'] = min( 100, max( 5, (int) ( $input['max_posts'] ?? 20 ) ) ); $clean['custom_sections'] = sanitize_textarea_field( $input['custom_sections'] ?? '' ); $clean['webhook_secret'] = sanitize_text_field( $input['webhook_secret'] ?? '' ); $clean['exclude_urls'] = sanitize_textarea_field( $input['exclude_urls'] ?? '' ); $clean['respect_robots_txt'] = ! empty( $input['respect_robots_txt'] ); return $clean; } public static function enqueue( string $hook ): void { if ( ! str_contains( $hook, 'aiready-llms-txt-generator' ) ) return; wp_enqueue_style( 'llmstxt-admin', LLMSTXT_URL . 'admin/admin.css', [], LLMSTXT_VERSION ); wp_enqueue_script( 'llmstxt-admin', LLMSTXT_URL . 'admin/admin.js', [ 'jquery' ], LLMSTXT_VERSION, true ); wp_localize_script( 'llmstxt-admin', 'llmstxtAdmin', [ 'nonce' => wp_create_nonce( 'llmstxt_nonce' ), 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'strings' => [ 'generateBtn' => __( '⚡ Generate Now', 'aiready-llms-txt-generator' ), 'generating' => __( 'Generating…', 'aiready-llms-txt-generator' ), 'previewBtn' => __( '👁 Preview', 'aiready-llms-txt-generator' ), 'previewLoading' => __( 'Loading…', 'aiready-llms-txt-generator' ), 'verifyBtn' => __( 'Verify', 'aiready-llms-txt-generator' ), 'verifying' => __( 'Verifying…', 'aiready-llms-txt-generator' ), 'success' => __( 'Done!', 'aiready-llms-txt-generator' ), 'error' => __( 'Error', 'aiready-llms-txt-generator' ), 'networkError' => __( 'Network error', 'aiready-llms-txt-generator' ), 'isLive' => __( 'llms.txt is live', 'aiready-llms-txt-generator' ), 'lastUpdated' => __( 'Last updated:', 'aiready-llms-txt-generator' ), 'lines' => __( 'lines', 'aiready-llms-txt-generator' ), 'keyValid' => __( '✅ Pro key valid', 'aiready-llms-txt-generator' ), 'keyInvalid' => __( '❌ Invalid key', 'aiready-llms-txt-generator' ), 'enterKey' => __( 'Please enter a key first.', 'aiready-llms-txt-generator' ), ], ] ); } public static function render_page(): void { $opts = get_option( 'llmstxt_settings', [] ); $file_info = get_option( 'llmstxt_file_info', [] ); $last_err = get_option( 'llmstxt_last_error', '' ); $mode = $opts['mode'] ?? 'free'; $site_url = home_url( '/llms.txt' ); include LLMSTXT_DIR . 'admin/templates/page-main.php'; } public static function ajax_generate(): void { check_ajax_referer( 'llmstxt_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error( 'Forbidden', 403 ); $generator = new LLMSTXT_Generator(); $ok = $generator->generate_and_save(); $err = get_option( 'llmstxt_last_error', '' ); if ( $ok ) { $info = get_option( 'llmstxt_file_info', [] ); wp_send_json_success( [ 'message' => __( 'llms.txt generated successfully.', 'aiready-llms-txt-generator' ), 'updated' => $info['updated'] ?? '', 'lines' => $info['lines'] ?? 0, 'size' => $info['size'] ?? 0, ] ); } else { wp_send_json_error( $err ?: __( 'Generation failed.', 'aiready-llms-txt-generator' ) ); } } public static function ajax_verify_key(): void { check_ajax_referer( 'llmstxt_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error( 'Forbidden', 403 ); $key = sanitize_text_field( wp_unslash( $_POST['key'] ?? '' ) ); if ( ! $key ) wp_send_json_error( __( 'No key provided.', 'aiready-llms-txt-generator' ) ); $client = new LLMSTXT_API_Client( $key ); $result = $client->verify_key(); if ( is_wp_error( $result ) ) { wp_send_json_error( $result->get_error_message() ); } if ( empty( $result['valid'] ) ) { wp_send_json_error( $result['error'] ?? __( 'Invalid key.', 'aiready-llms-txt-generator' ) ); } wp_send_json_success( $result ); } public static function ajax_preview(): void { check_ajax_referer( 'llmstxt_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error( 'Forbidden', 403 ); $content = ( new LLMSTXT_Generator() )->build_content( 'standard' ); wp_send_json_success( [ 'content' => $content ] ); } public static function ajax_dismiss_notice(): void { check_ajax_referer( 'llmstxt_nonce', 'nonce' ); update_option( 'llmstxt_notice_dismissed', true ); wp_send_json_success(); } public static function plugin_links( array $links ): array { array_unshift( $links, '' . __( 'Settings', 'aiready-llms-txt-generator' ) . '' ); return $links; } public static function maybe_show_notice(): void { $screen = get_current_screen(); if ( $screen && str_contains( $screen->id, 'llmstxt' ) ) return; if ( LLMSTXT_File_Writer::exists() ) return; if ( get_option( 'llmstxt_notice_dismissed' ) ) return; echo '

'; printf( // translators: %s: link to generate page esc_html__( '🤖 LLMs.txt Generator: Your llms.txt hasn\'t been generated yet. %s', 'aiready-llms-txt-generator' ), '' . esc_html__( 'Generate now →', 'aiready-llms-txt-generator' ) . '' ); echo '

'; } public static function ajax_save_key(): void { check_ajax_referer( 'llmstxt_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error( 'Forbidden', 403 ); $key = sanitize_text_field( wp_unslash( $_POST['key'] ?? '' ) ); $plan = sanitize_text_field( wp_unslash( $_POST['plan'] ?? 'pro' ) ); if ( ! $key ) wp_send_json_error( 'No key.' ); $opts = get_option( 'llmstxt_settings', [] ); $opts['mode'] = 'pro'; $opts['pro_key'] = $key; update_option( 'llmstxt_settings', $opts ); wp_send_json_success( [ 'plan' => $plan ] ); } }