PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 2.05.09
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v2.05.09
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / FrmStyle.php

FrmStyle.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 2.05.09, at classes/models/FrmStyle.php

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