$chars * 0.6 ) { $cut = mb_substr( $cut, 0, $space ); } return rtrim( $cut, " ,.;:-" ) . '…'; } /** * Unicode-aware word count (CJK characters count as one word each). * * @param string $text Plain text. * @return int */ public static function word_count( $text ) { $text = (string) $text; $cjk = preg_match_all( '/[\x{4E00}-\x{9FFF}\x{3040}-\x{30FF}\x{AC00}-\x{D7AF}]/u', $text ); $text = preg_replace( '/[\x{4E00}-\x{9FFF}\x{3040}-\x{30FF}\x{AC00}-\x{D7AF}]/u', ' ', $text ); $words = preg_match_all( '/[\p{L}\p{N}][\p{L}\p{N}\'’\-]*/u', (string) $text ); return (int) $words + (int) $cjk; } /** * Escape a value for YAML frontmatter (double-quoted scalar). * * @param string $value Value. * @return string */ public static function yaml( $value ) { $value = self::one_line( $value ); return '"' . str_replace( array( '\\', '"' ), array( '\\\\', '\\"' ), $value ) . '"'; } /** * Is the request one we should never touch (admin, AJAX, cron, CLI, REST for our own beacon)? * * @return bool */ public static function is_backend_request() { if ( is_admin() || wp_doing_cron() || wp_doing_ajax() ) { return true; } if ( defined( 'WP_CLI' ) && WP_CLI ) { return true; } if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) { return false; } return false; } /** * Is this one of our own internal loopback renders? * * @return bool */ public static function is_internal_render() { if ( empty( $_SERVER['HTTP_X_AEOORA_RENDER'] ) ) { return false; } $sent = sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_AEOORA_RENDER'] ) ); // Signed and time-boxed, so a client cannot fake it to dodge blocks or logging. $window = (int) floor( time() / 300 ); return hash_equals( self::render_token( $window ), $sent ) || hash_equals( self::render_token( $window - 1 ), $sent ); } /** * Secret token for our own loopback requests (rotates every 5 minutes). * * @param int|null $window Time window. * @return string */ public static function render_token( $window = null ) { $window = null === $window ? (int) floor( time() / 300 ) : (int) $window; return hash_hmac( 'sha256', 'aeoora-render|' . $window, wp_salt( 'auth' ) ); } /** * Web root of the site (where robots.txt / llms.txt live). Differs from * ABSPATH when WordPress is installed in its own directory (Bedrock…). * * Derived from ABSPATH and the difference between the Site Address and the * WordPress Address, never from get_home_path(): that one reads * SCRIPT_FILENAME and returns "/" on front-end and REST requests when * WordPress lives in a subdirectory. * * @return string With trailing slash. */ public static function root_path() { static $cache = array(); $blog = get_current_blog_id(); if ( isset( $cache[ $blog ] ) ) { return $cache[ $blog ]; } if ( defined( 'AEOORA_ROOT_PATH' ) && is_dir( (string) AEOORA_ROOT_PATH ) ) { $root = (string) AEOORA_ROOT_PATH; } else { $root = self::root_from_abspath( ABSPATH, home_url(), site_url() ); } $root = (string) apply_filters( 'aeoora_root_path', $root ); if ( '' === $root || ! is_dir( $root ) ) { $root = ABSPATH; } $cache[ $blog ] = trailingslashit( wp_normalize_path( $root ) ); return $cache[ $blog ]; } /** * Web root from ABSPATH: when the WordPress Address is the Site Address * plus a subdirectory (https://example.com/wp), ABSPATH ends with that * subdirectory and the web root is its parent. * * @param string $abspath ABSPATH. * @param string $home Site Address (home_url()). * @param string $siteurl WordPress Address (site_url()). * @return string With trailing slash. */ public static function root_from_abspath( $abspath, $home, $siteurl ) { $abs = trailingslashit( str_replace( '\\', '/', (string) $abspath ) ); $home = trim( (string) wp_parse_url( (string) $home, PHP_URL_PATH ), '/' ); $site = trim( (string) wp_parse_url( (string) $siteurl, PHP_URL_PATH ), '/' ); if ( $home === $site ) { return $abs; } if ( '' === $home ) { $rel = $site; } elseif ( 0 === strpos( $site . '/', $home . '/' ) ) { $rel = substr( $site, strlen( $home ) + 1 ); } else { return $abs; } $suffix = '/' . trim( (string) $rel, '/' ) . '/'; if ( '//' === $suffix || strlen( $abs ) <= strlen( $suffix ) ) { return $abs; } if ( 0 === strcasecmp( substr( $abs, -strlen( $suffix ) ), $suffix ) ) { return substr( $abs, 0, strlen( $abs ) - strlen( $suffix ) + 1 ); } return $abs; } /** * Run a callback as a logged-out visitor, so access checks of membership * plugins (which often read the current user) answer for the public. * * @param callable $callback Callback. * @return mixed Callback result. */ public static function as_visitor( $callback ) { $prev = get_current_user_id(); if ( $prev ) { wp_set_current_user( 0 ); } try { return call_user_func( $callback ); } finally { if ( $prev ) { wp_set_current_user( $prev ); } } } /** * Take a named lock (add_option is atomic). A lock older than $ttl seconds * was left by a run that died and is taken over. * * @param string $name Lock name (without prefix). * @param int $ttl Seconds after which the lock is stale. * @return bool */ public static function lock( $name, $ttl = 600 ) { $option = 'aeoora_lock_' . $name; $now = time(); if ( add_option( $option, $now, '', false ) ) { return true; } $held = (int) get_option( $option, 0 ); if ( $held && $now - $held < $ttl ) { return false; } update_option( $option, $now, false ); return true; } /** * Release a named lock. * * @param string $name Lock name (without prefix). */ public static function unlock( $name ) { delete_option( 'aeoora_lock_' . $name ); } /** * Is the client address possibly a proxy's (a CDN or load balancer in front * of the site whose forwarding header the owner has not marked as trusted)? * * @return bool */ public static function behind_untrusted_proxy() { $trusted = (array) apply_filters( 'aeoora_trusted_proxy_headers', array() ); if ( defined( 'AEOORA_TRUST_CLOUDFLARE' ) && AEOORA_TRUST_CLOUDFLARE ) { $trusted[] = 'HTTP_CF_CONNECTING_IP'; } foreach ( $trusted as $header ) { if ( ! empty( $_SERVER[ $header ] ) ) { return false; } } foreach ( array( 'HTTP_X_FORWARDED_FOR', 'HTTP_CF_CONNECTING_IP', 'HTTP_X_REAL_IP', 'HTTP_TRUE_CLIENT_IP', 'HTTP_FASTLY_CLIENT_IP' ) as $header ) { if ( ! empty( $_SERVER[ $header ] ) ) { return true; } } return false; } /** * Multibyte-safe lowercase (falls back to strtolower without mbstring). * * @param string $text Text. * @return string */ public static function lower( $text ) { return function_exists( 'mb_strtolower' ) ? mb_strtolower( (string) $text, 'UTF-8' ) : strtolower( (string) $text ); } /** * Multibyte-safe uppercase (falls back to strtoupper without mbstring). * * @param string $text Text. * @return string */ public static function upper( $text ) { return function_exists( 'mb_strtoupper' ) ? mb_strtoupper( (string) $text, 'UTF-8' ) : strtoupper( (string) $text ); } /** * Case-insensitive "contains" (multibyte-safe when mbstring is present). * * @param string $haystack Haystack. * @param string $needle Needle. * @return bool */ public static function icontains( $haystack, $needle ) { if ( '' === (string) $needle ) { return false; } return function_exists( 'mb_stripos' ) ? false !== mb_stripos( (string) $haystack, (string) $needle, 0, 'UTF-8' ) : false !== stripos( (string) $haystack, (string) $needle ); } /** * Origin (scheme + host + port) of the site, for building absolute links from logged paths. * * @return string */ public static function origin() { $p = wp_parse_url( home_url( '/' ) ); return ( isset( $p['scheme'] ) ? $p['scheme'] : 'https' ) . '://' . ( isset( $p['host'] ) ? $p['host'] : '' ) . ( isset( $p['port'] ) ? ':' . $p['port'] : '' ); } /** * Arguments for loopback requests to this site. * * @param array $extra Extra args. * @return array */ public static function loopback_args( array $extra = array() ) { return array_merge( array( 'timeout' => 20, 'redirection' => 3, 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- core or third-party hook. 'user-agent' => 'aeoora/' . AEOORA_VERSION . ' (+https://aeoora.com; site audit)', 'headers' => array( 'X-Aeoora-Render' => self::render_token(), 'Cache-Control' => 'no-cache', ), ), $extra ); } /** * Upload subdirectory for generated files, created on demand. * * @param string $sub Optional sub folder. * @return array{dir:string,url:string}|null */ public static function upload_dir( $sub = '' ) { $uploads = wp_upload_dir( null, false ); if ( ! empty( $uploads['error'] ) ) { return null; } $dir = trailingslashit( $uploads['basedir'] ) . 'aeoora' . ( $sub ? '/' . $sub : '' ); $url = trailingslashit( $uploads['baseurl'] ) . 'aeoora' . ( $sub ? '/' . $sub : '' ); if ( ! is_dir( $dir ) && ! wp_mkdir_p( $dir ) ) { return null; } // An empty page keeps the folder from being listed (no PHP is ever // written to uploads). The files here are public: llms.txt copies, // llms.txt backups and crawler logos. $index = trailingslashit( $dir ) . 'index.html'; if ( ! file_exists( $index ) ) { @file_put_contents( $index, '' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors,WordPress.WP.AlternativeFunctions } $legacy = trailingslashit( $dir ) . 'index.php'; // Written by aeoora 1.0 and 1.1. if ( file_exists( $legacy ) ) { wp_delete_file( $legacy ); } return array( 'dir' => $dir, 'url' => $url, ); } /** * Current time in MySQL format, UTC. * * @return string */ public static function now() { return gmdate( 'Y-m-d H:i:s' ); } /** * Does the current user may use AI features? * * @return bool */ public static function can_use_ai() { $cap = Aeoora_Settings::get( 'ai', 'roles', 'publish_posts' ); return current_user_can( $cap ) || current_user_can( 'manage_options' ); } }