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
update-trip.php
409 lines
| 1 | <?php |
| 2 | /** |
| 3 | * UpdateTrip. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category UpdateTrip |
| 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 | * UpdateTrip |
| 21 | * |
| 22 | * @category UpdateTrip |
| 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 UpdateTrip 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_update_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' => __( 'Update an Existing 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 | $trip_id = isset( $selected_options['trip_id'] ) ? absint( $selected_options['trip_id'] ) : 0; |
| 81 | |
| 82 | if ( empty( $trip_id ) ) { |
| 83 | return [ |
| 84 | 'status' => 'error', |
| 85 | 'message' => __( 'Trip ID is required.', 'suretriggers' ), |
| 86 | ]; |
| 87 | } |
| 88 | |
| 89 | $trip_post_type = defined( 'WP_TRAVEL_ENGINE_POST_TYPE' ) ? WP_TRAVEL_ENGINE_POST_TYPE : 'trip'; |
| 90 | $existing_trip = get_post( $trip_id ); |
| 91 | |
| 92 | if ( empty( $existing_trip ) || $trip_post_type !== $existing_trip->post_type ) { |
| 93 | return [ |
| 94 | 'status' => 'error', |
| 95 | 'message' => __( 'Invalid trip ID. Trip not found.', 'suretriggers' ), |
| 96 | ]; |
| 97 | } |
| 98 | |
| 99 | // Update post fields if provided. |
| 100 | $post_update = []; |
| 101 | |
| 102 | if ( ! empty( $selected_options['trip_name'] ) ) { |
| 103 | $post_update['post_title'] = sanitize_text_field( $selected_options['trip_name'] ); |
| 104 | } |
| 105 | |
| 106 | if ( isset( $selected_options['trip_description'] ) && '' !== $selected_options['trip_description'] ) { |
| 107 | $post_update['post_content'] = sanitize_textarea_field( $selected_options['trip_description'] ); |
| 108 | } |
| 109 | |
| 110 | if ( ! empty( $selected_options['status'] ) && in_array( $selected_options['status'], [ 'publish', 'draft' ], true ) ) { |
| 111 | $post_update['post_status'] = $selected_options['status']; |
| 112 | } |
| 113 | |
| 114 | if ( ! empty( $post_update ) ) { |
| 115 | $post_update['ID'] = $trip_id; |
| 116 | $result = wp_update_post( $post_update, true ); |
| 117 | if ( is_wp_error( $result ) ) { |
| 118 | return [ |
| 119 | 'status' => 'error', |
| 120 | 'message' => $result->get_error_message(), |
| 121 | ]; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Update trip meta settings. |
| 126 | $existing_settings = get_post_meta( $trip_id, 'wp_travel_engine_setting', true ); |
| 127 | if ( ! is_array( $existing_settings ) ) { |
| 128 | $existing_settings = []; |
| 129 | } |
| 130 | |
| 131 | $meta_updated = false; |
| 132 | |
| 133 | if ( isset( $selected_options['trip_duration'] ) && '' !== $selected_options['trip_duration'] ) { |
| 134 | $trip_duration = intval( $selected_options['trip_duration'] ); |
| 135 | $existing_settings['trip_duration'] = $trip_duration; |
| 136 | update_post_meta( $trip_id, 'wp_travel_engine_setting_trip_duration', $trip_duration ); |
| 137 | |
| 138 | $trip_duration_unit = isset( $selected_options['trip_duration_unit'] ) ? sanitize_text_field( $selected_options['trip_duration_unit'] ) : 'days'; |
| 139 | $existing_settings['trip_duration_unit'] = $trip_duration_unit; |
| 140 | update_post_meta( $trip_id, '_s_duration', $trip_duration * ( 'days' === $trip_duration_unit ? 24 : 1 ) ); |
| 141 | $meta_updated = true; |
| 142 | } |
| 143 | |
| 144 | if ( isset( $selected_options['trip_duration_night'] ) && '' !== $selected_options['trip_duration_night'] ) { |
| 145 | $existing_settings['trip_duration_nights'] = intval( $selected_options['trip_duration_night'] ); |
| 146 | $meta_updated = true; |
| 147 | } |
| 148 | |
| 149 | if ( isset( $selected_options['enable_cutoff_time'] ) && '' !== $selected_options['enable_cutoff_time'] ) { |
| 150 | $existing_settings['trip_cutoff_enable'] = filter_var( $selected_options['enable_cutoff_time'], FILTER_VALIDATE_BOOLEAN ); |
| 151 | $meta_updated = true; |
| 152 | } |
| 153 | |
| 154 | if ( isset( $selected_options['cutoff_time_value'] ) && '' !== $selected_options['cutoff_time_value'] ) { |
| 155 | $existing_settings['trip_cut_off_time'] = intval( $selected_options['cutoff_time_value'] ); |
| 156 | $meta_updated = true; |
| 157 | } |
| 158 | |
| 159 | if ( isset( $selected_options['cutoff_time_unit'] ) && '' !== $selected_options['cutoff_time_unit'] ) { |
| 160 | $existing_settings['trip_cut_off_unit'] = sanitize_text_field( $selected_options['cutoff_time_unit'] ); |
| 161 | $meta_updated = true; |
| 162 | } |
| 163 | |
| 164 | if ( isset( $selected_options['set_minimum_maximum_age'] ) && '' !== $selected_options['set_minimum_maximum_age'] ) { |
| 165 | $existing_settings['min_max_age_enable'] = filter_var( $selected_options['set_minimum_maximum_age'], FILTER_VALIDATE_BOOLEAN ); |
| 166 | $meta_updated = true; |
| 167 | } |
| 168 | |
| 169 | if ( isset( $selected_options['min_age'] ) && '' !== $selected_options['min_age'] ) { |
| 170 | $min_age = intval( $selected_options['min_age'] ); |
| 171 | update_post_meta( $trip_id, 'wp_travel_engine_trip_min_age', $min_age ); |
| 172 | $meta_updated = true; |
| 173 | } |
| 174 | |
| 175 | if ( isset( $selected_options['max_age'] ) && '' !== $selected_options['max_age'] ) { |
| 176 | $max_age = intval( $selected_options['max_age'] ); |
| 177 | update_post_meta( $trip_id, 'wp_travel_engine_trip_max_age', $max_age ); |
| 178 | $meta_updated = true; |
| 179 | } |
| 180 | |
| 181 | if ( isset( $selected_options['set_minimum_maximum_participants'] ) && '' !== $selected_options['set_minimum_maximum_participants'] ) { |
| 182 | $existing_settings['minmax_pax_enable'] = filter_var( $selected_options['set_minimum_maximum_participants'], FILTER_VALIDATE_BOOLEAN ); |
| 183 | $meta_updated = true; |
| 184 | } |
| 185 | |
| 186 | if ( isset( $selected_options['min_participants'] ) && '' !== $selected_options['min_participants'] ) { |
| 187 | $existing_settings['trip_minimum_pax'] = intval( $selected_options['min_participants'] ); |
| 188 | $meta_updated = true; |
| 189 | } |
| 190 | |
| 191 | if ( isset( $selected_options['max_participants'] ) && '' !== $selected_options['max_participants'] ) { |
| 192 | $existing_settings['trip_maximum_pax'] = intval( $selected_options['max_participants'] ); |
| 193 | $meta_updated = true; |
| 194 | } |
| 195 | |
| 196 | if ( isset( $selected_options['trip_highlights'] ) && '' !== $selected_options['trip_highlights'] ) { |
| 197 | $trip_highlights = array_filter( array_map( 'sanitize_text_field', explode( "\n", $selected_options['trip_highlights'] ) ) ); |
| 198 | $existing_settings['trip_highlights'] = array_map( |
| 199 | function( $h ) { |
| 200 | return [ 'highlight_text' => $h ]; |
| 201 | }, |
| 202 | $trip_highlights |
| 203 | ); |
| 204 | $meta_updated = true; |
| 205 | } |
| 206 | |
| 207 | if ( isset( $selected_options['trip_includes'] ) && '' !== $selected_options['trip_includes'] ) { |
| 208 | if ( ! isset( $existing_settings['cost'] ) || ! is_array( $existing_settings['cost'] ) ) { |
| 209 | $existing_settings['cost'] = []; |
| 210 | } |
| 211 | $existing_settings['cost']['cost_includes'] = sanitize_textarea_field( $selected_options['trip_includes'] ); |
| 212 | $meta_updated = true; |
| 213 | } |
| 214 | |
| 215 | if ( isset( $selected_options['trip_excludes'] ) && '' !== $selected_options['trip_excludes'] ) { |
| 216 | if ( ! isset( $existing_settings['cost'] ) || ! is_array( $existing_settings['cost'] ) ) { |
| 217 | $existing_settings['cost'] = []; |
| 218 | } |
| 219 | $existing_settings['cost']['cost_excludes'] = sanitize_textarea_field( $selected_options['trip_excludes'] ); |
| 220 | $meta_updated = true; |
| 221 | } |
| 222 | |
| 223 | // Update trip code. |
| 224 | if ( isset( $selected_options['trip_code'] ) && '' !== $selected_options['trip_code'] ) { |
| 225 | $existing_settings['trip_code'] = sanitize_text_field( $selected_options['trip_code'] ); |
| 226 | $meta_updated = true; |
| 227 | } |
| 228 | |
| 229 | // Update total travellers seats. |
| 230 | if ( isset( $selected_options['total_seats'] ) && '' !== $selected_options['total_seats'] ) { |
| 231 | $existing_settings['trip_maximum_pax'] = intval( $selected_options['total_seats'] ); |
| 232 | $existing_settings['minmax_pax_enable'] = true; |
| 233 | $meta_updated = true; |
| 234 | } |
| 235 | |
| 236 | // Update trip expiry date. |
| 237 | if ( isset( $selected_options['trip_expiry_date'] ) && '' !== $selected_options['trip_expiry_date'] ) { |
| 238 | $existing_settings['trip_expiry_date'] = sanitize_text_field( $selected_options['trip_expiry_date'] ); |
| 239 | $meta_updated = true; |
| 240 | } |
| 241 | |
| 242 | // Update itinerary. |
| 243 | if ( isset( $selected_options['itinerary'] ) && '' !== $selected_options['itinerary'] ) { |
| 244 | $itinerary_input = $selected_options['itinerary']; |
| 245 | |
| 246 | // Handle both array and JSON string input. |
| 247 | if ( is_array( $itinerary_input ) ) { |
| 248 | $itinerary_data = $itinerary_input; |
| 249 | } else { |
| 250 | $itinerary_data = json_decode( $itinerary_input, true ); |
| 251 | |
| 252 | // If json_decode fails, try with stripslashes (handles escaped quotes like \"). |
| 253 | if ( null === $itinerary_data ) { |
| 254 | $itinerary_data = json_decode( stripslashes( $itinerary_input ), true ); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if ( is_array( $itinerary_data ) && ! empty( $itinerary_data ) ) { |
| 259 | $itinerary_titles = []; |
| 260 | $itinerary_content = []; |
| 261 | |
| 262 | foreach ( $itinerary_data as $index => $item ) { |
| 263 | $itinerary_titles[ $index ] = isset( $item['title'] ) ? sanitize_text_field( $item['title'] ) : ''; |
| 264 | $itinerary_content[ $index ] = isset( $item['content'] ) ? sanitize_textarea_field( $item['content'] ) : ''; |
| 265 | } |
| 266 | |
| 267 | $existing_settings['itinerary'] = [ |
| 268 | 'itinerary_title' => $itinerary_titles, |
| 269 | 'itinerary_content' => $itinerary_content, |
| 270 | ]; |
| 271 | $meta_updated = true; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if ( $meta_updated ) { |
| 276 | update_post_meta( $trip_id, 'wp_travel_engine_setting', $existing_settings ); |
| 277 | } |
| 278 | |
| 279 | // Update destination taxonomy. |
| 280 | if ( ! empty( $selected_options['destination'] ) ) { |
| 281 | $destination = sanitize_text_field( $selected_options['destination'] ); |
| 282 | $term = term_exists( $destination, 'destination' ); |
| 283 | if ( ! $term ) { |
| 284 | $term = wp_insert_term( $destination, 'destination' ); |
| 285 | } |
| 286 | if ( ! is_wp_error( $term ) ) { |
| 287 | $term_id = is_array( $term ) && isset( $term['term_id'] ) ? intval( $term['term_id'] ) : 0; |
| 288 | if ( $term_id ) { |
| 289 | wp_set_object_terms( $trip_id, $term_id, 'destination' ); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // Update activities taxonomy. |
| 295 | if ( ! empty( $selected_options['activities'] ) ) { |
| 296 | $activities = array_filter( array_map( 'sanitize_text_field', explode( ',', $selected_options['activities'] ) ) ); |
| 297 | $term_ids = []; |
| 298 | foreach ( $activities as $activity ) { |
| 299 | $term = term_exists( trim( $activity ), 'activities' ); |
| 300 | if ( ! $term ) { |
| 301 | $term = wp_insert_term( trim( $activity ), 'activities' ); |
| 302 | } |
| 303 | if ( ! is_wp_error( $term ) && is_array( $term ) && isset( $term['term_id'] ) ) { |
| 304 | $term_ids[] = intval( $term['term_id'] ); |
| 305 | } |
| 306 | } |
| 307 | if ( ! empty( $term_ids ) ) { |
| 308 | wp_set_object_terms( $trip_id, $term_ids, 'activities' ); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // Update trip tags taxonomy. |
| 313 | if ( ! empty( $selected_options['trip_tag'] ) ) { |
| 314 | $tags = array_filter( array_map( 'sanitize_text_field', explode( ',', $selected_options['trip_tag'] ) ) ); |
| 315 | $tag_ids = []; |
| 316 | foreach ( $tags as $tag ) { |
| 317 | $term = term_exists( trim( $tag ), 'trip_tag' ); |
| 318 | if ( ! $term ) { |
| 319 | $term = wp_insert_term( trim( $tag ), 'trip_tag' ); |
| 320 | } |
| 321 | if ( ! is_wp_error( $term ) && is_array( $term ) && isset( $term['term_id'] ) ) { |
| 322 | $tag_ids[] = intval( $term['term_id'] ); |
| 323 | } |
| 324 | } |
| 325 | if ( ! empty( $tag_ids ) ) { |
| 326 | wp_set_object_terms( $trip_id, $tag_ids, 'trip_tag' ); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | // Update featured image. |
| 331 | if ( ! empty( $selected_options['featured_image_url'] ) ) { |
| 332 | $featured_image_url = esc_url_raw( $selected_options['featured_image_url'] ); |
| 333 | $this->set_featured_image_from_url( $trip_id, $featured_image_url ); |
| 334 | } |
| 335 | |
| 336 | $updated_trip = get_post( $trip_id ); |
| 337 | |
| 338 | return [ |
| 339 | 'status' => 'success', |
| 340 | 'message' => sprintf( |
| 341 | /* translators: %d: trip ID */ |
| 342 | __( 'Trip updated successfully. Trip ID: %d', 'suretriggers' ), |
| 343 | $trip_id |
| 344 | ), |
| 345 | 'trip_id' => $trip_id, |
| 346 | 'trip_title' => $updated_trip instanceof \WP_Post ? $updated_trip->post_title : '', |
| 347 | 'trip_status' => $updated_trip instanceof \WP_Post ? $updated_trip->post_status : '', |
| 348 | ]; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Download image from URL and set as featured image. |
| 353 | * |
| 354 | * @param int $post_id Post ID. |
| 355 | * @param string $image_url Image URL. |
| 356 | * @return void |
| 357 | */ |
| 358 | private function set_featured_image_from_url( $post_id, $image_url ) { |
| 359 | if ( empty( $image_url ) || ! filter_var( $image_url, FILTER_VALIDATE_URL ) ) { |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | $abspath = realpath( ABSPATH ); |
| 364 | if ( false === $abspath ) { |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | require_once $abspath . DIRECTORY_SEPARATOR . 'wp-admin' . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'file.php'; |
| 369 | require_once $abspath . DIRECTORY_SEPARATOR . 'wp-admin' . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'media.php'; |
| 370 | require_once $abspath . DIRECTORY_SEPARATOR . 'wp-admin' . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'image.php'; |
| 371 | |
| 372 | $tmp = download_url( $image_url ); |
| 373 | |
| 374 | if ( is_wp_error( $tmp ) ) { |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | preg_match( '/[^\?]+\.(jpg|jpeg|png|gif)/i', $image_url, $matches ); |
| 379 | $file_array = [ |
| 380 | 'name' => ! empty( $matches ) ? basename( $matches[0] ) : 'featured-image.jpg', |
| 381 | 'tmp_name' => $tmp, |
| 382 | ]; |
| 383 | |
| 384 | $attach_id = media_handle_sideload( $file_array, $post_id ); |
| 385 | |
| 386 | if ( is_wp_error( $attach_id ) ) { |
| 387 | $tmp_file = $file_array['tmp_name']; |
| 388 | $uploads_dir = wp_upload_dir(); |
| 389 | |
| 390 | $tmp_file_realpath = realpath( $tmp_file ); |
| 391 | $uploads_base_realpath = realpath( $uploads_dir['basedir'] ); |
| 392 | |
| 393 | if ( |
| 394 | file_exists( $tmp_file ) && |
| 395 | false !== $tmp_file_realpath && |
| 396 | false !== $uploads_base_realpath && |
| 397 | 0 === strpos( $tmp_file_realpath, $uploads_base_realpath ) |
| 398 | ) { |
| 399 | wp_delete_file( $tmp_file ); |
| 400 | } |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | set_post_thumbnail( $post_id, $attach_id ); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | UpdateTrip::get_instance(); |
| 409 |