PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 5.2.9
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v5.2.9
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Modules / Form / Settings / FormCssJs.php

FormCssJs.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 5.2.9, at app/Modules/Form/Settings/FormCssJs.php

256 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Modules\Form\Settings;
4
5 use FluentForm\App\Helpers\Helper;
6 use FluentForm\Framework\Helpers\ArrayHelper;
7
8 class FormCssJs
9 {
10 /**
11 * Request object
12 *
13 * @var \FluentForm\Framework\Request\Request $request
14 */
15 protected $request;
16
17 public function __construct()
18 {
19 $this->request = wpFluentForm('request');
20 }
21
22 public function addCustomCssJs($formId)
23 {
24 if (did_action('fluentform/adding_custom_css_js_' . $formId)) {
25 return;
26 }
27
28 do_action('fluentform/adding_custom_css_js_' . $formId, $formId);
29
30 $metaKeys = ['_custom_form_css', '_custom_form_js'];
31
32 $metas = (new \FluentForm\App\Services\Settings\Customizer())->get($formId, $metaKeys);
33
34 foreach ($metas as $metaKey => $metaValue) {
35 if ($metaValue) {
36 switch ($metaKey) {
37 case 'css':
38 $css = $metaValue;
39 $css = str_replace('{form_id}', $formId, $css);
40 $customCss = str_replace('FF_ID', $formId, $css);
41
42 if ($customCss) {
43 $this->addCss($formId, $customCss, 'fluentform_custom_css_' . $formId);
44 }
45 break;
46 case 'js':
47 $this->addJs($formId, $metaValue);
48 break;
49 }
50 }
51 }
52 }
53
54 public function addStylerCSS($formId, $styles = [])
55 {
56 $metaKeys = array_merge(
57 ['_ff_form_styler_css', '_ff_selected_style'],
58 $styles
59 );
60
61 $metas = (new \FluentForm\App\Services\Settings\Customizer())->get($formId, $metaKeys);
62
63 foreach ($styles as $style) {
64 if (!$style) {
65 continue;
66 }
67
68 if ('ffs_inherit_theme' === $style) {
69 continue;
70 }
71
72 $loadCss = ArrayHelper::get($metas, $style);
73
74 if (!$loadCss) {
75 $loadCss = apply_filters('fluentform/build_style_from_theme', '', $formId, $style);
76
77 // todo: remove this from next version. it's only here to support if the user updates the free version first.
78 if (!$loadCss) {
79 $selectedStyle = ArrayHelper::get($metas, '_ff_selected_style');
80 $selectedStyleCSS = ArrayHelper::get($metas, '_ff_form_styler_css');
81
82 if ($selectedStyle == $style && $selectedStyleCSS) {
83 $loadCss = $selectedStyleCSS;
84 }
85 }
86 }
87
88 if ($loadCss) {
89 $this->addCss($formId, $loadCss, 'fluentform_styler_css_' . $formId . '_' . $style);
90
91 do_action('fluent_form/loaded_styler_' . $formId . '_' . $style);
92 }
93 }
94 }
95
96 public function getCss($formId)
97 {
98 $cssMeta = wpFluent()->table('fluentform_form_meta')
99 ->where('form_id', $formId)
100 ->where('meta_key', '_custom_form_css')
101 ->first();
102
103 if (!$cssMeta || !$cssMeta->value) {
104 return '';
105 }
106
107 $css = $cssMeta->value;
108 $css = str_replace('{form_id}', $formId, $css);
109 $css = str_replace('FF_ID', $formId, $css);
110 return fluentformSanitizeCSS($css);
111 }
112
113 public function getJs($formId)
114 {
115 $jsMeta = wpFluent()->table('fluentform_form_meta')
116 ->where('form_id', $formId)
117 ->where('meta_key', '_custom_form_js')
118 ->first();
119
120 if (!$jsMeta || !$jsMeta->value) {
121 return '';
122 }
123
124 return $jsMeta->value;
125 }
126
127 public function addCss($formId, $css, $cssId = 'fluentform_custom_css')
128 {
129 if ($css) {
130 $action = false;
131
132 if (!did_action('wp_head')) {
133 $action = 'wp_head';
134 } elseif (!did_action('wp_footer')) {
135 $action = 'wp_footer';
136 }
137
138 if (Helper::isBlockEditor()) {
139 $action = false;
140 }
141
142 if ($action) {
143 add_action($action, function () use ($css, $cssId) {
144 ?>
145 <style id="<?php echo esc_attr($cssId); ?>" type="text/css">
146 <?php echo fluentformSanitizeCSS($css); ?>
147 </style>
148
149 <?php
150 }, 99);
151 } else {
152 ?>
153 <style id="<?php echo esc_attr($cssId); ?>" type="text/css">
154 <?php echo fluentformSanitizeCSS($css); ?>
155 </style>
156 <?php
157 }
158 }
159 }
160
161 public function addJs($formId, $customJS)
162 {
163 if (trim($customJS)) {
164 add_action('wp_footer', function () use ($formId, $customJS) {
165 ?>
166 <script type="text/javascript">
167 jQuery(document.body).on('fluentform_init_<?php echo esc_attr($formId); ?>',
168 function(event, data) {
169 var $form = jQuery(data[0]);
170 var formId = "<?php echo esc_attr($formId); ?>";
171 var $ = jQuery;
172 try {
173 <?php echo fluentform_kses_js($customJS); ?>
174 } catch (e) {
175 console.warn('Error in custom JS of Fluentform ID: ' + formId);
176 console.error(e);
177 }
178 });
179 </script>
180 <?php
181 }, 100);
182 }
183 }
184
185 /**
186 * Get settings for a particular form by id
187 */
188 public function getSettingsAjax()
189 {
190 $formId = absint($this->request->get('form_id'));
191 wp_send_json_success([
192 'custom_css' => $this->getData($formId, '_custom_form_css'),
193 'custom_js' => $this->getData($formId, '_custom_form_js'),
194 ], 200);
195 }
196
197 /**
198 * Save settings for a particular form by id
199 */
200 public function saveSettingsAjax()
201 {
202 if (!fluentformCanUnfilteredHTML()) {
203 wp_send_json_error([
204 'message' => __('You need unfiltered_html permission to save Custom CSS & JS', 'fluentform'),
205 ], 423);
206 }
207
208 $formId = absint($this->request->get('form_id'));
209
210 $css = fluentformSanitizeCSS($this->request->get('custom_css'));
211 $js = fluentform_kses_js($this->request->get('custom_js'));
212
213 $this->store($formId, '_custom_form_css', $css);
214 $this->store($formId, '_custom_form_js', $js);
215
216 wp_send_json_success([
217 'message' => __('Custom CSS & JS successfully updated', 'fluentform'),
218 ], 200);
219 }
220
221 protected function getData($formId, $metaKey)
222 {
223 $row = wpFluent()->table('fluentform_form_meta')
224 ->where('form_id', $formId)
225 ->where('meta_key', $metaKey)
226 ->first();
227 if ($row) {
228 return $row->value;
229 }
230 return '';
231 }
232
233 protected function store($formId, $metaKey, $metaValue)
234 {
235 $row = wpFluent()->table('fluentform_form_meta')
236 ->where('form_id', $formId)
237 ->where('meta_key', $metaKey)
238 ->first();
239
240 if (!$row) {
241 return wpFluent()->table('fluentform_form_meta')
242 ->insertGetId([
243 'form_id' => $formId,
244 'meta_key' => $metaKey,
245 'value' => $metaValue,
246 ]);
247 }
248
249 return wpFluent()->table('fluentform_form_meta')
250 ->where('id', $row->id)
251 ->update([
252 'value' => $metaValue,
253 ]);
254 }
255 }
256