AI Crawlers screen. * * @package Latchbot */ defined( 'ABSPATH' ) || exit; /** The settings page, presets and the visit report. */ class Latchbot_Admin { const PAGE = 'latchbot'; public static function init() { add_action( 'admin_menu', array( __CLASS__, 'menu' ) ); add_action( 'admin_init', array( __CLASS__, 'register' ) ); add_action( 'admin_post_latchbot_preset', array( __CLASS__, 'handle_preset' ) ); add_action( 'admin_post_latchbot_clear', array( __CLASS__, 'handle_clear' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue' ) ); add_filter( 'plugin_action_links_' . plugin_basename( LATCHBOT_FILE ), array( __CLASS__, 'action_links' ) ); } public static function menu() { add_options_page( __( 'AI Crawlers', 'latchbot' ), __( 'AI Crawlers', 'latchbot' ), 'manage_options', self::PAGE, array( __CLASS__, 'render' ) ); } public static function register() { register_setting( 'latchbot', Latchbot_Settings::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( 'Latchbot_Settings', 'sanitize' ), 'default' => Latchbot_Settings::defaults(), ) ); if ( function_exists( 'wp_add_privacy_policy_content' ) ) { wp_add_privacy_policy_content( 'Latchbot', wp_kses_post( '
' . __( 'This site counts visits of known AI crawlers by the crawler name, the day and the kind of page visited. No IP address, page address, cookie or other visitor data is stored, and nothing is sent to a third party.', 'latchbot' ) . '
' ) ); } } public static function action_links( $links ) { array_unshift( $links, '' . esc_html__( 'Settings', 'latchbot' ) . '' ); return $links; } public static function enqueue( $hook ) { if ( 'settings_page_' . self::PAGE === $hook ) { wp_enqueue_style( 'latchbot-admin', plugins_url( 'assets/admin.css', LATCHBOT_FILE ), array(), LATCHBOT_VERSION ); } } public static function url( $args = array() ) { return add_query_arg( array_merge( array( 'page' => self::PAGE ), $args ), admin_url( 'options-general.php' ) ); } public static function handle_preset() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to change these settings.', 'latchbot' ), 403 ); } check_admin_referer( 'latchbot_preset' ); $preset = isset( $_POST['preset'] ) ? sanitize_key( wp_unslash( $_POST['preset'] ) ) : ''; if ( in_array( $preset, Latchbot_Settings::PRESETS, true ) ) { $settings = Latchbot_Settings::get(); $settings['policies'] = Latchbot_Settings::preset_policies( $preset ); Latchbot_Settings::update( $settings ); } wp_safe_redirect( self::url( array( 'latchbot_notice' => 'preset' ) ) ); exit; } public static function handle_clear() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to change these settings.', 'latchbot' ), 403 ); } check_admin_referer( 'latchbot_clear' ); Latchbot_Log::clear(); wp_safe_redirect( self::url( array( 'latchbot_notice' => 'cleared' ) ) ); exit; } /** Problems that make the rules ineffective, as plain sentences. */ public static function warnings( $settings ) { $warnings = array(); if ( file_exists( trailingslashit( self::site_root() ) . 'robots.txt' ) ) { $warnings[] = __( 'A physical robots.txt file exists in your site root, so the web server sends that file and WordPress never builds robots.txt. Copy the rules from "Your robots.txt additions" below into that file, or delete it.', 'latchbot' ); } if ( '/' !== Latchbot_Rules::home_path() ) { $warnings[] = __( 'WordPress runs in a subdirectory. Crawlers read robots.txt and /.well-known/tdmrep.json only at the root of the domain, so copy the rules there.', 'latchbot' ); } if ( ! get_option( 'permalink_structure' ) ) { $warnings[] = __( 'Plain permalinks are on, so most servers do not pass robots.txt and /.well-known/tdmrep.json to WordPress. Choose any other structure under Settings > Permalinks.', 'latchbot' ); } if ( ! empty( $settings['log'] ) && ( defined( 'WP_CACHE' ) && WP_CACHE ) ) { $warnings[] = __( 'A page cache is active. Cached pages are served without running WordPress, so the visit counts and the 403 answers cover only uncached requests.', 'latchbot' ); } return $warnings; } private static function site_root() { if ( ! function_exists( 'get_home_path' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } return get_home_path(); } public static function render() { if ( ! current_user_can( 'manage_options' ) ) { return; } Latchbot_Log::prune(); $settings = Latchbot_Settings::get(); $bots = Latchbot_Bots::all(); $visits = Latchbot_Log::by_bot( 30 ); $sections = Latchbot_Log::by_section( 30 ); $name = Latchbot_Settings::OPTION; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display-only notice after a redirect. $notice = isset( $_GET['latchbot_notice'] ) ? sanitize_key( wp_unslash( $_GET['latchbot_notice'] ) ) : ''; ?>