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

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