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

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