Completed
Push — master ( 02b793...7006ed )
by Warwick
10:41 queued 02:31
created

Settings   B

Complexity

Total Complexity 50

Size/Duplication

Total Lines 309
Duplicated Lines 6.8 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 21
loc 309
rs 8.4
c 0
b 0
f 0
wmc 50
lcom 2
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A get_instance() 0 7 2
F display_page() 21 224 42
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 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 Settings, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * The Settings Screen for the Importer Plugin
4
 *
5
 * @package   wetu_importer
6
 * @author    LightSpeed
7
 * @license   GPL-2.0+
8
 * @link
9
 * @copyright 2019 LightSpeed
10
 **/
11
12
namespace wetu_importer\classes;
13
14
/**
15
 * The Welcome Screen for the Importer Plugin
16
 */
17
class Settings {
18
19
	/**
20
	 * Holds instance of the class
21
	 *
22
	 * @var object
23
	 */
24
	private static $instance;
25
26
	/**
27
	 * Holds the default settings.
28
	 *
29
	 * @var array
30
	 */
31
	public $defaults = array();
32
33
	/**
34
	 * Holds the settings fields available.
35
	 *
36
	 * @var array
37
	 */
38
	public $fields = array();
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
		$this->defaults = array(
49
			'api_key'                            => '',
50
			'disable_tour_descriptions'          => '',
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
		);
61
		$this->fields   = array_keys( $this->defaults );
62
		add_action( 'admin_init', array( $this, 'save_options' ) );
63
	}
64
65
	/**
66
	 * Return an instance of this class.
67
	 *
68
	 * @return  object
69
	 */
70
	public static function get_instance() {
71
		// If the single instance hasn't been set, set it now.
72
		if ( ! isset( self::$instance ) ) {
73
			self::$instance = new self();
74
		}
75
		return self::$instance;
76
	}
77
78
	/**
79
	 * Display the importer welcome screen
80
	 */
81
	public function display_page() {
82
		$options = \wetu_importer\includes\helpers\get_options();
83
		foreach ( $options as $key => $value ) {
84
			$value = trim( $value );
85
		}
86
		$options = wp_parse_args( $options, $this->defaults );
87
		?>
88
		<div class="wrap">
89
			<form method="post" class="">
90
				<?php wp_nonce_field( 'wetu_importer_save', 'wetu_importer_save_options' ); ?>
91
				
92
				<h1><?php esc_html_e( 'General', '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', 'wetu-importer' ); ?></label>
98
							</th>
99
							<td>
100
								<input type="text" value="<?php if ( isset( $options['api_key'] ) ) { echo esc_attr( $options['api_key'] ); } ?>" name="api_key" />
101
							</td>
102
						</tr>
103
						<tr class="form-field -wrap">
104
							<th scope="row">
105
								<label for="disable_tour_descriptions"><?php esc_html_e( 'Disable Tour Descriptions', 'wetu-importer' ); ?></label>
106
							</th>
107
							<td>
108
								<input type="checkbox"
109
								<?php
110
								if ( isset( $options['disable_tour_descriptions'] ) && '' !== $options['disable_tour_descriptions'] ) {
111
									echo esc_attr( 'checked="checked"' );
112
								}
113
								?>
114
								name="disable_tour_descriptions" />
115
116
								<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.', 'wetu-importer' ); ?></small>
117
							</td>
118
						</tr>
119
						<tr class="form-field -wrap">
120
							<th scope="row">
121
								<label for="disable_accommodation_descriptions"><?php esc_html_e( 'Disable Accommodation Descriptions', 'wetu-importer' ); ?></label>
122
							</th>
123
							<td>
124
								<input type="checkbox"
125
								<?php
126
								if ( isset( $options['disable_accommodation_descriptions'] ) && '' !== $options['disable_accommodation_descriptions'] ) {
127
									echo esc_attr( 'checked="checked"' );
128
								}
129
								?>
130
								name="disable_accommodation_descriptions" />
131
								<small><?php esc_html_e( 'If you are going to edit the accommodation descriptions imported then enable this setting.', 'wetu-importer' ); ?></small>
132
							</td>
133
						</tr>
134
						<tr class="form-field -wrap">
135
							<th scope="row">
136
								<label for="disable_accommodation_excerpts"><?php esc_html_e( 'Disable Accommodation Excerpts', 'wetu-importer' ); ?></label>
137
							</th>
138
							<td>
139
								<input type="checkbox"
140
								<?php
141
								if ( isset( $options['disable_accommodation_excerpts'] ) && '' !== $options['disable_accommodation_excerpts'] ) {
142
									echo esc_attr( 'checked="checked"' );
143
								}
144
								?>
145
								name="disable_accommodation_excerpts" />
146
								<small><?php esc_html_e( 'If you are going to edit the accommodation excerpts then enable this setting.', 'wetu-importer' ); ?></small>
147
							</td>
148
						</tr>
149
						<tr class="form-field -wrap">
150
							<th scope="row">
151
								<label for="disable_destination_descriptions"><?php esc_html_e( 'Disable Destinations Descriptions', 'wetu-importer' ); ?></label>
152
							</th>
153
							<td>
154
								<input type="checkbox"
155
								<?php
156
								if ( isset( $options['disable_destination_descriptions'] ) && '' !== $options['disable_destination_descriptions'] ) {
157
									echo esc_attr( 'checked="checked"' );
158
								}
159
								?>
160
								name="disable_destination_descriptions" />
161
								<small><?php esc_html_e( 'If you are going to edit the destination descriptions on this site then enable this setting.', 'wetu-importer' ); ?></small>
162
							</td>
163
						</tr>					
164
					</tbody>
165
				</table>
166
167
				<h1><?php esc_html_e( 'Images', 'wetu-importer' ); ?></h1>
168
169
				<table class="form-table">
170
					<tbody>
171
						<tr class="form-field -wrap">
172
							<th scope="row">
173
								<label for="image_replacing"><?php esc_html_e( 'Replace Images', 'wetu-importer' ); ?></label>
174
							</th>
175
							<td>
176
								<input type="checkbox"
177
								<?php
178
								if ( isset( $options['image_replacing'] ) && '' !== $options['image_replacing'] ) {
179
									echo esc_attr( 'checked="checked"' );
180
								}
181
								?>
182
								name="image_replacing" />
183
								<p><?php esc_html_e( 'Do you want your images to be replaced on each import.', 'wetu-importer' ); ?></p>
184
							</td>
185
						</tr>
186
						<tr class="form-field -wrap">
187
							<th scope="row">
188
								<label for="image_limit"> <?php esc_html_e( 'Limit the amount of images imported to the gallery', 'wetu-importer' ); ?></label>
189
							</th>
190
							<td>
191
								<input placeholder="" type="text" value="<?php
192
								if ( isset( $options['image_limit'] ) && '' !== $options['image_limit'] ) {
193
									echo esc_attr( $options['image_limit'] );
194
								}
195
								?>
196
								"
197
								name="image_limit" />
198
							</td>
199
						</tr>
200
201
						<tr class="form-field -wrap">
202
							<th scope="row">
203
								<label for="image_scaling"><?php esc_html_e( 'Enable Image Scaling', 'wetu-importer' ); ?></label>
204
							</th>
205
							<td>
206
								<input type="checkbox"
207
								<?php
208
								if ( isset( $options['image_scaling'] ) && '' !== $options['image_scaling'] ) {
209
									echo esc_attr( 'checked="checked"' );
210
								}
211
								?>
212
								name="image_scaling" />
213
							</td>
214
						</tr>
215
						<tr class="form-field -wrap">
216
							<th scope="row">
217
								<label for="width"> <?php esc_html_e( 'Width (px)', 'wetu-importer' ); ?></label>
218
							</th>
219
							<td>
220
								<input placeholder="800" type="text" value="<?php
221
								if ( isset( $options['width'] ) && '' !== $options['width'] ) {
222
									echo esc_attr( $options['width'] );
223
								}
224
								?>"
225
								name="width" />
226
							</td>
227
						</tr>
228
						<tr class="form-field -wrap">
229
							<th scope="row">
230
								<label for="height"> <?php esc_html_e( 'Height (px)', 'wetu-importer' ); ?></label>
231
							</th>
232
							<td>
233
								<input placeholder="600" type="text" value="<?php
234
								if ( isset( $options['height'] ) && '' !== $options['height'] ) {
235
									echo esc_attr( $options['height'] );
236
								}
237
								?>"
238
								name="height" />
239
							</td>
240
						</tr>
241
242
						<tr class="form-field -wrap">
243
							<th scope="row">
244
								<label for="scaling"> <?php esc_html_e( 'Scaling', 'wetu-importer' ); ?></label>
245
							</th>
246
							<td>
247
								<input type="radio"
248
								<?php
249 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...
250
									echo esc_attr( 'checked="checked"' );
251
								}
252
								?>
253
								name="scaling" value="raw" /> <?php esc_html_e( 'Get the Full size image, no cropping takes place.', 'wetu-importer' ); ?><br />
254
								<input type="radio"
255
								<?php
256 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...
257
									echo esc_attr( 'checked="checked"' );
258
								}
259
								?>
260
								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.', 'wetu-importer' ); ?><br />
261
								<input type="radio"
262
								<?php
263 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...
264
									echo esc_attr( 'checked="checked"' );
265
								}
266
								?>
267
								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', 'wetu-importer' ); ?><br />
268
								<input type="radio"
269
								<?php
270 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...
271
									echo esc_attr( 'checked="checked"' );
272
								}
273
								?>
274
								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', 'wetu-importer' ); ?><br />
275
								<input type="radio"
276
								<?php
277 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...
278
									echo esc_attr( 'checked="checked"' );
279
								}
280
								?>
281
								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', 'wetu-importer' ); ?><br />
282
								<input type="radio"
283
								<?php
284 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...
285
									echo esc_attr( 'checked="checked"' );
286
								}
287
								?>
288
								name="scaling"  value="n" /> <?php esc_html_e( 'Resize the image to fit within the frame. but do not upscale the image.', 'wetu-importer' ); ?><br />
289
								<input type="radio"
290
								<?php
291 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...
292
									echo esc_attr( 'checked="checked"' );
293
								}
294
								?>
295
								name="scaling"  value="W" /> <?php esc_html_e( 'Resize the image to fit within the frame. Image will not exceed specified dimensions', 'wetu-importer' ); ?>
296
							</td>
297
						</tr>
298
					</tbody>
299
				</table>
300
				<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php esc_html_e( 'Save Changes', 'wetu-importer' ); ?>"></p>
301
			</form>
302
		</div>
303
		<?php
304
	}
305
306
	/**
307
	 * Save the options fields
308
	 *
309
	 * @return void
310
	 */
311
	public function save_options() {
312
		if ( ! isset( $_POST['wetu_importer_save_options'] ) || ! wp_verify_nonce( $_POST['wetu_importer_save_options'], 'wetu_importer_save' ) ) {
313
			return;
314
		}
315
		$data_to_save = array();
316
		foreach ( $this->defaults as $key => $field ) {
317
			if ( isset( $_POST[ $key ] ) ) {
318
				$data_to_save[ $key ] = $_POST[ $key ];
319
			} else {
320
				$data_to_save[ $key ] = '';
321
			}
322
		}
323
		update_option( 'wetu_importer_settings', $data_to_save );
324
	}
325
}
326