| Total Complexity | 142 |
| Total Lines | 728 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like LSX_WETU_Importer_Destination 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_Destination, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class LSX_WETU_Importer_Destination 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 = 'destination'; |
||
| 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 | * Options |
||
| 41 | * |
||
| 42 | * @since 0.0.1 |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | public $options = false; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The fields you wish to import |
||
| 50 | * |
||
| 51 | * @since 0.0.1 |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | public $destination_options = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Initialize the plugin by setting localization, filters, and administration functions. |
||
| 59 | * |
||
| 60 | * @since 1.0.0 |
||
| 61 | * |
||
| 62 | * @access private |
||
| 63 | */ |
||
| 64 | public function __construct() { |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Sets the variables used throughout the plugin. |
||
| 70 | */ |
||
| 71 | public function set_variables() { |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Display the importer administration screen |
||
| 90 | */ |
||
| 91 | public function display_page() { |
||
| 92 | ?> |
||
| 93 | <div class="wrap"> |
||
| 94 | <div class="tablenav top"> |
||
| 95 | <div class="actions"> |
||
| 96 | <?php $this->search_form(); ?> |
||
| 97 | </div> |
||
| 98 | </div> |
||
| 99 | |||
| 100 | <form method="get" action="" id="posts-filter"> |
||
| 101 | <input type="hidden" name="post_type" class="post_type" value="<?php echo esc_attr( $this->tab_slug ); ?>"/> |
||
| 102 | |||
| 103 | <table class="wp-list-table widefat fixed posts"> |
||
| 104 | <?php $this->table_header(); ?> |
||
| 105 | |||
| 106 | <tbody id="the-list"> |
||
| 107 | <tr class="post-0 type-tour status-none" id="post-0"> |
||
| 108 | <th class="check-column" scope="row"> |
||
| 109 | <label for="cb-select-0" |
||
| 110 | class="screen-reader-text"><?php esc_html_e( 'Enter a title to search for and press enter', 'lsx-wetu-importer' ); ?></label> |
||
| 111 | </th> |
||
| 112 | <td class="post-title page-title column-title"> |
||
| 113 | <strong> |
||
| 114 | <?php esc_html_e( 'Enter a title to search for', 'lsx-wetu-importer' ); ?> |
||
| 115 | </strong> |
||
| 116 | </td> |
||
| 117 | <td class="date column-date"> |
||
| 118 | </td> |
||
| 119 | <td class="ssid column-ssid"> |
||
| 120 | </td> |
||
| 121 | </tr> |
||
| 122 | </tbody> |
||
| 123 | |||
| 124 | <?php $this->table_footer(); ?> |
||
| 125 | |||
| 126 | </table> |
||
| 127 | |||
| 128 | <p><input class="button button-primary add" type="button" |
||
| 129 | value="<?php esc_html_e( 'Add to List', 'lsx-wetu-importer' ); ?>"/> |
||
| 130 | <input class="button button-primary clear" type="button" |
||
| 131 | value="<?php esc_html_e( 'Clear', 'lsx-wetu-importer' ); ?>"/> |
||
| 132 | </p> |
||
| 133 | </form> |
||
| 134 | |||
| 135 | <div style="display:none;" class="import-list-wrapper"> |
||
| 136 | <br/> |
||
| 137 | <form method="get" action="" id="import-list"> |
||
| 138 | |||
| 139 | <div class="row"> |
||
| 140 | <div class="settings-all" style="width:30%;display:block;float:left;"> |
||
| 141 | <h3><?php esc_html_e( 'What content to Sync from WETU' ); ?></h3> |
||
| 142 | <ul> |
||
| 143 | <?php if ( isset( $this->options ) && isset( $this->options['disable_destination_descriptions'] ) && 'on' !== $this->options['disable_destination_descriptions'] ) { ?> |
||
| 144 | <li> |
||
| 145 | <input class="content" checked="checked" |
||
| 146 | type="checkbox" name="content[]" |
||
| 147 | value="description"/> <?php esc_html_e( 'Description', 'lsx-wetu-importer' ); ?></li> |
||
| 148 | <?php } ?> |
||
| 149 | |||
| 150 | <li> |
||
| 151 | <input class="content" checked="checked" |
||
| 152 | type="checkbox" name="content[]" |
||
| 153 | value="gallery"/> <?php esc_html_e( 'Main Gallery', 'lsx-wetu-importer' ); ?></li> |
||
| 154 | |||
| 155 | <li> |
||
| 156 | <input class="content" checked="checked" |
||
| 157 | type="checkbox" name="content[]" |
||
| 158 | value="location"/> <?php esc_html_e( 'Location', 'lsx-wetu-importer' ); ?> |
||
| 159 | </li> |
||
| 160 | |||
| 161 | |||
| 162 | <?php if ( class_exists( 'LSX_TO_Videos' ) ) { ?> |
||
| 163 | <li> |
||
| 164 | <input class="content" checked="checked" |
||
| 165 | type="checkbox" name="content[]" |
||
| 166 | value="videos"/> <?php esc_html_e( 'Videos', 'lsx-wetu-importer' ); ?></li> |
||
| 167 | <?php } ?> |
||
| 168 | |||
| 169 | </ul> |
||
| 170 | <h4><?php esc_html_e( 'Additional Content' ); ?></h4> |
||
| 171 | <ul> |
||
| 172 | <li> |
||
| 173 | <input class="content" checked="checked" |
||
| 174 | type="checkbox" name="content[]" |
||
| 175 | value="country"/> <?php esc_html_e( 'Set Country', 'lsx-wetu-importer' ); ?></li> |
||
| 176 | <li> |
||
| 177 | <input class="content" checked="checked" |
||
| 178 | type="checkbox" name="content[]" |
||
| 179 | value="continent"/> <?php esc_html_e( 'Set Continent', 'lsx-wetu-importer' ); ?></li> |
||
| 180 | |||
| 181 | <li> |
||
| 182 | <input class="content" checked="checked" |
||
| 183 | type="checkbox" name="content[]" |
||
| 184 | value="featured_image"/> <?php esc_html_e( 'Set Featured Image', 'lsx-wetu-importer' ); ?> |
||
| 185 | </li> |
||
| 186 | |||
| 187 | <li> |
||
| 188 | <input class="content" checked="checked" |
||
| 189 | type="checkbox" name="content[]" |
||
| 190 | value="banner_image"/> <?php esc_html_e( 'Set Banner Image', 'lsx-wetu-importer' ); ?> |
||
| 191 | </li> |
||
| 192 | <?php |
||
| 193 | /* |
||
| 194 | <li> |
||
| 195 | <input class="content" |
||
| 196 | type="checkbox" name="content[]" |
||
| 197 | value="unique_banner_image"/> <?php esc_html_e( 'Use the WETU banner field', 'lsx-wetu-importer' ); ?> |
||
| 198 | </li>*/ |
||
| 199 | ?> |
||
| 200 | |||
| 201 | <li> |
||
| 202 | <input class="content" checked="checked" |
||
| 203 | type="checkbox" name="content[]" |
||
| 204 | value="strip_tags"/> <?php esc_html_e( 'Strip HTML from the description', 'lsx-wetu-importer' ); ?></li> |
||
| 205 | </ul> |
||
| 206 | </div> |
||
| 207 | <div class="settings-all" style="width:30%;display:block;float:left;"> |
||
| 208 | <h3><?php esc_html_e( 'Travel Information' ); ?></h3> |
||
| 209 | <ul> |
||
| 210 | <li> |
||
| 211 | <input class="content" checked="checked" |
||
| 212 | type="checkbox" name="content[]" |
||
| 213 | value="electricity"/> <?php esc_html_e( 'Electricity', 'lsx-wetu-importer' ); ?></li> |
||
| 214 | <li> |
||
| 215 | <input class="content" checked="checked" |
||
| 216 | type="checkbox" name="content[]" |
||
| 217 | value="banking"/> <?php esc_html_e( 'Banking', 'lsx-wetu-importer' ); ?></li> |
||
| 218 | <li> |
||
| 219 | <input class="content" checked="checked" |
||
| 220 | type="checkbox" name="content[]" |
||
| 221 | value="cuisine"/> <?php esc_html_e( 'Cuisine', 'lsx-wetu-importer' ); ?></li> |
||
| 222 | <li> |
||
| 223 | <input class="content" checked="checked" |
||
| 224 | type="checkbox" name="content[]" |
||
| 225 | value="climate"/> <?php esc_html_e( 'Climate', 'lsx-wetu-importer' ); ?></li> |
||
| 226 | <li> |
||
| 227 | <input class="content" checked="checked" |
||
| 228 | type="checkbox" name="content[]" |
||
| 229 | value="transport"/> <?php esc_html_e( 'Transport', 'lsx-wetu-importer' ); ?></li> |
||
| 230 | <li><input class="content" checked="checked" |
||
| 231 | type="checkbox" name="content[]" |
||
| 232 | value="dress"/> <?php esc_html_e( 'Dress', 'lsx-wetu-importer' ); ?></li> |
||
| 233 | <li><input class="content" checked="checked" |
||
| 234 | type="checkbox" name="content[]" |
||
| 235 | value="health"/> <?php esc_html_e( 'Health', 'lsx-wetu-importer' ); ?></li> |
||
| 236 | <li><input class="content" checked="checked" |
||
| 237 | type="checkbox" name="content[]" |
||
| 238 | value="safety"/> <?php esc_html_e( 'Safety', 'lsx-wetu-importer' ); ?></li> |
||
| 239 | <li><input class="content" checked="checked" |
||
| 240 | type="checkbox" name="content[]" |
||
| 241 | value="visa"/> <?php esc_html_e( 'Visa', 'lsx-wetu-importer' ); ?></li> |
||
| 242 | <li><input class="content" checked="checked" |
||
| 243 | type="checkbox" name="content[]" |
||
| 244 | value="additional_info"/> <?php esc_html_e( 'General', 'lsx-wetu-importer' ); ?></li> |
||
| 245 | </ul> |
||
| 246 | </div> |
||
| 247 | |||
| 248 | <?php if ( class_exists( 'LSX_TO_Team' ) ) { ?> |
||
| 249 | <div style="width:30%;display:block;float:left;"> |
||
| 250 | <h3><?php esc_html_e( 'Assign a Team Member' ); ?></h3> |
||
| 251 | <?php $this->team_member_checkboxes( $this->destination_options ); ?> |
||
| 252 | </div> |
||
| 253 | <?php } ?> |
||
| 254 | |||
| 255 | <br clear="both"/> |
||
| 256 | </div> |
||
| 257 | |||
| 258 | |||
| 259 | <h3><?php esc_html_e( 'Your List' ); ?></h3> |
||
| 260 | <p><input class="button button-primary" type="submit" |
||
| 261 | value="<?php esc_html_e( 'Sync', 'lsx-wetu-importer' ); ?>"/></p> |
||
| 262 | <table class="wp-list-table widefat fixed posts"> |
||
| 263 | <?php $this->table_header(); ?> |
||
| 264 | |||
| 265 | <tbody> |
||
| 266 | |||
| 267 | </tbody> |
||
| 268 | |||
| 269 | <?php $this->table_footer(); ?> |
||
| 270 | |||
| 271 | </table> |
||
| 272 | |||
| 273 | <p><input class="button button-primary" type="submit" |
||
| 274 | value="<?php esc_html_e( 'Sync', 'lsx-wetu-importer' ); ?>"/></p> |
||
| 275 | </form> |
||
| 276 | </div> |
||
| 277 | |||
| 278 | <div style="display:none;" class="completed-list-wrapper"> |
||
| 279 | <h3><?php esc_html_e( 'Completed' ); ?></h3> |
||
| 280 | <ul> |
||
| 281 | </ul> |
||
| 282 | </div> |
||
| 283 | </div> |
||
| 284 | <?php |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Grab all the current destination posts via the lsx_wetu_id field. |
||
| 289 | */ |
||
| 290 | public function find_current_destination( $post_type = 'destination' ) { |
||
| 291 | global $wpdb; |
||
| 292 | $return = array(); |
||
| 293 | |||
| 294 | // @codingStandardsIgnoreStart |
||
| 295 | $current_destination = $wpdb->get_results(" |
||
| 296 | SELECT key1.post_id,key1.meta_value,key2.post_title as name,key2.post_date as last_modified |
||
| 297 | FROM {$wpdb->postmeta} key1 |
||
| 298 | |||
| 299 | INNER JOIN {$wpdb->posts} key2 |
||
| 300 | ON key1.post_id = key2.ID |
||
| 301 | |||
| 302 | WHERE key1.meta_key = 'lsx_wetu_id' |
||
| 303 | AND key2.post_type = '{$post_type}' |
||
| 304 | |||
| 305 | LIMIT 0,1000 |
||
| 306 | "); |
||
| 307 | // @codingStandardsIgnoreEnd |
||
| 308 | |||
| 309 | if ( null !== $current_destination && ! empty( $current_destination ) ) { |
||
| 310 | foreach ( $current_destination as $accom ) { |
||
| 311 | $return[ $accom->meta_value ] = $accom; |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | return $return; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Run through the accommodation grabbed from the DB. |
||
| 320 | */ |
||
| 321 | public function process_ajax_search() { |
||
| 322 | $return = false; |
||
| 323 | check_ajax_referer( 'lsx_wetu_ajax_action', 'security' ); |
||
| 324 | if ( isset( $_POST['action'] ) && 'lsx_tour_importer' === $_POST['action'] && isset( $_POST['type'] ) && 'destination' === $_POST['type'] ) { |
||
| 325 | |||
| 326 | $searched_items = false; |
||
| 327 | if ( isset( $_POST['keyword'] ) ) { |
||
| 328 | $keyphrases = array_map( 'sanitize_text_field', wp_unslash( $_POST['keyword'] ) ); |
||
| 329 | } else { |
||
| 330 | $keyphrases = array( 0 ); |
||
| 331 | } |
||
| 332 | |||
| 333 | if ( ! is_array( $keyphrases ) ) { |
||
| 334 | $keyphrases = array( $keyphrases ); |
||
| 335 | } |
||
| 336 | foreach ( $keyphrases as &$keyword ) { |
||
| 337 | $keyword = ltrim( rtrim( $keyword ) ); |
||
| 338 | } |
||
| 339 | |||
| 340 | $post_status = false; |
||
| 341 | |||
| 342 | if ( in_array( 'publish', $keyphrases ) ) { |
||
| 343 | $post_status = 'publish'; |
||
| 344 | } |
||
| 345 | if ( in_array( 'pending', $keyphrases ) ) { |
||
| 346 | $post_status = 'pending'; |
||
| 347 | } |
||
| 348 | if ( in_array( 'draft', $keyphrases ) ) { |
||
| 349 | $post_status = 'draft'; |
||
| 350 | } |
||
| 351 | if ( in_array( 'import', $keyphrases ) ) { |
||
| 352 | $post_status = 'import'; |
||
| 353 | } |
||
| 354 | |||
| 355 | // If there is a post status use it. |
||
| 356 | if ( false !== $post_status ) { |
||
| 357 | |||
| 358 | $accommodation = array(); |
||
| 359 | $current_accommodation = $this->find_current_accommodation( 'destination' ); |
||
| 360 | if ( ! empty( $current_accommodation ) ) { |
||
| 361 | foreach ( $current_accommodation as $cs_key => $ccs_id ) { |
||
| 362 | $accommodation[] = $this->prepare_row_attributes( $cs_key, $ccs_id->post_id ); |
||
| 363 | } |
||
| 364 | } |
||
| 365 | |||
| 366 | // Run through each accommodation and use it. |
||
| 367 | if ( ! empty( $accommodation ) ) { |
||
| 368 | foreach ( $accommodation as $row_key => $row ) { |
||
| 369 | $row['post_title'] = $row['name']; |
||
| 370 | if ( 'import' === $post_status ) { |
||
| 371 | if ( is_array( $this->queued_imports ) && in_array( $row['post_id'], $this->queued_imports ) ) { |
||
| 372 | $current_status = get_post_status( $row['post_id'] ); |
||
| 373 | if ( 'draft' === $current_status ) { |
||
| 374 | $searched_items[ sanitize_title( $row['name'] ) . '-' . $row['id'] ] = $this->format_row( $row, $row_key ); |
||
| 375 | } |
||
| 376 | } else { |
||
| 377 | continue; |
||
| 378 | } |
||
| 379 | } else { |
||
| 380 | if ( 0 === $row['post_id'] ) { |
||
| 381 | continue; |
||
| 382 | } else { |
||
| 383 | $current_status = get_post_status( $row['post_id'] ); |
||
| 384 | |||
| 385 | if ( $current_status !== $post_status ) { |
||
| 386 | continue; |
||
| 387 | } |
||
| 388 | } |
||
| 389 | $searched_items[ sanitize_title( $row['name'] ) . '-' . $row['id'] ] = $this->format_row( $row, $row_key ); |
||
| 390 | } |
||
| 391 | } |
||
| 392 | } |
||
| 393 | } else { |
||
| 394 | |||
| 395 | $key_string_search = implode( '+', $keyphrases ); |
||
| 396 | $search_data = wp_remote_get( $this->url . '/Search/' . $key_string_search . '/?all=include' ); |
||
| 397 | |||
| 398 | if ( ! is_wp_error( $search_data ) || ! empty( $search_data ) && isset( $search_data['response'] ) && isset( $search_data['response']['code'] ) && 200 === $search_data['response']['code'] ) { |
||
| 399 | $search_data = json_decode( $search_data['body'], true ); |
||
| 400 | foreach ( $search_data as $sdata_key => $sdata ) { |
||
| 401 | |||
| 402 | if ( isset( $sdata['type'] ) && 'Destination' !== trim( $sdata['type'] ) && 'Area' !== trim( $sdata['type'] ) ) { |
||
| 403 | continue; |
||
| 404 | } |
||
| 405 | |||
| 406 | $temp_id = $this->get_post_id_by_key_value( $sdata['id'] ); |
||
| 407 | if ( false === $temp_id ) { |
||
| 408 | $sdata['post_id'] = 0; |
||
| 409 | $sdata['post_title'] = $sdata['name']; |
||
| 410 | } else { |
||
| 411 | $sdata['post_id'] = $temp_id; |
||
| 412 | $sdata['post_title'] = get_the_title( $temp_id ); |
||
| 413 | } |
||
| 414 | $searched_items[ sanitize_title( $sdata['name'] ) . '-' . $sdata['id'] ] = $this->format_row( $sdata, $sdata_key ); |
||
| 415 | } |
||
| 416 | } |
||
| 417 | } |
||
| 418 | |||
| 419 | if ( false !== $searched_items ) { |
||
| 420 | $return = implode( $searched_items ); |
||
| 421 | } |
||
| 422 | print_r( $return ); |
||
| 423 | } else { |
||
| 424 | echo esc_attr( 'None found' ); |
||
| 425 | } |
||
| 426 | die(); |
||
| 427 | } |
||
| 428 | |||
| 429 | public function prepare_row_attributes( $cs_key, $ccs_id ) { |
||
| 430 | $row_item = array( |
||
| 431 | 'id' => $cs_key, |
||
| 432 | 'type' => 'Destination', |
||
| 433 | 'name' => get_the_title( $ccs_id ), |
||
| 434 | 'last_modified' => date( 'Y-m-d', strtotime( 'now' ) ), |
||
| 435 | 'post_id' => $ccs_id, |
||
| 436 | ); |
||
| 437 | return $row_item; |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Formats the row for output on the screen. |
||
| 442 | */ |
||
| 443 | public function format_row( $row = false, $row_key = '' ) { |
||
| 444 | if ( false !== $row ) { |
||
| 445 | |||
| 446 | $status = 'import'; |
||
| 447 | if ( 0 !== $row['post_id'] ) { |
||
| 448 | $status = '<a href="' . admin_url( '/post.php?post=' . $row['post_id'] . '&action=edit' ) . '" target="_blank">' . get_post_status( $row['post_id'] ) . '</a>'; |
||
| 449 | } |
||
| 450 | |||
| 451 | $row_html = ' |
||
| 452 | <tr class="post-' . $row['post_id'] . ' type-tour" id="post-' . $row['post_id'] . '"> |
||
| 453 | <th class="check-column" scope="row"> |
||
| 454 | <label for="cb-select-' . $row['id'] . '" class="screen-reader-text">' . $row['name'] . '</label> |
||
| 455 | <input type="checkbox" data-identifier="' . $row['id'] . '" value="' . $row['post_id'] . '" name="post[]" id="cb-select-' . $row['id'] . '"> |
||
| 456 | </th> |
||
| 457 | <td class="column-order"> |
||
| 458 | ' . ( $row_key + 1 ) . ' |
||
| 459 | </td> |
||
| 460 | <td class="post-title page-title column-title"> |
||
| 461 | <strong>' . $row['post_title'] . '</strong> - ' . $status . ' |
||
| 462 | </td> |
||
| 463 | <td class="date column-date"> |
||
| 464 | <abbr title="' . date( 'Y/m/d', strtotime( $row['last_modified'] ) ) . '">' . date( 'Y/m/d', strtotime( $row['last_modified'] ) ) . '</abbr><br>Last Modified |
||
| 465 | </td> |
||
| 466 | <td class="ssid column-ssid"> |
||
| 467 | ' . $row['id'] . ' |
||
| 468 | </td> |
||
| 469 | </tr>'; |
||
| 470 | return $row_html; |
||
| 471 | } |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Connect to wetu |
||
| 476 | */ |
||
| 477 | public function process_ajax_import() { |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Saves the queue to the option. |
||
| 525 | */ |
||
| 526 | public function remove_from_queue( $id ) { |
||
| 527 | if ( ! empty( $this->queued_imports ) ) { |
||
| 528 | $key = array_search( $id, $this->queued_imports ); |
||
| 529 | if ( false !== $key ) { |
||
| 530 | unset( $this->queued_imports[ $key ] ); |
||
| 531 | |||
| 532 | delete_option( 'lsx_wetu_importer_que' ); |
||
| 533 | update_option( 'lsx_wetu_importer_que', $this->queued_imports ); |
||
| 534 | } |
||
| 535 | } |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Connect to wetu |
||
| 540 | */ |
||
| 541 | public function import_row( $data, $wetu_id, $id = 0, $team_members = false, $importable_content = array(), $safari_brands = false ) { |
||
| 542 | if ( 'Destination' === trim( $data[0]['type'] ) || 'Area' === trim( $data[0]['type'] ) ) { |
||
| 543 | $post_name = ''; |
||
| 544 | $data_post_content = ''; |
||
| 545 | $data_post_excerpt = ''; |
||
| 546 | |||
| 547 | $post = array( |
||
| 548 | 'post_type' => 'destination', |
||
| 549 | ); |
||
| 550 | |||
| 551 | if ( ! empty( $importable_content ) && in_array( 'country', $importable_content ) ) { |
||
| 552 | $parent = $this->check_for_parent( $data ); |
||
| 553 | if ( false !== $parent ) { |
||
| 554 | $post['post_parent'] = $parent; |
||
| 555 | } |
||
| 556 | } |
||
| 557 | |||
| 558 | // Set the post_content. |
||
| 559 | if ( ! empty( $importable_content ) && in_array( 'description', $importable_content ) ) { |
||
| 560 | if ( isset( $data[0]['content']['general_description'] ) ) { |
||
| 561 | |||
| 562 | if ( in_array( 'strip_tags', $importable_content ) ) { |
||
| 563 | $post['post_content'] = wp_strip_all_tags( $data[0]['content']['general_description'] ); |
||
| 564 | } else { |
||
| 565 | $post['post_content'] = $data[0]['content']['general_description']; |
||
| 566 | } |
||
| 567 | } |
||
| 568 | } |
||
| 569 | |||
| 570 | if ( false !== $id && '0' !== $id ) { |
||
| 571 | $post['ID'] = $id; |
||
| 572 | if ( isset( $this->options ) && 'on' !== $this->options['disable_destination_title'] && isset( $data[0]['name'] ) ) { |
||
| 573 | $post['post_title'] = $data[0]['name']; |
||
| 574 | $post['post_name'] = wp_unique_post_slug( sanitize_title( $data[0]['name'] ), $id, 'draft', 'destination', 0 ); |
||
| 575 | } |
||
| 576 | $post['post_status'] = 'publish'; |
||
| 577 | |||
| 578 | $id = wp_update_post( $post ); |
||
| 579 | $prev_date = get_post_meta( $id, 'lsx_wetu_modified_date', true ); |
||
| 580 | update_post_meta( $id, 'lsx_wetu_modified_date', strtotime( $data[0]['last_modified'] ), $prev_date ); |
||
| 581 | } else { |
||
| 582 | // Set the name. |
||
| 583 | if ( isset( $data[0]['name'] ) ) { |
||
| 584 | $post_name = wp_unique_post_slug( sanitize_title( $data[0]['name'] ), $id, 'draft', 'destination', 0 ); |
||
| 585 | } |
||
| 586 | |||
| 587 | $post['post_name'] = $post_name; |
||
| 588 | $post['post_title'] = $data[0]['name']; |
||
| 589 | $post['post_status'] = 'publish'; |
||
| 590 | $id = wp_insert_post( $post ); |
||
| 591 | |||
| 592 | // Save the WETU ID and the Last date it was modified. |
||
| 593 | if ( false !== $id ) { |
||
| 594 | add_post_meta( $id, 'lsx_wetu_id', $wetu_id ); |
||
| 595 | add_post_meta( $id, 'lsx_wetu_modified_date', strtotime( $data[0]['last_modified'] ) ); |
||
| 596 | } |
||
| 597 | } |
||
| 598 | |||
| 599 | $this->find_attachments( $id ); |
||
| 600 | |||
| 601 | // Set the team member if it is there. |
||
| 602 | if ( post_type_exists( 'team' ) && false !== $team_members && '' !== $team_members ) { |
||
| 603 | $this->set_team_member( $id, $team_members ); |
||
| 604 | } |
||
| 605 | |||
| 606 | $this->set_map_data( $data, $id, 9 ); |
||
| 607 | |||
| 608 | // Set the Room Data. |
||
| 609 | if ( false !== $importable_content && in_array( 'videos', $importable_content ) ) { |
||
| 610 | $this->set_video_data( $data, $id ); |
||
| 611 | } |
||
| 612 | |||
| 613 | // Set the Electricity. |
||
| 614 | if ( false !== $importable_content && in_array( 'electricity', $importable_content ) ) { |
||
| 615 | $this->set_travel_info( $data, $id, 'electricity', $importable_content ); |
||
| 616 | } |
||
| 617 | // Set the cuisine. |
||
| 618 | if ( false !== $importable_content && in_array( 'cuisine', $importable_content ) ) { |
||
| 619 | $this->set_travel_info( $data, $id, 'cuisine', $importable_content ); |
||
| 620 | } |
||
| 621 | // Set the banking. |
||
| 622 | if ( false !== $importable_content && in_array( 'banking', $importable_content ) ) { |
||
| 623 | $this->set_travel_info( $data, $id, 'banking', $importable_content ); |
||
| 624 | } |
||
| 625 | // Set the transport. |
||
| 626 | if ( false !== $importable_content && in_array( 'transport', $importable_content ) ) { |
||
| 627 | $this->set_travel_info( $data, $id, 'transport', $importable_content ); |
||
| 628 | } |
||
| 629 | // Set the dress. |
||
| 630 | if ( false !== $importable_content && in_array( 'dress', $importable_content ) ) { |
||
| 631 | $this->set_travel_info( $data, $id, 'dress', $importable_content ); |
||
| 632 | } |
||
| 633 | // Set the climate. |
||
| 634 | if ( false !== $importable_content && in_array( 'climate', $importable_content ) ) { |
||
| 635 | $this->set_travel_info( $data, $id, 'climate', $importable_content ); |
||
| 636 | } |
||
| 637 | // Set the Health. |
||
| 638 | if ( false !== $importable_content && in_array( 'health', $importable_content ) ) { |
||
| 639 | $this->set_travel_info( $data, $id, 'health', $importable_content ); |
||
| 640 | } |
||
| 641 | // Set the Safety. |
||
| 642 | if ( false !== $importable_content && in_array( 'safety', $importable_content ) ) { |
||
| 643 | $this->set_travel_info( $data, $id, 'safety', $importable_content ); |
||
| 644 | } |
||
| 645 | // Set the Visa. |
||
| 646 | if ( false !== $importable_content && in_array( 'visa', $importable_content ) ) { |
||
| 647 | $this->set_travel_info( $data, $id, 'visa', $importable_content ); |
||
| 648 | } |
||
| 649 | // Set the General. |
||
| 650 | if ( false !== $importable_content && in_array( 'additional_info', $importable_content ) ) { |
||
| 651 | $this->set_travel_info( $data, $id, 'additional_info', $importable_content ); |
||
| 652 | } |
||
| 653 | |||
| 654 | // Setup some default for use in the import. |
||
| 655 | if ( false !== $importable_content && ( in_array( 'gallery', $importable_content ) || in_array( 'banner_image', $importable_content ) || in_array( 'featured_image', $importable_content ) ) ) { |
||
| 656 | $this->find_attachments( $id ); |
||
| 657 | |||
| 658 | // Set the featured image. |
||
| 659 | if ( false !== $importable_content && in_array( 'featured_image', $importable_content ) ) { |
||
| 660 | $this->set_featured_image( $data, $id ); |
||
| 661 | } |
||
| 662 | if ( false !== $importable_content && in_array( 'banner_image', $importable_content ) ) { |
||
| 663 | $this->set_banner_image( $data, $id, $importable_content ); |
||
| 664 | } |
||
| 665 | // Import the main gallery. |
||
| 666 | if ( false !== $importable_content && in_array( 'gallery', $importable_content ) ) { |
||
| 667 | $this->create_main_gallery( $data, $id ); |
||
| 668 | } |
||
| 669 | } |
||
| 670 | |||
| 671 | // Set the continent. |
||
| 672 | if ( false !== $importable_content && in_array( 'continent', $importable_content ) ) { |
||
| 673 | $this->set_continent( $data, $id ); |
||
| 674 | } |
||
| 675 | } |
||
| 676 | |||
| 677 | return $id; |
||
| 678 | } |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Saves the room data |
||
| 682 | */ |
||
| 683 | public function set_travel_info( $data, $id, $meta_key, $importable = array( 'none' ) ) { |
||
| 684 | if ( ! empty( $data[0]['travel_information'] ) && isset( $data[0]['travel_information'][ $meta_key ] ) ) { |
||
| 685 | $content = $data[0]['travel_information'][ $meta_key ]; |
||
| 686 | |||
| 687 | if ( in_array( 'strip_tags', $importable ) ) { |
||
| 688 | $content = strip_tags( $content ); |
||
| 689 | } |
||
| 690 | |||
| 691 | $this->save_custom_field( $content, $meta_key, $id ); |
||
| 692 | } |
||
| 693 | } |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Set the Travel Style |
||
| 697 | */ |
||
| 698 | public function set_continent( $data, $id ) { |
||
| 699 | |||
| 700 | if ( isset( $data[0]['position']['country'] ) && $data[0]['map_object_id'] === $data[0]['position']['country_content_entity_id'] ) { |
||
| 701 | // Get the continent code. |
||
| 702 | $country_code = to_country_data( $data[0]['position']['country'], false ); |
||
| 703 | $continent_code = to_continent_code( $country_code ); |
||
| 704 | $continent_label = to_continent_label( $continent_code ); |
||
| 705 | |||
| 706 | if ( ! empty( tour_operator()->options['display']['enable_search_region_filter'] ) ) { |
||
| 707 | $continent_label = to_continent_region_label( $country_code ); |
||
| 708 | } |
||
| 709 | |||
| 710 | if ( '' !== $continent_label ) { |
||
| 711 | $term = term_exists( trim( $continent_label ), 'continent' ); |
||
| 712 | if ( ! $term ) { |
||
| 713 | $term = wp_insert_term( trim( $continent_label ), 'continent' ); |
||
| 714 | |||
| 715 | if ( is_wp_error( $term ) ) { |
||
| 716 | echo wp_kses_post( $term->get_error_message() ); |
||
| 717 | } |
||
| 718 | } else { |
||
| 719 | wp_set_object_terms( $id, sanitize_title( $continent_label ), 'continent', true ); |
||
| 720 | } |
||
| 721 | } |
||
| 722 | } |
||
| 723 | } |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Save the list of Accommodation into an option |
||
| 727 | */ |
||
| 728 | public function check_for_parent( $data = array() ) { |
||
| 738 | } |
||
| 739 | } |
||
| 740 |