Completed
Push — master ( a84d89...a9842c )
by Fernando
02:35
created

WETU_Importer_Tours::attach_destination_images()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 43
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 21
nc 10
nop 1

How to fix   Complexity   

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   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
		if ( false !== $this->api_username && false !== $this->api_password ) {
102
			$this->url    = 'https://wetu.com/API/Itinerary/';
103
			$this->url_qs = 'username=' . $this->api_username . '&password=' . $this->api_password;
104
		} elseif ( false !== $this->api_key ) {
105
			$this->url    = 'https://wetu.com/API/Itinerary/' . $this->api_key;
106
			$this->url_qs = '';
107
		}
108
109
		$temp_options = get_option( '_lsx-to_settings',false );
110
111
		if ( false !== $temp_options && isset( $temp_options[ $this->plugin_slug ] ) && ! empty( $temp_options[ $this->plugin_slug ] ) ) {
112
			$this->options = $temp_options[ $this->plugin_slug ];
113
		}
114
115
		$tour_options = get_option( 'wetu_importer_tour_settings',false );
116
117
		if ( false !== $tour_options ) {
118
			$this->tour_options = $tour_options;
119
		}
120
	}
121
122
	/**
123
	 * Display the importer administration screen
124
	 */
125
	public function display_page() {
126
		?>
127
		<div class="wrap">
128
			<?php $this->navigation( 'tour' ); ?>
129
130
			<?php $this->update_options_form(); ?>
131
132
			<?php $this->search_form(); ?>
133
134
			<form method="get" action="" id="posts-filter">
135
				<input type="hidden" name="post_type" class="post_type" value="<?php echo esc_attr( $this->tab_slug ); ?>" />
136
137
				<p><input class="button button-primary add" type="button" value="<?php esc_html_e( 'Add to List','wetu-importer' ); ?>" />
138
					<input class="button button-primary clear" type="button" value="<?php esc_html_e( 'Clear','wetu-importer' ); ?>" />
139
				</p>
140
141
				<table class="wp-list-table widefat fixed posts">
142
					<?php $this->table_header(); ?>
0 ignored issues
show
Unused Code introduced by
The call to the method WETU_Importer_Tours::table_header() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
143
144
					<tbody id="the-list">
145
						<tr class="post-0 type-tour status-none" id="post-0">
146
							<th class="check-column" scope="row">
147
								<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>
148
							</th>
149
							<td class="post-title page-title column-title">
150
								<strong>
151
									<?php esc_html_e( 'Enter a title to search for','wetu-importer' ); ?>
152
								</strong>
153
							</td>
154
							<td class="date column-date">
155
							</td>
156
							<td class="ssid column-ssid">
157
							</td>
158
						</tr>
159
					</tbody>
160
161
					<?php $this->table_footer(); ?>
0 ignored issues
show
Unused Code introduced by
The call to the method WETU_Importer_Tours::table_footer() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
162
163
				</table>
164
165
				<p><input class="button button-primary add" type="button" value="<?php esc_html_e( 'Add to List','wetu-importer' ); ?>" />
166
					<input class="button button-primary clear" type="button" value="<?php esc_html_e( 'Clear','wetu-importer' ); ?>" />
167
				</p>
168
			</form>
169
170
			<div style="display:none;" class="import-list-wrapper">
171
				<br />
172
				<form method="get" action="" id="import-list">
173
174
					<div class="row">
175
						<div class="settings-all" style="width:30%;display:block;float:left;">
176
							<h3><?php esc_html_e( 'What content to Sync from WETU' ); ?></h3>
177
							<ul>
178
								<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...
179
180
								<?php if ( isset( $this->options ) && 'on' !== $this->options['disable_tour_descriptions'] ) { ?>
181
									<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...
182
								<?php } ?>
183
184
								<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...
185
								<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...
186
187
								<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...
188
189
								<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...
190
191
								<?php /*if(class_exists('LSX_TO_Maps')){ ?>
0 ignored issues
show
Unused Code Comprehensibility introduced by
83% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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
							</ul>
195
						</div>
196
						<div class="settings-all" style="width:30%;display:block;float:left;">
197
							<h3><?php esc_html_e( 'Itinerary Info' ); ?></h3>
198
							<ul>
199
								<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...
200
								<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...
201
								<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...
202
							</ul>
203
204
							<h4><?php esc_html_e( 'Additional Content' ); ?></h4>
205
							<ul>
206
								<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...
207
								<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...
208
								<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...
209
								<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...
210
							</ul>
211
						</div>
212
						<?php if ( class_exists( 'LSX_TO_Team' ) ) { ?>
213
							<div style="width:30%;display:block;float:left;">
214
								<h3><?php esc_html_e( 'Assign a Team Member' ); ?></h3>
215
								<?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...
216
							</div>
217
						<?php } ?>
218
219
						<br clear="both" />
220
					</div>
221
222
					<h3><?php esc_html_e( 'Your List' ); ?></h3>
223
					<p><input class="button button-primary" type="submit" value="<?php esc_html_e( 'Sync','wetu-importer' ); ?>" /></p>
224
					<table class="wp-list-table widefat fixed posts">
225
						<?php $this->table_header(); ?>
0 ignored issues
show
Unused Code introduced by
The call to the method WETU_Importer_Tours::table_header() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
226
227
						<tbody>
228
229
						</tbody>
230
231
						<?php $this->table_footer(); ?>
0 ignored issues
show
Unused Code introduced by
The call to the method WETU_Importer_Tours::table_footer() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
232
233
					</table>
234
235
					<p><input class="button button-primary" type="submit" value="<?php esc_html_e( 'Sync','wetu-importer' ); ?>" /></p>
236
				</form>
237
			</div>
238
239
			<div style="display:none;" class="completed-list-wrapper">
240
				<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>
241
				<ul>
242
				</ul>
243
			</div>
244
		</div>
245
		<?php
246
	}
247
248
	/**
249
	 * search_form
250
	 */
251
	public function update_options_form() {
252
		$tours = get_transient( 'lsx_ti_tours' );
253
254
		echo '<div class="wetu-status tour-wetu-status"><h3>' . esc_html__( 'Wetu Status','wetu-importer' ) . ' - ';
255
256
		if ( '' === $tours || false === $tours || isset( $_GET['refresh_tours'] ) ) {
257
			$result = $this->update_options();
258
259
			if ( true === $result ) {
260
				echo '<span style="color:green;">' . esc_attr( 'Connected','wetu-importer' ) . '</span>';
261
				echo ' - <small><a href="#">' . esc_attr( 'Refresh','wetu-importer' ) . '</a></small>';
262
			} else {
263
				echo '<span style="color:red;">' . wp_kses_post( $result ) . '</span>';
264
			}
265
		} else {
266
			echo '<span style="color:green;">' . esc_attr( 'Connected','wetu-importer' ) . '</span> - <small><a href="#">' . esc_attr( 'Refresh','wetu-importer' ) . '</a></small>';
267
		}
268
269
		echo '</h3>';
270
271
		$form_options = get_option( 'lsx_ti_tours_api_options' );
272
		if ( false === $form_options ) {
273
			$form_options = array( 0 );
274
		}
275
		?>
276
		<form method="get" class="tour-refresh-form" action="<?php echo esc_attr( admin_url( 'admin.php' ) ); ?>">
277
			<input type="hidden" name="page" value="<?php echo esc_attr( $this->plugin_slug ); ?>" />
278
			<input type="hidden" name="tab" value="tour" />
279
			<input type="hidden" name="refresh_tours" value="true" />
280
281
			<p class="tour-search-options">
282
				<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>
283
			</p>
284
285
			<p class="tour-search-options">
286
				<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>
287
				<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>
288
				<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>
289
			</p>
290
291
			<p><input class="button button-primary submit" type="submit" value="<?php echo esc_attr( 'Refresh Tours','wetu-importer' ); ?>"></p>
292
		</form>
293
		<br />
294
		<?php
295
		echo '</div>';
296
	}
297
298
	/**
299
	 * Save the list of Tours into an option
300
	 */
301
	public function update_options() {
302
		$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...
303
		$options = array();
304
305
		delete_option( 'lsx_ti_tours_api_options' );
306
307
		if ( isset( $_GET['own'] ) ) {
308
			$this->url_qs .= '&own=true';
309
			$options[] = 'own';
310
		}
311
312
		if ( isset( $_GET['type'] ) ) {
313
			$this->url_qs .= '&type=' . implode( '',$_GET['type'] );
314
			$options[] = implode( '',$_GET['type'] );
315
		}
316
317
		$this->url_qs .= '&results=2000';
318
319
		add_option( 'lsx_ti_tours_api_options',$options );
320
321
		$data = file_get_contents( $this->url . '/V7/List?' . $this->url_qs );
322
323
		$tours = json_decode( $data, true );
324
325
		if ( isset( $tours['error'] ) ) {
326
			return $tours['error'];
327 View Code Duplication
		} elseif ( isset( $tours['itineraries'] ) && ! empty( $tours['itineraries'] ) ) {
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...
328
			set_transient( 'lsx_ti_tours',$tours['itineraries'],60 * 60 * 2 );
329
			return true;
330
		}
331
	}
332
333
	/**
334
	 * Grab all the current tour posts via the lsx_wetu_id field.
335
	 */
336 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...
337
		global $wpdb;
338
		$return = array();
339
340
		$current_tours = $wpdb->get_results("
341
			SELECT key1.post_id,key1.meta_value
342
			FROM {$wpdb->postmeta} key1
343
344
			INNER JOIN  {$wpdb->posts} key2
345
			ON key1.post_id = key2.ID
346
347
			WHERE key1.meta_key = 'lsx_wetu_id'
348
			AND key2.post_type = 'tour'
349
350
			LIMIT 0,500
351
		");
352
353
		if ( null !== $current_tours && ! empty( $current_tours ) ) {
354
			foreach ( $current_tours as $tour ) {
355
				$return[ $tour->meta_value ] = $tour;
356
			}
357
		}
358
359
		return $return;
360
	}
361
362
	/**
363
	 * Run through the accommodation grabbed from the DB.
364
	 */
365
	public function process_ajax_search() {
366
		$return = false;
367
368
		// @codingStandardsIgnoreLine
369
		if ( isset( $_POST['action'] ) && $_POST['action'] === 'lsx_tour_importer' && isset( $_POST['type'] ) && $_POST['type'] === $this->tab_slug ) {
370
			$tours = get_transient( 'lsx_ti_tours' );
371
372
			if ( false !== $tours ) {
373
374
				$searched_items = false;
375
376
				// @codingStandardsIgnoreLine
377 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...
378
					// @codingStandardsIgnoreLine
379
					$keyphrases = $_POST['keyword'];
380
				} else {
381
					$keyphrases = array( 0 );
382
				}
383
384
				if ( ! is_array( $keyphrases ) ) {
385
					$keyphrases = array( $keyphrases );
386
				}
387
				foreach ( $keyphrases as &$keyword ) {
388
					$keyword = ltrim( rtrim( $keyword ) );
389
				}
390
391
				$post_status = false;
392
				if ( in_array( 'publish',$keyphrases ) ) {
393
					$post_status = 'publish';
394
				}
395
				if ( in_array( 'pending',$keyphrases ) ) {
396
					$post_status = 'pending';
397
				}
398
				if ( in_array( 'draft',$keyphrases ) ) {
399
					$post_status = 'draft';
400
				}
401
				if ( in_array( 'import',$keyphrases ) ) {
402
					$post_status = 'import';
403
				}
404
405
				if ( ! empty( $tours ) ) {
406
					$current_tours = $this->find_current_tours();
407
408
					foreach ( $tours as $row_key => $row ) {
409
						if ( isset( $row['is_disabled'] ) && true === $row['is_disabled'] ) {
410
							continue;
411
						}
412
413
						/*if('Sample' === $row['type']){
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
414
							continue;
415
						}*/
416
417
						//If this is a current tour, add its ID to the row.
418
						$row['post_id'] = 0;
419
420
						if ( false !== $current_tours && array_key_exists( $row['identifier'], $current_tours ) ) {
421
							$row['post_id'] = $current_tours[ $row['identifier'] ]->post_id;
422
						}
423
424
						//If we are searching for
425
						if ( false !== $post_status ) {
426
							if ( 'import' === $post_status ) {
427
428
								if ( 0 !== $row['post_id'] ) {
429
									continue;
430
								} else {
431
									$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...
432
								}
433
							} else {
434
								if ( 0 === $row['post_id'] ) {
435
									continue;
436
								} else {
437
									$current_status = get_post_status( $row['post_id'] );
438
439
									if ( $current_status !== $post_status ) {
440
										continue;
441
									}
442
								}
443
444
								$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...
445
							}
446
						} else {
447
							//Search through each keyword.
448
							foreach ( $keyphrases as $keyphrase ) {
449
450
								//Make sure the keyphrase is turned into an array
451
								$keywords = explode( ' ',$keyphrase );
452
								if ( ! is_array( $keywords ) ) {
453
									$keywords = array( $keywords );
454
								}
455
456
								if ( $this->multineedle_stripos( ltrim( rtrim( $row['name'] ) ), $keywords ) !== false ) {
457
									$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...
458
								}
459
							}
460
						}
461
					}
462
				}
463
464
				if ( false !== $searched_items ) {
465
					ksort( $searched_items );
466
					$return = implode( $searched_items );
467
				}
468
			}
469
			print_r( $return );
470
			die();
471
		}
472
	}
473
474
	/**
475
	 * Formats the row for output on the screen.
476
	 */
477 View Code Duplication
	public function format_row( $row = false ) {
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...
478
		if ( false !== $row ) {
479
			$status = 'import';
480
481
			if ( 0 !== $row['post_id'] ) {
482
				$status = '<a href="' . admin_url( '/post.php?post=' . $row['post_id'] . '&action=edit' ) . '" target="_blank">' . get_post_status( $row['post_id'] ) . '</a>';
483
			}
484
485
			$row_html = '
486
			<tr class="post-' . $row['post_id'] . ' type-tour" id="post-' . $row['post_id'] . '">
487
				<th class="check-column" scope="row">
488
					<label for="cb-select-' . $row['identifier'] . '" class="screen-reader-text">' . $row['name'] . '</label>
489
					<input type="checkbox" data-identifier="' . $row['identifier'] . '" value="' . $row['post_id'] . '" name="post[]" id="cb-select-' . $row['identifier'] . '">
490
				</th>
491
				<td class="post-title page-title column-title">
492
					<strong>' . $row['name'] . '</strong> - ' . $status . '
493
				</td>
494
				<td class="date column-date">
495
					<abbr title="' . date( 'Y/m/d',strtotime( $row['last_modified'] ) ) . '">' . date( 'Y/m/d',strtotime( $row['last_modified'] ) ) . '</abbr><br>Last Modified
496
				</td>
497
				<td class="ssid column-ssid">
498
					' . $row['identifier'] . '
499
				</td>
500
			</tr>';
501
			return $row_html;
502
		}
503
	}
504
505
	/**
506
	 * Connect to wetu
507
	 */
508
	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...
509
		$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...
510
511
		// @codingStandardsIgnoreLine
512
		if ( isset( $_POST['action'] ) && $_POST['action'] === 'lsx_import_items' && isset( $_POST['type'] ) && $_POST['type'] === $this->tab_slug && isset( $_POST['wetu_id'] ) ) {
513
514
			// @codingStandardsIgnoreLine
515
			$wetu_id = $_POST['wetu_id'];
516
517
			// @codingStandardsIgnoreLine
518
			if ( isset( $_POST['post_id'] ) ) {
519
				// @codingStandardsIgnoreLine
520
				$post_id = $_POST['post_id'];
521
			} else {
522
				$post_id = 0;
523
			}
524
525
			delete_option( 'wetu_importer_tour_settings' );
526
527
			// @codingStandardsIgnoreLine
528 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...
529
				// @codingStandardsIgnoreLine
530
				$content = $_POST['content'];
531
				add_option( 'wetu_importer_tour_settings',$content );
532
			} else {
533
				$content = false;
534
			}
535
536
			$jdata = file_get_contents( 'http://wetu.com/API/Itinerary/V7/Get?id=' . $wetu_id );
537
538 View Code Duplication
			if ( $jdata ) {
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
				$jdata = json_decode( $jdata,true );
540
				if ( ! empty( $jdata ) && ! isset( $jdata['error'] ) ) {
541
					$return = $this->import_row( $jdata,$wetu_id,$post_id,$content );
542
					$this->format_completed_row( $return );
543
					$this->save_queue();
544
					$this->cleanup_posts();
545
					$this->attach_destination_images( $content );
546
					$this->clean_attached_destinations( $return );
547
				} else {
548
					if ( isset( $adata['error'] ) ) {
0 ignored issues
show
Bug introduced by
The variable $adata seems to never exist, and therefore isset should always return false. 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...
549
						$this->format_error( $adata['error'] );
550
					} else {
551
						$this->format_error( esc_html__( 'There was a problem importing your tour, please try refreshing the page.','wetu-importer' ) );
552
					}
553
				}
554
			}
555
		}
556
	}
557
558
	/**
559
	 * Amends the tours destinations instead of replace.
560
	 *
561
	 * @param $id string
562
	 * @return void
563
	 */
564
	public function clean_attached_destinations( $id ) {
565
		$current_connections = get_post_meta( $id, 'destination_to_tour', false );
566
		delete_post_meta( $id,'destination_to_tour' );
567
		$current_connections = array_unique( $current_connections );
568
569
		foreach ( $current_connections as $connection ) {
570
			add_post_meta( $id, 'destination_to_tour',$connection, false );
571
		}
572
	}
573
574
	/**
575
	 * Connect to wetu
576
	 *
577
	 * @param $data array
578
	 * @param $wetu_id string
579
	 */
580
	public function import_row( $data, $wetu_id, $id = 0, $importable_content = false, $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...
581
		$post_name = '';
582
		$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...
583
		$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...
584
585
		$post = array(
586
		  'post_type'		=> 'tour',
587
		);
588
589
		//Set the post_content
590
		$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...
591
592
		if ( false !== $importable_content && in_array( 'description',$importable_content ) ) {
593
			$data_post_content = '';
594
595
			if ( isset( $data['description'] ) ) {
596
				$data_post_content = $data['description'];
597
			} elseif ( isset( $data['summary'] ) ) {
598
				$data_post_content = $data['summary'];
599
			}
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
		} else {
612
			//Set the name
613
			if ( isset( $data['name'] ) ) {
614
				$post_name = wp_unique_post_slug( sanitize_title( $data['name'] ),$id, 'draft', 'tour', 0 );
615
			}
616
617
			$post['post_name'] = $post_name;
618
			$post['post_title'] = $data['name'];
619
			$post['post_status'] = 'publish';
620
			$id = wp_insert_post( $post );
621
622
			//Save the WETU ID and the Last date it was modified.
623
			if ( false !== $id ) {
624
				add_post_meta( $id,'lsx_wetu_id',$wetu_id );
625
				add_post_meta( $id,'lsx_wetu_modified_date',strtotime( $data['last_modified'] ) );
626
			}
627
		}
628
629
		//Set the price
630
		if ( false !== $importable_content && in_array( 'price',$importable_content ) ) {
631
			$this->set_price( $data,$id );
632
		}
633
634
		//Set the Duration
635
		if ( false !== $importable_content && in_array( 'duration',$importable_content ) ) {
636
			$this->set_duration( $data,$id );
637
		}
638
639
		if ( false !== $importable_content && in_array( 'itineraries',$importable_content ) && isset( $data['legs'] ) && ! empty( $data['legs'] ) ) {
640
			$this->process_itineraries( $data,$id,$importable_content );
641
		}
642
643
		if ( in_array( 'map',$importable_content ) && isset( $data['routes'] ) && ! empty( $data['routes'] ) ) {
644
			$this->set_map_data( $data,$id );
645
		}
646
647
		return $id;
648
	}
649
650
	/**
651
	 * A loop which runs through each leg on the tour.
652
	 */
653
	public function process_itineraries( $data, $id, $importable_content ) {
654
		$day_counter = 1;
655
		$leg_counter = 0;
656
657
		delete_post_meta( $id,'itinerary' );
658
659
		if ( false !== $importable_content && in_array( 'accommodation',$importable_content ) ) {
660
			delete_post_meta( $id,'accommodation_to_tour' );
661
		}
662
		if ( false !== $importable_content && in_array( 'destination',$importable_content ) ) {
663
			//delete_post_meta($id,'destination_to_tour');
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
664
			delete_post_meta( $id,'departs_from' );
665
			delete_post_meta( $id,'ends_in' );
666
		}
667
668
		$departs_from = false;
669
		$ends_in = false;
670
671
		foreach ( $data['legs'] as $leg ) {
672
			//Itinerary Accommodation
673
			$current_accommodation = false;
674
			if ( false !== $importable_content && in_array( 'accommodation',$importable_content ) ) {
675
				$current_accommodation = $this->set_accommodation( $leg,$id );
676
			}
677
678
			//Itinerary Destination
679
			$current_destination = false;
680
			if ( false !== $importable_content && in_array( 'destination',$importable_content ) ) {
681
				$current_destination = $this->set_destination( $leg,$id,$leg_counter );
682
			}
683
684
			//If the Nights are the same mount of days in the array,  then it isnt "By Destination"
685
			if ( ((1 <= (int) $leg['nights'] && isset( $leg['days'] ))) || 0 === $leg['itinerary_leg_id'] ) {
686
				foreach ( $leg['days'] as $day ) {
687
					$current_day = array();
688
					$current_day['title'] = esc_attr( 'Day ', 'wetu-importer' ) . $day_counter;
689
690
					//print_r('<pre>');print_r($day['notes']);print_r('</pre>');
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
691
692
					//Description
693 View Code Duplication
					if ( false !== $importable_content && in_array( 'itinerary_description',$importable_content ) && isset( $day['notes'] ) ) {
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...
694
						$current_day['description'] = $day['notes'];
695
					} else {
696
						$current_day['description'] = '';
697
					}
698
699
					//Itinerary Gallery
700 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...
701
						$current_day['featured_image'] = '';
702
					} else {
703
						$current_day['featured_image'] = '';
704
					}
705
706
					//Accommodation
707 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...
708
						$current_day['accommodation_to_tour'] = array( $current_accommodation );
709
					} else {
710
						$current_day['accommodation_to_tour'] = array();
711
					}
712
713
					//Destination
714 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...
715
						$current_day['destination_to_tour'] = array( $current_destination );
716
					} else {
717
						$current_day['destination_to_tour'] = array();
718
					}
719
720
					//Included
721 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...
722
						$current_day['included'] = $day['included'];
723
					} else {
724
						$current_day['included'] = '';
725
					}
726
727
					//Excluded
728 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...
729
						$current_day['excluded'] = $day['excluded'];
730
					} else {
731
						$current_day['excluded'] = '';
732
					}
733
734
					$this->set_itinerary_day( $current_day,$id );
735
					$day_counter++;
736
				}
737
			} else {
738
				// This is for the by destination
739
740
				$current_day = array();
741
				$next_day_count = $day_counter + (int) $leg['nights'];
742
				$day_count_label = $next_day_count -1;
743
744
				$current_day['title'] = esc_attr( 'Day ','wetu-importer' ) . $day_counter;
745
746
				if ( 0 !== (int) $leg['nights'] ) {
747
					$current_day['title'] .= ' - ' . $day_count_label;
748
				}
749
750
				//Description
751 View Code Duplication
				if ( false !== $importable_content && in_array( 'itinerary_description',$importable_content ) && isset( $leg['notes'] ) ) {
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['description'] = $leg['notes'];
753
				} else {
754
					$current_day['description'] = '';
755
				}
756
757
				//Itinerary Gallery
758 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...
759
					$current_day['featured_image'] = '';
760
				} else {
761
					$current_day['featured_image'] = '';
762
				}
763
764
				//Accommodation
765 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...
766
					$current_day['accommodation_to_tour'] = array( $current_accommodation );
767
				} else {
768
					$current_day['accommodation_to_tour'] = array();
769
				}
770
771
				//Destination
772 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...
773
					$current_day['destination_to_tour'] = array( $current_destination );
774
				} else {
775
					$current_day['destination_to_tour'] = array();
776
				}
777
778
				//Included
779
				if ( false !== $importable_content && in_array( 'itinerary_included',$importable_content ) && isset( $leg['included'] ) && '' !== $leg['included'] ) {
780
					$current_day['included'] = $leg['included'];
781
				} else {
782
					$current_day['included'] = '';
783
				}
784
785
				//Excluded
786
				if ( false !== $importable_content && in_array( 'itinerary_excluded',$importable_content ) && isset( $leg['excluded'] ) && '' !== $leg['excluded'] ) {
787
					$current_day['excluded'] = $leg['excluded'];
788
				} else {
789
					$current_day['excluded'] = '';
790
				}
791
792
				$this->set_itinerary_day( $current_day,$id );
793
				$day_counter = $next_day_count;
794
			}
795
796
			//If we are in the first leg,  and the destination was attached then save it as the departure field.
797
			if ( 0 === $leg_counter && false !== $current_destination ) {
798
				$departs_from = $current_destination;
799
			}
800
801
			//If its the last leg then save it as the ends in.
802
			// @codingStandardsIgnoreLine
803
			if ( $leg_counter === (count( $data['legs'] ) -2) && false !== $current_destination ) {
804
				$ends_in = $current_destination;
805
			}
806
807
			$leg_counter++;
808
		}
809
810
		if ( false !== $departs_from ) {
811
			add_post_meta( $id,'departs_from',$departs_from,true );
812
		}
813
		if ( false !== $ends_in ) {
814
			add_post_meta( $id,'ends_in',$ends_in,true );
815
		}
816
	}
817
818
	/**
819
	 * Run through your routes and save the points as a KML file.
820
	 */
821
	public function set_map_data( $data, $id, $zoom = 9 ) {
822
		if ( ! empty( $data['routes'] ) ) {
823
			delete_post_meta( $id,'wetu_map_points' );
824
825
			$points = array();
826
827
			foreach ( $data['routes'] as $route ) {
828
829
				if ( isset( $route['points'] ) && '' !== $route['points'] ) {
830
831
					$temp_points = explode( ';',$route['points'] );
832
					$point_counter = count( $temp_points );
833
834
					for ( $x = 0; $x <= $point_counter; $x++ ) {
835
						$y = $x + 1;
836
						$points[] = $temp_points[ $x ] . ',' . $temp_points[ $y ];
837
						$x++;
838
					}
839
				}
840
			}
841
842
			if ( ! empty( $points ) ) {
843
				$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...
844
			}
845
		}
846
847
	}
848
849
	// CLASS SPECIFIC FUNCTIONS
850
851
	/**
852
	 * Set the Itinerary Day
853
	 */
854
	public function set_itinerary_day( $day, $id ) {
855
		$this->save_custom_field( $day,'itinerary',$id,false,false );
856
	}
857
858
	/**
859
	 * Set the price
860
	 */
861
	public function set_price( $data, $id ) {
862
		//Price
863 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...
864
			$price = preg_replace( '/[^0-9,.]/', '', $data['price'] );
865
			$this->save_custom_field( $price,'price',$id );
866
		}
867
868
		//Price includes
869
		if ( isset( $data['price_includes'] ) && '' !== $data['price_includes'] ) {
870
			$this->save_custom_field( $data['price_includes'],'included',$id );
871
		}
872
873
		//Price Excludes
874
		if ( isset( $data['price_excludes'] ) && '' !== $data['price_excludes'] ) {
875
			$this->save_custom_field( $data['price_excludes'],'not_included',$id );
876
		}
877
	}
878
879
	/**
880
	 * Set the duration
881
	 */
882
	public function set_duration( $data, $id ) {
883 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...
884
			$price = $data['days'];
885
			$price = preg_replace( '/[^0-9,.]/', '', $price );
886
			$this->save_custom_field( $price,'duration',$id );
887
		}
888
	}
889
890
	/**
891
	 * Connects the Accommodation if its available
892
	 */
893
	public function set_accommodation( $day, $id ) {
894
		$ac_id = false;
895
		$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...
896
897
		if ( isset( $day['content_entity_id'] ) && ! empty( $day['content_entity_id'] ) ) {
898
			if ( false !== $this->current_accommodation && ! empty( $this->current_accommodation ) && array_key_exists( $day['content_entity_id'],$this->current_accommodation ) ) {
899
				$ac_id = $this->current_accommodation[ $day['content_entity_id'] ];
900
			} else {
901
				$ac_id = wp_insert_post(array(
902
					'post_type' => 'accommodation',
903
					'post_status' => 'draft',
904
					'post_title' => $day['content_entity_id'],
905
				));
906
907
				$this->save_custom_field( $day['content_entity_id'],'lsx_wetu_id',$ac_id );
908
			}
909
910
			if ( '' !== $ac_id && false !== $ac_id ) {
911
				$this->save_custom_field( $ac_id,'accommodation_to_tour',$id,false,false );
912
				$this->save_custom_field( $id,'tour_to_accommodation',$ac_id,false,false );
913
				$this->queue_item( $ac_id );
914
			}
915
		}
916
		return $ac_id;
917
	}
918
919
	/**
920
	 * Grab all the current accommodation posts via the lsx_wetu_id field.
921
	 *
922
	 * @param $post_type string
923
	 * @return boolean / array
924
	 */
925
	public function find_current_accommodation( $post_type = 'accommodation' ) {
926
		global $wpdb;
927
		$accommodation = parent::find_current_accommodation( $post_type );
928
		$return = false;
929
930
		if ( ! empty( $accommodation ) ) {
931
			foreach ( $accommodation as $key => $acc ) {
932
				$return[ $acc->meta_value ] = $acc->post_id;
933
			}
934
		}
935
936
		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...
937
	}
938
939
	/**
940
	 * Grab all the current accommodation posts via the lsx_wetu_id field.
941
	 * @return boolean / array
942
	 */
943
	public function find_current_destinations() {
944
		return $this->find_current_accommodation( 'destination' );
945
	}
946
947
	/**
948
	 * Connects the destinations post type
949
	 *
950
	 * @param $day array
951
	 * @param $id string
952
	 * @return boolean / string
953
	 */
954
	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...
955
		$dest_id = false;
956
		$country_id = false;
957
		$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...
958
959
		if ( isset( $day['destination_content_entity_id'] ) && ! empty( $day['destination_content_entity_id'] ) ) {
960
			if ( false !== $this->current_destinations && ! empty( $this->current_destinations ) && array_key_exists( $day['destination_content_entity_id'],$this->current_destinations ) ) {
961
				$dest_id = $this->current_destinations[ $day['destination_content_entity_id'] ];
962
963
				//TODO Check for attachments here.
964
				$this->destination_images[ $id ][] = array( $dest_id, $day['destination_content_entity_id'] );
965
966
				//Check if there is a country asigned.
967
				$potential_id = wp_get_post_parent_id( $dest_id );
968
				$country_wetu_id = get_post_meta( $potential_id,'lsx_wetu_id',true );
969
970
				if ( false !== $country_wetu_id ) {
971
					$country_id = $this->set_country( $country_wetu_id, $id );
972
				}
973
			} else {
974
				$destination_json = file_get_contents( 'http://wetu.com/API/Pins/' . $this->api_key . '/Get?ids=' . $day['destination_content_entity_id'] );
975
976
				if ( $destination_json ) {
977
					$destination_data = json_decode( $destination_json, true );
978
979
					if ( ! empty( $destination_data ) && ! isset( $destination_data['error'] ) ) {
980
						$destination_title = $day['destination_content_entity_id'];
981
982
						if ( isset( $destination_data[0]['name'] ) ) {
983
							$destination_title = $destination_data[0]['name'];
984
						}
985
986
						if ( isset( $destination_data[0]['map_object_id'] ) && isset( $destination_data[0]['position']['country_content_entity_id'] )
987
							&& $destination_data[0]['map_object_id'] !== $destination_data[0]['position']['country_content_entity_id'] ) {
988
989
							$country_id = $this->set_country( $destination_data[0]['position']['country_content_entity_id'], $id );
990
							// Save the destination so we can grab the tour featured image and banner from them
991
						}
992
993
						$dest_post = array(
994
							'post_type' => 'destination',
995
							'post_status' => 'draft',
996
							'post_title' => $destination_title,
997
						);
998
999
						if ( false !== $country_id ) {
1000
							$dest_post['post_parent'] = $country_id;
1001
						}
1002
						$dest_id = wp_insert_post( $dest_post );
1003
1004
						//Make sure we register the
1005
						$this->current_destinations[ $day['destination_content_entity_id'] ] = $dest_id;
1006
1007
						//If there are images attached then use the destination
1008 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...
1009
							$this->destination_images[ $id ][] = array( $dest_id, $day['destination_content_entity_id'] );
1010
						}
1011
1012
						$this->save_custom_field( $day['destination_content_entity_id'], 'lsx_wetu_id', $dest_id );
1013
					}
1014
				}
1015
			}
1016
1017
			if ( '' !== $dest_id && false !== $dest_id ) {
1018
				$this->save_custom_field( $dest_id, 'destination_to_tour', $id, false, false );
1019
				$this->save_custom_field( $id, 'tour_to_destination', $dest_id, false, false );
1020
1021
				//Save the item to display in the queue
1022
				$this->queue_item( $dest_id );
1023
1024
				//Save the item to clean up the amount of connections.
1025
				$this->cleanup_posts[ $dest_id ] = 'tour_to_destination';
1026
1027
				//Add this relation info so we can make sure certain items are set as countries.
1028
				if ( 0 !== $country_id && false !== $country_id ) {
1029
					$this->relation_meta[ $dest_id ] = $country_id;
1030
					$this->relation_meta[ $country_id ] = 0;
1031
				} else {
1032
					$this->relation_meta[ $dest_id ] = 0;
1033
				}
1034
			}
1035
		}
1036
		return $dest_id;
1037
	}
1038
1039
	/**
1040
	 * Connects the destinations post type
1041
	 *
1042
	 * @param $dest_id string
1043
	 * @param $country_id array
1044
	 * @param $id string
1045
	 *
1046
	 * @return string
1047
	 */
1048
	public function set_country( $country_wetu_id, $id ) {
1049
		$country_id = false;
1050
		$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...
1051
1052
		if ( false !== $this->current_destinations && ! empty( $this->current_destinations ) && array_key_exists( $country_wetu_id, $this->current_destinations ) ) {
1053
			$country_id = $this->current_destinations[ $country_wetu_id ];
1054
			$this->destination_images[ $id ][] = array( $country_id, $country_wetu_id );
1055
		} else {
1056
			$country_json = file_get_contents( 'http://wetu.com/API/Pins/' . $this->api_key . '/Get?ids=' . $country_wetu_id );
1057
1058
			if ( $country_json ) {
1059
				$country_data = json_decode( $country_json, true );
1060
1061
				if ( ! empty( $country_data ) && ! isset( $country_data['error'] ) ) {
1062
1063
					//Format the title of the destination if its available,  otherwise default to the WETU ID.
1064
					$country_title = $country_wetu_id;
1065
1066
					if ( isset( $country_data[0]['name'] ) ) {
1067
						$country_title = $country_data[0]['name'];
1068
					}
1069
1070
					$country_id = wp_insert_post(array(
1071
						'post_type' => 'destination',
1072
						'post_status' => 'draft',
1073
						'post_title' => $country_title,
1074
					));
1075
1076
					//add the country to the current destination stack
1077
					$this->current_destinations[ $country_wetu_id ] = $country_id;
1078
1079
					// Check if there are images and save fore use later.
1080 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...
1081
						$this->destination_images[ $id ][] = array( $country_id,$country_wetu_id );
1082
					}
1083
1084
					//Save the wetu field
1085
					$this->save_custom_field( $country_wetu_id, 'lsx_wetu_id', $country_id );
1086
				}
1087
			}
1088
		}
1089
1090
		if ( '' !== $country_id && false !== $country_id ) {
1091
			$this->save_custom_field( $country_id, 'destination_to_tour', $id, false, false );
1092
			$this->save_custom_field( $id, 'tour_to_destination', $country_id, false, false );
1093
			$this->queue_item( $country_id );
1094
			$this->cleanup_posts[ $country_id ] = 'tour_to_destination';
1095
1096
			return $country_id;
1097
		}
1098
	}
1099
1100
	/**
1101
	 * Connects the destinations post type
1102
	 *
1103
	 * @param $dest_id string
1104
	 * @param $country_id array
1105
	 * @param $id string
1106
	 *
1107
	 * @return string
1108
	 */
1109
	public function attach_destination_images( $importable_content = array() ) {
1110
		if ( false !== $this->destination_images ) {
1111
			$this->shuffle_assoc( $this->destination_images );
1112
1113
			foreach ( $this->destination_images as $tour => $destinations ) {
1114
				$this->shuffle_assoc( $destinations );
1115
1116
				/*print_r('<pre>');
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1117
				print_r( $tour );
1118
				print_r( $destinations );
1119
				print_r('</pre>');*/
1120
1121
				$image_set = false;
1122
1123
				foreach ( $destinations as $destination ) {
1124
					if ( false === $image_set ) {
1125
						$url = 'https://wetu.com/API/Pins/' . $this->api_key;
1126
						$url_qs = '';
1127
1128
						$jdata = file_get_contents( $url . '/Get?' . $url_qs . '&ids=' . $destination[1] );
1129
1130
						if ( $jdata ) {
1131
							$adata = json_decode( $jdata, true );
1132
1133
							if ( ! empty( $adata ) && ! empty( $adata[0]['content']['images'] ) ) {
1134
								$this->find_attachments( $destination[0] );
1135
1136
								//Set the featured image
1137
								if ( false !== $importable_content && in_array( 'featured_image', $importable_content ) ) {
1138
									$image_set = $this->set_featured_image( $adata, $tour );
1139
								}
1140
								if ( false !== $importable_content && in_array( 'banner_image', $importable_content ) ) {
1141
									$image_set = $this->set_banner_image( $adata, $tour );
1142
								}
1143
							}
1144
						}
1145
					} else {
1146
						continue;
1147
					}
1148
				}
1149
			}
1150
		}
1151
	}
1152
1153
	/**
1154
	 * Creates the main gallery data
1155
	 */
1156
	public function set_featured_image( $data, $id ) {
1157
		$image_set = false;
1158
		$counter = 0;
1159
1160
		if ( is_array( $data[0]['content']['images'] ) && ! empty( $data[0]['content']['images'] ) ) {
1161
			foreach ( $data[0]['content']['images'] as $v ) {
1162
				/*print_r('<pre>');
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1163
				print_r( $v );
1164
				print_r('</pre>');*/
1165
1166
				if ( true === $image_set || 0 === $counter ) {
1167
					$counter++;
1168
					continue;
1169
				}
1170
1171
				if ( ! $this->check_if_image_is_used( $v ) ) {
1172
					$temp_featured_image = $this->attach_image( $v , $id );
1173
1174
					if ( false !== $temp_featured_image ) {
1175
						$this->featured_image = $temp_featured_image;
1176
						delete_post_meta( $id, '_thumbnail_id' );
1177
						add_post_meta( $id, '_thumbnail_id', $this->featured_image, true );
1178
						$image_set = true;
1179
					}
1180
				}
1181
1182
				$counter++;
1183
			}
1184
		}
1185
		return $image_set;
1186
	}
1187
1188
	/**
1189
	 * Sets a banner image
1190
	 */
1191
	public function set_banner_image( $data, $id ) {
1192
		$image_set = false;
1193
1194
		if ( is_array( $data[0]['content']['images'] ) && ! empty( $data[0]['content']['images'] ) ) {
1195
			$temp_banner = $this->attach_image( $data[0]['content']['images'][1], $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...
1196
				'width' => '1920',
1197
				'height' => '600',
1198
				'cropping' => 'c',
1199
			) );
1200
1201 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...
1202
				$this->banner_image = $temp_banner;
1203
1204
				delete_post_meta( $id,'image_group' );
1205
1206
				$new_banner = array(
1207
					'banner_image' => array(
1208
						'cmb-field-0' => $this->banner_image,
1209
					),
1210
				);
1211
1212
				add_post_meta( $id,'image_group',$new_banner,true );
1213
				$image_set = true;
1214
			}
1215
		}
1216
1217
		return $image_set;
1218
	}
1219
1220
	/**
1221
	 * Grabs all of the current used featured images on the site.
1222
	 */
1223
	public function check_if_image_is_used( $v ) {
1224
		global $wpdb;
1225
1226
		$temp_fragment = explode( '/',$v['url_fragment'] );
1227
		$url_filename = $temp_fragment[ count( $temp_fragment ) -1 ];
1228
		$url_filename = str_replace( array( '.jpg', '.png', '.jpeg' ),'',$url_filename );
1229
		$url_filename = trim( $url_filename );
1230
		$url_filename = str_replace( ' ','_',$url_filename );
1231
1232
		if ( in_array( $url_filename,$this->found_attachments ) ) {
1233
			//check to see if there is a featured image set with this ID.
1234
			$found_id = array_search( $url_filename,$this->found_attachments );
1235
1236
			$querystring = "
1237
				SELECT      post_id
1238
				FROM        {$wpdb->postmeta}
1239
				WHERE       meta_value = '{$found_id}'
1240
				AND 		meta_key = '_thumbnail_id'
1241
			";
1242
			// @codingStandardsIgnoreLine
1243
			$results = $wpdb->get_results( $querystring );
1244
1245
			if ( ! empty( $results ) ) {
1246
				return true;
1247
			} else {
1248
				return false;
1249
			}
1250
		} else {
1251
			$querystring = "
1252
				SELECT      ID
1253
				FROM        {$wpdb->posts}
1254
				WHERE       post_name = '{$url_filename}'
1255
			";
1256
			// @codingStandardsIgnoreLine
1257
			$results = $wpdb->get_results( $querystring );
1258
			if ( ! empty( $results ) ) {
1259
				$querystring = "
1260
					SELECT      post_id
1261
					FROM        {$wpdb->postmeta}
1262
					WHERE       meta_value = '{$results[0]->ID}'
1263
					AND 		meta_key = '_thumbnail_id'
1264
				";
1265
				// @codingStandardsIgnoreLine
1266
				$results = $wpdb->get_results( $querystring );
1267
				if ( ! empty( $results ) ) {
1268
					return true;
1269
				} else {
1270
					return false;
1271
				}
1272
			} else {
1273
				return false;
1274
			}
1275
		}
1276
	}
1277
1278
	/**
1279
	 * Que an item to be saved.
1280
	 *
1281
	 * @param   $id     int
1282
	 */
1283
	public function queue_item( $id ) {
1284
		if ( is_array( $this->import_queue ) && ! in_array( $id,$this->import_queue ) ) {
1285
			$this->import_queue[] = $id;
1286
		} else {
1287
			$this->import_queue[] = $id;
1288
		}
1289
	}
1290
1291
	/**
1292
	 * Saves the queue to the option.
1293
	 */
1294
	public function save_queue() {
1295
		if ( ! empty( $this->import_queue ) ) {
1296
			if ( ! empty( $this->queued_imports ) ) {
1297
				$saved_imports = array_merge( $this->queued_imports,$this->import_queue );
1298
			} else {
1299
				$saved_imports = $this->import_queue;
1300
			}
1301
1302
			delete_option( 'wetu_importer_que' );
1303
1304
			if ( ! empty( $saved_imports ) ) {
1305
				$saved_imports = array_unique( $saved_imports );
1306
				update_option( 'wetu_importer_que',$saved_imports );
1307
			}
1308
		}
1309
	}
1310
1311
}
1312