' . "\n"; if ( '' !== $settings['tdm_policy'] ) { echo '' . "\n"; } } public static function tdmrep_document( $settings ) { $rule = array( 'location' => self::home_path() . '*', 'tdm-reservation' => 1, ); if ( '' !== $settings['tdm_policy'] ) { $rule['tdm-policy'] = $settings['tdm_policy']; } return array( $rule ); } /** Path of the request relative to the site root, without a leading slash. */ public static function request_path() { $uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; $path = (string) wp_parse_url( $uri, PHP_URL_PATH ); $home = self::home_path(); if ( 0 === strpos( $path, $home ) ) { $path = substr( $path, strlen( $home ) ); } return ltrim( $path, '/' ); } public static function home_path() { $path = (string) wp_parse_url( home_url( '/' ), PHP_URL_PATH ); return '' === $path ? '/' : trailingslashit( $path ); } public static function maybe_serve_tdmrep() { $settings = Latchbot_Settings::get(); if ( empty( $settings['tdm'] ) || self::TDMREP_PATH !== self::request_path() ) { return; } $bot = Latchbot_Bots::match( self::user_agent() ); if ( '' !== $bot ) { Latchbot_Log::record( $bot, 'tdmrep.json', false ); } status_header( 200 ); header( 'Content-Type: application/json; charset=utf-8' ); header( 'Access-Control-Allow-Origin: *' ); echo wp_json_encode( self::tdmrep_document( $settings ), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ); exit; } /** Counts AI crawler visits and answers 403 to blocked ones; robots.txt always stays readable. */ public static function screen_request() { $bot = Latchbot_Bots::match( self::user_agent() ); if ( '' === $bot ) { return; } $settings = Latchbot_Settings::get(); $section = self::section(); $refuse = ! empty( $settings['refuse'] ) && 'robots.txt' !== $section && 'favicon' !== $section && 'block' === Latchbot_Settings::policy( $bot, $settings ); $refuse = (bool) apply_filters( 'latchbot_refuse', $refuse, $bot, $section ); Latchbot_Log::record( $bot, $section, $refuse ); if ( ! $refuse ) { return; } status_header( 403 ); nocache_headers(); header( 'Content-Type: text/plain; charset=utf-8' ); echo esc_html__( 'This site does not allow this crawler. See /robots.txt.', 'latchbot' ); exit; } /** A coarse content label instead of the URL, so the log never grows with the number of pages. */ public static function section() { if ( is_robots() ) { return 'robots.txt'; } if ( function_exists( 'is_favicon' ) && is_favicon() ) { return 'favicon'; } if ( is_feed() ) { return 'feed'; } if ( is_search() ) { return 'search'; } if ( is_404() ) { return '404'; } if ( is_front_page() || is_home() ) { return 'home'; } if ( is_attachment() ) { return 'attachment'; } if ( is_singular() ) { $type = sanitize_key( (string) get_post_type( get_queried_object_id() ) ); return '' === $type ? 'other' : substr( $type, 0, 40 ); } if ( is_archive() ) { return 'archive'; } return 'other'; } public static function user_agent() { return isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : ''; } }