Completed
Push — master ( 593133...8c8e21 )
by Fernando
02:56
created

WETU_Importer_Destination   D

Complexity

Total Complexity 110

Size/Duplication

Total Lines 615
Duplicated Lines 32.68 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 110
lcom 2
cbo 1
dl 201
loc 615
rs 4.8118
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 16 16 4
A set_variables() 0 8 2
B display_page() 0 111 4
B search_form() 34 34 1
B find_current_destination() 21 23 4
D process_ajax_search() 5 74 20
B format_row() 0 27 3
C process_ajax_import() 14 40 13
F import_row() 52 107 37
A set_team_member() 7 7 2
C set_map_data() 52 52 17
A set_travel_info() 0 7 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like WETU_Importer_Destination often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use WETU_Importer_Destination, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @package   WETU_Importer_Destination
4
 * @author    LightSpeed
5
 * @license   GPL-2.0+
6
 * @link      
7
 * @copyright 2016 LightSpeed
8
 **/
9
10
class WETU_Importer_Destination 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 = 'destination';
20
21
	/**
22
	 * The url to list items from WETU
23
	 *
24
	 * @since 0.0.1
25
	 *
26
	 * @var      string
27
	 */
28
	public $url = false;
29
30
	/**
31
	 * Options
32
	 *
33
	 * @since 0.0.1
34
	 *
35
	 * @var      string
36
	 */
37
	public $options = false;			
38
39
	/**
40
	 * Initialize the plugin by setting localization, filters, and administration functions.
41
	 *
42
	 * @since 1.0.0
43
	 *
44
	 * @access private
45
	 */
46 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...
47
		$this->set_variables();
48
49
		add_action( 'lsx_tour_importer_admin_tab_'.$this->tab_slug, array($this,'display_page') );
50
51
		add_action('wp_ajax_lsx_tour_importer',array($this,'process_ajax_search'));	
52
		add_action('wp_ajax_nopriv_lsx_tour_importer',array($this,'process_ajax_search'));		
53
54
		add_action('wp_ajax_lsx_import_items',array($this,'process_ajax_import'));	
55
		add_action('wp_ajax_nopriv_lsx_import_items',array($this,'process_ajax_import'));
56
57
		$temp_options = get_option('_lsx-to_settings',false);
58
		if(false !== $temp_options && isset($temp_options[$this->plugin_slug]) && !empty($temp_options[$this->plugin_slug])){
59
			$this->options = $temp_options[$this->plugin_slug];
60
		}
61
	}
62
63
	/**
64
	 * Sets the variables used throughout the plugin.
65
	 */
66
	public function set_variables()
67
	{
68
		parent::set_variables();
69
70
		if(false !== $this->api_key){
71
		    $this->url = 'https://wetu.com/API/Pins/'.$this->api_key;
72
        }
73
	}
74
75
	/**
76
	 * Display the importer administration screen
77
	 */
78
	public function display_page() {
79
        ?>
80
        <div class="wrap">
81
            <?php screen_icon(); ?>
82
83
            <?php $this->search_form(); ?>
84
85
			<form method="get" action="" id="posts-filter">
86
				<input type="hidden" name="post_type" class="post_type" value="<?php echo $this->tab_slug; ?>" />
87
				
88
				<p><input class="button button-primary add" type="button" value="<?php _e('Add to List','wetu-importer'); ?>" />
89
					<input class="button button-primary clear" type="button" value="<?php _e('Clear','wetu-importer'); ?>" />
90
				</p>				
91
92
				<table class="wp-list-table widefat fixed posts">
93
					<?php $this->table_header(); ?>
0 ignored issues
show
Unused Code introduced by
The call to the method WETU_Importer_Destination::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...
94
				
95
					<tbody id="the-list">
96
						<tr class="post-0 type-tour status-none" id="post-0">
97
							<th class="check-column" scope="row">
98
								<label for="cb-select-0" class="screen-reader-text"><?php _e('Enter a title to search for and press enter','wetu-importer'); ?></label>
99
							</th>
100
							<td class="post-title page-title column-title">
101
								<strong>
102
									<?php _e('Enter a title to search for','wetu-importer'); ?>
103
								</strong>
104
							</td>
105
							<td class="date column-date">							
106
							</td>
107
							<td class="ssid column-ssid">
108
							</td>
109
						</tr>									
110
					</tbody>
111
112
					<?php $this->table_footer(); ?>
0 ignored issues
show
Unused Code introduced by
The call to the method WETU_Importer_Destination::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...
113
114
				</table>
115
116
				<p><input class="button button-primary add" type="button" value="<?php _e('Add to List','wetu-importer'); ?>" />
117
					<input class="button button-primary clear" type="button" value="<?php _e('Clear','wetu-importer'); ?>" />
118
				</p>
119
			</form> 
120
121
			<div style="display:none;" class="import-list-wrapper">
122
				<br />        
123
				<form method="get" action="" id="import-list">
124
125
					<div class="row">
126
						<div style="width:30%;display:block;float:left;">
127
							<h3><?php _e('What content to Sync from WETU'); ?></h3>
128
							<ul>
129
								<li><input class="content" type="checkbox" name="content[]" value="description" /> <?php _e('Description','wetu-importer'); ?></li>
130
								<li><input class="content" type="checkbox" name="content[]" value="excerpt" /> <?php _e('Excerpt','wetu-importer'); ?></li>
131
132
		                        <?php if(class_exists('TO_Maps')){ ?>
133
                                    <li><input class="content" type="checkbox" name="content[]" value="location" /> <?php _e('Location','wetu-importer'); ?></li>
134
		                        <?php } ?>
135
136
		                        <?php if(class_exists('TO_Videos')){ ?>
137
								    <li><input class="content" type="checkbox" name="content[]" value="videos" /> <?php _e('Videos','wetu-importer'); ?></li>
138
		                        <?php } ?>
139
140
							</ul>
141
						</div>
142
                        <div style="width:30%;display:block;float:left;">
143
                            <h3><?php _e('Travel Information'); ?></h3>
144
                            <ul>
145
                                <li><input class="content" type="checkbox" name="content[]" value="electricity" /> <?php _e('Electricity','wetu-importer'); ?></li>
146
                                <li><input class="content" type="checkbox" name="content[]" value="banking" /> <?php _e('Banking','wetu-importer'); ?></li>
147
                                <li><input class="content" type="checkbox" name="content[]" value="cuisine" /> <?php _e('Cuisine','wetu-importer'); ?></li>
148
                                <li><input class="content" type="checkbox" name="content[]" value="climate" /> <?php _e('Climate','wetu-importer'); ?></li>
149
                                <li><input class="content" type="checkbox" name="content[]" value="transport" /> <?php _e('Transport','wetu-importer'); ?></li>
150
                                <li><input class="content" type="checkbox" name="content[]" value="dress" /> <?php _e('Dress','wetu-importer'); ?></li>
151
                            </ul>
152
                        </div>
153
154
		                <?php if(class_exists('TO_Team')){ ?>
155
                            <div style="width:30%;display:block;float:left;">
156
                                <h3><?php _e('Assign a Team Member'); ?></h3>
157
                                <?php $this->team_member_checkboxes(); ?>
158
                            </div>
159
                        <?php } ?>
160
161
						<br clear="both" />			
162
					</div>
163
164
165
					<h3><?php _e('Your List'); ?></h3> 
166
					<table class="wp-list-table widefat fixed posts">
167
						<?php $this->table_header(); ?>
0 ignored issues
show
Unused Code introduced by
The call to the method WETU_Importer_Destination::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...
168
169
						<tbody>
170
171
						</tbody>
172
173
						<?php $this->table_footer(); ?>
0 ignored issues
show
Unused Code introduced by
The call to the method WETU_Importer_Destination::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...
174
175
					</table>
176
177
					<p><input class="button button-primary" type="submit" value="<?php _e('Sync','wetu-importer'); ?>" /></p>
178
				</form>
179
			</div>
180
181
			<div style="display:none;" class="completed-list-wrapper">
182
				<h3><?php _e('Completed'); ?></h3>
183
				<ul>
184
				</ul>
185
			</div>
186
        </div>
187
        <?php
188
	}
189
190
	/**
191
	 * search_form
192
	 */
193 View Code Duplication
	public function search_form() {
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...
194
	?>
195
        <form class="ajax-form" id="<?php echo $this->plugin_slug; ?>-search-form" method="get" action="tools.php" data-type="<?php echo $this->tab_slug; ?>">
196
        	<input type="hidden" name="page" value="<?php echo $this->tab_slug; ?>" />
197
198
        	<h3><span class="dashicons dashicons-search"></span> <?php _e('Search','wetu-importer'); ?></h3>
199
            <div class="normal-search">
200
                <input pattern=".{3,}" placeholder="3 characters minimum" class="keyword" name="keyword" value=""> <input class="button button-primary submit" type="submit" value="<?php _e('Search','wetu-importer'); ?>" />
201
            </div>
202
203
204
            <div class="advanced-search hidden" style="display:none;">
205
                <p><?php _e('Enter several keywords, each on a new line.','wetu-importer'); ?></p>
206
                <textarea rows="10" cols="40" name="bulk-keywords"></textarea>
207
                <input class="button button-primary submit" type="submit" value="<?php _e('Search','wetu-importer'); ?>" />
208
            </div>
209
210
            <p>
211
                <a class="advanced-search-toggle" href="#"><?php _e('Bulk Search','wetu-importer'); ?></a> |
212
                <a class="published search-toggle" href="#publish"><?php esc_attr_e('Published','wetu-importer'); ?></a> |
213
                <a class="pending search-toggle"  href="#pending"><?php esc_attr_e('Pending','wetu-importer'); ?></a> |
214
                <a class="draft search-toggle"  href="#draft"><?php esc_attr_e('Draft','wetu-importer'); ?></a>
215
            </p>
216
217
            <div class="ajax-loader" style="display:none;width:100%;text-align:center;">
218
            	<img style="width:64px;" src="<?php echo WETU_IMPORTER_URL.'assets/images/ajaxloader.gif';?>" />
219
            </div>
220
221
            <div class="ajax-loader-small" style="display:none;width:100%;text-align:center;">
222
            	<img style="width:32px;" src="<?php echo WETU_IMPORTER_URL.'assets/images/ajaxloader.gif';?>" />
223
            </div>
224
        </form>
225
	<?php
226
	}
227
228
	/**
229
	 * Grab all the current destination posts via the lsx_wetu_id field.
230
	 */
231
	/*public function find_current_destination($post_type='destination') {
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
232
		global $wpdb;
233
		$return = array();
234
235
		$current_destination = $wpdb->get_results("
236
					SELECT key1.post_id,key1.meta_value
237
					FROM {$wpdb->postmeta} key1
238
239
					INNER JOIN  {$wpdb->posts} key2 
240
    				ON key1.post_id = key2.ID
241
					
242
					WHERE key1.meta_key = 'lsx_wetu_id'
243
					AND key2.post_type = '{$post_type}'
244
245
					LIMIT 0,500
246
		");
247
		if(null !== $current_destination && !empty($current_destination)){
248
			foreach($current_destination as $accom){
249
				$return[$accom->meta_value] = $accom;
250
			}
251
		}
252
		return $return;
253
	}*/
254
255
	/**
256
	 * Grab all the current destination posts via the lsx_wetu_id field.
257
	 */
258 View Code Duplication
	public function find_current_destination($post_type='destination') {
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...
259
		global $wpdb;
260
		$return = array();
261
262
		$current_destination = $wpdb->get_results("
263
					SELECT key1.post_id,key1.meta_value,key2.post_title as name,key2.post_date as last_modified
264
					FROM {$wpdb->postmeta} key1
265
266
					INNER JOIN  {$wpdb->posts} key2 
267
    				ON key1.post_id = key2.ID
268
					
269
					WHERE key1.meta_key = 'lsx_wetu_id'
270
					AND key2.post_type = '{$post_type}'
271
272
					LIMIT 0,500
273
		");
274
		if(null !== $current_destination && !empty($current_destination)){
275
			foreach($current_destination as $accom){
276
				$return[$accom->meta_value] = $accom;
277
			}
278
		}
279
		return $return;
280
	}
281
282
	/**
283
	 * Run through the destination grabbed from the DB.
284
	 */
285
	public function process_ajax_search() {
286
		$return = false;
287
		if(isset($_POST['action']) && $_POST['action'] === 'lsx_tour_importer' && isset($_POST['type']) && $_POST['type'] === 'destination'){
288
289
			if ( isset($_POST['keyword'] )) {
290
				$searched_items = false;
291
292 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...
293
					$keyphrases = $_POST['keyword'];
294
				}else{
295
					$keyphrases = array(0);
296
                }
297
298
				if(!is_array($keyphrases)){
299
					$keyphrases = array($keyphrases);
300
				}
301
				foreach($keyphrases as &$keyword){
302
					$keyword = ltrim(rtrim($keyword));
303
				}
304
305
				$post_status = false;
306
				if(in_array('publish',$keyphrases)){
307
					$post_status = 'publish';
308
				}
309
				if(in_array('pending',$keyphrases)){
310
					$post_status = 'pending';
311
				}
312
				if(in_array('draft',$keyphrases)){
313
					$post_status = 'draft';
314
				}
315
316
				$destination = $this->find_current_destination();
317
318
				if (!empty($destination)) {
319
320
					foreach($destination as $row){
321
322
						//If we are searching for
323
						if(false !== $post_status){
324
325
                            $current_status = get_post_status($row->post_id);
326
                            if($current_status !== $post_status){
327
                                continue;
328
                            }
329
                            $searched_items[sanitize_title($row->name).'-'.$row->meta_value] = $this->format_row($row);
330
331
332
						}else{
333
							//Search through each keyword.
334
							foreach($keyphrases as $keyphrase){
335
336
								//Make sure the keyphrase is turned into an array
337
								$keywords = explode(" ",$keyphrase);
338
								if(!is_array($keywords)){
339
									$keywords = array($keywords);
340
								}
341
342
								if($this->multineedle_stripos(ltrim(rtrim($row->name)), $keywords) !== false){
343
									$searched_items[sanitize_title($row->name).'-'.$row->meta_value] = $this->format_row($row);
344
								}
345
							}
346
						}
347
					}		
348
				}
349
350
				if(false !== $searched_items){
351
					ksort($searched_items);
352
					$return = implode($searched_items);
353
				}
354
			}
355
			print_r($return);
356
			die();
357
		}
358
	}
359
360
	/**
361
	 * Formats the row for output on the screen.
362
	 */	
363
	public function format_row($row = false){
364
		if(false !== $row){
365
366
			$status = 'import';
367
			if(0 !== $row->post_id){
368
				$status = '<a href="'.admin_url('/post.php?post='.$row->post_id.'&action=edit').'" target="_blank">'.get_post_status($row->post_id).'</a>';
369
			}
370
371
			$row_html = '
372
			<tr class="post-'.$row->post_id.' type-tour" id="post-'.$row->post_id.'">
373
				<th class="check-column" scope="row">
374
					<label for="cb-select-'.$row->meta_value.'" class="screen-reader-text">'.$row->name.'</label>
375
					<input type="checkbox" data-identifier="'.$row->meta_value.'" value="'.$row->post_id.'" name="post[]" id="cb-select-'.$row->meta_value.'">
376
				</th>
377
				<td class="post-title page-title column-title">
378
					<strong>'.$row->name.'</strong> - '.$status.'
379
				</td>
380
				<td class="date column-date">
381
					<abbr title="'.date('Y/m/d',strtotime($row->last_modified)).'">'.date('Y/m/d',strtotime($row->last_modified)).'</abbr><br>Last Modified
382
				</td>
383
				<td class="ssid column-ssid">
384
					'.$row->meta_value.'
385
				</td>
386
			</tr>';		
387
			return $row_html;
388
		}
389
	}
390
391
	/**
392
	 * Connect to wetu
393
	 */
394
	public function process_ajax_import() {
395
		$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...
396
		if(isset($_POST['action']) && $_POST['action'] === 'lsx_import_items' && isset($_POST['type']) && $_POST['type'] === 'destination' && isset($_POST['wetu_id'])){
397
			
398
			$wetu_id = $_POST['wetu_id'];
399
			if(isset($_POST['post_id'])){
400
				$post_id = $_POST['post_id'];	
401
			}else{
402
				$post_id = 0;
403
			}
404
405
			if(isset($_POST['team_members'])){
406
				$team_members = $_POST['team_members'];	
407
			}else{
408
				$team_members = false;
409
			}
410
411
            $safari_brands = false;
412
413 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...
414
				$content = $_POST['content'];	
415
			}else{
416
				$content = false;
417
			}
418
419
            $jdata=file_get_contents($this->url."/Get?ids=".$wetu_id);
420 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...
421
            {
422
                $adata=json_decode($jdata,true);
423
                if(!empty($adata))
424
                {
425
                	$return = $this->import_row($adata,$wetu_id,$post_id,$team_members,$content,$safari_brands);
426
                	$this->format_completed_row($return);
427
                }
428
            }
429
430
			die();
431
		}
432
433
	}	
434
435
	/**
436
	 * Connect to wetu
437
	 */
438
	public function import_row($data,$wetu_id,$id=0,$team_members=false,$importable_content=false,$safari_brands=false) {
439
440
        if(trim($data[0]['type'])=='Destination')
441
        {
442
	        $post_name = $data_post_content = $data_post_excerpt = '';
443
	        $post = array(
444
	          'post_type'		=> 'destination',
445
	        );
446
447
	        $content_used_general_description = false;
448
449
	        //Set the post_content
450 View Code Duplication
	        if(false !== $importable_content && in_array('description',$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...
451
		        if(isset($data[0]['content']['extended_description']))
452
		        {
453
		            $data_post_content = $data[0]['content']['extended_description'];
454
		        }elseif(isset($data[0]['content']['general_description'])){
455
		            $data_post_content = $data[0]['content']['general_description'];
456
		            $content_used_general_description = true;
457
		        }elseif(isset($data[0]['content']['teaser_description'])){
458
		        	$data_post_content = $data[0]['content']['teaser_description'];
459
		        }
460
	        	$post['post_content'] = wp_strip_all_tags($data_post_content);
461
	        }
462
463
	        //set the post_excerpt
464 View Code Duplication
	        if(false !== $importable_content && in_array('excerpt',$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...
465
		        if(isset($data[0]['content']['teaser_description'])){
466
		        	$data_post_excerpt = $data[0]['content']['teaser_description'];
467
		        }elseif(isset($data[0]['content']['extended_description'])){
468
					$data_post_excerpt = $data[0]['content']['extended_description'];
469
				}elseif(isset($data[0]['content']['general_description']) && false === $content_used_general_description){
470
		            $data_post_excerpt = $data[0]['content']['general_description'];
471
		        }	   
472
		        $post['post_excerpt'] = $data_post_excerpt;     	
473
	        }
474
475 View Code Duplication
	        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...
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...
476
	        	$post['ID'] = $id;
477
				if(isset($data[0]['name'])){
478
					$post['post_title'] = $data[0]['name'];
479
					$post['post_status'] = 'pending';
480
					$post['post_name'] = wp_unique_post_slug(sanitize_title($data[0]['name']),$id, 'draft', 'destination', 0);
481
				}
482
	        	$id = wp_update_post($post);
483
	        	$prev_date = get_post_meta($id,'lsx_wetu_modified_date',true);
484
	        	update_post_meta($id,'lsx_wetu_modified_date',strtotime($data[0]['last_modified']),$prev_date);
485
	        }else{
486
487
		        //Set the name
488
		        if(isset($data[0]['name'])){
489
		            $post_name = wp_unique_post_slug(sanitize_title($data[0]['name']),$id, 'draft', 'destination', 0);
490
		        }
491
	        	$post['post_name'] = $post_name;
492
	        	$post['post_title'] = $data[0]['name'];
493
	        	$post['post_status'] = 'pending';
494
	        	$id = wp_insert_post($post);
495
496
	        	//Save the WETU ID and the Last date it was modified.
497
	        	if(false !== $id){
498
	        		add_post_meta($id,'lsx_wetu_id',$wetu_id);
499
	        		add_post_meta($id,'lsx_wetu_modified_date',strtotime($data[0]['last_modified']));
500
	        	}
501
	        }
502
503
	        //Set the team member if it is there
504 View Code Duplication
	        if(post_type_exists('team') && false !== $team_members && '' !== $team_members){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
505
	        	$this->set_team_member($id,$team_members);
506
	    	}
507
508
	        if(false !== $importable_content && in_array('location',$importable_content)){
509
	        	$this->set_map_data($data,$id);
510
	        }
511
512
	        //Set the Room Data
513
	        if(false !== $importable_content && in_array('videos',$importable_content)){
514
	        	$this->set_video_data($data,$id);
515
	    	}
516
517
			//Set the Electricity
518
			if(false !== $importable_content && in_array('electricity',$importable_content)){
519
				$this->set_travel_info($data,$id,'electricity');
520
			}
521
			//Set the cuisine
522
			if(false !== $importable_content && in_array('cuisine',$importable_content)){
523
				$this->set_travel_info($data,$id,'cuisine');
524
			}
525
			//Set the banking
526
			if(false !== $importable_content && in_array('banking',$importable_content)){
527
				$this->set_travel_info($data,$id,'banking');
528
			}
529
			//Set the transport
530
			if(false !== $importable_content && in_array('transport',$importable_content)){
531
				$this->set_travel_info($data,$id,'transport');
532
			}
533
			//Set the dress
534
			if(false !== $importable_content && in_array('dress',$importable_content)){
535
				$this->set_travel_info($data,$id,'dress');
536
			}
537
			//Set the climate
538
			if(false !== $importable_content && in_array('climate',$importable_content)){
539
				$this->set_travel_info($data,$id,'climate');
540
			}
541
542
        }
543
        return $id;
544
	}
545
546
	/**
547
	 * Set the team memberon each item.
548
	 */
549 View Code Duplication
	public function set_team_member($id,$team_members) {
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...
550
551
		delete_post_meta($id, 'team_to_'.$this->tab_slug);
552
		foreach($team_members as $team){
553
        	add_post_meta($id,'team_to_'.$this->tab_slug,$team);			
554
		}
555
	}
556
	
557
	/**
558
	 * Saves the longitude and lattitude, as well as sets the map marker.
559
	 */
560 View Code Duplication
	public function set_map_data($data,$id) {
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...
561
		$longitude = $latitude = $address = false;
562
		$zoom = '15';	
563
564
		if(isset($data[0]['position'])){
565
566
			if(isset($data[0]['position']['driving_latitude'])){
567
				$latitude = $data[0]['position']['driving_latitude'];
568
			}elseif(isset($data[0]['position']['latitude'])){
569
				$latitude = $data[0]['position']['latitude'];
570
			}
571
572
			if(isset($data[0]['position']['driving_longitude'])){
573
				$longitude = $data[0]['position']['driving_longitude'];
574
			}elseif(isset($data[0]['position']['longitude'])){
575
				$longitude = $data[0]['position']['longitude'];
576
			}		
577
578
		}
579
		if(isset($data[0]['content']) && isset($data[0]['content']['contact_information'])){
580
			if(isset($data[0]['content']['contact_information']['address'])){
581
				$address = strip_tags($data[0]['content']['contact_information']['address']);
582
583
				$address = explode("\n",$address);
584
				foreach($address as $bitkey => $bit){
585
					$bit = ltrim(rtrim($bit));
586
					if(false === $bit || '' === $bit || null === $bit or empty($bit)){
587
						unset($address[$bitkey]);
588
					}
589
				}
590
				$address = implode(', ',$address);
591
				$address = str_replace(', , ', ', ', $address);
592
			}	
593
		}
594
595
596
		if(false !== $longitude){
597
			$location_data = array(
598
				'address'	=>	$address,
599
				'lat'		=>	$latitude,
600
				'long'		=>	$longitude,
601
				'zoom'		=>	$zoom,
602
				'elevation'	=>	'',
603
			);
604
			if(false !== $id && '0' !== $id){
605
	        	$prev = get_post_meta($id,'location',true);
606
	        	update_post_meta($id,'location',$location_data,$prev);
607
	        }else{
608
	        	add_post_meta($id,'location',$location_data,true);
609
	        }
610
		}
611
	}
612
613
	/**
614
	 * Saves the room data
615
	 */
616
	public function set_travel_info($data,$id,$meta_key) {
617
618
		if(!empty($data[0]['travel_information']) && isset($data[0]['travel_information'][$meta_key])){
619
			$content = strip_tags($data[0]['travel_information'][$meta_key]);
620
			$this->save_custom_field($content,$meta_key,$id);
0 ignored issues
show
Documentation introduced by
$content 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...
621
		}
622
	}
623
624
}
625
$wetu_importer_destination = new WETU_Importer_Destination();