Completed
Push — master ( 2939bf...29e8a9 )
by Warwick
02:39
created

WETU_Importer_Tours::__construct()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 13
nc 4
nop 0
1
<?php
2
/**
3
 * @package   WETU_Importer_Tours
4
 * @author    LightSpeed
5
 * @license   GPL-3+
6
 * @link      
7
 * @copyright 2017 LightSpeed
8
 **/
9
10
class WETU_Importer_Tours extends WETU_Importer_Accommodation {
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
	 * Options
59
	 *
60
	 * @since 0.0.1
61
	 *
62
	 * @var      string
63
	 */
64
	public $options = false;
65
66
	/**
67
	 * The fields you wish to import
68
	 *
69
	 * @since 0.0.1
70
	 *
71
	 * @var      string
72
	 */
73
	public $tour_options = false;
74
75
	/**
76
	 * Initialize the plugin by setting localization, filters, and administration functions.
77
	 *
78
	 * @since 1.0.0
79
	 *
80
	 * @access private
81
	 */
82 View Code Duplication
	public function __construct() {
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...
83
		$this->set_variables();
84
85
		add_action( 'lsx_tour_importer_admin_tab_'.$this->tab_slug, array($this,'display_page') );
86
		add_action('wp_ajax_lsx_tour_importer',array($this,'process_ajax_search'));	
87
		add_action('wp_ajax_nopriv_lsx_tour_importer',array($this,'process_ajax_search'));		
88
89
		add_action('wp_ajax_lsx_import_items',array($this,'process_ajax_import'));	
90
		add_action('wp_ajax_nopriv_lsx_import_items',array($this,'process_ajax_import'));
91
92
		$temp_options = get_option('_lsx-to_settings',false);
93
		if(false !== $temp_options && isset($temp_options[$this->plugin_slug]) && !empty($temp_options[$this->plugin_slug])){
94
			$this->options = $temp_options[$this->plugin_slug];
95
		}
96
97
		$tour_options = get_option('wetu_importer_tour_settings',false);
98
		if(false !== $tour_options){
99
			$this->tour_options = $tour_options;
100
        }
101
	}
102
103
	/**
104
	 * Sets the variables used throughout the plugin.
105
	 */
106
	public function set_variables()
107
	{
108
		parent::set_variables();
109
110
		if ( false !== $this->api_username && false !== $this->api_password ) {
111
			$this->url    = 'https://wetu.com/API/Itinerary/';
112
			$this->url_qs = 'username=' . $this->api_username . '&password=' . $this->api_password;
113
		} elseif ( false !== $this->api_key ) {
114
			$this->url    = 'https://wetu.com/API/Itinerary/' . $this->api_key;
115
			$this->url_qs = '';
116
		}
117
	}
118
119
	/**
120
	 * Display the importer administration screen
121
	 */
122
	public function display_page() {
123
        ?>
124
        <div class="wrap">
125
            <?php screen_icon(); ?>
126
127
            <?php $this->update_options_form(); ?>
128
129
            <?php $this->search_form(); ?>
130
131
			<form method="get" action="" id="posts-filter">
132
				<input type="hidden" name="post_type" class="post_type" value="<?php echo $this->tab_slug; ?>" />
133
				
134
				<p><input class="button button-primary add" type="button" value="<?php _e('Add to List','wetu-importer'); ?>" />
135
					<input class="button button-primary clear" type="button" value="<?php _e('Clear','wetu-importer'); ?>" />
136
				</p>				
137
138
				<table class="wp-list-table widefat fixed posts">
139
					<?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...
140
				
141
					<tbody id="the-list">
142
						<tr class="post-0 type-tour status-none" id="post-0">
143
							<th class="check-column" scope="row">
144
								<label for="cb-select-0" class="screen-reader-text"><?php _e('Enter a title to search for and press enter','wetu-importer'); ?></label>
145
							</th>
146
							<td class="post-title page-title column-title">
147
								<strong>
148
									<?php _e('Enter a title to search for','wetu-importer'); ?>
149
								</strong>
150
							</td>
151
							<td class="date column-date">							
152
							</td>
153
							<td class="ssid column-ssid">
154
							</td>
155
						</tr>									
156
					</tbody>
157
158
					<?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...
159
160
				</table>
161
162
				<p><input class="button button-primary add" type="button" value="<?php _e('Add to List','wetu-importer'); ?>" />
163
					<input class="button button-primary clear" type="button" value="<?php _e('Clear','wetu-importer'); ?>" />
164
				</p>
165
			</form> 
166
167
			<div style="display:none;" class="import-list-wrapper">
168
				<br />        
169
				<form method="get" action="" id="import-list">
170
171
					<div class="row">
172
						<div style="width:30%;display:block;float:left;">
173
							<h3><?php _e('What content to Sync from WETU'); ?></h3>
174
							<ul>
175
								<li><input class="content" checked="<?php $this->checked($this->tour_options,'description'); ?>" type="checkbox" name="content[]" value="description" /> <?php _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...
176
								<li><input class="content" checked="<?php $this->checked($this->tour_options,'excerpt'); ?>" type="checkbox" name="content[]" value="excerpt" /> <?php _e('Excerpt','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...
177
178
                                <li><input class="content" checked="<?php $this->checked($this->tour_options,'price'); ?>" type="checkbox" name="content[]" value="price" /> <?php _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...
179
                                <li><input class="content" checked="<?php $this->checked($this->tour_options,'duration'); ?>" type="checkbox" name="content[]" value="duration" /> <?php _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...
180
181
								<li><input class="content" checked="<?php $this->checked($this->tour_options,'category'); ?>" type="checkbox" name="content[]" value="category" /> <?php _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...
182
183
                                <li><input class="content" checked="<?php $this->checked($this->tour_options,'itineraries'); ?>" type="checkbox" name="content[]" value="itineraries" /> <?php _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...
184
185
								<?php if(class_exists('TO_Maps')){ ?>
186
                                    <li><input class="content" checked="<?php $this->checked($this->tour_options,'map'); ?>" type="checkbox" name="content[]" value="map" /> <?php _e('Map Coordinates (generates a KML file)','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...
187
								<?php } ?>
188
							</ul>
189
						</div>
190
                        <div style="width:30%;display:block;float:left;">
191
                            <h3><?php _e('Itinerary Info'); ?></h3>
192
                            <ul>
193
                                <li><input class="content" checked="<?php $this->checked($this->tour_options,'itinerary_description'); ?>" type="checkbox" name="content[]" value="itinerary_description" /> <?php _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...
194
                                <li><input class="content" checked="<?php $this->checked($this->tour_options,'itinerary_included'); ?>" type="checkbox" name="content[]" value="itinerary_included" /> <?php _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...
195
                                <li><input class="content" checked="<?php $this->checked($this->tour_options,'itinerary_excluded'); ?>" type="checkbox" name="content[]" value="itinerary_excluded" /> <?php _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...
196
                            </ul>
197
198
                            <h4><?php _e('Additional Content'); ?></h4>
199
                            <ul>
200
                                <li><input class="content" checked="<?php $this->checked($this->tour_options,'accommodation'); ?>" type="checkbox" name="content[]" value="accommodation" /> <?php _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...
201
                                <li><input class="content" checked="<?php $this->checked($this->tour_options,'destination'); ?>" type="checkbox" name="content[]" value="destination" /> <?php _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...
202
                            </ul>
203
                        </div>
204
                        <?php if(class_exists('TO_Team')){ ?>
205
                            <div style="width:30%;display:block;float:left;">
206
                                <h3><?php _e('Assign a Team Member'); ?></h3>
207
                                <?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...
208
                            </div>
209
                        <?php } ?>
210
211
						<br clear="both" />			
212
					</div>
213
214
215
					<h3><?php _e('Your List'); ?></h3>
216
                    <p><input class="button button-primary" type="submit" value="<?php _e('Sync','wetu-importer'); ?>" /></p>
217
					<table class="wp-list-table widefat fixed posts">
218
						<?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...
219
220
						<tbody>
221
222
						</tbody>
223
224
						<?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...
225
226
					</table>
227
228
					<p><input class="button button-primary" type="submit" value="<?php _e('Sync','wetu-importer'); ?>" /></p>
229
				</form>
230
			</div>
231
232
			<div style="display:none;" class="completed-list-wrapper">
233
				<h3><?php _e('Completed','wetu-importer'); ?> - <small><?php _e('Import your','wetu-importer'); ?> <a href="<?php echo admin_url('admin.php'); ?>?page=<?php echo $this->plugin_slug; ?>&tab=accommodation"><?php _e('accommodation'); ?></a> <?php _e('next','wetu-importer'); ?></small></h3>
234
				<ul>
235
				</ul>
236
			</div>
237
        </div>
238
        <?php
239
	}
240
241
	/**
242
	 * search_form
243
	 */
244
	public function update_options_form() {
245
		$tours = get_transient('lsx_ti_tours');
246
		echo '<div class="wetu-status"><h3>'.__('Wetu Status','wetu-importer').' - ';
247
		if('' === $tours || false === $tours || isset($_GET['refresh_tours'])){
248
			$result = $this->update_options();
249
250
			if(true === $result){
251
			    echo '<span style="color:green;">'.esc_attr('Connected','wetu-importer').'</span>';
252
            }else{
253
			    echo '<span style="color:red;">'.wp_kses_post($result).'</span>';
254
            }
255
		}else{
256
			echo '<span style="color:green;">'.esc_attr('Connected','wetu-importer').'</span>';
257
        }
258
		echo '</h3></div>';
259
	}
260
261
	/**
262
	 * Save the list of Tours into an option
263
	 */
264
	public function update_options() {
265
		$data = file_get_contents( $this->url . '/V7/List?' . $this->url_qs . '&own=true&type=All' );
266
		$tours = json_decode($data, true);
267
268
		if(isset($tours['error'])){
269
		    return $tours['error'];
270 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...
271
			set_transient('lsx_ti_tours',$tours['itineraries'],60*60*2);
272
			return true;
273
		}
274
	}
275
276
	/**
277
	 * Grab all the current tour posts via the lsx_wetu_id field.
278
	 */
279 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...
280
		global $wpdb;
281
		$return = array();
282
283
		$current_tours = $wpdb->get_results("
284
					SELECT key1.post_id,key1.meta_value
285
					FROM {$wpdb->postmeta} key1
286
287
					INNER JOIN  {$wpdb->posts} key2 
288
    				ON key1.post_id = key2.ID
289
					
290
					WHERE key1.meta_key = 'lsx_wetu_id'
291
					AND key2.post_type = 'tour'
292
293
					LIMIT 0,500
294
		");
295
		if(null !== $current_tours && !empty($current_tours)){
296
			foreach($current_tours as $tour){
297
				$return[$tour->meta_value] = $tour;
298
			}
299
		}
300
		return $return;
301
	}	
302
303
	/**
304
	 * Run through the accommodation grabbed from the DB.
305
	 */
306
	public function process_ajax_search() {
307
		$return = false;
308
309
		if(isset($_POST['action']) && $_POST['action'] === 'lsx_tour_importer' && isset($_POST['type']) && $_POST['type'] === $this->tab_slug){
310
			$tours = get_transient('lsx_ti_tours');
311
			if ( false !== $tours) {
312
313
				$searched_items = false;
314
315 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...
316
					$keyphrases = $_POST['keyword'];
317
				}else{
318
					$keyphrases = array(0);
319
                }
320
321
				if(!is_array($keyphrases)){
322
					$keyphrases = array($keyphrases);
323
				}
324
				foreach($keyphrases as &$keyword){
325
					$keyword = ltrim(rtrim($keyword));
326
				}
327
328
				$post_status = false;
329
				if(in_array('publish',$keyphrases)){
330
					$post_status = 'publish';
331
				}
332
				if(in_array('pending',$keyphrases)){
333
					$post_status = 'pending';
334
				}
335
				if(in_array('draft',$keyphrases)){
336
					$post_status = 'draft';
337
				}
338
				if(in_array('import',$keyphrases)){
339
					$post_status = 'import';
340
				}
341
342
343
				if (!empty($tours)) {
344
					$current_tours = $this->find_current_tours();
345
346
					foreach($tours as $row_key => $row){
347
348
					    if(isset($row['is_disabled']) && true === $row['is_disabled']){
349
                            continue;
350
                        }
351
352
                        /*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...
353
                            continue;
354
                        }*/
355
356
                        //If this is a current tour, add its ID to the row.
357
						$row['post_id'] = 0;
358
						if(false !== $current_tours && array_key_exists($row['identifier'], $current_tours)){
359
							$row['post_id'] = $current_tours[$row['identifier']]->post_id;
360
						}
361
362
						//If we are searching for
363 View Code Duplication
						if(false !== $post_status){
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...
364
365
                            if('import' === $post_status){
366
367
								if(0 !== $row['post_id']){
368
								    continue;
369
								}else{
370
									$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...
371
                                }
372
373
374
                            }else{
375
376
								if(0 === $row['post_id']){
377
									continue;
378
								}else{
379
									$current_status = get_post_status($row['post_id']);
380
									if($current_status !== $post_status){
381
									    continue;
382
                                    }
383
384
								}
385
								$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...
386
387
                            }
388
389
                        }else{
390
							//Search through each keyword.
391
							foreach($keyphrases as $keyphrase){
392
393
								//Make sure the keyphrase is turned into an array
394
								$keywords = explode(" ",$keyphrase);
395
								if(!is_array($keywords)){
396
									$keywords = array($keywords);
397
								}
398
399
								if($this->multineedle_stripos(ltrim(rtrim($row['name'])), $keywords) !== false){
400
									$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...
401
								}
402
							}
403
                        }
404
					}		
405
				}
406
407
				if(false !== $searched_items){
408
					ksort($searched_items);
409
					$return = implode($searched_items);
410
				}
411
			}
412
			print_r($return);
413
			die();
414
		}
415
	}
416
417
	/**
418
	 * Formats the row for output on the screen.
419
	 */	
420 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...
421
		if(false !== $row){
422
423
			$status = 'import';
424
			if(0 !== $row['post_id']){
425
				$status = '<a href="'.admin_url('/post.php?post='.$row['post_id'].'&action=edit').'" target="_blank">'.get_post_status($row['post_id']).'</a>';
426
			}
427
428
			$row_html = '
429
			<tr class="post-'.$row['post_id'].' type-tour" id="post-'.$row['post_id'].'">
430
				<th class="check-column" scope="row">
431
					<label for="cb-select-'.$row['identifier'].'" class="screen-reader-text">'.$row['name'].'</label>
432
					<input type="checkbox" data-identifier="'.$row['identifier'].'" value="'.$row['post_id'].'" name="post[]" id="cb-select-'.$row['identifier'].'">
433
				</th>
434
				<td class="post-title page-title column-title">
435
					<strong>'.$row['name'].'</strong> - '.$status.'
436
				</td>
437
				<td class="date column-date">
438
					<abbr title="'.date('Y/m/d',strtotime($row['last_modified'])).'">'.date('Y/m/d',strtotime($row['last_modified'])).'</abbr><br>Last Modified
439
				</td>
440
				<td class="ssid column-ssid">
441
					'.$row['identifier'].'
442
				</td>
443
			</tr>';		
444
			return $row_html;
445
		}
446
	}
447
448
	/**
449
	 * Connect to wetu
450
	 */
451
	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...
452
		$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...
453
		if(isset($_POST['action']) && $_POST['action'] === 'lsx_import_items' && isset($_POST['type']) && $_POST['type'] === $this->tab_slug && isset($_POST['wetu_id'])){
454
			
455
			$wetu_id = $_POST['wetu_id'];
456
			if(isset($_POST['post_id'])){
457
				$post_id = $_POST['post_id'];	
458
			}else{
459
				$post_id = 0;
460
			}
461
462 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...
463
				$content = $_POST['content'];
464
				add_option('wetu_importer_tour_settings',$content);
465
			}else{
466
				delete_option('wetu_importer_tour_settings');
467
				$content = false;
468
			}
469
470
            $jdata=file_get_contents("http://wetu.com/API/Itinerary/V7/Get?id=".$wetu_id);
471
472 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...
473
            {
474
				$jdata=json_decode($jdata,true);
475
                if(!empty($jdata))
476
                {
477
                	$return = $this->import_row($jdata,$wetu_id,$post_id,$content);
478
                	$this->format_completed_row($return);
479
                }
480
            }
481
			die();
482
		}
483
484
	}
485
486
	/**
487
	 * Connect to wetu
488
     *
489
     * @param $data array
490
     * @param $wetu_id string
491
	 */
492
	public function import_row($data,$wetu_id,$id=0,$importable_content=false,$old1=false,$old2=false) {
493
        $post_name = $data_post_content = $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...
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...
494
        $post = array(
495
          'post_type'		=> 'tour',
496
        );
497
498
        //Set the post_content
499
		$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...
500
        if(false !== $importable_content && in_array('description',$importable_content)){
501
            $data_post_content = '';
502
503
            if(isset($data['description'])){
504
                $data_post_content = $data['description'];
505
            }elseif(isset($data['summary'])){
506
                $data_post_content = $data['summary'];
507
            }
508
            $post['post_content'] = $data_post_content;
509
        }
510
511
        //Create or update the post
512
        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...
513
            $post['ID'] = $id;
514
	        $post['post_status'] = 'publish';
515
            $id = wp_update_post($post);
516
            $prev_date = get_post_meta($id,'lsx_wetu_modified_date',true);
517
            update_post_meta($id,'lsx_wetu_modified_date',strtotime($data['last_modified']),$prev_date);
518
        }else{
519
520
            //Set the name
521
            if(isset($data['name'])){
522
                $post_name = wp_unique_post_slug(sanitize_title($data['name']),$id, 'draft', 'tour', 0);
523
            }
524
            $post['post_name'] = $post_name;
525
            $post['post_title'] = $data['name'];
526
            $post['post_status'] = 'publish';
527
            $id = wp_insert_post($post);
528
529
            //Save the WETU ID and the Last date it was modified.
530
            if(false !== $id){
531
                add_post_meta($id,'lsx_wetu_id',$wetu_id);
532
                add_post_meta($id,'lsx_wetu_modified_date',strtotime($data['last_modified']));
533
            }
534
        }
535
536
537
		//Set the price
538
		if(false !== $importable_content && in_array('price',$importable_content)){
539
			$this->set_price($data,$id);
540
		}
541
542
		//Set the Duration
543
		if(false !== $importable_content && in_array('duration',$importable_content)){
544
			$this->set_duration($data,$id);
545
		}
546
547
        if(in_array('itineraries',$importable_content) && isset($data['legs']) && !empty($data['legs'])){
548
            $this->process_itineraries($data,$id,$importable_content);
549
        }
550
551
		if(in_array('map',$importable_content) && isset($data['routes']) && !empty($data['routes'])){
552
			$this->process_map_points($data,$id);
553
		}
554
555
		//TODO Test These
556
		//Setup some default for use in the import
557 View Code Duplication
		if(false !== $importable_content && (in_array('itinerary_gallery',$importable_content) || in_array('gallery',$importable_content) || in_array('banner_image',$importable_content) || in_array('featured_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...
558
			$this->find_attachments($id);
559
		}
560
        //Set the featured image
561
        //TODO Test These
562
        if(false !== $importable_content && in_array('featured_image',$importable_content)){
563
            $this->set_featured_image($data,$id);
564
        }
565
566
		//TODO Test These
567
        if(false !== $importable_content && in_array('banner_image',$importable_content)){
568
            $this->set_banner_image($data,$id);
569
        }
570
571
		//TODO Test These
572
        //Import the main gallery
573
        if(false !== $importable_content && in_array('gallery',$importable_content)){
574
            $this->create_main_gallery($data,$id);
575
        }
576
577
        return $id;
578
	}
579
580
	/**
581
	 * A loop which runs through each leg on the tour.
582
	 */
583
	public function process_itineraries($data,$id,$importable_content) {
584
		$day_counter = 1;
585
		$leg_counter = 0;
586
587
		delete_post_meta($id,'itinerary');
588
589
		if(false !== $importable_content && in_array('accommodation',$importable_content)){
590
			delete_post_meta($id,'accommodation_to_tour');
591
		}
592
		if(false !== $importable_content && in_array('destination',$importable_content)){
593
			delete_post_meta($id,'destination_to_tour');
594
			delete_post_meta($id,'departs_from');
595
			delete_post_meta($id,'ends_in');
596
		}
597
598
		$departs_from = false;
599
		$ends_in = false;
600
601
		foreach($data['legs'] as $leg){
602
603
			if(isset($leg['days']) && !empty($leg['days'])){
604
605
				//Itinerary Accommodation
606
				$current_accommodation = false;
607
				if(false !== $importable_content && in_array('accommodation',$importable_content)){
608
					$current_accommodation = $this->set_accommodation($leg,$id);
609
				}
610
611
				//Itinerary Destination
612
				$current_destination = false;
613
				if(false !== $importable_content && in_array('destination',$importable_content)){
614
					$current_destination = $this->set_destination($leg,$id);
615
				}
616
617
				//If the Nights are the same mount of days in the array,  then it isnt "By Destination"
618
				if($leg['nights'] === count($leg['days']) || 0 === $leg['itinerary_leg_id']){
619
620
					foreach($leg['days'] as $day){
621
622
						$current_day = array();
623
624
						$current_day['title'] =  esc_attr('Day ','wetu-importer').$day_counter;
625
626
						//Description
627 View Code Duplication
						if(false !== $importable_content && in_array('itinerary_description',$importable_content) && isset($day['notes']) && '' !== $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...
628
							$current_day['description'] = strip_tags($day['notes']);
629
						}else{
630
							$current_day['description'] = '';
631
						}
632
633
						//Itinerary Gallery
634
						if(false !== $importable_content && in_array('itinerary_gallery',$importable_content) && isset($day['images'])){
635
							$current_day['featured_image'] = '';
636
						}else{
637
							$current_day['featured_image'] = '';
638
						}
639
640
						//Accommodation
641
						if(false !== $current_accommodation){
642
							$current_day['accommodation_to_tour'] = array($current_accommodation);
643
						}else{
644
							$current_day['accommodation_to_tour'] = array();
645
						}
646
647
						//Destination
648
						if(false !== $current_destination){
649
							$current_day['destination_to_tour'] = array($current_destination);
650
						}else{
651
							$current_day['destination_to_tour'] = array();
652
						}
653
654
						//Included
655 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...
656
							$current_day['included'] = strip_tags($day['included']);
657
						}else{
658
							$current_day['description'] = '';
659
						}
660
661
						//Excluded
662 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...
663
							$current_day['excluded'] = strip_tags($day['excluded']);
664
						}else{
665
							$current_day['excluded'] = '';
666
						}
667
668
						$this->set_itinerary_day($current_day,$id);
669
						$day_counter++;
670
					}
671
672
				}else{
673
					$day_counter = $day_counter + (int)$leg['nights'];
674
				}
675
676
			}
677
678
			//If we are in the first leg,  and the destination was attached then save it as the departure field.
679
			if( 0 === $leg_counter && false !== $current_destination){
680
				$departs_from = $current_destination;
0 ignored issues
show
Bug introduced by
The variable $current_destination does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
681
			}
682
683
			//If its the last leg then save it as the ends in.
684
			if( $leg_counter === (count($data['legs'])-2) && false !== $current_destination){
685
				$ends_in = $current_destination;
686
			}
687
			$leg_counter++;
688
		}
689
690
		if(false !== $departs_from){
691
			add_post_meta($id,'departs_from',$departs_from,true);
692
		}
693
		if(false !== $ends_in){
694
			add_post_meta($id,'ends_in',$ends_in,true);
695
		}
696
	}
697
698
	/**
699
	 * Run through your routes and save the points as a KML file.
700
	 */
701
	public function process_map_points($data,$id) {
702
703
	    if(!empty($data['routes'])){
704
705
	        delete_post_meta($id,'wetu_map_points');
706
707
	        $points = array();
708
709
	        foreach($data['routes'] as $route){
710
711
712
	            if(isset($route['points']) && '' !== $route['points']){
713
714
	                $temp_points = explode(';',$route['points']);
715
	                $point_counter = count($temp_points);
716
717
					for ($x = 0; $x <= $point_counter; $x++) {
718
					    $y = $x+1;
719
						$points[] = $temp_points[$x].','.$temp_points[$y];
720
						$x++;
721
					}
722
				}
723
            }
724
            if(!empty($points)){
725
				$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...
726
            }
727
        }
728
729
	}
730
731
	/**
732
	 * Set the Itinerary Day
733
	 */
734
	public function set_itinerary_day($day,$id) {
735
        $this->save_custom_field($day,'itinerary',$id,false,false);
736
	}
737
738
	/**
739
	 * Set the price
740
	 */
741
	public function set_price($data,$id) {
742 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...
743
            $price = preg_replace("/[^0-9,.]/", "", $data['price']);
744
            $this->save_custom_field($price,'price',$id);
745
		}
746
	}
747
748
	/**
749
	 * Set the duration
750
	 */
751
	public function set_duration($data,$id) {
752 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...
753
			$price = $data['days'];
754
			$price = preg_replace("/[^0-9,.]/", "", $price);
755
			$this->save_custom_field($price,'duration',$id);
756
		}
757
	}
758
759
760
	/**
761
	 * Connects the Accommodation if its available
762
	 */
763
	public function set_accommodation($day,$id) {
764
765
	    $ac_id = false;
766
		$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...
767
		
768
		if(isset($day['content_entity_id']) && !empty($day['content_entity_id'])){
769
770
			if(false !== $this->current_accommodation && !empty($this->current_accommodation) && array_key_exists($day['content_entity_id'],$this->current_accommodation)){
771
                $ac_id = $this->current_accommodation[$day['content_entity_id']];
772
			}else{
773
				$ac_id = wp_insert_post(array(
774
                    'post_type' => 'accommodation',
775
                    'post_status' => 'draft',
776
                    'post_title' => $day['content_entity_id']
777
                ));
778
				$this->save_custom_field($day['content_entity_id'],'lsx_wetu_id',$ac_id);
779
			}
780
781
			if('' !== $ac_id && false !== $ac_id){
782
			    $this->save_custom_field($ac_id,'accommodation_to_tour',$id,false,false);
783
				$this->save_custom_field($id,'tour_to_accommodation',$ac_id,false,false);
784
            }
785
		}
786
		return $ac_id;
787
	}
788
789
	/**
790
	 * Grab all the current accommodation posts via the lsx_wetu_id field.
791
     *
792
     * @param $post_type string
793
     * @return boolean / array
794
	 */
795
	public function find_current_accommodation($post_type='accommodation') {
796
		global $wpdb;
797
		$accommodation = parent::find_current_accommodation($post_type);
798
799
		$return = false;
800
		if(!empty($accommodation)){
801
		    foreach($accommodation as $key => $acc){
802
				$return[$acc->meta_value] = $acc->post_id;
803
            }
804
        }
805
		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_Accommodat...d_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...
806
	}
807
808
	/**
809
	 * Grab all the current accommodation posts via the lsx_wetu_id field.
810
     * @return boolean / array
811
	 */
812
	public function find_current_destinations() {
813
		return $this->find_current_accommodation('destination');
814
	}
815
816
	/**
817
	 * Connects the destinations post type
818
	 *
819
	 * @param $day array
820
	 * @param $id string
821
	 * @return boolean / string
822
	 */
823
	public function set_destination($day,$id) {
824
		$dest_id = false;
825
		$country_id = false;
826
		$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...
827
828
		if(isset($day['destination_content_entity_id']) && !empty($day['destination_content_entity_id'])){
829
830
			if(false !== $this->current_destinations && !empty($this->current_destinations) && array_key_exists($day['destination_content_entity_id'],$this->current_destinations)){
831
				$dest_id = $this->current_destinations[$day['destination_content_entity_id']];
832
833
				$potential_id = wp_get_post_parent_id($dest_id);
834
				$country_wetu_id = get_post_meta($potential_id,'lsx_wetu_id',true);
835
				if(false !== $country_wetu_id){
836
					$this->set_country($country_wetu_id, $id);
837
                }
838
839
			}else {
840
841
				$destination_json = file_get_contents("http://wetu.com/API/Pins/".$this->api_key."/Get?ids=" . $day['destination_content_entity_id']);
842
843
				if ($destination_json) {
844
					$destination_data = json_decode($destination_json, true);
845
846
					if (!empty($destination_data) && !isset($destination_data['error'])) {
847
848
					    $destination_title = $day['destination_content_entity_id'];
849
850
					    if(isset($destination_data[0]['name'])){
851
							$destination_title = $destination_data[0]['name'];
852
                        }
853
854
					    if(isset($destination_data[0]['map_object_id']) && isset($destination_data[0]['position']['country_content_entity_id'])
855
                            && $destination_data[0]['map_object_id'] !== $destination_data[0]['position']['country_content_entity_id']){
856
857
							$country_id = $this->set_country($destination_data[0]['position']['country_content_entity_id'], $id);
858
                        }
859
860
                        $dest_post = array(
861
							'post_type' => 'destination',
862
							'post_status' => 'draft',
863
							'post_title' => $destination_title
864
						);
865
866
					    if(false !== $country_id){
867
							$dest_post['post_parent'] = $country_id;
868
                        }
869
						$dest_id = wp_insert_post($dest_post);
870
871
						//Make sure we register the
872
						$this->current_destinations[$day['destination_content_entity_id']] = $dest_id;
873
874
						$this->save_custom_field($day['destination_content_entity_id'], 'lsx_wetu_id', $dest_id);
875
					}
876
				}
877
			}
878
879 View Code Duplication
			if ('' !== $dest_id && false !== $dest_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...
880
				$this->save_custom_field($dest_id, 'destination_to_tour', $id, false, false);
881
				$this->save_custom_field($id, 'tour_to_destination', $dest_id, false, false);
882
			}
883
		}
884
		return $dest_id;
885
	}
886
	/**
887
	 * Connects the destinations post type
888
	 *
889
	 * @param $dest_id string
890
     * @param $country_id array
891
	 * @param $id string
892
	 */
893
	public function set_country($country_wetu_id, $id) {
894
	    $country_id = false;
895
		$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...
896
897
        if (false !== $this->current_destinations && !empty($this->current_destinations) && array_key_exists($country_wetu_id, $this->current_destinations)) {
898
            $country_id = $this->current_destinations[$country_wetu_id];
899
        } else {
900
901
            $country_json = file_get_contents("http://wetu.com/API/Pins/".$this->api_key."/Get?ids=" . $country_wetu_id);
902
903
            if ($country_json) {
904
                $country_data = json_decode($country_json, true);
905
906
                if (!empty($country_data) && !isset($country_data['error'])) {
907
908
					//Format the title of the destination if its available,  otherwise default to the WETU ID.
909
                    $country_title = $country_wetu_id;
910
                    if (isset($country_data[0]['name'])) {
911
						$country_title = $country_data[0]['name'];
912
                    }
913
914
					$country_id = wp_insert_post(array(
915
                        'post_type' => 'destination',
916
                        'post_status' => 'draft',
917
                        'post_title' => $country_title
918
                    ));
919
					//add the country to the current destination stack
920
					$this->current_destinations[$country_wetu_id] = $country_id;
921
922
					//Save the wetu field
923
                    $this->save_custom_field($country_wetu_id, 'lsx_wetu_id', $country_id);
924
                }
925
            }
926
        }
927
928 View Code Duplication
        if ('' !== $country_id && false !== $country_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...
929
            $this->save_custom_field($country_id, 'destination_to_tour', $id, false, false);
930
            $this->save_custom_field($id, 'tour_to_destination', $country_id, false, false);
931
932
            return $country_id;
933
        }
934
	}
935
}
936
$wetu_importer_tours = new WETU_Importer_Tours();