Completed
Push — master ( d652f3...3704f2 )
by Warwick
04:03
created

WETU_Importer_Admin::navigation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
/**
3
 * @package   WETU_Importer_Admin
4
 * @author    LightSpeed
5
 * @license   GPL-2.0+
6
 * @link      
7
 * @copyright 2016 LightSpeed
8
 **/
9
10
class WETU_Importer_Admin extends WETU_Importer {
11
12
	/**
13
	 * The previously attached images
14
	 *
15
	 * @var      array()
16
	 */
17
	public $found_attachments = array();	
18
19
	/**
20
	 * The gallery ids for the found attachements
21
	 *
22
	 * @var      array()
23
	 */
24
	public $gallery_meta = array();		
25
26
	/**
27
	 * the featured image id
28
	 *
29
	 * @var      int
30
	 */
31
	public $featured_image = false;
32
33
	/**
34
	 * the banner image
35
	 *
36
	 * @var      int
37
	 */
38
	public $banner_image = false;	
39
40
	/**
41
	 * Initialize the plugin by setting localization, filters, and administration functions.
42
	 *
43
	 * @since 1.0.0
44
	 *
45
	 * @access private
46
	 */
47
	public function __construct() {
48
		add_action( 'admin_enqueue_scripts', array($this,'admin_scripts') ,11 );
49
		add_action( 'admin_menu', array( $this, 'register_importer_page' ),20 );
50
	}
51
52
	/**
53
	 * Registers the admin page which will house the importer form.
54
	 */
55
	public function register_importer_page() {
56
		add_submenu_page( 'tour-operator',esc_html__( 'Importer', 'tour-operator' ), esc_html__( 'Importer', 'tour-operator' ), 'manage_options', 'wetu-importer', array( $this, 'display_page' ) );
57
	}
58
59
	/**
60
	 * Enqueue the JS needed to contact wetu and return your result.
61
	 */
62
	public function admin_scripts() {
63
		if ( defined( 'WP_DEBUG' ) && true === WP_DEBUG ) {
64
			$min = '';
0 ignored issues
show
Unused Code introduced by
$min 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...
65
		} else {
66
			$min = '.min';
0 ignored issues
show
Unused Code introduced by
$min 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...
67
		}
68
		$min = '';
69
70
		if(is_admin() && isset($_GET['page']) && $this->plugin_slug === $_GET['page']){
71
			wp_enqueue_script( 'wetu-importers-script', WETU_IMPORTER_URL . 'assets/js/wetu-importer' . $min . '.js', array( 'jquery' ), WETU_IMPORTER_VER, true );
72
			wp_localize_script( 'wetu-importers-script', 'lsx_tour_importer_params', array(
73
			'ajax_url' => admin_url('admin-ajax.php'),
74
			) );			
75
		}
76
	}			
77
78
	/**
79
	 * Display the importer administration screen
80
	 */
81
	public function display_page() {
82
        ?>
83
        <div class="wrap">
84
            <?php screen_icon(); ?>
85
86
            <?php if(!isset($_GET['tab'])){ ?>
87
	            <h2><?php _e('Welcome to the LSX Wetu Importer','wetu-importer'); ?></h2>
88
	            <p>If this is the first time you are running the import, then follow the steps below.</p>
89
	            <ul>
90
                    <li>Step 1 - Import your <a href="<?php echo admin_url('admin.php'); ?>?page=<?php echo $this->plugin_slug; ?>&tab=tour"><?php _e('Tours','wetu-importer'); ?></a></li>
91
	            	<li>Step 2 - The tour import will have created draft <a href="<?php echo admin_url('admin.php'); ?>?page=<?php echo $this->plugin_slug; ?>&tab=accommodation"><?php _e('accommodation','wetu-importer'); ?></a> that will need to be imported.</li>
92
                    <li>Step 3 - Lastly import the <a href="<?php echo admin_url('admin.php'); ?>?page=<?php echo $this->plugin_slug; ?>&tab=destination"><?php _e('destinations','wetu-importer'); ?></a> draft posts created during the previous two steps.</li>
93
	            </ul>
94
95
		            <h3><?php _e('Additional Tools','wetu-importer'); ?></h3>
96
		            <ul>
97
		            	<li><a href="<?php echo admin_url('admin.php'); ?>?page=<?php echo $this->plugin_slug; ?>&tab=connect_accommodation"><?php _e('Connect Accommodation','wetu-importer'); ?></a> <small><?php _e('If you already have accommodation, you can "connect" it with its WETU counter part, so it works with the importer.','wetu-importer'); ?></small></li>
98
		            	<?php if(class_exists('Lsx_Banners')){ ?>
99
		            		<li><a href="<?php echo admin_url('admin.php'); ?>?page=<?php echo $this->plugin_slug; ?>&tab=banners"><?php _e('Sync High Res Banner Images','wetu-importer'); ?></a></li>
100
		            	<?php } ?>
101
		            </ul> 
102
	             	            
103
            <?php } else {
104
            	do_action('lsx_tour_importer_admin_tab_'.$_GET['tab']);
105
            } ?>
106
        </div>
107
        <?php
108
	}
109
110
	/**
111
	 * The header of the item list
112
	 */
113
	public function table_header() {
114
	?>
115
		<thead>
116
			<tr>
117
				<th style="" class="manage-column column-cb check-column" id="cb" scope="col">
118
					<label for="cb-select-all-1" class="screen-reader-text">Select All</label>
119
					<input type="checkbox" id="cb-select-all-1">
120
				</th>
121
				<th style="" class="manage-column column-title " id="title" style="width:50%;" scope="col">Title</th>
122
				<th style="" class="manage-column column-date" id="date" scope="col">Date</th>
123
				<th style="" class="manage-column column-ssid" id="ssid" scope="col">WETU ID</th>
124
			</tr>
125
		</thead>
126
	<?php 
127
	}	
128
129
	/**
130
	 * The footer of the item list
131
	 */
132
	public function table_footer() {
133
	?>
134
		<tfoot>
135
			<tr>
136
				<th style="" class="manage-column column-cb check-column" id="cb" scope="col">
137
					<label for="cb-select-all-1" class="screen-reader-text">Select All</label>
138
					<input type="checkbox" id="cb-select-all-1">
139
				</th>
140
				<th style="" class="manage-column column-title" scope="col">Title</th>
141
				<th style="" class="manage-column column-date" scope="col">Date</th>
142
				<th style="" class="manage-column column-ssid" scope="col">WETU ID</th>
143
			</tr>
144
		</tfoot>
145
	<?php 
146
	}
147
148
	/**
149
	 * set_taxonomy with some terms
150
	 */
151
	public function set_taxonomy($taxonomy,$terms,$id) {
0 ignored issues
show
Unused Code introduced by
The parameter $terms 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...
152
        $result=array();
153
        if(!empty($data))
0 ignored issues
show
Bug introduced by
The variable $data seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
154
        {
155
            foreach($data as $k)
156
            {
157
                if($id)
158
                {
159
                    if(!$term = term_exists(trim($k), $tax))
160
                    {
161
                        $term = wp_insert_term(trim($k), $tax);
0 ignored issues
show
Bug introduced by
The variable $tax does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
162
                        if ( is_wp_error($term) )
163
                        {
164
                            echo $term->get_error_message();
165
                        }
166
                        else
167
                        {
168
                            wp_set_object_terms( $id, intval($term['term_id']), $taxonomy,true);
169
                        }
170
                    }
171
                    else
172
                    {
173
                        wp_set_object_terms( $id, intval($term['term_id']), $taxonomy,true);
174
                    }
175
                }
176
                else
177
                {
178
                    $result[]=trim($k);
179
                }
180
            }
181
        }
182
        return $result;
183
	}
184
185
	/**
186
	 * set_taxonomy with some terms
187
	 */
188
	public function team_member_checkboxes($selected=array()) {
189
		if(post_type_exists('team')) { ?>
190
    		<ul>
191
    			<?php
192
    				$team_args=array(
193
    					'post_type'	=>	'team',
194
    					'post_status' => 'publish',
195
    					'nopagin' => true,
196
    					'fields' => 'ids'
197
    				);
198
    				$team_members = new WP_Query($team_args);
199
    					if($team_members->have_posts()){
200
    						foreach($team_members->posts as $member){ ?>
201
    							<li><input class="team" <?php $this->checked($selected,$member); ?> type="checkbox" value="<?php echo $member; ?>" /> <?php echo get_the_title($member); ?></li>
0 ignored issues
show
Documentation introduced by
$selected is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
202
    						<?php }
203
    					}else{ ?>
204
    							<li><input class="team" type="checkbox" value="0" /> <?php _e('None','wetu-importer'); ?></li>
205
    					<?php }
206
    				?>
207
    		</ul>
208
    	<?php }		
209
	}
210
211
	/**
212
	 * set_taxonomy with some terms
213
	 */
214
	public function taxonomy_checkboxes($taxonomy=false,$selected=array()) {
215
		$return = '';
216
		if(false !== $taxonomy){
217
			$return .= '<ul>';
218
			$terms = get_terms(array('taxonomy'=>$taxonomy,'hide_empty'=>false));
219
220
			if(!is_wp_error($terms)){
221
				foreach($terms as $term){
222
					$return .= '<li><input class="'.$taxonomy.'" '.$this->checked($selected,$term->term_id,false).' type="checkbox" value="'.$term->term_id.'" /> '.$term->name.'</li>';
0 ignored issues
show
Documentation introduced by
$selected is of type array, 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...
223
				}
224
			}else{
225
				$return .= '<li><input type="checkbox" value="" /> '.__('None','wetu-importer').'</li>';
226
			}
227
			$return .= '</ul>';
228
		}
229
		return $return;		
230
	}
231
232
	/**
233
	 * Saves the room data
234
	 */
235
	public function save_custom_field($value=false,$meta_key,$id,$decrease=false,$unique=true) {
236
		if(false !== $value){
237
			if(false !== $decrease){
238
				$value = intval($value);
239
				$value--;
240
			}
241
			$prev = get_post_meta($id,$meta_key,true);
242
243
			if(false !== $id && '0' !== $id && false !== $prev && true === $unique){
244
				update_post_meta($id,$meta_key,$value,$prev);
245
			}else{
246
				add_post_meta($id,$meta_key,$value,$unique);
247
			}
248
		}
249
	}
250
251
	/**
252
	 * grabs any attachments for the current item
253
	 */
254
	public function find_attachments($id=false) {
255
		if(false !== $id){
256
			if(empty($this->found_attachments)){
257
258
		    	$attachments_args = array(
259
		    			'post_parent' => $id,
260
		    			'post_status' => 'inherit',
261
		    			'post_type' => 'attachment',
262
		    			'order' => 'ASC',
263
                        'nopagin' => 'true',
264
                        'posts_per_page' => '-1'
265
		    	);   	
266
		    	 
267
		    	$attachments = new WP_Query($attachments_args);
268
		    	if($attachments->have_posts()){
269
		    		foreach($attachments->posts as $attachment){
270
		    			$this->found_attachments[$attachment->ID] = str_replace(array('.jpg','.png','.jpeg'),'',$attachment->post_title);
271
		    			$this->gallery_meta[] = $attachment->ID;
272
		    		}
273
		    	}
274
			}
275
		}
276
	}
277
278
	/**
279
	 * Checks to see if an item is checked.
280
     *
281
     * @param $haystack array|string
282
     * @param $needle string
283
     * @param $echo bool
284
	 */
285 View Code Duplication
	public function checked($haystack=false,$needle='',$echo=true) {
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...
286
	    $return = $this->itemd($haystack,$needle,'checked');
287
	    if('' !== $return) {
288
			if (true === $echo) {
289
				echo $return;
290
			} else {
291
				return $return;
292
			}
293
		}
294
	}
295
296
	/**
297
	 * Checks to see if an item is checked.
298
	 *
299
	 * @param $haystack array|string
300
	 * @param $needle string
301
     * @param $echo bool
302
	 */
303 View Code Duplication
	public function selected($haystack=false,$needle='',$echo=true) {
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...
304
		$return = $this->itemd($haystack,$needle,'selected');
305
		if('' !== $return) {
306
			if (true === $echo) {
307
				echo $return;
308
			} else {
309
				return $return;
310
			}
311
		}
312
	}
313
314
	/**
315
	 * Checks to see if an item is selected. If $echo is false,  it will return the $type if conditions are true.
316
	 *
317
	 * @param $haystack array|string
318
	 * @param $needle string
319
     * @param $type string
320
     * @param $wrap bool
321
     * @return $html string
0 ignored issues
show
Documentation introduced by
The doc-type $html could not be parsed: Unknown type name "$html" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
322
	 */
323
	public function itemd($haystack=false,$needle='',$type='',$wrap=true) {
324
		$html = '';
325
		if('' !== $type) {
326
			if (!is_array($haystack)) {
327
				$haystack = array($haystack);
328
			}
329
			if (in_array($needle, $haystack)) {
330
			    if(true === $wrap || 'true' === $wrap) {
331
					$html = $type . '"' . $type . '"';
332
				}else{
333
					$html = $type;
334
                }
335
			}
336
		}
337
        return $html;
338
339
	}
340
341
	/**
342
	 * Displays the importers navigation
343
	 *
344
	 * @param $tab string
345
	 */
346
	public function navigation($tab='') {
347
        $post_types = array(
348
            'tour'              => esc_attr('Tours','wetu-importer'),
349
            'accommodation'     => esc_attr('Accommodation','wetu-importer'),
350
            'destination'       => esc_attr('Destinations','wetu-importer'),
351
        );
352
353
354
		echo '<div class="wet-navigation"><div class="subsubsub"><a class="'.$this->itemd($tab,'','current',false).'" href="'.admin_url('admin.php').'?page='.$this->plugin_slug.'">'.esc_attr('Home','wetu-importer').'</a>';
0 ignored issues
show
Documentation introduced by
$tab 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...
355
		foreach($post_types as $post_type => $label){
356
		    echo ' | <a class="'.$this->itemd($tab,$post_type,'current',false).'" href="'.admin_url('admin.php').'?page='.$this->plugin_slug.'&tab='.$post_type.'">'.$label.'</a>';
0 ignored issues
show
Documentation introduced by
$tab 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...
357
        }
358
        echo '</div><br clear="both"/></div>';
359
	}
360
}
361
$wetu_importer_admin = new WETU_Importer_Admin();