PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.21
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.21
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 4.3.21, at app/Modules/Form/Settings/FormCssJs.php

206 lines 6.1 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
7 class FormCssJs
8 {
9 /**
10 * Request object
11 *
12 * @var \FluentForm\Framework\Request\Request $request
13 */
14 protected $request;
15
16 public function __construct()
17 {
18 $this->request = wpFluentForm('request');
19 }
20
21 public function addCssJs($formId)
22 {
23 // @todo: Limit 3 sometimes make things double
24 $metas = wpFluent()->table('fluentform_form_meta')
25 ->where('form_id', $formId)
26 ->whereIn('meta_key', [
27 '_custom_form_css',
28 '_custom_form_js',
29 '_ff_form_styler_css',
30 ])
31 ->groupBy('meta_key')
32 //->limit(3)
33 ->get();
34
35 if (!$metas) {
36 return;
37 }
38
39 foreach ($metas as $meta) {
40 if ($meta->value) {
41 if ('_custom_form_css' == $meta->meta_key) {
42 $css = $meta->value;
43 $css = str_replace('{form_id}', $formId, $css);
44 $css = str_replace('FF_ID', $formId, $css);
45 $this->addCss($formId, $css, 'fluentform_custom_css_' . $formId);
46 } elseif ('_ff_form_styler_css' == $meta->meta_key) {
47 $css = $meta->value;
48 $this->addCss($formId, $css, 'fluentform_styler_css_' . $formId);
49 } elseif ('_custom_form_js' == $meta->meta_key) {
50 $this->addJs($formId, $meta->value);
51 }
52 }
53 }
54 }
55
56 public function getCss($formId)
57 {
58 $cssMeta = wpFluent()->table('fluentform_form_meta')
59 ->where('form_id', $formId)
60 ->where('meta_key', '_custom_form_css')
61 ->first();
62
63 if (!$cssMeta || !$cssMeta->value) {
64 return '';
65 }
66
67 $css = $cssMeta->value;
68 $css = str_replace('{form_id}', $formId, $css);
69 $css = str_replace('FF_ID', $formId, $css);
70 return fluentformSanitizeCSS($css);
71 }
72
73 public function getJs($formId)
74 {
75 $jsMeta = wpFluent()->table('fluentform_form_meta')
76 ->where('form_id', $formId)
77 ->where('meta_key', '_custom_form_js')
78 ->first();
79
80 if (!$jsMeta || !$jsMeta->value) {
81 return '';
82 }
83
84 return $jsMeta->value;
85 }
86
87 public function addCss($formId, $css, $cssId = 'fluentform_custom_css')
88 {
89 if ($css) {
90 if (!did_action('wp_head')) {
91 add_action('wp_head', function () use ($css, $formId, $cssId) {
92 ?>
93
94 <style id="<?php echo esc_attr($cssId); ?>" type="text/css">
95 <?php echo fluentformSanitizeCSS($css); ?>
96 </style>
97
98 <?php
99 }, 10);
100 } else {
101 ?>
102
103 <style id="<?php echo esc_attr($cssId); ?>" type="text/css">
104 <?php echo fluentformSanitizeCSS($css); ?>
105 </style>
106 <?php
107 }
108 }
109 }
110
111 public function addJs($formId, $customJS)
112 {
113 if (trim($customJS)) {
114 add_action('wp_footer', function () use ($formId, $customJS) {
115 ?>
116 <script type="text/javascript">
117 jQuery(document.body).on('fluentform_init_<?php echo esc_attr($formId); ?>',
118 function(event, data) {
119 var $form = jQuery(data[0]);
120 var formId = "<?php echo esc_attr($formId); ?>";
121 var $ = jQuery;
122 try {
123 <?php echo fluentform_kses_js($customJS); ?>
124 } catch (e) {
125 console.warn('Error in custom JS of Fluentform ID: ' + formId);
126 console.error(e);
127 }
128 });
129 </script>
130 <?php
131 }, 100);
132 }
133 }
134
135 /**
136 * Get settings for a particular form by id
137 */
138 public function getSettingsAjax()
139 {
140 $formId = absint($this->request->get('form_id'));
141 wp_send_json_success([
142 'custom_css' => $this->getData($formId, '_custom_form_css'),
143 'custom_js' => $this->getData($formId, '_custom_form_js'),
144 ], 200);
145 }
146
147 /**
148 * Save settings for a particular form by id
149 */
150 public function saveSettingsAjax()
151 {
152 if (!fluentformCanUnfilteredHTML()) {
153 wp_send_json_error([
154 'message' => __('You need unfiltered_html permission to save Custom CSS & JS', 'fluentform'),
155 ], 423);
156 }
157
158 $formId = absint($this->request->get('form_id'));
159
160 $css = fluentformSanitizeCSS($this->request->get('custom_css'));
161 $js = fluentform_kses_js($this->request->get('custom_js'));
162
163 $this->store($formId, '_custom_form_css', $css);
164 $this->store($formId, '_custom_form_js', $js);
165
166 wp_send_json_success([
167 'message' => __('Custom CSS & JS successfully updated', 'fluentform'),
168 ], 200);
169 }
170
171 protected function getData($formId, $metaKey)
172 {
173 $row = wpFluent()->table('fluentform_form_meta')
174 ->where('form_id', $formId)
175 ->where('meta_key', $metaKey)
176 ->first();
177 if ($row) {
178 return $row->value;
179 }
180 return '';
181 }
182
183 protected function store($formId, $metaKey, $metaValue)
184 {
185 $row = wpFluent()->table('fluentform_form_meta')
186 ->where('form_id', $formId)
187 ->where('meta_key', $metaKey)
188 ->first();
189
190 if (!$row) {
191 return wpFluent()->table('fluentform_form_meta')
192 ->insert([
193 'form_id' => $formId,
194 'meta_key' => $metaKey,
195 'value' => $metaValue,
196 ]);
197 }
198
199 return wpFluent()->table('fluentform_form_meta')
200 ->where('id', $row->id)
201 ->update([
202 'value' => $metaValue,
203 ]);
204 }
205 }
206