getTimestamp() ) )->setTimezone( $tz ); $end = $local->setTime( 23, 59, 59 )->modify( '+' . max( 0, (int) $days ) . ' days' ); return $end->setTimezone( new \DateTimeZone( 'UTC' ) ); } /** * Whether the window is still open. * * @param \DateTimeInterface|null $ends_at End, or null when the window has not started yet. * @param \DateTimeInterface $now Now. * @return bool */ public static function is_open( $ends_at, \DateTimeInterface $now ) { return null === $ends_at || $now->getTimestamp() <= $ends_at->getTimestamp(); } /** * Whole days left (0 on the last day, negative once closed). * * @param \DateTimeInterface $ends_at End. * @param \DateTimeInterface $now Now. * @return int */ public static function days_left( \DateTimeInterface $ends_at, \DateTimeInterface $now ) { $diff = $ends_at->getTimestamp() - $now->getTimestamp(); return (int) floor( $diff / 86400 ); } /** * When an order's window starts. An order that has not reached that point yet (not * completed, say) has not started its window: the customer may still ask, as the law lets a * buyer withdraw before the goods arrive. * * @param \WC_Order $order Order. * @param string $from completed|paid|created. * @return \DateTimeInterface|null */ public static function start_of( $order, $from ) { if ( 'created' === $from ) { return $order->get_date_created(); } if ( 'paid' === $from ) { return $order->get_date_paid() ? $order->get_date_paid() : null; } return $order->get_date_completed() ? $order->get_date_completed() : null; } /** * An order's window end with the shop's settings, or null when it has not started. * * @param \WC_Order $order Order. * @return \DateTimeImmutable|null */ public static function for_order( $order ) { $start = self::start_of( $order, (string) Settings::get( 'window_start' ) ); if ( ! $start ) { return null; } /** * The window length for one order, in days. * * @param int $days Days from the settings. * @param \WC_Order $order Order. */ $days = (int) apply_filters( 'returnwell_window_days', (int) Settings::get( 'window_days' ), $order ); return self::ends_at( $start, $days, wp_timezone() ); } }