Skip to main content

Force the platform benchmark cache to refresh outside the daily cron tick

Free Intermediate Since v1.0.0

Benchmark data refreshes on a 24-hour cron cycle. Add-ons that need to drive a refresh in response to their own events can trigger one directly via the service container rather than waiting for the next tick. The refresh source label flows through to benecaster_benchmarks_fetch_skip, so staging installs can freeze the cache selectively while still allowing specific paths — such as a manual admin trigger — to go through. benecaster_benchmarks_refreshed only fires after a confirmed write; failed fetches never trigger it.

Code

<?php
add_filter( 'benecaster_benchmarks_fetch_skip', function ( bool $skip, string $source ): bool {
    // Freeze the cache on the staging install for snapshot review days,
    // but let the manual "Refresh now" admin button still go through.
    if ( 'cron' === $source && defined( 'BENECASTER_BENCHMARK_FREEZE' ) ) {
        return true;
    }
    return $skip;
}, 10, 2 );

add_action( 'benecaster_benchmarks_refreshed', function ( array $payload ): void {
    // Mirror the just-cached payload into a Slack channel for an at-a-glance daily check.
    if ( empty( $payload['benchmarks']['subscriber_count'] ) ) {
        return;
    }
    wp_remote_post( SLACK_WEBHOOK_URL, [
        'timeout'  => 5,
        'blocking' => false,
        'body'     => wp_json_encode( [ 'text' => 'Benchmarks refreshed — p50: ' . $payload['benchmarks']['subscriber_count']['p50'] ] ),
    ] );
} );

View on GitHub →

Hooks Used