'GET', 'callback' => array(__CLASS__, 'status'), 'permission_callback' => array(__CLASS__, 'can_manage')), array('methods' => 'POST', 'callback' => array(__CLASS__, 'write'), 'permission_callback' => array(__CLASS__, 'can_manage')), array('methods' => 'DELETE', 'callback' => array(__CLASS__, 'remove'), 'permission_callback' => array(__CLASS__, 'can_manage')), )); } /** A file at the site root is a site setting, so it needs an administrator, not an editor. */ public static function can_manage() { return current_user_can('manage_options'); } private static function content() { $content = get_option(self::OPTION, ''); return is_string($content) ? $content : ''; } private static function at_domain_root() { $path = wp_parse_url(home_url('/'), PHP_URL_PATH); return !is_string($path) || trim($path, '/') === ''; } /** A real llms.txt file is served by the web server before WordPress runs, and it is never replaced. */ private static function physical_file() { if (!function_exists('get_home_path')) { require_once ABSPATH . 'wp-admin/includes/file.php'; } return file_exists(trailingslashit(get_home_path()) . 'llms.txt'); } /** Why /llms.txt cannot be served from here, or null when it can. */ private static function blocker() { if (!self::at_domain_root()) { return is_multisite() && !is_main_site() ? 'multisite_subfolder' : 'not_domain_root'; } if (self::physical_file()) { return 'physical_file'; } // Without pretty permalinks the web server answers /llms.txt itself, so WordPress never sees the request. if (!get_option('permalink_structure')) { return 'permalinks_off'; } return null; } private static function blocker_message($blocker) { switch ($blocker) { case 'physical_file': return __('The site already has an llms.txt file, so it was left as it is.', 'agents24x7'); case 'multisite_subfolder': return __('This site is a subfolder of a multisite network, and llms.txt at the domain root belongs to the main site.', 'agents24x7'); case 'not_domain_root': return __('WordPress runs in a subfolder of this domain, so it cannot answer at the domain root.', 'agents24x7'); case 'permalinks_off': return __('Permalinks are set to Plain, so WordPress cannot answer at /llms.txt.', 'agents24x7'); } return ''; } public static function status() { $content = self::content(); $blocker = self::blocker(); return array( 'pluginVersion' => AGENTS24X7_VERSION, 'url' => home_url('/llms.txt'), 'managed' => $content !== '', 'digest' => $content === '' ? null : hash('sha256', $content), 'blocker' => $blocker, 'blockerMessage' => $blocker ? self::blocker_message($blocker) : null, 'multisite' => is_multisite(), 'permalinks' => (bool) get_option('permalink_structure'), ); } /** Plain text with Unix line ends and no control characters but tabs and newlines. */ private static function clean($content) { $content = str_replace(array("\r\n", "\r"), "\n", $content); return preg_replace('/[\x00-\x08\x0B-\x1F\x7F]/', '', $content); } public static function write($request) { $content = $request->get_param('content'); if (!is_string($content) || trim($content) === '' || strlen($content) > self::MAX_BYTES) { return new WP_Error('llms_txt_invalid', esc_html__('llms.txt needs plain text under 100 KB.', 'agents24x7'), array('status' => 400)); } $blocker = self::blocker(); if ($blocker) { return new WP_Error('llms_txt_' . $blocker, esc_html(self::blocker_message($blocker)), array('status' => 409)); } $content = self::clean($content); $previous = self::content(); update_option(self::OPTION, $content, false); return array( 'written' => true, 'url' => home_url('/llms.txt'), 'digest' => hash('sha256', $content), 'previousDigest' => $previous === '' ? null : hash('sha256', $previous), ); } public static function remove() { $had = self::content() !== ''; delete_option(self::OPTION); return array('removed' => $had); } /** The text to answer a request path with, or null to leave the request to WordPress. */ public static function body_for($path) { if ($path !== '/llms.txt') { return null; } $content = self::content(); if ($content === '' || !self::at_domain_root() || self::physical_file()) { return null; } return $content; } public static function serve($request) { $content = self::body_for(wp_parse_url(wp_unslash($_SERVER['REQUEST_URI'] ?? ''), PHP_URL_PATH)); if ($content === null) { return; } if (!headers_sent()) { status_header(200); header('Content-Type: text/plain; charset=utf-8'); header('Cache-Control: public, max-age=3600'); header('X-Content-Type-Options: nosniff'); } echo $content; exit; } }