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

157 lines 4.6 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 public function addCssJs($formId)
10 {
11 $metas = wpFluent()->table('fluentform_form_meta')
12 ->where('form_id', $formId)
13 ->whereIn('meta_key', [
14 '_custom_form_css',
15 '_custom_form_js',
16 '_ff_form_styler_css'
17 ])
18 ->limit(3)
19 ->get();
20
21 if (!$metas) {
22 return;
23 }
24
25
26 foreach ($metas as $meta) {
27 if ($meta->meta_key == '_custom_form_css' && $meta->value) {
28 $css = $meta->value;
29 $css = str_replace('{form_id}', $formId, $css);
30 $css = str_replace('FF_ID', $formId, $css);
31 $this->addCss($formId, $css, 'fluentform_custom_css_'.$formId );
32 } else if(($meta->meta_key == '_ff_form_styler_css' && $meta->value)) {
33 $css = $meta->value;
34 $this->addCss($formId, $css, 'fluentform_styler_css_'.$formId );
35 } else if ($meta->meta_key == '_custom_form_js' && $meta->value) {
36 $this->addJs($formId, $meta->value);
37 }
38 }
39 }
40
41 public function addCss($formId, $css, $cssId = 'fluentform_custom_css')
42 {
43 if ($css) {
44 if (!did_action('wp_head')) {
45 add_action('wp_head', function () use ($css, $formId, $cssId) {
46 ?>
47
48 <style id="<?php echo $cssId; ?>" type="text/css">
49 <?php echo $css; ?>
50 </style>
51 <?php
52 }, 10);
53 } else {
54 ?>
55
56 <style id="<?php echo $cssId; ?>" type="text/css">
57 <?php echo $css; ?>
58 </style>
59 <?php
60 }
61 }
62 }
63
64 public function addJs($formId, $customJS)
65 {
66 if (trim($customJS)) {
67 add_action('wp_footer', function () use ($formId, $customJS) {
68 ?>
69 <script type="text/javascript">
70 jQuery(document.body).on('fluentform_init_<?php echo $formId; ?>', function (event, data) {
71 var $form = jQuery(data[0]);
72 var formId = "<?php echo $formId; ?>";
73 var $ = jQuery;
74 try {
75 <?php echo $customJS; ?>
76 } catch (e) {
77 console.warn('Error in custom JS of Fluentform ID: ' + $form.data('form_id'));
78 console.error(e);
79 }
80 });
81 </script>
82 <?php
83 }, 100);
84 }
85 }
86
87 /**
88 * Get settings for a particular form by id
89 * @return void
90 */
91 public function getSettingsAjax()
92 {
93 $formId = absint($_REQUEST['form_id']);
94 wp_send_json_success(array(
95 'custom_css' => $this->getData($formId, '_custom_form_css'),
96 'custom_js' => $this->getData($formId, '_custom_form_js'),
97 ), 200);
98 }
99
100 /**
101 * Save settings for a particular form by id
102 * @return void
103 */
104 public function saveSettingsAjax()
105 {
106 $formId = absint($_REQUEST['form_id']);
107
108 $css = $_REQUEST['custom_css'];
109 $js = $_REQUEST['custom_js'];
110 $css = wp_strip_all_tags(wp_unslash($css));
111 $js = wp_unslash($js);
112
113 $this->store($formId, '_custom_form_css', $css);
114 $this->store($formId, '_custom_form_js', $js);
115
116 wp_send_json_success([
117 'message' => __('Custom CSS and JS successfully updated', 'fluentform')
118 ], 200);
119 }
120
121
122 protected function getData($formId, $metaKey)
123 {
124 $row = wpFluent()->table('fluentform_form_meta')
125 ->where('form_id', $formId)
126 ->where('meta_key', $metaKey)
127 ->first();
128 if ($row) {
129 return $row->value;
130 }
131 return '';
132 }
133
134
135 protected function store($formId, $metaKey, $metaValue)
136 {
137 $row = wpFluent()->table('fluentform_form_meta')
138 ->where('form_id', $formId)
139 ->where('meta_key', $metaKey)
140 ->first();
141
142 if (!$row) {
143 return wpFluent()->table('fluentform_form_meta')
144 ->insert([
145 'form_id' => $formId,
146 'meta_key' => $metaKey,
147 'value' => $metaValue
148 ]);
149 }
150
151 return wpFluent()->table('fluentform_form_meta')
152 ->where('id', $row->id)
153 ->update([
154 'value' => $metaValue
155 ]);
156 }
157 }