Passed
Push — master ( 491c9d...051ec6 )
by Warwick
17:03
created
classes/class-lsx-wetu-importer-destination.php 1 patch
Indentation   +506 added lines, -506 removed lines patch added patch discarded remove patch
@@ -9,87 +9,87 @@  discard block
 block discarded – undo
9 9
 
10 10
 class LSX_WETU_Importer_Destination extends LSX_WETU_Importer {
11 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() {
65
-		$this->set_variables();
66
-	}
67
-
68
-	/**
69
-	 * Sets the variables used throughout the plugin.
70
-	 */
71
-	public function set_variables() {
72
-		parent::set_variables();
73
-		$this->url    = 'https://wetu.com/API/Pins/' . $this->api_key;
74
-		$this->url_qs = 'all=include';
75
-		$temp_options = get_option( '_lsx-to_settings', false );
76
-
77
-		if ( false !== $temp_options && isset( $temp_options[ $this->plugin_slug ] ) && ! empty( $temp_options[ $this->plugin_slug ] ) ) {
78
-			$this->options = $temp_options[ $this->plugin_slug ];
79
-		}
80
-
81
-		$destination_options = get_option( 'lsx_wetu_importer_destination_settings', false );
82
-
83
-		if ( false !== $destination_options ) {
84
-			$this->destination_options = $destination_options;
85
-		}
86
-	}
87
-
88
-	/**
89
-	 * Display the importer administration screen
90
-	 */
91
-	public function display_page() {
92
-		?>
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() {
65
+          $this->set_variables();
66
+     }
67
+
68
+     /**
69
+      * Sets the variables used throughout the plugin.
70
+      */
71
+     public function set_variables() {
72
+          parent::set_variables();
73
+          $this->url    = 'https://wetu.com/API/Pins/' . $this->api_key;
74
+          $this->url_qs = 'all=include';
75
+          $temp_options = get_option( '_lsx-to_settings', false );
76
+
77
+          if ( false !== $temp_options && isset( $temp_options[ $this->plugin_slug ] ) && ! empty( $temp_options[ $this->plugin_slug ] ) ) {
78
+               $this->options = $temp_options[ $this->plugin_slug ];
79
+          }
80
+
81
+          $destination_options = get_option( 'lsx_wetu_importer_destination_settings', false );
82
+
83
+          if ( false !== $destination_options ) {
84
+               $this->destination_options = $destination_options;
85
+          }
86
+     }
87
+
88
+     /**
89
+      * Display the importer administration screen
90
+      */
91
+     public function display_page() {
92
+          ?>
93 93
 		<div class="wrap">
94 94
 			<div class="tablenav top">
95 95
 				<div class="actions">
@@ -279,17 +279,17 @@  discard block
 block discarded – undo
279 279
 			</div>
280 280
 		</div>
281 281
 		<?php
282
-	}
282
+     }
283 283
 
284
-	/**
285
-	 * Grab all the current destination posts via the lsx_wetu_id field.
286
-	 */
287
-	public function find_current_destination( $post_type = 'destination' ) {
288
-		global $wpdb;
289
-		$return = array();
284
+     /**
285
+      * Grab all the current destination posts via the lsx_wetu_id field.
286
+      */
287
+     public function find_current_destination( $post_type = 'destination' ) {
288
+          global $wpdb;
289
+          $return = array();
290 290
 
291
-		// @codingStandardsIgnoreStart
292
-		$current_destination = $wpdb->get_results("
291
+          // @codingStandardsIgnoreStart
292
+          $current_destination = $wpdb->get_results("
293 293
 			SELECT key1.post_id,key1.meta_value,key2.post_title as name,key2.post_date as last_modified
294 294
 			FROM {$wpdb->postmeta} key1
295 295
 
@@ -301,151 +301,151 @@  discard block
 block discarded – undo
301 301
 
302 302
 			LIMIT 0,1000
303 303
 		");
304
-		// @codingStandardsIgnoreEnd
305
-
306
-		if ( null !== $current_destination && ! empty( $current_destination ) ) {
307
-			foreach ( $current_destination as $accom ) {
308
-				$return[ $accom->meta_value ] = $accom;
309
-			}
310
-		}
311
-
312
-		return $return;
313
-	}
314
-
315
-	/**
316
-	 * Run through the accommodation grabbed from the DB.
317
-	 */
318
-	public function process_ajax_search() {
319
-		$return = false;
320
-		check_ajax_referer( 'lsx_wetu_ajax_action', 'security' );
321
-		if ( isset( $_POST['action'] ) && 'lsx_tour_importer' === $_POST['action'] && isset( $_POST['type'] ) && 'destination' === $_POST['type'] ) {
322
-
323
-			$searched_items = false;
324
-			if ( isset( $_POST['keyword'] ) ) {
325
-				$keyphrases = array_map( 'sanitize_text_field', wp_unslash( $_POST['keyword'] ) );
326
-			} else {
327
-				$keyphrases = array( 0 );
328
-			}
329
-
330
-			if ( ! is_array( $keyphrases ) ) {
331
-				$keyphrases = array( $keyphrases );
332
-			}
333
-			foreach ( $keyphrases as &$keyword ) {
334
-				$keyword = ltrim( rtrim( $keyword ) );
335
-			}
336
-
337
-			$post_status = false;
338
-
339
-			if ( in_array( 'publish', $keyphrases ) ) {
340
-				$post_status = 'publish';
341
-			}
342
-			if ( in_array( 'pending', $keyphrases ) ) {
343
-				$post_status = 'pending';
344
-			}
345
-			if ( in_array( 'draft', $keyphrases ) ) {
346
-				$post_status = 'draft';
347
-			}
348
-			if ( in_array( 'import', $keyphrases ) ) {
349
-				$post_status = 'import';
350
-			}
351
-
352
-			// If there is a post status use it.
353
-			if ( false !== $post_status ) {
354
-
355
-				$accommodation = array();
356
-				$current_accommodation = $this->find_current_accommodation( 'destination' );
357
-				if ( ! empty( $current_accommodation ) ) {
358
-					foreach ( $current_accommodation as $cs_key => $ccs_id ) {
359
-						$accommodation[] = $this->prepare_row_attributes( $cs_key, $ccs_id->post_id );
360
-					}
361
-				}
362
-
363
-				// Run through each accommodation and use it.
364
-				if ( ! empty( $accommodation ) ) {
365
-					foreach ( $accommodation as $row_key => $row ) {
366
-						$row['post_title'] = $row['name'];
367
-						if ( 'import' === $post_status ) {
368
-							if ( is_array( $this->queued_imports ) && in_array( $row['post_id'], $this->queued_imports ) ) {
369
-								$current_status = get_post_status( $row['post_id'] );
370
-								if ( 'draft' === $current_status ) {
371
-									$searched_items[ sanitize_title( $row['name'] ) . '-' . $row['id'] ] = $this->format_row( $row, $row_key );
372
-								}
373
-							} else {
374
-								continue;
375
-							}
376
-						} else {
377
-							if ( 0 === $row['post_id'] ) {
378
-								continue;
379
-							} else {
380
-								$current_status = get_post_status( $row['post_id'] );
381
-
382
-								if ( $current_status !== $post_status ) {
383
-									continue;
384
-								}
385
-							}
386
-							$searched_items[ sanitize_title( $row['name'] ) . '-' . $row['id'] ] = $this->format_row( $row, $row_key );
387
-						}
388
-					}
389
-				}
390
-			} else {
391
-
392
-				$key_string_search = implode( '+', $keyphrases );
393
-				$search_data       = wp_remote_get( $this->url . '/Search/' . $key_string_search . '/?all=include' );
394
-
395
-				if ( ! is_wp_error( $search_data ) || ! empty( $search_data ) && isset( $search_data['response'] ) && isset( $search_data['response']['code'] ) && 200 === $search_data['response']['code'] ) {
396
-					$search_data = json_decode( $search_data['body'], true );
397
-					foreach ( $search_data as $sdata_key => $sdata ) {
398
-
399
-						if ( isset( $sdata['type'] ) && 'Destination' !== trim( $sdata['type'] ) && 'Area' !== trim( $sdata['type'] ) ) {
400
-							continue;
401
-						}
402
-
403
-						$temp_id = $this->get_post_id_by_key_value( $sdata['id'] );
404
-						if ( false === $temp_id ) {
405
-							$sdata['post_id'] = 0;
406
-							$sdata['post_title'] = $sdata['name'];
407
-						} else {
408
-							$sdata['post_id'] = $temp_id;
409
-							$sdata['post_title'] = get_the_title( $temp_id );
410
-						}
411
-						$searched_items[ sanitize_title( $sdata['name'] ) . '-' . $sdata['id'] ] = $this->format_row( $sdata, $sdata_key );
412
-					}
413
-				}
414
-			}
415
-
416
-			if ( false !== $searched_items ) {
417
-				$return = implode( $searched_items );
418
-			}
419
-			print_r( $return );
420
-		} else {
421
-			echo esc_attr( 'None found' );
422
-		}
423
-		die();
424
-	}
425
-
426
-	public function prepare_row_attributes( $cs_key, $ccs_id ) {
427
-		$row_item = array(
428
-			'id' => $cs_key,
429
-			'type' => 'Destination',
430
-			'name' => get_the_title( $ccs_id ),
431
-			'last_modified' => date( 'Y-m-d', strtotime( 'now' ) ),
432
-			'post_id' => $ccs_id,
433
-		);
434
-		return $row_item;
435
-	}
436
-
437
-	/**
438
-	 * Formats the row for output on the screen.
439
-	 */
440
-	public function format_row( $row = false, $row_key = '' ) {
441
-		if ( false !== $row ) {
442
-
443
-			$status = 'import';
444
-			if ( 0 !== $row['post_id'] ) {
445
-				$status = '<a href="' . admin_url( '/post.php?post=' . $row['post_id'] . '&action=edit' ) . '" target="_blank">' . get_post_status( $row['post_id'] ) . '</a>';
446
-			}
447
-
448
-			$row_html = '
304
+          // @codingStandardsIgnoreEnd
305
+
306
+          if ( null !== $current_destination && ! empty( $current_destination ) ) {
307
+               foreach ( $current_destination as $accom ) {
308
+                    $return[ $accom->meta_value ] = $accom;
309
+               }
310
+          }
311
+
312
+          return $return;
313
+     }
314
+
315
+     /**
316
+      * Run through the accommodation grabbed from the DB.
317
+      */
318
+     public function process_ajax_search() {
319
+          $return = false;
320
+          check_ajax_referer( 'lsx_wetu_ajax_action', 'security' );
321
+          if ( isset( $_POST['action'] ) && 'lsx_tour_importer' === $_POST['action'] && isset( $_POST['type'] ) && 'destination' === $_POST['type'] ) {
322
+
323
+               $searched_items = false;
324
+               if ( isset( $_POST['keyword'] ) ) {
325
+                    $keyphrases = array_map( 'sanitize_text_field', wp_unslash( $_POST['keyword'] ) );
326
+               } else {
327
+                    $keyphrases = array( 0 );
328
+               }
329
+
330
+               if ( ! is_array( $keyphrases ) ) {
331
+                    $keyphrases = array( $keyphrases );
332
+               }
333
+               foreach ( $keyphrases as &$keyword ) {
334
+                    $keyword = ltrim( rtrim( $keyword ) );
335
+               }
336
+
337
+               $post_status = false;
338
+
339
+               if ( in_array( 'publish', $keyphrases ) ) {
340
+                    $post_status = 'publish';
341
+               }
342
+               if ( in_array( 'pending', $keyphrases ) ) {
343
+                    $post_status = 'pending';
344
+               }
345
+               if ( in_array( 'draft', $keyphrases ) ) {
346
+                    $post_status = 'draft';
347
+               }
348
+               if ( in_array( 'import', $keyphrases ) ) {
349
+                    $post_status = 'import';
350
+               }
351
+
352
+               // If there is a post status use it.
353
+               if ( false !== $post_status ) {
354
+
355
+                    $accommodation = array();
356
+                    $current_accommodation = $this->find_current_accommodation( 'destination' );
357
+                    if ( ! empty( $current_accommodation ) ) {
358
+                         foreach ( $current_accommodation as $cs_key => $ccs_id ) {
359
+                              $accommodation[] = $this->prepare_row_attributes( $cs_key, $ccs_id->post_id );
360
+                         }
361
+                    }
362
+
363
+                    // Run through each accommodation and use it.
364
+                    if ( ! empty( $accommodation ) ) {
365
+                         foreach ( $accommodation as $row_key => $row ) {
366
+                              $row['post_title'] = $row['name'];
367
+                              if ( 'import' === $post_status ) {
368
+                                   if ( is_array( $this->queued_imports ) && in_array( $row['post_id'], $this->queued_imports ) ) {
369
+                                        $current_status = get_post_status( $row['post_id'] );
370
+                                        if ( 'draft' === $current_status ) {
371
+                                             $searched_items[ sanitize_title( $row['name'] ) . '-' . $row['id'] ] = $this->format_row( $row, $row_key );
372
+                                        }
373
+                                   } else {
374
+                                        continue;
375
+                                   }
376
+                              } else {
377
+                                   if ( 0 === $row['post_id'] ) {
378
+                                        continue;
379
+                                   } else {
380
+                                        $current_status = get_post_status( $row['post_id'] );
381
+
382
+                                        if ( $current_status !== $post_status ) {
383
+                                             continue;
384
+                                        }
385
+                                   }
386
+                                   $searched_items[ sanitize_title( $row['name'] ) . '-' . $row['id'] ] = $this->format_row( $row, $row_key );
387
+                              }
388
+                         }
389
+                    }
390
+               } else {
391
+
392
+                    $key_string_search = implode( '+', $keyphrases );
393
+                    $search_data       = wp_remote_get( $this->url . '/Search/' . $key_string_search . '/?all=include' );
394
+
395
+                    if ( ! is_wp_error( $search_data ) || ! empty( $search_data ) && isset( $search_data['response'] ) && isset( $search_data['response']['code'] ) && 200 === $search_data['response']['code'] ) {
396
+                         $search_data = json_decode( $search_data['body'], true );
397
+                         foreach ( $search_data as $sdata_key => $sdata ) {
398
+
399
+                              if ( isset( $sdata['type'] ) && 'Destination' !== trim( $sdata['type'] ) && 'Area' !== trim( $sdata['type'] ) ) {
400
+                                   continue;
401
+                              }
402
+
403
+                              $temp_id = $this->get_post_id_by_key_value( $sdata['id'] );
404
+                              if ( false === $temp_id ) {
405
+                                   $sdata['post_id'] = 0;
406
+                                   $sdata['post_title'] = $sdata['name'];
407
+                              } else {
408
+                                   $sdata['post_id'] = $temp_id;
409
+                                   $sdata['post_title'] = get_the_title( $temp_id );
410
+                              }
411
+                              $searched_items[ sanitize_title( $sdata['name'] ) . '-' . $sdata['id'] ] = $this->format_row( $sdata, $sdata_key );
412
+                         }
413
+                    }
414
+               }
415
+
416
+               if ( false !== $searched_items ) {
417
+                    $return = implode( $searched_items );
418
+               }
419
+               print_r( $return );
420
+          } else {
421
+               echo esc_attr( 'None found' );
422
+          }
423
+          die();
424
+     }
425
+
426
+     public function prepare_row_attributes( $cs_key, $ccs_id ) {
427
+          $row_item = array(
428
+               'id' => $cs_key,
429
+               'type' => 'Destination',
430
+               'name' => get_the_title( $ccs_id ),
431
+               'last_modified' => date( 'Y-m-d', strtotime( 'now' ) ),
432
+               'post_id' => $ccs_id,
433
+          );
434
+          return $row_item;
435
+     }
436
+
437
+     /**
438
+      * Formats the row for output on the screen.
439
+      */
440
+     public function format_row( $row = false, $row_key = '' ) {
441
+          if ( false !== $row ) {
442
+
443
+               $status = 'import';
444
+               if ( 0 !== $row['post_id'] ) {
445
+                    $status = '<a href="' . admin_url( '/post.php?post=' . $row['post_id'] . '&action=edit' ) . '" target="_blank">' . get_post_status( $row['post_id'] ) . '</a>';
446
+               }
447
+
448
+               $row_html = '
449 449
 			<tr class="post-' . $row['post_id'] . ' type-tour" id="post-' . $row['post_id'] . '">
450 450
 				<th class="check-column" scope="row">
451 451
 					<label for="cb-select-' . $row['id'] . '" class="screen-reader-text">' . $row['name'] . '</label>
@@ -464,275 +464,275 @@  discard block
 block discarded – undo
464 464
 					' . $row['id'] . '
465 465
 				</td>
466 466
 			</tr>';
467
-			return $row_html;
468
-		}
469
-	}
470
-
471
-	/**
472
-	 * Connect to wetu
473
-	 */
474
-	public function process_ajax_import() {
475
-		$return = false;
476
-
477
-		check_ajax_referer( 'lsx_wetu_ajax_action', 'security' );
478
-		if ( isset( $_POST['action'] ) && 'lsx_import_items' === $_POST['action'] && isset( $_POST['type'] ) && 'destination' === $_POST['type'] && isset( $_POST['wetu_id'] ) ) {
479
-
480
-			$wetu_id = sanitize_text_field( $_POST['wetu_id'] );
481
-
482
-			if ( isset( $_POST['post_id'] ) ) {
483
-				$post_id = sanitize_text_field( $_POST['post_id'] );
484
-				$this->current_post = get_post( $post_id );
485
-			} else {
486
-				$post_id = 0;
487
-			}
488
-
489
-			if ( isset( $_POST['team_members'] ) ) {
490
-				$team_members = array_map( 'sanitize_text_field', wp_unslash( $_POST['team_members'] ) );
491
-			} else {
492
-				$team_members = false;
493
-			}
494
-
495
-			$safari_brands = false;
496
-
497
-			delete_option( 'lsx_wetu_importer_destination_settings' );
498
-
499
-			if ( isset( $_POST['content'] ) && is_array( $_POST['content'] ) && ! empty( $_POST['content'] ) ) {
500
-				$content = array_map( 'sanitize_text_field', wp_unslash( $_POST['content'] ) );
501
-				add_option( 'lsx_wetu_importer_destination_settings', $content );
502
-			} else {
503
-				$content = false;
504
-			}
505
-
506
-			$jdata = wp_remote_get( $this->url . '/Get?' . $this->url_qs . '&ids=' . $wetu_id );
507
-
508
-			if ( ! empty( $jdata ) && isset( $jdata['response'] ) && isset( $jdata['response']['code'] ) && 200 === $jdata['response']['code'] ) {
509
-				$adata  = json_decode( $jdata['body'], true );
510
-				$return = $this->import_row( $adata, $wetu_id, $post_id, $team_members, $content, $safari_brands );
511
-				$this->remove_from_queue( $return );
512
-				$this->format_completed_row( $return );
513
-			} else {
514
-				$this->format_error( esc_html__( 'There was a problem importing your destination, please try refreshing the page.', 'lsx-wetu-importer' ) );
515
-			}
516
-		}
517
-		die();
518
-	}
519
-
520
-	/**
521
-	 * Saves the queue to the option.
522
-	 */
523
-	public function remove_from_queue( $id ) {
524
-		if ( ! empty( $this->queued_imports ) ) {
525
-			$key = array_search( $id, $this->queued_imports );
526
-			if ( false !== $key ) {
527
-				unset( $this->queued_imports[ $key ] );
528
-
529
-				delete_option( 'lsx_wetu_importer_que' );
530
-				update_option( 'lsx_wetu_importer_que', $this->queued_imports );
531
-			}
532
-		}
533
-	}
534
-
535
-	/**
536
-	 * Connect to wetu
537
-	 */
538
-	public function import_row( $data, $wetu_id, $id = 0, $team_members = false, $importable_content = array(), $safari_brands = false ) {
539
-		if ( 'Destination' === trim( $data[0]['type'] ) || 'Area' === trim( $data[0]['type'] ) ) {
540
-			$post_name = '';
541
-			$data_post_content = '';
542
-			$data_post_excerpt = '';
543
-
544
-			$post = array(
545
-				'post_type' => 'destination',
546
-			);
547
-
548
-			if ( ! empty( $importable_content ) && in_array( 'country', $importable_content ) ) {
549
-				$parent = $this->check_for_parent( $data );
550
-				if ( false !== $parent ) {
551
-					$post['post_parent'] = $parent;
552
-				}
553
-			}
554
-
555
-			// Set the post_content.
556
-			if ( ! empty( $importable_content ) && in_array( 'description', $importable_content ) ) {
557
-				if ( isset( $data[0]['content']['general_description'] ) ) {
558
-
559
-					if ( in_array( 'strip_tags', $importable_content ) ) {
560
-						$post['post_content'] = wp_strip_all_tags( $data[0]['content']['general_description'] );
561
-					} else {
562
-						$post['post_content'] = $data[0]['content']['general_description'];
563
-					}
564
-				}
565
-			}
566
-
567
-			if ( false !== $id && '0' !== $id ) {
568
-				$post['ID'] = $id;
569
-				if ( isset( $this->options ) && 'on' !== $this->options['disable_destination_title'] && isset( $data[0]['name'] ) ) {
570
-					$post['post_title'] = $data[0]['name'];
571
-					$post['post_name'] = wp_unique_post_slug( sanitize_title( $data[0]['name'] ), $id, 'draft', 'destination', 0 );
572
-				}
573
-				$post['post_status'] = 'publish';
574
-
575
-				$id = wp_update_post( $post );
576
-				$prev_date = get_post_meta( $id, 'lsx_wetu_modified_date', true );
577
-				update_post_meta( $id, 'lsx_wetu_modified_date', strtotime( $data[0]['last_modified'] ), $prev_date );
578
-			} else {
579
-				// Set the name.
580
-				if ( isset( $data[0]['name'] ) ) {
581
-					$post_name = wp_unique_post_slug( sanitize_title( $data[0]['name'] ), $id, 'draft', 'destination', 0 );
582
-				}
583
-
584
-				$post['post_name'] = $post_name;
585
-				$post['post_title'] = $data[0]['name'];
586
-				$post['post_status'] = 'publish';
587
-				$id = wp_insert_post( $post );
588
-
589
-				// Save the WETU ID and the Last date it was modified.
590
-				if ( false !== $id ) {
591
-					add_post_meta( $id, 'lsx_wetu_id', $wetu_id );
592
-					add_post_meta( $id, 'lsx_wetu_modified_date', strtotime( $data[0]['last_modified'] ) );
593
-				}
594
-			}
595
-
596
-			$this->find_attachments( $id );
597
-
598
-			// Set the team member if it is there.
599
-			if ( post_type_exists( 'team' ) && false !== $team_members && '' !== $team_members ) {
600
-				$this->set_team_member( $id, $team_members );
601
-			}
602
-
603
-			$this->set_map_data( $data, $id, 9 );
604
-
605
-			// Set the Room Data.
606
-			if ( false !== $importable_content && in_array( 'videos', $importable_content ) ) {
607
-				$this->set_video_data( $data, $id );
608
-			}
609
-
610
-			// Set the Electricity.
611
-			if ( false !== $importable_content && in_array( 'electricity', $importable_content ) ) {
612
-				$this->set_travel_info( $data, $id, 'electricity', $importable_content );
613
-			}
614
-			// Set the cuisine.
615
-			if ( false !== $importable_content && in_array( 'cuisine', $importable_content ) ) {
616
-				$this->set_travel_info( $data, $id, 'cuisine', $importable_content );
617
-			}
618
-			// Set the banking.
619
-			if ( false !== $importable_content && in_array( 'banking', $importable_content ) ) {
620
-				$this->set_travel_info( $data, $id, 'banking', $importable_content );
621
-			}
622
-			// Set the transport.
623
-			if ( false !== $importable_content && in_array( 'transport', $importable_content ) ) {
624
-				$this->set_travel_info( $data, $id, 'transport', $importable_content );
625
-			}
626
-			// Set the dress.
627
-			if ( false !== $importable_content && in_array( 'dress', $importable_content ) ) {
628
-				$this->set_travel_info( $data, $id, 'dress', $importable_content );
629
-			}
630
-			// Set the climate.
631
-			if ( false !== $importable_content && in_array( 'climate', $importable_content ) ) {
632
-				$this->set_travel_info( $data, $id, 'climate', $importable_content );
633
-			}
634
-			// Set the Health.
635
-			if ( false !== $importable_content && in_array( 'health', $importable_content ) ) {
636
-				$this->set_travel_info( $data, $id, 'health', $importable_content );
637
-			}
638
-			// Set the Safety.
639
-			if ( false !== $importable_content && in_array( 'safety', $importable_content ) ) {
640
-				$this->set_travel_info( $data, $id, 'safety', $importable_content );
641
-			}
642
-			// Set the Visa.
643
-			if ( false !== $importable_content && in_array( 'visa', $importable_content ) ) {
644
-				$this->set_travel_info( $data, $id, 'visa', $importable_content );
645
-			}
646
-			// Set the General.
647
-			if ( false !== $importable_content && in_array( 'additional_info', $importable_content ) ) {
648
-				$this->set_travel_info( $data, $id, 'additional_info', $importable_content );
649
-			}
650
-
651
-			// Setup some default for use in the import.
652
-			if ( false !== $importable_content && (in_array( 'gallery', $importable_content ) || in_array( 'banner_image', $importable_content ) || in_array( 'featured_image', $importable_content )) ) {
653
-				$this->find_attachments( $id );
654
-
655
-				// Set the featured image.
656
-				if ( false !== $importable_content && in_array( 'featured_image', $importable_content ) ) {
657
-					$this->set_featured_image( $data, $id );
658
-				}
659
-				var_dump($importable_content);
660
-				if ( false !== $importable_content && in_array( 'banner_image', $importable_content ) ) {
661
-					var_dump($id);
662
-					$this->set_banner_image( $data, $id, $importable_content );
663
-				}
664
-				// Import the main gallery.
665
-				if ( false !== $importable_content && in_array( 'gallery', $importable_content ) ) {
666
-					$this->create_main_gallery( $data, $id );
667
-				}
668
-			}
669
-
670
-			// Set the continent.
671
-			if ( false !== $importable_content && in_array( 'continent', $importable_content ) ) {
672
-				$this->set_continent( $data, $id );
673
-			}
674
-		}
675
-
676
-		return $id;
677
-	}
678
-
679
-	/**
680
-	 * Saves the room data
681
-	 */
682
-	public function set_travel_info( $data, $id, $meta_key, $importable = array( 'none' ) ) {
683
-		if ( ! empty( $data[0]['travel_information'] ) && isset( $data[0]['travel_information'][ $meta_key ] ) ) {
684
-			$content = $data[0]['travel_information'][ $meta_key ];
685
-
686
-			if ( in_array( 'strip_tags', $importable ) ) {
687
-				$content = strip_tags( $content );
688
-			}
689
-
690
-			$this->save_custom_field( $content, $meta_key, $id );
691
-		}
692
-	}
693
-
694
-	/**
695
-	 * Set the Travel Style
696
-	 */
697
-	public function set_continent( $data, $id ) {
698
-
699
-		if ( isset( $data[0]['position']['country'] ) && $data[0]['map_object_id'] === $data[0]['position']['country_content_entity_id'] ) {
700
-			// Get the continent code.
701
-			$country_code    = to_country_data( $data[0]['position']['country'], false );
702
-			$continent_code  = to_continent_code( $country_code );
703
-			$continent_label = to_continent_label( $continent_code );
704
-
705
-			if ( ! empty( tour_operator()->options['display']['enable_search_region_filter'] ) ) {
706
-				$continent_label = to_continent_region_label( $country_code );
707
-			}
708
-
709
-			if ( '' !== $continent_label ) {
710
-				$term = term_exists( trim( $continent_label ), 'continent' );
711
-				if ( ! $term ) {
712
-					$term = wp_insert_term( trim( $continent_label ), 'continent' );
713
-
714
-					if ( is_wp_error( $term ) ) {
715
-						echo wp_kses_post( $term->get_error_message() );
716
-					}
717
-				} else {
718
-					wp_set_object_terms( $id, sanitize_title( $continent_label ), 'continent', true );
719
-				}
720
-			}
721
-		}
722
-	}
723
-
724
-	/**
725
-	 * Save the list of Accommodation into an option
726
-	 */
727
-	public function check_for_parent( $data = array() ) {
728
-		global $wpdb;
729
-
730
-		if ( $data[0]['position']['country_content_entity_id'] !== $data[0]['position']['destination_content_entity_id'] ) {
731
-			$result = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'lsx_wetu_id' AND meta_value = '%s'", array( $data[0]['position']['country_content_entity_id'] ) ) );
732
-			if ( ! empty( $result ) && '' !== $result && false !== $result ) {
733
-				return $result;
734
-			}
735
-		}
736
-		return false;
737
-	}
467
+               return $row_html;
468
+          }
469
+     }
470
+
471
+     /**
472
+      * Connect to wetu
473
+      */
474
+     public function process_ajax_import() {
475
+          $return = false;
476
+
477
+          check_ajax_referer( 'lsx_wetu_ajax_action', 'security' );
478
+          if ( isset( $_POST['action'] ) && 'lsx_import_items' === $_POST['action'] && isset( $_POST['type'] ) && 'destination' === $_POST['type'] && isset( $_POST['wetu_id'] ) ) {
479
+
480
+               $wetu_id = sanitize_text_field( $_POST['wetu_id'] );
481
+
482
+               if ( isset( $_POST['post_id'] ) ) {
483
+                    $post_id = sanitize_text_field( $_POST['post_id'] );
484
+                    $this->current_post = get_post( $post_id );
485
+               } else {
486
+                    $post_id = 0;
487
+               }
488
+
489
+               if ( isset( $_POST['team_members'] ) ) {
490
+                    $team_members = array_map( 'sanitize_text_field', wp_unslash( $_POST['team_members'] ) );
491
+               } else {
492
+                    $team_members = false;
493
+               }
494
+
495
+               $safari_brands = false;
496
+
497
+               delete_option( 'lsx_wetu_importer_destination_settings' );
498
+
499
+               if ( isset( $_POST['content'] ) && is_array( $_POST['content'] ) && ! empty( $_POST['content'] ) ) {
500
+                    $content = array_map( 'sanitize_text_field', wp_unslash( $_POST['content'] ) );
501
+                    add_option( 'lsx_wetu_importer_destination_settings', $content );
502
+               } else {
503
+                    $content = false;
504
+               }
505
+
506
+               $jdata = wp_remote_get( $this->url . '/Get?' . $this->url_qs . '&ids=' . $wetu_id );
507
+
508
+               if ( ! empty( $jdata ) && isset( $jdata['response'] ) && isset( $jdata['response']['code'] ) && 200 === $jdata['response']['code'] ) {
509
+                    $adata  = json_decode( $jdata['body'], true );
510
+                    $return = $this->import_row( $adata, $wetu_id, $post_id, $team_members, $content, $safari_brands );
511
+                    $this->remove_from_queue( $return );
512
+                    $this->format_completed_row( $return );
513
+               } else {
514
+                    $this->format_error( esc_html__( 'There was a problem importing your destination, please try refreshing the page.', 'lsx-wetu-importer' ) );
515
+               }
516
+          }
517
+          die();
518
+     }
519
+
520
+     /**
521
+      * Saves the queue to the option.
522
+      */
523
+     public function remove_from_queue( $id ) {
524
+          if ( ! empty( $this->queued_imports ) ) {
525
+               $key = array_search( $id, $this->queued_imports );
526
+               if ( false !== $key ) {
527
+                    unset( $this->queued_imports[ $key ] );
528
+
529
+                    delete_option( 'lsx_wetu_importer_que' );
530
+                    update_option( 'lsx_wetu_importer_que', $this->queued_imports );
531
+               }
532
+          }
533
+     }
534
+
535
+     /**
536
+      * Connect to wetu
537
+      */
538
+     public function import_row( $data, $wetu_id, $id = 0, $team_members = false, $importable_content = array(), $safari_brands = false ) {
539
+          if ( 'Destination' === trim( $data[0]['type'] ) || 'Area' === trim( $data[0]['type'] ) ) {
540
+               $post_name = '';
541
+               $data_post_content = '';
542
+               $data_post_excerpt = '';
543
+
544
+               $post = array(
545
+                    'post_type' => 'destination',
546
+               );
547
+
548
+               if ( ! empty( $importable_content ) && in_array( 'country', $importable_content ) ) {
549
+                    $parent = $this->check_for_parent( $data );
550
+                    if ( false !== $parent ) {
551
+                         $post['post_parent'] = $parent;
552
+                    }
553
+               }
554
+
555
+               // Set the post_content.
556
+               if ( ! empty( $importable_content ) && in_array( 'description', $importable_content ) ) {
557
+                    if ( isset( $data[0]['content']['general_description'] ) ) {
558
+
559
+                         if ( in_array( 'strip_tags', $importable_content ) ) {
560
+                              $post['post_content'] = wp_strip_all_tags( $data[0]['content']['general_description'] );
561
+                         } else {
562
+                              $post['post_content'] = $data[0]['content']['general_description'];
563
+                         }
564
+                    }
565
+               }
566
+
567
+               if ( false !== $id && '0' !== $id ) {
568
+                    $post['ID'] = $id;
569
+                    if ( isset( $this->options ) && 'on' !== $this->options['disable_destination_title'] && isset( $data[0]['name'] ) ) {
570
+                         $post['post_title'] = $data[0]['name'];
571
+                         $post['post_name'] = wp_unique_post_slug( sanitize_title( $data[0]['name'] ), $id, 'draft', 'destination', 0 );
572
+                    }
573
+                    $post['post_status'] = 'publish';
574
+
575
+                    $id = wp_update_post( $post );
576
+                    $prev_date = get_post_meta( $id, 'lsx_wetu_modified_date', true );
577
+                    update_post_meta( $id, 'lsx_wetu_modified_date', strtotime( $data[0]['last_modified'] ), $prev_date );
578
+               } else {
579
+                    // Set the name.
580
+                    if ( isset( $data[0]['name'] ) ) {
581
+                         $post_name = wp_unique_post_slug( sanitize_title( $data[0]['name'] ), $id, 'draft', 'destination', 0 );
582
+                    }
583
+
584
+                    $post['post_name'] = $post_name;
585
+                    $post['post_title'] = $data[0]['name'];
586
+                    $post['post_status'] = 'publish';
587
+                    $id = wp_insert_post( $post );
588
+
589
+                    // Save the WETU ID and the Last date it was modified.
590
+                    if ( false !== $id ) {
591
+                         add_post_meta( $id, 'lsx_wetu_id', $wetu_id );
592
+                         add_post_meta( $id, 'lsx_wetu_modified_date', strtotime( $data[0]['last_modified'] ) );
593
+                    }
594
+               }
595
+
596
+               $this->find_attachments( $id );
597
+
598
+               // Set the team member if it is there.
599
+               if ( post_type_exists( 'team' ) && false !== $team_members && '' !== $team_members ) {
600
+                    $this->set_team_member( $id, $team_members );
601
+               }
602
+
603
+               $this->set_map_data( $data, $id, 9 );
604
+
605
+               // Set the Room Data.
606
+               if ( false !== $importable_content && in_array( 'videos', $importable_content ) ) {
607
+                    $this->set_video_data( $data, $id );
608
+               }
609
+
610
+               // Set the Electricity.
611
+               if ( false !== $importable_content && in_array( 'electricity', $importable_content ) ) {
612
+                    $this->set_travel_info( $data, $id, 'electricity', $importable_content );
613
+               }
614
+               // Set the cuisine.
615
+               if ( false !== $importable_content && in_array( 'cuisine', $importable_content ) ) {
616
+                    $this->set_travel_info( $data, $id, 'cuisine', $importable_content );
617
+               }
618
+               // Set the banking.
619
+               if ( false !== $importable_content && in_array( 'banking', $importable_content ) ) {
620
+                    $this->set_travel_info( $data, $id, 'banking', $importable_content );
621
+               }
622
+               // Set the transport.
623
+               if ( false !== $importable_content && in_array( 'transport', $importable_content ) ) {
624
+                    $this->set_travel_info( $data, $id, 'transport', $importable_content );
625
+               }
626
+               // Set the dress.
627
+               if ( false !== $importable_content && in_array( 'dress', $importable_content ) ) {
628
+                    $this->set_travel_info( $data, $id, 'dress', $importable_content );
629
+               }
630
+               // Set the climate.
631
+               if ( false !== $importable_content && in_array( 'climate', $importable_content ) ) {
632
+                    $this->set_travel_info( $data, $id, 'climate', $importable_content );
633
+               }
634
+               // Set the Health.
635
+               if ( false !== $importable_content && in_array( 'health', $importable_content ) ) {
636
+                    $this->set_travel_info( $data, $id, 'health', $importable_content );
637
+               }
638
+               // Set the Safety.
639
+               if ( false !== $importable_content && in_array( 'safety', $importable_content ) ) {
640
+                    $this->set_travel_info( $data, $id, 'safety', $importable_content );
641
+               }
642
+               // Set the Visa.
643
+               if ( false !== $importable_content && in_array( 'visa', $importable_content ) ) {
644
+                    $this->set_travel_info( $data, $id, 'visa', $importable_content );
645
+               }
646
+               // Set the General.
647
+               if ( false !== $importable_content && in_array( 'additional_info', $importable_content ) ) {
648
+                    $this->set_travel_info( $data, $id, 'additional_info', $importable_content );
649
+               }
650
+
651
+               // Setup some default for use in the import.
652
+               if ( false !== $importable_content && (in_array( 'gallery', $importable_content ) || in_array( 'banner_image', $importable_content ) || in_array( 'featured_image', $importable_content )) ) {
653
+                    $this->find_attachments( $id );
654
+
655
+                    // Set the featured image.
656
+                    if ( false !== $importable_content && in_array( 'featured_image', $importable_content ) ) {
657
+                         $this->set_featured_image( $data, $id );
658
+                    }
659
+                    var_dump($importable_content);
660
+                    if ( false !== $importable_content && in_array( 'banner_image', $importable_content ) ) {
661
+                         var_dump($id);
662
+                         $this->set_banner_image( $data, $id, $importable_content );
663
+                    }
664
+                    // Import the main gallery.
665
+                    if ( false !== $importable_content && in_array( 'gallery', $importable_content ) ) {
666
+                         $this->create_main_gallery( $data, $id );
667
+                    }
668
+               }
669
+
670
+               // Set the continent.
671
+               if ( false !== $importable_content && in_array( 'continent', $importable_content ) ) {
672
+                    $this->set_continent( $data, $id );
673
+               }
674
+          }
675
+
676
+          return $id;
677
+     }
678
+
679
+     /**
680
+      * Saves the room data
681
+      */
682
+     public function set_travel_info( $data, $id, $meta_key, $importable = array( 'none' ) ) {
683
+          if ( ! empty( $data[0]['travel_information'] ) && isset( $data[0]['travel_information'][ $meta_key ] ) ) {
684
+               $content = $data[0]['travel_information'][ $meta_key ];
685
+
686
+               if ( in_array( 'strip_tags', $importable ) ) {
687
+                    $content = strip_tags( $content );
688
+               }
689
+
690
+               $this->save_custom_field( $content, $meta_key, $id );
691
+          }
692
+     }
693
+
694
+     /**
695
+      * Set the Travel Style
696
+      */
697
+     public function set_continent( $data, $id ) {
698
+
699
+          if ( isset( $data[0]['position']['country'] ) && $data[0]['map_object_id'] === $data[0]['position']['country_content_entity_id'] ) {
700
+               // Get the continent code.
701
+               $country_code    = to_country_data( $data[0]['position']['country'], false );
702
+               $continent_code  = to_continent_code( $country_code );
703
+               $continent_label = to_continent_label( $continent_code );
704
+
705
+               if ( ! empty( tour_operator()->options['display']['enable_search_region_filter'] ) ) {
706
+                    $continent_label = to_continent_region_label( $country_code );
707
+               }
708
+
709
+               if ( '' !== $continent_label ) {
710
+                    $term = term_exists( trim( $continent_label ), 'continent' );
711
+                    if ( ! $term ) {
712
+                         $term = wp_insert_term( trim( $continent_label ), 'continent' );
713
+
714
+                         if ( is_wp_error( $term ) ) {
715
+                              echo wp_kses_post( $term->get_error_message() );
716
+                         }
717
+                    } else {
718
+                         wp_set_object_terms( $id, sanitize_title( $continent_label ), 'continent', true );
719
+                    }
720
+               }
721
+          }
722
+     }
723
+
724
+     /**
725
+      * Save the list of Accommodation into an option
726
+      */
727
+     public function check_for_parent( $data = array() ) {
728
+          global $wpdb;
729
+
730
+          if ( $data[0]['position']['country_content_entity_id'] !== $data[0]['position']['destination_content_entity_id'] ) {
731
+               $result = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'lsx_wetu_id' AND meta_value = '%s'", array( $data[0]['position']['country_content_entity_id'] ) ) );
732
+               if ( ! empty( $result ) && '' !== $result && false !== $result ) {
733
+                    return $result;
734
+               }
735
+          }
736
+          return false;
737
+     }
738 738
 }
Please login to merge, or discard this patch.