Publish eventLabs 0.5.0

This commit is contained in:
josh
2026-11-11 21:30:20 -05:00
parent 31a327d16f
commit 258aee939c
41 changed files with 6737 additions and 0 deletions

139
includes/class-schedule.php Normal file
View File

@@ -0,0 +1,139 @@
<?php
namespace BurnEvents;
use DateTimeImmutable;
use DateTimeZone;
use Generator;
use InvalidArgumentException;
defined( 'ABSPATH' ) || exit;
final class Schedule {
public const REPEATS = [ 'none', 'daily', 'weekly', 'monthly', 'yearly' ];
public const WEEKDAYS = [ 1 => 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ];
public static function validate( array $input, string $default_zone ): array {
$value = static fn( string $key ): string => is_scalar( $input[ $key ] ?? null ) ? trim( (string) $input[ $key ] ) : '';
$zone = $value( 'timezone' ) ?: $default_zone;
try {
$timezone = new DateTimeZone( $zone );
} catch ( \Exception $error ) {
throw new InvalidArgumentException( __( 'Choose a valid time zone.', 'burn-events' ) );
}
$all_day = ! empty( $input['all_day'] );
$start = self::parse( $value( 'start_date' ), $all_day ? '00:00' : $value( 'start_time' ), $timezone );
$end = self::parse( $value( 'end_date' ) ?: $value( 'start_date' ), $all_day ? '00:00' : $value( 'end_time' ), $timezone );
if ( $end < $start || ( ! $all_day && $end == $start ) ) {
throw new InvalidArgumentException( __( 'The end must be after the start.', 'burn-events' ) );
}
$repeat = $value( 'repeat' );
$until = $value( 'until' );
if ( $until && self::parse( $until, '00:00', $timezone )->format( 'Y-m-d' ) < $start->format( 'Y-m-d' ) ) {
throw new InvalidArgumentException( __( 'The recurrence end date must be on or after the event start.', 'burn-events' ) );
}
$weekdays = array_intersect( array_keys( self::WEEKDAYS ), array_map( 'intval', (array) ( $input['weekdays'] ?? [] ) ) );
return [
'start_date' => $start->format( 'Y-m-d' ),
'start_time' => $start->format( '00' === $start->format( 's' ) ? 'H:i' : 'H:i:s' ),
'end_date' => $end->format( 'Y-m-d' ),
'end_time' => $end->format( '00' === $end->format( 's' ) ? 'H:i' : 'H:i:s' ),
'timezone' => $timezone->getName(),
'all_day' => $all_day,
'repeat' => in_array( $repeat, self::REPEATS, true ) ? $repeat : 'none',
'interval' => max( 1, min( 365, (int) $value( 'interval' ) ) ),
'weekdays' => array_values( $weekdays ),
'monthly' => 'weekday' === $value( 'monthly' ) ? 'weekday' : 'day',
'count' => max( 0, min( 10000, (int) $value( 'count' ) ) ),
'until' => $until,
'include' => self::dates( $value( 'include' ), $timezone ),
'exclude' => self::dates( $value( 'exclude' ), $timezone ),
];
}
private static function parse( string $day, string $time, DateTimeZone $zone ): DateTimeImmutable {
$value = $day . ' ' . $time;
$format = 8 === strlen( $time ) ? 'Y-m-d H:i:s' : 'Y-m-d H:i';
$date = DateTimeImmutable::createFromFormat( '!' . $format, $value, $zone );
if ( ! $date || $date->format( $format ) !== $value ) {
throw new InvalidArgumentException( __( 'Enter a valid date and time for the selected time zone.', 'burn-events' ) );
}
return $date;
}
private static function dates( string $value, DateTimeZone $zone ): array {
$dates = preg_split( '/[\s,]+/', $value, -1, PREG_SPLIT_NO_EMPTY );
return array_values( array_unique( array_map( static fn( string $day ): string => self::parse( $day, '00:00', $zone )->format( 'Y-m-d' ), $dates ) ) );
}
public static function occurrences( array $rule, DateTimeImmutable $horizon ): Generator {
$zone = new DateTimeZone( $rule['timezone'] );
$start = self::parse( $rule['start_date'], $rule['start_time'], $zone );
$end = self::parse( $rule['end_date'], $rule['end_time'], $zone );
$span = (int) $start->setTime( 0, 0 )->diff( $end->setTime( 0, 0 ) )->days + (int) $rule['all_day'];
$anchor = $start->setTime( 0, 0 );
$limit = $horizon->setTimezone( $zone )->setTime( 0, 0 );
$dates = [ $rule['start_date'] => true ];
$generated = 1;
if ( 'none' !== $rule['repeat'] ) {
$until = $rule['until'] ? new DateTimeImmutable( $rule['until'], $zone ) : $limit;
$until = min( $limit, $until );
for ( $date = $anchor->modify( '+1 day' ); $date <= $until; $date = $date->modify( '+1 day' ) ) {
if ( $rule['count'] && $generated >= $rule['count'] ) {
break;
}
if ( self::matches( $rule, $anchor, $date ) ) {
$dates[ $date->format( 'Y-m-d' ) ] = true;
$generated++;
}
}
}
foreach ( $rule['include'] as $date ) {
$dates[ $date ] = true;
}
foreach ( $rule['exclude'] as $date ) {
unset( $dates[ $date ] );
}
ksort( $dates );
$utc = new DateTimeZone( 'UTC' );
foreach ( array_keys( $dates ) as $day ) {
$from = new DateTimeImmutable( $day . ' ' . $rule['start_time'], $zone );
$last_day = ( new DateTimeImmutable( $day, $zone ) )->modify( "+{$span} days" )->format( 'Y-m-d' );
$to = new DateTimeImmutable( $last_day . ' ' . $rule['end_time'], $zone );
if ( $to <= $from ) {
$to = $from->modify( '+' . max( 60, $end->getTimestamp() - $start->getTimestamp() ) . ' seconds' );
}
yield [ $from->setTimezone( $utc )->format( 'Y-m-d H:i:s' ), $to->setTimezone( $utc )->format( 'Y-m-d H:i:s' ) ];
}
}
private static function matches( array $rule, DateTimeImmutable $anchor, DateTimeImmutable $date ): bool {
$interval = $rule['interval'];
$days = (int) $anchor->diff( $date )->days;
switch ( $rule['repeat'] ) {
case 'daily':
return 0 === $days % $interval;
case 'weekly':
$week = intdiv( $days + (int) $anchor->format( 'N' ) - 1, 7 );
$weekdays = $rule['weekdays'] ?: [ (int) $anchor->format( 'N' ) ];
return 0 === $week % $interval && in_array( (int) $date->format( 'N' ), $weekdays, true );
case 'monthly':
$months = ( (int) $date->format( 'Y' ) - (int) $anchor->format( 'Y' ) ) * 12 + (int) $date->format( 'n' ) - (int) $anchor->format( 'n' );
if ( 0 !== $months % $interval ) {
return false;
}
return 'weekday' === $rule['monthly']
? $date->format( 'N' ) === $anchor->format( 'N' ) && intdiv( (int) $date->format( 'j' ) - 1, 7 ) === intdiv( (int) $anchor->format( 'j' ) - 1, 7 )
: $date->format( 'j' ) === $anchor->format( 'j' );
case 'yearly':
return 0 === ( (int) $date->format( 'Y' ) - (int) $anchor->format( 'Y' ) ) % $interval && $date->format( 'm-d' ) === $anchor->format( 'm-d' );
}
return false;
}
}