PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 4.01.02
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v4.01.02
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 4.01.02, at classes/models/FrmStyle.php

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