Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Sets the variables used throughout the plugin. |
||
| 97 | */ |
||
| 98 | public function set_variables() { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Display the importer administration screen |
||
| 112 | */ |
||
| 113 | public function display_page() { |
||
| 114 | ?> |
||
| 115 | <div class="wrap"> |
||
| 116 | |||
| 117 | <div class="tablenav top"> |
||
| 118 | <div class="alignleft actions"> |
||
| 119 | <?php $this->search_form(); ?> |
||
| 120 | </div> |
||
| 121 | |||
| 122 | <div class="alignleft actions"> |
||
| 123 | <?php $this->update_options_form(); ?> |
||
| 124 | </div> |
||
| 125 | <br clear="both" /> |
||
| 126 | </div> |
||
| 127 | |||
| 128 | <form method="get" action="" id="posts-filter"> |
||
| 129 | <input type="hidden" name="post_type" class="post_type" value="<?php echo esc_attr( $this->tab_slug ); ?>" /> |
||
| 130 | |||
| 131 | <table class="wp-list-table widefat fixed posts"> |
||
| 132 | <?php $this->table_header(); ?> |
||
| 133 | |||
| 134 | <tbody id="the-list"> |
||
| 135 | <tr class="post-0 type-tour status-none" id="post-0"> |
||
| 136 | <td class="date column-date column-ref" colspan="5"> |
||
| 137 | <strong> |
||
| 138 | <?php esc_html_e( 'Search for tours using the search form above','lsx-wetu-importer' ); ?> |
||
| 139 | </strong> |
||
| 140 | </td> |
||
| 141 | </tr> |
||
| 142 | </tbody> |
||
| 143 | |||
| 144 | <?php $this->table_footer(); ?> |
||
| 145 | |||
| 146 | </table> |
||
| 147 | |||
| 148 | <p><input class="button button-primary add" type="button" value="<?php esc_html_e( 'Add to List','lsx-wetu-importer' ); ?>" /> |
||
| 149 | <input class="button button-primary clear" type="button" value="<?php esc_html_e( 'Clear','lsx-wetu-importer' ); ?>" /> |
||
| 150 | </p> |
||
| 151 | </form> |
||
| 152 | |||
| 153 | <div style="display:none;" class="import-list-wrapper"> |
||
| 154 | <br /> |
||
| 155 | <form method="get" action="" id="import-list"> |
||
| 156 | |||
| 157 | <div class="row"> |
||
| 158 | <div class="settings-all" style="width:30%;display:block;float:left;"> |
||
| 159 | <h3><?php esc_html_e( 'What content to Sync from WETU' ); ?></h3> |
||
| 160 | <ul> |
||
| 161 | View Code Duplication | <?php if ( isset( $this->options ) && isset( $this->options['disable_tour_descriptions'] ) && 'on' !== $this->options['disable_tour_descriptions'] ) { ?> |
|
|
|
|||
| 162 | <li><input class="content" checked="checked" type="checkbox" name="content[]" value="description" /> <?php esc_html_e( 'Description','lsx-wetu-importer' ); ?></li> |
||
| 163 | <?php } ?> |
||
| 164 | |||
| 165 | <li><input class="content" checked="checked" type="checkbox" name="content[]" value="price" /> <?php esc_html_e( 'Price','lsx-wetu-importer' ); ?></li> |
||
| 166 | <li><input class="content" checked="checked" type="checkbox" name="content[]" value="duration" /> <?php esc_html_e( 'Duration','lsx-wetu-importer' ); ?></li> |
||
| 167 | <li><input class="content" checked="checked" type="checkbox" name="content[]" value="group_size" /> <?php esc_html_e( 'Group Size','lsx-wetu-importer' ); ?></li> |
||
| 168 | <li><input class="content" checked="checked" type="checkbox" name="content[]" value="category" /> <?php esc_html_e( 'Category','lsx-wetu-importer' ); ?></li> |
||
| 169 | View Code Duplication | <?php if ( isset( $this->options ) && isset( $this->options['disable_tour_tags'] ) && 'on' !== $this->options['disable_tour_tags'] ) { ?> |
|
| 170 | <li><input class="content" checked="checked" type="checkbox" name="content[]" value="tags" /> <?php esc_html_e( 'Tags','lsx-wetu-importer' ); ?></li> |
||
| 171 | <?php } ?> |
||
| 172 | <li><input class="content" checked="checked" type="checkbox" name="content[]" value="itineraries" /> <?php esc_html_e( 'Itinerary Days','lsx-wetu-importer' ); ?></li> |
||
| 173 | </ul> |
||
| 174 | </div> |
||
| 175 | <div class="settings-all" style="width:30%;display:block;float:left;"> |
||
| 176 | <h3><?php esc_html_e( 'Itinerary Info' ); ?></h3> |
||
| 177 | <ul> |
||
| 178 | <li><input class="content" checked="checked" type="checkbox" name="content[]" value="itinerary_description" /> <?php esc_html_e( 'Description','lsx-wetu-importer' ); ?></li> |
||
| 179 | <li><input class="content" checked="checked" type="checkbox" name="content[]" value="itinerary_included" /> <?php esc_html_e( 'Included','lsx-wetu-importer' ); ?></li> |
||
| 180 | <li><input class="content" checked="checked" type="checkbox" name="content[]" value="itinerary_excluded" /> <?php esc_html_e( 'Excluded','lsx-wetu-importer' ); ?></li> |
||
| 181 | <li><input class="content" checked="checked" type="checkbox" name="content[]" value="room_basis" /> <?php esc_html_e( 'Room Basis','lsx-wetu-importer' ); ?></li> |
||
| 182 | <li><input class="content" checked="checked" type="checkbox" name="content[]" value="drinks_basis" /> <?php esc_html_e( 'Drink Bases','lsx-wetu-importer' ); ?></li> |
||
| 183 | </ul> |
||
| 184 | |||
| 185 | <h4><?php esc_html_e( 'Additional Content' ); ?></h4> |
||
| 186 | <ul> |
||
| 187 | <li><input class="content" checked="checked" type="checkbox" name="content[]" value="accommodation" /> <?php esc_html_e( 'Sync Accommodation','lsx-wetu-importer' ); ?></li> |
||
| 188 | <li><input class="content" checked="checked" type="checkbox" name="content[]" value="destination" /> <?php esc_html_e( 'Sync Destinations','lsx-wetu-importer' ); ?></li> |
||
| 189 | <li><input class="content" checked="checked" type="checkbox" name="content[]" value="featured_image" /> <?php esc_html_e( 'Featured Image','lsx-wetu-importer' ); ?></li> |
||
| 190 | <li><input class="content" checked="checked" type="checkbox" name="content[]" value="banner_image" /> <?php esc_html_e( 'Banner Image','lsx-wetu-importer' ); ?></li> |
||
| 191 | </ul> |
||
| 192 | </div> |
||
| 193 | <?php if ( class_exists( 'LSX_TO_Team' ) ) { ?> |
||
| 194 | <div style="width:30%;display:block;float:left;"> |
||
| 195 | <h3><?php esc_html_e( 'Assign a Team Member' ); ?></h3> |
||
| 196 | <?php $this->team_member_checkboxes( $this->tour_options ); ?> |
||
| 197 | </div> |
||
| 198 | <?php } ?> |
||
| 199 | |||
| 200 | <br clear="both" /> |
||
| 201 | </div> |
||
| 202 | |||
| 203 | <h3><?php esc_html_e( 'Your List' ); ?></h3> |
||
| 204 | <p><input class="button button-primary" type="submit" value="<?php esc_html_e( 'Sync', 'lsx-wetu-importer' ); ?>" /></p> |
||
| 205 | <table class="wp-list-table widefat fixed posts"> |
||
| 206 | <?php $this->table_header(); ?> |
||
| 207 | |||
| 208 | <tbody> |
||
| 209 | |||
| 210 | </tbody> |
||
| 211 | |||
| 212 | <?php $this->table_footer(); ?> |
||
| 213 | |||
| 214 | </table> |
||
| 215 | |||
| 216 | <p><input class="button button-primary" type="submit" value="<?php esc_html_e( 'Sync', 'lsx-wetu-importer' ); ?>" /></p> |
||
| 217 | </form> |
||
| 218 | </div> |
||
| 219 | |||
| 220 | <div style="display:none;" class="completed-list-wrapper"> |
||
| 221 | <h3><?php esc_html_e( 'Completed', 'lsx-wetu-importer' ); ?> - <small><?php esc_html_e( 'Import your', 'lsx-wetu-importer' ); ?> <a href="<?php echo esc_attr( admin_url( 'admin.php' ) ); ?>?page=<?php echo esc_attr( $this->plugin_slug ); ?>&tab=accommodation"><?php esc_html_e( 'accommodation' ); ?></a> <?php esc_html_e( 'next','lsx-wetu-importer' ); ?></small></h3> |
||
| 222 | <ul> |
||
| 223 | </ul> |
||
| 224 | </div> |
||
| 225 | </div> |
||
| 226 | <?php |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Displays the options for the form. |
||
| 231 | * |
||
| 232 | * @return void |
||
| 233 | */ |
||
| 234 | public function update_options_form() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Grab all the current tour posts via the lsx_wetu_id field. |
||
| 258 | */ |
||
| 259 | View Code Duplication | public function find_current_tours() { |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Run through the accommodation grabbed from the DB. |
||
| 287 | */ |
||
| 288 | public function process_ajax_search() { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Formats the row for output on the screen. |
||
| 396 | */ |
||
| 397 | public function format_row( $row = false, $row_key = '' ) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Connect to wetu |
||
| 433 | */ |
||
| 434 | public function process_ajax_import( $force = false ) { |
||
| 435 | $return = false; |
||
| 436 | check_ajax_referer( 'lsx_wetu_ajax_action', 'security' ); |
||
| 437 | if ( isset( $_POST['action'] ) && 'lsx_import_items' === $_POST['action'] && isset( $_POST['type'] ) && $_POST['type'] === $this->tab_slug && isset( $_POST['wetu_id'] ) ) { |
||
| 438 | |||
| 439 | $wetu_id = sanitize_text_field( $_POST['wetu_id'] ); |
||
| 440 | View Code Duplication | if ( isset( $_POST['post_id'] ) ) { |
|
| 441 | $post_id = sanitize_text_field( $_POST['post_id'] ); |
||
| 442 | } else { |
||
| 443 | $post_id = 0; |
||
| 444 | } |
||
| 445 | |||
| 446 | delete_option( 'lsx_wetu_importer_tour_settings' ); |
||
| 447 | |||
| 448 | View Code Duplication | if ( isset( $_POST['team_members'] ) ) { |
|
| 449 | $team_members = array_map( 'sanitize_text_field', wp_unslash( $_POST['team_members'] ) ); |
||
| 450 | } else { |
||
| 451 | $team_members = false; |
||
| 452 | } |
||
| 453 | |||
| 454 | View Code Duplication | if ( isset( $_POST['content'] ) && is_array( $_POST['content'] ) && ! empty( $_POST['content'] ) ) { |
|
| 455 | $content = array_map( 'sanitize_text_field', wp_unslash( $_POST['content'] ) ); |
||
| 456 | add_option( 'lsx_wetu_importer_tour_settings',$content ); |
||
| 457 | } else { |
||
| 458 | $content = false; |
||
| 459 | } |
||
| 460 | $jdata = wp_remote_get( 'https://wetu.com/API/Itinerary/V8/Get?id=' . $wetu_id ); |
||
| 461 | |||
| 462 | if ( ! is_wp_error( $jdata ) && ! empty( $jdata ) && isset( $jdata['response'] ) && isset( $jdata['response']['code'] ) && 200 === $jdata['response']['code'] ) { |
||
| 463 | $jdata = json_decode( $jdata['body'], true ); |
||
| 464 | $return = $this->import_row( $jdata, $wetu_id, $post_id, $team_members, $content ); |
||
| 465 | $this->format_completed_row( $return ); |
||
| 466 | $this->save_queue(); |
||
| 467 | $this->cleanup_posts(); |
||
| 468 | $this->attach_destination_images( $content ); |
||
| 469 | $this->clean_attached_destinations( $return ); |
||
| 470 | } else { |
||
| 471 | $this->format_error( esc_html__( 'There was a problem importing your tour, please contact support.', 'lsx-wetu-importer' ) ); |
||
| 472 | } |
||
| 473 | } |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Amends the tours destinations instead of replace. |
||
| 478 | * |
||
| 479 | * @param $id string |
||
| 480 | * @return void |
||
| 481 | */ |
||
| 482 | public function clean_attached_destinations( $id ) { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Connect to wetu |
||
| 494 | * |
||
| 495 | * @param $data array |
||
| 496 | * @param $wetu_id string |
||
| 497 | */ |
||
| 498 | public function import_row( $data, $wetu_id, $id = 0, $team_members = false, $importable_content = array(), $old1 = false, $old2 = false ) { |
||
| 499 | $post_name = ''; |
||
| 500 | $data_post_content = ''; |
||
| 501 | $data_post_excerpt = ''; |
||
| 502 | |||
| 503 | $current_post = get_post( $id ); |
||
| 504 | |||
| 505 | $post = array( |
||
| 506 | 'post_type' => 'tour', |
||
| 507 | ); |
||
| 508 | |||
| 509 | $content_used_general_description = false; |
||
| 510 | |||
| 511 | if ( ! empty( $importable_content ) && in_array( 'description', $importable_content ) ) { |
||
| 512 | $data_post_content = $current_post->post_content; |
||
| 513 | if ( isset( $data['summary'] ) && ! empty( $data['summary'] ) ) { |
||
| 514 | $data_post_content = $data['summary']; |
||
| 515 | } |
||
| 516 | $post['post_content'] = $data_post_content; |
||
| 517 | } |
||
| 518 | |||
| 519 | // Create or update the post. |
||
| 520 | if ( false !== $id && '0' !== $id ) { |
||
| 521 | $post['ID'] = $id; |
||
| 522 | $post['post_status'] = 'publish'; |
||
| 523 | if ( isset( $this->options ) && 'on' !== $this->options['disable_accommodation_title'] ) { |
||
| 524 | $post['post_title'] = $data['name']; |
||
| 525 | } |
||
| 526 | $id = wp_update_post( $post ); |
||
| 527 | } else { |
||
| 528 | // Set the name. |
||
| 529 | if ( isset( $data['name'] ) ) { |
||
| 530 | $post_name = wp_unique_post_slug( sanitize_title( $data['name'] ), $id, 'draft', 'tour', 0 ); |
||
| 531 | } |
||
| 532 | |||
| 533 | if ( ! isset( $post['post_content'] ) ) { |
||
| 534 | $post['post_content'] = ' '; |
||
| 535 | } |
||
| 536 | |||
| 537 | $post['post_name'] = $post_name; |
||
| 538 | $post['post_title'] = $data['name']; |
||
| 539 | $post['post_status'] = 'publish'; |
||
| 540 | $id = wp_insert_post( $post ); |
||
| 541 | |||
| 542 | // Save the WETU ID and the Last date it was modified. |
||
| 543 | if ( false !== $id ) { |
||
| 544 | add_post_meta( $id, 'lsx_wetu_id', $wetu_id ); |
||
| 545 | add_post_meta( $id, 'lsx_wetu_modified_date', strtotime( $data['last_modified'] ) ); |
||
| 546 | } |
||
| 547 | } |
||
| 548 | |||
| 549 | // Set reference number. |
||
| 550 | $this->set_reference_number( $data, $id ); |
||
| 551 | |||
| 552 | // Set the team member if it is there. |
||
| 553 | View Code Duplication | if ( post_type_exists( 'team' ) && false !== $team_members && '' !== $team_members ) { |
|
| 554 | $this->set_team_member( $id, $team_members ); |
||
| 555 | } |
||
| 556 | |||
| 557 | // Set the price. |
||
| 558 | if ( false !== $importable_content && in_array( 'price', $importable_content ) ) { |
||
| 559 | $this->set_price( $data, $id ); |
||
| 560 | } |
||
| 561 | |||
| 562 | // Set the Duration. |
||
| 563 | if ( false !== $importable_content && in_array( 'duration', $importable_content ) ) { |
||
| 564 | $this->set_duration( $data, $id ); |
||
| 565 | } |
||
| 566 | |||
| 567 | // Set the Group Size. |
||
| 568 | if ( false !== $importable_content && in_array( 'group_size', $importable_content ) ) { |
||
| 569 | $this->set_group_size( $data, $id ); |
||
| 570 | } |
||
| 571 | |||
| 572 | // Set the Group Size. |
||
| 573 | if ( false !== $importable_content && in_array( 'tags', $importable_content ) ) { |
||
| 574 | $this->set_travel_styles( $id, $data ); |
||
| 575 | } |
||
| 576 | |||
| 577 | if ( false !== $importable_content && in_array( 'itineraries', $importable_content ) && isset( $data['legs'] ) && ! empty( $data['legs'] ) ) { |
||
| 578 | $this->process_itineraries( $data, $id, $importable_content ); |
||
| 579 | } |
||
| 580 | |||
| 581 | if ( in_array( 'map', $importable_content ) && isset( $data['routes'] ) && ! empty( $data['routes'] ) ) { |
||
| 582 | $this->set_map_data( $data, $id ); |
||
| 583 | } |
||
| 584 | |||
| 585 | return $id; |
||
| 586 | } |
||
| 587 | |||
| 588 | /** |
||
| 589 | * A loop which runs through each leg on the tour. |
||
| 590 | */ |
||
| 591 | public function process_itineraries( $data, $id, $importable_content ) { |
||
| 592 | $day_counter = 1; |
||
| 593 | $leg_counter = 0; |
||
| 594 | |||
| 595 | delete_post_meta( $id, 'itinerary' ); |
||
| 596 | |||
| 597 | if ( false !== $importable_content && in_array( 'accommodation', $importable_content ) ) { |
||
| 598 | delete_post_meta( $id, 'accommodation_to_tour' ); |
||
| 599 | } |
||
| 600 | if ( false !== $importable_content && in_array( 'destination', $importable_content ) ) { |
||
| 601 | delete_post_meta( $id, 'departs_from' ); |
||
| 602 | delete_post_meta( $id, 'ends_in' ); |
||
| 603 | } |
||
| 604 | |||
| 605 | $departs_from = false; |
||
| 606 | $ends_in = false; |
||
| 607 | |||
| 608 | foreach ( $data['legs'] as $leg ) { |
||
| 609 | // Itinerary Accommodation. |
||
| 610 | $current_accommodation = false; |
||
| 611 | if ( false !== $importable_content && in_array( 'accommodation', $importable_content ) ) { |
||
| 612 | $current_accommodation = $this->set_accommodation( $leg, $id ); |
||
| 613 | } |
||
| 614 | |||
| 615 | // Itinerary Destination. |
||
| 616 | $current_destination = false; |
||
| 617 | if ( false !== $importable_content && in_array( 'destination', $importable_content ) ) { |
||
| 618 | $current_destination = $this->set_destination( $leg, $id, $leg_counter ); |
||
| 619 | } |
||
| 620 | |||
| 621 | // If the Nights are the same mount of days in the array, then it isnt "By Destination". |
||
| 622 | if ( ( 1 <= (int) $leg['nights'] && isset( $leg['periods'] ) ) || 0 === $leg['itinerary_leg_id'] ) { |
||
| 623 | |||
| 624 | foreach ( $leg['periods'] as $day_key => $day ) { |
||
| 625 | $current_day = array(); |
||
| 626 | |||
| 627 | // If this is a moble tented solution. |
||
| 628 | $next_day_count = $day_counter + (int) $day['days']; |
||
| 629 | |||
| 630 | if ( isset( $leg['stops'] ) || ( 1 < (int) $day['days'] ) ) { |
||
| 631 | $day_count_label = ' - ' . ( $next_day_count - 1 ); |
||
| 632 | } else { |
||
| 633 | $day_count_label = ''; |
||
| 634 | } |
||
| 635 | $current_day['title'] = esc_attr( 'Day ', 'lsx-wetu-importer' ) . $day_counter . $day_count_label; |
||
| 636 | |||
| 637 | // Description. |
||
| 638 | if ( false !== $importable_content && in_array( 'itinerary_description', $importable_content ) && isset( $day['notes'] ) ) { |
||
| 639 | $current_day['description'] = $day['notes']; |
||
| 640 | } else { |
||
| 641 | $current_day['description'] = ''; |
||
| 642 | } |
||
| 643 | |||
| 644 | // Itinerary Gallery. |
||
| 645 | View Code Duplication | if ( false !== $importable_content && in_array( 'itinerary_gallery', $importable_content ) && isset( $day['images'] ) ) { |
|
| 646 | $current_day['featured_image'] = ''; |
||
| 647 | } else { |
||
| 648 | $current_day['featured_image'] = ''; |
||
| 649 | } |
||
| 650 | |||
| 651 | // Accommodation. |
||
| 652 | View Code Duplication | if ( false !== $current_accommodation ) { |
|
| 653 | $current_day['accommodation_to_tour'] = array( $current_accommodation ); |
||
| 654 | } else { |
||
| 655 | $current_day['accommodation_to_tour'] = array(); |
||
| 656 | } |
||
| 657 | |||
| 658 | // Destination. |
||
| 659 | View Code Duplication | if ( false !== $current_destination ) { |
|
| 660 | $current_day['destination_to_tour'] = array( $current_destination ); |
||
| 661 | } else { |
||
| 662 | $current_day['destination_to_tour'] = array(); |
||
| 663 | } |
||
| 664 | |||
| 665 | // Included. |
||
| 666 | View Code Duplication | if ( false !== $importable_content && in_array( 'itinerary_included', $importable_content ) && isset( $day['included'] ) && '' !== $day['included'] ) { |
|
| 667 | $current_day['included'] = $day['included']; |
||
| 668 | } else { |
||
| 669 | $current_day['included'] = ''; |
||
| 670 | } |
||
| 671 | |||
| 672 | // Excluded. |
||
| 673 | View Code Duplication | if ( false !== $importable_content && in_array( 'itinerary_excluded', $importable_content ) && isset( $day['excluded'] ) && '' !== $day['excluded'] ) { |
|
| 674 | $current_day['excluded'] = $day['excluded']; |
||
| 675 | } else { |
||
| 676 | $current_day['excluded'] = ''; |
||
| 677 | } |
||
| 678 | |||
| 679 | // Excluded. |
||
| 680 | View Code Duplication | if ( false !== $importable_content && in_array( 'room_basis', $importable_content ) && isset( $day['room_basis'] ) && '' !== $day['room_basis'] ) { |
|
| 681 | $current_day['room_basis'] = $day['room_basis']; |
||
| 682 | } else { |
||
| 683 | $current_day['room_basis'] = ''; |
||
| 684 | } |
||
| 685 | |||
| 686 | // Excluded. |
||
| 687 | View Code Duplication | if ( false !== $importable_content && in_array( 'drinks_basis', $importable_content ) && isset( $day['drinks_basis'] ) && '' !== $day['drinks_basis'] ) { |
|
| 688 | $current_day['drinks_basis'] = $day['drinks_basis']; |
||
| 689 | } else { |
||
| 690 | $current_day['drinks_basis'] = ''; |
||
| 691 | } |
||
| 692 | |||
| 693 | $this->set_itinerary_day( $current_day, $id ); |
||
| 694 | $day_counter = $next_day_count; |
||
| 695 | } |
||
| 696 | } else { |
||
| 697 | // This is for the by destination. |
||
| 698 | |||
| 699 | $current_day = array(); |
||
| 700 | $next_day_count = $day_counter + (int) $leg['nights']; |
||
| 701 | $day_count_label = $next_day_count - 1; |
||
| 702 | |||
| 703 | $current_day['title'] = esc_attr( 'Day ', 'lsx-wetu-importer' ) . $day_counter; |
||
| 704 | |||
| 705 | if ( 0 !== (int) $leg['nights'] ) { |
||
| 706 | $current_day['title'] .= ' - ' . $day_count_label; |
||
| 707 | } |
||
| 708 | |||
| 709 | // Description. |
||
| 710 | if ( false !== $importable_content && in_array( 'itinerary_description', $importable_content ) && isset( $leg['notes'] ) ) { |
||
| 711 | $current_day['description'] = $leg['notes']; |
||
| 712 | } else { |
||
| 713 | $current_day['description'] = ''; |
||
| 714 | } |
||
| 715 | |||
| 716 | // Itinerary Gallery. |
||
| 717 | View Code Duplication | if ( false !== $importable_content && in_array( 'itinerary_gallery', $importable_content ) && isset( $leg['images'] ) ) { |
|
| 718 | $current_day['featured_image'] = ''; |
||
| 719 | } else { |
||
| 720 | $current_day['featured_image'] = ''; |
||
| 721 | } |
||
| 722 | |||
| 723 | // Accommodation. |
||
| 724 | View Code Duplication | if ( false !== $current_accommodation ) { |
|
| 725 | $current_day['accommodation_to_tour'] = array( $current_accommodation ); |
||
| 726 | } else { |
||
| 727 | $current_day['accommodation_to_tour'] = array(); |
||
| 728 | } |
||
| 729 | |||
| 730 | // Destination. |
||
| 731 | View Code Duplication | if ( false !== $current_destination ) { |
|
| 732 | $current_day['destination_to_tour'] = array( $current_destination ); |
||
| 733 | } else { |
||
| 734 | $current_day['destination_to_tour'] = array(); |
||
| 735 | } |
||
| 736 | |||
| 737 | // Included. |
||
| 738 | View Code Duplication | if ( false !== $importable_content && in_array( 'itinerary_included', $importable_content ) && isset( $leg['included'] ) && '' !== $leg['included'] ) { |
|
| 739 | $current_day['included'] = $leg['included']; |
||
| 740 | } else { |
||
| 741 | $current_day['included'] = ''; |
||
| 742 | } |
||
| 743 | |||
| 744 | // Excluded. |
||
| 745 | View Code Duplication | if ( false !== $importable_content && in_array( 'itinerary_excluded', $importable_content ) && isset( $leg['excluded'] ) && '' !== $leg['excluded'] ) { |
|
| 746 | $current_day['excluded'] = $leg['excluded']; |
||
| 747 | } else { |
||
| 748 | $current_day['excluded'] = ''; |
||
| 749 | } |
||
| 750 | |||
| 751 | // Excluded. |
||
| 752 | View Code Duplication | if ( false !== $importable_content && in_array( 'room_basis', $importable_content ) && isset( $leg['room_basis'] ) && '' !== $leg['room_basis'] ) { |
|
| 753 | $current_day['room_basis'] = $leg['room_basis']; |
||
| 754 | } else { |
||
| 755 | $current_day['room_basis'] = ''; |
||
| 756 | } |
||
| 757 | |||
| 758 | // Excluded. |
||
| 759 | View Code Duplication | if ( false !== $importable_content && in_array( 'drinks_basis', $importable_content ) && isset( $leg['drinks_basis'] ) && '' !== $leg['drinks_basis'] ) { |
|
| 760 | $current_day['drinks_basis'] = $leg['drinks_basis']; |
||
| 761 | } else { |
||
| 762 | $current_day['drinks_basis'] = ''; |
||
| 763 | } |
||
| 764 | |||
| 765 | $this->set_itinerary_day( $current_day,$id ); |
||
| 766 | $day_counter = $next_day_count; |
||
| 767 | } |
||
| 768 | |||
| 769 | // If we are in the first leg, and the destination was attached then save it as the departure field. |
||
| 770 | if ( 0 === $leg_counter && false !== $current_destination ) { |
||
| 771 | $departs_from = $current_destination; |
||
| 772 | } |
||
| 773 | |||
| 774 | // If its the last leg then save it as the ends in. |
||
| 775 | if ( ( count( $data['legs'] ) - 2 ) === $leg_counter && false !== $current_destination ) { |
||
| 776 | $ends_in = $current_destination; |
||
| 777 | } |
||
| 778 | |||
| 779 | $leg_counter++; |
||
| 780 | } |
||
| 781 | |||
| 782 | if ( false !== $departs_from ) { |
||
| 783 | add_post_meta( $id, 'departs_from', $departs_from, true ); |
||
| 784 | } |
||
| 785 | if ( false !== $ends_in ) { |
||
| 786 | add_post_meta( $id, 'ends_in', $ends_in, true ); |
||
| 787 | } |
||
| 788 | } |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Run through your routes and save the points as a KML file. |
||
| 792 | */ |
||
| 793 | public function set_map_data( $data, $id, $zoom = 9 ) { |
||
| 820 | |||
| 821 | // CLASS SPECIFIC FUNCTIONS. |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Set the Itinerary Day. |
||
| 825 | */ |
||
| 826 | public function set_itinerary_day( $day, $id ) { |
||
| 829 | |||
| 830 | /** |
||
| 831 | * Set the ref number |
||
| 832 | */ |
||
| 833 | public function set_reference_number( $data, $id ) { |
||
| 838 | |||
| 839 | /** |
||
| 840 | * Set the price. |
||
| 841 | */ |
||
| 842 | public function set_price( $data, $id ) { |
||
| 865 | |||
| 866 | /** |
||
| 867 | * Set the duration. |
||
| 868 | */ |
||
| 869 | public function set_duration( $data, $id ) { |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Set the group size |
||
| 879 | */ |
||
| 880 | public function set_group_size( $data, $id ) { |
||
| 886 | |||
| 887 | /** |
||
| 888 | * Takes the WETU tags and sets the Travel Styles. |
||
| 889 | * |
||
| 890 | * @param string $id |
||
| 891 | * @param array $travel_styles |
||
| 892 | * @return void |
||
| 893 | */ |
||
| 894 | public function set_travel_styles( $id, $data ) { |
||
| 902 | |||
| 903 | /** |
||
| 904 | * Connects the Accommodation if its available |
||
| 905 | */ |
||
| 906 | public function set_accommodation( $day, $id ) { |
||
| 931 | |||
| 932 | /** |
||
| 933 | * Grab all the current accommodation posts via the lsx_wetu_id field. |
||
| 934 | * |
||
| 935 | * @param $post_type string |
||
| 936 | * @return boolean / array |
||
| 937 | */ |
||
| 938 | public function find_current_accommodation( $post_type = 'accommodation' ) { |
||
| 951 | |||
| 952 | /** |
||
| 953 | * Grab all the current accommodation posts via the lsx_wetu_id field. |
||
| 954 | * @return boolean / array |
||
| 955 | */ |
||
| 956 | public function find_current_destinations() { |
||
| 959 | |||
| 960 | /** |
||
| 961 | * Connects the destinations post type |
||
| 962 | * |
||
| 963 | * @param $day array |
||
| 964 | * @param $id string |
||
| 965 | * @return boolean / string |
||
| 966 | */ |
||
| 967 | public function set_destination( $day, $id, $leg_counter ) { |
||
| 1054 | |||
| 1055 | /** |
||
| 1056 | * Connects the destinations post type |
||
| 1057 | * |
||
| 1058 | * @param $dest_id string |
||
| 1059 | * @param $country_id array |
||
| 1060 | * @param $id string |
||
| 1061 | * |
||
| 1062 | * @return string |
||
| 1063 | */ |
||
| 1064 | public function set_country( $country_wetu_id, $id ) { |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * Connects the destinations post type |
||
| 1115 | * |
||
| 1116 | * @param $dest_id string |
||
| 1117 | * @param $country_id array |
||
| 1118 | * @param $id string |
||
| 1119 | * |
||
| 1120 | * @return string |
||
| 1121 | */ |
||
| 1122 | public function attach_destination_images( $importable_content = array() ) { |
||
| 1166 | |||
| 1167 | /** |
||
| 1168 | * Creates the main gallery data |
||
| 1169 | */ |
||
| 1170 | public function set_featured_image( $data, $id ) { |
||
| 1201 | |||
| 1202 | /** |
||
| 1203 | * Sets a banner image |
||
| 1204 | */ |
||
| 1205 | public function set_banner_image( $data, $id, $content = array( 'none' ) ) { |
||
| 1248 | |||
| 1249 | /** |
||
| 1250 | * Grabs all of the current used featured images on the site. |
||
| 1251 | */ |
||
| 1252 | public function check_if_image_is_used( $v ) { |
||
| 1286 | |||
| 1287 | /** |
||
| 1288 | * Que an item to be saved. |
||
| 1289 | * |
||
| 1290 | * @param $id int |
||
| 1291 | */ |
||
| 1292 | public function queue_item( $id ) { |
||
| 1299 | |||
| 1300 | /** |
||
| 1301 | * Saves the queue to the option. |
||
| 1302 | */ |
||
| 1303 | public function save_queue() { |
||
| 1319 | |||
| 1320 | /** |
||
| 1321 | * The header of the item list |
||
| 1322 | */ |
||
| 1323 | View Code Duplication | public function table_header() { |
|
| 1340 | |||
| 1341 | /** |
||
| 1342 | * The footer of the item list |
||
| 1343 | */ |
||
| 1344 | View Code Duplication | public function table_footer() { |
|
| 1361 | } |
||
| 1362 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.