Passed
Push — master ( 5040b2...ef5e8d )
by Warwick
01:52
created

LSX_WETU_Importer_Settings   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 382
Duplicated Lines 5.5 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 21
loc 382
rs 5.5199
c 0
b 0
f 0
wmc 56
lcom 2
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 1
A get_instance() 0 7 2
F display_page() 21 294 48
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_descriptions' => '',
52
			'disable_accommodation_excerpts'     => '',
53
			'disable_destination_descriptions'   => '',
54
			'image_replacing'                    => 'on',
55
			'image_limit'                        => '15',
56
			'image_scaling'                      => 'on',
57
			'width'                              => '800',
58
			'height'                             => '600',
59
			'scaling'                            => 'h',
60
			'enable_tour_ref_column'             => '',
61
		);
62
		$this->fields   = array_keys( $this->defaults );
63
		add_action( 'admin_init', array( $this, 'save_options' ) );
64
	}
65
66
	/**
67
	 * Return an instance of this class.
68
	 *
69
	 * @return  object
70
	 */
71
	public static function get_instance() {
72
		// If the single instance hasn't been set, set it now.
73
		if ( ! isset( self::$instance ) ) {
74
			self::$instance = new self();
75
		}
76
		return self::$instance;
77
	}
78
79
	/**
80
	 * Display the importer welcome screen
81
	 */
82
	public function display_page() {
83
		$options = lsx_wetu_get_options();
84
		foreach ( $options as $key => $value ) {
85
			$value = trim( $value );
86
		}
87
		$options = wp_parse_args( $options, $this->defaults );
88
		?>
89
		<div class="wrap">
90
			<form method="post" class="">
91
				<?php wp_nonce_field( 'lsx_wetu_importer_save', 'lsx_wetu_importer_save_options' ); ?>
92
				<h1><?php esc_html_e( 'General', 'lsx-wetu-importer' ); ?></h1>
93
				<table class="form-table">
94
					<tbody>
95
						<tr class="form-field">
96
							<th scope="row">
97
								<label for="wetu_api_key"> <?php esc_html_e( 'API Key', 'lsx-wetu-importer' ); ?></label>
98
							</th>
99
							<td>
100
								<input type="text" value="<?php
101
								if ( isset( $options['api_key'] ) ) {
102
									echo esc_attr( $options['api_key'] );
103
								}
104
								?>" name="api_key" />
105
							</td>
106
						</tr>					
107
					</tbody>
108
				</table>
109
110
				<h1><?php esc_html_e( 'Tours', 'lsx-wetu-importer' ); ?></h1>
111
				<table class="form-table">
112
					<tbody>
113
					<tr class="form-field -wrap">
114
							<th scope="row">
115
								<label for="disable_tour_title"><?php esc_html_e( 'Enable Custom Titles', 'lsx-wetu-importer' ); ?></label>
116
							</th>
117
							<td>
118
								<input type="checkbox"
119
								<?php
120
								if ( isset( $options['disable_tour_title'] ) && '' !== $options['disable_tour_title'] ) {
121
									echo esc_attr( 'checked="checked"' );
122
								}
123
								?>
124
								name="disable_tour_title" />
125
126
								<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>
127
							</td>
128
						</tr>
129
						<tr class="form-field -wrap">
130
							<th scope="row">
131
								<label for="disable_tour_descriptions"><?php esc_html_e( 'Disable Descriptions', 'lsx-wetu-importer' ); ?></label>
132
							</th>
133
							<td>
134
								<input type="checkbox"
135
								<?php
136
								if ( isset( $options['disable_tour_descriptions'] ) && '' !== $options['disable_tour_descriptions'] ) {
137
									echo esc_attr( 'checked="checked"' );
138
								}
139
								?>
140
								name="disable_tour_descriptions" />
141
142
								<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>
143
							</td>
144
						</tr>
145
						<tr class="form-field -wrap">
146
							<th scope="row">
147
								<label for="disable_tour_tags"><?php esc_html_e( 'Disable Tags / Travel Styles', 'lsx-wetu-importer' ); ?></label>
148
							</th>
149
							<td>
150
								<input type="checkbox"
151
								<?php
152
								if ( isset( $options['disable_tour_tags'] ) && '' !== $options['disable_tour_tags'] ) {
153
									echo esc_attr( 'checked="checked"' );
154
								}
155
								?>
156
								name="disable_tour_tags" />
157
158
								<small><?php esc_html_e( 'Disable this is you dont want the option available on the import screen.', 'lsx-wetu-importer' ); ?></small>
159
							</td>
160
						</tr>
161
162
						<tr class="form-field -wrap">
163
							<th scope="row">
164
								<label for="enable_tour_ref_column"><?php esc_html_e( 'Enable Reference Column', 'lsx-wetu-importer' ); ?></label>
165
							</th>
166
							<td>
167
								<input type="checkbox"
168
								<?php
169
								if ( isset( $options['enable_tour_ref_column'] ) && '' !== $options['enable_tour_ref_column'] ) {
170
									echo esc_attr( 'checked="checked"' );
171
								}
172
								?>
173
								name="enable_tour_ref_column" />
174
								<small><?php esc_html_e( 'Enables the use of the WETU Reference Column for better tours management.', 'lsx-wetu-importer' ); ?></small>
175
							</td>
176
						</tr>						
177
					</tbody>
178
				</table>
179
180
				<h1><?php esc_html_e( 'Accommodation', 'lsx-wetu-importer' ); ?></h1>
181
182
				<table class="form-table">
183
					<tbody>
184
						<tr class="form-field -wrap">
185
							<th scope="row">
186
								<label for="disable_accommodation_descriptions"><?php esc_html_e( 'Disable Accommodation Descriptions', 'lsx-wetu-importer' ); ?></label>
187
							</th>
188
							<td>
189
								<input type="checkbox"
190
								<?php
191
								if ( isset( $options['disable_accommodation_descriptions'] ) && '' !== $options['disable_accommodation_descriptions'] ) {
192
									echo esc_attr( 'checked="checked"' );
193
								}
194
								?>
195
								name="disable_accommodation_descriptions" />
196
								<small><?php esc_html_e( 'If you are going to edit the accommodation descriptions imported then enable this setting.', 'lsx-wetu-importer' ); ?></small>
197
							</td>
198
						</tr>
199
						<tr class="form-field -wrap">
200
							<th scope="row">
201
								<label for="disable_accommodation_excerpts"><?php esc_html_e( 'Disable Accommodation Excerpts', 'lsx-wetu-importer' ); ?></label>
202
							</th>
203
							<td>
204
								<input type="checkbox"
205
								<?php
206
								if ( isset( $options['disable_accommodation_excerpts'] ) && '' !== $options['disable_accommodation_excerpts'] ) {
207
									echo esc_attr( 'checked="checked"' );
208
								}
209
								?>
210
								name="disable_accommodation_excerpts" />
211
								<small><?php esc_html_e( 'If you are going to edit the accommodation excerpts then enable this setting.', 'lsx-wetu-importer' ); ?></small>
212
							</td>
213
						</tr>					
214
					</tbody>
215
				</table>
216
217
				<h1><?php esc_html_e( 'Destinations', 'lsx-wetu-importer' ); ?></h1>
218
219
				<table class="form-table">
220
					<tbody>
221
						<tr class="form-field -wrap">
222
							<th scope="row">
223
								<label for="disable_destination_descriptions"><?php esc_html_e( 'Disable Destinations Descriptions', 'lsx-wetu-importer' ); ?></label>
224
							</th>
225
							<td>
226
								<input type="checkbox"
227
								<?php
228
								if ( isset( $options['disable_destination_descriptions'] ) && '' !== $options['disable_destination_descriptions'] ) {
229
									echo esc_attr( 'checked="checked"' );
230
								}
231
								?>
232
								name="disable_destination_descriptions" />
233
								<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>
234
							</td>
235
						</tr>						
236
					</tbody>
237
				</table>
238
239
				<h1><?php esc_html_e( 'Images', 'lsx-wetu-importer' ); ?></h1>
240
241
				<table class="form-table">
242
					<tbody>
243
						<tr class="form-field -wrap">
244
							<th scope="row">
245
								<label for="image_replacing"><?php esc_html_e( 'Replace Images', 'lsx-wetu-importer' ); ?></label>
246
							</th>
247
							<td>
248
								<input type="checkbox"
249
								<?php
250
								if ( isset( $options['image_replacing'] ) && '' !== $options['image_replacing'] ) {
251
									echo esc_attr( 'checked="checked"' );
252
								}
253
								?>
254
								name="image_replacing" />
255
								<p><?php esc_html_e( 'Do you want your images to be replaced on each import.', 'lsx-wetu-importer' ); ?></p>
256
							</td>
257
						</tr>
258
						<tr class="form-field -wrap">
259
							<th scope="row">
260
								<label for="image_limit"> <?php esc_html_e( 'Limit the amount of images imported to the gallery', 'lsx-wetu-importer' ); ?></label>
261
							</th>
262
							<td>
263
								<input placeholder="" type="text" value="<?php
264
								if ( isset( $options['image_limit'] ) && '' !== $options['image_limit'] ) {
265
									echo esc_attr( $options['image_limit'] );
266
								}
267
								?>"
268
								name="image_limit" />
269
							</td>
270
						</tr>
271
272
						<tr class="form-field -wrap">
273
							<th scope="row">
274
								<label for="image_scaling"><?php esc_html_e( 'Enable Image Scaling', 'lsx-wetu-importer' ); ?></label>
275
							</th>
276
							<td>
277
								<input type="checkbox"
278
								<?php
279
								if ( isset( $options['image_scaling'] ) && '' !== $options['image_scaling'] ) {
280
									echo esc_attr( 'checked="checked"' );
281
								}
282
								?>
283
								name="image_scaling" />
284
							</td>
285
						</tr>
286
						<tr class="form-field -wrap">
287
							<th scope="row">
288
								<label for="width"> <?php esc_html_e( 'Width (px)', 'lsx-wetu-importer' ); ?></label>
289
							</th>
290
							<td>
291
								<input placeholder="800" type="text" value="<?php
292
								if ( isset( $options['width'] ) && '' !== $options['width'] ) {
293
									echo esc_attr( $options['width'] );
294
								}
295
								?>"
296
								name="width" />
297
							</td>
298
						</tr>
299
						<tr class="form-field -wrap">
300
							<th scope="row">
301
								<label for="height"> <?php esc_html_e( 'Height (px)', 'lsx-wetu-importer' ); ?></label>
302
							</th>
303
							<td>
304
								<input placeholder="600" type="text" value="<?php
305
								if ( isset( $options['height'] ) && '' !== $options['height'] ) {
306
									echo esc_attr( $options['height'] );
307
								}
308
								?>"
309
								name="height" />
310
							</td>
311
						</tr>
312
313
						<tr class="form-field -wrap">
314
							<th scope="row">
315
								<label for="scaling"> <?php esc_html_e( 'Scaling', 'lsx-wetu-importer' ); ?></label>
316
							</th>
317
							<td>
318
								<input type="radio"
319
								<?php
320 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...
321
									echo esc_attr( 'checked="checked"' );
322
								}
323
								?>
324
								name="scaling" value="raw" /> <?php esc_html_e( 'Get the Full size image, no cropping takes place.', 'lsx-wetu-importer' ); ?><br />
325
								<input type="radio"
326
								<?php
327 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...
328
									echo esc_attr( 'checked="checked"' );
329
								}
330
								?>
331
								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 />
332
								<input type="radio"
333
								<?php
334 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...
335
									echo esc_attr( 'checked="checked"' );
336
								}
337
								?>
338
								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 />
339
								<input type="radio"
340
								<?php
341 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...
342
									echo esc_attr( 'checked="checked"' );
343
								}
344
								?>
345
								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 />
346
								<input type="radio"
347
								<?php
348 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...
349
									echo esc_attr( 'checked="checked"' );
350
								}
351
								?>
352
								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 />
353
								<input type="radio"
354
								<?php
355 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...
356
									echo esc_attr( 'checked="checked"' );
357
								}
358
								?>
359
								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 />
360
								<input type="radio"
361
								<?php
362 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...
363
									echo esc_attr( 'checked="checked"' );
364
								}
365
								?>
366
								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' ); ?>
367
							</td>
368
						</tr>
369
					</tbody>
370
				</table>
371
				<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>
372
			</form>
373
		</div>
374
		<?php
375
	}
376
377
	/**
378
	 * Save the options fields
379
	 *
380
	 * @return void
381
	 */
382
	public function save_options() {
383
		if ( ! isset( $_POST['lsx_wetu_importer_save_options'] ) || ! wp_verify_nonce( $_POST['lsx_wetu_importer_save_options'], 'lsx_wetu_importer_save' ) ) {
384
			return;
385
		}
386
		$data_to_save = array();
387
		foreach ( $this->defaults as $key => $field ) {
388
			if ( isset( $_POST[ $key ] ) ) {
389
				$data_to_save[ $key ] = sanitize_text_field( $_POST[ $key ] );
390
			} else {
391
				$data_to_save[ $key ] = '';
392
			}
393
		}
394
		update_option( 'lsx_wetu_importer_settings', $data_to_save );
395
	}
396
}
397