Completed
Push — master ( 34a52e...661e23 )
by Stephanie
04:47
created

FrmStyle::get_default_style()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 6
nop 1
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
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 = pow(36, 6);
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 FrmAppHelper::save_settings( $settings, 'frm_styles' );
35
    }
36
37
	public function duplicate( $id ) {
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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'] = sanitize_text_field( $_POST['frm_style_setting']['post_title'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
68
 			$new_instance['post_content'] = $_POST['frm_style_setting']['post_content'];
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
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;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
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 ( strpos( $setting, 'color' ) !== false || in_array( $setting, array( 'error_bg', 'error_border', 'error_text' ) ) ) {
81
                    //if is a color
82
					$new_instance['post_content'][ $setting ] = str_replace( '#', '', $new_instance['post_content'][ $setting ] );
83
				} else if ( in_array( $setting, array( 'submit_style', 'important_style', 'auto_width' ) ) && ! isset( $new_instance['post_content'][ $setting ] ) ) {
84
					$new_instance['post_content'][ $setting ] = 0;
85
                } else if ( $setting == 'font' ) {
86
                	$new_instance['post_content'][ $setting ] = $this->force_balanced_quotation( $new_instance['post_content'][ $setting ] );
87
                }
88
            }
89
90
			$all_instances[ $number ] = $new_instance;
91
92
            $action_ids[] = $this->save($new_instance);
93
94
 		}
95
96
 		$this->save_settings($all_instances);
97
98
 		return $action_ids;
99
 	}
100
101
    /**
102
     * Create static css file
103
     */
104
	public function save_settings( $styles ) {
0 ignored issues
show
Unused Code introduced by
The parameter $styles is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
		$filename = FrmAppHelper::plugin_path() . '/css/custom_theme.css.php';
106
		update_option( 'frm_last_style_update', date('njGi') );
107
108
        if ( ! is_file($filename) ) {
109
            return;
110
        }
111
112
		$css = $this->get_css_content( $filename );
113
114
		$create_file = new FrmCreateFile( array( 'folder_name' => 'formidable/css', 'file_name' => 'formidablepro.css' ) );
115
		$create_file->create_file( $css );
116
117
        update_option('frmpro_css', $css);
118
119
        delete_transient('frmpro_css');
120
        set_transient('frmpro_css', $css);
121
	}
122
123
	private function get_css_content( $filename ) {
124
		$css = '/* ' . __( 'WARNING: Any changes made to this file will be lost when your Formidable settings are updated', 'formidable' ) . ' */' . "\n";
125
126
		$saving = true;
127
		$frm_style = $this;
128
129
        ob_start();
130
        include( $filename );
131
		$css .= preg_replace( '/\/\*(.|\s)*?\*\//', '', str_replace( array( "\r\n", "\r", "\n", "\t", '    ' ), '', ob_get_contents() ) );
132
        ob_end_clean();
133
134
		return $css;
135
	}
136
137
	public function destroy( $id ) {
138
        return wp_delete_post($id);
139
    }
140
141
    public function get_one() {
142
        if ( 'default' == $this->id ) {
143
            $style = $this->get_default_style();
144
            if ( $style ) {
145
                $this->id = $style->ID;
146
            } else {
147
                $this->id = 0;
148
            }
149
            return $style;
150
        }
151
152
        $style = get_post($this->id);
153
154
        if ( ! $style ) {
155
            return $style;
156
        }
157
158
        $style->post_content = FrmAppHelper::maybe_json_decode($style->post_content);
159
160
        $default_values = $this->get_defaults();
161
162
        // fill default values
163
        $style->post_content = $this->override_defaults($style->post_content);
164
        $style->post_content = wp_parse_args( $style->post_content, $default_values);
165
166
        return $style;
167
    }
168
169
    public function get_all( $orderby = 'title', $order = 'ASC', $limit = 99 ) {
170
        $post_atts = array(
171
			'post_type'   => FrmStylesController::$post_type,
172
			'post_status' => 'publish',
173
			'numberposts' => $limit,
174
			'orderby'     => $orderby,
175
			'order'       => $order,
176
        );
177
178
        $temp_styles = FrmAppHelper::check_cache(serialize($post_atts), 'frm_styles', $post_atts, 'get_posts');
0 ignored issues
show
Documentation introduced by
$post_atts is of type array<string,string|inte...ing","order":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
179
180
        if ( empty($temp_styles) ) {
181
            global $wpdb;
182
            // make sure there wasn't a conflict with the query
183
			$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' );
184
            $temp_styles = FrmAppHelper::check_cache('frm_backup_style_check', 'frm_styles', $query, 'get_results');
185
186
            if ( empty($temp_styles) ) {
187
                // create a new style if there are none
188
         		$new = $this->get_new();
189
         		$new->post_title = $new->post_name = __( 'Formidable Style', 'formidable' );
190
         		$new->menu_order = 1;
191
         		$new = $this->save( (array) $new);
192
         		$this->update('default');
193
194
                $post_atts['include'] = $new;
195
196
                $temp_styles = get_posts( $post_atts );
197
            }
198
        }
199
200
        $default_values = $this->get_defaults();
201
        $default_style = false;
202
203
        $styles = array();
204
        foreach ( $temp_styles as $style ) {
205
            $this->id = $style->ID;
206
            if ( $style->menu_order ) {
207
                if ( $default_style ) {
208
                    // only return one default
209
                    $style->menu_order = 0;
210
                } else {
211
                    // check for a default style
212
                    $default_style = $style->ID;
213
                }
214
            }
215
216
            $style->post_content = FrmAppHelper::maybe_json_decode($style->post_content);
217
218
            // fill default values
219
            $style->post_content = $this->override_defaults($style->post_content);
220
            $style->post_content = wp_parse_args( $style->post_content, $default_values);
221
222
			$styles[ $style->ID ] = $style;
223
        }
224
225
        if ( ! $default_style ) {
226
            $default_style = reset($styles);
227
			$styles[ $default_style->ID ]->menu_order = 1;
228
        }
229
230
        return $styles;
231
    }
232
233
	public function get_default_style( $styles = null ) {
234
        if ( ! isset($styles) ) {
235
			$styles = $this->get_all( 'menu_order', 'DESC', 1 );
236
        }
237
238
        foreach ( $styles as $style ) {
239
            if ( $style->menu_order ) {
240
                return $style;
241
            }
242
        }
243
    }
244
245
	public function override_defaults( $settings ) {
246
	    if ( ! is_array($settings) ) {
247
	        return $settings;
248
	    }
249
250
	    $settings['line_height'] = ( ! isset($settings['field_height']) || $settings['field_height'] == '' || $settings['field_height'] == 'auto') ? 'normal' : $settings['field_height'];
251
252
	    if ( ! isset($settings['form_desc_size']) && isset($settings['description_font_size']) ) {
253
	        $settings['form_desc_size'] = $settings['description_font_size'];
254
	        $settings['form_desc_color'] = $settings['description_color'];
255
	        $settings['title_color'] = $settings['label_color'];
256
	    }
257
258
	    if ( ! isset($settings['section_color']) && isset($settings['label_color']) ) {
259
	        $settings['section_color'] = $settings['label_color'];
260
	        $settings['section_border_color'] = $settings['border_color'];
261
	    }
262
263
	    if ( ! isset($settings['submit_hover_bg_color']) && isset($settings['submit_bg_color']) ) {
264
	        $settings['submit_hover_bg_color'] = $settings['submit_bg_color'];
265
	        $settings['submit_hover_color'] = $settings['submit_text_color'];
266
	        $settings['submit_hover_border_color'] = $settings['submit_border_color'];
267
268
	        $settings['submit_active_bg_color'] = $settings['submit_bg_color'];
269
	        $settings['submit_active_color'] = $settings['submit_text_color'];
270
            $settings['submit_active_border_color'] = $settings['submit_border_color'];
271
	    }
272
273
	    return $settings;
274
	}
275
276
	public function get_defaults() {
277
        return array(
278
            'theme_css'         => 'ui-lightness',
279
            'theme_name'        => 'UI Lightness',
280
281
			'center_form'		=> '',
282
            'form_width'        => '100%',
283
            'form_align'        => 'left',
284
            'direction'         => is_rtl() ? 'rtl' : 'ltr',
285
            'fieldset'          => '0px',
286
            'fieldset_color'    => '000000',
287
            'fieldset_padding'  => '0 0 15px 0',
288
            'fieldset_bg_color' => '',
289
290
            'title_size'        => '20px',
291
            'title_color'       => '444444',
292
			'title_margin_top'  => '10px',
293
			'title_margin_bottom' => '10px',
294
            'form_desc_size'    => '14px',
295
            'form_desc_color'   => '666666',
296
			'form_desc_margin_top' => '10px',
297
			'form_desc_margin_bottom' => '25px',
298
299
            'font'              => '"Lucida Grande","Lucida Sans Unicode",Tahoma,sans-serif',
300
            'font_size'         => '14px',
301
            'label_color'       => '444444',
302
            'weight'            => 'bold',
303
            'position'          => 'none',
304
            'align'             => 'left',
305
            'width'             => '150px',
306
            'required_color'    => 'B94A48',
307
            'required_weight'   => 'bold',
308
            'label_padding'     => '0 0 3px 0',
309
310
            'description_font_size' => '12px',
311
            'description_color' => '666666',
312
            'description_weight' => 'normal',
313
            'description_style' => 'normal',
314
            'description_align' => 'left',
315
316
            'field_font_size'   => '14px',
317
            'field_height' 		=> '32px',
318
            'line_height'		=> 'normal',
319
            'field_width'       => '100%',
320
            'auto_width'        => false,
321
            'field_pad'         => '6px 10px',
322
            'field_margin'      => '20px',
323
			'field_weight' => 'normal',
324
            'text_color'        => '555555',
325
            //'border_color_hv'   => 'cccccc',
326
            'border_color'      => 'cccccc',
327
            'field_border_width' => '1px',
328
            'field_border_style' => 'solid',
329
330
            'bg_color'          => 'ffffff',
331
            //'bg_color_hv'       => 'ffffff',
332
			'remove_box_shadow' => '',
333
            'bg_color_active'   => 'ffffff',
334
			'border_color_active' => '66afe9',
335
			'remove_box_shadow_active' => '',
336
            'text_color_error'  => '444444',
337
            'bg_color_error'    => 'ffffff',
338
			'border_color_error' => 'B94A48',
339
			'border_width_error' => '1px',
340
			'border_style_error' => 'solid',
341
            'bg_color_disabled' => 'ffffff',
342
            'border_color_disabled' => 'E5E5E5',
343
            'text_color_disabled' => 'A1A1A1',
344
345
            'radio_align'       => 'block',
346
            'check_align'       => 'block',
347
            'check_font_size'   => '13px',
348
            'check_label_color' => '444444',
349
            'check_weight'      => 'normal',
350
351
            'section_font_size' => '18px',
352
            'section_color'     => '444444',
353
            'section_weight'    => 'bold',
354
            'section_pad'       => '15px 0 3px 0',
355
            'section_mar_top'   => '15px',
356
			'section_mar_bottom' => '12px',
357
            'section_bg_color'  => '',
358
            'section_border_color' => 'e8e8e8',
359
            'section_border_width' => '2px',
360
            'section_border_style' => 'solid',
361
            'section_border_loc' => '-top',
362
            'collapse_icon'     => '6',
363
            'collapse_pos'      => 'after',
364
            'repeat_icon'       => '1',
365
366
            'submit_style'      => false,
367
            'submit_font_size'  => '14px',
368
            'submit_width'      => 'auto',
369
            'submit_height'     => 'auto',
370
            'submit_bg_color'   => 'ffffff',
371
            'submit_border_color' => 'cccccc',
372
            'submit_border_width' => '1px',
373
            'submit_text_color' => '444444',
374
            'submit_weight'     => 'normal',
375
            'submit_border_radius' => '4px',
376
            'submit_bg_img'     => '',
377
            'submit_margin'     => '10px',
378
            'submit_padding'    => '6px 11px',
379
            'submit_shadow_color' => 'eeeeee',
380
            'submit_hover_bg_color' => 'efefef',
381
            'submit_hover_color' => '444444',
382
            'submit_hover_border_color' => 'cccccc',
383
            'submit_active_bg_color' => 'efefef',
384
            'submit_active_color' => '444444',
385
            'submit_active_border_color' => 'cccccc',
386
387
            'border_radius'     => '4px',
388
            'error_bg'          => 'F2DEDE',
389
            'error_border'      => 'EBCCD1',
390
            'error_text'        => 'B94A48',
391
            'error_font_size'   => '14px',
392
393
            'success_bg_color'  => 'DFF0D8',
394
            'success_border_color' => 'D6E9C6',
395
            'success_text_color' => '468847',
396
            'success_font_size' => '14px',
397
398
            'important_style'   => false,
399
400
            'custom_css'        => '',
401
        );
402
    }
403
404
	public function get_field_name( $field_name, $post_field = 'post_content' ) {
405
		return 'frm_style_setting' . ( empty( $post_field ) ? '' : '[' . $post_field . ']' ) . '[' . $field_name . ']';
406
	}
407
408
	public static function get_bold_options() {
409
		return array(
410
			100 => 100, 200 => 200, 300 => 300,
411
			'normal' => __( 'normal', 'formidable' ),
412
			500 => 500, 600 => 600,
413
			'bold' => __( 'bold', 'formidable' ),
414
			800 => 800, 900 => 900,
415
		);
416
	}
417
418
419
	/**
420
	 * Don't let imbalanced font families ruin the whole stylesheet
421
	 */
422
	public function force_balanced_quotation( $value ) {
423
		$balanced_characters = array( '"', "'" );
424
		foreach ( $balanced_characters as $char ) {
425
			$char_count = substr_count( $value, $char );
426
			$is_balanced = $char_count % 2 == 0;
427
			if ( ! $is_balanced ) {
428
				$value .= $char;
429
			}
430
		}
431
		return $value;
432
	}
433
}
434