$bot ) { if ( empty( $bot['token'] ) ) { continue; } $policy = Aeoora_Bots::policy( $id ); if ( 'disallow' === $policy || 'block' === $policy ) { $disallow[ $bot['token'] ] = true; } } $has_rules = false; if ( $disallow ) { foreach ( array_keys( $disallow ) as $token ) { $lines[] = 'User-agent: ' . $token; } $lines[] = 'Disallow: /'; $lines[] = ''; $has_rules = true; } if ( ! empty( $s['content_signals'] ) && $public ) { $lines[] = '# Content Signals (https://contentsignals.org): how AI may use this content.'; $lines[] = 'User-agent: *'; $lines[] = sprintf( 'Content-Signal: search=%s, ai-input=%s, ai-train=%s', 'no' === $s['signal_search'] ? 'no' : 'yes', 'no' === $s['signal_ai_input'] ? 'no' : 'yes', 'no' === $s['signal_ai_train'] ? 'no' : 'yes' ); $lines[] = 'Allow: /'; $lines[] = ''; $has_rules = true; } $custom = trim( (string) $s['custom'] ); if ( '' !== $custom ) { $lines[] = '# Custom rules'; foreach ( preg_split( '/\r\n|\r|\n/', $custom ) as $line ) { $lines[] = self::clean_line( $line ); } $lines[] = ''; $has_rules = true; } if ( ! empty( $s['llms_hint'] ) && Aeoora_Settings::get( 'llms', 'enabled', true ) ) { $lines[] = '# LLM-friendly site guide: ' . home_url( '/llms.txt' ); if ( Aeoora_Settings::get( 'llms', 'full_enabled', true ) ) { $lines[] = '# Full content for LLMs: ' . home_url( '/llms-full.txt' ); } $has_rules = true; } if ( $public && false === stripos( $existing, 'sitemap:' ) ) { $sitemap = Aeoora_Compat::sitemap_url(); if ( $sitemap ) { $lines[] = 'Sitemap: ' . $sitemap; $has_rules = true; } } if ( ! $has_rules ) { return ''; } $lines[] = self::END; return implode( "\n", $lines ); } /** * Remove control characters and HTML from a custom line. * * @param string $line Line. * @return string */ private static function clean_line( $line ) { $line = wp_strip_all_tags( (string) $line ); return trim( preg_replace( '/[\x00-\x1F\x7F]/', '', $line ) ); } /** * Remove an existing aeoora block (any case: 1.0.0 wrote "# BEGIN AEOORA"). * * @param string $content Content. * @return string */ public static function strip_block( $content ) { $pattern = '/\n*' . preg_quote( self::BEGIN, '/' ) . '.*?' . preg_quote( self::END, '/' ) . '\n*/si'; $result = preg_replace( $pattern, "\n", $content ); return null === $result ? $content : rtrim( $result ) . "\n"; } /** * Preview of the full virtual robots.txt (same code path as do_robots()). * * @return string */ public static function preview() { $public = (bool) get_option( 'blog_public' ); $output = "User-agent: *\n"; if ( ! $public ) { $output .= "Disallow: /\n"; } else { // WordPress's own default rules, with the real admin and AJAX addresses. $output .= 'Disallow: ' . (string) wp_parse_url( admin_url(), PHP_URL_PATH ) . "\n"; $output .= 'Allow: ' . (string) wp_parse_url( admin_url( 'admin-ajax.php' ), PHP_URL_PATH ) . "\n"; } return (string) apply_filters( 'robots_txt', $output, $public ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals } /** * Path of a physical robots.txt. * * @return string */ public static function physical_path() { return Aeoora_Util::root_path() . 'robots.txt'; } /** * May this site manage the physical robots.txt? On multisite the file is * shared by the whole network, so only the main site with network rights. * * @return bool */ public static function can_manage_physical() { if ( ! is_multisite() ) { return true; } return is_main_site() && ( current_user_can( 'manage_network_options' ) || wp_doing_cron() ); } /** * On subdirectory networks only the main site's robots.txt exists. * * @return bool */ public static function rules_not_served_here() { return is_multisite() && ! is_main_site() && ! is_subdomain_install(); } /** * Does a physical robots.txt exist (it overrides the virtual one)? * * @return bool */ public static function has_physical() { return file_exists( self::physical_path() ); } /** * Status info for the admin. * * @return array */ public static function physical_status() { $exists = self::has_physical(); $text = $exists ? (string) file_get_contents( self::physical_path() ) : ''; // phpcs:ignore WordPress.WP.AlternativeFunctions return array( 'exists' => $exists, 'writable' => self::can_manage_physical() && ( $exists ? wp_is_writable( self::physical_path() ) : wp_is_writable( Aeoora_Util::root_path() ) ), 'network' => is_multisite() && ! self::can_manage_physical(), 'managed' => false !== stripos( $text, self::BEGIN ), 'content' => $text, ); } /** * Write our block into the physical robots.txt (keeping everything else). * * @return true|WP_Error */ public static function sync_physical() { $path = self::physical_path(); if ( ! self::can_manage_physical() ) { return new WP_Error( 'aeoora_network_file', __( 'On a multisite network the physical robots.txt is shared by every site. Only a network administrator can sync it, from the main site.', 'aeoora' ) ); } if ( ! file_exists( $path ) ) { return new WP_Error( 'aeoora_no_file', __( 'There is no physical robots.txt file. WordPress serves a virtual one, which aeoora already manages.', 'aeoora' ) ); } if ( ! wp_is_writable( $path ) ) { return new WP_Error( 'aeoora_not_writable', __( 'robots.txt is not writable. Copy the aeoora rules into it manually.', 'aeoora' ) ); } $current = (string) file_get_contents( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( false === get_option( 'aeoora_robots_backup' ) ) { update_option( 'aeoora_robots_backup', $current, false ); } $base = self::strip_block( $current ); $block = self::build_block( $base, (bool) get_option( 'blog_public' ) ); $new = rtrim( $base ) . ( '' !== $block ? "\n\n" . $block . "\n" : "\n" ); $ok = file_put_contents( $path, $new, LOCK_EX ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( false === $ok ) { return new WP_Error( 'aeoora_write_failed', __( 'Could not write robots.txt.', 'aeoora' ) ); } update_option( 'aeoora_robots_physical_sync', 1, false ); Aeoora_Compat::purge_urls( array( home_url( '/robots.txt' ) ) ); return true; } /** * Keep a physical file in sync once the owner opted in. */ public static function maybe_sync_physical() { if ( ( get_option( 'aeoora_robots_physical_sync' ) || Aeoora_Settings::get( 'robots', 'auto_sync', false ) ) && self::has_physical() && ( ! is_multisite() || is_main_site() ) ) { self::sync_physical(); } Aeoora_Compat::purge_urls( array( home_url( '/robots.txt' ) ) ); } /** * Scheduled re-apply (setting robots.auto_sync, or after a manual sync): * when a physical robots.txt exists and aeoora's block is missing or out * of date (another plugin or a deploy rewrote the file), write it again. * The file's original content is backed up the first time. * * @return bool Whether the file was written. */ public static function auto_sync() { if ( ! Aeoora_Settings::get( 'robots', 'enabled', true ) || ! self::has_physical() || ! self::can_manage_physical() ) { return false; } if ( ! Aeoora_Settings::get( 'robots', 'auto_sync', false ) && ! get_option( 'aeoora_robots_physical_sync' ) ) { return false; } $current = (string) file_get_contents( self::physical_path() ); // phpcs:ignore WordPress.WP.AlternativeFunctions $base = self::strip_block( $current ); $block = self::build_block( $base, (bool) get_option( 'blog_public' ) ); $wanted = rtrim( $base ) . ( '' !== $block ? "\n\n" . $block . "\n" : "\n" ); if ( $wanted === $current ) { return false; } return true === self::sync_physical(); } /** * Remove our block from a physical file (on uninstall / opt-out). */ public static function unsync_physical() { delete_option( 'aeoora_robots_physical_sync' ); $path = self::physical_path(); if ( file_exists( $path ) && wp_is_writable( $path ) ) { $current = (string) file_get_contents( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( false !== stripos( $current, self::BEGIN ) ) { file_put_contents( $path, self::strip_block( $current ), LOCK_EX ); // phpcs:ignore WordPress.WP.AlternativeFunctions } } } /** * Minimal robots.txt evaluator: is `$path` allowed for `$token`? * Implements RFC 9309 group selection and longest-match precedence. * * @param string $robots Robots.txt content. * @param string $token User-agent product token. * @param string $path Path to test. * @return bool */ public static function is_allowed( $robots, $token, $path = '/' ) { $groups = array(); $current = null; $last_ua = false; foreach ( preg_split( '/\r\n|\r|\n/', (string) $robots ) as $line ) { $line = trim( preg_replace( '/#.*$/', '', $line ) ); if ( '' === $line || false === strpos( $line, ':' ) ) { continue; } list( $field, $value ) = array_map( 'trim', explode( ':', $line, 2 ) ); $field = strtolower( $field ); if ( 'user-agent' === $field ) { if ( ! $last_ua ) { $groups[] = array( 'agents' => array(), 'rules' => array(), ); $current = count( $groups ) - 1; } $groups[ $current ]['agents'][] = strtolower( $value ); $last_ua = true; } elseif ( in_array( $field, array( 'allow', 'disallow' ), true ) && null !== $current ) { $groups[ $current ]['rules'][] = array( $field, $value ); $last_ua = false; } else { $last_ua = false; } } $token = strtolower( $token ); $rules = array(); foreach ( $groups as $g ) { if ( in_array( $token, $g['agents'], true ) ) { $rules = array_merge( $rules, $g['rules'] ); } } if ( ! $rules ) { foreach ( $groups as $g ) { if ( in_array( '*', $g['agents'], true ) ) { $rules = array_merge( $rules, $g['rules'] ); } } } $best_len = -1; $allowed = true; foreach ( $rules as $rule ) { list( $type, $pattern ) = $rule; if ( '' === $pattern ) { continue; } $regex = '~^' . str_replace( array( '\*', '\$' ), array( '.*', '$' ), preg_quote( $pattern, '~' ) ) . '~'; if ( preg_match( $regex, $path ) ) { $len = strlen( $pattern ); if ( $len > $best_len || ( $len === $best_len && 'allow' === $type ) ) { $best_len = $len; $allowed = ( 'allow' === $type ); } } } return $allowed; } }