roles; return mb_substr( $roles ? (string) reset( $roles ) : 'member', 0, 20 ); } /** * Whether the user agent looks like a bot. * * @return bool */ public static function is_bot() { $ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? strtolower( sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) ) : ''; return '' === $ua || (bool) preg_match( '/bot|crawl|spider|slurp|preview|headless|lighthouse|monitor|curl|wget|python-requests/', $ua ); } /** * Whether the current request should be counted. * * @return bool */ public static function countable() { if ( self::is_bot() ) { return false; } if ( isset( $_SERVER['HTTP_DNT'] ) && '1' === $_SERVER['HTTP_DNT'] ) { return false; } if ( current_user_can( 'edit_posts' ) && ! self::count_editors() ) { return false; } return true; } /** * Whether logged-in editors' visits are counted. * * @return bool */ public static function count_editors() { /** * Whether to count visits from logged-in editors (default: the analytics setting). * * @param bool $count Count. */ return (bool) apply_filters( 'kdocs_count_editor_views', (bool) Settings::get( 'analytics_count_editors', false ) ); } /** * Simple per-visitor rate limit for the collector. * * @param int $events Events in this request. * @return bool Allowed. */ public static function within_limit( $events ) { $key = 'kdocs_rl_' . self::visitor(); $count = (int) get_transient( $key ); if ( $count + $events > 300 ) { return false; } set_transient( $key, $count + $events, MINUTE_IN_SECONDS ); return true; } }