* The avatar cache class.
* Caches remote (e.g., Gravatar) avatars locally and rewrites URLs
* to serve cached copies with a TTL. Supports on-demand generation
* during page render and batch generation via cron.
defined( 'WPINC' ) || exit();
class Avatar extends Base {
const TYPE_GENERATE = 'generate';
* Avatar cache TTL (seconds).
private $_conf_cache_ttl;
* In-request map from original URL => rewritten URL to avoid duplicates.
* @var array<string,string>
private $_avatar_realtime_gen_dict = [];
* Summary/status data for last requests.
* @var array<string,mixed>
public function __construct() {
$this->_tb = Data::cls()->tb( 'avatar' );
if ( ! $this->conf( self::O_DISCUSS_AVATAR_CACHE ) ) {
self::debug2( '[Avatar] init' );
$this->_conf_cache_ttl = $this->conf( self::O_DISCUSS_AVATAR_CACHE_TTL );
add_filter( 'get_avatar_url', [ $this, 'crawl_avatar' ] );
$this->_summary = self::get_summary();
* Check whether DB table is needed.
public function need_db() {
return (bool) $this->conf( self::O_DISCUSS_AVATAR_CACHE );
* Serve static avatar by md5 (used by local static route).
* @param string $md5 MD5 hash of original avatar URL.
public function serve_static( $md5 ) {
self::debug( '[Avatar] is avatar request' );
if ( strlen( $md5 ) !== 32 ) {
self::debug( '[Avatar] wrong md5 ' . $md5 );
$url = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
'SELECT url FROM `' . $this->_tb . '` WHERE md5 = %s', // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
self::debug( '[Avatar] no matched url for md5 ' . $md5 );
$url = $this->_generate( $url );
wp_safe_redirect( $url );
* Localize/replace avatar URL with cached one (filter callback).
* @param string $url Original avatar URL.
* @return string Rewritten/cached avatar URL (or original).
public function crawl_avatar( $url ) {
// Check if already generated in this request.
if ( ! empty( $this->_avatar_realtime_gen_dict[ $url ] ) ) {
self::debug2( '[Avatar] already in dict [url] ' . $url );
return $this->_avatar_realtime_gen_dict[ $url ];
$realpath = $this->_realpath( $url );
$mtime = file_exists( $realpath ) ? filemtime( $realpath ) : false;
if ( $mtime && time() - (int) $mtime <= $this->_conf_cache_ttl ) {
self::debug2( '[Avatar] cache file exists [url] ' . $url );
return $this->_rewrite( $url, $mtime );
// Only handle gravatar or known remote avatar providers; keep generic check for "gravatar.com".
if ( strpos( $url, 'gravatar.com' ) === false ) {
if ( ! empty( $this->_summary['curr_request'] ) && time() - (int) $this->_summary['curr_request'] < 300 ) {
self::debug2( '[Avatar] Bypass generating due to interval limit [url] ' . $url );
// Generate immediately and track for this request.
$this->_avatar_realtime_gen_dict[ $url ] = $this->_generate( $url );
return $this->_avatar_realtime_gen_dict[ $url ];
* Count queued avatars (expired ones) for cron.
public function queue_count() {
if ( ! Data::cls()->tb_exist( 'avatar' ) ) {
Data::cls()->tb_create( 'avatar' );
$cnt = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
'SELECT COUNT(*) FROM `' . $this->_tb . '` WHERE dateline < %d', // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
time() - $this->_conf_cache_ttl
* Build final local URL for cached avatar.
* @param string $url Original URL.
* @param int|null $time Optional filemtime for cache busting.
* @return string Local URL.
private function _rewrite( $url, $time = null ) {
$qs = $time ? '?ver=' . $time : '';
return LITESPEED_STATIC_URL . '/avatar/' . $this->_filepath( $url ) . $qs;
* Generate filesystem realpath for cache file.
* @param string $url Original URL.
* @return string Absolute filesystem path.
private function _realpath( $url ) {
return LITESPEED_STATIC_DIR . '/avatar/' . $this->_filepath( $url );
* Get relative filepath for cached avatar.
* @param string $url Original URL.
* @return string Relative path under avatar/ (may include blog id).
private function _filepath( $url ) {
$filename = md5( $url ) . '.jpg';
$filename = get_current_blog_id() . '/' . $filename;
* Cron generation for expired avatars.
* @param bool $force Bypass throttle.
public static function cron( $force = false ) {
$_instance = self::cls();
if ( ! $_instance->queue_count() ) {
self::debug( '[Avatar] no queue' );
// For cron, need to check request interval too.
if ( ! empty( $_instance->_summary['curr_request'] ) && time() - (int) $_instance->_summary['curr_request'] < 300 ) {
self::debug( '[Avatar] curr_request too close' );
$list = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
'SELECT url FROM `' . $_instance->_tb . '` WHERE dateline < %d ORDER BY id DESC LIMIT %d', // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
time() - $_instance->_conf_cache_ttl,
(int) apply_filters( 'litespeed_avatar_limit', 30 )
self::debug( '[Avatar] cron job [count] ' . ( $list ? count( $list ) : 0 ) );
foreach ( $list as $v ) {
self::debug( '[Avatar] cron job [url] ' . $v->url );
$_instance->_generate( $v->url );
* Download and store the avatar locally, then update DB row.
* @param string $url Original avatar URL.
* @return string Rewritten local URL (fallback to original on failure).
private function _generate( $url ) {
$file = $this->_realpath( $url );
'curr_request' => time(),
// Ensure cache directory exists
$this->_maybe_mk_cache_folder( 'avatar' );
$response = wp_safe_remote_get(
self::debug( '[Avatar] _generate [url] ' . $url );
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
if ( file_exists( $file ) ) {
self::debug( '[Avatar] failed to get: ' . $error_message );
'last_spent' => time() - (int) $this->_summary['curr_request'],
'last_request' => $this->_summary['curr_request'],
// Update/insert DB record
$existed = $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
'UPDATE `' . $this->_tb . '` SET dateline = %d WHERE md5 = %s', // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
'INSERT INTO `' . $this->_tb . '` (url, md5, dateline) VALUES (%s, %s, %d)', // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
self::debug( '[Avatar] saved avatar ' . $file );
return $this->_rewrite( $url );
* Handle all request actions from main cls.
public function handler() {
$type = Router::verify_type();
case self::TYPE_GENERATE: