ui = $ui; $this->ai_bots = $ai_bots; $this->global_settings = $global_settings; $this->features = $features; } /** * The AI buttons follow the master AI switch (Dashboard → Features), the * same gate the REST endpoint enforces — rendering a button that is * guaranteed to answer 403 would be worse than rendering none. */ private function ai_enabled(): bool { return null === $this->features || $this->features->enabled( 'ai' ); } public function slug(): string { return 'llms'; } public function label(): string { return __( 'llms.txt', 'acez-seo' ); } public function icon(): string { return 'llms'; } public function enqueue(): void { if ( ! $this->ai_enabled() ) { return; } // The llms.txt AI buttons reuse the editor's field-agnostic click // handler (see FormControls::ai_button()), so no second script is // needed — only this enqueue plus the localized endpoint/nonce. wp_enqueue_script( 'acez-seo-meta-box-ai', ACEZ_SEO_URL . 'assets/js/meta-box-ai.js', array(), ACEZ_SEO_VERSION, true ); wp_localize_script( 'acez-seo-meta-box-ai', 'AcezSeoAi', array( 'restUrl' => esc_url_raw( rest_url( 'acez-seo/v1/generate' ) ), 'nonce' => wp_create_nonce( 'wp_rest' ), // Site-level fields belong to no post; the REST controller // never reads post_id for them (AiRestController). 'postId' => 0, 'generating' => __( 'Generating…', 'acez-seo' ), 'error' => __( 'Failed to generate content.', 'acez-seo' ), ) ); } public function handles_form_key( string $raw_tab ): bool { return in_array( sanitize_key( $raw_tab ), array( 'ai_bots', 'llms_takeover' ), true ); } /** @return array */ public function save( string $raw_tab ): array { // Defence in depth: SettingsPage::handle_save() verifies this nonce // before dispatch; re-checking keeps every entry point independently safe. check_admin_referer( FormControls::NONCE_ACTION ); $tab_key = sanitize_key( $raw_tab ); if ( 'ai_bots' === $tab_key ) { if ( null !== $this->ai_bots ) { $this->ai_bots->update( array( 'llms_enabled' => isset( $_POST['llms_enabled'] ), 'llms_title' => isset( $_POST['llms_title'] ) ? sanitize_text_field( wp_unslash( $_POST['llms_title'] ) ) : '', 'llms_description' => isset( $_POST['llms_description'] ) ? sanitize_textarea_field( wp_unslash( $_POST['llms_description'] ) ) : '', 'llms_intro' => isset( $_POST['llms_intro'] ) ? sanitize_textarea_field( wp_unslash( $_POST['llms_intro'] ) ) : '', 'llms_footer' => isset( $_POST['llms_footer'] ) ? sanitize_textarea_field( wp_unslash( $_POST['llms_footer'] ) ) : '', 'llms_sitemap_opt_in' => isset( $_POST['llms_sitemap_opt_in'] ), 'noai' => isset( $_POST['noai'] ), ) ); } // Real bug fixed here (present since the M57 split): the // coordinator's default redirect tab is `sanitize_key($raw_tab)`, // i.e. literally "ai_bots" — not a real tab slug, so without this // override every llms.txt save bounced the user to the Dashboard // instead of staying on the llms.txt tab. return array( 'tab' => 'llms' ); } // llms_takeover $args = array( 'tab' => 'llms' ); if ( ! $this->handle_llms_takeover() ) { $args['takeover_error'] = '1'; } return $args; } public function render(): void { if ( null === $this->ai_bots ) { return; } $settings = $this->ai_bots->get(); $ai_fields = $this->ai_enabled(); echo '

' . esc_html__( 'The llms.txt file (llmstxt.org standard) is a concise site guide for language models: who you are and which content matters most.', 'acez-seo' ) . wp_kses( HelpIcon::render( 'llms_txt' ), KsesTags::help_icon() ) . '

'; $this->ui->card_open( __( 'llms.txt and AI bots', 'acez-seo' ), 'llms', '', 'llms-txt' ); echo '

' . esc_html__( 'Generated automatically from the organization name, site description, main pages, and recent content (posts/pages/post types with noindex — including noindex set by another active SEO plugin — are skipped).', 'acez-seo' ) . '

'; if ( LlmsTxtProvider::has_physical_file() ) { $this->render_physical_file_notice(); } if ( (bool) ( $this->global_settings->get()['hide_from_search'] ?? false ) ) { echo '

' . esc_html__( 'The site is hidden from search engines (Settings → Visibility) — llms.txt will not be served while the lock is active.', 'acez-seo' ) . '

'; } $this->ui->form_open( 'ai_bots' ); echo ''; $this->ui->checkbox_field( 'llms_enabled', __( 'Serve the llms.txt file (a content guide for AI models)', 'acez-seo' ), (bool) $settings['llms_enabled'] ); $this->ui->text_field( 'llms_title', __( 'Custom title (optional)', 'acez-seo' ), (string) $settings['llms_title'], __( 'default: organization name, otherwise the site name', 'acez-seo' ), '', '', '', $ai_fields ? 'llms_title' : '' ); $this->ui->textarea_field( 'llms_description', __( 'Custom description (optional)', 'acez-seo' ), (string) $settings['llms_description'], __( 'default: site tagline (Settings → General)', 'acez-seo' ), '', $ai_fields ? 'llms_description' : '' ); $this->ui->textarea_field( 'llms_intro', __( 'Custom introduction (optional, markdown)', 'acez-seo' ), (string) $settings['llms_intro'], __( 'e.g. We write about SEO and WordPress. You will find the most up-to-date guides in the Blog section.', 'acez-seo' ), '', $ai_fields ? 'llms_intro' : '' ); $this->ui->checkbox_field( 'noai', __( 'Send the noai/noimageai signal (opt-out from AI training on your content)', 'acez-seo' ), (bool) $settings['noai'] ); echo '
'; $this->render_footer_field( (string) $settings['llms_footer'], $ai_fields ); echo '

' . esc_html__( 'Note: llms.txt and noai are opposite in intent — the first invites models to your content, the second objects to training. You can use both deliberately (e.g. "read and cite, but do not train"), but usually you pick one.', 'acez-seo' ) . '

'; $this->render_sitemap_opt_in_field( (bool) $settings['llms_sitemap_opt_in'] ); printf( '

%2$s

', esc_url( home_url( '/llms.txt' ) ), esc_html__( 'View the live /llms.txt ↗', 'acez-seo' ) ); submit_button(); $this->ui->form_close(); $this->ui->card_close(); } /** * M61 — llms.txt is noindex'd by default (LlmsTxtProvider always sends * `X-Robots-Tag: noindex` unless this is on): a text digest for LLM * crawlers is not meant to show up as a Google search result, and if it * contains full post content, listing it can trigger duplicate-content * flags. Explicit, informed opt-in only — same warning-then-checkbox * pattern as the visibility "danger zone" card. */ private function render_sitemap_opt_in_field( bool $opted_in ): void { echo '

'; esc_html_e( 'Warning: including llms.txt in your sitemap may lead to it being crawled and indexed by search engines. If the file contains full post content, this could trigger a duplicate-content issue. Enable only if you understand the SEO impact.', 'acez-seo' ); echo '

'; echo ''; $this->ui->checkbox_field( 'llms_sitemap_opt_in', __( 'I understand the SEO risks and want to include llms.txt in the sitemap', 'acez-seo' ), $opted_in, 'llms_sitemap_opt_in' ); echo '
'; } /** * Free since M69 Etap 1 (was Pro-gated in M57). WordPress.org's manual * review flagged license-gated local functionality as trialware * (Guideline 5) — and this cosmetic footer was never worth the cost of * migrating into the Pro addon: nobody buys a licence for a textarea * appended to the end of a file. Unlocking it closes that review point * at zero architectural cost and makes the free tier slightly better. */ private function render_footer_field( string $footer, bool $ai_fields = false ): void { echo ''; $this->ui->textarea_field( 'llms_footer', __( 'Footer (optional) — appended at the very end of the file', 'acez-seo' ), $footer, __( 'e.g. © 2026 Your Company. Content available under a license…', 'acez-seo' ), 'llms_footer', $ai_fields ? 'llms_footer' : '' ); echo '
'; } /** * Shown when a physical llms.txt exists (same doctrine as robots.txt, * M19): warning, a read-only preview, and — new in this milestone — a * one-click takeover that migrates the file's content into the plugin's * own "own intro" field and renames the file to a backup, so the * plugin's generator takes over. Deliberately no in-place editing of the * physical file (single source of truth stays with the plugin). */ private function render_physical_file_notice(): void { $path = ABSPATH . 'llms.txt'; if ( isset( $_GET['takeover_error'] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- presence-only check on a redirect flag this plugin sets itself after the nonce-verified save; the value is never read and only a static, translated notice is printed. { echo '

' . esc_html__( 'Failed to take over the llms.txt file — check PHP write permissions for the file and the site\'s root directory.', 'acez-seo' ) . '

'; } echo '

' . esc_html__( 'A physical llms.txt file exists in the site root — the server will serve it directly, so the generator below has no effect until the file is deleted or renamed.', 'acez-seo' ) . '

'; if ( ! is_readable( $path ) ) { echo '

' . esc_html__( 'The file exists, but PHP has no permission to read it — preview unavailable.', 'acez-seo' ) . '

'; return; } echo '
' . esc_html( (string) file_get_contents( $path ) ) . '
'; if ( PhysicalFileTakeover::can_take_over( $path ) ) { echo '

' . esc_html__( 'You can take over this file with one click: its content will be copied into the "Custom introduction" field below, the plugin will turn on serving llms.txt, and the file itself will be renamed to a backup (llms.txt.acez-backup). The operation is reversible: restore the file\'s old name to go back to the current state.', 'acez-seo' ) . '

'; $this->ui->form_open( 'llms_takeover' ); printf( '

', esc_attr__( 'Take over llms.txt? The file will be renamed to a backup, its content will be moved to the "Custom introduction" field, and the plugin will turn on serving llms.txt.', 'acez-seo' ), esc_html__( 'Take over the content and disable the file', 'acez-seo' ) ); $this->ui->form_close(); } else { echo '

' . esc_html__( 'Automatic takeover unavailable — PHP cannot write the file or the root directory. Rename the file manually (e.g. via FTP).', 'acez-seo' ) . '

'; } } /** * Migrates the physical llms.txt content into the "own intro" field * (merged after any existing intro, same append pattern as robots.txt's * custom_rules) and turns on serving — otherwise the take-over would * remove the physical file and leave llms.txt served by nobody at all. * Renames the file to a backup only after the content is safely stored. */ private function handle_llms_takeover(): bool { $path = ABSPATH . 'llms.txt'; if ( ! PhysicalFileTakeover::can_take_over( $path ) || null === $this->ai_bots ) { return false; } $content = PhysicalFileTakeover::read_content( $path ); if ( '' !== $content ) { $current = $this->ai_bots->get(); $existing = trim( (string) $current['llms_intro'] ); $this->ai_bots->update( array( 'llms_intro' => '' === $existing ? $content : $existing . "\n\n" . $content, 'llms_enabled' => true, ) ); } else { $this->ai_bots->update( array( 'llms_enabled' => true ) ); } return PhysicalFileTakeover::rename_to_backup( $path ); } }