Completed
Push — master ( 3c9d8a...07b729 )
by Stephanie
04:17
created

FrmStyle::get_one()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 27
rs 8.5806
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();
97
98
 		return $action_ids;
99
 	}
100
101
    /**
102
     * Create static css file
103
     */
104
	public function save_settings() {
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
		$this->clear_cache();
120
121
        set_transient('frmpro_css', $css);
122
	}
123
124
	private function get_css_content( $filename ) {
125
		$css = '/* ' . __( 'WARNING: Any changes made to this file will be lost when your Formidable settings are updated', 'formidable' ) . ' */' . "\n";
126
127
		$saving = true;
128
		$frm_style = $this;
129
130
        ob_start();
131
        include( $filename );
132
		$css .= preg_replace( '/\/\*(.|\s)*?\*\//', '', str_replace( array( "\r\n", "\r", "\n", "\t", '    ' ), '', ob_get_contents() ) );
133
        ob_end_clean();
134
135
		return $css;
136
	}
137
138
	private function clear_cache() {
139
		$default_post_atts = array(
140
			'post_type'   => FrmStylesController::$post_type,
141
			'post_status' => 'publish',
142
			'numberposts' => 99,
0 ignored issues
show
introduced by
Detected high pagination limit, numberposts is set to 99
Loading history...
143
			'orderby'     => 'title',
144
			'order'       => 'ASC',
145
		);
146
147
		FrmAppHelper::delete_cache_and_transient( serialize( $default_post_atts ), 'frm_styles' );
148
		FrmAppHelper::cache_delete_group( 'frm_styles' );
149
		FrmAppHelper::delete_cache_and_transient( 'frmpro_css' );
150
	}
151
152
	public function destroy( $id ) {
153
        return wp_delete_post($id);
154
    }
155
156
    public function get_one() {
157
        if ( 'default' == $this->id ) {
158
            $style = $this->get_default_style();
159
            if ( $style ) {
160
                $this->id = $style->ID;
161
            } else {
162
                $this->id = 0;
163
            }
164
            return $style;
165
        }
166
167
        $style = get_post($this->id);
168
169
        if ( ! $style ) {
170
            return $style;
171
        }
172
173
        $style->post_content = FrmAppHelper::maybe_json_decode($style->post_content);
174
175
        $default_values = $this->get_defaults();
176
177
        // fill default values
178
        $style->post_content = $this->override_defaults($style->post_content);
179
        $style->post_content = wp_parse_args( $style->post_content, $default_values);
180
181
        return $style;
182
    }
183
184
    public function get_all( $orderby = 'title', $order = 'ASC', $limit = 99 ) {
185
        $post_atts = array(
186
			'post_type'   => FrmStylesController::$post_type,
187
			'post_status' => 'publish',
188
			'numberposts' => $limit,
189
			'orderby'     => $orderby,
190
			'order'       => $order,
191
        );
192
193
        $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...
194
195
        if ( empty($temp_styles) ) {
196
            global $wpdb;
197
            // make sure there wasn't a conflict with the query
198
			$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' );
199
            $temp_styles = FrmAppHelper::check_cache('frm_backup_style_check', 'frm_styles', $query, 'get_results');
200
201
            if ( empty($temp_styles) ) {
202
                // create a new style if there are none
203
         		$new = $this->get_new();
204
         		$new->post_title = $new->post_name = __( 'Formidable Style', 'formidable' );
205
         		$new->menu_order = 1;
206
         		$new = $this->save( (array) $new);
207
         		$this->update('default');
208
209
                $post_atts['include'] = $new;
210
211
                $temp_styles = get_posts( $post_atts );
212
            }
213
        }
214
215
        $default_values = $this->get_defaults();
216
        $default_style = false;
217
218
        $styles = array();
219
        foreach ( $temp_styles as $style ) {
220
            $this->id = $style->ID;
221
            if ( $style->menu_order ) {
222
                if ( $default_style ) {
223
                    // only return one default
224
                    $style->menu_order = 0;
225
                } else {
226
                    // check for a default style
227
                    $default_style = $style->ID;
228
                }
229
            }
230
231
            $style->post_content = FrmAppHelper::maybe_json_decode($style->post_content);
232
233
            // fill default values
234
            $style->post_content = $this->override_defaults($style->post_content);
235
            $style->post_content = wp_parse_args( $style->post_content, $default_values);
236
237
			$styles[ $style->ID ] = $style;
238
        }
239
240
        if ( ! $default_style ) {
241
            $default_style = reset($styles);
242
			$styles[ $default_style->ID ]->menu_order = 1;
243
        }
244
245
        return $styles;
246
    }
247
248
	public function get_default_style( $styles = null ) {
249
        if ( ! isset($styles) ) {
250
			$styles = $this->get_all( 'menu_order', 'DESC', 1 );
251
        }
252
253
        foreach ( $styles as $style ) {
254
            if ( $style->menu_order ) {
255
                return $style;
256
            }
257
        }
258
    }
259
260
	public function override_defaults( $settings ) {
261
	    if ( ! is_array($settings) ) {
262
	        return $settings;
263
	    }
264
265
	    $settings['line_height'] = ( ! isset($settings['field_height']) || $settings['field_height'] == '' || $settings['field_height'] == 'auto') ? 'normal' : $settings['field_height'];
266
267
	    if ( ! isset($settings['form_desc_size']) && isset($settings['description_font_size']) ) {
268
	        $settings['form_desc_size'] = $settings['description_font_size'];
269
	        $settings['form_desc_color'] = $settings['description_color'];
270
	        $settings['title_color'] = $settings['label_color'];
271
	    }
272
273
	    if ( ! isset($settings['section_color']) && isset($settings['label_color']) ) {
274
	        $settings['section_color'] = $settings['label_color'];
275
	        $settings['section_border_color'] = $settings['border_color'];
276
	    }
277
278
	    if ( ! isset($settings['submit_hover_bg_color']) && isset($settings['submit_bg_color']) ) {
279
	        $settings['submit_hover_bg_color'] = $settings['submit_bg_color'];
280
	        $settings['submit_hover_color'] = $settings['submit_text_color'];
281
	        $settings['submit_hover_border_color'] = $settings['submit_border_color'];
282
283
	        $settings['submit_active_bg_color'] = $settings['submit_bg_color'];
284
	        $settings['submit_active_color'] = $settings['submit_text_color'];
285
            $settings['submit_active_border_color'] = $settings['submit_border_color'];
286
	    }
287
288
	    return $settings;
289
	}
290
291
	public function get_defaults() {
292
        return array(
293
            'theme_css'         => 'ui-lightness',
294
            'theme_name'        => 'UI Lightness',
295
296
			'center_form'		=> '',
297
            'form_width'        => '100%',
298
            'form_align'        => 'left',
299
            'direction'         => is_rtl() ? 'rtl' : 'ltr',
300
            'fieldset'          => '0px',
301
            'fieldset_color'    => '000000',
302
            'fieldset_padding'  => '0 0 15px 0',
303
            'fieldset_bg_color' => '',
304
305
            'title_size'        => '20px',
306
            'title_color'       => '444444',
307
			'title_margin_top'  => '10px',
308
			'title_margin_bottom' => '10px',
309
            'form_desc_size'    => '14px',
310
            'form_desc_color'   => '666666',
311
			'form_desc_margin_top' => '10px',
312
			'form_desc_margin_bottom' => '25px',
313
314
            'font'              => '"Lucida Grande","Lucida Sans Unicode",Tahoma,sans-serif',
315
            'font_size'         => '14px',
316
            'label_color'       => '444444',
317
            'weight'            => 'bold',
318
            'position'          => 'none',
319
            'align'             => 'left',
320
            'width'             => '150px',
321
            'required_color'    => 'B94A48',
322
            'required_weight'   => 'bold',
323
            'label_padding'     => '0 0 3px 0',
324
325
            'description_font_size' => '12px',
326
            'description_color' => '666666',
327
            'description_weight' => 'normal',
328
            'description_style' => 'normal',
329
            'description_align' => 'left',
330
			'description_margin' => '0',
331
332
            'field_font_size'   => '14px',
333
            'field_height' 		=> '32px',
334
            'line_height'		=> 'normal',
335
            'field_width'       => '100%',
336
            'auto_width'        => false,
337
            'field_pad'         => '6px 10px',
338
            'field_margin'      => '20px',
339
			'field_weight' => 'normal',
340
            'text_color'        => '555555',
341
            //'border_color_hv'   => 'cccccc',
342
            'border_color'      => 'cccccc',
343
            'field_border_width' => '1px',
344
            'field_border_style' => 'solid',
345
346
            'bg_color'          => 'ffffff',
347
            //'bg_color_hv'       => 'ffffff',
348
			'remove_box_shadow' => '',
349
            'bg_color_active'   => 'ffffff',
350
			'border_color_active' => '66afe9',
351
			'remove_box_shadow_active' => '',
352
            'text_color_error'  => '444444',
353
            'bg_color_error'    => 'ffffff',
354
			'border_color_error' => 'B94A48',
355
			'border_width_error' => '1px',
356
			'border_style_error' => 'solid',
357
            'bg_color_disabled' => 'ffffff',
358
            'border_color_disabled' => 'E5E5E5',
359
            'text_color_disabled' => 'A1A1A1',
360
361
            'radio_align'       => 'block',
362
            'check_align'       => 'block',
363
            'check_font_size'   => '13px',
364
            'check_label_color' => '444444',
365
            'check_weight'      => 'normal',
366
367
            'section_font_size' => '18px',
368
            'section_color'     => '444444',
369
            'section_weight'    => 'bold',
370
            'section_pad'       => '15px 0 3px 0',
371
            'section_mar_top'   => '15px',
372
			'section_mar_bottom' => '12px',
373
            'section_bg_color'  => '',
374
            'section_border_color' => 'e8e8e8',
375
            'section_border_width' => '2px',
376
            'section_border_style' => 'solid',
377
            'section_border_loc' => '-top',
378
            'collapse_icon'     => '6',
379
            'collapse_pos'      => 'after',
380
            'repeat_icon'       => '1',
381
382
            'submit_style'      => false,
383
            'submit_font_size'  => '14px',
384
            'submit_width'      => 'auto',
385
            'submit_height'     => 'auto',
386
            'submit_bg_color'   => 'ffffff',
387
            'submit_border_color' => 'cccccc',
388
            'submit_border_width' => '1px',
389
            'submit_text_color' => '444444',
390
            'submit_weight'     => 'normal',
391
            'submit_border_radius' => '4px',
392
            'submit_bg_img'     => '',
393
            'submit_margin'     => '10px',
394
            'submit_padding'    => '6px 11px',
395
            'submit_shadow_color' => 'eeeeee',
396
            'submit_hover_bg_color' => 'efefef',
397
            'submit_hover_color' => '444444',
398
            'submit_hover_border_color' => 'cccccc',
399
            'submit_active_bg_color' => 'efefef',
400
            'submit_active_color' => '444444',
401
            'submit_active_border_color' => 'cccccc',
402
403
            'border_radius'     => '4px',
404
            'error_bg'          => 'F2DEDE',
405
            'error_border'      => 'EBCCD1',
406
            'error_text'        => 'B94A48',
407
            'error_font_size'   => '14px',
408
409
            'success_bg_color'  => 'DFF0D8',
410
            'success_border_color' => 'D6E9C6',
411
            'success_text_color' => '468847',
412
            'success_font_size' => '14px',
413
414
            'important_style'   => false,
415
416
            'custom_css'        => '',
417
        );
418
    }
419
420
	public function get_field_name( $field_name, $post_field = 'post_content' ) {
421
		return 'frm_style_setting' . ( empty( $post_field ) ? '' : '[' . $post_field . ']' ) . '[' . $field_name . ']';
422
	}
423
424
	public static function get_bold_options() {
425
		return array(
426
			100 => 100, 200 => 200, 300 => 300,
427
			'normal' => __( 'normal', 'formidable' ),
428
			500 => 500, 600 => 600,
429
			'bold' => __( 'bold', 'formidable' ),
430
			800 => 800, 900 => 900,
431
		);
432
	}
433
434
435
	/**
436
	 * Don't let imbalanced font families ruin the whole stylesheet
437
	 */
438
	public function force_balanced_quotation( $value ) {
439
		$balanced_characters = array( '"', "'" );
440
		foreach ( $balanced_characters as $char ) {
441
			$char_count = substr_count( $value, $char );
442
			$is_balanced = $char_count % 2 == 0;
443
			if ( ! $is_balanced ) {
444
				$value .= $char;
445
			}
446
		}
447
		return $value;
448
	}
449
}
450