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

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