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
2 weeks ago
list-trips.php
11 months ago
retrieve-booking.php
11 months ago
retrieve-coupon.php
2 weeks ago
retrieve-enquiry.php
2 months ago
update-booking-status.php
11 months ago
update-coupon.php
2 weeks ago
update-trip.php
6 months ago
create-trip.php
324 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CreateTrip. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category CreateTrip |
| 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 | * CreateTrip |
| 21 | * |
| 22 | * @category CreateTrip |
| 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 CreateTrip extends AutomateAction { |
| 30 | |
| 31 | /** |
| 32 | * Integration type. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | public $integration = 'WPTravelEngine'; |
| 37 | |
| 38 | /** |
| 39 | * Action name. |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | public $action = 'wpte_create_trip'; |
| 44 | |
| 45 | use SingletonLoader; |
| 46 | |
| 47 | /** |
| 48 | * Register a action. |
| 49 | * |
| 50 | * @param array $actions actions. |
| 51 | * @return array |
| 52 | */ |
| 53 | public function register( $actions ) { |
| 54 | $actions[ $this->integration ][ $this->action ] = [ |
| 55 | 'label' => __( 'Create a Trip', 'suretriggers' ), |
| 56 | 'action' => $this->action, |
| 57 | 'function' => [ $this, 'action_listener' ], |
| 58 | ]; |
| 59 | return $actions; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Action listener. |
| 64 | * |
| 65 | * @param int $user_id user_id. |
| 66 | * @param int $automation_id automation_id. |
| 67 | * @param array $fields fields. |
| 68 | * @param array $selected_options selectedOptions. |
| 69 | * @return array |
| 70 | */ |
| 71 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 72 | |
| 73 | if ( ! function_exists( 'wp_travel_engine_get_settings' ) ) { |
| 74 | return [ |
| 75 | 'status' => 'error', |
| 76 | 'message' => __( 'WP Travel Engine plugin is not active.', 'suretriggers' ), |
| 77 | |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | $trip_name = isset( $selected_options['trip_name'] ) ? sanitize_text_field( $selected_options['trip_name'] ) : ''; |
| 82 | $trip_description = isset( $selected_options['trip_description'] ) ? sanitize_textarea_field( $selected_options['trip_description'] ) : ''; |
| 83 | $trip_duration = isset( $selected_options['trip_duration'] ) ? intval( $selected_options['trip_duration'] ) : 1; |
| 84 | $trip_duration_night = isset( $selected_options['trip_duration_night'] ) ? intval( $selected_options['trip_duration_night'] ) : 0; |
| 85 | $trip_duration_unit = isset( $selected_options['trip_duration_unit'] ) ? sanitize_text_field( $selected_options['trip_duration_unit'] ) : 'days'; |
| 86 | $enable_cutoff_time = isset( $selected_options['enable_cutoff_time'] ) ? filter_var( $selected_options['enable_cutoff_time'], FILTER_VALIDATE_BOOLEAN ) : false; |
| 87 | $cutoff_time_value = isset( $selected_options['cutoff_time_value'] ) ? intval( $selected_options['cutoff_time_value'] ) : 0; |
| 88 | $cutoff_time_unit = isset( $selected_options['cutoff_time_unit'] ) ? sanitize_text_field( $selected_options['cutoff_time_unit'] ) : 'days'; |
| 89 | $min_max_age_enable = isset( $selected_options['set_minimum_maximum_age'] ) ? filter_var( $selected_options['set_minimum_maximum_age'], FILTER_VALIDATE_BOOLEAN ) : false; |
| 90 | $min_age = isset( $selected_options['min_age'] ) ? intval( $selected_options['min_age'] ) : 0; |
| 91 | $max_age = isset( $selected_options['max_age'] ) ? intval( $selected_options['max_age'] ) : 0; |
| 92 | $minmax_pax_enable = isset( $selected_options['set_minimum_maximum_participants'] ) ? filter_var( $selected_options['set_minimum_maximum_participants'], FILTER_VALIDATE_BOOLEAN ) : false; |
| 93 | $min_participants = isset( $selected_options['min_participants'] ) ? intval( $selected_options['min_participants'] ) : 0; |
| 94 | $max_participants = isset( $selected_options['max_participants'] ) ? intval( $selected_options['max_participants'] ) : 0; |
| 95 | $destination = isset( $selected_options['destination'] ) ? sanitize_text_field( $selected_options['destination'] ) : ''; |
| 96 | $activities = isset( $selected_options['activities'] ) ? array_filter( array_map( 'sanitize_text_field', explode( ',', $selected_options['activities'] ) ) ) : []; |
| 97 | $trip_highlights = isset( $selected_options['trip_highlights'] ) ? array_filter( array_map( 'sanitize_text_field', explode( "\n", $selected_options['trip_highlights'] ) ) ) : []; |
| 98 | $trip_includes = isset( $selected_options['trip_includes'] ) ? sanitize_textarea_field( $selected_options['trip_includes'] ) : ''; |
| 99 | $trip_excludes = isset( $selected_options['trip_excludes'] ) ? sanitize_textarea_field( $selected_options['trip_excludes'] ) : ''; |
| 100 | $featured_image_url = isset( $selected_options['featured_image_url'] ) ? esc_url_raw( $selected_options['featured_image_url'] ) : ''; |
| 101 | $status = ( isset( $selected_options['status'] ) && in_array( $selected_options['status'], [ 'publish', 'draft' ] ) ) ? $selected_options['status'] : 'publish'; |
| 102 | $trip_code = isset( $selected_options['trip_code'] ) ? sanitize_text_field( $selected_options['trip_code'] ) : ''; |
| 103 | $total_seats = isset( $selected_options['total_seats'] ) ? intval( $selected_options['total_seats'] ) : 0; |
| 104 | $trip_expiry_date = isset( $selected_options['trip_expiry_date'] ) ? sanitize_text_field( $selected_options['trip_expiry_date'] ) : ''; |
| 105 | $trip_tags = isset( $selected_options['trip_tag'] ) ? array_filter( array_map( 'sanitize_text_field', explode( ',', $selected_options['trip_tag'] ) ) ) : []; |
| 106 | $itinerary_input = isset( $selected_options['itinerary'] ) ? $selected_options['itinerary'] : ''; |
| 107 | |
| 108 | if ( empty( $trip_name ) ) { |
| 109 | return [ |
| 110 | 'status' => 'error', |
| 111 | 'message' => __( 'Trip name is required.', 'suretriggers' ), |
| 112 | |
| 113 | ]; |
| 114 | } |
| 115 | |
| 116 | $post_id = wp_insert_post( |
| 117 | [ |
| 118 | 'post_title' => $trip_name, |
| 119 | 'post_content' => $trip_description, |
| 120 | 'post_status' => $status, |
| 121 | 'post_type' => defined( 'WP_TRAVEL_ENGINE_POST_TYPE' ) ? WP_TRAVEL_ENGINE_POST_TYPE : 'trip', |
| 122 | 'post_author' => get_current_user_id(), |
| 123 | 'meta_input' => [ |
| 124 | 'trip_version' => '2.0.0', |
| 125 | 'wp_travel_engine_setting_trip_duration' => $trip_duration, |
| 126 | '_s_duration' => $trip_duration * ( 'days' === $trip_duration_unit ? 24 : 1 ), |
| 127 | 'wp_travel_engine_trip_min_age' => $min_age, |
| 128 | 'wp_travel_engine_trip_max_age' => $max_age, |
| 129 | 'wp_travel_engine_setting' => [ |
| 130 | 'trip_duration' => $trip_duration, |
| 131 | 'trip_duration_unit' => $trip_duration_unit, |
| 132 | 'trip_duration_nights' => $trip_duration_night, |
| 133 | 'trip_cutoff_enable' => $enable_cutoff_time, |
| 134 | 'trip_cut_off_time' => $cutoff_time_value, |
| 135 | 'trip_cut_off_unit' => $cutoff_time_unit, |
| 136 | 'min_max_age_enable' => $min_max_age_enable, |
| 137 | 'trip_minimum_pax' => $min_participants, |
| 138 | 'trip_maximum_pax' => $max_participants, |
| 139 | 'minmax_pax_enable' => $minmax_pax_enable, |
| 140 | 'overview_section_title' => 'Overview', |
| 141 | 'tab_content' => [ '1_wpeditor' => $trip_description ], |
| 142 | 'trip_highlights_title' => 'Trip Highlights', |
| 143 | 'trip_highlights' => array_map( |
| 144 | function( $h ) { |
| 145 | return [ 'highlight_text' => $h ]; }, |
| 146 | $trip_highlights |
| 147 | ), |
| 148 | 'cost_tab_sec_title' => 'Includes/Excludes', |
| 149 | 'cost' => [ |
| 150 | 'includes_title' => 'Cost Includes', |
| 151 | 'cost_includes' => $trip_includes, |
| 152 | 'excludes_title' => 'Cost Excludes', |
| 153 | 'cost_excludes' => $trip_excludes, |
| 154 | ], |
| 155 | 'trip_code' => $trip_code, |
| 156 | 'trip_expiry_date' => $trip_expiry_date, |
| 157 | ], |
| 158 | ], |
| 159 | ] |
| 160 | ); |
| 161 | |
| 162 | if ( empty( $post_id ) ) { |
| 163 | return [ |
| 164 | 'status' => 'error', |
| 165 | 'message' => __( 'Failed to create trip.', 'suretriggers' ), |
| 166 | |
| 167 | ]; |
| 168 | } |
| 169 | |
| 170 | // Update settings with total seats and itinerary if provided. |
| 171 | if ( ! empty( $total_seats ) || ! empty( $itinerary_input ) ) { |
| 172 | $settings = get_post_meta( $post_id, 'wp_travel_engine_setting', true ); |
| 173 | if ( ! is_array( $settings ) ) { |
| 174 | $settings = []; |
| 175 | } |
| 176 | |
| 177 | if ( ! empty( $total_seats ) ) { |
| 178 | $settings['trip_maximum_pax'] = $total_seats; |
| 179 | $settings['minmax_pax_enable'] = true; |
| 180 | } |
| 181 | |
| 182 | if ( ! empty( $itinerary_input ) ) { |
| 183 | $itinerary_data = json_decode( $itinerary_input, true ); |
| 184 | if ( is_array( $itinerary_data ) ) { |
| 185 | $itinerary_titles = []; |
| 186 | $itinerary_content = []; |
| 187 | foreach ( $itinerary_data as $index => $item ) { |
| 188 | $itinerary_titles[ $index ] = isset( $item['title'] ) ? sanitize_text_field( $item['title'] ) : ''; |
| 189 | $itinerary_content[ $index ] = isset( $item['content'] ) ? sanitize_textarea_field( $item['content'] ) : ''; |
| 190 | } |
| 191 | $settings['itinerary'] = [ |
| 192 | 'itinerary_title' => $itinerary_titles, |
| 193 | 'itinerary_content' => $itinerary_content, |
| 194 | ]; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | update_post_meta( $post_id, 'wp_travel_engine_setting', $settings ); |
| 199 | } |
| 200 | |
| 201 | if ( ! empty( $destination ) ) { |
| 202 | $term = term_exists( $destination, 'destination' ); |
| 203 | if ( ! $term ) { |
| 204 | $term = wp_insert_term( $destination, 'destination' ); |
| 205 | } |
| 206 | if ( ! is_wp_error( $term ) ) { |
| 207 | $term_id = 0; |
| 208 | if ( is_array( $term ) && isset( $term['term_id'] ) ) { |
| 209 | $term_id = intval( $term['term_id'] ); |
| 210 | } |
| 211 | if ( $term_id ) { |
| 212 | wp_set_object_terms( $post_id, $term_id, 'destination' ); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if ( ! empty( $activities ) ) { |
| 218 | foreach ( $activities as $activity ) { |
| 219 | $term = term_exists( $activity, 'activities' ); |
| 220 | if ( ! $term ) { |
| 221 | $term = wp_insert_term( $activity, 'activities' ); |
| 222 | } |
| 223 | if ( ! is_wp_error( $term ) ) { |
| 224 | $term_id = 0; |
| 225 | if ( is_array( $term ) && isset( $term['term_id'] ) ) { |
| 226 | $term_id = intval( $term['term_id'] ); |
| 227 | } |
| 228 | if ( $term_id ) { |
| 229 | wp_set_object_terms( $post_id, $term_id, 'activities', true ); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if ( ! empty( $trip_tags ) ) { |
| 236 | foreach ( $trip_tags as $tag ) { |
| 237 | $term = term_exists( trim( $tag ), 'trip_tag' ); |
| 238 | if ( ! $term ) { |
| 239 | $term = wp_insert_term( trim( $tag ), 'trip_tag' ); |
| 240 | } |
| 241 | if ( ! is_wp_error( $term ) ) { |
| 242 | $term_id = 0; |
| 243 | if ( is_array( $term ) && isset( $term['term_id'] ) ) { |
| 244 | $term_id = intval( $term['term_id'] ); |
| 245 | } |
| 246 | if ( $term_id ) { |
| 247 | wp_set_object_terms( $post_id, $term_id, 'trip_tag', true ); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if ( ! empty( $featured_image_url ) ) { |
| 254 | $this->set_featured_image_from_url( $post_id, $featured_image_url ); |
| 255 | } |
| 256 | |
| 257 | return [ |
| 258 | 'status' => 'success', |
| 259 | 'message' => sprintf( __( 'Trip created with ID %d', 'suretriggers' ), $post_id ), |
| 260 | 'trip_id' => $post_id, |
| 261 | ]; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Download image from URL and set as featured image. |
| 266 | * |
| 267 | * @param int $post_id Post ID. |
| 268 | * @param string $image_url Image URL. |
| 269 | * @return void |
| 270 | */ |
| 271 | private function set_featured_image_from_url( $post_id, $image_url ) { |
| 272 | if ( empty( $image_url ) || ! filter_var( $image_url, FILTER_VALIDATE_URL ) ) { |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | $abspath = realpath( ABSPATH ); |
| 277 | if ( false === $abspath ) { |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | require_once $abspath . DIRECTORY_SEPARATOR . 'wp-admin' . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'file.php'; |
| 282 | require_once $abspath . DIRECTORY_SEPARATOR . 'wp-admin' . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'media.php'; |
| 283 | require_once $abspath . DIRECTORY_SEPARATOR . 'wp-admin' . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'image.php'; |
| 284 | |
| 285 | $tmp = download_url( $image_url ); |
| 286 | |
| 287 | if ( is_wp_error( $tmp ) ) { |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | preg_match( '/[^\?]+\.(jpg|jpeg|png|gif)/i', $image_url, $matches ); |
| 292 | $file_array = [ |
| 293 | 'name' => ! empty( $matches ) ? basename( $matches[0] ) : 'featured-image.jpg', |
| 294 | 'tmp_name' => $tmp, |
| 295 | ]; |
| 296 | |
| 297 | $attach_id = media_handle_sideload( $file_array, $post_id ); |
| 298 | |
| 299 | if ( is_wp_error( $attach_id ) ) { |
| 300 | $tmp_file = $file_array['tmp_name']; |
| 301 | $uploads_dir = wp_upload_dir(); |
| 302 | |
| 303 | $tmp_file_realpath = realpath( $tmp_file ); |
| 304 | $uploads_base_realpath = realpath( $uploads_dir['basedir'] ); |
| 305 | |
| 306 | if ( |
| 307 | file_exists( $tmp_file ) && |
| 308 | false !== $tmp_file_realpath && |
| 309 | false !== $uploads_base_realpath && |
| 310 | 0 === strpos( $tmp_file_realpath, $uploads_base_realpath ) |
| 311 | ) { |
| 312 | wp_delete_file( $tmp_file ); |
| 313 | } |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | set_post_thumbnail( $post_id, $attach_id ); |
| 319 | } |
| 320 | |
| 321 | } |
| 322 | |
| 323 | CreateTrip::get_instance(); |
| 324 |