$max_minutes ) { return false; } $began = get_transient( self::key( $user_id ) ); if ( false === $began ) { self::start( $user_id ); return false; } $began = (int) $began; if ( time() - $began <= $max_minutes * MINUTE_IN_SECONDS ) { // Still inside the limit. Reset the idle timer, but keep the // start time. set_transient( self::key( $user_id ), $began, self::IDLE ); return false; } // Out of time. End the call so the caller can call again. // The monthly plan limits total spend, not this setting. self::end( $user_id ); return true; } /** * When this caller's call must stop, as a Unix time. * * too_long() is checked once, when a turn starts. A turn can then run * for a long time (speech to text, model, tools, retry, speech). So the * turn checks this time again before each paid step. * * @param int $user_id Caller. * @param int $max_minutes From `voice.maxCallMinutes`. Zero turns it off. * @return int 0 when there is no limit or no open call. */ public static function ends_at( int $user_id, int $max_minutes ): int { if ( 1 > $max_minutes ) { return 0; } $began = get_transient( self::key( $user_id ) ); if ( false === $began ) { return 0; } return (int) $began + $max_minutes * MINUTE_IN_SECONDS; } /** * Forget this caller's call. * * @param int $user_id Caller. * @return void */ public static function end( int $user_id ): void { delete_transient( self::key( $user_id ) ); } /** * Transient name for one caller. * * @param int $user_id Caller. * @return string */ private static function key( int $user_id ): string { return self::PREFIX . $user_id; } }