Total Complexity | 319 |
Total Lines | 1447 |
Duplicated Lines | 0 % |
Changes | 35 | ||
Bugs | 2 | Features | 0 |
Complex classes like LSX_WETU_Importer_Tours often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LSX_WETU_Importer_Tours, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class LSX_WETU_Importer_Tours extends LSX_WETU_Importer { |
||
11 | |||
12 | /** |
||
13 | * The url to list items from WETU |
||
14 | * |
||
15 | * @since 0.0.1 |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | public $tab_slug = 'tour'; |
||
20 | |||
21 | /** |
||
22 | * The url to list items from WETU |
||
23 | * |
||
24 | * @since 0.0.1 |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | public $url = false; |
||
29 | |||
30 | /** |
||
31 | * The query string url to list items from WETU |
||
32 | * |
||
33 | * @since 0.0.1 |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | public $url_qs = false; |
||
38 | |||
39 | /** |
||
40 | * Holds a list of any current accommodation |
||
41 | * |
||
42 | * @since 0.0.1 |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | public $current_accommodation = false; |
||
47 | |||
48 | /** |
||
49 | * Holds a list of any current destinations |
||
50 | * |
||
51 | * @since 0.0.1 |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | public $current_destinations = false; |
||
56 | |||
57 | /** |
||
58 | * Holds a list of the destination and the image it needs to grab. |
||
59 | * |
||
60 | * @since 0.0.1 |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | public $destination_images = false; |
||
65 | |||
66 | /** |
||
67 | * Options |
||
68 | * |
||
69 | * @since 0.0.1 |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | public $options = false; |
||
74 | |||
75 | /** |
||
76 | * The fields you wish to import |
||
77 | * |
||
78 | * @since 0.0.1 |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | public $tour_options = false; |
||
83 | |||
84 | /** |
||
85 | * Initialize the plugin by setting localization, filters, and administration functions. |
||
86 | * |
||
87 | * @since 1.0.0 |
||
88 | * |
||
89 | * @access private |
||
90 | */ |
||
91 | public function __construct() { |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Sets the variables used throughout the plugin. |
||
97 | */ |
||
98 | public function set_variables() { |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Display the importer administration screen |
||
112 | */ |
||
113 | public function display_page() { |
||
223 | <ul> |
||
224 | </ul> |
||
225 | </div> |
||
226 | </div> |
||
227 | <?php |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Displays the options for the form. |
||
232 | * |
||
233 | * @return void |
||
234 | */ |
||
235 | public function update_options_form() { |
||
262 | </form> |
||
263 | <?php |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Grab all the current tour posts via the lsx_wetu_id field. |
||
268 | */ |
||
269 | public function find_current_tours() { |
||
270 | global $wpdb; |
||
271 | $return = array(); |
||
272 | |||
273 | $current_tours = $wpdb->get_results( |
||
274 | " |
||
275 | SELECT key1.post_id,key1.meta_value,key2.post_title |
||
276 | FROM {$wpdb->postmeta} key1 |
||
277 | |||
278 | INNER JOIN {$wpdb->posts} key2 |
||
279 | ON key1.post_id = key2.ID |
||
280 | |||
281 | WHERE key1.meta_key = 'lsx_wetu_id' |
||
282 | AND key2.post_type = 'tour' |
||
283 | |||
284 | LIMIT 0,500 |
||
285 | " |
||
286 | ); |
||
287 | |||
288 | if ( null !== $current_tours && ! empty( $current_tours ) ) { |
||
289 | foreach ( $current_tours as $tour ) { |
||
290 | $return[ $tour->meta_value ] = $tour; |
||
291 | } |
||
292 | } |
||
293 | |||
294 | return $return; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Run through the accommodation grabbed from the DB. |
||
299 | */ |
||
300 | public function process_ajax_search() { |
||
403 | } |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * Formats the row for output on the screen. |
||
408 | */ |
||
409 | public function format_row( $row = false, $row_key = '' ) { |
||
410 | if ( false !== $row ) { |
||
411 | $status = 'import'; |
||
412 | |||
413 | if ( 0 !== $row['post_id'] ) { |
||
414 | $status = '<a href="' . admin_url( '/post.php?post=' . $row['post_id'] . '&action=edit' ) . '" target="_blank">' . get_post_status( $row['post_id'] ) . '</a>'; |
||
415 | } |
||
416 | |||
417 | $row_html = ' |
||
418 | <tr class="post-' . $row['post_id'] . ' type-tour" id="post-' . $row['post_id'] . '"> |
||
419 | <td class="check-column"> |
||
420 | <label for="cb-select-' . $row['identifier'] . '" class="screen-reader-text">' . $row['post_title'] . '</label> |
||
421 | <input type="checkbox" data-identifier="' . $row['identifier'] . '" value="' . $row['post_id'] . '" name="post[]" id="cb-select-' . $row['identifier'] . '"> |
||
422 | </td> |
||
423 | <td class="column-order"> |
||
424 | ' . ( $row_key + 1 ) . ' |
||
425 | </td> |
||
426 | <td class="post-title page-title column-title"> |
||
427 | ' . $row['post_title'] . ' - ' . $status . ' |
||
428 | </td> |
||
429 | <td class="date column-date"> |
||
430 | ' . $row['reference_number'] . ' |
||
431 | </td> |
||
432 | <td class="date column-date"> |
||
433 | <abbr title="' . date( 'Y/m/d', strtotime( $row['last_modified'] ) ) . '">' . date( 'Y/m/d', strtotime( $row['last_modified'] ) ) . '</abbr><br>Last Modified |
||
434 | </td> |
||
435 | <td class="ssid column-ssid"> |
||
436 | ' . $row['identifier'] . ' |
||
437 | </td> |
||
438 | </tr>'; |
||
439 | return $row_html; |
||
440 | } |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Connect to wetu |
||
445 | */ |
||
446 | public function process_ajax_import( $force = false ) { |
||
447 | $return = false; |
||
448 | check_ajax_referer( 'lsx_wetu_ajax_action', 'security' ); |
||
449 | if ( isset( $_POST['action'] ) && 'lsx_import_items' === $_POST['action'] && isset( $_POST['type'] ) && $_POST['type'] === $this->tab_slug && isset( $_POST['wetu_id'] ) ) { |
||
450 | |||
451 | $wetu_id = sanitize_text_field( $_POST['wetu_id'] ); |
||
452 | if ( isset( $_POST['post_id'] ) ) { |
||
453 | $post_id = sanitize_text_field( $_POST['post_id'] ); |
||
454 | } else { |
||
455 | $post_id = 0; |
||
456 | } |
||
457 | |||
458 | delete_option( 'lsx_wetu_importer_tour_settings' ); |
||
459 | |||
460 | if ( isset( $_POST['team_members'] ) ) { |
||
461 | $team_members = array_map( 'sanitize_text_field', wp_unslash( $_POST['team_members'] ) ); |
||
462 | } else { |
||
463 | $team_members = false; |
||
464 | } |
||
465 | |||
466 | if ( isset( $_POST['content'] ) && is_array( $_POST['content'] ) && ! empty( $_POST['content'] ) ) { |
||
467 | $content = array_map( 'sanitize_text_field', wp_unslash( $_POST['content'] ) ); |
||
468 | add_option( 'lsx_wetu_importer_tour_settings', $content ); |
||
469 | } else { |
||
470 | $content = false; |
||
471 | } |
||
472 | $jdata = wp_remote_get( 'https://wetu.com/API/Itinerary/V8/Get?id=' . $wetu_id ); |
||
473 | |||
474 | if ( ! is_wp_error( $jdata ) && ! empty( $jdata ) && isset( $jdata['response'] ) && isset( $jdata['response']['code'] ) && 200 === $jdata['response']['code'] ) { |
||
475 | $jdata = json_decode( $jdata['body'], true ); |
||
476 | $return = $this->import_row( $jdata, $wetu_id, $post_id, $team_members, $content ); |
||
477 | $this->format_completed_row( $return ); |
||
478 | $this->save_queue(); |
||
479 | $this->cleanup_posts(); |
||
480 | $this->attach_destination_images( $content ); |
||
481 | $this->clean_attached_destinations( $return ); |
||
482 | } else { |
||
483 | $this->format_error( esc_html__( 'There was a problem importing your tour, please try again.', 'lsx-wetu-importer' ) ); |
||
484 | } |
||
485 | } |
||
486 | } |
||
487 | |||
488 | /** |
||
489 | * Amends the tours destinations instead of replace. |
||
490 | * |
||
491 | * @param $id string |
||
492 | * @return void |
||
493 | */ |
||
494 | public function clean_attached_destinations( $id ) { |
||
495 | $current_connections = get_post_meta( $id, 'destination_to_tour', false ); |
||
496 | delete_post_meta( $id, 'destination_to_tour' ); |
||
497 | $current_connections = array_unique( $current_connections ); |
||
498 | |||
499 | foreach ( $current_connections as $connection ) { |
||
500 | add_post_meta( $id, 'destination_to_tour', $connection, false ); |
||
501 | } |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * Connect to wetu |
||
506 | * |
||
507 | * @param $data array |
||
508 | * @param $wetu_id string |
||
509 | */ |
||
510 | public function import_row( $data, $wetu_id, $id = 0, $team_members = false, $importable_content = array(), $old1 = false, $old2 = false ) { |
||
511 | $post_name = ''; |
||
512 | $data_post_content = ''; |
||
513 | $data_post_excerpt = ''; |
||
514 | |||
515 | $current_post = get_post( $id ); |
||
516 | |||
517 | $post = array( |
||
518 | 'post_type' => 'tour', |
||
519 | ); |
||
520 | |||
521 | $content_used_general_description = false; |
||
522 | |||
523 | if ( ! empty( $importable_content ) && in_array( 'description', $importable_content ) ) { |
||
524 | $data_post_content = $current_post->post_content; |
||
525 | if ( isset( $data['summary'] ) && ! empty( $data['summary'] ) ) { |
||
526 | $data_post_content = $data['summary']; |
||
527 | } |
||
528 | $post['post_content'] = $data_post_content; |
||
529 | } |
||
530 | |||
531 | // Create or update the post. |
||
532 | if ( false !== $id && '0' !== $id ) { |
||
533 | $post['ID'] = $id; |
||
534 | $post['post_status'] = 'publish'; |
||
535 | if ( isset( $this->options ) && 'on' !== $this->options['disable_accommodation_title'] ) { |
||
536 | $post['post_title'] = $data['name']; |
||
537 | } |
||
538 | $id = wp_update_post( $post ); |
||
539 | } else { |
||
540 | // Set the name. |
||
541 | if ( isset( $data['name'] ) ) { |
||
542 | $post_name = wp_unique_post_slug( sanitize_title( $data['name'] ), $id, 'draft', 'tour', 0 ); |
||
543 | } |
||
544 | |||
545 | if ( ! isset( $post['post_content'] ) ) { |
||
546 | $post['post_content'] = ' '; |
||
547 | } |
||
548 | |||
549 | $post['post_name'] = $post_name; |
||
550 | $post['post_title'] = $data['name']; |
||
551 | $post['post_status'] = 'publish'; |
||
552 | $id = wp_insert_post( $post ); |
||
553 | |||
554 | // Save the WETU ID and the Last date it was modified. |
||
555 | if ( false !== $id ) { |
||
556 | add_post_meta( $id, 'lsx_wetu_id', $wetu_id ); |
||
557 | add_post_meta( $id, 'lsx_wetu_modified_date', strtotime( $data['last_modified'] ) ); |
||
558 | } |
||
559 | } |
||
560 | |||
561 | // Set reference number. |
||
562 | $this->set_reference_number( $data, $id ); |
||
563 | |||
564 | // Set the team member if it is there. |
||
565 | if ( post_type_exists( 'team' ) && false !== $team_members && '' !== $team_members ) { |
||
566 | $this->set_team_member( $id, $team_members ); |
||
567 | } |
||
568 | |||
569 | // Set the price. |
||
570 | if ( false !== $importable_content && in_array( 'price', $importable_content ) ) { |
||
571 | $this->set_price( $data, $id ); |
||
572 | } |
||
573 | |||
574 | // Set the Duration. |
||
575 | if ( false !== $importable_content && in_array( 'duration', $importable_content ) ) { |
||
576 | $this->set_duration( $data, $id ); |
||
577 | } |
||
578 | |||
579 | // Set the Group Size. |
||
580 | if ( false !== $importable_content && in_array( 'group_size', $importable_content ) ) { |
||
581 | $this->set_group_size( $data, $id ); |
||
582 | } |
||
583 | |||
584 | // Set the Group Size. |
||
585 | if ( false !== $importable_content && in_array( 'tags', $importable_content ) ) { |
||
586 | $this->set_travel_styles( $id, $data ); |
||
587 | } |
||
588 | |||
589 | // Set the Start and End Point Destinations. |
||
590 | if ( false !== $importable_content && in_array( 'start_end_point', $importable_content ) ) { |
||
591 | $this->set_start_end_point( $data, $id ); |
||
592 | } |
||
593 | |||
594 | if ( false !== $importable_content && in_array( 'itineraries', $importable_content ) && isset( $data['legs'] ) && ! empty( $data['legs'] ) ) { |
||
595 | $this->process_itineraries( $data, $id, $importable_content ); |
||
596 | } |
||
597 | |||
598 | if ( in_array( 'map', $importable_content ) && isset( $data['routes'] ) && ! empty( $data['routes'] ) ) { |
||
599 | $this->set_map_data( $data, $id ); |
||
600 | } |
||
601 | |||
602 | return $id; |
||
603 | } |
||
604 | |||
605 | /** |
||
606 | * A loop which runs through each leg on the tour. |
||
607 | */ |
||
608 | public function process_itineraries( $data, $id, $importable_content ) { |
||
609 | $day_counter = 1; |
||
610 | $leg_counter = 0; |
||
611 | |||
612 | delete_post_meta( $id, 'itinerary' ); |
||
613 | |||
614 | if ( false !== $importable_content && in_array( 'accommodation', $importable_content ) ) { |
||
615 | delete_post_meta( $id, 'accommodation_to_tour' ); |
||
616 | } |
||
617 | |||
618 | foreach ( $data['legs'] as $leg ) { |
||
619 | |||
620 | // Itinerary Accommodation. |
||
621 | $current_accommodation = false; |
||
622 | $current_destination = false; |
||
623 | if ( 'Mobile' !== $leg['type'] ) { |
||
624 | if ( false !== $importable_content && in_array( 'accommodation', $importable_content ) ) { |
||
625 | $current_accommodation = $this->set_accommodation( $leg, $id ); |
||
626 | } |
||
627 | if ( false !== $importable_content && in_array( 'destination', $importable_content ) ) { |
||
628 | $current_destination = $this->set_destination( $leg, $id, $leg_counter ); |
||
629 | } |
||
630 | } |
||
631 | |||
632 | // If the Nights are the same mount of days in the array, then it isnt "By Destination". |
||
633 | if ( ( 1 <= (int) $leg['nights'] && isset( $leg['periods'] ) ) || 0 === $leg['itinerary_leg_id'] ) { |
||
634 | |||
635 | foreach ( $leg['periods'] as $day_key => $day ) { |
||
636 | $current_day = array(); |
||
637 | |||
638 | // If this is a moble tented solution. |
||
639 | $next_day_count = $day_counter + (int) $day['days']; |
||
640 | |||
641 | if ( ( isset( $leg['stops'] ) && 'Mobile' !== $leg['type'] ) || ( 1 < (int) $day['days'] ) ) { |
||
642 | $day_count_label = ' - ' . ( $next_day_count - 1 ); |
||
643 | } else { |
||
644 | $day_count_label = ''; |
||
645 | } |
||
646 | $current_day['title'] = esc_attr( 'Day ', 'lsx-wetu-importer' ) . $day_counter . $day_count_label; |
||
647 | |||
648 | // Description. |
||
649 | if ( false !== $importable_content && in_array( 'itinerary_description', $importable_content ) && isset( $day['notes'] ) ) { |
||
650 | $current_day['description'] = $day['notes']; |
||
651 | } else { |
||
652 | $current_day['description'] = ''; |
||
653 | } |
||
654 | |||
655 | // Itinerary Gallery. |
||
656 | if ( false !== $importable_content && in_array( 'itinerary_gallery', $importable_content ) && isset( $day['images'] ) ) { |
||
657 | $current_day['featured_image'] = ''; |
||
658 | } else { |
||
659 | $current_day['featured_image'] = ''; |
||
660 | } |
||
661 | |||
662 | // If its a mobile safari, we need to get the destination and accommodation data from the stops. |
||
663 | if ( 'Mobile' === $leg['type'] ) { |
||
664 | $current_destination = $this->get_mobile_destination( $day, $leg, $id ); |
||
665 | $current_accommodation = $this->get_mobile_accommodation( $day, $leg, $id ); |
||
666 | } |
||
667 | |||
668 | // Accommodation. |
||
669 | if ( false !== $current_accommodation ) { |
||
670 | $current_day['accommodation_to_tour'] = array( $current_accommodation ); |
||
671 | } else { |
||
672 | $current_day['accommodation_to_tour'] = array(); |
||
673 | } |
||
674 | |||
675 | // Destination. |
||
676 | if ( false !== $current_destination ) { |
||
677 | $current_day['destination_to_tour'] = array( $current_destination ); |
||
678 | } else { |
||
679 | $current_day['destination_to_tour'] = array(); |
||
680 | } |
||
681 | |||
682 | // Included. |
||
683 | if ( false !== $importable_content && in_array( 'itinerary_included', $importable_content ) && isset( $day['included'] ) && '' !== $day['included'] ) { |
||
684 | $current_day['included'] = $day['included']; |
||
685 | } else { |
||
686 | $current_day['included'] = ''; |
||
687 | } |
||
688 | |||
689 | // Excluded. |
||
690 | if ( false !== $importable_content && in_array( 'itinerary_excluded', $importable_content ) && isset( $day['excluded'] ) && '' !== $day['excluded'] ) { |
||
691 | $current_day['excluded'] = $day['excluded']; |
||
692 | } else { |
||
693 | $current_day['excluded'] = ''; |
||
694 | } |
||
695 | |||
696 | // Excluded. |
||
697 | if ( false !== $importable_content && in_array( 'room_basis', $importable_content ) && isset( $day['room_basis'] ) && '' !== $day['room_basis'] ) { |
||
698 | $current_day['room_basis'] = $day['room_basis']; |
||
699 | } else { |
||
700 | $current_day['room_basis'] = ''; |
||
701 | } |
||
702 | |||
703 | // Excluded. |
||
704 | if ( false !== $importable_content && in_array( 'drinks_basis', $importable_content ) && isset( $day['drinks_basis'] ) && '' !== $day['drinks_basis'] ) { |
||
705 | $current_day['drinks_basis'] = $day['drinks_basis']; |
||
706 | } else { |
||
707 | $current_day['drinks_basis'] = ''; |
||
708 | } |
||
709 | |||
710 | $this->set_itinerary_day( $current_day, $id ); |
||
711 | $day_counter = $next_day_count; |
||
712 | } |
||
713 | } else { |
||
714 | // This is for the by destination. |
||
715 | |||
716 | $current_day = array(); |
||
717 | $next_day_count = $day_counter + (int) $leg['nights']; |
||
718 | $day_count_label = $next_day_count - 1; |
||
719 | |||
720 | $current_day['title'] = esc_attr( 'Day ', 'lsx-wetu-importer' ) . $day_counter; |
||
721 | |||
722 | if ( 0 !== (int) $leg['nights'] ) { |
||
723 | $current_day['title'] .= ' - ' . $day_count_label; |
||
724 | } |
||
725 | |||
726 | // Description. |
||
727 | if ( false !== $importable_content && in_array( 'itinerary_description', $importable_content ) && isset( $leg['notes'] ) ) { |
||
728 | $current_day['description'] = $leg['notes']; |
||
729 | } else { |
||
730 | $current_day['description'] = ''; |
||
731 | } |
||
732 | |||
733 | // Itinerary Gallery. |
||
734 | if ( false !== $importable_content && in_array( 'itinerary_gallery', $importable_content ) && isset( $leg['images'] ) ) { |
||
735 | $current_day['featured_image'] = ''; |
||
736 | } else { |
||
737 | $current_day['featured_image'] = ''; |
||
738 | } |
||
739 | |||
740 | // Accommodation. |
||
741 | if ( false !== $current_accommodation ) { |
||
742 | $current_day['accommodation_to_tour'] = array( $current_accommodation ); |
||
743 | } else { |
||
744 | $current_day['accommodation_to_tour'] = array(); |
||
745 | } |
||
746 | |||
747 | // Destination. |
||
748 | if ( false !== $current_destination ) { |
||
749 | $current_day['destination_to_tour'] = array( $current_destination ); |
||
750 | } else { |
||
751 | $current_day['destination_to_tour'] = array(); |
||
752 | } |
||
753 | |||
754 | // Included. |
||
755 | if ( false !== $importable_content && in_array( 'itinerary_included', $importable_content ) && isset( $leg['included'] ) && '' !== $leg['included'] ) { |
||
756 | $current_day['included'] = $leg['included']; |
||
757 | } else { |
||
758 | $current_day['included'] = ''; |
||
759 | } |
||
760 | |||
761 | // Excluded. |
||
762 | if ( false !== $importable_content && in_array( 'itinerary_excluded', $importable_content ) && isset( $leg['excluded'] ) && '' !== $leg['excluded'] ) { |
||
763 | $current_day['excluded'] = $leg['excluded']; |
||
764 | } else { |
||
765 | $current_day['excluded'] = ''; |
||
766 | } |
||
767 | |||
768 | // Excluded. |
||
769 | if ( false !== $importable_content && in_array( 'room_basis', $importable_content ) && isset( $leg['room_basis'] ) && '' !== $leg['room_basis'] ) { |
||
770 | $current_day['room_basis'] = $leg['room_basis']; |
||
771 | } else { |
||
772 | $current_day['room_basis'] = ''; |
||
773 | } |
||
774 | |||
775 | // Excluded. |
||
776 | if ( false !== $importable_content && in_array( 'drinks_basis', $importable_content ) && isset( $leg['drinks_basis'] ) && '' !== $leg['drinks_basis'] ) { |
||
777 | $current_day['drinks_basis'] = $leg['drinks_basis']; |
||
778 | } else { |
||
779 | $current_day['drinks_basis'] = ''; |
||
780 | } |
||
781 | |||
782 | $this->set_itinerary_day( $current_day, $id ); |
||
783 | $day_counter = $next_day_count; |
||
784 | } |
||
785 | $leg_counter++; |
||
786 | } |
||
787 | } |
||
788 | |||
789 | /** |
||
790 | * Sets the departs from and ends in points on the tours. |
||
791 | * |
||
792 | * @param array $data |
||
793 | * @param string $id |
||
794 | * @return void |
||
795 | */ |
||
796 | public function set_start_end_point( $data, $id ) { |
||
797 | delete_post_meta( $id, 'departs_from' ); |
||
798 | delete_post_meta( $id, 'ends_in' ); |
||
799 | $departs_from = false; |
||
800 | $ends_in = false; |
||
801 | |||
802 | $args = array( |
||
803 | 'points' => $data['legs'], |
||
804 | 'start_index' => 0, |
||
805 | 'end_index' => count( $data['legs'] ) - 2, |
||
806 | ); |
||
807 | $args = apply_filters( 'lsx_wetu_start_end_args', $args, $data ); |
||
808 | |||
809 | if ( ! empty( $args['points'] ) && is_array( $args['points'] ) ) { |
||
810 | $leg_counter = 0; |
||
811 | |||
812 | foreach ( $args['points'] as $point ) { |
||
813 | // If we are in the first leg, and the destination was attached then save it as the departure field. |
||
814 | if ( $leg_counter === $args['start_index'] ) { |
||
815 | $departs_from_destination = $this->set_country( $point['destination_content_entity_id'], $id ); |
||
816 | if ( false !== $departs_from_destination ) { |
||
817 | $departs_from = $departs_from_destination; |
||
818 | } |
||
819 | } |
||
820 | // If its the last leg then save it as the ends in. |
||
821 | if ( $leg_counter === $args['end_index'] ) { |
||
822 | $ends_in = $point['destination_content_entity_id']; |
||
823 | } |
||
824 | $leg_counter++; |
||
825 | } |
||
826 | |||
827 | $departs_from = apply_filters( 'lsx_wetu_departs_from_id', $departs_from, $data, $this ); |
||
828 | if ( false !== $departs_from ) { |
||
829 | add_post_meta( $id, 'departs_from', $departs_from, true ); |
||
830 | } |
||
831 | |||
832 | if ( false !== $ends_in ) { |
||
833 | $ends_in = apply_filters( 'lsx_wetu_ends_in_id', $ends_in, $data, $this ); |
||
834 | $ends_in_destination = $this->set_country( $ends_in, $id ); |
||
835 | if ( false !== $ends_in_destination ) { |
||
836 | add_post_meta( $id, 'ends_in', $ends_in_destination, true ); |
||
837 | } |
||
838 | } |
||
839 | } |
||
840 | } |
||
841 | |||
842 | /** |
||
843 | * Gets the destination for the mobile camp. |
||
844 | * |
||
845 | * @param $day |
||
846 | * @param $leg |
||
847 | * @return void |
||
848 | */ |
||
849 | public function get_mobile_destination( $day, $leg, $id ) { |
||
850 | $current_destination = false; |
||
851 | $current_day = (int) $day['period_start_day']; |
||
852 | if ( isset( $leg['stops'] ) ) { |
||
853 | foreach ( $leg['stops'] as $stop ) { |
||
854 | $arrival_day = (int) $stop['arrival_day']; |
||
855 | $departure_day = (int) $stop['departure_day']; |
||
856 | if ( $arrival_day <= $current_day && $current_day < $departure_day ) { |
||
857 | $current_destination = $this->set_destination( $stop, $id, 0 ); |
||
858 | } |
||
859 | } |
||
860 | } |
||
861 | return $current_destination; |
||
862 | } |
||
863 | |||
864 | /** |
||
865 | * Gets the accommodation for the mobile camp. |
||
866 | * |
||
867 | * @param $day |
||
868 | * @param $leg |
||
869 | * @return void |
||
870 | */ |
||
871 | public function get_mobile_accommodation( $day, $leg, $id ) { |
||
872 | $current_accommodation = false; |
||
873 | $current_day = (int) $day['period_start_day']; |
||
874 | if ( isset( $leg['stops'] ) ) { |
||
875 | foreach ( $leg['stops'] as $stop ) { |
||
876 | $arrival_day = (int) $stop['arrival_day']; |
||
877 | $departure_day = (int) $stop['departure_day']; |
||
878 | if ( $arrival_day <= $current_day && $current_day < $departure_day ) { |
||
879 | $current_accommodation = $this->set_accommodation( $stop, $id, 0 ); |
||
880 | } |
||
881 | } |
||
882 | } |
||
883 | return $current_accommodation; |
||
884 | } |
||
885 | |||
886 | /** |
||
887 | * Run through your routes and save the points as a KML file. |
||
888 | */ |
||
889 | public function set_map_data( $data, $id, $zoom = 9 ) { |
||
890 | if ( ! empty( $data['routes'] ) ) { |
||
891 | delete_post_meta( $id, 'wetu_map_points' ); |
||
892 | |||
893 | $points = array(); |
||
894 | |||
895 | foreach ( $data['routes'] as $route ) { |
||
896 | |||
897 | if ( isset( $route['points'] ) && '' !== $route['points'] ) { |
||
898 | |||
899 | $temp_points = explode( ';', $route['points'] ); |
||
900 | $point_counter = count( $temp_points ); |
||
901 | |||
902 | for ( $x = 0; $x <= $point_counter; $x++ ) { |
||
903 | $y = $x + 1; |
||
904 | $points[] = $temp_points[ $x ] . ',' . $temp_points[ $y ]; |
||
905 | $x++; |
||
906 | } |
||
907 | } |
||
908 | } |
||
909 | |||
910 | if ( ! empty( $points ) ) { |
||
911 | $this->save_custom_field( implode( ' ', $points ), 'wetu_map_points', $id, false, true ); |
||
912 | } |
||
913 | } |
||
914 | |||
915 | } |
||
916 | |||
917 | // CLASS SPECIFIC FUNCTIONS. |
||
918 | |||
919 | /** |
||
920 | * Set the Itinerary Day. |
||
921 | */ |
||
922 | public function set_itinerary_day( $day, $id ) { |
||
923 | $this->save_custom_field( $day, 'itinerary', $id, false, false ); |
||
924 | } |
||
925 | |||
926 | /** |
||
927 | * Set the ref number |
||
928 | */ |
||
929 | public function set_reference_number( $data, $id ) { |
||
930 | if ( isset( $data['reference_number'] ) && '' !== $data['reference_number'] ) { |
||
931 | $this->save_custom_field( $data['reference_number'], 'lsx_wetu_ref', $id ); |
||
932 | } |
||
933 | } |
||
934 | |||
935 | /** |
||
936 | * Set the price. |
||
937 | */ |
||
938 | public function set_price( $data, $id ) { |
||
939 | // Price. |
||
940 | if ( isset( $data['price'] ) && '' !== $data['price'] ) { |
||
941 | $price = $data['price']; |
||
942 | if ( false === apply_filters( 'lsx_wetu_importer_disable_tour_price_filter', false ) ) { |
||
943 | $price = preg_replace( '/[^0-9,.]/', '', $price ); |
||
944 | } |
||
945 | $meta_key = apply_filters( 'lsx_wetu_importer_price_meta_key', 'price' ); |
||
946 | $this->save_custom_field( $price, $meta_key, $id ); |
||
947 | } |
||
948 | |||
949 | // Price includes. |
||
950 | if ( isset( $data['price_includes'] ) && '' !== $data['price_includes'] ) { |
||
951 | $meta_key = apply_filters( 'lsx_wetu_importer_included_meta_key', 'included' ); |
||
952 | $this->save_custom_field( $data['price_includes'], $meta_key, $id ); |
||
953 | } |
||
954 | |||
955 | // Price Excludes. |
||
956 | if ( isset( $data['price_excludes'] ) && '' !== $data['price_excludes'] ) { |
||
957 | $meta_key = apply_filters( 'lsx_wetu_importer_not_included_meta_key', 'not_included' ); |
||
958 | $this->save_custom_field( $data['price_excludes'], $meta_key, $id ); |
||
959 | } |
||
960 | } |
||
961 | |||
962 | /** |
||
963 | * Set the duration. |
||
964 | */ |
||
965 | public function set_duration( $data, $id ) { |
||
966 | if ( isset( $data['days'] ) && ! empty( $data['days'] ) ) { |
||
967 | $price = $data['days']; |
||
968 | $price = preg_replace( '/[^0-9,.]/', '', $price ); |
||
969 | $this->save_custom_field( $price, 'duration', $id ); |
||
970 | } |
||
971 | } |
||
972 | |||
973 | /** |
||
974 | * Set the group size |
||
975 | */ |
||
976 | public function set_group_size( $data, $id ) { |
||
977 | if ( isset( $data['group_size'] ) && ! empty( $data['group_size'] ) ) { |
||
978 | $group_size = $data['group_size']; |
||
979 | $this->save_custom_field( $group_size, 'group_size', $id ); |
||
980 | } |
||
981 | } |
||
982 | |||
983 | /** |
||
984 | * Takes the WETU tags and sets the Travel Styles. |
||
985 | * |
||
986 | * @param string $id |
||
987 | * @param array $travel_styles |
||
988 | * @return void |
||
989 | */ |
||
990 | public function set_travel_styles( $id, $data ) { |
||
991 | $tags = apply_filters( 'lsx_wetu_importer_tour_travel_styles', $data['tags'] ); |
||
992 | if ( isset( $data['tags'] ) && ! empty( $tags ) ) { |
||
993 | foreach ( $tags as $tag ) { |
||
994 | $this->set_term( $id, $tag, 'travel-style' ); |
||
995 | } |
||
996 | } |
||
997 | } |
||
998 | |||
999 | /** |
||
1000 | * Connects the Accommodation if its available |
||
1001 | */ |
||
1002 | public function set_accommodation( $day, $id ) { |
||
1003 | $ac_id = false; |
||
1004 | $this->current_accommodation = $this->find_current_accommodation(); |
||
1005 | |||
1006 | if ( isset( $day['content_entity_id'] ) && ! empty( $day['content_entity_id'] ) && ! in_array( (int) $day['content_entity_id'], array( 25862 ) ) ) { |
||
1007 | if ( false !== $this->current_accommodation && ! empty( $this->current_accommodation ) && array_key_exists( $day['content_entity_id'], $this->current_accommodation ) ) { |
||
1008 | $ac_id = $this->current_accommodation[ $day['content_entity_id'] ]; |
||
1009 | } else { |
||
1010 | $ac_id = wp_insert_post( |
||
1011 | array( |
||
1012 | 'post_type' => 'accommodation', |
||
1013 | 'post_status' => 'draft', |
||
1014 | 'post_title' => $day['content_entity_id'], |
||
1015 | ) |
||
1016 | ); |
||
1017 | |||
1018 | $this->save_custom_field( $day['content_entity_id'], 'lsx_wetu_id', $ac_id ); |
||
1019 | } |
||
1020 | |||
1021 | if ( '' !== $ac_id && false !== $ac_id ) { |
||
1022 | $this->save_custom_field( $ac_id, 'accommodation_to_tour', $id, false, false ); |
||
1023 | $this->save_custom_field( $id, 'tour_to_accommodation', $ac_id, false, false ); |
||
1024 | $this->queue_item( $ac_id ); |
||
1025 | } |
||
1026 | } |
||
1027 | return $ac_id; |
||
1028 | } |
||
1029 | |||
1030 | /** |
||
1031 | * Grab all the current accommodation posts via the lsx_wetu_id field. |
||
1032 | * |
||
1033 | * @param $post_type string |
||
1034 | * @return boolean / array |
||
1035 | */ |
||
1036 | public function find_current_accommodation( $post_type = 'accommodation' ) { |
||
1037 | global $wpdb; |
||
1038 | $accommodation = parent::find_current_accommodation( $post_type ); |
||
1039 | $return = false; |
||
1040 | |||
1041 | if ( ! empty( $accommodation ) ) { |
||
1042 | foreach ( $accommodation as $key => $acc ) { |
||
1043 | $return[ $acc->meta_value ] = $acc->post_id; |
||
1044 | } |
||
1045 | } |
||
1046 | |||
1047 | return $return; |
||
1048 | } |
||
1049 | |||
1050 | /** |
||
1051 | * Grab all the current accommodation posts via the lsx_wetu_id field. |
||
1052 | * |
||
1053 | * @return boolean / array |
||
1054 | */ |
||
1055 | public function find_current_destinations() { |
||
1056 | return $this->find_current_accommodation( 'destination' ); |
||
1057 | } |
||
1058 | |||
1059 | /** |
||
1060 | * Connects the destinations post type |
||
1061 | * |
||
1062 | * @param $day array |
||
1063 | * @param $id string |
||
1064 | * @return boolean / string |
||
1065 | */ |
||
1066 | public function set_destination( $day, $id, $leg_counter ) { |
||
1067 | $dest_id = false; |
||
1068 | $country_id = false; |
||
1069 | |||
1070 | $this->current_destinations = $this->find_current_destinations(); |
||
1071 | |||
1072 | if ( isset( $day['destination_content_entity_id'] ) && ! empty( $day['destination_content_entity_id'] ) ) { |
||
1073 | if ( false !== $this->current_destinations && ! empty( $this->current_destinations ) && array_key_exists( $day['destination_content_entity_id'], $this->current_destinations ) ) { |
||
1074 | $dest_id = $this->current_destinations[ $day['destination_content_entity_id'] ]; |
||
1075 | |||
1076 | // TODO Check for attachments here. |
||
1077 | $this->destination_images[ $id ][] = array( $dest_id, $day['destination_content_entity_id'] ); |
||
1078 | |||
1079 | // Check if there is a country asigned. |
||
1080 | $potential_id = wp_get_post_parent_id( $dest_id ); |
||
1081 | $country_wetu_id = get_post_meta( $potential_id, 'lsx_wetu_id', true ); |
||
1082 | |||
1083 | if ( false !== $country_wetu_id ) { |
||
1084 | $country_id = $this->set_country( $country_wetu_id, $id ); |
||
1085 | // $this->destination_images[ $id ][] = array( $id, $country_wetu_id ); |
||
1086 | } |
||
1087 | } else { |
||
1088 | $destination_json = wp_remote_get( 'https://wetu.com/API/Pins/' . $this->api_key . '/Get?ids=' . $day['destination_content_entity_id'] ); |
||
1089 | |||
1090 | if ( ! is_wp_error( $destination_json ) && ! empty( $destination_json ) && isset( $destination_json['response'] ) && isset( $destination_json['response']['code'] ) && 200 === $destination_json['response']['code'] ) { |
||
1091 | |||
1092 | $destination_data = json_decode( $destination_json['body'], true ); |
||
1093 | |||
1094 | if ( ! empty( $destination_data ) && ! isset( $destination_data['error'] ) ) { |
||
1095 | $destination_title = $day['destination_content_entity_id']; |
||
1096 | |||
1097 | if ( isset( $destination_data[0]['name'] ) ) { |
||
1098 | $destination_title = $destination_data[0]['name']; |
||
1099 | } |
||
1100 | |||
1101 | if ( isset( $destination_data[0]['map_object_id'] ) && isset( $destination_data[0]['position']['country_content_entity_id'] ) |
||
1102 | && $destination_data[0]['map_object_id'] !== $destination_data[0]['position']['country_content_entity_id'] ) { |
||
1103 | |||
1104 | $country_id = $this->set_country( $destination_data[0]['position']['country_content_entity_id'], $id ); |
||
1105 | // Save the destination so we can grab the tour featured image and banner from them. |
||
1106 | } |
||
1107 | |||
1108 | $dest_post = array( |
||
1109 | 'post_type' => 'destination', |
||
1110 | 'post_status' => 'draft', |
||
1111 | 'post_title' => $destination_title, |
||
1112 | ); |
||
1113 | |||
1114 | if ( false !== $country_id ) { |
||
1115 | $dest_post['post_parent'] = $country_id; |
||
1116 | } |
||
1117 | $dest_id = wp_insert_post( $dest_post ); |
||
1118 | |||
1119 | // Make sure we register the. |
||
1120 | $this->current_destinations[ $day['destination_content_entity_id'] ] = $dest_id; |
||
1121 | |||
1122 | // If there are images attached then use the destination. |
||
1123 | if ( isset( $destination_data[0]['content']['images'] ) && ! empty( $destination_data[0]['content']['images'] ) ) { |
||
1124 | $this->destination_images[ $id ][] = array( $dest_id, $day['destination_content_entity_id'] ); |
||
1125 | } |
||
1126 | |||
1127 | $this->save_custom_field( $day['destination_content_entity_id'], 'lsx_wetu_id', $dest_id ); |
||
1128 | } |
||
1129 | } |
||
1130 | } |
||
1131 | |||
1132 | if ( '' !== $dest_id && false !== $dest_id ) { |
||
1133 | $this->save_custom_field( $dest_id, 'destination_to_tour', $id, false, false ); |
||
1134 | $this->save_custom_field( $id, 'tour_to_destination', $dest_id, false, false ); |
||
1135 | |||
1136 | // Save the item to display in the queue |
||
1137 | $this->queue_item( $dest_id ); |
||
1138 | |||
1139 | // Save the item to clean up the amount of connections. |
||
1140 | $this->cleanup_posts[ $dest_id ] = 'tour_to_destination'; |
||
1141 | |||
1142 | // Add this relation info so we can make sure certain items are set as countries. |
||
1143 | if ( 0 !== $country_id && false !== $country_id ) { |
||
1144 | $this->relation_meta[ $dest_id ] = $country_id; |
||
1145 | $this->relation_meta[ $country_id ] = 0; |
||
1146 | } else { |
||
1147 | $this->relation_meta[ $dest_id ] = 0; |
||
1148 | } |
||
1149 | } |
||
1150 | } |
||
1151 | return $dest_id; |
||
1152 | } |
||
1153 | |||
1154 | /** |
||
1155 | * Connects the destinations post type |
||
1156 | * |
||
1157 | * @param $dest_id string |
||
1158 | * @param $country_id array |
||
1159 | * @param $id string |
||
1160 | * |
||
1161 | * @return string |
||
1162 | */ |
||
1163 | public function set_country( $country_wetu_id, $id ) { |
||
1164 | $country_id = false; |
||
1165 | $this->current_destinations = $this->find_current_destinations(); |
||
1166 | |||
1167 | if ( false !== $this->current_destinations && ! empty( $this->current_destinations ) && array_key_exists( $country_wetu_id, $this->current_destinations ) ) { |
||
1168 | $country_id = $this->current_destinations[ $country_wetu_id ]; |
||
1169 | $this->destination_images[ $id ][] = array( $country_id, $country_wetu_id ); |
||
1170 | } else { |
||
1171 | $country_json = wp_remote_get( 'https://wetu.com/API/Pins/' . $this->api_key . '/Get?ids=' . $country_wetu_id ); |
||
1172 | |||
1173 | if ( ! is_wp_error( $country_json ) && ! empty( $country_json ) && isset( $country_json['response'] ) && isset( $country_json['response']['code'] ) && 200 === $country_json['response']['code'] ) { |
||
1174 | $country_data = json_decode( $country_json['body'], true ); |
||
1175 | |||
1176 | // Format the title of the destination if its available, otherwise default to the WETU ID. |
||
1177 | $country_title = $country_wetu_id; |
||
1178 | |||
1179 | if ( isset( $country_data[0]['name'] ) ) { |
||
1180 | $country_title = $country_data[0]['name']; |
||
1181 | } |
||
1182 | |||
1183 | $country_id = wp_insert_post( |
||
1184 | array( |
||
1185 | 'post_type' => 'destination', |
||
1186 | 'post_status' => 'draft', |
||
1187 | 'post_title' => $country_title, |
||
1188 | ) |
||
1189 | ); |
||
1190 | |||
1191 | // add the country to the current destination stack |
||
1192 | $this->current_destinations[ $country_wetu_id ] = $country_id; |
||
1193 | |||
1194 | // Check if there are images and save fore use later. |
||
1195 | if ( isset( $country_data[0]['content']['images'] ) && ! empty( $country_data[0]['content']['images'] ) ) { |
||
1196 | $this->destination_images[ $id ][] = array( $country_id, $country_wetu_id ); |
||
1197 | } |
||
1198 | |||
1199 | // Save the wetu field |
||
1200 | $this->save_custom_field( $country_wetu_id, 'lsx_wetu_id', $country_id ); |
||
1201 | } |
||
1202 | } |
||
1203 | |||
1204 | if ( '' !== $country_id && false !== $country_id ) { |
||
1205 | $this->save_custom_field( $country_id, 'destination_to_tour', $id, false, false ); |
||
1206 | $this->save_custom_field( $id, 'tour_to_destination', $country_id, false, false ); |
||
1207 | $this->queue_item( $country_id ); |
||
1208 | $this->cleanup_posts[ $country_id ] = 'tour_to_destination'; |
||
1209 | |||
1210 | return $country_id; |
||
1211 | } |
||
1212 | } |
||
1213 | |||
1214 | /** |
||
1215 | * Connects the destinations post type |
||
1216 | * |
||
1217 | * @param $dest_id string |
||
1218 | * @param $country_id array |
||
1219 | * @param $id string |
||
1220 | * |
||
1221 | * @return string |
||
1222 | */ |
||
1223 | public function attach_destination_images( $importable_content = array() ) { |
||
1224 | if ( false !== $this->destination_images ) { |
||
1225 | $this->shuffle_assoc( $this->destination_images ); |
||
1226 | foreach ( $this->destination_images as $tour => $destinations ) { |
||
1227 | shuffle( $destinations ); |
||
1228 | $image_set = false; |
||
1229 | $forced = false; |
||
1230 | |||
1231 | foreach ( $destinations as $destination ) { |
||
1232 | if ( false === $image_set && false === $forced ) { |
||
1233 | $url = 'https://wetu.com/API/Pins/' . $this->api_key; |
||
1234 | |||
1235 | $url_qs = ''; |
||
1236 | $jdata = wp_remote_get( $url . '/Get?' . $url_qs . '&ids=' . $destination[1] ); |
||
1237 | |||
1238 | if ( ! is_wp_error( $jdata ) && ! empty( $jdata ) && isset( $jdata['response'] ) && isset( $jdata['response']['code'] ) && 200 === $jdata['response']['code'] ) { |
||
1239 | $adata = json_decode( $jdata['body'], true ); |
||
1240 | |||
1241 | if ( ! empty( $adata ) && ! empty( $adata[0]['content']['images'] ) ) { |
||
1242 | $this->find_attachments( $destination[0] ); |
||
1243 | |||
1244 | // Set the featured image. |
||
1245 | if ( false !== $importable_content && in_array( 'featured_image', $importable_content ) ) { |
||
1246 | $image_set = $this->set_featured_image( $adata, $tour ); |
||
1247 | if ( false !== $importable_content && in_array( 'banner_image', $importable_content ) ) { |
||
1248 | $image_set = $this->set_banner_image( $adata, $tour ); |
||
1249 | $forced = true; |
||
1250 | } |
||
1251 | continue; |
||
1252 | } |
||
1253 | if ( false !== $importable_content && in_array( 'banner_image', $importable_content ) ) { |
||
1254 | $image_set = $this->set_banner_image( $adata, $tour ); |
||
1255 | } |
||
1256 | } |
||
1257 | } |
||
1258 | } else { |
||
1259 | continue; |
||
1260 | } |
||
1261 | } |
||
1262 | } |
||
1263 | } |
||
1264 | } |
||
1265 | |||
1266 | /** |
||
1267 | * Creates the main gallery data |
||
1268 | */ |
||
1269 | public function set_featured_image( $data, $id ) { |
||
1270 | $image_set = false; |
||
1271 | $counter = 0; |
||
1272 | |||
1273 | if ( is_array( $data[0]['content']['images'] ) && ! empty( $data[0]['content']['images'] ) ) { |
||
1274 | $images_array = $data[0]['content']['images']; |
||
1275 | |||
1276 | if ( 'on' === $this->options['enable_tour_featured_random'] ) { |
||
1277 | shuffle( $images_array ); |
||
1278 | } |
||
1279 | |||
1280 | foreach ( $images_array as $v ) { |
||
1281 | |||
1282 | if ( true === $image_set ) { |
||
1283 | $counter++; |
||
1284 | continue; |
||
1285 | } |
||
1286 | |||
1287 | if ( ! $this->check_if_image_is_used( $v ) ) { |
||
1288 | $temp_featured_image = $this->attach_image( $v, $id ); |
||
1289 | |||
1290 | if ( false !== $temp_featured_image ) { |
||
1291 | $this->featured_image = $temp_featured_image; |
||
1292 | delete_post_meta( $id, '_thumbnail_id' ); |
||
1293 | add_post_meta( $id, '_thumbnail_id', $this->featured_image, true ); |
||
1294 | $image_set = true; |
||
1295 | } |
||
1296 | } |
||
1297 | |||
1298 | $counter++; |
||
1299 | } |
||
1300 | } |
||
1301 | return $image_set; |
||
1302 | } |
||
1303 | |||
1304 | /** |
||
1305 | * Sets a banner image |
||
1306 | */ |
||
1307 | public function set_banner_image( $data, $id, $content = array( 'none' ) ) { |
||
1308 | $image_set = false; |
||
1309 | $counter = 0; |
||
1310 | |||
1311 | if ( is_array( $data[0]['content']['images'] ) && ! empty( $data[0]['content']['images'] ) ) { |
||
1312 | |||
1313 | foreach ( $data[0]['content']['images'] as $v ) { |
||
1314 | /* |
||
1315 | print_r('<pre>'); |
||
1316 | print_r( $v ); |
||
1317 | print_r('</pre>');*/ |
||
1318 | |||
1319 | if ( true === $image_set || 0 === $counter ) { |
||
1320 | $counter++; |
||
1321 | continue; |
||
1322 | } |
||
1323 | |||
1324 | if ( ! $this->check_if_image_is_used( $v ) ) { |
||
1325 | $temp_banner = $this->attach_image( |
||
1326 | $v, |
||
1327 | $id, |
||
1328 | array( |
||
1329 | 'width' => '1920', |
||
1330 | 'height' => '600', |
||
1331 | 'cropping' => 'c', |
||
1332 | ) |
||
1333 | ); |
||
1334 | |||
1335 | if ( false !== $temp_banner ) { |
||
1336 | $this->banner_image = $temp_banner; |
||
1337 | |||
1338 | delete_post_meta( $id, 'image_group' ); |
||
1339 | |||
1340 | $new_banner = array( |
||
1341 | 'banner_image' => array( |
||
1342 | 'cmb-field-0' => $this->banner_image, |
||
1343 | ), |
||
1344 | ); |
||
1345 | add_post_meta( $id, 'image_group', $new_banner, true ); |
||
1346 | $image_set = true; |
||
1347 | } |
||
1348 | } |
||
1349 | $counter++; |
||
1350 | } |
||
1351 | } |
||
1352 | |||
1353 | return $image_set; |
||
1354 | } |
||
1355 | |||
1356 | /** |
||
1357 | * Grabs all of the current used featured images on the site. |
||
1358 | */ |
||
1359 | public function check_if_image_is_used( $v ) { |
||
1360 | global $wpdb; |
||
1361 | $return = false; |
||
1362 | |||
1363 | $results = $wpdb->get_results( |
||
1364 | $wpdb->prepare( |
||
1365 | "SELECT post_id |
||
1366 | FROM {$wpdb->postmeta} |
||
1367 | WHERE meta_value = '%s' |
||
1368 | AND meta_key = 'lsx_wetu_id' |
||
1369 | ", |
||
1370 | array( $value ) |
||
1371 | ) |
||
1372 | ); |
||
1373 | $attached_tours = array(); |
||
1374 | if ( ! empty( $results ) ) { |
||
1375 | foreach ( $results as $result ) { |
||
1376 | if ( 'tour' === get_post_type( $result['post_id'] ) ) { |
||
1377 | $attached_tours[] = $result['post_id']; |
||
1378 | } |
||
1379 | } |
||
1380 | } |
||
1381 | if ( ! empty( $attached_tours ) ) { |
||
1382 | $return = true; |
||
1383 | } |
||
1384 | return $return; |
||
1385 | } |
||
1386 | |||
1387 | /** |
||
1388 | * Que an item to be saved. |
||
1389 | * |
||
1390 | * @param $id int |
||
1391 | */ |
||
1392 | public function queue_item( $id ) { |
||
1393 | if ( is_array( $this->import_queue ) && ! in_array( $id, $this->import_queue ) ) { |
||
1394 | $this->import_queue[] = $id; |
||
1395 | } else { |
||
1396 | $this->import_queue[] = $id; |
||
1397 | } |
||
1398 | } |
||
1399 | |||
1400 | /** |
||
1401 | * Saves the queue to the option. |
||
1402 | */ |
||
1403 | public function save_queue() { |
||
1404 | if ( ! empty( $this->import_queue ) ) { |
||
1405 | if ( ! empty( $this->queued_imports ) ) { |
||
1406 | $saved_imports = array_merge( $this->queued_imports, $this->import_queue ); |
||
1407 | } else { |
||
1408 | $saved_imports = $this->import_queue; |
||
1409 | } |
||
1410 | |||
1411 | delete_option( 'lsx_wetu_importer_que' ); |
||
1412 | |||
1413 | if ( ! empty( $saved_imports ) ) { |
||
1414 | $saved_imports = array_unique( $saved_imports ); |
||
1415 | update_option( 'lsx_wetu_importer_que', $saved_imports ); |
||
1416 | } |
||
1417 | } |
||
1418 | } |
||
1419 | |||
1420 | /** |
||
1421 | * The header of the item list |
||
1422 | */ |
||
1423 | public function table_header() { |
||
1424 | ?> |
||
1425 | <thead> |
||
1426 | <tr> |
||
1427 | <th class="manage-column column-cb check-column" id="cb" scope="col"> |
||
1428 | <label for="cb-select-all-1" class="screen-reader-text"><?php esc_attr_e( 'Select All', 'lsx-wetu-importer' ); ?></label> |
||
1429 | <input type="checkbox" id="cb-select-all-1"> |
||
1430 | </th> |
||
1431 | <th class="manage-column column-order " id="order" scope="col"><?php esc_attr_e( 'Order', 'lsx-wetu-importer' ); ?></th> |
||
1432 | <th class="manage-column column-title " id="title" scope="col"><?php esc_attr_e( 'Title', 'lsx-wetu-importer' ); ?></th> |
||
1433 | <th class="manage-column column-date" id="ref" scope="col"><?php esc_attr_e( 'Ref', 'lsx-wetu-importer' ); ?></th> |
||
1434 | <th class="manage-column column-date" id="date" scope="col"><?php esc_attr_e( 'Date', 'lsx-wetu-importer' ); ?></th> |
||
1435 | <th class="manage-column column-ssid" id="ssid" scope="col"><?php esc_attr_e( 'WETU ID', 'lsx-wetu-importer' ); ?></th> |
||
1436 | </tr> |
||
1437 | </thead> |
||
1438 | <?php |
||
1439 | } |
||
1440 | |||
1441 | /** |
||
1442 | * The footer of the item list |
||
1443 | */ |
||
1444 | public function table_footer() { |
||
1457 | </tr> |
||
1458 | </tfoot> |
||
1459 | <?php |
||
1460 | } |
||
1461 | } |
||
1462 |