Files

44 lines
1.8 KiB
PHP
Raw Permalink Normal View History

<?php
declare( strict_types=1 );
namespace StreamGoals\Overlay;
/**
* Overlay build id: plugin version + a manual "epoch". The overlay page embeds it, every overlay REST response
* repeats it in a header, and a page that sees a different value reloads itself (OBS never does that on its own).
*/
final class Build {
public const HEADER = 'X-SG-Build';
public const OPTION = 'sg_overlay_epoch';
/** Pure: the build id string for a version and epoch. Only [A-Za-z0-9._-] survive, so it is header- and attribute-safe. */
public static function id( string $version, mixed $epoch ): string {
$v = preg_replace( '/[^A-Za-z0-9._-]/', '', $version ) ?? '';
return ( '' === $v ? '0' : $v ) . '-' . self::epochInt( $epoch );
}
/** Pure: stored option value (anything) to a non-negative integer. */
public static function epochInt( mixed $raw ): int {
return ( is_int( $raw ) || ( is_string( $raw ) && ctype_digit( $raw ) && strlen( $raw ) < 15 ) ) ? max( 0, (int) $raw ) : 0;
}
/** Pure: the epoch after a bump. Strictly increasing, never lower than the clock so a deleted option cannot repeat an old value. */
public static function nextEpoch( mixed $current, int $now ): int {
return max( self::epochInt( $current ) + 1, $now );
}
/** Pure: response headers carrying the build id (merged into the existing ones; ETag semantics untouched). */
public static function headers( string $build ): array {
return array( self::HEADER => $build );
}
public static function current(): string {
return self::id( defined( 'STREAMGOALS_VERSION' ) ? (string) STREAMGOALS_VERSION : '', get_option( self::OPTION, 0 ) );
}
/** Reloads every open overlay at its next poll. */
public static function bump(): void {
update_option( self::OPTION, self::nextEpoch( get_option( self::OPTION, 0 ), time() ), false );
}
}