Helper.php
108 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\WooCommerce\WooCommerceBookings; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\WP\Functions; |
| 9 | |
| 10 | class Helper { |
| 11 | |
| 12 | private Functions $wp; |
| 13 | |
| 14 | public function __construct( |
| 15 | Functions $wp |
| 16 | ) { |
| 17 | $this->wp = $wp; |
| 18 | } |
| 19 | |
| 20 | public function isWooCommerceBookingsActive(): bool { |
| 21 | return $this->wp->isPluginActive('woocommerce-bookings/woocommerce-bookings.php'); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Returns all booking statuses keyed by status with their labels. |
| 26 | * |
| 27 | * WooCommerce Bookings splits its statuses across several "contexts" and none of them |
| 28 | * exposes every status, so we merge them. We also add the cart statuses, which Bookings |
| 29 | * registers as post statuses but leaves out of the contexts above (in particular the |
| 30 | * internal "was-in-cart" status that the abandoned booking automation relies on). |
| 31 | * |
| 32 | * @return array<string, string> |
| 33 | */ |
| 34 | public function getBookingStatuses(): array { |
| 35 | if (!function_exists('get_wc_booking_statuses')) { |
| 36 | return []; |
| 37 | } |
| 38 | |
| 39 | $statuses = []; |
| 40 | foreach (['fully_booked', 'user', 'cancel', 'scheduled'] as $context) { |
| 41 | foreach (get_wc_booking_statuses($context, true) as $status => $label) { |
| 42 | $statuses[$status] = $label; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | foreach (['in-cart', 'was-in-cart'] as $cartStatus) { |
| 47 | if (isset($statuses[$cartStatus])) { |
| 48 | continue; |
| 49 | } |
| 50 | $object = $this->wp->getPostStatusObject($cartStatus); |
| 51 | if (!$object) { |
| 52 | continue; |
| 53 | } |
| 54 | // WordPress falls back the label to the status key when a post status registers |
| 55 | // label => false (as Bookings does for was-in-cart), so humanize it in that case. |
| 56 | $label = $object->label ?? ''; |
| 57 | if (!is_string($label) || $label === '' || $label === $cartStatus) { |
| 58 | $label = ucwords(str_replace('-', ' ', $cartStatus)); |
| 59 | } |
| 60 | $statuses[$cartStatus] = $label; |
| 61 | } |
| 62 | |
| 63 | return $statuses; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @param int $id |
| 68 | * @return false|\WC_Booking |
| 69 | */ |
| 70 | public function getBooking(int $id) { |
| 71 | if (!function_exists('get_wc_booking')) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | return get_wc_booking($id); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Counts a customer's bookings created within the last $seconds, excluding one booking id |
| 80 | * (typically the booking that triggered the automation) and cancelled/in-cart bookings. |
| 81 | * |
| 82 | * Used to detect whether a customer has booked again, e.g. before sending a rebooking nudge. |
| 83 | */ |
| 84 | public function countRecentCustomerBookings(int $customerId, int $seconds, int $excludeBookingId = 0): int { |
| 85 | if ($customerId <= 0 || $seconds <= 0 || !class_exists(\WC_Booking_Data_Store::class)) { |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | $threshold = time() - $seconds; |
| 90 | $ignoredStatuses = ['cancelled', 'was-in-cart', 'in-cart']; |
| 91 | $count = 0; |
| 92 | foreach (\WC_Booking_Data_Store::get_bookings_for_user($customerId) as $booking) { |
| 93 | if (!$booking instanceof \WC_Booking || $booking->get_id() === $excludeBookingId) { |
| 94 | continue; |
| 95 | } |
| 96 | if (in_array($booking->get_status(), $ignoredStatuses, true)) { |
| 97 | continue; |
| 98 | } |
| 99 | $created = $booking->get_date_created(); |
| 100 | if ($created && $created->getTimestamp() >= $threshold) { |
| 101 | $count++; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return $count; |
| 106 | } |
| 107 | } |
| 108 |