Passed
Push — master ( ebabc7...865d96 )
by Warwick
02:00
created

LSX_WETU_Importer_Tours::display_page()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 115

Duplication

Lines 6
Ratio 5.22 %

Importance

Changes 0
Metric Value
cc 8
nc 8
nop 0
dl 6
loc 115
rs 6.7555
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package   LSX_WETU_Importer_Tours
4
 * @author    LightSpeed
5
 * @license   GPL-3+
6
 * @link
7
 * @copyright 2017 LightSpeed
8
 **/
9
10
class LSX_WETU_Importer_Tours extends LSX_WETU_Importer {
11
12
	/**
13
	 * The url to list items from WETU
14
	 *
15
	 * @since 0.0.1
16
	 *
17
	 * @var      string
18
	 */
19
	public $tab_slug = 'tour';
20
21
	/**
22
	 * The url to list items from WETU
23
	 *
24
	 * @since 0.0.1
25
	 *
26
	 * @var      string
27
	 */
28
	public $url = false;
29
30
	/**
31
	 * The query string url to list items from WETU
32
	 *
33
	 * @since 0.0.1
34
	 *
35
	 * @var      string
36
	 */
37
	public $url_qs = false;
38
39
	/**
40
	 * Holds a list of any current accommodation
41
	 *
42
	 * @since 0.0.1
43
	 *
44
	 * @var      string
45
	 */
46
	public $current_accommodation = false;
47
48
	/**
49
	 * Holds a list of any current destinations
50
	 *
51
	 * @since 0.0.1
52
	 *
53
	 * @var      string
54
	 */
55
	public $current_destinations = false;
56
57
	/**
58
	 * Holds a list of the destination and the image it needs to grab.
59
	 *
60
	 * @since 0.0.1
61
	 *
62
	 * @var      string
63
	 */
64
	public $destination_images = false;
65
66
	/**
67
	 * Options
68
	 *
69
	 * @since 0.0.1
70
	 *
71
	 * @var      string
72
	 */
73
	public $options = false;
74
75
	/**
76
	 * The fields you wish to import
77
	 *
78
	 * @since 0.0.1
79
	 *
80
	 * @var      string
81
	 */
82
	public $tour_options = false;
83
84
	/**
85
	 * Initialize the plugin by setting localization, filters, and administration functions.
86
	 *
87
	 * @since 1.0.0
88
	 *
89
	 * @access private
90
	 */
91
	public function __construct() {
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
		if ( false !== $this->api_key ) {
101
			$this->url    = 'https://wetu.com/API/Itinerary/' . $this->api_key;
102
			$this->url_qs = '';
103
		}
104
		$tour_options = get_option( 'lsx_wetu_importer_tour_settings',false );
105
		if ( false !== $tour_options ) {
106
			$this->tour_options = $tour_options;
107
		}
108
	}
109
110
	/**
111
	 * Display the importer administration screen
112
	 */
113
	public function display_page() {
114
		?>
115
		<div class="wrap">
116
117
			<div class="tablenav top">
118
				<div class="alignleft actions">
119
					<?php $this->search_form(); ?>
120
				</div>
121
122
				<div class="alignleft actions">
123
					<?php $this->update_options_form(); ?>
124
				</div>
125
				<br clear="both" />
126
			</div>
127
128
			<form method="get" action="" id="posts-filter">
129
				<input type="hidden" name="post_type" class="post_type" value="<?php echo esc_attr( $this->tab_slug ); ?>" />
130
131
				<table class="wp-list-table widefat fixed posts">
132
					<?php $this->table_header(); ?>
133
134
					<tbody id="the-list">
135
						<tr class="post-0 type-tour status-none" id="post-0">
136
							<td class="date column-date column-ref" colspan="5">
137
								<strong>
138
									<?php esc_html_e( 'Search for tours using the search form above','lsx-wetu-importer' ); ?>
139
								</strong>
140
							</td>
141
						</tr>
142
					</tbody>
143
144
					<?php $this->table_footer(); ?>
145
146
				</table>
147
148
				<p><input class="button button-primary add" type="button" value="<?php esc_html_e( 'Add to List','lsx-wetu-importer' ); ?>" />
149
					<input class="button button-primary clear" type="button" value="<?php esc_html_e( 'Clear','lsx-wetu-importer' ); ?>" />
150
				</p>
151
			</form>
152
153
			<div style="display:none;" class="import-list-wrapper">
154
				<br />
155
				<form method="get" action="" id="import-list">
156
157
					<div class="row">
158
						<div class="settings-all" style="width:30%;display:block;float:left;">
159
							<h3><?php esc_html_e( 'What content to Sync from WETU' ); ?></h3>
160
							<ul>
161 View Code Duplication
								<?php if ( isset( $this->options ) && isset( $this->options['disable_tour_descriptions'] ) && 'on' !== $this->options['disable_tour_descriptions'] ) { ?>
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...
162
									<li><input class="content" checked="checked" type="checkbox" name="content[]" value="description" /> <?php esc_html_e( 'Description','lsx-wetu-importer' ); ?></li>
163
								<?php } ?>
164
165
								<li><input class="content" checked="checked" type="checkbox" name="content[]" value="price" /> <?php esc_html_e( 'Price','lsx-wetu-importer' ); ?></li>
166
								<li><input class="content" checked="checked" type="checkbox" name="content[]" value="duration" /> <?php esc_html_e( 'Duration','lsx-wetu-importer' ); ?></li>
167
								<li><input class="content" checked="checked" type="checkbox" name="content[]" value="group_size" /> <?php esc_html_e( 'Group Size','lsx-wetu-importer' ); ?></li>
168
								<li><input class="content" checked="checked" type="checkbox" name="content[]" value="category" /> <?php esc_html_e( 'Category','lsx-wetu-importer' ); ?></li>
169 View Code Duplication
								<?php if ( isset( $this->options ) && isset( $this->options['disable_tour_tags'] ) && 'on' !== $this->options['disable_tour_tags'] ) { ?>
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...
170
									<li><input class="content" checked="checked" type="checkbox" name="content[]" value="tags" /> <?php esc_html_e( 'Tags','lsx-wetu-importer' ); ?></li>
171
								<?php } ?>
172
								<li><input class="content" checked="checked" type="checkbox" name="content[]" value="itineraries" /> <?php esc_html_e( 'Itinerary Days','lsx-wetu-importer' ); ?></li>
173
							</ul>
174
						</div>
175
						<div class="settings-all" style="width:30%;display:block;float:left;">
176
							<h3><?php esc_html_e( 'Itinerary Info' ); ?></h3>
177
							<ul>
178
								<li><input class="content" checked="checked" type="checkbox" name="content[]" value="itinerary_description" /> <?php esc_html_e( 'Description','lsx-wetu-importer' ); ?></li>
179
								<li><input class="content" checked="checked" type="checkbox" name="content[]" value="itinerary_included" /> <?php esc_html_e( 'Included','lsx-wetu-importer' ); ?></li>
180
								<li><input class="content" checked="checked" type="checkbox" name="content[]" value="itinerary_excluded" /> <?php esc_html_e( 'Excluded','lsx-wetu-importer' ); ?></li>
181
								<li><input class="content" checked="checked" type="checkbox" name="content[]" value="room_basis" /> <?php esc_html_e( 'Room Basis','lsx-wetu-importer' ); ?></li>
182
								<li><input class="content" checked="checked" type="checkbox" name="content[]" value="drinks_basis" /> <?php esc_html_e( 'Drink Bases','lsx-wetu-importer' ); ?></li>
183
							</ul>
184
185
							<h4><?php esc_html_e( 'Additional Content' ); ?></h4>
186
							<ul>
187
								<li><input class="content" checked="checked" type="checkbox" name="content[]" value="accommodation" /> <?php esc_html_e( 'Sync Accommodation','lsx-wetu-importer' ); ?></li>
188
								<li><input class="content" checked="checked" type="checkbox" name="content[]" value="destination" /> <?php esc_html_e( 'Sync Destinations','lsx-wetu-importer' ); ?></li>
189
								<li><input class="content" checked="checked" type="checkbox" name="content[]" value="featured_image" /> <?php esc_html_e( 'Featured Image','lsx-wetu-importer' ); ?></li>
190
								<li><input class="content" checked="checked" type="checkbox" name="content[]" value="banner_image" /> <?php esc_html_e( 'Banner Image','lsx-wetu-importer' ); ?></li>
191
							</ul>
192
						</div>
193
						<?php if ( class_exists( 'LSX_TO_Team' ) ) { ?>
194
							<div style="width:30%;display:block;float:left;">
195
								<h3><?php esc_html_e( 'Assign a Team Member' ); ?></h3>
196
								<?php $this->team_member_checkboxes( $this->tour_options ); ?>
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...
197
							</div>
198
						<?php } ?>
199
200
						<br clear="both" />
201
					</div>
202
203
					<h3><?php esc_html_e( 'Your List' ); ?></h3>
204
					<p><input class="button button-primary" type="submit" value="<?php esc_html_e( 'Sync', 'lsx-wetu-importer' ); ?>" /></p>
205
					<table class="wp-list-table widefat fixed posts">
206
						<?php $this->table_header(); ?>
207
208
						<tbody>
209
210
						</tbody>
211
212
						<?php $this->table_footer(); ?>
213
214
					</table>
215
216
					<p><input class="button button-primary" type="submit" value="<?php esc_html_e( 'Sync', 'lsx-wetu-importer' ); ?>" /></p>
217
				</form>
218
			</div>
219
220
			<div style="display:none;" class="completed-list-wrapper">
221
				<h3><?php esc_html_e( 'Completed', 'lsx-wetu-importer' ); ?> - <small><?php esc_html_e( 'Import your', 'lsx-wetu-importer' ); ?> <a href="<?php echo esc_attr( admin_url( 'admin.php' ) ); ?>?page=<?php echo esc_attr( $this->plugin_slug ); ?>&tab=accommodation"><?php esc_html_e( 'accommodation' ); ?></a> <?php esc_html_e( 'next','lsx-wetu-importer' ); ?></small></h3>
222
				<ul>
223
				</ul>
224
			</div>
225
		</div>
226
		<?php
227
	}
228
229
	/**
230
	 * Displays the options for the form.
231
	 *
232
	 * @return void
233
	 */
234
	public function update_options_form() {
235
		$form_options = get_option( 'lsx_ti_tours_api_options' );
236
		if ( false === $form_options ) {
237
			$form_options = array( 'sample' );
238
		}
239
		?>
240
		<form method="get" class="tour-refresh-form">
241
			<input type="hidden" name="page" value="<?php echo esc_attr( $this->plugin_slug ); ?>" />
242
			<input type="hidden" name="tab" value="tour" />
243
			<input type="hidden" name="refresh_tours" value="true" />
244
			<input class="content" type="hidden" name="own" value="true" />
245
246
			<select name="type">
247
				<option <?php if ( in_array( 'allitineraries', $form_options ) ) { echo esc_attr( 'selected="selected"' ); } ?> value="allitineraries"><?php esc_html_e( 'All Itineraries','lsx-wetu-importer' ); ?></option>
248
				<option <?php if ( in_array( 'sample', $form_options ) ) { echo esc_attr( 'selected="selected"' ); } ?>value="sample"><?php esc_html_e( 'Sample','lsx-wetu-importer' ); ?></option>
249
				<option <?php if ( in_array( 'personal', $form_options ) ) { echo esc_attr( 'selected="selected"' ); } ?>value="personal"><?php esc_html_e( 'Personal','lsx-wetu-importer' ); ?></option>
250
			</select>
251
			<input class="button submit" type="submit" value="<?php esc_attr_e( 'Refresh', 'lsx-wetu-importer' ); ?>" />
252
		</form>
253
		<?php
254
	}
255
256
	/**
257
	 * Grab all the current tour posts via the lsx_wetu_id field.
258
	 */
259 View Code Duplication
	public function find_current_tours() {
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...
260
		global $wpdb;
261
		$return = array();
262
263
		$current_tours = $wpdb->get_results("
264
			SELECT key1.post_id,key1.meta_value,key2.post_title
265
			FROM {$wpdb->postmeta} key1
266
267
			INNER JOIN  {$wpdb->posts} key2
268
			ON key1.post_id = key2.ID
269
270
			WHERE key1.meta_key = 'lsx_wetu_id'
271
			AND key2.post_type = 'tour'
272
273
			LIMIT 0,500
274
		");
275
276
		if ( null !== $current_tours && ! empty( $current_tours ) ) {
277
			foreach ( $current_tours as $tour ) {
278
				$return[ $tour->meta_value ] = $tour;
279
			}
280
		}
281
282
		return $return;
283
	}
284
285
	/**
286
	 * Run through the accommodation grabbed from the DB.
287
	 */
288
	public function process_ajax_search() {
289
		$return = false;
290
		check_ajax_referer( 'lsx_wetu_ajax_action', 'security' );
291
292
		if ( isset( $_POST['action'] ) && 'lsx_tour_importer' === $_POST['action'] && isset( $_POST['type'] ) && $_POST['type'] === $this->tab_slug ) {
293
			$tours = get_transient( 'lsx_ti_tours' );
294
295
			if ( false !== $tours ) {
296
				$searched_items = false;
297 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...
298
					$keyphrases = array_map( 'sanitize_text_field', wp_unslash( $_POST['keyword'] ) );
299
				} else {
300
					$keyphrases = array( 0 );
301
				}
302
303
				if ( ! is_array( $keyphrases ) ) {
304
					$keyphrases = array( $keyphrases );
305
				}
306
				foreach ( $keyphrases as &$keyword ) {
307
					$keyword = ltrim( rtrim( $keyword ) );
308
				}
309
310
				$post_status = false;
311
				if ( in_array( 'publish',$keyphrases ) ) {
312
					$post_status = 'publish';
313
				}
314
				if ( in_array( 'pending',$keyphrases ) ) {
315
					$post_status = 'pending';
316
				}
317
				if ( in_array( 'draft',$keyphrases ) ) {
318
					$post_status = 'draft';
319
				}
320
				if ( in_array( 'import',$keyphrases ) ) {
321
					$post_status = 'import';
322
				}
323
324
				if ( ! empty( $tours ) ) {
325
					$current_tours = $this->find_current_tours();
326
327
					foreach ( $tours as $row_key => $row ) {
328
						if ( isset( $row['is_disabled'] ) && true === $row['is_disabled'] ) {
329
							continue;
330
						}
331
332
						// If this is a current tour, add its ID to the row.
333
						$row['post_id'] = 0;
334
						$row['post_title'] = $row['name'];
335
336
						if ( false !== $current_tours && array_key_exists( $row['identifier'], $current_tours ) ) {
337
							$row['post_id'] = $current_tours[ $row['identifier'] ]->post_id;
338
							$row['post_title'] = $current_tours[ $row['identifier'] ]->post_title;
339
						}
340
341
						// If we are searching for.
342
						if ( false !== $post_status ) {
343
							if ( 'import' === $post_status ) {
344
345
								if ( 0 !== $row['post_id'] ) {
346
									continue;
347
								} else {
348
									$searched_items[ sanitize_title( $row['name'] ) . '-' . $row['identifier'] ] = $this->format_row( $row, $row_key );
0 ignored issues
show
Documentation introduced by
$row is of type array<string,?,{"post_title":"?","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...
349
								}
350
							} else {
351
								if ( 0 === $row['post_id'] ) {
352
									continue;
353
								} else {
354
									$current_status = get_post_status( $row['post_id'] );
355
356
									if ( $current_status !== $post_status ) {
357
										continue;
358
									}
359
								}
360
361
								$searched_items[ sanitize_title( $row['name'] ) . '-' . $row['identifier'] ] = $this->format_row( $row, $row_key );
0 ignored issues
show
Documentation introduced by
$row is of type array<string,?,{"post_title":"?","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...
362
							}
363
						} else {
364
							// Search through each keyword.
365
							foreach ( $keyphrases as $keyphrase ) {
366
367
								// Make sure the keyphrase is turned into an array.
368
								$keywords = explode( ' ',$keyphrase );
369
								if ( ! is_array( $keywords ) ) {
370
									$keywords = array( $keywords );
371
								}
372
373
								if ( $this->multineedle_stripos( ltrim( rtrim( $row['name'] ) ), $keywords ) !== false ) {
374
									$searched_items[ sanitize_title( $row['name'] ) . '-' . $row['identifier'] ] = $this->format_row( $row, $row_key );
0 ignored issues
show
Documentation introduced by
$row is of type array<string,?,{"post_title":"?"}>, 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...
375
								} else if ( $this->multineedle_stripos( ltrim( rtrim( $row['reference_number'] ) ), $keywords ) !== false ) {
376
									$searched_items[ sanitize_title( $row['name'] ) . '-' . $row['identifier'] ] = $this->format_row( $row, $row_key );
0 ignored issues
show
Documentation introduced by
$row is of type array<string,?,{"post_title":"?"}>, 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...
377
								} else if ( $this->multineedle_stripos( ltrim( rtrim( $row['identifier_key'] ) ), $keywords ) !== false ) {
378
									$searched_items[ sanitize_title( $row['name'] ) . '-' . $row['identifier'] ] = $this->format_row( $row, $row_key );
0 ignored issues
show
Documentation introduced by
$row is of type array<string,?,{"post_title":"?"}>, 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...
379
								}
380
							}
381
						}
382
					}
383
				}
384
385
				if ( false !== $searched_items ) {
386
					$return = implode( $searched_items );
387
				}
388
			}
389
			print_r( $return );
390
			die();
391
		}
392
	}
393
394
	/**
395
	 * Formats the row for output on the screen.
396
	 */
397
	public function format_row( $row = false, $row_key = '' ) {
398
		if ( false !== $row ) {
399
			$status = 'import';
400
401
			if ( 0 !== $row['post_id'] ) {
402
				$status = '<a href="' . admin_url( '/post.php?post=' . $row['post_id'] . '&action=edit' ) . '" target="_blank">' . get_post_status( $row['post_id'] ) . '</a>';
403
			}
404
405
			$row_html = '
406
			<tr class="post-' . $row['post_id'] . ' type-tour" id="post-' . $row['post_id'] . '">
407
				<td class="check-column">
408
					<label for="cb-select-' . $row['identifier'] . '" class="screen-reader-text">' . $row['post_title'] . '</label>
409
					<input type="checkbox" data-identifier="' . $row['identifier'] . '" value="' . $row['post_id'] . '" name="post[]" id="cb-select-' . $row['identifier'] . '">
410
				</td>
411
				<td class="column-order">
412
					' . ( $row_key + 1 ) . '
413
				</td>
414
				<td class="post-title page-title column-title">
415
					' . $row['post_title'] . ' - ' . $status . '
416
				</td>
417
				<td class="date column-date">
418
					' . $row['reference_number'] . '
419
				</td>				
420
				<td class="date column-date">
421
					<abbr title="' . date( 'Y/m/d',strtotime( $row['last_modified'] ) ) . '">' . date( 'Y/m/d',strtotime( $row['last_modified'] ) ) . '</abbr><br>Last Modified
422
				</td>
423
				<td class="ssid column-ssid">
424
					' . $row['identifier'] . '
425
				</td>
426
			</tr>';
427
			return $row_html;
428
		}
429
	}
430
431
	/**
432
	 * Connect to wetu
433
	 */
434
	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...
435
		$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...
436
		check_ajax_referer( 'lsx_wetu_ajax_action', 'security' );
437
		if ( isset( $_POST['action'] ) && 'lsx_import_items' === $_POST['action'] && isset( $_POST['type'] ) && $_POST['type'] === $this->tab_slug && isset( $_POST['wetu_id'] ) ) {
438
439
			$wetu_id = sanitize_text_field( $_POST['wetu_id'] );
440 View Code Duplication
			if ( isset( $_POST['post_id'] ) ) {
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...
441
				$post_id = sanitize_text_field( $_POST['post_id'] );
442
			} else {
443
				$post_id = 0;
444
			}
445
446
			delete_option( 'lsx_wetu_importer_tour_settings' );
447
448 View Code Duplication
			if ( isset( $_POST['team_members'] ) ) {
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...
449
				$team_members = array_map( 'sanitize_text_field', wp_unslash( $_POST['team_members'] ) );
450
			} else {
451
				$team_members = false;
452
			}
453
454 View Code Duplication
			if ( isset( $_POST['content'] ) && is_array( $_POST['content'] ) && ! empty( $_POST['content'] ) ) {
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...
455
				$content = array_map( 'sanitize_text_field', wp_unslash( $_POST['content'] ) );
456
				add_option( 'lsx_wetu_importer_tour_settings',$content );
457
			} else {
458
				$content = false;
459
			}
460
			$jdata = wp_remote_get( 'https://wetu.com/API/Itinerary/V8/Get?id=' . $wetu_id );
461
462
			if ( ! is_wp_error( $jdata ) && ! empty( $jdata ) && isset( $jdata['response'] ) && isset( $jdata['response']['code'] ) && 200 === $jdata['response']['code'] ) {
463
				$jdata = json_decode( $jdata['body'], true );
464
				$return = $this->import_row( $jdata, $wetu_id, $post_id, $team_members, $content );
0 ignored issues
show
Bug introduced by
It seems like $team_members defined by array_map('sanitize_text..._POST['team_members'])) on line 449 can also be of type array; however, LSX_WETU_Importer_Tours::import_row() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Security Bug introduced by
It seems like $content defined by false on line 458 can also be of type false; however, LSX_WETU_Importer_Tours::import_row() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
465
				$this->format_completed_row( $return );
466
				$this->save_queue();
467
				$this->cleanup_posts();
468
				$this->attach_destination_images( $content );
0 ignored issues
show
Security Bug introduced by
It seems like $content defined by false on line 458 can also be of type false; however, LSX_WETU_Importer_Tours:...ch_destination_images() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
469
				$this->clean_attached_destinations( $return );
470
			} else {
471
				$this->format_error( esc_html__( 'There was a problem importing your tour, please contact support.', 'lsx-wetu-importer' ) );
472
			}
473
		}
474
	}
475
476
	/**
477
	 * Amends the tours destinations instead of replace.
478
	 *
479
	 * @param $id string
480
	 * @return void
481
	 */
482
	public function clean_attached_destinations( $id ) {
483
		$current_connections = get_post_meta( $id, 'destination_to_tour', false );
484
		delete_post_meta( $id,'destination_to_tour' );
485
		$current_connections = array_unique( $current_connections );
486
487
		foreach ( $current_connections as $connection ) {
488
			add_post_meta( $id, 'destination_to_tour',$connection, false );
489
		}
490
	}
491
492
	/**
493
	 * Connect to wetu
494
	 *
495
	 * @param $data array
496
	 * @param $wetu_id string
497
	 */
498
	public function import_row( $data, $wetu_id, $id = 0, $team_members = false, $importable_content = array(), $old1 = false, $old2 = false ) {
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...
499
		$post_name = '';
500
		$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...
501
		$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...
502
503
		$current_post = get_post( $id );
504
505
		$post = array(
506
			'post_type' => 'tour',
507
		);
508
509
		$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...
510
511
		if ( ! empty( $importable_content ) && in_array( 'description', $importable_content ) ) {
512
			$data_post_content = $current_post->post_content;
513
			if ( isset( $data['summary'] ) && ! empty( $data['summary'] ) ) {
514
				$data_post_content = $data['summary'];
515
			}
516
			$post['post_content'] = $data_post_content;
517
		}
518
519
		// Create or update the post.
520
		if ( false !== $id && '0' !== $id ) {
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...
521
			$post['ID'] = $id;
522
			$post['post_status'] = 'publish';
523
			if ( isset( $this->options ) && 'on' !== $this->options['disable_accommodation_title'] ) {
524
				$post['post_title'] = $data['name'];
525
			}
526
			$id = wp_update_post( $post );
527
		} else {
528
			// Set the name.
529
			if ( isset( $data['name'] ) ) {
530
				$post_name = wp_unique_post_slug( sanitize_title( $data['name'] ), $id, 'draft', 'tour', 0 );
531
			}
532
533
			if ( ! isset( $post['post_content'] ) ) {
534
				$post['post_content'] = ' ';
535
			}
536
537
			$post['post_name']   = $post_name;
538
			$post['post_title']  = $data['name'];
539
			$post['post_status'] = 'publish';
540
			$id = wp_insert_post( $post );
541
542
			// Save the WETU ID and the Last date it was modified.
543
			if ( false !== $id ) {
544
				add_post_meta( $id, 'lsx_wetu_id', $wetu_id );
545
				add_post_meta( $id, 'lsx_wetu_modified_date', strtotime( $data['last_modified'] ) );
546
			}
547
		}
548
549
		// Set reference number.
550
		$this->set_reference_number( $data, $id );
551
552
		// Set the team member if it is there.
553 View Code Duplication
		if ( post_type_exists( 'team' ) && false !== $team_members && '' !== $team_members ) {
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...
554
			$this->set_team_member( $id, $team_members );
0 ignored issues
show
Bug introduced by
The method set_team_member() does not seem to exist on object<LSX_WETU_Importer_Tours>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
555
		}
556
557
		// Set the price.
558
		if ( false !== $importable_content && in_array( 'price', $importable_content ) ) {
559
			$this->set_price( $data, $id );
560
		}
561
562
		// Set the Duration.
563
		if ( false !== $importable_content && in_array( 'duration', $importable_content ) ) {
564
			$this->set_duration( $data, $id );
565
		}
566
567
		// Set the Group Size.
568
		if ( false !== $importable_content && in_array( 'group_size', $importable_content ) ) {
569
			$this->set_group_size( $data, $id );
570
		}
571
572
		// Set the Group Size.
573
		if ( false !== $importable_content && in_array( 'tags', $importable_content ) ) {
574
			$this->set_travel_styles( $id, $data );
575
		}
576
577
		if ( false !== $importable_content && in_array( 'itineraries', $importable_content ) && isset( $data['legs'] ) && ! empty( $data['legs'] ) ) {
578
			$this->process_itineraries( $data, $id, $importable_content );
579
		}
580
581
		if ( in_array( 'map', $importable_content ) && isset( $data['routes'] ) && ! empty( $data['routes'] ) ) {
582
			$this->set_map_data( $data, $id );
583
		}
584
585
		return $id;
586
	}
587
588
	/**
589
	 * A loop which runs through each leg on the tour.
590
	 */
591
	public function process_itineraries( $data, $id, $importable_content ) {
592
		$day_counter = 1;
593
		$leg_counter = 0;
594
595
		delete_post_meta( $id, 'itinerary' );
596
597
		if ( false !== $importable_content && in_array( 'accommodation', $importable_content ) ) {
598
			delete_post_meta( $id, 'accommodation_to_tour' );
599
		}
600
		if ( false !== $importable_content && in_array( 'destination', $importable_content ) ) {
601
			delete_post_meta( $id, 'departs_from' );
602
			delete_post_meta( $id, 'ends_in' );
603
		}
604
605
		$departs_from = false;
606
		$ends_in = false;
607
608
		foreach ( $data['legs'] as $leg ) {
609
			// Itinerary Accommodation.
610
			$current_accommodation = false;
611
			if ( false !== $importable_content && in_array( 'accommodation', $importable_content ) ) {
612
				$current_accommodation = $this->set_accommodation( $leg, $id );
613
			}
614
615
			// Itinerary Destination.
616
			$current_destination = false;
617
			if ( false !== $importable_content && in_array( 'destination', $importable_content ) ) {
618
				$current_destination = $this->set_destination( $leg, $id, $leg_counter );
619
			}
620
621
			// If the Nights are the same mount of days in the array,  then it isnt "By Destination".
622
			if ( ( 1 <= (int) $leg['nights'] && isset( $leg['periods'] ) ) || 0 === $leg['itinerary_leg_id'] ) {
623
624
				foreach ( $leg['periods'] as $day_key => $day ) {
625
					$current_day = array();
626
627
					// If this is a moble tented solution.
628
					$next_day_count = $day_counter + (int) $day['days'];
629
630
					if ( isset( $leg['stops'] ) || ( 1 < (int) $day['days'] ) ) {
631
						$day_count_label = ' - ' . ( $next_day_count - 1 );
632
					} else {
633
						$day_count_label = '';
634
					}
635
					$current_day['title'] = esc_attr( 'Day ', 'lsx-wetu-importer' ) . $day_counter . $day_count_label;
636
637
					// Description.
638
					if ( false !== $importable_content && in_array( 'itinerary_description', $importable_content ) && isset( $day['notes'] ) ) {
639
						$current_day['description'] = $day['notes'];
640
					} else {
641
						$current_day['description'] = '';
642
					}
643
644
					// Itinerary Gallery.
645 View Code Duplication
					if ( false !== $importable_content && in_array( 'itinerary_gallery', $importable_content ) && isset( $day['images'] ) ) {
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...
646
						$current_day['featured_image'] = '';
647
					} else {
648
						$current_day['featured_image'] = '';
649
					}
650
651
					// Accommodation.
652 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...
653
						$current_day['accommodation_to_tour'] = array( $current_accommodation );
654
					} else {
655
						$current_day['accommodation_to_tour'] = array();
656
					}
657
658
					// Destination.
659 View Code Duplication
					if ( false !== $current_destination ) {
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...
660
						$current_day['destination_to_tour'] = array( $current_destination );
661
					} else {
662
						$current_day['destination_to_tour'] = array();
663
					}
664
665
					// Included.
666 View Code Duplication
					if ( false !== $importable_content && in_array( 'itinerary_included', $importable_content ) && isset( $day['included'] ) && '' !== $day['included'] ) {
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...
667
						$current_day['included'] = $day['included'];
668
					} else {
669
						$current_day['included'] = '';
670
					}
671
672
					// Excluded.
673 View Code Duplication
					if ( false !== $importable_content && in_array( 'itinerary_excluded', $importable_content ) && isset( $day['excluded'] ) && '' !== $day['excluded'] ) {
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...
674
						$current_day['excluded'] = $day['excluded'];
675
					} else {
676
						$current_day['excluded'] = '';
677
					}
678
679
					// Excluded.
680 View Code Duplication
					if ( false !== $importable_content && in_array( 'room_basis', $importable_content ) && isset( $day['room_basis'] ) && '' !== $day['room_basis'] ) {
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...
681
						$current_day['room_basis'] = $day['room_basis'];
682
					} else {
683
						$current_day['room_basis'] = '';
684
					}
685
686
					// Excluded.
687 View Code Duplication
					if ( false !== $importable_content && in_array( 'drinks_basis', $importable_content ) && isset( $day['drinks_basis'] ) && '' !== $day['drinks_basis'] ) {
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...
688
						$current_day['drinks_basis'] = $day['drinks_basis'];
689
					} else {
690
						$current_day['drinks_basis'] = '';
691
					}
692
693
					$this->set_itinerary_day( $current_day, $id );
694
					$day_counter = $next_day_count;
695
				}
696
			} else {
697
				// This is for the by destination.
698
699
				$current_day = array();
700
				$next_day_count = $day_counter + (int) $leg['nights'];
701
				$day_count_label = $next_day_count - 1;
702
703
				$current_day['title'] = esc_attr( 'Day ', 'lsx-wetu-importer' ) . $day_counter;
704
705
				if ( 0 !== (int) $leg['nights'] ) {
706
					$current_day['title'] .= ' - ' . $day_count_label;
707
				}
708
709
				// Description.
710
				if ( false !== $importable_content && in_array( 'itinerary_description', $importable_content ) && isset( $leg['notes'] ) ) {
711
					$current_day['description'] = $leg['notes'];
712
				} else {
713
					$current_day['description'] = '';
714
				}
715
716
				// Itinerary Gallery.
717 View Code Duplication
				if ( false !== $importable_content && in_array( 'itinerary_gallery', $importable_content ) && isset( $leg['images'] ) ) {
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...
718
					$current_day['featured_image'] = '';
719
				} else {
720
					$current_day['featured_image'] = '';
721
				}
722
723
				// Accommodation.
724 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...
725
					$current_day['accommodation_to_tour'] = array( $current_accommodation );
726
				} else {
727
					$current_day['accommodation_to_tour'] = array();
728
				}
729
730
				// Destination.
731 View Code Duplication
				if ( false !== $current_destination ) {
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...
732
					$current_day['destination_to_tour'] = array( $current_destination );
733
				} else {
734
					$current_day['destination_to_tour'] = array();
735
				}
736
737
				// Included.
738 View Code Duplication
				if ( false !== $importable_content && in_array( 'itinerary_included', $importable_content ) && isset( $leg['included'] ) && '' !== $leg['included'] ) {
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...
739
					$current_day['included'] = $leg['included'];
740
				} else {
741
					$current_day['included'] = '';
742
				}
743
744
				// Excluded.
745 View Code Duplication
				if ( false !== $importable_content && in_array( 'itinerary_excluded', $importable_content ) && isset( $leg['excluded'] ) && '' !== $leg['excluded'] ) {
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...
746
					$current_day['excluded'] = $leg['excluded'];
747
				} else {
748
					$current_day['excluded'] = '';
749
				}
750
751
				// Excluded.
752 View Code Duplication
				if ( false !== $importable_content && in_array( 'room_basis', $importable_content ) && isset( $leg['room_basis'] ) && '' !== $leg['room_basis'] ) {
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...
753
					$current_day['room_basis'] = $leg['room_basis'];
754
				} else {
755
					$current_day['room_basis'] = '';
756
				}
757
758
				// Excluded.
759 View Code Duplication
				if ( false !== $importable_content && in_array( 'drinks_basis', $importable_content ) && isset( $leg['drinks_basis'] ) && '' !== $leg['drinks_basis'] ) {
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...
760
					$current_day['drinks_basis'] = $leg['drinks_basis'];
761
				} else {
762
					$current_day['drinks_basis'] = '';
763
				}
764
765
				$this->set_itinerary_day( $current_day,$id );
766
				$day_counter = $next_day_count;
767
			}
768
769
			// If we are in the first leg,  and the destination was attached then save it as the departure field.
770
			if ( 0 === $leg_counter && false !== $current_destination ) {
771
				$departs_from = $current_destination;
772
			}
773
774
			// If its the last leg then save it as the ends in.
775
			if ( ( count( $data['legs'] ) - 2 ) === $leg_counter && false !== $current_destination ) {
776
				$ends_in = $current_destination;
777
			}
778
779
			$leg_counter++;
780
		}
781
782
		if ( false !== $departs_from ) {
783
			add_post_meta( $id, 'departs_from', $departs_from, true );
784
		}
785
		if ( false !== $ends_in ) {
786
			add_post_meta( $id, 'ends_in', $ends_in, true );
787
		}
788
	}
789
790
	/**
791
	 * Run through your routes and save the points as a KML file.
792
	 */
793
	public function set_map_data( $data, $id, $zoom = 9 ) {
794
		if ( ! empty( $data['routes'] ) ) {
795
			delete_post_meta( $id,'wetu_map_points' );
796
797
			$points = array();
798
799
			foreach ( $data['routes'] as $route ) {
800
801
				if ( isset( $route['points'] ) && '' !== $route['points'] ) {
802
803
					$temp_points = explode( ';',$route['points'] );
804
					$point_counter = count( $temp_points );
805
806
					for ( $x = 0; $x <= $point_counter; $x++ ) {
807
						$y = $x + 1;
808
						$points[] = $temp_points[ $x ] . ',' . $temp_points[ $y ];
809
						$x++;
810
					}
811
				}
812
			}
813
814
			if ( ! empty( $points ) ) {
815
				$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...
816
			}
817
		}
818
819
	}
820
821
	// CLASS SPECIFIC FUNCTIONS.
822
823
	/**
824
	 * Set the Itinerary Day.
825
	 */
826
	public function set_itinerary_day( $day, $id ) {
827
		$this->save_custom_field( $day, 'itinerary', $id, false, false );
828
	}
829
830
	/**
831
	 * Set the ref number
832
	 */
833
	public function set_reference_number( $data, $id ) {
834
		if ( isset( $data['reference_number'] ) && '' !== $data['reference_number'] ) {
835
			$this->save_custom_field( $data['reference_number'], 'lsx_wetu_ref', $id );
836
		}
837
	}
838
839
	/**
840
	 * Set the price.
841
	 */
842
	public function set_price( $data, $id ) {
843
		// Price.
844
		if ( isset( $data['price'] ) && '' !== $data['price'] ) {
845
			$price = $data['price'];
846
			if ( false === apply_filters( 'lsx_wetu_importer_disable_tour_price_filter', false ) ) {
847
				$price = preg_replace( '/[^0-9,.]/', '', $price );
848
			}
849
			$meta_key = apply_filters( 'lsx_wetu_importer_price_meta_key', 'price' );
850
			$this->save_custom_field( $price, $meta_key, $id );
851
		}
852
853
		// Price includes.
854
		if ( isset( $data['price_includes'] ) && '' !== $data['price_includes'] ) {
855
			$meta_key = apply_filters( 'lsx_wetu_importer_included_meta_key', 'included' );
856
			$this->save_custom_field( $data['price_includes'], $meta_key, $id );
857
		}
858
859
		// Price Excludes.
860
		if ( isset( $data['price_excludes'] ) && '' !== $data['price_excludes'] ) {
861
			$meta_key = apply_filters( 'lsx_wetu_importer_not_included_meta_key', 'not_included' );
862
			$this->save_custom_field( $data['price_excludes'], $meta_key, $id );
863
		}
864
	}
865
866
	/**
867
	 * Set the duration.
868
	 */
869
	public function set_duration( $data, $id ) {
870
		if ( isset( $data['days'] ) && ! empty( $data['days'] ) ) {
871
			$price = $data['days'];
872
			$price = preg_replace( '/[^0-9,.]/', '', $price );
873
			$this->save_custom_field( $price, 'duration', $id );
874
		}
875
	}
876
877
	/**
878
	 * Set the group size
879
	 */
880
	public function set_group_size( $data, $id ) {
881
		if ( isset( $data['group_size'] ) && ! empty( $data['group_size'] ) ) {
882
			$group_size = $data['group_size'];
883
			$this->save_custom_field( $group_size,'group_size',$id );
884
		}
885
	}
886
887
	/**
888
	 * Takes the WETU tags and sets the Travel Styles.
889
	 *
890
	 * @param string $id
891
	 * @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...
892
	 * @return void
893
	 */
894
	public function set_travel_styles( $id, $data ) {
895
		$tags = apply_filters( 'lsx_wetu_importer_tour_travel_styles', $data['tags'] );
896
		if ( isset( $data['tags'] ) && ! empty( $tags ) ) {
897
			foreach ( $tags as $tag ) {
898
				$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...
899
			}
900
		}
901
	}
902
903
	/**
904
	 * Connects the Accommodation if its available
905
	 */
906
	public function set_accommodation( $day, $id ) {
907
		$ac_id = false;
908
		$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...
909
910
		if ( isset( $day['content_entity_id'] ) && ! empty( $day['content_entity_id'] ) ) {
911
			if ( false !== $this->current_accommodation && ! empty( $this->current_accommodation ) && array_key_exists( $day['content_entity_id'],$this->current_accommodation ) ) {
912
				$ac_id = $this->current_accommodation[ $day['content_entity_id'] ];
913
			} else {
914
				$ac_id = wp_insert_post(array(
915
					'post_type' => 'accommodation',
916
					'post_status' => 'draft',
917
					'post_title' => $day['content_entity_id'],
918
				));
919
920
				$this->save_custom_field( $day['content_entity_id'],'lsx_wetu_id',$ac_id );
921
			}
922
923
			if ( '' !== $ac_id && false !== $ac_id ) {
924
				$this->save_custom_field( $ac_id,'accommodation_to_tour',$id,false,false );
925
				$this->save_custom_field( $id,'tour_to_accommodation',$ac_id,false,false );
926
				$this->queue_item( $ac_id );
927
			}
928
		}
929
		return $ac_id;
930
	}
931
932
	/**
933
	 * Grab all the current accommodation posts via the lsx_wetu_id field.
934
	 *
935
	 * @param $post_type string
936
	 * @return boolean / array
937
	 */
938
	public function find_current_accommodation( $post_type = 'accommodation' ) {
939
		global $wpdb;
940
		$accommodation = parent::find_current_accommodation( $post_type );
941
		$return = false;
942
943
		if ( ! empty( $accommodation ) ) {
944
			foreach ( $accommodation as $key => $acc ) {
945
				$return[ $acc->meta_value ] = $acc->post_id;
946
			}
947
		}
948
949
		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 LSX_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...
950
	}
951
952
	/**
953
	 * Grab all the current accommodation posts via the lsx_wetu_id field.
954
	 * @return boolean / array
955
	 */
956
	public function find_current_destinations() {
957
		return $this->find_current_accommodation( 'destination' );
958
	}
959
960
	/**
961
	 * Connects the destinations post type
962
	 *
963
	 * @param $day array
964
	 * @param $id string
965
	 * @return boolean / string
966
	 */
967
	public function set_destination( $day, $id, $leg_counter ) {
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...
968
		$dest_id    = false;
969
		$country_id = false;
970
971
		$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...
972
973
		if ( isset( $day['destination_content_entity_id'] ) && ! empty( $day['destination_content_entity_id'] ) ) {
974
			if ( false !== $this->current_destinations && ! empty( $this->current_destinations ) && array_key_exists( $day['destination_content_entity_id'], $this->current_destinations ) ) {
975
				$dest_id = $this->current_destinations[ $day['destination_content_entity_id'] ];
976
977
				//TODO Check for attachments here.
978
				$this->destination_images[ $id ][] = array( $dest_id, $day['destination_content_entity_id'] );
979
980
				//Check if there is a country asigned.
981
				$potential_id = wp_get_post_parent_id( $dest_id );
982
				$country_wetu_id = get_post_meta( $potential_id, 'lsx_wetu_id', true );
983
984
				if ( false !== $country_wetu_id ) {
985
					$country_id = $this->set_country( $country_wetu_id, $id );
986
					$this->destination_images[ $id ][] = array( $id, $country_wetu_id );
987
				}
988
			} else {
989
				$destination_json = wp_remote_get( 'https://wetu.com/API/Pins/' . $this->api_key . '/Get?ids=' . $day['destination_content_entity_id'] );
990
991
				if ( ! empty( $destination_json ) && isset( $destination_json['response'] ) && isset( $destination_json['response']['code'] ) && 200 === $destination_json['response']['code'] ) {
992
993
					$destination_data = json_decode( $destination_json['body'], true );
994
995
					if ( ! empty( $destination_data ) && ! isset( $destination_data['error'] ) ) {
996
						$destination_title = $day['destination_content_entity_id'];
997
998
						if ( isset( $destination_data[0]['name'] ) ) {
999
							$destination_title = $destination_data[0]['name'];
1000
						}
1001
1002
						if ( isset( $destination_data[0]['map_object_id'] ) && isset( $destination_data[0]['position']['country_content_entity_id'] )
1003
							&& $destination_data[0]['map_object_id'] !== $destination_data[0]['position']['country_content_entity_id'] ) {
1004
1005
							$country_id = $this->set_country( $destination_data[0]['position']['country_content_entity_id'], $id );
1006
							// Save the destination so we can grab the tour featured image and banner from them.
1007
						}
1008
1009
						$dest_post = array(
1010
							'post_type' => 'destination',
1011
							'post_status' => 'draft',
1012
							'post_title' => $destination_title,
1013
						);
1014
1015
						if ( false !== $country_id ) {
1016
							$dest_post['post_parent'] = $country_id;
1017
						}
1018
						$dest_id = wp_insert_post( $dest_post );
1019
1020
						// Make sure we register the.
1021
						$this->current_destinations[ $day['destination_content_entity_id'] ] = $dest_id;
1022
1023
						// If there are images attached then use the destination.
1024 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...
1025
							$this->destination_images[ $id ][] = array( $dest_id, $day['destination_content_entity_id'] );
1026
						}
1027
1028
						$this->save_custom_field( $day['destination_content_entity_id'], 'lsx_wetu_id', $dest_id );
1029
					}
1030
				}
1031
			}
1032
1033
			if ( '' !== $dest_id && false !== $dest_id ) {
1034
				$this->save_custom_field( $dest_id, 'destination_to_tour', $id, false, false );
1035
				$this->save_custom_field( $id, 'tour_to_destination', $dest_id, false, false );
1036
1037
				//Save the item to display in the queue
1038
				$this->queue_item( $dest_id );
1039
1040
				//Save the item to clean up the amount of connections.
1041
				$this->cleanup_posts[ $dest_id ] = 'tour_to_destination';
1042
1043
				//Add this relation info so we can make sure certain items are set as countries.
1044
				if ( 0 !== $country_id && false !== $country_id ) {
1045
					$this->relation_meta[ $dest_id ] = $country_id;
1046
					$this->relation_meta[ $country_id ] = 0;
1047
				} else {
1048
					$this->relation_meta[ $dest_id ] = 0;
1049
				}
1050
			}
1051
		}
1052
		return $dest_id;
1053
	}
1054
1055
	/**
1056
	 * Connects the destinations post type
1057
	 *
1058
	 * @param $dest_id string
1059
	 * @param $country_id array
1060
	 * @param $id string
1061
	 *
1062
	 * @return string
1063
	 */
1064
	public function set_country( $country_wetu_id, $id ) {
1065
		$country_id = false;
1066
		$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...
1067
1068
		if ( false !== $this->current_destinations && ! empty( $this->current_destinations ) && array_key_exists( $country_wetu_id, $this->current_destinations ) ) {
1069
			$country_id = $this->current_destinations[ $country_wetu_id ];
1070
			$this->destination_images[ $id ][] = array( $country_id, $country_wetu_id );
1071
		} else {
1072
			$country_json = wp_remote_get( 'https://wetu.com/API/Pins/' . $this->api_key . '/Get?ids=' . $country_wetu_id );
1073
1074
			if ( ! empty( $country_json ) && isset( $country_json['response'] ) && isset( $country_json['response']['code'] ) && 200 === $country_json['response']['code'] ) {
1075
				$country_data = json_decode( $country_json['body'], true );
1076
1077
				// Format the title of the destination if its available,  otherwise default to the WETU ID.
1078
				$country_title = $country_wetu_id;
1079
1080
				if ( isset( $country_data[0]['name'] ) ) {
1081
					$country_title = $country_data[0]['name'];
1082
				}
1083
1084
				$country_id = wp_insert_post(array(
1085
					'post_type' => 'destination',
1086
					'post_status' => 'draft',
1087
					'post_title' => $country_title,
1088
				));
1089
1090
				//add the country to the current destination stack
1091
				$this->current_destinations[ $country_wetu_id ] = $country_id;
1092
1093
				// Check if there are images and save fore use later.
1094 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...
1095
					$this->destination_images[ $id ][] = array( $country_id,$country_wetu_id );
1096
				}
1097
1098
				//Save the wetu field
1099
				$this->save_custom_field( $country_wetu_id, 'lsx_wetu_id', $country_id );
1100
			}
1101
		}
1102
1103
		if ( '' !== $country_id && false !== $country_id ) {
1104
			$this->save_custom_field( $country_id, 'destination_to_tour', $id, false, false );
1105
			$this->save_custom_field( $id, 'tour_to_destination', $country_id, false, false );
1106
			$this->queue_item( $country_id );
1107
			$this->cleanup_posts[ $country_id ] = 'tour_to_destination';
1108
1109
			return $country_id;
1110
		}
1111
	}
1112
1113
	/**
1114
	 * Connects the destinations post type
1115
	 *
1116
	 * @param $dest_id string
1117
	 * @param $country_id array
1118
	 * @param $id string
1119
	 *
1120
	 * @return string
1121
	 */
1122
	public function attach_destination_images( $importable_content = array() ) {
1123
		if ( false !== $this->destination_images ) {
1124
			$this->shuffle_assoc( $this->destination_images );
1125
1126
			foreach ( $this->destination_images as $tour => $destinations ) {
1127
				//$this->shuffle_assoc( $destinations );
1128
1129
				$image_set = false;
1130
				$forced = false;
1131
1132
				foreach ( $destinations as $destination ) {
1133
					if ( false === $image_set && false === $forced ) {
1134
						$url = 'https://wetu.com/API/Pins/' . $this->api_key;
1135
1136
						$url_qs = '';
1137
						$jdata = wp_remote_get( $url . '/Get?' . $url_qs . '&ids=' . $destination[1] );
1138
1139
						if ( ! empty( $jdata ) && isset( $jdata['response'] ) && isset( $jdata['response']['code'] ) && 200 === $jdata['response']['code'] ) {
1140
							$adata = json_decode( $jdata['body'], true );
1141
1142
							if ( ! empty( $adata ) && ! empty( $adata[0]['content']['images'] ) ) {
1143
								$this->find_attachments( $destination[0] );
1144
1145
								// Set the featured image.
1146
								if ( false !== $importable_content && in_array( 'featured_image', $importable_content ) ) {
1147
									$image_set = $this->set_featured_image( $adata, $tour );
1148 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...
1149
										$image_set = $this->set_banner_image( $adata, $tour );
1150
										$forced = true;
1151
									}
1152
									continue;
1153
								}
1154 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...
1155
									$image_set = $this->set_banner_image( $adata, $tour );
1156
								}
1157
							}
1158
						}
1159
					} else {
1160
						continue;
1161
					}
1162
				}
1163
			}
1164
		}
1165
	}
1166
1167
	/**
1168
	 * Creates the main gallery data
1169
	 */
1170
	public function set_featured_image( $data, $id ) {
1171
		$image_set = false;
1172
		$counter = 0;
1173
1174
		if ( is_array( $data[0]['content']['images'] ) && ! empty( $data[0]['content']['images'] ) ) {
1175
			foreach ( $data[0]['content']['images'] as $v ) {
1176
				/*print_r('<pre>');
1177
				print_r( $v );
1178
				print_r('</pre>');*/
1179
1180
				if ( true === $image_set ) {
1181
					$counter++;
1182
					continue;
1183
				}
1184
1185
				if ( ! $this->check_if_image_is_used( $v ) ) {
1186
					$temp_featured_image = $this->attach_image( $v , $id );
1187
1188
					if ( false !== $temp_featured_image ) {
1189
						$this->featured_image = $temp_featured_image;
1190
						delete_post_meta( $id, '_thumbnail_id' );
1191
						add_post_meta( $id, '_thumbnail_id', $this->featured_image, true );
1192
						$image_set = true;
1193
					}
1194
				}
1195
1196
				$counter++;
1197
			}
1198
		}
1199
		return $image_set;
1200
	}
1201
1202
	/**
1203
	 * Sets a banner image
1204
	 */
1205
	public function set_banner_image( $data, $id, $content = array( 'none' ) ) {
1206
		$image_set = false;
1207
		$counter = 0;
1208
1209
		if ( is_array( $data[0]['content']['images'] ) && ! empty( $data[0]['content']['images'] ) ) {
1210
1211
			foreach ( $data[0]['content']['images'] as $v ) {
1212
				/*print_r('<pre>');
1213
				print_r( $v );
1214
				print_r('</pre>');*/
1215
1216
				if ( true === $image_set || 0 === $counter ) {
1217
					$counter++;
1218
					continue;
1219
				}
1220
1221
				if ( ! $this->check_if_image_is_used( $v ) ) {
1222
					$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...
1223
						'width' => '1920',
1224
						'height' => '600',
1225
						'cropping' => 'c',
1226
					) );
1227
1228 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...
1229
						$this->banner_image = $temp_banner;
1230
1231
						delete_post_meta( $id,'image_group' );
1232
1233
						$new_banner = array(
1234
							'banner_image' => array(
1235
								'cmb-field-0' => $this->banner_image,
1236
							),
1237
						);
1238
						add_post_meta( $id,'image_group',$new_banner,true );
1239
						$image_set = true;
1240
					}
1241
				}
1242
				$counter++;
1243
			}
1244
		}
1245
1246
		return $image_set;
1247
	}
1248
1249
	/**
1250
	 * Grabs all of the current used featured images on the site.
1251
	 */
1252
	public function check_if_image_is_used( $v ) {
1253
		global $wpdb;
1254
1255
		$temp_fragment = explode( '/', $v['url_fragment'] );
1256
		$url_filename = $temp_fragment[ count( $temp_fragment ) - 1 ];
1257
		$url_filename = str_replace( array( '.jpg', '.png', '.jpeg' ), '', $url_filename );
1258
		$url_filename = trim( $url_filename );
1259
		$url_filename = str_replace( ' ', '_', $url_filename );
1260
1261
		if ( in_array( $url_filename, $this->found_attachments ) ) {
1262
			// check to see if there is a featured image set with this ID.
1263
			$found_id = array_search( $url_filename, $this->found_attachments );
1264
1265
			$results = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value = '%s' AND meta_key = '_thumbnail_id'", array( $found_id ) ) );
1266
			if ( ! empty( $results ) ) {
1267
				return true;
1268
			} else {
1269
				return false;
1270
			}
1271
		} else {
1272
			$results = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_name = '%s'", array( $url_filename ) ) );
1273
1274
			if ( ! empty( $results ) ) {
1275
				$results = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value = '%s' AND meta_key = '_thumbnail_id'", array( $results[0]->ID ) ) );
1276
				if ( ! empty( $results ) ) {
1277
					return true;
1278
				} else {
1279
					return false;
1280
				}
1281
			} else {
1282
				return false;
1283
			}
1284
		}
1285
	}
1286
1287
	/**
1288
	 * Que an item to be saved.
1289
	 *
1290
	 * @param   $id     int
1291
	 */
1292
	public function queue_item( $id ) {
1293
		if ( is_array( $this->import_queue ) && ! in_array( $id, $this->import_queue ) ) {
1294
			$this->import_queue[] = $id;
1295
		} else {
1296
			$this->import_queue[] = $id;
1297
		}
1298
	}
1299
1300
	/**
1301
	 * Saves the queue to the option.
1302
	 */
1303
	public function save_queue() {
1304
		if ( ! empty( $this->import_queue ) ) {
1305
			if ( ! empty( $this->queued_imports ) ) {
1306
				$saved_imports = array_merge( $this->queued_imports, $this->import_queue );
1307
			} else {
1308
				$saved_imports = $this->import_queue;
1309
			}
1310
1311
			delete_option( 'lsx_wetu_importer_que' );
1312
1313
			if ( ! empty( $saved_imports ) ) {
1314
				$saved_imports = array_unique( $saved_imports );
1315
				update_option( 'lsx_wetu_importer_que', $saved_imports );
1316
			}
1317
		}
1318
	}
1319
1320
	/**
1321
	 * The header of the item list
1322
	 */
1323 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...
1324
		?>
1325
		<thead>
1326
		<tr>
1327
			<th class="manage-column column-cb check-column" id="cb" scope="col">
1328
				<label for="cb-select-all-1" class="screen-reader-text"><?php esc_attr_e( 'Select All', 'lsx-wetu-importer' ); ?></label>
1329
				<input type="checkbox" id="cb-select-all-1">
1330
			</th>
1331
			<th class="manage-column column-order " id="order" scope="col"><?php esc_attr_e( 'Order', 'lsx-wetu-importer' ); ?></th>
1332
			<th class="manage-column column-title " id="title" scope="col"><?php esc_attr_e( 'Title', 'lsx-wetu-importer' ); ?></th>
1333
			<th class="manage-column column-date" id="ref" scope="col"><?php esc_attr_e( 'Ref', 'lsx-wetu-importer' ); ?></th>
1334
			<th class="manage-column column-date" id="date" scope="col"><?php esc_attr_e( 'Date', 'lsx-wetu-importer' ); ?></th>
1335
			<th class="manage-column column-ssid" id="ssid" scope="col"><?php esc_attr_e( 'WETU ID', 'lsx-wetu-importer' ); ?></th>
1336
		</tr>
1337
		</thead>
1338
		<?php
1339
	}
1340
1341
	/**
1342
	 * The footer of the item list
1343
	 */
1344 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...
1345
		?>
1346
		<tfoot>
1347
		<tr>
1348
			<th class="manage-column column-cb check-column" id="cb" scope="col">
1349
				<label for="cb-select-all-1" class="screen-reader-text"><?php esc_attr_e( 'Select All', 'lsx-wetu-importer' ); ?></label>
1350
				<input type="checkbox" id="cb-select-all-1">
1351
			</th>
1352
			<th class="manage-column column-order " id="order" scope="col"><?php esc_attr_e( 'Order', 'lsx-wetu-importer' ); ?></th>
1353
			<th class="manage-column column-title" scope="col"><?php esc_attr_e( 'Title', 'lsx-wetu-importer' ); ?></th>
1354
			<th class="manage-column column-date" id="ref" scope="col"><?php esc_attr_e( 'Ref', 'lsx-wetu-importer' ); ?></th>			
1355
			<th class="manage-column column-date" scope="col"><?php esc_attr_e( 'Date', 'lsx-wetu-importer' ); ?></th>
1356
			<th class="manage-column column-ssid" scope="col"><?php esc_attr_e( 'WETU ID', 'lsx-wetu-importer' ); ?></th>
1357
		</tr>
1358
		</tfoot>
1359
		<?php
1360
	}
1361
}
1362