PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.19
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.19
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 3.6.66 All 195 releases
fluentform / app / Modules / Form / Settings / FormCssJs.php

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

200 lines 5.9 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 $formId = absint($this->request->get('form_id'));
153
154 $css = fluentformSanitizeCSS($this->request->get('custom_css'));
155 $js = fluentform_kses_js($this->request->get('custom_js'));
156
157 $this->store($formId, '_custom_form_css', $css);
158 $this->store($formId, '_custom_form_js', $js);
159
160 wp_send_json_success([
161 'message' => __('Custom CSS and JS successfully updated', 'fluentform'),
162 ], 200);
163 }
164
165 protected function getData($formId, $metaKey)
166 {
167 $row = wpFluent()->table('fluentform_form_meta')
168 ->where('form_id', $formId)
169 ->where('meta_key', $metaKey)
170 ->first();
171 if ($row) {
172 return $row->value;
173 }
174 return '';
175 }
176
177 protected function store($formId, $metaKey, $metaValue)
178 {
179 $row = wpFluent()->table('fluentform_form_meta')
180 ->where('form_id', $formId)
181 ->where('meta_key', $metaKey)
182 ->first();
183
184 if (!$row) {
185 return wpFluent()->table('fluentform_form_meta')
186 ->insert([
187 'form_id' => $formId,
188 'meta_key' => $metaKey,
189 'value' => $metaValue,
190 ]);
191 }
192
193 return wpFluent()->table('fluentform_form_meta')
194 ->where('id', $row->id)
195 ->update([
196 'value' => $metaValue,
197 ]);
198 }
199 }
200