enableMap = $enableMap; } /** * Get module id. * * @return string The result. */ public function getId(): string { return 'robots'; } /** * Get human readable name. * * @return string The result. */ public function getName(): string { return __( 'Crawl Signals', 'rankkernel' ); } /** * Module priority. * * @return int The result. */ public function getPriority(): int { return 25; } /** * Dependencies. * * @return string[] The result. */ public function dependsOn(): array { return []; } /** * Whether the module is enabled. * * @return bool The result. */ public function isEnabled(): bool { if ( null !== $this->enabledCache ) { return $this->enabledCache; } if ( null !== $this->enableMap ) { $this->enabledCache = $this->enableMap->isEnabled( 'robots' ); return $this->enabledCache; } $map = get_option( 'rankkernel_modules', [] ); if ( ! is_array( $map ) ) { $map = []; } $this->enabledCache = array_key_exists( 'robots', $map ) ? (bool) $map['robots'] : in_array( 'robots', $map, true ); return $this->enabledCache; } /** * Register services. Nothing heavy is built here. */ public function register(): void { } /** * Boot hooks, only when enabled. */ public function boot(): void { if ( ! $this->isEnabled() ) { return; } $this->settings = new RobotsSettings(); $this->builder = new RobotsBuilder(); add_filter( 'robots_txt', [ $this, 'filterRobots' ], 5, 2 ); add_action( 'admin_notices', [ $this, 'renderPhysicalFileNotice' ] ); $this->llmsSettings = new LlmsSettings(); if ( (bool) $this->llmsSettings->get( 'enabled', false ) ) { $this->llmsWriter = new LlmsFileWriter(); $this->llmsRouter = new LlmsRouter( new LlmsGenerator(), $this->llmsSettings ); $this->llmsRouter->register(); add_action( 'save_post', [ $this, 'invalidateLlms' ] ); add_action( 'edited_terms', [ $this, 'invalidateLlms' ] ); } // The shadowing warning must show even when the feature is off: a // leftover physical file keeps serving ahead of the virtual route. add_action( 'admin_notices', [ $this, 'renderLlmsPhysicalNotice' ] ); $this->maybeFlushRules(); } /** * Invalidate the cached llms.txt document. */ public function invalidateLlms(): void { LlmsRouter::invalidate(); } /** * Warn that a physical llms.txt shadows the virtual route. * * The file is never overwritten. */ public function renderLlmsPhysicalNotice(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } if ( ! function_exists( 'get_current_screen' ) ) { return; } $screen = get_current_screen(); if ( ! is_object( $screen ) || ! property_exists( $screen, 'id' ) || ! str_contains( (string) $screen->id, 'rankkernel' ) ) { return; } $writer = $this->llmsWriter ?? new LlmsFileWriter(); if ( ! $writer->exists() ) { return; } echo '

'; echo esc_html__( 'A physical llms.txt file exists in the site root, so the virtual llms.txt route is not used. RankKernel will not overwrite it.', 'rankkernel' ); echo '

'; } /** * Get the llms.txt router, for testing. * * @return LlmsRouter|null The result. */ public function getLlmsRouter(): ?LlmsRouter { return $this->llmsRouter; } /** * Get the llms.txt file writer, for testing. * * @return LlmsFileWriter|null The result. */ public function getLlmsWriter(): ?LlmsFileWriter { return $this->llmsWriter; } /** * Filter the virtual robots.txt output. * * @param string $output Core robots.txt output. * @param mixed $isPublic Whether the site is public. * @return string The result. */ public function filterRobots( string $output, mixed $isPublic = true ): string { if ( ! (bool) $isPublic ) { return $output; } $settings = ( null !== $this->settings ) ? $this->settings : new RobotsSettings(); $builder = ( null !== $this->builder ) ? $this->builder : new RobotsBuilder(); $policies = CrawlerPolicy::sanitizeMap( $settings->get( 'crawlers', [] ) ); $override = (string) $settings->get( 'override', '' ); $text = $builder->build( $output, true, $policies, $override ); return $this->healExampleSitemap( $text ); } /** * Replace placeholder Sitemap lines with this site's real index URL. * * Saved overrides predate the dynamic preview and can carry the old * example.com placeholder, which would otherwise ship verbatim in * live robots.txt. Healing at render time fixes stored rows without * requiring a re-save. * * @param string $text Robots.txt document. * @return string The result. */ private function healExampleSitemap( string $text ): string { $healed = preg_replace( '/^Sitemap:\s*https?:\/\/example\.com\/.*$/mi', 'Sitemap: ' . Router::indexUrl(), $text ); return is_string( $healed ) ? $healed : $text; } /** * Warn that a physical robots.txt shadows the virtual editor. * * The file is never deleted. */ public function renderPhysicalFileNotice(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } if ( ! function_exists( 'get_current_screen' ) ) { return; } $screen = get_current_screen(); if ( ! is_object( $screen ) || ! property_exists( $screen, 'id' ) || ! str_contains( (string) $screen->id, 'rankkernel' ) ) { return; } if ( ! $this->hasPhysicalFile() ) { return; } echo '

'; echo esc_html__( 'A physical robots.txt file exists in the site root, so the server serves it before WordPress. The RankKernel robots settings have no effect while it is present. RankKernel will not delete it.', 'rankkernel' ); echo '

'; } /** * Whether a physical robots.txt exists at the site root. * * @return bool The result. */ public function hasPhysicalFile(): bool { $path = $this->physicalFilePath(); return '' !== $path && file_exists( $path ); } /** * Absolute path of the physical robots.txt, filterable for tests. * * @return string The result. */ private function physicalFilePath(): string { $default = defined( 'ABSPATH' ) ? ABSPATH . 'robots.txt' : ''; $path = apply_filters( 'rankkernel/robots/physical_file', $default ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- public hook name, part of the plugin API, must stay stable. if ( ! is_string( $path ) || '' === $path || $path === $default ) { return $default; } if ( ! class_exists( LlmsFileWriter::class ) ) { return $default; } // Reuse the llms writer containment check so a filtered path that // climbs outside the site root falls back instead of being trusted. $writer = new LlmsFileWriter(); $method = new \ReflectionMethod( $writer, 'containedPath' ); $method->setAccessible( true ); $contained = $method->invoke( $writer, $path, $default ); return is_string( $contained ) ? $contained : $default; } /** * Flush rewrite rules once per plugin version. * * Reserved for the llms.txt route that joins this module; harmless when * no rewrite rule is registered. */ private function maybeFlushRules(): void { $version = \RankKernel\Plugin::version(); $stored = get_option( 'rankkernel_robots_rules_version', '' ); if ( $version !== $stored ) { flush_rewrite_rules( false ); update_option( 'rankkernel_robots_rules_version', $version, false ); } } }