PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.0.10
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.0.10
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 / helpers / FrmStylesHelper.php

FrmStylesHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 5.0.10, at classes/helpers/FrmStylesHelper.php

536 lines 14.3 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 FrmStylesHelper {
7
8 public static function get_upload_base() {
9 $uploads = wp_upload_dir();
10 if ( is_ssl() && ! preg_match( '/^https:\/\/.*\..*$/', $uploads['baseurl'] ) ) {
11 $uploads['baseurl'] = str_replace( 'http://', 'https://', $uploads['baseurl'] );
12 }
13
14 return $uploads;
15 }
16
17 /**
18 * @since 4.0
19 */
20 public static function get_style_menu( $active = '' ) {
21 ob_start();
22 self::style_menu( $active );
23 $menu = ob_get_contents();
24 ob_end_clean();
25
26 return $menu;
27 }
28
29 public static function style_menu( $active = '' ) {
30 ?>
31 <ul class="frm_form_nav">
32 <li>
33 <a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable-styles' ) ); ?>"
34 class="<?php echo ( '' == $active ) ? 'current_page' : ''; ?>">
35 <?php esc_html_e( 'Edit Styles', 'formidable' ); ?>
36 </a>
37 </li>
38 <li>
39 <a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable-styles&frm_action=manage' ) ); ?>"
40 class="<?php echo ( 'manage' == $active ) ? 'current_page' : ''; ?>">
41 <?php esc_html_e( 'Manage Styles', 'formidable' ); ?>
42 </a>
43 </li>
44 <li>
45 <a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable-styles&frm_action=custom_css' ) ); ?>"
46 class="<?php echo ( 'custom_css' == $active ) ? 'current_page' : ''; ?>">
47 <?php esc_html_e( 'Custom CSS', 'formidable' ); ?>
48 </a>
49 </li>
50 </ul>
51 <?php
52 }
53
54 /**
55 * @since 4.0
56 */
57 public static function styler_save_button( $atts ) {
58 $style = $atts['style'];
59 if ( ! empty( $style->ID ) && empty( $style->menu_order ) ) {
60 $delete_link = admin_url( 'admin.php?page=formidable-styles&frm_action=destroy&id=' . $style->ID );
61 }
62
63 include( FrmAppHelper::plugin_path() . '/classes/views/styles/header-buttons.php' );
64 }
65
66 /**
67 * Called from the admin header.
68 *
69 * @since 4.0
70 */
71 public static function save_button() {
72 ?>
73 <input type="submit" name="submit" class="button button-primary frm-button-primary" value="<?php esc_attr_e( 'Update', 'formidable' ); ?>" />
74 <?php
75 }
76
77 /**
78 * Either get the heading or the style switcher.
79 *
80 * @since 4.0
81 */
82 public static function styler_switcher( $atts ) {
83 if ( has_action( 'frm_style_switcher_heading' ) ) {
84 do_action( 'frm_style_switcher_heading', $atts );
85 } else {
86 ?>
87 <div class="frm_top_left">
88 <h1>
89 <?php echo esc_html( $atts['style']->post_title ); ?>
90 </h1>
91 </div>
92 <?php
93 }
94 }
95
96 /**
97 * @since 2.05
98 */
99 public static function get_css_label_positions() {
100 return array(
101 'none' => __( 'top', 'formidable' ),
102 'left' => __( 'left', 'formidable' ),
103 'right' => __( 'right', 'formidable' ),
104 'no_label' => __( 'none', 'formidable' ),
105 'inside' => __( 'inside', 'formidable' ),
106 );
107 }
108
109 public static function get_single_label_positions() {
110 return array(
111 'top' => __( 'Top', 'formidable' ),
112 'left' => __( 'Left', 'formidable' ),
113 'right' => __( 'Right', 'formidable' ),
114 'inline' => __( 'Inline (left without a set width)', 'formidable' ),
115 'none' => __( 'None', 'formidable' ),
116 'hidden' => __( 'Hidden (but leave the space)', 'formidable' ),
117 'inside' => __( 'Placeholder inside the field', 'formidable' ),
118 );
119 }
120
121 public static function minus_icons() {
122 return array(
123 0 => array(
124 '-' => '62e',
125 '+' => '62f',
126 ),
127 1 => array(
128 '-' => '600',
129 '+' => '602',
130 ),
131 2 => array(
132 '-' => '604',
133 '+' => '603',
134 ),
135 3 => array(
136 '-' => '633',
137 '+' => '632',
138 ),
139 4 => array(
140 '-' => '613',
141 '+' => '60f',
142 ),
143 );
144 }
145
146 public static function arrow_icons() {
147 $minus_icons = self::minus_icons();
148
149 return array(
150 6 => array(
151 '-' => '62d',
152 '+' => '62a',
153 ),
154 0 => array(
155 '-' => '60d',
156 '+' => '609',
157 ),
158 1 => array(
159 '-' => '60e',
160 '+' => '60c',
161 ),
162 2 => array(
163 '-' => '630',
164 '+' => '631',
165 ),
166 3 => array(
167 '-' => '62b',
168 '+' => '628',
169 ),
170 4 => array(
171 '-' => '62c',
172 '+' => '629',
173 ),
174 5 => array(
175 '-' => '635',
176 '+' => '634',
177 ),
178 'p0' => $minus_icons[0],
179 'p1' => $minus_icons[1],
180 'p2' => $minus_icons[2],
181 'p3' => $minus_icons[3],
182 'p4' => $minus_icons[4],
183 );
184 }
185
186 /**
187 * @since 2.0
188 * @return string The class for this icon.
189 */
190 public static function icon_key_to_class( $key, $icon = '+', $type = 'arrow' ) {
191 if ( 'arrow' === $type && is_numeric( $key ) ) {
192 //frm_arrowup6_icon
193 $arrow = array(
194 '-' => 'down',
195 '+' => 'up',
196 );
197 $class = 'frm_arrow' . $arrow[ $icon ];
198 } else {
199 //frm_minus1_icon
200 $key = str_replace( 'p', '', $key );
201 $plus = array(
202 '-' => 'minus',
203 '+' => 'plus',
204 );
205 $class = 'frm_' . $plus[ $icon ];
206 }
207
208 if ( $key ) {
209 $class .= $key;
210 }
211 $class .= '_icon';
212
213 return $class;
214 }
215
216 public static function bs_icon_select( $style, $frm_style, $type = 'arrow' ) {
217 $function_name = $type . '_icons';
218 $icons = self::$function_name();
219 unset( $function_name );
220
221 $name = ( 'arrow' == $type ) ? 'collapse_icon' : 'repeat_icon';
222 ?>
223 <div class="btn-group" id="frm_<?php echo esc_attr( $name ); ?>_select">
224 <button class="multiselect dropdown-toggle btn btn-default" data-toggle="dropdown" type="button">
225 <?php FrmAppHelper::icon_by_class( 'frmfont ' . self::icon_key_to_class( $style->post_content[ $name ], '+', $type ) ); ?>
226 <?php FrmAppHelper::icon_by_class( 'frmfont ' . self::icon_key_to_class( $style->post_content[ $name ], '-', $type ) ); ?>
227 <b class="caret"></b>
228 </button>
229 <ul class="multiselect-container frm-dropdown-menu">
230 <?php foreach ( $icons as $key => $icon ) { ?>
231 <li <?php echo ( $style->post_content['collapse_icon'] == $key ) ? 'class="active"' : ''; ?>>
232 <a href="javascript:void(0);">
233 <label>
234 <input type="radio" value="<?php echo esc_attr( $key ); ?>" name="<?php echo esc_attr( $frm_style->get_field_name( $name ) ); ?>" <?php checked( $style->post_content[ $name ], $key ); ?>/>
235 <span>
236 <?php FrmAppHelper::icon_by_class( 'frmfont ' . self::icon_key_to_class( $key, '+', $type ) ); ?>
237 <?php FrmAppHelper::icon_by_class( 'frmfont ' . self::icon_key_to_class( $key, '-', $type ) ); ?>
238 </span>
239 </label>
240 </a>
241 </li>
242 <?php } ?>
243 </ul>
244 </div>
245 <?php
246 }
247
248 public static function hex2rgb( $hex ) {
249 $hex = str_replace( '#', '', $hex );
250
251 list( $r, $g, $b ) = sscanf( $hex, '%02x%02x%02x' );
252
253 $rgb = array( $r, $g, $b );
254
255 return implode( ',', $rgb );
256 }
257
258 /**
259 * @since 4.0
260 */
261 public static function hex2rgba( $hex, $a ) {
262 $rgb = self::hex2rgb( $hex );
263
264 return 'rgba(' . $rgb . ',' . $a . ')';
265 }
266
267 /**
268 * @param $hex string - The original color in hex format #ffffff
269 * @param $steps integer - should be between -255 and 255. Negative = darker, positive = lighter
270 *
271 * @since 2.3
272 */
273 public static function adjust_brightness( $hex, $steps ) {
274 $steps = max( - 255, min( 255, $steps ) );
275
276 // Normalize into a six character long hex string
277 $hex = str_replace( '#', '', $hex );
278 if ( strlen( $hex ) == 3 ) {
279 $hex = str_repeat( substr( $hex, 0, 1 ), 2 );
280 $hex .= str_repeat( substr( $hex, 1, 1 ), 2 );
281 $hex .= str_repeat( substr( $hex, 2, 1 ), 2 );
282 }
283
284 // Split into three parts: R, G and B
285 $color_parts = str_split( $hex, 2 );
286 $return = '#';
287
288 foreach ( $color_parts as $color ) {
289 $color = hexdec( $color ); // Convert to decimal
290 $color = max( 0, min( 255, $color + $steps ) ); // Adjust color
291 $return .= str_pad( dechex( $color ), 2, '0', STR_PAD_LEFT ); // Make two char hex code
292 }
293
294 return $return;
295 }
296
297 /**
298 * @since 4.05.02
299 */
300 public static function get_css_vars( $vars = array() ) {
301 $vars = apply_filters( 'frm_css_vars', $vars );
302 return array_unique( $vars );
303 }
304
305 /**
306 * @since 4.05.02
307 */
308 public static function output_vars( $settings, $defaults = array(), $vars = array() ) {
309 if ( empty( $vars ) ) {
310 $vars = self::get_css_vars( array_keys( $settings ) );
311 }
312 $remove = array( 'remove_box_shadow', 'remove_box_shadow_active', 'theme_css', 'theme_name', 'theme_selector', 'important_style', 'submit_style', 'collapse_icon', 'center_form', 'custom_css', 'style_class', 'submit_bg_img', 'change_margin', 'repeat_icon' );
313 $vars = array_diff( $vars, $remove );
314
315 foreach ( $vars as $var ) {
316 if ( ! isset( $settings[ $var ] ) ) {
317 continue;
318 }
319 if ( ! isset( $defaults[ $var ] ) ) {
320 $defaults[ $var ] = '';
321 }
322 $show = empty( $defaults ) || ( $settings[ $var ] !== '' && $settings[ $var ] !== $defaults[ $var ] );
323 if ( $show ) {
324 echo '--' . esc_html( str_replace( '_', '-', $var ) ) . ':' . ( $var === 'font' ? FrmAppHelper::kses( $settings[ $var ] ) : esc_html( $settings[ $var ] ) ) . ';'; // WPCS: XSS ok.
325 }
326 }
327 }
328
329 /**
330 * @since 2.3
331 *
332 * @param WP_Post $style
333 * @return array
334 */
335 public static function get_settings_for_output( $style ) {
336 if ( self::previewing_style() ) {
337
338 if ( isset( $_POST['frm_style_setting'] ) ) {
339 // Sanitizing is done later.
340 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
341 $posted = wp_unslash( $_POST['frm_style_setting'] );
342 if ( ! is_array( $posted ) ) {
343 $posted = json_decode( $posted, true );
344 FrmAppHelper::format_form_data( $posted );
345 $settings = self::sanitize_settings( $posted['frm_style_setting']['post_content'] );
346 $style_name = sanitize_title( $posted['style_name'] );
347 } else {
348 $settings = self::sanitize_settings( $posted['post_content'] );
349 $style_name = FrmAppHelper::get_post_param( 'style_name', '', 'sanitize_title' );
350 }
351 } else {
352 $settings = self::sanitize_settings( wp_unslash( $_GET ) );
353 $style_name = FrmAppHelper::get_param( 'style_name', '', 'get', 'sanitize_title' );
354 }
355
356 FrmAppHelper::sanitize_value( 'sanitize_text_field', $settings );
357
358 $settings['style_class'] = '';
359 if ( ! empty( $style_name ) ) {
360 $settings['style_class'] = $style_name . '.';
361 }
362 } else {
363 $settings = $style->post_content;
364 $settings['style_class'] = 'frm_style_' . $style->post_name . '.';
365 }
366
367 $settings['style_class'] .= 'with_frm_style';
368 $settings['font'] = stripslashes( $settings['font'] );
369 $settings['change_margin'] = self::description_margin_for_screensize( $settings['width'] );
370
371 $checkbox_opts = array( 'important_style', 'auto_width', 'submit_style', 'collapse_icon', 'center_form' );
372 foreach ( $checkbox_opts as $opt ) {
373 if ( ! isset( $settings[ $opt ] ) ) {
374 $settings[ $opt ] = 0;
375 }
376 }
377
378 self::prepare_color_output( $settings );
379
380 $settings['field_height'] = $settings['field_height'] === '' ? 'auto' : $settings['field_height'];
381 $settings['field_width'] = $settings['field_width'] === '' ? 'auto' : $settings['field_width'];
382 $settings['auto_width'] = $settings['auto_width'] ? 'auto' : $settings['field_width'];
383 $settings['box_shadow'] = ( isset( $settings['remove_box_shadow'] ) && $settings['remove_box_shadow'] ) ? 'none' : '0 1px 1px rgba(0, 0, 0, 0.075) inset';
384
385 if ( ! isset( $settings['repeat_icon'] ) ) {
386 $settings['repeat_icon'] = 1;
387 }
388
389 return $settings;
390 }
391
392 /**
393 * @since 5.0.10
394 *
395 * @param array $settings
396 * @return array
397 */
398 private static function sanitize_settings( $settings ) {
399 $style = new FrmStyle();
400 $defaults = $style->get_defaults();
401 $valid_keys = array_keys( $defaults );
402 $sanitized_settings = array();
403 foreach ( $valid_keys as $key ) {
404 if ( isset( $settings[ $key ] ) ) {
405 $sanitized_settings[ $key ] = sanitize_text_field( $settings[ $key ] );
406 } else {
407 $sanitized_settings[ $key ] = $defaults[ $key ];
408 }
409 }
410 return $sanitized_settings;
411 }
412
413 /**
414 * @since 2.3
415 */
416 public static function prepare_color_output( &$settings, $allow_transparent = true ) {
417 $colors = self::allow_color_override();
418 foreach ( $colors as $css => $opts ) {
419 if ( $css === 'transparent' && ! $allow_transparent ) {
420 $css = '';
421 }
422 foreach ( $opts as $opt ) {
423 self::get_color_output( $css, $settings[ $opt ] );
424 }
425 }
426 }
427
428 /**
429 * @since 2.3
430 */
431 private static function allow_color_override() {
432 $frm_style = new FrmStyle();
433 $colors = $frm_style->get_color_settings();
434
435 $transparent = array(
436 'fieldset_color',
437 'fieldset_bg_color',
438 'bg_color',
439 'section_bg_color',
440 'error_bg',
441 'success_bg_color',
442 'progress_bg_color',
443 'progress_active_bg_color',
444 );
445
446 return array(
447 'transparent' => $transparent,
448 '' => array_diff( $colors, $transparent ),
449 );
450 }
451
452 /**
453 * @since 2.3
454 */
455 private static function get_color_output( $default, &$color ) {
456 $color = trim( $color );
457 if ( empty( $color ) ) {
458 $color = $default;
459 } elseif ( false !== strpos( $color, 'rgb(' ) ) {
460 $color = str_replace( 'rgb(', 'rgba(', $color );
461 $color = str_replace( ')', ',1)', $color );
462 } elseif ( strpos( $color, '#' ) === false && false === strpos( $color, 'rgba(' ) ) {
463 $color = '#' . $color;
464 }
465 }
466
467 /**
468 * If left/right label is over a certain size,
469 * adjust the field description margin at a different screen size
470 *
471 * @since 2.3
472 */
473 private static function description_margin_for_screensize( $width ) {
474 $temp_label_width = str_replace( 'px', '', $width );
475 $change_margin = false;
476 if ( $temp_label_width >= 230 ) {
477 $change_margin = '800px';
478 } elseif ( $width >= 215 ) {
479 $change_margin = '700px';
480 } elseif ( $width >= 180 ) {
481 $change_margin = '650px';
482 }
483
484 return $change_margin;
485 }
486
487 /**
488 * @since 2.3
489 */
490 public static function previewing_style() {
491 $ajax_change = isset( $_POST['action'] ) && $_POST['action'] === 'frm_change_styling' && isset( $_POST['frm_style_setting'] );
492
493 return $ajax_change || isset( $_GET['flat'] );
494 }
495
496 /**
497 * @deprecated 3.01
498 * @codeCoverageIgnore
499 */
500 public static function get_sigle_label_postitions() {
501 return FrmDeprecated::get_sigle_label_postitions();
502 }
503
504 /**
505 * @deprecated 3.02.03
506 * @codeCoverageIgnore
507 */
508 public static function jquery_themes() {
509 return FrmDeprecated::jquery_themes();
510 }
511
512 /**
513 * @deprecated 3.02.03
514 * @codeCoverageIgnore
515 */
516 public static function jquery_css_url( $theme_css ) {
517 return FrmDeprecated::jquery_css_url( $theme_css );
518 }
519
520 /**
521 * @deprecated 3.02.03
522 * @codeCoverageIgnore
523 */
524 public static function enqueue_jquery_css() {
525 FrmDeprecated::enqueue_jquery_css();
526 }
527
528 /**
529 * @deprecated 3.02.03
530 * @codeCoverageIgnore
531 */
532 public static function get_form_for_page() {
533 return FrmDeprecated::get_form_for_page();
534 }
535 }
536