0 && ( time() - $held ) < $ttl ) { return false; } update_option( $option, (string) time(), false ); self::release_on_shutdown( $job_id ); return true; } /** * Give the lock back. * * @param string $job_id Job id. * @return void */ public static function release( string $job_id ): void { delete_option( self::option( $job_id ) ); } /** * Release the lock even if this request dies mid-step. * * @param string $job_id Job id. * @return void */ private static function release_on_shutdown( string $job_id ): void { register_shutdown_function( static function () use ( $job_id ) { self::release( $job_id ); } ); } /** * Option name for a job. * * @param string $job_id Job id. * @return string */ private static function option( string $job_id ): string { return self::PREFIX . preg_replace( '/[^A-Za-z0-9_.]/', '', $job_id ); } }