renderer = $renderer ?: new IcsRenderer(); } public function create(Booking $booking): string { $offering = $booking->offering(); $session = $booking->getData()->getFirstSession(); if (!$offering || !$session || !strtotime($session)) return ''; $start = new DateTimeImmutable($session, new DateTimeZone('UTC')); $end = $start->add($offering->getInterval()); try { if ($end <= $start) $end = $start->add(new \DateInterval('PT1H')); } catch (\Throwable $e) { $end = $start->add(new \DateInterval('PT1H')); } $upload = wp_upload_dir(); $dir = trailingslashit($upload['basedir']) . 'daytidy-notifications'; if (!wp_mkdir_p($dir)) return ''; $filename = wp_unique_filename( $dir, 'daytidy-booking-' . $booking->id() . '-' . time() . '.ics' ); $path = trailingslashit($dir) . $filename; $offering_post = $offering->get(); $host = wp_parse_url(home_url('/'), PHP_URL_HOST); $uid = 'daytidy-booking-' . $booking->id() . '-' . $booking->refId() . '@' . ($host ?: 'daytidy'); $written = file_put_contents($path, $this->renderer->render([[ 'uid' => $uid, 'start' => $start, 'end' => $end, 'summary' => $offering_post ? $offering_post->post_title : '', 'description' => sprintf( // translators: Placeholder values. esc_html__('Booking Ref: %s', 'daytidy'), $booking->refId() ), 'url' => get_permalink($offering->id()), 'status' => 'CONFIRMED', ]], esc_html__('DayTidy Booking', 'daytidy'))); return $written ? $path : ''; } }