Completed
Push — master ( ae12eb...603d32 )
by Stephanie
02:47
created

FrmStyle::update()   F

Complexity

Conditions 20
Paths 294

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
nc 294
nop 1
dl 0
loc 62
rs 2.1583
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
class FrmStyle {
3
	public $number = false; // Unique ID number of the current instance.
4
	public $id = 0; // the id of the post
5
6
	/**
7
	 * @param int|string $id The id of the stylsheet or 'default'
8
	 */
9
	public function __construct( $id = 0 ) {
10
        $this->id = $id;
11
    }
12
13
    public function get_new() {
14
		$this->id = 0;
15
16
        $max_slug_value = 2147483647;
17
        $min_slug_value = 37; // we want to have at least 2 characters in the slug
18
		$key = base_convert( rand( $min_slug_value, $max_slug_value ), 10, 36 );
19
20
        $style = array(
21
            'post_type'     => FrmStylesController::$post_type,
22
            'ID'            => '',
23
            'post_title'    => __( 'New Style', 'formidable' ),
24
            'post_name'     => $key,
25
            'post_content'  => $this->get_defaults(),
26
            'menu_order'    => '',
27
            'post_status'   => 'publish',
28
        );
29
30
        return (object) $style;
31
    }
32
33
	public function save( $settings ) {
34
		return FrmDb::save_settings( $settings, 'frm_styles' );
35
    }
36
37
	public function duplicate( $id ) {
38
        // duplicating is a pro feature
39
    }
40
41
    public function update( $id = 'default' ) {
42
 		$all_instances = $this->get_all();
43
44
 		if ( empty( $id ) ) {
45
 		     $new_style = (array) $this->get_new();
46
 		     $all_instances[] = $new_style;
47
 		}
48
49
        $action_ids = array();
50
51
 		foreach ( $all_instances as $number => $new_instance ) {
52
 			$new_instance = stripslashes_deep( (array) $new_instance );
53
 			$this->id = $new_instance['ID'];
54
 			if ( $id != $this->id || ! $_POST || ! isset( $_POST['frm_style_setting'] ) ) {
55
				$all_instances[ $number ] = $new_instance;
56
57
				if ( $new_instance['menu_order'] && $_POST && empty( $_POST['prev_menu_order'] ) && isset( $_POST['frm_style_setting']['menu_order'] ) ) {
58
 			        // this style was set to default, so remove default setting on previous default style
59
 			        $new_instance['menu_order'] = 0;
60
					$action_ids[] = $this->save( $new_instance );
61
 			    }
62
63
 			    // don't continue if not saving this style
64
 			    continue;
65
 			}
66
67
 			$new_instance['post_title'] = isset( $_POST['frm_style_setting']['post_title'] ) ? sanitize_text_field( $_POST['frm_style_setting']['post_title'] ) : '';
68
 			$new_instance['post_content'] = isset( $_POST['frm_style_setting']['post_content'] ) ? $_POST['frm_style_setting']['post_content'] : '';
69
 			$new_instance['post_type']  = FrmStylesController::$post_type;
70
            $new_instance['post_status']  = 'publish';
71
			$new_instance['menu_order']  = isset( $_POST['frm_style_setting']['menu_order'] ) ? absint( $_POST['frm_style_setting']['menu_order'] ) : 0;
72
73
			if ( empty( $id ) ) {
74
                $new_instance['post_name'] = $new_instance['post_title'];
75
            }
76
77
            $default_settings = $this->get_defaults();
78
79
            foreach ( $default_settings as $setting => $default ) {
80
				if ( ! isset( $new_instance['post_content'][ $setting ] ) ) {
81
					$new_instance['post_content'][ $setting ] = $default;
82
				}
83
84
				if ( $this->is_color( $setting ) ) {
85
					$new_instance['post_content'][ $setting ] = str_replace( '#', '', $new_instance['post_content'][ $setting ] );
86
				} else if ( in_array( $setting, array( 'submit_style', 'important_style', 'auto_width' ) ) && ! isset( $new_instance['post_content'][ $setting ] ) ) {
87
					$new_instance['post_content'][ $setting ] = 0;
88
                } else if ( $setting == 'font' ) {
89
                	$new_instance['post_content'][ $setting ] = $this->force_balanced_quotation( $new_instance['post_content'][ $setting ] );
90
                }
91
            }
92
93
			$all_instances[ $number ] = $new_instance;
94
95
			$action_ids[] = $this->save( $new_instance );
96
97
 		}
98
99
 		$this->save_settings();
100
101
 		return $action_ids;
102
 	}
103
104
	/**
105
	 * @since 3.01.01
106
	 */
107
	private function is_color( $setting ) {
108
		$extra_colors = array( 'error_bg', 'error_border', 'error_text' );
109
		return strpos( $setting, 'color' ) !== false || in_array( $setting, $extra_colors );
110
	}
111
112
	/**
113
	 * @since 3.01.01
114
	 */
115
	public function get_color_settings() {
116
		$defaults = $this->get_defaults();
117
		$settings = array_keys( $defaults );
118
		return array_filter( $settings, array( $this, 'is_color' ) );
119
	}
120
121
    /**
122
     * Create static css file
123
     */
124
	public function save_settings() {
125
		$filename = FrmAppHelper::plugin_path() . '/css/custom_theme.css.php';
126
		update_option( 'frm_last_style_update', date( 'njGi' ) );
127
128
		if ( ! is_file( $filename ) ) {
129
            return;
130
        }
131
132
		$this->clear_cache();
133
134
		$css = $this->get_css_content( $filename );
135
136
		$create_file = new FrmCreateFile(
137
			array(
138
				'file_name'     => FrmStylesController::get_file_name(),
139
				'new_file_path' => FrmAppHelper::plugin_path() . '/css',
140
			)
141
		);
142
		$create_file->create_file( $css );
143
144
		update_option( 'frmpro_css', $css, 'no' );
145
		set_transient( 'frmpro_css', $css, MONTH_IN_SECONDS );
146
	}
147
148
	private function get_css_content( $filename ) {
149
		$css = '/* ' . __( 'WARNING: Any changes made to this file will be lost when your Formidable settings are updated', 'formidable' ) . ' */' . "\n";
150
151
		$saving = true;
152
		$frm_style = $this;
153
154
        ob_start();
155
        include( $filename );
156
		$css .= preg_replace( '/\/\*(.|\s)*?\*\//', '', str_replace( array( "\r\n", "\r", "\n", "\t", '    ' ), '', ob_get_contents() ) );
157
        ob_end_clean();
158
159
		return $css;
160
	}
161
162
	private function clear_cache() {
163
		$default_post_atts = array(
164
			'post_type'   => FrmStylesController::$post_type,
165
			'post_status' => 'publish',
166
			'numberposts' => 99,
0 ignored issues
show
introduced by
Detected high pagination limit, numberposts is set to 99
Loading history...
167
			'orderby'     => 'title',
168
			'order'       => 'ASC',
169
		);
170
171
		FrmDb::delete_cache_and_transient( serialize( $default_post_atts ), 'frm_styles' );
172
		FrmDb::cache_delete_group( 'frm_styles' );
173
		FrmDb::delete_cache_and_transient( 'frmpro_css' );
174
	}
175
176
	public function destroy( $id ) {
177
		return wp_delete_post( $id );
178
    }
179
180
    public function get_one() {
181
        if ( 'default' == $this->id ) {
182
            $style = $this->get_default_style();
183
            if ( $style ) {
184
                $this->id = $style->ID;
185
            } else {
186
                $this->id = 0;
187
            }
188
            return $style;
189
        }
190
191
		$style = get_post( $this->id );
192
193
        if ( ! $style ) {
194
            return $style;
195
        }
196
197
		$style->post_content = FrmAppHelper::maybe_json_decode( $style->post_content );
198
199
        $default_values = $this->get_defaults();
200
201
		// fill default values
202
		$style->post_content = $this->override_defaults( $style->post_content );
203
		$style->post_content = wp_parse_args( $style->post_content, $default_values );
204
205
        return $style;
206
    }
207
208
    public function get_all( $orderby = 'title', $order = 'ASC', $limit = 99 ) {
209
        $post_atts = array(
210
			'post_type'   => FrmStylesController::$post_type,
211
			'post_status' => 'publish',
212
			'numberposts' => $limit,
213
			'orderby'     => $orderby,
214
			'order'       => $order,
215
        );
216
217
		$temp_styles = FrmDb::check_cache( serialize( $post_atts ), 'frm_styles', $post_atts, 'get_posts' );
218
219
		if ( empty( $temp_styles ) ) {
220
            global $wpdb;
221
            // make sure there wasn't a conflict with the query
222
			$query = $wpdb->prepare( 'SELECT * FROM ' . $wpdb->posts . ' WHERE post_type=%s AND post_status=%s ORDER BY post_title ASC LIMIT 99', FrmStylesController::$post_type, 'publish' );
223
			$temp_styles = FrmDb::check_cache( 'frm_backup_style_check', 'frm_styles', $query, 'get_results' );
224
225
			if ( empty( $temp_styles ) ) {
226
                // create a new style if there are none
227
         		$new = $this->get_new();
228
				$new->post_title = __( 'Formidable Style', 'formidable' );
229
				$new->post_name = $new->post_title;
230
         		$new->menu_order = 1;
231
				$new = $this->save( (array) $new );
232
				$this->update( 'default' );
233
234
                $post_atts['include'] = $new;
235
236
                $temp_styles = get_posts( $post_atts );
237
            }
238
        }
239
240
        $default_values = $this->get_defaults();
241
        $default_style = false;
242
243
        $styles = array();
244
        foreach ( $temp_styles as $style ) {
245
            $this->id = $style->ID;
246
            if ( $style->menu_order ) {
247
                if ( $default_style ) {
248
                    // only return one default
249
                    $style->menu_order = 0;
250
                } else {
251
                    // check for a default style
252
                    $default_style = $style->ID;
253
                }
254
            }
255
256
			$style->post_content = FrmAppHelper::maybe_json_decode( $style->post_content );
257
258
			// fill default values
259
			$style->post_content = $this->override_defaults( $style->post_content );
260
			$style->post_content = wp_parse_args( $style->post_content, $default_values );
261
262
			$styles[ $style->ID ] = $style;
263
        }
264
265
        if ( ! $default_style ) {
266
			$default_style = reset( $styles );
267
			$styles[ $default_style->ID ]->menu_order = 1;
268
        }
269
270
        return $styles;
271
    }
272
273
	public function get_default_style( $styles = null ) {
274
		if ( ! isset( $styles ) ) {
275
			$styles = $this->get_all( 'menu_order', 'DESC', 1 );
276
        }
277
278
        foreach ( $styles as $style ) {
279
            if ( $style->menu_order ) {
280
                return $style;
281
            }
282
        }
283
    }
284
285
	public function override_defaults( $settings ) {
286
		if ( ! is_array( $settings ) ) {
287
	        return $settings;
288
	    }
289
290
		$settings['line_height'] = ( ! isset( $settings['field_height'] ) || $settings['field_height'] == '' || $settings['field_height'] == 'auto' ) ? 'normal' : $settings['field_height'];
291
292
		if ( ! isset( $settings['form_desc_size'] ) && isset( $settings['description_font_size'] ) ) {
293
	        $settings['form_desc_size'] = $settings['description_font_size'];
294
	        $settings['form_desc_color'] = $settings['description_color'];
295
	        $settings['title_color'] = $settings['label_color'];
296
	    }
297
298
		if ( ! isset( $settings['section_color'] ) && isset( $settings['label_color'] ) ) {
299
	        $settings['section_color'] = $settings['label_color'];
300
	        $settings['section_border_color'] = $settings['border_color'];
301
	    }
302
303
		if ( ! isset( $settings['submit_hover_bg_color'] ) && isset( $settings['submit_bg_color'] ) ) {
304
	        $settings['submit_hover_bg_color'] = $settings['submit_bg_color'];
305
	        $settings['submit_hover_color'] = $settings['submit_text_color'];
306
	        $settings['submit_hover_border_color'] = $settings['submit_border_color'];
307
308
	        $settings['submit_active_bg_color'] = $settings['submit_bg_color'];
309
	        $settings['submit_active_color'] = $settings['submit_text_color'];
310
            $settings['submit_active_border_color'] = $settings['submit_border_color'];
311
	    }
312
313
	    return apply_filters( 'frm_override_default_styles', $settings );
314
	}
315
316
	public function get_defaults() {
317
		$defaults = array(
318
            'theme_css'         => 'ui-lightness',
319
            'theme_name'        => 'UI Lightness',
320
321
			'center_form'       => '',
322
            'form_width'        => '100%',
323
            'form_align'        => 'left',
324
            'direction'         => is_rtl() ? 'rtl' : 'ltr',
325
            'fieldset'          => '0px',
326
            'fieldset_color'    => '000000',
327
            'fieldset_padding'  => '0 0 15px 0',
328
            'fieldset_bg_color' => '',
329
330
            'title_size'        => '20px',
331
            'title_color'       => '444444',
332
			'title_margin_top'  => '10px',
333
			'title_margin_bottom' => '10px',
334
            'form_desc_size'    => '14px',
335
            'form_desc_color'   => '666666',
336
			'form_desc_margin_top' => '10px',
337
			'form_desc_margin_bottom' => '25px',
338
339
            'font'              => '"Lucida Grande","Lucida Sans Unicode",Tahoma,sans-serif',
340
            'font_size'         => '14px',
341
            'label_color'       => '444444',
342
            'weight'            => 'bold',
343
            'position'          => 'none',
344
            'align'             => 'left',
345
            'width'             => '150px',
346
            'required_color'    => 'B94A48',
347
            'required_weight'   => 'bold',
348
            'label_padding'     => '0 0 3px 0',
349
350
            'description_font_size' => '12px',
351
            'description_color' => '666666',
352
            'description_weight' => 'normal',
353
            'description_style' => 'normal',
354
            'description_align' => 'left',
355
			'description_margin' => '0',
356
357
            'field_font_size'   => '14px',
358
            'field_height'      => '32px',
359
            'line_height'       => 'normal',
360
            'field_width'       => '100%',
361
            'auto_width'        => false,
362
            'field_pad'         => '6px 10px',
363
            'field_margin'      => '20px',
364
			'field_weight' => 'normal',
365
            'text_color'        => '555555',
366
            //'border_color_hv'   => 'cccccc',
367
            'border_color'      => 'cccccc',
368
            'field_border_width' => '1px',
369
            'field_border_style' => 'solid',
370
371
            'bg_color'          => 'ffffff',
372
            //'bg_color_hv'       => 'ffffff',
373
			'remove_box_shadow' => '',
374
            'bg_color_active'   => 'ffffff',
375
			'border_color_active' => '66afe9',
376
			'remove_box_shadow_active' => '',
377
            'text_color_error'  => '444444',
378
            'bg_color_error'    => 'ffffff',
379
			'border_color_error' => 'B94A48',
380
			'border_width_error' => '1px',
381
			'border_style_error' => 'solid',
382
            'bg_color_disabled' => 'ffffff',
383
            'border_color_disabled' => 'E5E5E5',
384
            'text_color_disabled' => 'A1A1A1',
385
386
            'radio_align'       => 'block',
387
            'check_align'       => 'block',
388
            'check_font_size'   => '13px',
389
            'check_label_color' => '444444',
390
            'check_weight'      => 'normal',
391
392
            'section_font_size' => '18px',
393
            'section_color'     => '444444',
394
            'section_weight'    => 'bold',
395
            'section_pad'       => '15px 0 3px 0',
396
            'section_mar_top'   => '15px',
397
			'section_mar_bottom' => '12px',
398
            'section_bg_color'  => '',
399
            'section_border_color' => 'e8e8e8',
400
            'section_border_width' => '2px',
401
            'section_border_style' => 'solid',
402
            'section_border_loc' => '-top',
403
            'collapse_icon'     => '6',
404
            'collapse_pos'      => 'after',
405
            'repeat_icon'       => '1',
406
407
            'submit_style'      => false,
408
            'submit_font_size'  => '14px',
409
            'submit_width'      => 'auto',
410
            'submit_height'     => 'auto',
411
            'submit_bg_color'   => 'ffffff',
412
            'submit_border_color' => 'cccccc',
413
            'submit_border_width' => '1px',
414
            'submit_text_color' => '444444',
415
            'submit_weight'     => 'normal',
416
            'submit_border_radius' => '4px',
417
            'submit_bg_img'     => '',
418
            'submit_margin'     => '10px',
419
            'submit_padding'    => '6px 11px',
420
            'submit_shadow_color' => 'eeeeee',
421
            'submit_hover_bg_color' => 'efefef',
422
            'submit_hover_color' => '444444',
423
            'submit_hover_border_color' => 'cccccc',
424
            'submit_active_bg_color' => 'efefef',
425
            'submit_active_color' => '444444',
426
            'submit_active_border_color' => 'cccccc',
427
428
            'border_radius'     => '4px',
429
            'error_bg'          => 'F2DEDE',
430
            'error_border'      => 'EBCCD1',
431
            'error_text'        => 'B94A48',
432
            'error_font_size'   => '14px',
433
434
            'success_bg_color'  => 'DFF0D8',
435
            'success_border_color' => 'D6E9C6',
436
            'success_text_color' => '468847',
437
            'success_font_size' => '14px',
438
439
            'important_style'   => false,
440
441
			'progress_bg_color'     => 'dddddd',
442
			'progress_active_color' => 'ffffff',
443
			'progress_active_bg_color' => '008ec2',
444
			'progress_color'        => 'ffffff',
445
			'progress_border_color' => 'dfdfdf',
446
			'progress_border_size'  => '2px',
447
			'progress_size'         => '30px',
448
449
            'custom_css'        => '',
450
		);
451
		return apply_filters( 'frm_default_style_settings', $defaults );
452
    }
453
454
	public function get_field_name( $field_name, $post_field = 'post_content' ) {
455
		return 'frm_style_setting' . ( empty( $post_field ) ? '' : '[' . $post_field . ']' ) . '[' . $field_name . ']';
456
	}
457
458
	public static function get_bold_options() {
459
		return array(
460
			100 => 100,
461
			200 => 200,
462
			300 => 300,
463
			'normal' => __( 'normal', 'formidable' ),
464
			500 => 500,
465
			600 => 600,
466
			'bold' => __( 'bold', 'formidable' ),
467
			800 => 800,
468
			900 => 900,
469
		);
470
	}
471
472
	/**
473
	 * Don't let imbalanced font families ruin the whole stylesheet
474
	 */
475
	public function force_balanced_quotation( $value ) {
476
		$balanced_characters = array( '"', "'" );
477
		foreach ( $balanced_characters as $char ) {
478
			$char_count = substr_count( $value, $char );
479
			$is_balanced = $char_count % 2 == 0;
480
			if ( ! $is_balanced ) {
481
				$value .= $char;
482
			}
483
		}
484
		return $value;
485
	}
486
}
487