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

WETU_Importer_Admin::checked()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 2
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 = '';
65
		} else {
66
			$min = '.min';
67
		}
68
69
		if(is_admin() && isset($_GET['page']) && $this->plugin_slug === $_GET['page']){
70
			wp_enqueue_script( 'wetu-importers-script', WETU_IMPORTER_URL . 'assets/js/wetu-importer' . $min . '.js', array( 'jquery' ), WETU_IMPORTER_VER, true );
71
			wp_localize_script( 'wetu-importers-script', 'lsx_tour_importer_params', array(
72
			'ajax_url' => admin_url('admin-ajax.php'),
73
			) );			
74
		}
75
	}			
76
77
	/**
78
	 * Display the importer administration screen
79
	 */
80
	public function display_page() {
81
        ?>
82
        <div class="wrap">
83
            <?php screen_icon(); ?>
84
85
            <?php if(!isset($_GET['tab'])){ ?>
86
	            <h2><?php _e('Welcome to the LSX Wetu Importer','wetu-importer'); ?></h2>
87
	            <p>If this is the first time you are running the import, then follow the steps below.</p>
88
	            <ul>
89
                    <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>
90
	            	<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>
91
                    <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>
92
	            </ul>
93
94
		            <h3><?php _e('Additional Tools','wetu-importer'); ?></h3>
95
		            <ul>
96
		            	<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>
97
		            	<?php if(class_exists('Lsx_Banners')){ ?>
98
		            		<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>
99
		            	<?php } ?>
100
		            </ul> 
101
	             	            
102
            <?php } else {
103
            	do_action('lsx_tour_importer_admin_tab_'.$_GET['tab']);
104
            } ?>
105
        </div>
106
        <?php
107
	}
108
109
	/**
110
	 * The header of the item list
111
	 */
112
	public function table_header() {
113
	?>
114
		<thead>
115
			<tr>
116
				<th style="" class="manage-column column-cb check-column" id="cb" scope="col">
117
					<label for="cb-select-all-1" class="screen-reader-text">Select All</label>
118
					<input type="checkbox" id="cb-select-all-1">
119
				</th>
120
				<th style="" class="manage-column column-title " id="title" style="width:50%;" scope="col">Title</th>
121
				<th style="" class="manage-column column-date" id="date" scope="col">Date</th>
122
				<th style="" class="manage-column column-ssid" id="ssid" scope="col">WETU ID</th>
123
			</tr>
124
		</thead>
125
	<?php 
126
	}	
127
128
	/**
129
	 * The footer of the item list
130
	 */
131
	public function table_footer() {
132
	?>
133
		<tfoot>
134
			<tr>
135
				<th style="" class="manage-column column-cb check-column" id="cb" scope="col">
136
					<label for="cb-select-all-1" class="screen-reader-text">Select All</label>
137
					<input type="checkbox" id="cb-select-all-1">
138
				</th>
139
				<th style="" class="manage-column column-title" scope="col">Title</th>
140
				<th style="" class="manage-column column-date" scope="col">Date</th>
141
				<th style="" class="manage-column column-ssid" scope="col">WETU ID</th>
142
			</tr>
143
		</tfoot>
144
	<?php 
145
	}
146
147
	/**
148
	 * set_taxonomy with some terms
149
	 */
150
	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...
151
        $result=array();
152
        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...
153
        {
154
            foreach($data as $k)
155
            {
156
                if($id)
157
                {
158
                    if(!$term = term_exists(trim($k), $tax))
159
                    {
160
                        $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...
161
                        if ( is_wp_error($term) )
162
                        {
163
                            echo $term->get_error_message();
164
                        }
165
                        else
166
                        {
167
                            wp_set_object_terms( $id, intval($term['term_id']), $taxonomy,true);
168
                        }
169
                    }
170
                    else
171
                    {
172
                        wp_set_object_terms( $id, intval($term['term_id']), $taxonomy,true);
173
                    }
174
                }
175
                else
176
                {
177
                    $result[]=trim($k);
178
                }
179
            }
180
        }
181
        return $result;
182
	}
183
184
	/**
185
	 * set_taxonomy with some terms
186
	 */
187
	public function team_member_checkboxes($selected=array()) {
188
		if(post_type_exists('team')) { ?>
189
    		<ul>
190
    			<?php
191
    				$team_args=array(
192
    					'post_type'	=>	'team',
193
    					'post_status' => 'publish',
194
    					'nopagin' => true,
195
    					'fields' => 'ids'
196
    				);
197
    				$team_members = new WP_Query($team_args);
198
    					if($team_members->have_posts()){
199
    						foreach($team_members->posts as $member){ ?>
200
    							<li><input class="team" checked="<?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...
201
    						<?php }
202
    					}else{ ?>
203
    							<li><input class="team" type="checkbox" value="0" /> <?php _e('None','wetu-importer'); ?></li>
204
    					<?php }
205
    				?>
206
    		</ul>
207
    	<?php }		
208
	}
209
210
	/**
211
	 * set_taxonomy with some terms
212
	 */
213
	public function taxonomy_checkboxes($taxonomy=false) {
214
		$return = '';
215
		if(false !== $taxonomy){
216
			$return .= '<ul>';
217
			$terms = get_terms($taxonomy,array('empty'=>true));
218
			if(!is_wp_error($terms)){
219
				foreach($terms as $term){
220
					$return .= '<li><input class="'.$taxonomy.'" type="checkbox" value="'.$term->term_id.'" /> '.$term->name.'</li>';
221
				}
222
			}else{
223
				$return .= '<li><input type="checkbox" value="" /> '.__('None','wetu-importer').'</li>';
224
			}
225
			$return .= '</ul>';
226
		}
227
		return $return;		
228
	}
229
230
	/**
231
	 * Saves the room data
232
	 */
233
	public function save_custom_field($value=false,$meta_key,$id,$decrease=false,$unique=true) {
234
		if(false !== $value){
235
			if(false !== $decrease){
236
				$value = intval($value);
237
				$value--;
238
			}
239
			$prev = get_post_meta($id,$meta_key,true);
240
241
			if(false !== $id && '0' !== $id && false !== $prev && true === $unique){
242
				update_post_meta($id,$meta_key,$value,$prev);
243
			}else{
244
				add_post_meta($id,$meta_key,$value,$unique);
245
			}
246
		}
247
	}
248
249
	/**
250
	 * grabs any attachments for the current item
251
	 */
252
	public function find_attachments($id=false) {
253
		if(false !== $id){
254
			if(empty($this->found_attachments)){
255
256
		    	$attachments_args = array(
257
		    			'post_parent' => $id,
258
		    			'post_status' => 'inherit',
259
		    			'post_type' => 'attachment',
260
		    			'order' => 'ASC',
261
		    	);   	
262
		    	 
263
		    	$attachments = new WP_Query($attachments_args);
264
		    	if($attachments->have_posts()){
265
		    		foreach($attachments->posts as $attachment){
266
		    			$this->found_attachments[$attachment->ID] = str_replace(array('.jpg','.png','.jpeg'),'',$attachment->post_title);
267
		    			$this->gallery_meta[] = $attachment->ID;
268
		    		}
269
		    	}
270
			}
271
		}
272
	}
273
274
	/**
275
	 * Checks to see if an item is selected.
276
     *
277
     * @param $haystack array|string
278
     * @param $needle string
279
     * @return string
280
	 */
281
	public function checked($haystack=false,$needle='') {
282
	    $return = '';
283
	    if(!is_array($haystack)){
284
			$haystack = array($haystack);
285
        }
286
        if(in_array($needle,$haystack)){
287
			$return = 'checked';
288
        }
289
	    return $return;
290
	}
291
}
292
$wetu_importer_admin = new WETU_Importer_Admin();