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

WETU_Importer_Destination::__construct()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
dl 21
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 13
nc 4
nop 0
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
	 * 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
	 * Options
41
	 *
42
	 * @since 0.0.1
43
	 *
44
	 * @var      string
45
	 */
46
	public $options = false;
47
48
	/**
49
	 * The fields you wish to import
50
	 *
51
	 * @since 0.0.1
52
	 *
53
	 * @var      string
54
	 */
55
	public $destination_options = false;
56
57
	/**
58
	 * Initialize the plugin by setting localization, filters, and administration functions.
59
	 *
60
	 * @since 1.0.0
61
	 *
62
	 * @access private
63
	 */
64 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...
65
		$this->set_variables();
66
67
		add_action( 'lsx_tour_importer_admin_tab_'.$this->tab_slug, array($this,'display_page') );
68
69
		add_action('wp_ajax_lsx_tour_importer',array($this,'process_ajax_search'));	
70
		add_action('wp_ajax_nopriv_lsx_tour_importer',array($this,'process_ajax_search'));		
71
72
		add_action('wp_ajax_lsx_import_items',array($this,'process_ajax_import'));	
73
		add_action('wp_ajax_nopriv_lsx_import_items',array($this,'process_ajax_import'));
74
75
		$temp_options = get_option('_lsx-to_settings',false);
76
		if(false !== $temp_options && isset($temp_options[$this->plugin_slug]) && !empty($temp_options[$this->plugin_slug])){
77
			$this->options = $temp_options[$this->plugin_slug];
78
		}
79
80
		$destination_options = get_option('wetu_importer_destination_settings',false);
81
		if(false !== $destination_options){
82
			$this->destination_options = $destination_options;
83
		}
84
	}
85
86
	/**
87
	 * Sets the variables used throughout the plugin.
88
	 */
89
	public function set_variables()
90
	{
91
		parent::set_variables();
92
93
		// ** This request only works with API KEY **
94
		//if ( false !== $this->api_username && false !== $this->api_password ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% 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...
95
		//	$this->url    = 'https://wetu.com/API/Pins/';
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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...
96
		//	$this->url_qs = 'username=' . $this->api_username . '&password=' . $this->api_password;
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% 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...
97
		//} elseif ( false !== $this->api_key ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
98
			$this->url    = 'https://wetu.com/API/Pins/' . $this->api_key;
99
			$this->url_qs = '';
100
		//}
101
	}
102
103
	/**
104
	 * Display the importer administration screen
105
	 */
106
	public function display_page() {
107
        ?>
108
        <div class="wrap">
109
            <?php screen_icon(); ?>
110
111
            <?php $this->search_form(); ?>
112
113
			<form method="get" action="" id="posts-filter">
114
				<input type="hidden" name="post_type" class="post_type" value="<?php echo $this->tab_slug; ?>" />
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
120
				<table class="wp-list-table widefat fixed posts">
121
					<?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...
122
				
123
					<tbody id="the-list">
124
						<tr class="post-0 type-tour status-none" id="post-0">
125
							<th class="check-column" scope="row">
126
								<label for="cb-select-0" class="screen-reader-text"><?php _e('Enter a title to search for and press enter','wetu-importer'); ?></label>
127
							</th>
128
							<td class="post-title page-title column-title">
129
								<strong>
130
									<?php _e('Enter a title to search for','wetu-importer'); ?>
131
								</strong>
132
							</td>
133
							<td class="date column-date">							
134
							</td>
135
							<td class="ssid column-ssid">
136
							</td>
137
						</tr>									
138
					</tbody>
139
140
					<?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...
141
142
				</table>
143
144
				<p><input class="button button-primary add" type="button" value="<?php _e('Add to List','wetu-importer'); ?>" />
145
					<input class="button button-primary clear" type="button" value="<?php _e('Clear','wetu-importer'); ?>" />
146
				</p>
147
			</form> 
148
149
			<div style="display:none;" class="import-list-wrapper">
150
				<br />        
151
				<form method="get" action="" id="import-list">
152
153
					<div class="row">
154
						<div style="width:30%;display:block;float:left;">
155
							<h3><?php _e('What content to Sync from WETU'); ?></h3>
156
							<ul>
157
								<li><input class="content" checked="<?php $this->checked($this->destination_options,'description'); ?>" type="checkbox" name="content[]" value="description" /> <?php _e('Description','wetu-importer'); ?></li>
0 ignored issues
show
Documentation introduced by
$this->destination_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...
158
								<li><input class="content" checked="<?php $this->checked($this->destination_options,'excerpt'); ?>" type="checkbox" name="content[]" value="excerpt" /> <?php _e('Excerpt','wetu-importer'); ?></li>
0 ignored issues
show
Documentation introduced by
$this->destination_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...
159
160
		                        <?php if(class_exists('TO_Maps')){ ?>
161
                                    <li><input class="content" checked="<?php $this->checked($this->destination_options,'location'); ?>" type="checkbox" name="content[]" value="location" /> <?php _e('Location','wetu-importer'); ?></li>
0 ignored issues
show
Documentation introduced by
$this->destination_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...
162
		                        <?php } ?>
163
164
		                        <?php if(class_exists('TO_Videos')){ ?>
165
								    <li><input class="content" checked="<?php $this->checked($this->destination_options,'videos'); ?>" type="checkbox" name="content[]" value="videos" /> <?php _e('Videos','wetu-importer'); ?></li>
0 ignored issues
show
Documentation introduced by
$this->destination_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...
166
		                        <?php } ?>
167
168
							</ul>
169
						</div>
170
                        <div style="width:30%;display:block;float:left;">
171
                            <h3><?php _e('Travel Information'); ?></h3>
172
                            <ul>
173
                                <li><input class="content" checked="<?php $this->checked($this->destination_options,'electricity'); ?>" type="checkbox" name="content[]" value="electricity" /> <?php _e('Electricity','wetu-importer'); ?></li>
0 ignored issues
show
Documentation introduced by
$this->destination_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...
174
                                <li><input class="content" checked="<?php $this->checked($this->destination_options,'banking'); ?>" type="checkbox" name="content[]" value="banking" /> <?php _e('Banking','wetu-importer'); ?></li>
0 ignored issues
show
Documentation introduced by
$this->destination_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...
175
                                <li><input class="content" checked="<?php $this->checked($this->destination_options,'cuisine'); ?>" type="checkbox" name="content[]" value="cuisine" /> <?php _e('Cuisine','wetu-importer'); ?></li>
0 ignored issues
show
Documentation introduced by
$this->destination_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->destination_options,'climate'); ?>" type="checkbox" name="content[]" value="climate" /> <?php _e('Climate','wetu-importer'); ?></li>
0 ignored issues
show
Documentation introduced by
$this->destination_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
                                <li><input class="content" checked="<?php $this->checked($this->destination_options,'transport'); ?>" type="checkbox" name="content[]" value="transport" /> <?php _e('Transport','wetu-importer'); ?></li>
0 ignored issues
show
Documentation introduced by
$this->destination_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...
178
                                <li><input class="content" checked="<?php $this->checked($this->destination_options,'dress'); ?>" type="checkbox" name="content[]" value="dress" /> <?php _e('Dress','wetu-importer'); ?></li>
0 ignored issues
show
Documentation introduced by
$this->destination_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
                            </ul>
180
                        </div>
181
182
		                <?php if(class_exists('TO_Team')){ ?>
183
                            <div style="width:30%;display:block;float:left;">
184
                                <h3><?php _e('Assign a Team Member'); ?></h3>
185
                                <?php $this->team_member_checkboxes($this->destination_options); ?>
0 ignored issues
show
Documentation introduced by
$this->destination_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...
186
                            </div>
187
                        <?php } ?>
188
189
						<br clear="both" />			
190
					</div>
191
192
193
					<h3><?php _e('Your List'); ?></h3>
194
                    <p><input class="button button-primary" type="submit" value="<?php _e('Sync','wetu-importer'); ?>" /></p>
195
					<table class="wp-list-table widefat fixed posts">
196
						<?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...
197
198
						<tbody>
199
200
						</tbody>
201
202
						<?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...
203
204
					</table>
205
206
					<p><input class="button button-primary" type="submit" value="<?php _e('Sync','wetu-importer'); ?>" /></p>
207
				</form>
208
			</div>
209
210
			<div style="display:none;" class="completed-list-wrapper">
211
				<h3><?php _e('Completed'); ?></h3>
212
				<ul>
213
				</ul>
214
			</div>
215
        </div>
216
        <?php
217
	}
218
219
	/**
220
	 * search_form
221
	 */
222 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...
223
	?>
224
        <form class="ajax-form" id="<?php echo $this->plugin_slug; ?>-search-form" method="get" action="tools.php" data-type="<?php echo $this->tab_slug; ?>">
225
        	<input type="hidden" name="page" value="<?php echo $this->tab_slug; ?>" />
226
227
        	<h3><span class="dashicons dashicons-search"></span> <?php _e('Search','wetu-importer'); ?></h3>
228
            <div class="normal-search">
229
                <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'); ?>" />
230
            </div>
231
232
233
            <div class="advanced-search hidden" style="display:none;">
234
                <p><?php _e('Enter several keywords, each on a new line.','wetu-importer'); ?></p>
235
                <textarea rows="10" cols="40" name="bulk-keywords"></textarea>
236
                <input class="button button-primary submit" type="submit" value="<?php _e('Search','wetu-importer'); ?>" />
237
            </div>
238
239
            <p>
240
                <a class="advanced-search-toggle" href="#"><?php _e('Bulk Search','wetu-importer'); ?></a> |
241
                <a class="published search-toggle" href="#publish"><?php esc_attr_e('Published','wetu-importer'); ?></a> |
242
                <a class="pending search-toggle"  href="#pending"><?php esc_attr_e('Pending','wetu-importer'); ?></a> |
243
                <a class="draft search-toggle"  href="#draft"><?php esc_attr_e('Draft','wetu-importer'); ?></a>
244
            </p>
245
246
            <div class="ajax-loader" style="display:none;width:100%;text-align:center;">
247
            	<img style="width:64px;" src="<?php echo WETU_IMPORTER_URL.'assets/images/ajaxloader.gif';?>" />
248
            </div>
249
250
            <div class="ajax-loader-small" style="display:none;width:100%;text-align:center;">
251
            	<img style="width:32px;" src="<?php echo WETU_IMPORTER_URL.'assets/images/ajaxloader.gif';?>" />
252
            </div>
253
        </form>
254
	<?php
255
	}
256
257
	/**
258
	 * Grab all the current destination posts via the lsx_wetu_id field.
259
	 */
260
	/*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...
261
		global $wpdb;
262
		$return = array();
263
264
		$current_destination = $wpdb->get_results("
265
					SELECT key1.post_id,key1.meta_value
266
					FROM {$wpdb->postmeta} key1
267
268
					INNER JOIN  {$wpdb->posts} key2 
269
    				ON key1.post_id = key2.ID
270
					
271
					WHERE key1.meta_key = 'lsx_wetu_id'
272
					AND key2.post_type = '{$post_type}'
273
274
					LIMIT 0,500
275
		");
276
		if(null !== $current_destination && !empty($current_destination)){
277
			foreach($current_destination as $accom){
278
				$return[$accom->meta_value] = $accom;
279
			}
280
		}
281
		return $return;
282
	}*/
283
284
	/**
285
	 * Grab all the current destination posts via the lsx_wetu_id field.
286
	 */
287 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...
288
		global $wpdb;
289
		$return = array();
290
291
		$current_destination = $wpdb->get_results("
292
					SELECT key1.post_id,key1.meta_value,key2.post_title as name,key2.post_date as last_modified
293
					FROM {$wpdb->postmeta} key1
294
295
					INNER JOIN  {$wpdb->posts} key2 
296
    				ON key1.post_id = key2.ID
297
					
298
					WHERE key1.meta_key = 'lsx_wetu_id'
299
					AND key2.post_type = '{$post_type}'
300
301
					LIMIT 0,500
302
		");
303
		if(null !== $current_destination && !empty($current_destination)){
304
			foreach($current_destination as $accom){
305
				$return[$accom->meta_value] = $accom;
306
			}
307
		}
308
		return $return;
309
	}
310
311
	/**
312
	 * Run through the destination grabbed from the DB.
313
	 */
314
	public function process_ajax_search() {
315
		$return = false;
316
		if(isset($_POST['action']) && $_POST['action'] === 'lsx_tour_importer' && isset($_POST['type']) && $_POST['type'] === 'destination'){
317
318
			if ( isset($_POST['keyword'] )) {
319
				$searched_items = false;
320
321 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...
322
					$keyphrases = $_POST['keyword'];
323
				}else{
324
					$keyphrases = array(0);
325
                }
326
327
				if(!is_array($keyphrases)){
328
					$keyphrases = array($keyphrases);
329
				}
330
				foreach($keyphrases as &$keyword){
331
					$keyword = ltrim(rtrim($keyword));
332
				}
333
334
				$post_status = false;
335
				if(in_array('publish',$keyphrases)){
336
					$post_status = 'publish';
337
				}
338
				if(in_array('pending',$keyphrases)){
339
					$post_status = 'pending';
340
				}
341
				if(in_array('draft',$keyphrases)){
342
					$post_status = 'draft';
343
				}
344
345
				$destination = $this->find_current_destination();
346
347
				if (!empty($destination)) {
348
349
					foreach($destination as $row){
350
351
						//If we are searching for
352
						if(false !== $post_status){
353
354
                            $current_status = get_post_status($row->post_id);
355
                            if($current_status !== $post_status){
356
                                continue;
357
                            }
358
                            $searched_items[sanitize_title($row->name).'-'.$row->meta_value] = $this->format_row($row);
359
360
361
						}else{
362
							//Search through each keyword.
363
							foreach($keyphrases as $keyphrase){
364
365
								//Make sure the keyphrase is turned into an array
366
								$keywords = explode(" ",$keyphrase);
367
								if(!is_array($keywords)){
368
									$keywords = array($keywords);
369
								}
370
371
								if($this->multineedle_stripos(ltrim(rtrim($row->name)), $keywords) !== false){
372
									$searched_items[sanitize_title($row->name).'-'.$row->meta_value] = $this->format_row($row);
373
								}
374
							}
375
						}
376
					}		
377
				}
378
379
				if(false !== $searched_items){
380
					ksort($searched_items);
381
					$return = implode($searched_items);
382
				}
383
			}
384
			print_r($return);
385
			die();
386
		}
387
	}
388
389
	/**
390
	 * Formats the row for output on the screen.
391
	 */	
392
	public function format_row($row = false){
393
		if(false !== $row){
394
395
			$status = 'import';
396
			if(0 !== $row->post_id){
397
				$status = '<a href="'.admin_url('/post.php?post='.$row->post_id.'&action=edit').'" target="_blank">'.get_post_status($row->post_id).'</a>';
398
			}
399
400
			$row_html = '
401
			<tr class="post-'.$row->post_id.' type-tour" id="post-'.$row->post_id.'">
402
				<th class="check-column" scope="row">
403
					<label for="cb-select-'.$row->meta_value.'" class="screen-reader-text">'.$row->name.'</label>
404
					<input type="checkbox" data-identifier="'.$row->meta_value.'" value="'.$row->post_id.'" name="post[]" id="cb-select-'.$row->meta_value.'">
405
				</th>
406
				<td class="post-title page-title column-title">
407
					<strong>'.$row->name.'</strong> - '.$status.'
408
				</td>
409
				<td class="date column-date">
410
					<abbr title="'.date('Y/m/d',strtotime($row->last_modified)).'">'.date('Y/m/d',strtotime($row->last_modified)).'</abbr><br>Last Modified
411
				</td>
412
				<td class="ssid column-ssid">
413
					'.$row->meta_value.'
414
				</td>
415
			</tr>';		
416
			return $row_html;
417
		}
418
	}
419
420
	/**
421
	 * Connect to wetu
422
	 */
423
	public function process_ajax_import() {
424
		$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...
425
		if(isset($_POST['action']) && $_POST['action'] === 'lsx_import_items' && isset($_POST['type']) && $_POST['type'] === 'destination' && isset($_POST['wetu_id'])){
426
			
427
			$wetu_id = $_POST['wetu_id'];
428
			if(isset($_POST['post_id'])){
429
				$post_id = $_POST['post_id'];	
430
			}else{
431
				$post_id = 0;
432
			}
433
434
			if(isset($_POST['team_members'])){
435
				$team_members = $_POST['team_members'];	
436
			}else{
437
				$team_members = false;
438
			}
439
440
            $safari_brands = false;
441
442 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...
443
				$content = $_POST['content'];
444
				add_option('wetu_importer_destination_settings',$content);
445
			}else{
446
				delete_option('wetu_importer_destination_settings');
447
				$content = false;
448
			}
449
450
            $jdata = file_get_contents( $this->url . '/Get?' . $this->url_qs . '&ids=' . $wetu_id );
451 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...
452
            {
453
                $adata=json_decode($jdata,true);
454
                if(!empty($adata))
455
                {
456
                	$return = $this->import_row($adata,$wetu_id,$post_id,$team_members,$content,$safari_brands);
457
                	$this->format_completed_row($return);
458
                }
459
            }
460
461
			die();
462
		}
463
464
	}	
465
466
	/**
467
	 * Connect to wetu
468
	 */
469
	public function import_row($data,$wetu_id,$id=0,$team_members=false,$importable_content=false,$safari_brands=false) {
470
471
        if(trim($data[0]['type'])=='Destination')
472
        {
473
	        $post_name = $data_post_content = $data_post_excerpt = '';
474
	        $post = array(
475
	          'post_type'		=> 'destination',
476
	        );
477
478
	        $content_used_general_description = false;
479
480
	        //Set the post_content
481 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...
482
		        if(isset($data[0]['content']['extended_description']))
483
		        {
484
		            $data_post_content = $data[0]['content']['extended_description'];
485
		        }elseif(isset($data[0]['content']['general_description'])){
486
		            $data_post_content = $data[0]['content']['general_description'];
487
		            $content_used_general_description = true;
488
		        }elseif(isset($data[0]['content']['teaser_description'])){
489
		        	$data_post_content = $data[0]['content']['teaser_description'];
490
		        }
491
	        	$post['post_content'] = wp_strip_all_tags($data_post_content);
492
	        }
493
494
	        //set the post_excerpt
495 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...
496
		        if(isset($data[0]['content']['teaser_description'])){
497
		        	$data_post_excerpt = $data[0]['content']['teaser_description'];
498
		        }elseif(isset($data[0]['content']['extended_description'])){
499
					$data_post_excerpt = $data[0]['content']['extended_description'];
500
				}elseif(isset($data[0]['content']['general_description']) && false === $content_used_general_description){
501
		            $data_post_excerpt = $data[0]['content']['general_description'];
502
		        }	   
503
		        $post['post_excerpt'] = $data_post_excerpt;     	
504
	        }
505
506 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...
507
	        	$post['ID'] = $id;
508
				if(isset($data[0]['name'])){
509
					$post['post_title'] = $data[0]['name'];
510
					$post['post_status'] = 'publish';
511
					$post['post_name'] = wp_unique_post_slug(sanitize_title($data[0]['name']),$id, 'draft', 'destination', 0);
512
				}
513
	        	$id = wp_update_post($post);
514
	        	$prev_date = get_post_meta($id,'lsx_wetu_modified_date',true);
515
	        	update_post_meta($id,'lsx_wetu_modified_date',strtotime($data[0]['last_modified']),$prev_date);
516
	        }else{
517
518
		        //Set the name
519
		        if(isset($data[0]['name'])){
520
		            $post_name = wp_unique_post_slug(sanitize_title($data[0]['name']),$id, 'draft', 'destination', 0);
521
		        }
522
	        	$post['post_name'] = $post_name;
523
	        	$post['post_title'] = $data[0]['name'];
524
	        	$post['post_status'] = 'publish';
525
	        	$id = wp_insert_post($post);
526
527
	        	//Save the WETU ID and the Last date it was modified.
528
	        	if(false !== $id){
529
	        		add_post_meta($id,'lsx_wetu_id',$wetu_id);
530
	        		add_post_meta($id,'lsx_wetu_modified_date',strtotime($data[0]['last_modified']));
531
	        	}
532
	        }
533
534
	        //Set the team member if it is there
535 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...
536
	        	$this->set_team_member($id,$team_members);
537
	    	}
538
539
	        if(class_exists('LSX_TO_Maps')){
540
	        	$this->set_map_data($data,$id);
541
	        }
542
543
	        //Set the Room Data
544
	        if(false !== $importable_content && in_array('videos',$importable_content)){
545
	        	$this->set_video_data($data,$id);
546
	    	}
547
548
			//Set the Electricity
549
			if(false !== $importable_content && in_array('electricity',$importable_content)){
550
				$this->set_travel_info($data,$id,'electricity');
551
			}
552
			//Set the cuisine
553
			if(false !== $importable_content && in_array('cuisine',$importable_content)){
554
				$this->set_travel_info($data,$id,'cuisine');
555
			}
556
			//Set the banking
557
			if(false !== $importable_content && in_array('banking',$importable_content)){
558
				$this->set_travel_info($data,$id,'banking');
559
			}
560
			//Set the transport
561
			if(false !== $importable_content && in_array('transport',$importable_content)){
562
				$this->set_travel_info($data,$id,'transport');
563
			}
564
			//Set the dress
565
			if(false !== $importable_content && in_array('dress',$importable_content)){
566
				$this->set_travel_info($data,$id,'dress');
567
			}
568
			//Set the climate
569
			if(false !== $importable_content && in_array('climate',$importable_content)){
570
				$this->set_travel_info($data,$id,'climate');
571
			}
572
573
        }
574
        return $id;
575
	}
576
577
	/**
578
	 * Set the team memberon each item.
579
	 */
580 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...
581
582
		delete_post_meta($id, 'team_to_'.$this->tab_slug);
583
		foreach($team_members as $team){
584
        	add_post_meta($id,'team_to_'.$this->tab_slug,$team);			
585
		}
586
	}
587
	
588
	/**
589
	 * Saves the longitude and lattitude, as well as sets the map marker.
590
	 */
591 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...
592
		$longitude = $latitude = $address = false;
593
		$zoom = '15';	
594
595
		if(isset($data[0]['position'])){
596
597
			if(isset($data[0]['position']['driving_latitude'])){
598
				$latitude = $data[0]['position']['driving_latitude'];
599
			}elseif(isset($data[0]['position']['latitude'])){
600
				$latitude = $data[0]['position']['latitude'];
601
			}
602
603
			if(isset($data[0]['position']['driving_longitude'])){
604
				$longitude = $data[0]['position']['driving_longitude'];
605
			}elseif(isset($data[0]['position']['longitude'])){
606
				$longitude = $data[0]['position']['longitude'];
607
			}		
608
609
		}
610
		if(isset($data[0]['content']) && isset($data[0]['content']['contact_information'])){
611
			if(isset($data[0]['content']['contact_information']['address'])){
612
				$address = strip_tags($data[0]['content']['contact_information']['address']);
613
614
				$address = explode("\n",$address);
615
				foreach($address as $bitkey => $bit){
616
					$bit = ltrim(rtrim($bit));
617
					if(false === $bit || '' === $bit || null === $bit or empty($bit)){
618
						unset($address[$bitkey]);
619
					}
620
				}
621
				$address = implode(', ',$address);
622
				$address = str_replace(', , ', ', ', $address);
623
			}	
624
		}
625
626
627
		if(false !== $longitude){
628
			$location_data = array(
629
				'address'	=>	$address,
630
				'lat'		=>	$latitude,
631
				'long'		=>	$longitude,
632
				'zoom'		=>	$zoom,
633
				'elevation'	=>	'',
634
			);
635
			if(false !== $id && '0' !== $id){
636
	        	$prev = get_post_meta($id,'location',true);
637
	        	update_post_meta($id,'location',$location_data,$prev);
638
	        }else{
639
	        	add_post_meta($id,'location',$location_data,true);
640
	        }
641
		}
642
	}
643
644
	/**
645
	 * Saves the room data
646
	 */
647
	public function set_travel_info($data,$id,$meta_key) {
648
649
		if(!empty($data[0]['travel_information']) && isset($data[0]['travel_information'][$meta_key])){
650
			$content = $data[0]['travel_information'][$meta_key];
651
			$this->save_custom_field($content,$meta_key,$id);
652
		}
653
	}
654
655
}
656
$wetu_importer_destination = new WETU_Importer_Destination();