Passed
Push — master ( 93685b...ba539e )
by Warwick
02:09
created

LSX_WETU_Importer_Settings::save_options()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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