__( 'Booked', 'abogo-booking' ), 'start' => __( 'Arriving', 'abogo-booking' ), 'processing' => __( 'Out now', 'abogo-booking' ), 'end' => __( 'Just back', 'abogo-booking' ), 'completed' => __( 'Completed', 'abogo-booking' ), 'cancelled' => __( 'Cancelled', 'abogo-booking' ), ); } /** * Move every booking that the calendar has caught up with. * * @since 1.0.0 * @return int How many bookings moved. */ public static function sweep() { $moved = 0; foreach ( Repository::open() as $booking ) { $next = self::next_for( $booking ); if ( $next === $booking->status ) { continue; } Repository::update_status( (int) $booking->id, $next ); ++$moved; } return $moved; } /** * The furthest step a booking may reach today. * * Walking stops at the first step the shop moves by hand, so nothing ever * jumps over a step the shop wants to control. * * @since 1.0.0 * @param object $booking Booking row. * @return string */ public static function next_for( $booking ) { $today = new DateTimeImmutable( 'today', wp_timezone() ); $start = new DateTimeImmutable( $booking->start_date, wp_timezone() ); $end = new DateTimeImmutable( $booking->end_date, wp_timezone() ); $reached = 'pending'; $steps = array( array( 'start', $start->modify( '-' . (int) Settings::get( 'keep_start' ) . ' days' ), Settings::get( 'set_start' ) ), array( 'processing', $start, Settings::get( 'set_processing' ) ), array( 'end', $end->modify( '+1 day' ), Settings::get( 'set_end' ) ), array( 'completed', $end->modify( '+' . ( (int) Settings::get( 'keep_end' ) + 1 ) . ' days' ), Settings::get( 'set_completed' ) ), ); foreach ( $steps as $step ) { list( $status, $due, $how ) = $step; if ( $today < $due ) { break; } if ( 'automatic' !== $how ) { break; } $reached = $status; } // A booking already carried further by hand is never pulled back. return self::furthest( $reached, $booking->status ); } /** * Whether a status exists. * * @since 1.0.0 * @param string $status Status to check. * @return bool */ public static function exists( $status ) { return array_key_exists( $status, self::labels() ); } /** * The later of two steps. * * @since 1.0.0 * @param string $one First status. * @param string $another Second status. * @return string */ private static function furthest( $one, $another ) { $order = array_flip( self::LADDER ); if ( ! isset( $order[ $one ], $order[ $another ] ) ) { return $one; } return $order[ $another ] > $order[ $one ] ? $another : $one; } }