Passed
Push — master ( 57e1a1...0913ec )
by Warwick
06:17 queued 03:20
created

WETU_Importer_Tours::set_accommodation()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 5
nop 2
dl 0
loc 25
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   WETU_Importer_Tours
4
 * @author    LightSpeed
5
 * @license   GPL-3+
6
 * @link
7
 * @copyright 2017 LightSpeed
8
 **/
9
10
class WETU_Importer_Tours extends 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() {
92
		$this->set_variables();
93
	}
94
95
	/**
96
	 * Sets the variables used throughout the plugin.
97
	 */
98
	public function set_variables() {
99
		parent::set_variables();
100
101
		//https://wetu.com/API/Itinerary/V8/Get
102
		if ( false !== $this->api_username && false !== $this->api_password ) {
103
			$this->url    = 'https://wetu.com/API/Itinerary/';
104
			$this->url_qs = 'username=' . $this->api_username . '&password=' . $this->api_password;
105
		} elseif ( false !== $this->api_key ) {
106
			$this->url    = 'https://wetu.com/API/Itinerary/' . $this->api_key;
107
			$this->url_qs = '';
108
		}
109
110
		$temp_options = get_option( '_lsx-to_settings',false );
111
112
		if ( false !== $temp_options && isset( $temp_options[ $this->plugin_slug ] ) && ! empty( $temp_options[ $this->plugin_slug ] ) ) {
113
			$this->options = $temp_options[ $this->plugin_slug ];
114
		}
115
116
		$tour_options = get_option( 'wetu_importer_tour_settings',false );
117
118
		if ( false !== $tour_options ) {
119
			$this->tour_options = $tour_options;
120
		}
121
	}
122
123
	/**
124
	 * Display the importer administration screen
125
	 */
126
	public function display_page() {
127
		?>
128
		<div class="wrap">
129
			<?php $this->navigation( 'tour' ); ?>
130
131
			<?php $this->update_options_form(); ?>
132
133
			<?php $this->search_form(); ?>
134
135
			<form method="get" action="" id="posts-filter">
136
				<input type="hidden" name="post_type" class="post_type" value="<?php echo esc_attr( $this->tab_slug ); ?>" />
137
138
				<p><input class="button button-primary add" type="button" value="<?php esc_html_e( 'Add to List','wetu-importer' ); ?>" />
139
					<input class="button button-primary clear" type="button" value="<?php esc_html_e( 'Clear','wetu-importer' ); ?>" />
140
				</p>
141
142
				<table class="wp-list-table widefat fixed posts">
143
					<?php $this->table_header(); ?>
144
145
					<tbody id="the-list">
146
						<tr class="post-0 type-tour status-none" id="post-0">
147
							<th class="check-column" scope="row">
148
								<label for="cb-select-0" class="screen-reader-text"><?php esc_html_e( 'Enter a title to search for and press enter','wetu-importer' ); ?></label>
149
							</th>
150
							<td class="date column-date column-ref" colspan="4">
151
								<strong>
152
									<?php esc_html_e( 'Search for tours using the search form above','wetu-importer' ); ?>
153
								</strong>							
154
							</td>
155
						</tr>
156
					</tbody>
157
158
					<?php $this->table_footer(); ?>
159
160
				</table>
161
162
				<p><input class="button button-primary add" type="button" value="<?php esc_html_e( 'Add to List','wetu-importer' ); ?>" />
163
					<input class="button button-primary clear" type="button" value="<?php esc_html_e( 'Clear','wetu-importer' ); ?>" />
164
				</p>
165
			</form>
166
167
			<div style="display:none;" class="import-list-wrapper">
168
				<br />
169
				<form method="get" action="" id="import-list">
170
171
					<div class="row">
172
						<div class="settings-all" style="width:30%;display:block;float:left;">
173
							<h3><?php esc_html_e( 'What content to Sync from WETU' ); ?></h3>
174
							<ul>
175
								<li><input class="content select-all" <?php $this->checked( $this->tour_options,'all' ); ?> type="checkbox"name="content[]"  value="all" /> <?php esc_html_e( 'Select All','wetu-importer' ); ?></li>
0 ignored issues
show
Documentation introduced by
$this->tour_options is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
176
177
								<?php if ( isset( $this->options ) && ! isset( $this->options['disable_tour_descriptions'] ) ) { ?>
178
									<li><input class="content" <?php $this->checked( $this->tour_options,'description' ); ?> type="checkbox" name="content[]" value="description" /> <?php esc_html_e( 'Description','wetu-importer' ); ?></li>
0 ignored issues
show
Documentation introduced by
$this->tour_options is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
179
								<?php } ?>
180
181
								<li><input class="content" <?php $this->checked( $this->tour_options,'price' ); ?> type="checkbox" name="content[]" value="price" /> <?php esc_html_e( 'Price','wetu-importer' ); ?></li>
0 ignored issues
show
Documentation introduced by
$this->tour_options is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
182
								<li><input class="content" <?php $this->checked( $this->tour_options,'duration' ); ?> type="checkbox" name="content[]" value="duration" /> <?php esc_html_e( 'Duration','wetu-importer' ); ?></li>
0 ignored issues
show
Documentation introduced by
$this->tour_options is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
183
								<li><input class="content" <?php $this->checked( $this->tour_options,'group_size' ); ?> type="checkbox" name="content[]" value="group_size" /> <?php esc_html_e( 'Group Size','wetu-importer' ); ?></li>
0 ignored issues
show
Documentation introduced by
$this->tour_options is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
184
								<li><input class="content" <?php $this->checked( $this->tour_options,'category' ); ?> type="checkbox" name="content[]" value="category" /> <?php esc_html_e( 'Category','wetu-importer' ); ?></li>
0 ignored issues
show
Documentation introduced by
$this->tour_options is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
185
								<li><input class="content" <?php $this->checked( $this->tour_options,'tags' ); ?> type="checkbox" name="content[]" value="tags" /> <?php esc_html_e( 'Tags','wetu-importer' ); ?></li>
0 ignored issues
show
Documentation introduced by
$this->tour_options is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
186
187
								<li><input class="content" <?php $this->checked( $this->tour_options,'itineraries' ); ?> type="checkbox" name="content[]" value="itineraries" /> <?php esc_html_e( 'Itinerary Days','wetu-importer' ); ?></li>
0 ignored issues
show
Documentation introduced by
$this->tour_options is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
188
189
								<?php
190
								/*
191
								if ( class_exists( 'LSX_TO_Maps' ) ) { ?>
192
								<li><input class="content" <?php $this->checked($this->tour_options,'map'); ?> type="checkbox" name="content[]" value="map" /> <?php esc_html_e('Map Coordinates (generates a KML file)','wetu-importer'); ?></li>
193
								<?php } 
194
								*/
195
								?>
196
							</ul>
197
						</div>
198
						<div class="settings-all" style="width:30%;display:block;float:left;">
199
							<h3><?php esc_html_e( 'Itinerary Info' ); ?></h3>
200
							<ul>
201
								<li><input class="content" <?php $this->checked( $this->tour_options,'itinerary_description' ); ?> type="checkbox" name="content[]" value="itinerary_description" /> <?php esc_html_e( 'Description','wetu-importer' ); ?></li>
0 ignored issues
show
Documentation introduced by
$this->tour_options is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
202
								<li><input class="content" <?php $this->checked( $this->tour_options,'itinerary_included' ); ?> type="checkbox" name="content[]" value="itinerary_included" /> <?php esc_html_e( 'Included','wetu-importer' ); ?></li>
0 ignored issues
show
Documentation introduced by
$this->tour_options is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
203
								<li><input class="content" <?php $this->checked( $this->tour_options,'itinerary_excluded' ); ?> type="checkbox" name="content[]" value="itinerary_excluded" /> <?php esc_html_e( 'Excluded','wetu-importer' ); ?></li>
0 ignored issues
show
Documentation introduced by
$this->tour_options is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
204
							</ul>
205
206
							<h4><?php esc_html_e( 'Additional Content' ); ?></h4>
207
							<ul>
208
								<li><input class="content" <?php $this->checked( $this->tour_options,'accommodation' ); ?> type="checkbox" name="content[]" value="accommodation" /> <?php esc_html_e( 'Sync Accommodation','wetu-importer' ); ?></li>
0 ignored issues
show
Documentation introduced by
$this->tour_options is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
209
								<li><input class="content" <?php $this->checked( $this->tour_options,'destination' ); ?> type="checkbox" name="content[]" value="destination" /> <?php esc_html_e( 'Sync Destinations','wetu-importer' ); ?></li>
0 ignored issues
show
Documentation introduced by
$this->tour_options is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
210
								<li><input class="content" <?php $this->checked( $this->tour_options,'featured_image' ); ?> type="checkbox" name="content[]" value="featured_image" /> <?php esc_html_e( 'Featured Image','wetu-importer' ); ?></li>
0 ignored issues
show
Documentation introduced by
$this->tour_options is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
211
								<li><input class="content" <?php $this->checked( $this->tour_options,'banner_image' ); ?> type="checkbox" name="content[]" value="banner_image" /> <?php esc_html_e( 'Banner Image','wetu-importer' ); ?></li>
0 ignored issues
show
Documentation introduced by
$this->tour_options is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
212
							</ul>
213
						</div>
214
						<?php if ( class_exists( 'LSX_TO_Team' ) ) { ?>
215
							<div style="width:30%;display:block;float:left;">
216
								<h3><?php esc_html_e( 'Assign a Team Member' ); ?></h3>
217
								<?php $this->team_member_checkboxes( $this->tour_options ); ?>
0 ignored issues
show
Documentation introduced by
$this->tour_options is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
218
							</div>
219
						<?php } ?>
220
221
						<br clear="both" />
222
					</div>
223
224
					<h3><?php esc_html_e( 'Your List' ); ?></h3>
225
					<p><input class="button button-primary" type="submit" value="<?php esc_html_e( 'Sync','wetu-importer' ); ?>" /></p>
226
					<table class="wp-list-table widefat fixed posts">
227
						<?php $this->table_header(); ?>
228
229
						<tbody>
230
231
						</tbody>
232
233
						<?php $this->table_footer(); ?>
234
235
					</table>
236
237
					<p><input class="button button-primary" type="submit" value="<?php esc_html_e( 'Sync','wetu-importer' ); ?>" /></p>
238
				</form>
239
			</div>
240
241
			<div style="display:none;" class="completed-list-wrapper">
242
				<h3><?php esc_html_e( 'Completed', 'wetu-importer' ); ?> - <small><?php esc_html_e( 'Import your', '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','wetu-importer' ); ?></small></h3>
243
				<ul>
244
				</ul>
245
			</div>
246
		</div>
247
		<?php
248
	}
249
250
	/**
251
	 * search_form
252
	 */
253
	public function update_options_form() {
254
		$tours = get_transient( 'lsx_ti_tours' );
255
256
		echo '<div class="wetu-status tour-wetu-status"><h3>' . esc_html__( 'Wetu Status','wetu-importer' ) . ' - ';
257
258
		if ( '' === $tours || false === $tours || isset( $_GET['refresh_tours'] ) ) {
259
			$result = $this->update_options();
260
261
			if ( true === $result ) {
262
				echo '<span style="color:green;">' . esc_attr( 'Connected','wetu-importer' ) . '</span>';
263
				echo ' - <small><a href="#">' . esc_attr( 'Refresh','wetu-importer' ) . '</a></small>';
264
			} else {
265
				echo '<span style="color:red;">' . wp_kses_post( $result ) . '</span>';
266
			}
267
		} else {
268
			echo '<span style="color:green;">' . esc_attr( 'Connected','wetu-importer' ) . '</span> - <small><a href="#">' . esc_attr( 'Refresh','wetu-importer' ) . '</a></small>';
269
		}
270
271
		echo '</h3>';
272
273
		$form_options = get_option( 'lsx_ti_tours_api_options' );
274
		if ( false === $form_options ) {
275
			$form_options = array( 0 );
276
		}
277
		?>
278
		<form method="get" class="tour-refresh-form" action="<?php echo esc_attr( admin_url( 'admin.php' ) ); ?>">
279
			<input type="hidden" name="page" value="<?php echo esc_attr( $this->plugin_slug ); ?>" />
280
			<input type="hidden" name="tab" value="tour" />
281
			<input type="hidden" name="refresh_tours" value="true" />
282
283
			<p class="tour-search-options">
284
				<label for="own"><input class="content" <?php if ( in_array( 'own',$form_options ) ) { echo 'checked'; } ?> type="checkbox" name="own" value="true" /> <?php esc_html_e( 'Own Tours','wetu-importer' ); ?> </label>
285
			</p>
286
287
			<p class="tour-search-options">
288
				<label for="type"><input class="content" <?php if ( in_array( 'allitineraries',$form_options ) ) { echo 'checked'; } ?> type="radio" name="type[]" value="allitineraries" /> <?php esc_html_e( 'All','wetu-importer' ); ?></label>
289
				<label for="type"><input class="content" <?php if ( in_array( 'sample',$form_options ) ) { echo 'checked'; } ?> type="radio" name="type[]" value="sample" /> <?php esc_html_e( 'Sample','wetu-importer' ); ?></label>
290
				<label for="type"><input class="content" <?php if ( in_array( 'personal',$form_options ) ) { echo 'checked'; } ?> type="radio" name="type[]" value="personal" /> <?php esc_html_e( 'Personal','wetu-importer' ); ?></label>
291
			</p>
292
293
			<p><input class="button button-primary submit" type="submit" value="<?php echo esc_attr( 'Refresh Tours','wetu-importer' ); ?>"></p>
294
		</form>
295
		<br />
296
		<?php
297
		echo '</div>';
298
	}
299
300
	/**
301
	 * Save the list of Tours into an option
302
	 */
303
	public function update_options() {
304
		$own = '';
0 ignored issues
show
Unused Code introduced by
$own is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
305
		$options = array();
306
307
		delete_option( 'lsx_ti_tours_api_options' );
308
309
		if ( isset( $_GET['own'] ) ) {
310
			$this->url_qs .= '&own=true';
311
			$options[] = 'own';
312
		}
313
314
		if ( isset( $_GET['type'] ) ) {
315
			$this->url_qs .= '&type=' . implode( '',$_GET['type'] );
316
			$options[] = implode( '',$_GET['type'] );
317
		}
318
319
		$this->url_qs .= '&results=2000';
320
321
		add_option( 'lsx_ti_tours_api_options',$options );
322
323
		$data = wp_remote_get( $this->url . '/V8/List?' . $this->url_qs );
324
		$tours = json_decode( wp_remote_retrieve_body( $data ), true );
325
326
		if ( isset( $tours['error'] ) ) {
327
			return $tours['error'];
328
		} elseif ( isset( $tours['itineraries'] ) && ! empty( $tours['itineraries'] ) ) {
329
			set_transient( 'lsx_ti_tours',$tours['itineraries'],60 * 60 * 2 );
330
			return true;
331
		}
332
	}
333
334
	/**
335
	 * Grab all the current tour posts via the lsx_wetu_id field.
336
	 */
337 View Code Duplication
	public function find_current_tours() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
338
		global $wpdb;
339
		$return = array();
340
341
		$current_tours = $wpdb->get_results("
342
			SELECT key1.post_id,key1.meta_value
343
			FROM {$wpdb->postmeta} key1
344
345
			INNER JOIN  {$wpdb->posts} key2
346
			ON key1.post_id = key2.ID
347
348
			WHERE key1.meta_key = 'lsx_wetu_id'
349
			AND key2.post_type = 'tour'
350
351
			LIMIT 0,500
352
		");
353
354
		if ( null !== $current_tours && ! empty( $current_tours ) ) {
355
			foreach ( $current_tours as $tour ) {
356
				$return[ $tour->meta_value ] = $tour;
357
			}
358
		}
359
360
		return $return;
361
	}
362
363
	/**
364
	 * Run through the accommodation grabbed from the DB.
365
	 */
366
	public function process_ajax_search() {
367
		$return = false;
368
369
		// @codingStandardsIgnoreLine
370
		if ( isset( $_POST['action'] ) && $_POST['action'] === 'lsx_tour_importer' && isset( $_POST['type'] ) && $_POST['type'] === $this->tab_slug ) {
371
			$tours = get_transient( 'lsx_ti_tours' );
372
373
			if ( false !== $tours ) {
374
375
				$searched_items = false;
376
377
				// @codingStandardsIgnoreLine
378 View Code Duplication
				if ( isset( $_POST['keyword'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
379
					// @codingStandardsIgnoreLine
380
					$keyphrases = $_POST['keyword'];
381
				} else {
382
					$keyphrases = array( 0 );
383
				}
384
385
				if ( ! is_array( $keyphrases ) ) {
386
					$keyphrases = array( $keyphrases );
387
				}
388
				foreach ( $keyphrases as &$keyword ) {
389
					$keyword = ltrim( rtrim( $keyword ) );
390
				}
391
392
				$post_status = false;
393
				if ( in_array( 'publish',$keyphrases ) ) {
394
					$post_status = 'publish';
395
				}
396
				if ( in_array( 'pending',$keyphrases ) ) {
397
					$post_status = 'pending';
398
				}
399
				if ( in_array( 'draft',$keyphrases ) ) {
400
					$post_status = 'draft';
401
				}
402
				if ( in_array( 'import',$keyphrases ) ) {
403
					$post_status = 'import';
404
				}
405
406
				if ( ! empty( $tours ) ) {
407
					$current_tours = $this->find_current_tours();
408
409
					foreach ( $tours as $row_key => $row ) {
410
						if ( isset( $row['is_disabled'] ) && true === $row['is_disabled'] ) {
411
							continue;
412
						}
413
414
						/*if('Sample' === $row['type']){
415
							continue;
416
						}*/
417
418
						//If this is a current tour, add its ID to the row.
419
						$row['post_id'] = 0;
420
421
						if ( false !== $current_tours && array_key_exists( $row['identifier'], $current_tours ) ) {
422
							$row['post_id'] = $current_tours[ $row['identifier'] ]->post_id;
423
						}
424
425
						//If we are searching for
426
						if ( false !== $post_status ) {
427
							if ( 'import' === $post_status ) {
428
429
								if ( 0 !== $row['post_id'] ) {
430
									continue;
431
								} else {
432
									$searched_items[ sanitize_title( $row['name'] ) . '-' . $row['identifier'] ] = $this->format_row( $row );
0 ignored issues
show
Documentation introduced by
$row is of type array<string,?,{"post_id":"?"}>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
433
								}
434
							} else {
435
								if ( 0 === $row['post_id'] ) {
436
									continue;
437
								} else {
438
									$current_status = get_post_status( $row['post_id'] );
439
440
									if ( $current_status !== $post_status ) {
441
										continue;
442
									}
443
								}
444
445
								$searched_items[ sanitize_title( $row['name'] ) . '-' . $row['identifier'] ] = $this->format_row( $row );
0 ignored issues
show
Documentation introduced by
$row is of type array<string,?,{"post_id":"?"}>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
446
							}
447
						} else {
448
							//Search through each keyword.
449
							foreach ( $keyphrases as $keyphrase ) {
450
451
								//Make sure the keyphrase is turned into an array
452
								$keywords = explode( ' ',$keyphrase );
453
								if ( ! is_array( $keywords ) ) {
454
									$keywords = array( $keywords );
455
								}
456
457
								if ( $this->multineedle_stripos( ltrim( rtrim( $row['name'] ) ), $keywords ) !== false ) {
458
									$searched_items[ sanitize_title( $row['name'] ) . '-' . $row['identifier'] ] = $this->format_row( $row );
0 ignored issues
show
Documentation introduced by
$row is of type array<string,?,{"post_id":"?"}>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
459
								} else if ( $this->multineedle_stripos( ltrim( rtrim( $row['reference_number'] ) ), $keywords ) !== false ) {
460
									$searched_items[ sanitize_title( $row['name'] ) . '-' . $row['identifier'] ] = $this->format_row( $row );
0 ignored issues
show
Documentation introduced by
$row is of type array<string,?,{"post_id":"?"}>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
461
								}else if ( $this->multineedle_stripos( ltrim( rtrim( $row['identifier_key'] ) ), $keywords ) !== false ) {
462
									$searched_items[ sanitize_title( $row['name'] ) . '-' . $row['identifier'] ] = $this->format_row( $row );
0 ignored issues
show
Documentation introduced by
$row is of type array<string,?,{"post_id":"?"}>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
463
								}
464
465
								//Add in the year and ref
466
							}
467
						}
468
					}
469
				}
470
471
				if ( false !== $searched_items ) {
472
					ksort( $searched_items );
473
					$return = implode( $searched_items );
474
				}
475
			}
476
			print_r( $return );
477
			die();
478
		}
479
	}
480
481
	/**
482
	 * Formats the row for output on the screen.
483
	 */
484
	public function format_row( $row = false ) {
485
		if ( false !== $row ) {
486
			$status = 'import';
487
488
			if ( 0 !== $row['post_id'] ) {
489
				$status = '<a href="' . admin_url( '/post.php?post=' . $row['post_id'] . '&action=edit' ) . '" target="_blank">' . get_post_status( $row['post_id'] ) . '</a>';
490
			}
491
492
			$row_html = '
493
			<tr class="post-' . $row['post_id'] . ' type-tour" id="post-' . $row['post_id'] . '">
494
				<th class="check-column" scope="row">
495
					<label for="cb-select-' . $row['identifier'] . '" class="screen-reader-text">' . $row['name'] . '</label>
496
					<input type="checkbox" data-identifier="' . $row['identifier'] . '" value="' . $row['post_id'] . '" name="post[]" id="cb-select-' . $row['identifier'] . '">
497
				</th>			
498
				<td class="post-title page-title column-title">
499
					<strong>' . $row['name'] . '</strong> - ' . $status . '
500
				</td>
501
				<td class="date column-date">
502
					' . $row['reference_number'] . '
503
				</td>				
504
				<td class="date column-date">
505
					<abbr title="' . date( 'Y/m/d',strtotime( $row['last_modified'] ) ) . '">' . date( 'Y/m/d',strtotime( $row['last_modified'] ) ) . '</abbr><br>Last Modified
506
				</td>
507
				<td class="ssid column-ssid">
508
					' . $row['identifier'] . '
509
				</td>
510
			</tr>';
511
			return $row_html;
512
		}
513
	}
514
515
	/**
516
	 * Connect to wetu
517
	 */
518
	public function process_ajax_import( $force = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $force is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
519
		$return = false;
0 ignored issues
show
Unused Code introduced by
$return is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
520
521
		// @codingStandardsIgnoreLine
522
		if ( isset( $_POST['action'] ) && $_POST['action'] === 'lsx_import_items' && isset( $_POST['type'] ) && $_POST['type'] === $this->tab_slug && isset( $_POST['wetu_id'] ) ) {
523
524
			// @codingStandardsIgnoreLine
525
			$wetu_id = $_POST['wetu_id'];
526
527
			// @codingStandardsIgnoreLine
528
			if ( isset( $_POST['post_id'] ) ) {
529
				// @codingStandardsIgnoreLine
530
				$post_id = $_POST['post_id'];
531
			} else {
532
				$post_id = 0;
533
			}
534
535
			delete_option( 'wetu_importer_tour_settings' );
536
537
			// @codingStandardsIgnoreLine
538 View Code Duplication
			if ( isset( $_POST['content'] ) && is_array( $_POST['content'] ) && ! empty( $_POST['content'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
539
				// @codingStandardsIgnoreLine
540
				$content = $_POST['content'];
541
				add_option( 'wetu_importer_tour_settings',$content );
542
			} else {
543
				$content = false;
544
			}
545
			$jdata = wp_remote_get( 'https://wetu.com/API/Itinerary/V8/Get?id=' . $wetu_id );		
546
547 View Code Duplication
			if ( ! empty( $jdata ) && isset( $jdata['response'] ) && isset( $jdata['response']['code'] ) && 200 === $jdata['response']['code'] ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
548
				$jdata = json_decode( $jdata['body'], true );
549
				$return = $this->import_row( $jdata, $wetu_id, $post_id, $content );
550
				$this->format_completed_row( $return );
551
				$this->save_queue();
552
				$this->cleanup_posts();
553
				$this->attach_destination_images( $content );
554
				$this->clean_attached_destinations( $return );
555
			} else {
556
				$this->format_error( esc_html__( 'There was a problem importing your tour, please contact support.', 'wetu-importer' ) );
557
			}
558
		}
559
	}
560
561
	/**
562
	 * Amends the tours destinations instead of replace.
563
	 *
564
	 * @param $id string
565
	 * @return void
566
	 */
567
	public function clean_attached_destinations( $id ) {
568
		$current_connections = get_post_meta( $id, 'destination_to_tour', false );
569
		delete_post_meta( $id,'destination_to_tour' );
570
		$current_connections = array_unique( $current_connections );
571
572
		foreach ( $current_connections as $connection ) {
573
			add_post_meta( $id, 'destination_to_tour',$connection, false );
574
		}
575
	}
576
577
	/**
578
	 * Connect to wetu
579
	 *
580
	 * @param $data array
581
	 * @param $wetu_id string
582
	 */
583
	public function import_row( $data, $wetu_id, $id = 0, $importable_content = array(), $old1 = false, $old2 = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $old1 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $old2 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
584
		$post_name = '';
585
		$data_post_content = '';
0 ignored issues
show
Unused Code introduced by
$data_post_content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
586
		$data_post_excerpt = '';
0 ignored issues
show
Unused Code introduced by
$data_post_excerpt is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
587
588
		$current_post = get_post( $id );
589
590
		$post = array(
591
			'post_type' => 'tour',
592
		);
593
594
		$content_used_general_description = false;
0 ignored issues
show
Unused Code introduced by
$content_used_general_description is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
595
596
		if ( ! empty( $importable_content ) && in_array( 'description', $importable_content ) ) {
597
			$data_post_content = $current_post->post_content;
598
			if ( isset( $data['summary'] ) && ! empty( $data['summary'] ) ) {
599
				$data_post_content = $data['summary'];
600
			}
601
			$post['post_content'] = $data_post_content;
602
		}
603
604
		// Create or update the post.
605
		if ( false !== $id && '0' !== $id ) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of '0' (string) and $id (integer) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
606
			$post['ID'] = $id;
607
			$post['post_status'] = 'publish';
608
			$id = wp_update_post( $post );
609
			$prev_date = get_post_meta( $id, 'lsx_wetu_modified_date', true );
610
			update_post_meta( $id, 'lsx_wetu_modified_date', strtotime( $data['last_modified'] ), $prev_date );
611
612
			// If the logger is enabled then log the data being saved.
613
			if ( $this->debug_enabled ) {
614
				$this->logger->log( 'wetu-importer', 'Creating Tour', print_r( $post, true ) );
615
			}
616
		} else {
617
			// Set the name.
618
			if ( isset( $data['name'] ) ) {
619
				$post_name = wp_unique_post_slug( sanitize_title( $data['name'] ), $id, 'draft', 'tour', 0 );
620
			}
621
622
			if ( ! isset( $post['post_content'] ) ) {
623
				$post['post_content'] = ' ';
624
			}
625
626
			$post['post_name']   = $post_name;
627
			$post['post_title']  = $data['name'];
628
			$post['post_status'] = 'publish';
629
630
			// If the logger is enabled then log the data being saved.
631
			if ( $this->debug_enabled ) {
632
				$this->logger->log( 'wetu-importer', 'Creating Tour', print_r( $post, true ) );
633
			}
634
			$id = wp_insert_post( $post );
635
636
			// Save the WETU ID and the Last date it was modified.
637
			if ( false !== $id ) {
638
				add_post_meta( $id, 'lsx_wetu_id', $wetu_id );
639
				add_post_meta( $id, 'lsx_wetu_modified_date', strtotime( $data['last_modified'] ) );
640
			}
641
		}
642
643
		// Set the price.
644
		if ( false !== $importable_content && in_array( 'price', $importable_content ) ) {
645
			$this->set_price( $data, $id );
646
		}
647
648
		// Set the Duration.
649
		if ( false !== $importable_content && in_array( 'duration', $importable_content ) ) {
650
			$this->set_duration( $data, $id );
651
		}
652
653
		// Set the Group Size.
654
		if ( false !== $importable_content && in_array( 'group_size', $importable_content ) ) {
655
			$this->set_group_size( $data, $id );
656
		}
657
658
		// Set the Group Size.
659
		if ( false !== $importable_content && in_array( 'tags', $importable_content ) ) {
660
			$this->set_travel_styles( $id, $data );
661
		}
662
663
		if ( false !== $importable_content && in_array( 'itineraries', $importable_content ) && isset( $data['legs'] ) && ! empty( $data['legs'] ) ) {
664
			$this->process_itineraries( $data, $id, $importable_content );
665
		}
666
667
		if ( in_array( 'map', $importable_content ) && isset( $data['routes'] ) && ! empty( $data['routes'] ) ) {
668
			$this->set_map_data( $data, $id );
669
		}
670
671
		return $id;
672
	}
673
674
	/**
675
	 * A loop which runs through each leg on the tour.
676
	 */
677
	public function process_itineraries( $data, $id, $importable_content ) {
678
		$day_counter = 1;
679
		$leg_counter = 0;
680
681
		delete_post_meta( $id, 'itinerary' );
682
683
		if ( false !== $importable_content && in_array( 'accommodation', $importable_content ) ) {
684
			delete_post_meta( $id, 'accommodation_to_tour' );
685
		}
686
		if ( false !== $importable_content && in_array( 'destination', $importable_content ) ) {
687
			delete_post_meta( $id, 'departs_from' );
688
			delete_post_meta( $id, 'ends_in' );
689
		}
690
691
		$departs_from = false;
692
		$ends_in = false;
693
694
		foreach ( $data['legs'] as $leg ) {
695
			// Itinerary Accommodation.
696
			$current_accommodation = false;
697
			if ( false !== $importable_content && in_array( 'accommodation', $importable_content ) ) {
698
				$current_accommodation = $this->set_accommodation( $leg, $id );
699
			}
700
701
			// Itinerary Destination.
702
			$current_destination = false;
703
			if ( false !== $importable_content && in_array( 'destination', $importable_content ) ) {
704
				$current_destination = $this->set_destination( $leg, $id, $leg_counter );
705
			}
706
707
			// If the Nights are the same mount of days in the array,  then it isnt "By Destination".
708
			if ( ( (1 <= (int) $leg['nights'] && isset( $leg['periods'] ) ) ) || 0 === $leg['itinerary_leg_id'] ) {
709
				foreach ( $leg['periods'] as $day ) {
710
					$current_day = array();
711
					$current_day['title'] = esc_attr( 'Day ', 'wetu-importer' ) . $day_counter;
712
713
					//print_r('<pre>');print_r($day['notes']);print_r('</pre>');
714
715
					// Description.
716
					if ( false !== $importable_content && in_array( 'itinerary_description', $importable_content ) && isset( $day['notes'] ) ) {
717
						$current_day['description'] = $day['notes'];
718
					} else {
719
						$current_day['description'] = '';
720
					}
721
722
					// Itinerary Gallery.
723 View Code Duplication
					if ( false !== $importable_content && in_array( 'itinerary_gallery', $importable_content ) && isset( $day['images'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
724
						$current_day['featured_image'] = '';
725
					} else {
726
						$current_day['featured_image'] = '';
727
					}
728
729
					// Accommodation.
730 View Code Duplication
					if ( false !== $current_accommodation ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
731
						$current_day['accommodation_to_tour'] = array( $current_accommodation );
732
					} else {
733
						$current_day['accommodation_to_tour'] = array();
734
					}
735
736
					// Destination.
737 View Code Duplication
					if ( false !== $current_destination ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
738
						$current_day['destination_to_tour'] = array( $current_destination );
739
					} else {
740
						$current_day['destination_to_tour'] = array();
741
					}
742
743
					// Included.
744 View Code Duplication
					if ( false !== $importable_content && in_array( 'itinerary_included', $importable_content ) && isset( $day['included'] ) && '' !== $day['included'] ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
745
						$current_day['included'] = $day['included'];
746
					} else {
747
						$current_day['included'] = '';
748
					}
749
750
					// Excluded.
751 View Code Duplication
					if ( false !== $importable_content && in_array( 'itinerary_excluded', $importable_content ) && isset( $day['excluded'] ) && '' !== $day['excluded'] ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
752
						$current_day['excluded'] = $day['excluded'];
753
					} else {
754
						$current_day['excluded'] = '';
755
					}
756
757
					$this->set_itinerary_day( $current_day,$id );
758
					$day_counter++;
759
				}
760
			} else {
761
				// This is for the by destination.
762
763
				$current_day = array();
764
				$next_day_count = $day_counter + (int) $leg['nights'];
765
				$day_count_label = $next_day_count - 1;
766
767
				$current_day['title'] = esc_attr( 'Day ', 'wetu-importer' ) . $day_counter;
768
769
				if ( 0 !== (int) $leg['nights'] ) {
770
					$current_day['title'] .= ' - ' . $day_count_label;
771
				}
772
773
				// Description.
774
				if ( false !== $importable_content && in_array( 'itinerary_description', $importable_content ) && isset( $leg['notes'] ) ) {
775
					$current_day['description'] = $leg['notes'];
776
				} else {
777
					$current_day['description'] = '';
778
				}
779
780
				// Itinerary Gallery.
781 View Code Duplication
				if ( false !== $importable_content && in_array( 'itinerary_gallery', $importable_content ) && isset( $leg['images'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
782
					$current_day['featured_image'] = '';
783
				} else {
784
					$current_day['featured_image'] = '';
785
				}
786
787
				// Accommodation.
788 View Code Duplication
				if ( false !== $current_accommodation ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
789
					$current_day['accommodation_to_tour'] = array( $current_accommodation );
790
				} else {
791
					$current_day['accommodation_to_tour'] = array();
792
				}
793
794
				// Destination.
795 View Code Duplication
				if ( false !== $current_destination ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
796
					$current_day['destination_to_tour'] = array( $current_destination );
797
				} else {
798
					$current_day['destination_to_tour'] = array();
799
				}
800
801
				//Included
802 View Code Duplication
				if ( false !== $importable_content && in_array( 'itinerary_included', $importable_content ) && isset( $leg['included'] ) && '' !== $leg['included'] ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
803
					$current_day['included'] = $leg['included'];
804
				} else {
805
					$current_day['included'] = '';
806
				}
807
808
				//Excluded
809 View Code Duplication
				if ( false !== $importable_content && in_array( 'itinerary_excluded', $importable_content ) && isset( $leg['excluded'] ) && '' !== $leg['excluded'] ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
810
					$current_day['excluded'] = $leg['excluded'];
811
				} else {
812
					$current_day['excluded'] = '';
813
				}
814
815
				$this->set_itinerary_day( $current_day,$id );
816
				$day_counter = $next_day_count;
817
			}
818
819
			//If we are in the first leg,  and the destination was attached then save it as the departure field.
820
			if ( 0 === $leg_counter && false !== $current_destination ) {
821
				$departs_from = $current_destination;
822
			}
823
824
			//If its the last leg then save it as the ends in.
825
			// @codingStandardsIgnoreLine
826
			if ( $leg_counter === (count( $data['legs'] ) -2) && false !== $current_destination ) {
827
				$ends_in = $current_destination;
828
			}
829
830
			$leg_counter++;
831
		}
832
833
		if ( false !== $departs_from ) {
834
			add_post_meta( $id, 'departs_from', $departs_from, true );
835
		}
836
		if ( false !== $ends_in ) {
837
			add_post_meta( $id, 'ends_in', $ends_in, true );
838
		}
839
	}
840
841
	/**
842
	 * Run through your routes and save the points as a KML file.
843
	 */
844
	public function set_map_data( $data, $id, $zoom = 9 ) {
845
		if ( ! empty( $data['routes'] ) ) {
846
			delete_post_meta( $id,'wetu_map_points' );
847
848
			$points = array();
849
850
			foreach ( $data['routes'] as $route ) {
851
852
				if ( isset( $route['points'] ) && '' !== $route['points'] ) {
853
854
					$temp_points = explode( ';',$route['points'] );
855
					$point_counter = count( $temp_points );
856
857
					for ( $x = 0; $x <= $point_counter; $x++ ) {
858
						$y = $x + 1;
859
						$points[] = $temp_points[ $x ] . ',' . $temp_points[ $y ];
860
						$x++;
861
					}
862
				}
863
			}
864
865
			if ( ! empty( $points ) ) {
866
				$this->save_custom_field( implode( ' ',$points ),'wetu_map_points',$id,false,true );
0 ignored issues
show
Documentation introduced by
implode(' ', $points) is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
867
			}
868
		}
869
870
	}
871
872
	// CLASS SPECIFIC FUNCTIONS.
873
874
	/**
875
	 * Set the Itinerary Day
876
	 */
877
	public function set_itinerary_day( $day, $id ) {
878
		$this->save_custom_field( $day,'itinerary',$id,false,false );
879
	}
880
881
	/**
882
	 * Set the price
883
	 */
884
	public function set_price( $data, $id ) {
885
		//Price
886 View Code Duplication
		if ( isset( $data['price'] ) && '' !== $data['price'] ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
887
			$price = preg_replace( '/[^0-9,.]/', '', $data['price'] );
888
			$this->save_custom_field( $price,'price',$id );
889
		}
890
891
		//Price includes
892
		if ( isset( $data['price_includes'] ) && '' !== $data['price_includes'] ) {
893
			$this->save_custom_field( $data['price_includes'],'included',$id );
894
		}
895
896
		//Price Excludes
897
		if ( isset( $data['price_excludes'] ) && '' !== $data['price_excludes'] ) {
898
			$this->save_custom_field( $data['price_excludes'],'not_included',$id );
899
		}
900
	}
901
902
	/**
903
	 * Set the duration
904
	 */
905
	public function set_duration( $data, $id ) {
906 View Code Duplication
		if ( isset( $data['days'] ) && ! empty( $data['days'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
907
			$price = $data['days'];
908
			$price = preg_replace( '/[^0-9,.]/', '', $price );
909
			$this->save_custom_field( $price,'duration',$id );
910
		}
911
	}
912
913
	/**
914
	 * Set the group size
915
	 */
916
	public function set_group_size( $data, $id ) {
917
		if ( isset( $data['group_size'] ) && ! empty( $data['group_size'] ) ) {
918
			$group_size = $data['group_size'];
919
			$this->save_custom_field( $group_size,'group_size',$id );
920
		}
921
	}
922
923
	/**
924
	 * Takes the WETU tags and sets the Travel Styles.
925
	 *
926
	 * @param string $id
927
	 * @param array $travel_styles
0 ignored issues
show
Bug introduced by
There is no parameter named $travel_styles. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
928
	 * @return void
929
	 */
930
	public function set_travel_styles( $id, $data ) {
931
		if ( isset( $data['tags'] ) ) {
932
			foreach ( $data['tags'] as $tag ) {
933
				$this->set_term( $id, $tag, 'travel-style' );
0 ignored issues
show
Documentation introduced by
$id is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'travel-style' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
934
			}
935
		}
936
	}
937
938
	/**
939
	 * Connects the Accommodation if its available
940
	 */
941
	public function set_accommodation( $day, $id ) {
942
		$ac_id = false;
943
		$this->current_accommodation = $this->find_current_accommodation();
0 ignored issues
show
Documentation Bug introduced by
The property $current_accommodation was declared of type string, but $this->find_current_accommodation() is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
944
945
		if ( isset( $day['content_entity_id'] ) && ! empty( $day['content_entity_id'] ) ) {
946
			if ( false !== $this->current_accommodation && ! empty( $this->current_accommodation ) && array_key_exists( $day['content_entity_id'],$this->current_accommodation ) ) {
947
				$ac_id = $this->current_accommodation[ $day['content_entity_id'] ];
948
			} else {
949
				$ac_id = wp_insert_post(array(
950
					'post_type' => 'accommodation',
951
					'post_status' => 'draft',
952
					'post_title' => $day['content_entity_id'],
953
				));
954
955
				$this->save_custom_field( $day['content_entity_id'],'lsx_wetu_id',$ac_id );
956
			}
957
958
			if ( '' !== $ac_id && false !== $ac_id ) {
959
				$this->save_custom_field( $ac_id,'accommodation_to_tour',$id,false,false );
960
				$this->save_custom_field( $id,'tour_to_accommodation',$ac_id,false,false );
961
				$this->queue_item( $ac_id );
962
			}
963
		}
964
		return $ac_id;
965
	}
966
967
	/**
968
	 * Grab all the current accommodation posts via the lsx_wetu_id field.
969
	 *
970
	 * @param $post_type string
971
	 * @return boolean / array
972
	 */
973
	public function find_current_accommodation( $post_type = 'accommodation' ) {
974
		global $wpdb;
975
		$accommodation = parent::find_current_accommodation( $post_type );
976
		$return = false;
977
978
		if ( ! empty( $accommodation ) ) {
979
			foreach ( $accommodation as $key => $acc ) {
980
				$return[ $acc->meta_value ] = $acc->post_id;
981
			}
982
		}
983
984
		return $return;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $return; (false) is incompatible with the return type of the parent method WETU_Importer::find_current_accommodation of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
985
	}
986
987
	/**
988
	 * Grab all the current accommodation posts via the lsx_wetu_id field.
989
	 * @return boolean / array
990
	 */
991
	public function find_current_destinations() {
992
		return $this->find_current_accommodation( 'destination' );
993
	}
994
995
	/**
996
	 * Connects the destinations post type
997
	 *
998
	 * @param $day array
999
	 * @param $id string
1000
	 * @return boolean / string
1001
	 */
1002
	public function set_destination( $day, $id, $leg_counter ) {
0 ignored issues
show
Unused Code introduced by
The parameter $leg_counter is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1003
		$dest_id    = false;
1004
		$country_id = false;
1005
		
1006
		$this->current_destinations = $this->find_current_destinations();
0 ignored issues
show
Documentation Bug introduced by
The property $current_destinations was declared of type string, but $this->find_current_destinations() is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
1007
1008
		if ( isset( $day['destination_content_entity_id'] ) && ! empty( $day['destination_content_entity_id'] ) ) {
1009
			if ( false !== $this->current_destinations && ! empty( $this->current_destinations ) && array_key_exists( $day['destination_content_entity_id'], $this->current_destinations ) ) {
1010
				$dest_id = $this->current_destinations[ $day['destination_content_entity_id'] ];
1011
1012
				//TODO Check for attachments here.
1013
				$this->destination_images[ $id ][] = array( $dest_id, $day['destination_content_entity_id'] );
1014
1015
				//Check if there is a country asigned.
1016
				$potential_id = wp_get_post_parent_id( $dest_id );
1017
				$country_wetu_id = get_post_meta( $potential_id, 'lsx_wetu_id', true );
1018
1019
				if ( false !== $country_wetu_id ) {
1020
					$country_id = $this->set_country( $country_wetu_id, $id );
1021
				}
1022
			} else {
1023
				$destination_json = wp_remote_get( 'https://wetu.com/API/Pins/' . $this->api_key . '/Get?ids=' . $day['destination_content_entity_id'] );
1024
1025
				if ( ! empty( $jdata ) && isset( $jdata['response'] ) && isset( $jdata['response']['code'] ) && 200 === $jdata['response']['code'] ) {
0 ignored issues
show
Bug introduced by
The variable $jdata seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
1026
1027
					$destination_data = json_decode( $destination_json['body'], true );
1028
1029
					if ( ! empty( $destination_data ) && ! isset( $destination_data['error'] ) ) {
1030
						$destination_title = $day['destination_content_entity_id'];
1031
1032
						if ( isset( $destination_data[0]['name'] ) ) {
1033
							$destination_title = $destination_data[0]['name'];
1034
						}
1035
1036
						if ( isset( $destination_data[0]['map_object_id'] ) && isset( $destination_data[0]['position']['country_content_entity_id'] )
1037
							&& $destination_data[0]['map_object_id'] !== $destination_data[0]['position']['country_content_entity_id'] ) {
1038
1039
							$country_id = $this->set_country( $destination_data[0]['position']['country_content_entity_id'], $id );
1040
							// Save the destination so we can grab the tour featured image and banner from them.
1041
						}
1042
1043
						$dest_post = array(
1044
							'post_type' => 'destination',
1045
							'post_status' => 'draft',
1046
							'post_title' => $destination_title,
1047
						);
1048
1049
						if ( false !== $country_id ) {
1050
							$dest_post['post_parent'] = $country_id;
1051
						}
1052
						$dest_id = wp_insert_post( $dest_post );
1053
1054
						// Make sure we register the.
1055
						$this->current_destinations[ $day['destination_content_entity_id'] ] = $dest_id;
1056
1057
						// If there are images attached then use the destination.
1058 View Code Duplication
						if ( isset( $destination_data[0]['content']['images'] ) && ! empty( $destination_data[0]['content']['images'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
1059
							$this->destination_images[ $id ][] = array( $dest_id, $day['destination_content_entity_id'] );
1060
						}
1061
1062
						$this->save_custom_field( $day['destination_content_entity_id'], 'lsx_wetu_id', $dest_id );
1063
					}
1064
				}
1065
			}
1066
1067
			if ( '' !== $dest_id && false !== $dest_id ) {
1068
				$this->save_custom_field( $dest_id, 'destination_to_tour', $id, false, false );
1069
				$this->save_custom_field( $id, 'tour_to_destination', $dest_id, false, false );
1070
1071
				//Save the item to display in the queue
1072
				$this->queue_item( $dest_id );
1073
1074
				//Save the item to clean up the amount of connections.
1075
				$this->cleanup_posts[ $dest_id ] = 'tour_to_destination';
1076
1077
				//Add this relation info so we can make sure certain items are set as countries.
1078
				if ( 0 !== $country_id && false !== $country_id ) {
1079
					$this->relation_meta[ $dest_id ] = $country_id;
1080
					$this->relation_meta[ $country_id ] = 0;
1081
				} else {
1082
					$this->relation_meta[ $dest_id ] = 0;
1083
				}
1084
			}
1085
		}
1086
		return $dest_id;
1087
	}
1088
1089
	/**
1090
	 * Connects the destinations post type
1091
	 *
1092
	 * @param $dest_id string
1093
	 * @param $country_id array
1094
	 * @param $id string
1095
	 *
1096
	 * @return string
1097
	 */
1098
	public function set_country( $country_wetu_id, $id ) {
1099
		$country_id = false;
1100
		$this->current_destinations = $this->find_current_destinations();
0 ignored issues
show
Documentation Bug introduced by
The property $current_destinations was declared of type string, but $this->find_current_destinations() is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
1101
1102
		if ( false !== $this->current_destinations && ! empty( $this->current_destinations ) && array_key_exists( $country_wetu_id, $this->current_destinations ) ) {
1103
			$country_id = $this->current_destinations[ $country_wetu_id ];
1104
			$this->destination_images[ $id ][] = array( $country_id, $country_wetu_id );
1105
		} else {
1106
			$country_json = file_get_contents( 'https://wetu.com/API/Pins/' . $this->api_key . '/Get?ids=' . $country_wetu_id );
1107
1108
			if ( $country_json ) {
1109
				$country_data = json_decode( $country_json, true );
1110
1111
				if ( ! empty( $country_data ) && ! isset( $country_data['error'] ) ) {
1112
1113
					//Format the title of the destination if its available,  otherwise default to the WETU ID.
1114
					$country_title = $country_wetu_id;
1115
1116
					if ( isset( $country_data[0]['name'] ) ) {
1117
						$country_title = $country_data[0]['name'];
1118
					}
1119
1120
					$country_id = wp_insert_post(array(
1121
						'post_type' => 'destination',
1122
						'post_status' => 'draft',
1123
						'post_title' => $country_title,
1124
					));
1125
1126
					//add the country to the current destination stack
1127
					$this->current_destinations[ $country_wetu_id ] = $country_id;
1128
1129
					// Check if there are images and save fore use later.
1130 View Code Duplication
					if ( isset( $country_data[0]['content']['images'] ) && ! empty( $country_data[0]['content']['images'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
1131
						$this->destination_images[ $id ][] = array( $country_id,$country_wetu_id );
1132
					}
1133
1134
					//Save the wetu field
1135
					$this->save_custom_field( $country_wetu_id, 'lsx_wetu_id', $country_id );
1136
				}
1137
			}
1138
		}
1139
1140
		if ( '' !== $country_id && false !== $country_id ) {
1141
			$this->save_custom_field( $country_id, 'destination_to_tour', $id, false, false );
1142
			$this->save_custom_field( $id, 'tour_to_destination', $country_id, false, false );
1143
			$this->queue_item( $country_id );
1144
			$this->cleanup_posts[ $country_id ] = 'tour_to_destination';
1145
1146
			return $country_id;
1147
		}
1148
	}
1149
1150
	/**
1151
	 * Connects the destinations post type
1152
	 *
1153
	 * @param $dest_id string
1154
	 * @param $country_id array
1155
	 * @param $id string
1156
	 *
1157
	 * @return string
1158
	 */
1159
	public function attach_destination_images( $importable_content = array() ) {
1160
		if ( false !== $this->destination_images ) {
1161
			$this->shuffle_assoc( $this->destination_images );
1162
1163
			foreach ( $this->destination_images as $tour => $destinations ) {
1164
				//$this->shuffle_assoc( $destinations );
1165
1166
				$image_set = false;
1167
				$forced = false;
1168
1169
				foreach ( $destinations as $destination ) {
1170
					if ( false === $image_set && false === $forced ) {
1171
						$url = 'https://wetu.com/API/Pins/' . $this->api_key;
1172
						$url_qs = '';
1173
1174
						$jdata = file_get_contents( $url . '/Get?' . $url_qs . '&ids=' . $destination[1] );
1175
1176
						if ( $jdata ) {
1177
							$adata = json_decode( $jdata, true );
1178
1179
							if ( ! empty( $adata ) && ! empty( $adata[0]['content']['images'] ) ) {
1180
								$this->find_attachments( $destination[0] );
1181
1182
								//Set the featured image
1183
								if ( false !== $importable_content && in_array( 'featured_image', $importable_content ) ) {
1184
									$image_set = $this->set_featured_image( $adata, $tour );
1185 View Code Duplication
									if ( false !== $importable_content && in_array( 'banner_image', $importable_content ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
1186
										$image_set = $this->set_banner_image( $adata, $tour );
1187
										$forced = true;
1188
									}
1189
									continue;
1190
								}
1191 View Code Duplication
								if ( false !== $importable_content && in_array( 'banner_image', $importable_content ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
1192
									$image_set = $this->set_banner_image( $adata, $tour );
1193
								}
1194
							}
1195
						}
1196
					} else {
1197
						continue;
1198
					}
1199
				}
1200
			}
1201
		}
1202
	}
1203
1204
	/**
1205
	 * Creates the main gallery data
1206
	 */
1207
	public function set_featured_image( $data, $id ) {
1208
		$image_set = false;
1209
		$counter = 0;
1210
1211
		if ( is_array( $data[0]['content']['images'] ) && ! empty( $data[0]['content']['images'] ) ) {
1212
			foreach ( $data[0]['content']['images'] as $v ) {
1213
				/*print_r('<pre>');
1214
				print_r( $v );
1215
				print_r('</pre>');*/
1216
1217
				if ( true === $image_set ) {
1218
					$counter++;
1219
					continue;
1220
				}
1221
1222
				if ( ! $this->check_if_image_is_used( $v ) ) {
1223
					$temp_featured_image = $this->attach_image( $v , $id );
1224
1225
					if ( false !== $temp_featured_image ) {
1226
						$this->featured_image = $temp_featured_image;
1227
						delete_post_meta( $id, '_thumbnail_id' );
1228
						add_post_meta( $id, '_thumbnail_id', $this->featured_image, true );
1229
						$image_set = true;
1230
					}
1231
				}
1232
1233
				$counter++;
1234
			}
1235
		}
1236
		return $image_set;
1237
	}
1238
1239
	/**
1240
	 * Sets a banner image
1241
	 */
1242
	public function set_banner_image( $data, $id, $content = array( 'none' ) ) {
1243
		$image_set = false;
1244
		$counter = 0;
1245
1246
		if ( is_array( $data[0]['content']['images'] ) && ! empty( $data[0]['content']['images'] ) ) {
1247
1248
			foreach ( $data[0]['content']['images'] as $v ) {
1249
				/*print_r('<pre>');
1250
				print_r( $v );
1251
				print_r('</pre>');*/
1252
1253
				if ( true === $image_set || 0 === $counter ) {
1254
					$counter++;
1255
					continue;
1256
				}
1257
1258
				if ( ! $this->check_if_image_is_used( $v ) ) {
1259
					$temp_banner = $this->attach_image( $v, $id, array(
0 ignored issues
show
Documentation introduced by
array('width' => '1920',...00', 'cropping' => 'c') is of type array<string,string,{"wi...","cropping":"string"}>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1260
						'width' => '1920',
1261
						'height' => '600',
1262
						'cropping' => 'c',
1263
					) );
1264
1265 View Code Duplication
					if ( false !== $temp_banner ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
1266
						$this->banner_image = $temp_banner;
1267
1268
						delete_post_meta( $id,'image_group' );
1269
1270
						$new_banner = array(
1271
							'banner_image' => array(
1272
								'cmb-field-0' => $this->banner_image,
1273
							),
1274
						);
1275
						add_post_meta( $id,'image_group',$new_banner,true );
1276
						$image_set = true;
1277
					}
1278
				}
1279
				$counter++;
1280
			}
1281
		}
1282
1283
		return $image_set;
1284
	}
1285
1286
	/**
1287
	 * Grabs all of the current used featured images on the site.
1288
	 */
1289
	public function check_if_image_is_used( $v ) {
1290
		global $wpdb;
1291
1292
		$temp_fragment = explode( '/',$v['url_fragment'] );
1293
		$url_filename = $temp_fragment[ count( $temp_fragment ) -1 ];
1294
		$url_filename = str_replace( array( '.jpg', '.png', '.jpeg' ),'',$url_filename );
1295
		$url_filename = trim( $url_filename );
1296
		$url_filename = str_replace( ' ','_',$url_filename );
1297
1298
		if ( in_array( $url_filename,$this->found_attachments ) ) {
1299
			//check to see if there is a featured image set with this ID.
1300
			$found_id = array_search( $url_filename,$this->found_attachments );
1301
1302
			$querystring = "
1303
				SELECT      post_id
1304
				FROM        {$wpdb->postmeta}
1305
				WHERE       meta_value = '{$found_id}'
1306
				AND 		meta_key = '_thumbnail_id'
1307
			";
1308
			// @codingStandardsIgnoreLine
1309
			$results = $wpdb->get_results( $querystring );
1310
1311
			if ( ! empty( $results ) ) {
1312
				return true;
1313
			} else {
1314
				return false;
1315
			}
1316
		} else {
1317
			$querystring = "
1318
				SELECT      ID
1319
				FROM        {$wpdb->posts}
1320
				WHERE       post_name = '{$url_filename}'
1321
			";
1322
			// @codingStandardsIgnoreLine
1323
			$results = $wpdb->get_results( $querystring );
1324
			if ( ! empty( $results ) ) {
1325
				$querystring = "
1326
					SELECT      post_id
1327
					FROM        {$wpdb->postmeta}
1328
					WHERE       meta_value = '{$results[0]->ID}'
1329
					AND 		meta_key = '_thumbnail_id'
1330
				";
1331
				// @codingStandardsIgnoreLine
1332
				$results = $wpdb->get_results( $querystring );
1333
				if ( ! empty( $results ) ) {
1334
					return true;
1335
				} else {
1336
					return false;
1337
				}
1338
			} else {
1339
				return false;
1340
			}
1341
		}
1342
	}
1343
1344
	/**
1345
	 * Que an item to be saved.
1346
	 *
1347
	 * @param   $id     int
1348
	 */
1349
	public function queue_item( $id ) {
1350
		if ( is_array( $this->import_queue ) && ! in_array( $id,$this->import_queue ) ) {
1351
			$this->import_queue[] = $id;
1352
		} else {
1353
			$this->import_queue[] = $id;
1354
		}
1355
	}
1356
1357
	/**
1358
	 * Saves the queue to the option.
1359
	 */
1360
	public function save_queue() {
1361
		if ( ! empty( $this->import_queue ) ) {
1362
			if ( ! empty( $this->queued_imports ) ) {
1363
				$saved_imports = array_merge( $this->queued_imports,$this->import_queue );
1364
			} else {
1365
				$saved_imports = $this->import_queue;
1366
			}
1367
1368
			delete_option( 'wetu_importer_que' );
1369
1370
			if ( ! empty( $saved_imports ) ) {
1371
				$saved_imports = array_unique( $saved_imports );
1372
				update_option( 'wetu_importer_que',$saved_imports );
1373
			}
1374
		}
1375
	}
1376
1377
	/**
1378
	 * The header of the item list
1379
	 */
1380 View Code Duplication
	public function table_header() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
1381
		?>
1382
		<thead>
1383
		<tr>
1384
			<th style="" class="manage-column column-cb check-column" id="cb" scope="col">
1385
				<label for="cb-select-all-1" class="screen-reader-text"><?php esc_attr_e( 'Select All', 'wetu-importer' ); ?></label>
1386
				<input type="checkbox" id="cb-select-all-1">
1387
			</th>
1388
			<th style="" class="manage-column column-title " id="title" style="width:50%;" scope="col"><?php esc_attr_e( 'Title', 'wetu-importer' ); ?></th>
1389
			<th style="" class="manage-column column-date" id="ref" style="width:10%;" scope="col"><?php esc_attr_e( 'Ref', 'wetu-importer' ); ?></th>
1390
			<th style="" class="manage-column column-date" id="date" scope="col"><?php esc_attr_e( 'Date', 'wetu-importer' ); ?></th>
1391
			<th style="" class="manage-column column-ssid" id="ssid" scope="col"><?php esc_attr_e( 'WETU ID', 'wetu-importer' ); ?></th>
1392
		</tr>
1393
		</thead>
1394
		<?php
1395
	}
1396
1397
	/**
1398
	 * The footer of the item list
1399
	 */
1400 View Code Duplication
	public function table_footer() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
1401
		?>
1402
		<tfoot>
1403
		<tr>
1404
			<th style="" class="manage-column column-cb check-column" id="cb" scope="col">
1405
				<label for="cb-select-all-1" class="screen-reader-text"><?php esc_attr_e( 'Select All', 'wetu-importer' ); ?></label>
1406
				<input type="checkbox" id="cb-select-all-1">
1407
			</th>
1408
			<th style="" class="manage-column column-title" scope="col"><?php esc_attr_e( 'Title', 'wetu-importer' ); ?></th>
1409
			<th style="" class="manage-column column-date" id="ref" style="width:10%;" scope="col"><?php esc_attr_e( 'Ref', 'wetu-importer' ); ?></th>			
1410
			<th style="" class="manage-column column-date" scope="col"><?php esc_attr_e( 'Date', 'wetu-importer' ); ?></th>
1411
			<th style="" class="manage-column column-ssid" scope="col"><?php esc_attr_e( 'WETU ID', 'wetu-importer' ); ?></th>
1412
		</tr>
1413
		</tfoot>
1414
		<?php
1415
	}	
1416
1417
}
1418