user_email ) ) ); $response = wp_remote_head( 'https://www.gravatar.com/avatar/' . $hash . '?d=404', [ 'timeout' => 3 ] ); // Erreur réseau : ne pas mettre en cache un faux négatif, retenter plus tard if ( is_wp_error( $response ) ) return; $url = wp_remote_retrieve_response_code( $response ) === 200 ? 'https://www.gravatar.com/avatar/' . $hash . '?s=300' : ''; set_transient( 'thalim_gravatar_' . $user_id, $url, WEEK_IN_SECONDS ); } add_action( 'thalim_warm_gravatar_user', 'thalim_refresh_gravatar_cache' ); // Cron quotidien : réchauffe le cache Gravatar des utilisateurs sans avatar // local, avant expiration des transients (1 semaine) — le premier rendu de // /membres ne paie plus jamais N requêtes HEAD de 3 s. add_action( 'init', function () { if ( ! wp_next_scheduled( 'thalim_warm_gravatar_cache' ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', 'thalim_warm_gravatar_cache' ); } } ); add_action( 'thalim_warm_gravatar_cache', function () { $users = get_users( [ 'fields' => 'ID' ] ); foreach ( $users as $user_id ) { $user_id = (int) $user_id; // Avatar local → pas besoin de Gravatar $meta = get_user_meta( $user_id, 'simple_local_avatar', true ); if ( is_array( $meta ) && ! empty( $meta['full'] ) ) continue; // Transient encore frais (posé il y a < 1 semaine) : on le rafraîchit // quand même s'il expire dans moins de 2 jours, sinon on saute. $timeout = (int) get_option( '_transient_timeout_thalim_gravatar_' . $user_id ); if ( $timeout && $timeout - time() > 2 * DAY_IN_SECONDS ) continue; thalim_refresh_gravatar_cache( $user_id ); } } );