Passed
Push — master ( ef5e8d...74af01 )
by Warwick
01:53
created

LSX_WETU_Importer_Settings   F

Complexity

Total Complexity 60

Size/Duplication

Total Lines 416
Duplicated Lines 5.05 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 21
loc 416
rs 3.6
c 0
b 0
f 0
wmc 60
lcom 2
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 1
A get_instance() 0 7 2
F display_page() 21 326 52
A save_options() 0 14 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

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

Common duplication problems, and corresponding solutions are:

Complex Class

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

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

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

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

1
<?php
2
/**
3
 * The Settings Screen for the Importer Plugin
4
 *
5
 * @package   lsx_wetu_importer
6
 * @author    LightSpeed
7
 * @license   GPL-2.0+
8
 * @link
9
 * @copyright 2019 LightSpeed
10
 **/
11
12
/**
13
 * The Welcome Screen for the Importer Plugin
14
 */
15
class LSX_WETU_Importer_Settings {
16
17
	/**
18
	 * Holds instance of the class
19
	 *
20
	 * @var object
21
	 */
22
	private static $instance;
23
24
	/**
25
	 * Holds the default settings.
26
	 *
27
	 * @var array
28
	 */
29
	public $defaults = array();
30
31
	/**
32
	 * Holds the settings fields available.
33
	 *
34
	 * @var array
35
	 */
36
	public $fields = array();
37
38
	/**
39
	 * Initialize the plugin by setting localization, filters, and administration functions.
40
	 *
41
	 * @since 1.0.0
42
	 *
43
	 * @access private
44
	 */
45
	public function __construct() {
46
		$this->defaults = array(
47
			'api_key'                            => '',
48
			'disable_tour_title'                 => '',
49
			'disable_tour_descriptions'          => '',
50
			'disable_tour_tags'                  => 'on',
51
			'disable_accommodation_title'        => '',
52
			'disable_accommodation_descriptions' => '',
53
			'disable_accommodation_excerpts'     => '',
54
			'disable_destination_title'          => '',
55
			'disable_destination_descriptions'   => '',
56
			'image_replacing'                    => 'on',
57
			'image_limit'                        => '15',
58
			'image_scaling'                      => 'on',
59
			'width'                              => '800',
60
			'height'                             => '600',
61
			'scaling'                            => 'h',
62
			'enable_tour_ref_column'             => '',
63
		);
64
		$this->fields   = array_keys( $this->defaults );
65
		add_action( 'admin_init', array( $this, 'save_options' ) );
66
	}
67
68
	/**
69
	 * Return an instance of this class.
70
	 *
71
	 * @return  object
72
	 */
73
	public static function get_instance() {
74
		// If the single instance hasn't been set, set it now.
75
		if ( ! isset( self::$instance ) ) {
76
			self::$instance = new self();
77
		}
78
		return self::$instance;
79
	}
80
81
	/**
82
	 * Display the importer welcome screen
83
	 */
84
	public function display_page() {
85
		$options = lsx_wetu_get_options();
86
		foreach ( $options as $key => $value ) {
87
			$value = trim( $value );
88
		}
89
		$options = wp_parse_args( $options, $this->defaults );
90
		?>
91
		<div class="wrap">
92
			<form method="post" class="">
93
				<?php wp_nonce_field( 'lsx_wetu_importer_save', 'lsx_wetu_importer_save_options' ); ?>
94
				<h1><?php esc_html_e( 'General', 'lsx-wetu-importer' ); ?></h1>
95
				<table class="form-table">
96
					<tbody>
97
						<tr class="form-field">
98
							<th scope="row">
99
								<label for="wetu_api_key"> <?php esc_html_e( 'API Key', 'lsx-wetu-importer' ); ?></label>
100
							</th>
101
							<td>
102
								<input type="text" value="<?php
103
								if ( isset( $options['api_key'] ) ) {
104
									echo esc_attr( $options['api_key'] );
105
								}
106
								?>" name="api_key" />
107
							</td>
108
						</tr>					
109
					</tbody>
110
				</table>
111
112
				<h1><?php esc_html_e( 'Tours', 'lsx-wetu-importer' ); ?></h1>
113
				<table class="form-table">
114
					<tbody>
115
						<tr class="form-field -wrap">
116
							<th scope="row">
117
								<label for="disable_tour_title"><?php esc_html_e( 'Enable Custom Titles', 'lsx-wetu-importer' ); ?></label>
118
							</th>
119
							<td>
120
								<input type="checkbox"
121
								<?php
122
								if ( isset( $options['disable_tour_title'] ) && '' !== $options['disable_tour_title'] ) {
123
									echo esc_attr( 'checked="checked"' );
124
								}
125
								?>
126
								name="disable_tour_title" />
127
128
								<small><?php esc_html_e( 'If you are going to manage your tour descriptions on this site and not on WETU then enable this setting.', 'lsx-wetu-importer' ); ?></small>
129
							</td>
130
						</tr>
131
						<tr class="form-field -wrap">
132
							<th scope="row">
133
								<label for="disable_tour_descriptions"><?php esc_html_e( 'Disable Descriptions', 'lsx-wetu-importer' ); ?></label>
134
							</th>
135
							<td>
136
								<input type="checkbox"
137
								<?php
138
								if ( isset( $options['disable_tour_descriptions'] ) && '' !== $options['disable_tour_descriptions'] ) {
139
									echo esc_attr( 'checked="checked"' );
140
								}
141
								?>
142
								name="disable_tour_descriptions" />
143
144
								<small><?php esc_html_e( 'If you are going to manage your tour descriptions on this site and not on WETU then enable this setting.', 'lsx-wetu-importer' ); ?></small>
145
							</td>
146
						</tr>
147
						<tr class="form-field -wrap">
148
							<th scope="row">
149
								<label for="disable_tour_tags"><?php esc_html_e( 'Disable Tags / Travel Styles', 'lsx-wetu-importer' ); ?></label>
150
							</th>
151
							<td>
152
								<input type="checkbox"
153
								<?php
154
								if ( isset( $options['disable_tour_tags'] ) && '' !== $options['disable_tour_tags'] ) {
155
									echo esc_attr( 'checked="checked"' );
156
								}
157
								?>
158
								name="disable_tour_tags" />
159
160
								<small><?php esc_html_e( 'Disable this is you dont want the option available on the import screen.', 'lsx-wetu-importer' ); ?></small>
161
							</td>
162
						</tr>
163
164
						<tr class="form-field -wrap">
165
							<th scope="row">
166
								<label for="enable_tour_ref_column"><?php esc_html_e( 'Enable Reference Column', 'lsx-wetu-importer' ); ?></label>
167
							</th>
168
							<td>
169
								<input type="checkbox"
170
								<?php
171
								if ( isset( $options['enable_tour_ref_column'] ) && '' !== $options['enable_tour_ref_column'] ) {
172
									echo esc_attr( 'checked="checked"' );
173
								}
174
								?>
175
								name="enable_tour_ref_column" />
176
								<small><?php esc_html_e( 'Enables the use of the WETU Reference Column for better tours management.', 'lsx-wetu-importer' ); ?></small>
177
							</td>
178
						</tr>						
179
					</tbody>
180
				</table>
181
182
				<h1><?php esc_html_e( 'Accommodation', 'lsx-wetu-importer' ); ?></h1>
183
184
				<table class="form-table">
185
					<tbody>
186
						<tr class="form-field -wrap">
187
							<th scope="row">
188
								<label for="disable_accommodation_title"><?php esc_html_e( 'Enable Custom Titles', 'lsx-wetu-importer' ); ?></label>
189
							</th>
190
							<td>
191
								<input type="checkbox"
192
								<?php
193
								if ( isset( $options['disable_accommodation_title'] ) && '' !== $options['disable_accommodation_title'] ) {
194
									echo esc_attr( 'checked="checked"' );
195
								}
196
								?>
197
								name="disable_accommodation_title" />
198
199
								<small><?php esc_html_e( 'If you are going to manage your tour descriptions on this site and not on WETU then enable this setting.', 'lsx-wetu-importer' ); ?></small>
200
							</td>
201
						</tr>
202
						<tr class="form-field -wrap">
203
							<th scope="row">
204
								<label for="disable_accommodation_descriptions"><?php esc_html_e( 'Disable Descriptions', 'lsx-wetu-importer' ); ?></label>
205
							</th>
206
							<td>
207
								<input type="checkbox"
208
								<?php
209
								if ( isset( $options['disable_accommodation_descriptions'] ) && '' !== $options['disable_accommodation_descriptions'] ) {
210
									echo esc_attr( 'checked="checked"' );
211
								}
212
								?>
213
								name="disable_accommodation_descriptions" />
214
								<small><?php esc_html_e( 'If you are going to edit the accommodation descriptions imported then enable this setting.', 'lsx-wetu-importer' ); ?></small>
215
							</td>
216
						</tr>
217
						<tr class="form-field -wrap">
218
							<th scope="row">
219
								<label for="disable_accommodation_excerpts"><?php esc_html_e( 'Disable Excerpts', 'lsx-wetu-importer' ); ?></label>
220
							</th>
221
							<td>
222
								<input type="checkbox"
223
								<?php
224
								if ( isset( $options['disable_accommodation_excerpts'] ) && '' !== $options['disable_accommodation_excerpts'] ) {
225
									echo esc_attr( 'checked="checked"' );
226
								}
227
								?>
228
								name="disable_accommodation_excerpts" />
229
								<small><?php esc_html_e( 'If you are going to edit the accommodation excerpts then enable this setting.', 'lsx-wetu-importer' ); ?></small>
230
							</td>
231
						</tr>					
232
					</tbody>
233
				</table>
234
235
				<h1><?php esc_html_e( 'Destinations', 'lsx-wetu-importer' ); ?></h1>
236
237
				<table class="form-table">
238
					<tbody>
239
						<tr class="form-field -wrap">
240
							<th scope="row">
241
								<label for="disable_destination_title"><?php esc_html_e( 'Enable Custom Titles', 'lsx-wetu-importer' ); ?></label>
242
							</th>
243
							<td>
244
								<input type="checkbox"
245
								<?php
246
								if ( isset( $options['disable_destination_title'] ) && '' !== $options['disable_destination_title'] ) {
247
									echo esc_attr( 'checked="checked"' );
248
								}
249
								?>
250
								name="disable_destination_title" />
251
252
								<small><?php esc_html_e( 'If you are going to manage your tour descriptions on this site and not on WETU then enable this setting.', 'lsx-wetu-importer' ); ?></small>
253
							</td>
254
						</tr>
255
						<tr class="form-field -wrap">
256
							<th scope="row">
257
								<label for="disable_destination_descriptions"><?php esc_html_e( 'Disable Descriptions', 'lsx-wetu-importer' ); ?></label>
258
							</th>
259
							<td>
260
								<input type="checkbox"
261
								<?php
262
								if ( isset( $options['disable_destination_descriptions'] ) && '' !== $options['disable_destination_descriptions'] ) {
263
									echo esc_attr( 'checked="checked"' );
264
								}
265
								?>
266
								name="disable_destination_descriptions" />
267
								<small><?php esc_html_e( 'If you are going to edit the destination descriptions on this site then enable this setting.', 'lsx-wetu-importer' ); ?></small>
268
							</td>
269
						</tr>						
270
					</tbody>
271
				</table>
272
273
				<h1><?php esc_html_e( 'Images', 'lsx-wetu-importer' ); ?></h1>
274
275
				<table class="form-table">
276
					<tbody>
277
						<tr class="form-field -wrap">
278
							<th scope="row">
279
								<label for="image_replacing"><?php esc_html_e( 'Replace Images', 'lsx-wetu-importer' ); ?></label>
280
							</th>
281
							<td>
282
								<input type="checkbox"
283
								<?php
284
								if ( isset( $options['image_replacing'] ) && '' !== $options['image_replacing'] ) {
285
									echo esc_attr( 'checked="checked"' );
286
								}
287
								?>
288
								name="image_replacing" />
289
								<p><?php esc_html_e( 'Do you want your images to be replaced on each import.', 'lsx-wetu-importer' ); ?></p>
290
							</td>
291
						</tr>
292
						<tr class="form-field -wrap">
293
							<th scope="row">
294
								<label for="image_limit"> <?php esc_html_e( 'Limit the amount of images imported to the gallery', 'lsx-wetu-importer' ); ?></label>
295
							</th>
296
							<td>
297
								<input placeholder="" type="text" value="<?php
298
								if ( isset( $options['image_limit'] ) && '' !== $options['image_limit'] ) {
299
									echo esc_attr( $options['image_limit'] );
300
								}
301
								?>"
302
								name="image_limit" />
303
							</td>
304
						</tr>
305
306
						<tr class="form-field -wrap">
307
							<th scope="row">
308
								<label for="image_scaling"><?php esc_html_e( 'Enable Image Scaling', 'lsx-wetu-importer' ); ?></label>
309
							</th>
310
							<td>
311
								<input type="checkbox"
312
								<?php
313
								if ( isset( $options['image_scaling'] ) && '' !== $options['image_scaling'] ) {
314
									echo esc_attr( 'checked="checked"' );
315
								}
316
								?>
317
								name="image_scaling" />
318
							</td>
319
						</tr>
320
						<tr class="form-field -wrap">
321
							<th scope="row">
322
								<label for="width"> <?php esc_html_e( 'Width (px)', 'lsx-wetu-importer' ); ?></label>
323
							</th>
324
							<td>
325
								<input placeholder="800" type="text" value="<?php
326
								if ( isset( $options['width'] ) && '' !== $options['width'] ) {
327
									echo esc_attr( $options['width'] );
328
								}
329
								?>"
330
								name="width" />
331
							</td>
332
						</tr>
333
						<tr class="form-field -wrap">
334
							<th scope="row">
335
								<label for="height"> <?php esc_html_e( 'Height (px)', 'lsx-wetu-importer' ); ?></label>
336
							</th>
337
							<td>
338
								<input placeholder="600" type="text" value="<?php
339
								if ( isset( $options['height'] ) && '' !== $options['height'] ) {
340
									echo esc_attr( $options['height'] );
341
								}
342
								?>"
343
								name="height" />
344
							</td>
345
						</tr>
346
347
						<tr class="form-field -wrap">
348
							<th scope="row">
349
								<label for="scaling"> <?php esc_html_e( 'Scaling', 'lsx-wetu-importer' ); ?></label>
350
							</th>
351
							<td>
352
								<input type="radio"
353
								<?php
354 View Code Duplication
								if ( isset( $options['scaling'] ) && '' !== $options['scaling'] && 'raw' === $options['scaling'] ) {
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...
355
									echo esc_attr( 'checked="checked"' );
356
								}
357
								?>
358
								name="scaling" value="raw" /> <?php esc_html_e( 'Get the Full size image, no cropping takes place.', 'lsx-wetu-importer' ); ?><br />
359
								<input type="radio"
360
								<?php
361 View Code Duplication
								if ( isset( $options['scaling'] ) && '' !== $options['scaling'] && 'c' === $options['scaling'] ) {
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...
362
									echo esc_attr( 'checked="checked"' );
363
								}
364
								?>
365
								name="scaling"  value="c" /> <?php esc_html_e( 'Crop image to fit fully into the frame, Crop is taken from middle, preserving as much of the image as possible.', 'lsx-wetu-importer' ); ?><br />
366
								<input type="radio"
367
								<?php
368 View Code Duplication
								if ( isset( $options['scaling'] ) && '' !== $options['scaling'] && 'h' === $options['scaling'] ) {
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...
369
									echo esc_attr( 'checked="checked"' );
370
								}
371
								?>
372
								name="scaling"  value="h" /> <?php esc_html_e( 'Crop image to fit fully into the frame, but resize to height first, then crop on width if needed', 'lsx-wetu-importer' ); ?><br />
373
								<input type="radio"
374
								<?php
375 View Code Duplication
								if ( isset( $options['scaling'] ) && '' !== $options['scaling'] && 'w' === $options['scaling'] ) {
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...
376
									echo esc_attr( 'checked="checked"' );
377
								}
378
								?>
379
								name="scaling"  value="w" /> <?php esc_html_e( 'Crop image to fit fully into the frame, but resize to width first, then crop on height if needed', 'lsx-wetu-importer' ); ?><br />
380
								<input type="radio"
381
								<?php
382 View Code Duplication
								if ( isset( $options['scaling'] ) && '' !== $options['scaling'] && 'nf' === $options['scaling'] ) {
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...
383
									echo esc_attr( 'checked="checked"' );
384
								}
385
								?>
386
								name="scaling"  value="nf" /> <?php esc_html_e( 'Resize the image to fit within the frame. but pad the image with white to ensure the resolution matches the frame', 'lsx-wetu-importer' ); ?><br />
387
								<input type="radio"
388
								<?php
389 View Code Duplication
								if ( isset( $options['scaling'] ) && '' !== $options['scaling'] && 'n' === $options['scaling'] ) {
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...
390
									echo esc_attr( 'checked="checked"' );
391
								}
392
								?>
393
								name="scaling"  value="n" /> <?php esc_html_e( 'Resize the image to fit within the frame. but do not upscale the image.', 'lsx-wetu-importer' ); ?><br />
394
								<input type="radio"
395
								<?php
396 View Code Duplication
								if ( isset( $options['scaling'] ) && '' !== $options['scaling'] && 'W' === $options['scaling'] ) {
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...
397
									echo esc_attr( 'checked="checked"' );
398
								}
399
								?>
400
								name="scaling"  value="W" /> <?php esc_html_e( 'Resize the image to fit within the frame. Image will not exceed specified dimensions', 'lsx-wetu-importer' ); ?>
401
							</td>
402
						</tr>
403
					</tbody>
404
				</table>
405
				<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php esc_html_e( 'Save Changes', 'lsx-wetu-importer' ); ?>"></p>
406
			</form>
407
		</div>
408
		<?php
409
	}
410
411
	/**
412
	 * Save the options fields
413
	 *
414
	 * @return void
415
	 */
416
	public function save_options() {
417
		if ( ! isset( $_POST['lsx_wetu_importer_save_options'] ) || ! wp_verify_nonce( $_POST['lsx_wetu_importer_save_options'], 'lsx_wetu_importer_save' ) ) {
418
			return;
419
		}
420
		$data_to_save = array();
421
		foreach ( $this->defaults as $key => $field ) {
422
			if ( isset( $_POST[ $key ] ) ) {
423
				$data_to_save[ $key ] = sanitize_text_field( $_POST[ $key ] );
424
			} else {
425
				$data_to_save[ $key ] = '';
426
			}
427
		}
428
		update_option( 'lsx_wetu_importer_settings', $data_to_save );
429
	}
430
}
431