create-coupon.php
11 months ago
create-trip.php
6 months ago
duplicate-trip.php
6 months ago
list-bookings.php
11 months ago
list-coupons.php
3 weeks ago
list-trips.php
11 months ago
retrieve-booking.php
11 months ago
retrieve-coupon.php
3 weeks ago
retrieve-enquiry.php
2 months ago
update-booking-status.php
11 months ago
update-coupon.php
3 weeks ago
update-trip.php
6 months ago
list-bookings.php
167 lines
| 1 | <?php |
| 2 | /** |
| 3 | * RetrieveBooking. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category RetrieveBooking |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\WPTravelEngine\Actions; |
| 15 | |
| 16 | use SureTriggers\Integrations\AutomateAction; |
| 17 | use SureTriggers\Traits\SingletonLoader; |
| 18 | |
| 19 | /** |
| 20 | * RetrieveBooking |
| 21 | * |
| 22 | * @category RetrieveBooking |
| 23 | * @package SureTriggers |
| 24 | * @author BSF <username@example.com> |
| 25 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 26 | * @link https://www.brainstormforce.com/ |
| 27 | * @since 1.0.0 |
| 28 | */ |
| 29 | class ListBookings extends AutomateAction { |
| 30 | |
| 31 | use SingletonLoader; |
| 32 | |
| 33 | /** |
| 34 | * Integration type. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $integration = 'WPTravelEngine'; |
| 39 | |
| 40 | /** |
| 41 | * Action name. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $action = 'wte_list_bookings'; |
| 46 | |
| 47 | /** |
| 48 | * Register the action. |
| 49 | * |
| 50 | * @param array $actions Actions array. |
| 51 | * @return array |
| 52 | */ |
| 53 | public function register( $actions ) { |
| 54 | $actions[ $this->integration ][ $this->action ] = [ |
| 55 | 'label' => __( 'List Bookings', 'suretriggers' ), |
| 56 | 'description' => __( 'Get a list of bookings with optional filtering', 'suretriggers' ), |
| 57 | 'action' => $this->action, |
| 58 | 'function' => [ $this, '_action_listener' ], |
| 59 | ]; |
| 60 | |
| 61 | return $actions; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Action listener. |
| 66 | * |
| 67 | * @param int $user_id User ID. |
| 68 | * @param int $automation_id Automation ID. |
| 69 | * @param array $fields Fields. |
| 70 | * @param array $selected_options Selected options. |
| 71 | * @return array |
| 72 | */ |
| 73 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 74 | if ( ! function_exists( 'wp_travel_engine_get_booking_status' ) ) { |
| 75 | return [ |
| 76 | 'status' => 'error', |
| 77 | 'message' => __( 'WP Travel Engine plugin is not active.', 'suretriggers' ), |
| 78 | |
| 79 | ]; |
| 80 | } |
| 81 | |
| 82 | $limit = ! empty( $selected_options['limit'] ) ? intval( $selected_options['limit'] ) : 20; |
| 83 | |
| 84 | $args = [ |
| 85 | 'post_type' => 'booking', |
| 86 | 'post_status' => 'publish', |
| 87 | 'posts_per_page' => $limit, |
| 88 | 'meta_query' => [], |
| 89 | ]; |
| 90 | |
| 91 | $bookings_query = new \WP_Query( $args ); |
| 92 | $bookings = []; |
| 93 | |
| 94 | if ( $bookings_query->have_posts() ) { |
| 95 | while ( $bookings_query->have_posts() ) { |
| 96 | $bookings_query->the_post(); |
| 97 | $booking_id = get_the_ID(); |
| 98 | |
| 99 | if ( ! $booking_id ) { |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | $booking_status = get_post_meta( $booking_id, 'wp_travel_engine_booking_status', true ); |
| 104 | |
| 105 | $cart_info_raw = get_post_meta( $booking_id, 'cart_info', true ); |
| 106 | $traveler_raw = get_post_meta( $booking_id, 'wptravelengine_travelers_details', true ); |
| 107 | |
| 108 | $cart_info = is_string( $cart_info_raw ) ? json_decode( wp_unslash( $cart_info_raw ), true ) : $cart_info_raw; |
| 109 | $traveler_data = is_string( $traveler_raw ) ? json_decode( wp_unslash( $traveler_raw ), true ) : $traveler_raw; |
| 110 | |
| 111 | if ( ! is_array( $cart_info ) ) { |
| 112 | $cart_info = []; |
| 113 | } |
| 114 | if ( ! is_array( $traveler_data ) ) { |
| 115 | $traveler_data = []; |
| 116 | } |
| 117 | |
| 118 | $trip_id = 0; |
| 119 | if ( isset( $cart_info['items'][0]['trip_id'] ) ) { |
| 120 | $trip_id = absint( $cart_info['items'][0]['trip_id'] ); |
| 121 | } |
| 122 | |
| 123 | $trip = $trip_id ? get_post( $trip_id ) : null; |
| 124 | |
| 125 | $fname = ''; |
| 126 | $lname = ''; |
| 127 | $email = ''; |
| 128 | if ( isset( $traveler_data[0] ) && is_array( $traveler_data[0] ) ) { |
| 129 | $fname = isset( $traveler_data[0]['fname'] ) ? sanitize_text_field( $traveler_data[0]['fname'] ) : ''; |
| 130 | $lname = isset( $traveler_data[0]['lname'] ) ? sanitize_text_field( $traveler_data[0]['lname'] ) : ''; |
| 131 | $email = isset( $traveler_data[0]['email'] ) ? sanitize_email( $traveler_data[0]['email'] ) : ''; |
| 132 | } |
| 133 | |
| 134 | $total_cost = isset( $cart_info['total'] ) ? floatval( $cart_info['total'] ) : 0; |
| 135 | $payment_status = isset( $cart_info['items'][0]['payment_status'] ) ? sanitize_text_field( $cart_info['items'][0]['payment_status'] ) : ''; |
| 136 | |
| 137 | if ( ! empty( $selected_options['trip_id'] ) && absint( $selected_options['trip_id'] ) !== $trip_id ) { |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | $bookings[] = [ |
| 142 | 'booking_id' => $booking_id, |
| 143 | 'booking_date' => get_the_date( 'Y-m-d H:i:s' ), |
| 144 | 'trip_id' => $trip_id, |
| 145 | 'trip_name' => $trip ? $trip->post_title : '', |
| 146 | 'customer_name' => trim( $fname . ' ' . $lname ), |
| 147 | 'customer_email' => $email, |
| 148 | 'total_cost' => $total_cost, |
| 149 | 'booking_status' => $booking_status, |
| 150 | 'payment_status' => $payment_status, |
| 151 | ]; |
| 152 | } |
| 153 | wp_reset_postdata(); |
| 154 | } |
| 155 | |
| 156 | return [ |
| 157 | 'status' => 'success', |
| 158 | 'data' => [ |
| 159 | 'bookings' => $bookings, |
| 160 | 'count' => count( $bookings ), |
| 161 | ], |
| 162 | ]; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | ListBookings::get_instance(); |
| 167 |