Passed
Push — master ( f00970...13e5ed )
by Warwick
03:05
created

WETU_Importer_Destination::process_ajax_search()   F

Complexity

Conditions 24
Paths 258

Size

Total Lines 85
Code Lines 47

Duplication

Lines 5
Ratio 5.88 %

Importance

Changes 0
Metric Value
dl 5
loc 85
rs 3.6773
c 0
b 0
f 0
cc 24
eloc 47
nc 258
nop 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package   WETU_Importer_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
11
{
12
13
	/**
14
	 * The url to list items from WETU
15
	 *
16
	 * @since 0.0.1
17
	 *
18
	 * @var      string
19
	 */
20
	public $tab_slug = 'destination';
21
22
	/**
23
	 * The url to list items from WETU
24
	 *
25
	 * @since 0.0.1
26
	 *
27
	 * @var      string
28
	 */
29
	public $url = false;
30
31
	/**
32
	 * The query string url to list items from WETU
33
	 *
34
	 * @since 0.0.1
35
	 *
36
	 * @var      string
37
	 */
38
	public $url_qs = false;
39
40
	/**
41
	 * Options
42
	 *
43
	 * @since 0.0.1
44
	 *
45
	 * @var      string
46
	 */
47
	public $options = false;
48
49
	/**
50
	 * The fields you wish to import
51
	 *
52
	 * @since 0.0.1
53
	 *
54
	 * @var      string
55
	 */
56
	public $destination_options = false;
57
58
	/**
59
	 * Initialize the plugin by setting localization, filters, and administration functions.
60
	 *
61
	 * @since 1.0.0
62
	 *
63
	 * @access private
64
	 */
65
	public function __construct()
66
	{
67
		$this->set_variables();
68
	}
69
70
	/**
71
	 * Sets the variables used throughout the plugin.
72
	 */
73 View Code Duplication
	public function set_variables()
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...
74
	{
75
		parent::set_variables();
76
		// ** This request only works with API KEY **
77
		//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...
78
		//	$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...
79
		//	$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...
80
		//} 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...
81
		$this->url = 'https://wetu.com/API/Pins/' . $this->api_key;
82
		$this->url_qs = '';
83
		//}
84
85
		$temp_options = get_option('_lsx-to_settings', false);
86
		if (false !== $temp_options && isset($temp_options[$this->plugin_slug]) && !empty($temp_options[$this->plugin_slug])) {
87
			$this->options = $temp_options[$this->plugin_slug];
88
		}
89
90
		$destination_options = get_option('wetu_importer_destination_settings', false);
91
		if (false !== $destination_options) {
92
			$this->destination_options = $destination_options;
93
		}
94
	}
95
96
	/**
97
	 * Display the importer administration screen
98
	 */
99
	public function display_page()
100
	{
101
		?>
102
        <div class="wrap">
103
			<?php $this->navigation('destination'); ?>
104
105
			<?php $this->search_form(); ?>
106
107
            <form method="get" action="" id="posts-filter">
108
                <input type="hidden" name="post_type" class="post_type" value="<?php echo $this->tab_slug; ?>"/>
109
110
                <p><input class="button button-primary add" type="button"
111
                          value="<?php _e('Add to List', 'wetu-importer'); ?>"/>
112
                    <input class="button button-primary clear" type="button"
113
                           value="<?php _e('Clear', 'wetu-importer'); ?>"/>
114
                </p>
115
116
                <table class="wp-list-table widefat fixed posts">
117
					<?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...
118
119
                    <tbody id="the-list">
120
                    <tr class="post-0 type-tour status-none" id="post-0">
121
                        <th class="check-column" scope="row">
122
                            <label for="cb-select-0"
123
                                   class="screen-reader-text"><?php _e('Enter a title to search for and press enter', 'wetu-importer'); ?></label>
124
                        </th>
125
                        <td class="post-title page-title column-title">
126
                            <strong>
127
								<?php _e('Enter a title to search for', 'wetu-importer'); ?>
128
                            </strong>
129
                        </td>
130
                        <td class="date column-date">
131
                        </td>
132
                        <td class="ssid column-ssid">
133
                        </td>
134
                    </tr>
135
                    </tbody>
136
137
					<?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...
138
139
                </table>
140
141
                <p><input class="button button-primary add" type="button"
142
                          value="<?php _e('Add to List', 'wetu-importer'); ?>"/>
143
                    <input class="button button-primary clear" type="button"
144
                           value="<?php _e('Clear', 'wetu-importer'); ?>"/>
145
                </p>
146
            </form>
147
148
            <div style="display:none;" class="import-list-wrapper">
149
                <br/>
150
                <form method="get" action="" id="import-list">
151
152
                    <div class="row">
153
                        <div class="settings-all" style="width:30%;display:block;float:left;">
154
                            <h3><?php _e('What content to Sync from WETU'); ?></h3>
155
                            <ul>
156
                                <li>
157
                                    <input class="content select-all" <?php $this->checked($this->destination_options, 'all'); ?>
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
                                           type="checkbox" name="content[]"
159
                                           value="all"/> <?php _e('Select All', 'wetu-importer'); ?></li>
160
                                <li>
161
                                    <input class="content" <?php $this->checked($this->destination_options, 'description'); ?>
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
                                           type="checkbox" name="content[]"
163
                                           value="description"/> <?php _e('Description', 'wetu-importer'); ?></li>
164
                                <li>
165
                                    <input class="content" <?php $this->checked($this->destination_options, 'excerpt'); ?>
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
                                           type="checkbox" name="content[]"
167
                                           value="excerpt"/> <?php _e('Excerpt', 'wetu-importer'); ?></li>
168
                                <li>
169
                                    <input class="content" <?php $this->checked($this->destination_options, 'gallery'); ?>
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...
170
                                           type="checkbox" name="content[]"
171
                                           value="gallery"/> <?php _e('Main Gallery', 'wetu-importer'); ?></li>
172 View Code Duplication
								<?php if (class_exists('LSX_TO_Maps')) { ?>
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...
173
                                    <li>
174
                                        <input class="content" <?php $this->checked($this->destination_options, 'location'); ?>
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
                                               type="checkbox" name="content[]"
176
                                               value="location"/> <?php _e('Location', 'wetu-importer'); ?></li>
177
								<?php } ?>
178
179 View Code Duplication
								<?php if (class_exists('LSX_TO_Videos')) { ?>
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...
180
                                    <li>
181
                                        <input class="content" <?php $this->checked($this->destination_options, 'videos'); ?>
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...
182
                                               type="checkbox" name="content[]"
183
                                               value="videos"/> <?php _e('Videos', 'wetu-importer'); ?></li>
184
								<?php } ?>
185
186
                            </ul>
187
                            <h4><?php _e('Additional Content'); ?></h4>
188
                            <ul>
189
                                <li>
190
                                    <input class="content" <?php $this->checked($this->destination_options, 'featured_image'); ?>
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...
191
                                           type="checkbox" name="content[]"
192
                                           value="featured_image"/> <?php _e('Set Featured Image', 'wetu-importer'); ?>
193
                                </li>
194 View Code Duplication
								<?php if (class_exists('LSX_Banners')) { ?>
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...
195
                                    <li>
196
                                        <input class="content" <?php $this->checked($this->destination_options, 'banner_image'); ?>
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...
197
                                               type="checkbox" name="content[]"
198
                                               value="banner_image"/> <?php _e('Set Banner Image', 'wetu-importer'); ?>
199
                                    </li>
200
								<?php } ?>
201
                            </ul>
202
                        </div>
203
                        <div class="settings-all" style="width:30%;display:block;float:left;">
204
                            <h3><?php _e('Travel Information'); ?></h3>
205
                            <ul>
206
                                <li>
207
                                    <input class="content" <?php $this->checked($this->destination_options, 'electricity'); ?>
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...
208
                                           type="checkbox" name="content[]"
209
                                           value="electricity"/> <?php _e('Electricity', 'wetu-importer'); ?></li>
210
                                <li>
211
                                    <input class="content" <?php $this->checked($this->destination_options, 'banking'); ?>
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...
212
                                           type="checkbox" name="content[]"
213
                                           value="banking"/> <?php _e('Banking', 'wetu-importer'); ?></li>
214
                                <li>
215
                                    <input class="content" <?php $this->checked($this->destination_options, 'cuisine'); ?>
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...
216
                                           type="checkbox" name="content[]"
217
                                           value="cuisine"/> <?php _e('Cuisine', 'wetu-importer'); ?></li>
218
                                <li>
219
                                    <input class="content" <?php $this->checked($this->destination_options, 'climate'); ?>
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...
220
                                           type="checkbox" name="content[]"
221
                                           value="climate"/> <?php _e('Climate', 'wetu-importer'); ?></li>
222
                                <li>
223
                                    <input class="content" <?php $this->checked($this->destination_options, 'transport'); ?>
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...
224
                                           type="checkbox" name="content[]"
225
                                           value="transport"/> <?php _e('Transport', 'wetu-importer'); ?></li>
226
                                <li><input class="content" <?php $this->checked($this->destination_options, 'dress'); ?>
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...
227
                                           type="checkbox" name="content[]"
228
                                           value="dress"/> <?php _e('Dress', 'wetu-importer'); ?></li>
229
                            </ul>
230
                        </div>
231
232
						<?php if (class_exists('LSX_TO_Team')) { ?>
233
                            <div style="width:30%;display:block;float:left;">
234
                                <h3><?php _e('Assign a Team Member'); ?></h3>
235
								<?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...
236
                            </div>
237
						<?php } ?>
238
239
                        <br clear="both"/>
240
                    </div>
241
242
243
                    <h3><?php _e('Your List'); ?></h3>
244
                    <p><input class="button button-primary" type="submit"
245
                              value="<?php _e('Sync', 'wetu-importer'); ?>"/></p>
246
                    <table class="wp-list-table widefat fixed posts">
247
						<?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...
248
249
                        <tbody>
250
251
                        </tbody>
252
253
						<?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...
254
255
                    </table>
256
257
                    <p><input class="button button-primary" type="submit"
258
                              value="<?php _e('Sync', 'wetu-importer'); ?>"/></p>
259
                </form>
260
            </div>
261
262
            <div style="display:none;" class="completed-list-wrapper">
263
                <h3><?php _e('Completed'); ?></h3>
264
                <ul>
265
                </ul>
266
            </div>
267
        </div>
268
		<?php
269
	}
270
271
	/**
272
	 * Grab all the current destination posts via the lsx_wetu_id field.
273
	 */
274 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...
275
	{
276
		global $wpdb;
277
		$return = array();
278
279
		$current_destination = $wpdb->get_results("
280
					SELECT key1.post_id,key1.meta_value,key2.post_title as name,key2.post_date as last_modified
281
					FROM {$wpdb->postmeta} key1
282
283
					INNER JOIN  {$wpdb->posts} key2 
284
    				ON key1.post_id = key2.ID
285
					
286
					WHERE key1.meta_key = 'lsx_wetu_id'
287
					AND key2.post_type = '{$post_type}'
288
289
					LIMIT 0,500
290
		");
291
		if (null !== $current_destination && !empty($current_destination)) {
292
			foreach ($current_destination as $accom) {
293
				$return[$accom->meta_value] = $accom;
294
			}
295
		}
296
		return $return;
297
	}
298
299
	/**
300
	 * Run through the destination grabbed from the DB.
301
	 */
302
	public function process_ajax_search()
303
	{
304
		$return = false;
305
306
		if (isset($_POST['action']) && $_POST['action'] === 'lsx_tour_importer' && isset($_POST['type']) && $_POST['type'] === 'destination') {
307
308
			if (isset($_POST['keyword'])) {
309
				$searched_items = false;
310
311 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...
312
					$keyphrases = $_POST['keyword'];
313
				} else {
314
					$keyphrases = array(0);
315
				}
316
317
				if (!is_array($keyphrases)) {
318
					$keyphrases = array($keyphrases);
319
				}
320
				foreach ($keyphrases as &$keyword) {
321
					$keyword = ltrim(rtrim($keyword));
322
				}
323
324
				$post_status = false;
325
				if (in_array('publish', $keyphrases)) {
326
					$post_status = 'publish';
327
				}
328
				if (in_array('pending', $keyphrases)) {
329
					$post_status = 'pending';
330
				}
331
				if (in_array('draft', $keyphrases)) {
332
					$post_status = 'draft';
333
				}
334
				if (in_array('import', $keyphrases)) {
335
					$post_status = 'import';
336
				}
337
338
				$destination = $this->find_current_destination();
339
340
				if (!empty($destination)) {
341
342
					foreach ($destination as $row) {
343
344
						//If we are searching for
345
						if('import' === $post_status) {
346
							if(is_array($this->queued_imports) && in_array($row->post_id,$this->queued_imports)){
347
								$searched_items[sanitize_title($row->name) . '-' . $row->meta_value] = $this->format_row($row);
348
							}else{
349
								continue;
350
							}
351
						}else if (false !== $post_status) {
352
353
							$current_status = get_post_status($row->post_id);
354
							if ($current_status !== $post_status) {
355
								continue;
356
							}
357
							$searched_items[sanitize_title($row->name) . '-' . $row->meta_value] = $this->format_row($row);
358
359
360
						} else {
361
							//Search through each keyword.
362
							foreach ($keyphrases as $keyphrase) {
363
364
								//Make sure the keyphrase is turned into an array
365
								$keywords = explode(" ", $keyphrase);
366
								if (!is_array($keywords)) {
367
									$keywords = array($keywords);
368
								}
369
370
								if ($this->multineedle_stripos(ltrim(rtrim($row->name)), $keywords) !== false) {
371
									$searched_items[sanitize_title($row->name) . '-' . $row->meta_value] = $this->format_row($row);
372
								}
373
							}
374
						}
375
					}
376
				}
377
378
				if (false !== $searched_items) {
379
					ksort($searched_items);
380
					$return = implode($searched_items);
381
				}
382
			}
383
			print_r($return);
384
			die();
385
		}
386
	}
387
388
	/**
389
	 * Formats the row for output on the screen.
390
	 */
391
	public function format_row($row = false)
392
	{
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
	{
425
		$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...
426
		if (isset($_POST['action']) && $_POST['action'] === 'lsx_import_items' && isset($_POST['type']) && $_POST['type'] === 'destination' && isset($_POST['wetu_id'])) {
427
428
			$wetu_id = $_POST['wetu_id'];
429
			if (isset($_POST['post_id'])) {
430
				$post_id = $_POST['post_id'];
431
			} else {
432
				$post_id = 0;
433
			}
434
435
			if (isset($_POST['team_members'])) {
436
				$team_members = $_POST['team_members'];
437
			} else {
438
				$team_members = false;
439
			}
440
441
			$safari_brands = false;
442
443
			delete_option('wetu_importer_destination_settings');
444 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...
445
				$content = $_POST['content'];
446
				add_option('wetu_importer_destination_settings', $content);
447
			} else {
448
				$content = false;
449
			}
450
451
			$jdata = file_get_contents($this->url . '/Get?' . $this->url_qs . '&ids=' . $wetu_id);
452
			if ($jdata) {
453
				$adata = json_decode($jdata, true);
454
				if (!empty($adata)) {
455
					$return = $this->import_row($adata, $wetu_id, $post_id, $team_members, $content, $safari_brands);
456
					$this->remove_from_queue($return);
457
					$this->format_completed_row($return);
458
				}
459
			}
460
		}
461
	}
462
463
	/**
464
	 * Saves the queue to the option.
465
	 */
466 View Code Duplication
	public function remove_from_queue($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...
467
		if (!empty($this->queued_imports)) {
468
469
			if(($key = array_search($id, $this->queued_imports)) !== false) {
470
				unset($this->queued_imports[$key]);
471
472
				delete_option('wetu_importer_que');
473
				update_option('wetu_importer_que',$this->queued_imports);
474
			}
475
		}
476
	}
477
478
	/**
479
	 * Connect to wetu
480
	 */
481
	public function import_row($data, $wetu_id, $id = 0, $team_members = false, $importable_content = false, $safari_brands = false)
0 ignored issues
show
Unused Code introduced by
The parameter $safari_brands 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...
482
	{
483
484
		if (trim($data[0]['type']) == 'Destination') {
485
			$post_name = $data_post_content = $data_post_excerpt = '';
486
			$post = array(
487
				'post_type' => 'destination',
488
			);
489
490
			$content_used_general_description = false;
491
492
			//Set the post_content
493 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...
494
				if (isset($data[0]['content']['extended_description'])) {
495
					$data_post_content = $data[0]['content']['extended_description'];
496
				} elseif (isset($data[0]['content']['general_description'])) {
497
					$data_post_content = $data[0]['content']['general_description'];
498
					$content_used_general_description = true;
499
				} elseif (isset($data[0]['content']['teaser_description'])) {
500
					$data_post_content = $data[0]['content']['teaser_description'];
501
				}
502
				$post['post_content'] = wp_strip_all_tags($data_post_content);
503
			}
504
505
			//set the post_excerpt
506 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...
507
				if (isset($data[0]['content']['teaser_description'])) {
508
					$data_post_excerpt = $data[0]['content']['teaser_description'];
509
				} elseif (isset($data[0]['content']['extended_description'])) {
510
					$data_post_excerpt = $data[0]['content']['extended_description'];
511
				} elseif (isset($data[0]['content']['general_description']) && false === $content_used_general_description) {
512
					$data_post_excerpt = $data[0]['content']['general_description'];
513
				}
514
				$post['post_excerpt'] = $data_post_excerpt;
515
			}
516
517 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...
518
				$post['ID'] = $id;
519
				if (isset($data[0]['name'])) {
520
					$post['post_title'] = $data[0]['name'];
521
					$post['post_status'] = 'publish';
522
					$post['post_name'] = wp_unique_post_slug(sanitize_title($data[0]['name']), $id, 'draft', 'destination', 0);
523
				}
524
				$id = wp_update_post($post);
525
				$prev_date = get_post_meta($id, 'lsx_wetu_modified_date', true);
526
				update_post_meta($id, 'lsx_wetu_modified_date', strtotime($data[0]['last_modified']), $prev_date);
527
			} else {
528
529
				//Set the name
530
				if (isset($data[0]['name'])) {
531
					$post_name = wp_unique_post_slug(sanitize_title($data[0]['name']), $id, 'draft', 'destination', 0);
532
				}
533
				$post['post_name'] = $post_name;
534
				$post['post_title'] = $data[0]['name'];
535
				$post['post_status'] = 'publish';
536
				$id = wp_insert_post($post);
537
538
				//Save the WETU ID and the Last date it was modified.
539
				if (false !== $id) {
540
					add_post_meta($id, 'lsx_wetu_id', $wetu_id);
541
					add_post_meta($id, 'lsx_wetu_modified_date', strtotime($data[0]['last_modified']));
542
				}
543
			}
544
545
			//Set the team member if it is there
546 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...
547
				$this->set_team_member($id, $team_members);
548
			}
549
550
			if (class_exists('LSX_TO_Maps')) {
551
				$this->set_map_data($data, $id, 4);
552
			}
553
554
			//Set the Room Data
555
			if (false !== $importable_content && in_array('videos', $importable_content)) {
556
				$this->set_video_data($data, $id);
557
			}
558
559
			//Set the Electricity
560
			if (false !== $importable_content && in_array('electricity', $importable_content)) {
561
				$this->set_travel_info($data, $id, 'electricity');
562
			}
563
			//Set the cuisine
564
			if (false !== $importable_content && in_array('cuisine', $importable_content)) {
565
				$this->set_travel_info($data, $id, 'cuisine');
566
			}
567
			//Set the banking
568
			if (false !== $importable_content && in_array('banking', $importable_content)) {
569
				$this->set_travel_info($data, $id, 'banking');
570
			}
571
			//Set the transport
572
			if (false !== $importable_content && in_array('transport', $importable_content)) {
573
				$this->set_travel_info($data, $id, 'transport');
574
			}
575
			//Set the dress
576
			if (false !== $importable_content && in_array('dress', $importable_content)) {
577
				$this->set_travel_info($data, $id, 'dress');
578
			}
579
			//Set the climate
580
			if (false !== $importable_content && in_array('climate', $importable_content)) {
581
				$this->set_travel_info($data, $id, 'climate');
582
			}
583
584
			//Setup some default for use in the import
585
			if (false !== $importable_content && (in_array('gallery', $importable_content) || in_array('banner_image', $importable_content) || in_array('featured_image', $importable_content))) {
586
				$this->find_attachments($id);
587
588
				//Set the featured image
589
				if (false !== $importable_content && in_array('featured_image', $importable_content)) {
590
					$this->set_featured_image($data, $id);
591
				}
592
				if (false !== $importable_content && in_array('banner_image', $importable_content)) {
593
					$this->set_banner_image($data, $id);
594
				}
595
				//Import the main gallery
596
				if (false !== $importable_content && in_array('gallery', $importable_content)) {
597
					$this->create_main_gallery($data, $id);
598
				}
599
			}
600
601
		}
602
		return $id;
603
	}
604
605
	/**
606
	 * Set the team memberon each item.
607
	 */
608 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...
609
	{
610
611
		delete_post_meta($id, 'team_to_' . $this->tab_slug);
612
		foreach ($team_members as $team) {
613
			add_post_meta($id, 'team_to_' . $this->tab_slug, $team);
614
		}
615
	}
616
617
	/**
618
	 * Saves the room data
619
	 */
620
	public function set_travel_info($data, $id, $meta_key)
621
	{
622
623
		if (!empty($data[0]['travel_information']) && isset($data[0]['travel_information'][$meta_key])) {
624
			$content = $data[0]['travel_information'][$meta_key];
625
			$this->save_custom_field($content, $meta_key, $id);
626
		}
627
	}
628
629
}