ui = $ui; $this->robots_txt = $robots_txt; } public function slug(): string { return 'robots'; } public function label(): string { return __( 'robots.txt', 'acez-seo' ); } public function icon(): string { return 'robots'; } public function enqueue(): void { wp_enqueue_script( 'acez-seo-robots-editor', ACEZ_SEO_URL . 'assets/js/robots-editor.js', array(), ACEZ_SEO_VERSION, true ); } public function handles_form_key( string $raw_tab ): bool { return in_array( sanitize_key( $raw_tab ), array( 'robots', 'robots_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 ( 'robots' === $tab_key ) { $this->robots_txt->update( array( 'blocked_bots' => isset( $_POST['blocked_bots'] ) ? array_map( 'sanitize_text_field', (array) wp_unslash( $_POST['blocked_bots'] ) ) : array(), 'custom_rules' => isset( $_POST['custom_rules'] ) ? TemplateSanitizer::sanitize_multiline( wp_strip_all_tags( wp_unslash( $_POST['custom_rules'] ) ) ) : '', ) ); return array(); } // robots_takeover $args = array( 'tab' => 'robots' ); if ( ! $this->handle_robots_takeover() ) { $args['takeover_error'] = '1'; } return $args; } public function render(): void { echo '

' . esc_html__( 'A graphical robots.txt editor: allow or block known search engine and AI bots with one click, plus room for your own rules.', 'acez-seo' ) . wp_kses( HelpIcon::render( 'robots_txt_bots' ), KsesTags::help_icon() ) . '

'; if ( RobotsTxtProvider::has_physical_file() ) { $this->render_physical_robots_notice(); return; } $settings = $this->robots_txt->get(); $this->ui->card_open( __( 'Known bots', 'acez-seo' ), 'robots', '', 'robots-known-bots' ); echo '
'; wp_nonce_field( FormControls::NONCE_ACTION ); echo ''; echo ''; echo ''; foreach ( KnownBots::all() as $bot ) { printf( '', esc_html( $bot['agent'] ), esc_attr( $bot['agent'] ), checked( in_array( $bot['agent'], (array) $settings['blocked_bots'], true ), true, false ), esc_html__( 'Block', 'acez-seo' ) . ' (' . esc_html( $bot['label'] ) . ')' ); } $this->ui->textarea_field( 'custom_rules', __( 'Custom rules (raw robots.txt text)', 'acez-seo' ), (string) $settings['custom_rules'], "User-agent: SomeBot\nDisallow: /private/", 'robots_txt_custom' ); echo '
%1$s
'; submit_button(); echo '
'; $this->ui->card_close(); $this->ui->card_open( __( 'Preview', 'acez-seo' ), 'robots', '', 'robots-preview' ); printf( '
%2$s
', esc_attr__( '(no additional rules)', 'acez-seo' ), esc_html( RobotsTxtBuilder::build( $settings ) ) ); printf( '

%2$s

', esc_url( home_url( '/robots.txt' ) ), esc_html__( 'View the current live /robots.txt ↗', 'acez-seo' ) ); $this->ui->card_close(); } /** * Shown instead of the editor when a physical robots.txt exists: warning, * a read-only preview (when readable) and a one-click takeover that copies * the content into the plugin's custom rules and renames the file to a * backup (reversible) — deliberately no in-place editing of the physical * file (single source of truth stays with the plugin). */ private function render_physical_robots_notice(): void { $path = ABSPATH . 'robots.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 robots.txt file — check PHP write permissions for the file and the site\'s root directory.', 'acez-seo' ) . '

'; } echo '

' . esc_html__( 'A physical robots.txt file exists in the site\'s root directory. The server then serves this file directly, bypassing WordPress — no plugin (including this one) can change it. Delete or rename this file so the editor can manage robots.txt.', '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; } $this->ui->card_open( __( 'Preview of the existing robots.txt file', 'acez-seo' ), 'robots', '', 'robots-file-preview' ); 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 editor\'s "Custom rules" field, and the file itself will be renamed to a backup (robots.txt.acez-backup) — from then on the plugin generates robots.txt. The operation is reversible: restore the file\'s old name to go back to the current state.', 'acez-seo' ) . '

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

', esc_attr__( 'Take over robots.txt? The file will be renamed to a backup and its content will be moved to the editor\'s "Custom rules" field.', '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' ) . '

'; } $this->ui->card_close(); } /** * Copies the physical robots.txt content into the plugin's custom rules * (content first — nothing is lost if the rename then fails) and renames * the file to a backup so the WP `robots_txt` filter takes over. */ private function handle_robots_takeover(): bool { $path = ABSPATH . 'robots.txt'; if ( ! PhysicalFileTakeover::can_take_over( $path ) ) { return false; } $content = PhysicalFileTakeover::read_content( $path ); if ( '' !== $content ) { // Full settings on purpose — RobotsTxtRepository::update() merges // with DEFAULTS, so a partial array would wipe blocked_bots. $settings = $this->robots_txt->get(); $existing = trim( (string) $settings['custom_rules'] ); $this->robots_txt->update( array( 'blocked_bots' => (array) $settings['blocked_bots'], 'custom_rules' => '' === $existing ? $content : $existing . "\n\n" . $content, ) ); } return PhysicalFileTakeover::rename_to_backup( $path ); } }