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

533 lines 14.7 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 if ( 0 === strpos( $hex, 'rgba(' ) ) {
277 $rgba = str_replace( ')', '', str_replace( 'rgba(', '', $hex ) );
278 list ( $r, $g, $b, $a ) = array_map( 'trim', explode( ',', $rgba ) );
279 $r = max( 0, min( 255, $r + $steps ) );
280 $g = max( 0, min( 255, $g + $steps ) );
281 $b = max( 0, min( 255, $b + $steps ) );
282 return 'rgba(' . $r . ',' . $g . ',' . $b . ',' . $a . ')';
283 }
284
285 // Normalize into a six character long hex string
286 $hex = str_replace( '#', '', $hex );
287 if ( strlen( $hex ) == 3 ) {
288 $hex = str_repeat( substr( $hex, 0, 1 ), 2 );
289 $hex .= str_repeat( substr( $hex, 1, 1 ), 2 );
290 $hex .= str_repeat( substr( $hex, 2, 1 ), 2 );
291 }
292
293 // Split into three parts: R, G and B
294 $color_parts = str_split( $hex, 2 );
295 $return = '#';
296
297 foreach ( $color_parts as $color ) {
298 $color = hexdec( $color ); // Convert to decimal
299 $color = max( 0, min( 255, $color + $steps ) ); // Adjust color
300 $return .= str_pad( dechex( $color ), 2, '0', STR_PAD_LEFT ); // Make two char hex code
301 }
302
303 return $return;
304 }
305
306 /**
307 * @since 4.05.02
308 */
309 public static function get_css_vars( $vars = array() ) {
310 $vars = apply_filters( 'frm_css_vars', $vars );
311 return array_unique( $vars );
312 }
313
314 /**
315 * @since 4.05.02
316 */
317 public static function output_vars( $settings, $defaults = array(), $vars = array() ) {
318 if ( empty( $vars ) ) {
319 $vars = self::get_css_vars( array_keys( $settings ) );
320 }
321 $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' );
322 $vars = array_diff( $vars, $remove );
323
324 foreach ( $vars as $var ) {
325 if ( ! isset( $settings[ $var ] ) ) {
326 continue;
327 }
328 if ( ! isset( $defaults[ $var ] ) ) {
329 $defaults[ $var ] = '';
330 }
331 $show = empty( $defaults ) || ( $settings[ $var ] !== '' && $settings[ $var ] !== $defaults[ $var ] );
332 if ( $show ) {
333 echo '--' . esc_html( str_replace( '_', '-', $var ) ) . ':' . ( $var === 'font' ? FrmAppHelper::kses( $settings[ $var ] ) : esc_html( $settings[ $var ] ) ) . ';'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
334 }
335 }
336 }
337
338 /**
339 * @since 2.3
340 *
341 * @param WP_Post $style
342 * @return array
343 */
344 public static function get_settings_for_output( $style ) {
345 if ( self::previewing_style() ) {
346
347 $frm_style = new FrmStyle();
348 if ( isset( $_POST['frm_style_setting'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
349
350 // Sanitizing is done later.
351 $posted = wp_unslash( $_POST['frm_style_setting'] ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing
352 if ( ! is_array( $posted ) ) {
353 $posted = json_decode( $posted, true );
354 FrmAppHelper::format_form_data( $posted );
355 $settings = $frm_style->sanitize_post_content( $posted['frm_style_setting']['post_content'] );
356 $style_name = sanitize_title( $posted['style_name'] );
357 } else {
358 $settings = $frm_style->sanitize_post_content( $posted['post_content'] );
359 $style_name = FrmAppHelper::get_post_param( 'style_name', '', 'sanitize_title' );
360 }
361 } else {
362 $settings = $frm_style->sanitize_post_content( wp_unslash( $_GET ) );
363 $style_name = FrmAppHelper::get_param( 'style_name', '', 'get', 'sanitize_title' );
364 }
365
366 FrmAppHelper::sanitize_value( 'sanitize_text_field', $settings );
367
368 $settings['style_class'] = '';
369 if ( ! empty( $style_name ) ) {
370 $settings['style_class'] = $style_name . '.';
371 }
372 } else {
373 $settings = $style->post_content;
374 $settings['style_class'] = 'frm_style_' . $style->post_name . '.';
375 }
376
377 $settings['style_class'] .= 'with_frm_style';
378 $settings['font'] = stripslashes( $settings['font'] );
379 $settings['change_margin'] = self::description_margin_for_screensize( $settings['width'] );
380
381 $checkbox_opts = array( 'important_style', 'auto_width', 'submit_style', 'collapse_icon', 'center_form' );
382 foreach ( $checkbox_opts as $opt ) {
383 if ( ! isset( $settings[ $opt ] ) ) {
384 $settings[ $opt ] = 0;
385 }
386 }
387
388 self::prepare_color_output( $settings );
389
390 $settings['field_height'] = $settings['field_height'] === '' ? 'auto' : $settings['field_height'];
391 $settings['field_width'] = $settings['field_width'] === '' ? 'auto' : $settings['field_width'];
392 $settings['auto_width'] = $settings['auto_width'] ? 'auto' : $settings['field_width'];
393 $settings['box_shadow'] = ( isset( $settings['remove_box_shadow'] ) && $settings['remove_box_shadow'] ) ? 'none' : '0 1px 1px rgba(0, 0, 0, 0.075) inset';
394
395 if ( ! isset( $settings['repeat_icon'] ) ) {
396 $settings['repeat_icon'] = 1;
397 }
398
399 return $settings;
400 }
401
402 /**
403 * @since 2.3
404 */
405 public static function prepare_color_output( &$settings, $allow_transparent = true ) {
406 $colors = self::allow_color_override();
407 foreach ( $colors as $css => $opts ) {
408 if ( $css === 'transparent' && ! $allow_transparent ) {
409 $css = '';
410 }
411 foreach ( $opts as $opt ) {
412 self::get_color_output( $css, $settings[ $opt ] );
413 }
414 }
415 }
416
417 /**
418 * @since 2.3
419 */
420 private static function allow_color_override() {
421 $frm_style = new FrmStyle();
422 $colors = $frm_style->get_color_settings();
423
424 $transparent = array(
425 'fieldset_color',
426 'fieldset_bg_color',
427 'bg_color',
428 'bg_color_active',
429 'bg_color_disabled',
430 'section_bg_color',
431 'error_bg',
432 'success_bg_color',
433 'progress_bg_color',
434 'progress_active_bg_color',
435 'submit_border_color',
436 'submit_hover_border_color',
437 'submit_active_border_color',
438 'submit_hover_bg_color',
439 'submit_active_bg_color',
440 'success_bg_color',
441 );
442
443 return array(
444 'transparent' => $transparent,
445 '' => array_diff( $colors, $transparent ),
446 );
447 }
448
449 /**
450 * @since 2.3
451 */
452 private static function get_color_output( $default, &$color ) {
453 $color = trim( $color );
454 if ( empty( $color ) ) {
455 $color = $default;
456 } elseif ( false !== strpos( $color, 'rgb(' ) ) {
457 $color = str_replace( 'rgb(', 'rgba(', $color );
458 $color = str_replace( ')', ',1)', $color );
459 } elseif ( strpos( $color, '#' ) === false && false === strpos( $color, 'rgba(' ) ) {
460 $color = '#' . $color;
461 }
462 }
463
464 /**
465 * If left/right label is over a certain size,
466 * adjust the field description margin at a different screen size
467 *
468 * @since 2.3
469 */
470 private static function description_margin_for_screensize( $width ) {
471 $temp_label_width = str_replace( 'px', '', $width );
472 $change_margin = false;
473 if ( $temp_label_width >= 230 ) {
474 $change_margin = '800px';
475 } elseif ( $width >= 215 ) {
476 $change_margin = '700px';
477 } elseif ( $width >= 180 ) {
478 $change_margin = '650px';
479 }
480
481 return $change_margin;
482 }
483
484 /**
485 * @since 2.3
486 */
487 public static function previewing_style() {
488 $ajax_change = isset( $_POST['action'] ) && $_POST['action'] === 'frm_change_styling' && isset( $_POST['frm_style_setting'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
489
490 return $ajax_change || isset( $_GET['flat'] );
491 }
492
493 /**
494 * @deprecated 3.01
495 * @codeCoverageIgnore
496 */
497 public static function get_sigle_label_postitions() {
498 return FrmDeprecated::get_sigle_label_postitions();
499 }
500
501 /**
502 * @deprecated 3.02.03
503 * @codeCoverageIgnore
504 */
505 public static function jquery_themes() {
506 return FrmDeprecated::jquery_themes();
507 }
508
509 /**
510 * @deprecated 3.02.03
511 * @codeCoverageIgnore
512 */
513 public static function jquery_css_url( $theme_css ) {
514 return FrmDeprecated::jquery_css_url( $theme_css );
515 }
516
517 /**
518 * @deprecated 3.02.03
519 * @codeCoverageIgnore
520 */
521 public static function enqueue_jquery_css() {
522 FrmDeprecated::enqueue_jquery_css();
523 }
524
525 /**
526 * @deprecated 3.02.03
527 * @codeCoverageIgnore
528 */
529 public static function get_form_for_page() {
530 return FrmDeprecated::get_form_for_page();
531 }
532 }
533