PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.8.2
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.8.2
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / inc / form-styling.php
form-styling.php
183 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Form Styling Handler.
4 *
5 * Handles form styling customization for both embed (srfm/form block)
6 * and instant forms. Maps block attributes to form styling array and
7 * provides filter hooks for Pro to extend with theme functionality.
8 *
9 * @package sureforms
10 * @since 2.7.0
11 */
12
13 namespace SRFM\Inc;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 /**
20 * Form_Styling class.
21 *
22 * @since 2.7.0
23 */
24 class Form_Styling {
25 /**
26 * Map block attributes to form styling array.
27 *
28 * Converts camelCase block attributes to snake_case form styling keys.
29 * This allows per-embed customization when formTheme is not 'inherit'.
30 *
31 * @param array<string,mixed> $form_styling Existing form styling from post meta.
32 * @param array<string,mixed> $block_attrs Block attributes from srfm/form block.
33 * @return array<string,mixed> Modified form styling array.
34 * @since 2.7.0
35 */
36 public static function map_block_attrs_to_styling( $form_styling, $block_attrs ) {
37 if ( empty( $block_attrs ) ) {
38 return $form_styling;
39 }
40
41 // Form Theme - allows Pro to add custom themes.
42 if ( ! empty( $block_attrs['formTheme'] ) ) {
43 $form_styling['form_theme'] = Helper::get_string_value( $block_attrs['formTheme'] );
44
45 // Apply theme styling if a theme is selected.
46 $form_styling = self::apply_theme_styling( $form_styling, Helper::get_string_value( $block_attrs['formTheme'] ) );
47 }
48
49 // Colors.
50 if ( ! empty( $block_attrs['primaryColor'] ) ) {
51 $form_styling['primary_color'] = Helper::sanitize_css_value( $block_attrs['primaryColor'] );
52 }
53 if ( ! empty( $block_attrs['textColor'] ) ) {
54 $form_styling['text_color'] = Helper::sanitize_css_value( $block_attrs['textColor'] );
55 }
56 if ( ! empty( $block_attrs['textOnPrimaryColor'] ) ) {
57 $form_styling['text_color_on_primary'] = Helper::sanitize_css_value( $block_attrs['textOnPrimaryColor'] );
58 }
59
60 // Padding.
61 $form_styling = self::map_dimension_attrs( $form_styling, $block_attrs, 'formPadding', 'form_padding' );
62
63 // Border Radius.
64 $form_styling = self::map_dimension_attrs( $form_styling, $block_attrs, 'formBorderRadius', 'form_border_radius' );
65
66 // Background.
67 if ( ! empty( $block_attrs['bgType'] ) && in_array( $block_attrs['bgType'], [ 'color', 'gradient', 'image' ], true ) ) {
68 $form_styling['bg_type'] = $block_attrs['bgType'];
69 }
70 if ( ! empty( $block_attrs['bgColor'] ) ) {
71 $form_styling['bg_color'] = Helper::sanitize_css_value( $block_attrs['bgColor'] );
72 }
73 if ( ! empty( $block_attrs['bgGradient'] ) ) {
74 $form_styling['bg_gradient'] = Helper::sanitize_css_value( $block_attrs['bgGradient'] );
75 }
76 if ( ! empty( $block_attrs['bgImage'] ) ) {
77 $form_styling['bg_image'] = esc_url_raw( Helper::get_string_value( $block_attrs['bgImage'] ) );
78 }
79 if ( ! empty( $block_attrs['bgImagePosition'] ) ) {
80 $form_styling['bg_image_position'] = $block_attrs['bgImagePosition'];
81 }
82 if ( ! empty( $block_attrs['bgImageSize'] ) && in_array( $block_attrs['bgImageSize'], [ 'auto', 'cover', 'contain' ], true ) ) {
83 $form_styling['bg_image_size'] = $block_attrs['bgImageSize'];
84 }
85 if ( ! empty( $block_attrs['bgImageRepeat'] ) && in_array( $block_attrs['bgImageRepeat'], [ 'repeat', 'no-repeat', 'repeat-x', 'repeat-y' ], true ) ) {
86 $form_styling['bg_image_repeat'] = $block_attrs['bgImageRepeat'];
87 }
88 if ( ! empty( $block_attrs['bgImageAttachment'] ) && in_array( $block_attrs['bgImageAttachment'], [ 'scroll', 'fixed', 'local' ], true ) ) {
89 $form_styling['bg_image_attachment'] = $block_attrs['bgImageAttachment'];
90 }
91
92 // Field Spacing and Button Alignment.
93 if ( ! empty( $block_attrs['fieldSpacing'] ) && in_array( $block_attrs['fieldSpacing'], [ 'small', 'medium', 'large' ], true ) ) {
94 $form_styling['field_spacing'] = $block_attrs['fieldSpacing'];
95 }
96 if ( ! empty( $block_attrs['buttonAlignment'] ) && in_array( $block_attrs['buttonAlignment'], [ 'left', 'center', 'right', 'full', 'justify' ], true ) ) {
97 $form_styling['submit_button_alignment'] = $block_attrs['buttonAlignment'];
98 }
99
100 /**
101 * Filter to allow Pro to extend block attribute mapping.
102 *
103 * @param array<string,mixed> $form_styling Modified form styling array.
104 * @param array<string,mixed> $block_attrs Original block attributes.
105 * @since 2.7.0
106 */
107 return apply_filters( 'srfm_embed_block_attrs_to_styling', $form_styling, $block_attrs );
108 }
109
110 /**
111 * Apply theme styling to form styling array.
112 *
113 * Returns theme-specific CSS variable values. In the free version,
114 * this returns the original styling. Pro overrides via filter to
115 * apply predefined theme presets.
116 *
117 * @param array<string,mixed> $form_styling Current form styling array.
118 * @param string $theme_slug Theme identifier (e.g., 'modern', 'minimal').
119 * @return array<string,mixed> Form styling with theme values applied.
120 * @since 2.7.0
121 */
122 public static function apply_theme_styling( $form_styling, $theme_slug ) {
123 if ( empty( $theme_slug ) || 'default' === $theme_slug || 'inherit' === $theme_slug ) {
124 return $form_styling;
125 }
126
127 /**
128 * Filter to apply theme-specific styling.
129 *
130 * Pro uses this filter to merge predefined theme CSS variables
131 * into the form styling array.
132 *
133 * @param array<string,mixed> $form_styling Current form styling array.
134 * @param string $theme_slug Theme identifier.
135 * @since 2.7.0
136 */
137 return apply_filters( 'srfm_apply_form_theme_styling', $form_styling, $theme_slug );
138 }
139
140 /**
141 * Check if embed has custom styling (formTheme is not 'inherit').
142 *
143 * @param array<string,mixed> $block_attrs Block attributes.
144 * @return bool True if using custom embed styling.
145 * @since 2.7.0
146 */
147 public static function has_custom_styling( $block_attrs ) {
148 return 'inherit' !== ( $block_attrs['formTheme'] ?? 'inherit' );
149 }
150
151 /**
152 * Map dimension block attributes (Top/Right/Bottom/Left/Unit) to form styling.
153 *
154 * @param array<string,mixed> $form_styling Form styling array.
155 * @param array<string,mixed> $block_attrs Block attributes.
156 * @param string $attr_prefix Block attribute prefix (e.g., 'formPadding').
157 * @param string $style_prefix Form styling key prefix (e.g., 'form_padding').
158 * @return array<string,mixed> Modified form styling array.
159 * @since 2.7.0
160 */
161 private static function map_dimension_attrs( $form_styling, $block_attrs, $attr_prefix, $style_prefix ) {
162 $sides = [ 'Top', 'Right', 'Bottom', 'Left' ];
163
164 foreach ( $sides as $side ) {
165 $attr_key = $attr_prefix . $side;
166 $style_key = $style_prefix . '_' . strtolower( $side );
167
168 if ( isset( $block_attrs[ $attr_key ] ) && is_scalar( $block_attrs[ $attr_key ] ) ) {
169 $form_styling[ $style_key ] = floatval( $block_attrs[ $attr_key ] );
170 }
171 }
172
173 $unit_attr_key = $attr_prefix . 'Unit';
174 $unit_style_key = $style_prefix . '_unit';
175
176 if ( ! empty( $block_attrs[ $unit_attr_key ] ) && in_array( $block_attrs[ $unit_attr_key ], [ 'px', 'em', 'rem', '%', 'vw', 'vh' ], true ) ) {
177 $form_styling[ $unit_style_key ] = $block_attrs[ $unit_attr_key ];
178 }
179
180 return $form_styling;
181 }
182 }
183