PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 1.2.4
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v1.2.4
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 / Component / Component.php

Component.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 1.2.4, at app/Modules/Component/Component.php

433 lines 12.3 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\Component;
4
5 use FluentForm\App\Modules\Acl\Acl;
6 use FluentForm\App\Services\ConditionAssesor;
7 use FluentForm\Framework\Foundation\Application;
8 use FluentForm\App\Services\FormBuilder\EditorShortcodeParser;
9 use FluentForm\App\Services\FormBuilder\MessageShortCodeParser;
10
11 class Component
12 {
13 /**
14 * FluentForm\Framework\Foundation\Application
15 * @var $app
16 */
17 protected $app = null;
18
19 /**
20 * Biuld the instance of this class
21 * @param \FluentForm\Framework\Foundation\Application $app
22 */
23 public function __construct(Application $app)
24 {
25 $this->app = $app;
26 }
27
28 /**
29 * Get all the available components
30 * @return void
31 * @throws \Exception
32 * @throws \FluentForm\Framework\Exception\UnResolveableEntityException
33 */
34 public function index()
35 {
36 Acl::verify('fluentform_forms_manager');
37
38 $this->app->doAction(
39 'fluent_editor_init',
40 $components = $this->app->make('components')
41 );
42
43 $editorCompnents = $components->sort();
44 $editorCompnents = $this->app->applyFilters('fluent_editor_components', $editorCompnents);
45 $countries = $this->app->load($this->app->appPath('Services/FormBuilder/CountryNames.php'));
46
47 wp_send_json_success(array(
48 'components' => $editorCompnents,
49 'countries' => $countries,
50 'disabled_components' => $this->getDisabledComponents()
51 ));
52 exit();
53 }
54
55
56 /**
57 * Get disabled components
58 * @return array
59 */
60 private function getDisabledComponents()
61 {
62 $isReCaptchaDisabled = !get_option('_fluentform_reCaptcha_keys_status', false);
63
64 $disabled = array(
65 'recaptcha' => array(
66 'contentComponent' => 'recaptcha',
67 'disabled' => $isReCaptchaDisabled
68 ),
69 'input_image' => array(
70 'disabled' => true
71 ),
72 'input_file' => array(
73 'disabled' => true
74 ),
75 'input_repeat' => array(
76 'disabled' => true
77 ),
78 'shortcode' => array(
79 'disabled' => true
80 ),
81 'action_hook' => array(
82 'disabled' => true
83 ),
84 'form_step' => array(
85 'disabled' => true
86 ),
87 );
88
89 return $this->app->applyFilters('disabled_components', $disabled);
90 }
91
92 /**
93 * Get available shortcodes for editor
94 * @return void
95 * @throws \Exception
96 */
97 public function getEditorShortcodes()
98 {
99 Acl::verify('fluentform_forms_manager');
100 $editor_shortcodes = fluentFormEditorShortCodes();
101 wp_send_json_success(array('shortcodes' => $editor_shortcodes), 200);
102 exit();
103 }
104
105 /**
106 * Register the form renderer shortcode
107 * @return void
108 */
109 public function addFluentFormShortCode() {
110 $this->app->addShortCode( 'fluentform', function ( $atts, $content ) {
111 $atts = shortcode_atts( array(
112 'id' => null,
113 'title' => null
114 ), $atts );
115 $form_id = $atts['id'];
116 if ( $form_id ) {
117 $form = wpFluent()->table( 'fluentform_forms' )->find( $form_id );
118 } else if ( $formTitle = $atts['title'] ) {
119 $form = wpFluent()->table( 'fluentform_forms' )->where( 'title', $formTitle )->first();
120 } else {
121 return;
122 }
123
124 if ( ! $form ) {
125 return;
126 }
127
128 $formSettings = wpFluent()
129 ->table( 'fluentform_form_meta' )
130 ->where( 'form_id', $form_id )
131 ->where( 'meta_key', 'formSettings' )
132 ->first();
133
134 $form->fields = json_decode( $form->form_fields, true );
135 if ( ! $form->fields['fields'] ) {
136 return;
137 }
138
139 $form->settings = json_decode( $formSettings->value, true );
140 $form = $this->app->applyFilters( 'fluentform_rendering_form', $form );
141
142 $isRenderable = array(
143 'status' => true,
144 'message' => ''
145 );
146
147 $isRenderable = $this->app->applyFilters( 'fluentform_is_form_renderable', $isRenderable, $form );
148
149 if ( is_array( $isRenderable ) && ! $isRenderable['status'] ) {
150 return "<div id='ff_form_{$form->id}' class='ff_form_not_render'>{$isRenderable['message']}</div>";
151 }
152
153 $formBuilder = $this->app->make( 'formBuilder' );
154 $output = $formBuilder->build( $form );
155
156 wp_enqueue_style(
157 'fluent-form-styles',
158 $this->app->publicUrl( 'css/fluent-forms-public.css' )
159 );
160
161 wp_enqueue_style(
162 'fluentform-public-default',
163 $this->app->publicUrl( 'css/fluentform-public-default.css' )
164 );
165
166 wp_enqueue_script(
167 'fluent-form-submission',
168 $this->app->publicUrl( 'js/form-submission.js' ),
169 array( 'jquery' ),
170 false,
171 true
172 );
173
174 $form_vars = array(
175 'id' => $form->id,
176 'settings' => $form->settings,
177 'rules' => $formBuilder->validationRules,
178 'do_analytics' => $this->app->applyFilters( 'fluentform_do_analytics', true )
179 );
180
181 if ( $conditionals = $formBuilder->conditions ) {
182 wp_enqueue_script(
183 'fluent-form-conditionals',
184 $this->app->publicUrl( 'js/form-conditionals.js' ),
185 array( 'jquery' ),
186 false,
187 true
188 );
189
190 $form_vars['conditionals'] = $conditionals;
191 }
192
193 wp_localize_script( 'fluent-form-submission', 'fluentFormVars', array(
194 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
195 'forms' => (Object) array()
196 ) );
197
198 $this->addInlineVars( json_encode( $form_vars ), $form->id );
199
200 return $output;
201
202 } );
203 }
204
205 /**
206 * Register renderer actions for compiling each element
207 * @return void
208 */
209 public function addRendererActions()
210 {
211 $actionMappings = [
212 'Select@compile' => ['render_item_select'],
213 'Address@compile' => ['render_item_address'],
214 'Name@compile' => ['render_item_input_name'],
215 'TextArea@compile' => ['render_item_textarea'],
216 'DateTime@compile' => ['render_item_input_date'],
217 'Recaptcha@compile' => ['render_item_recaptcha'],
218 'Container@compile' => ['render_item_container'],
219 'CustomHtml@compile' => ['render_item_custom_html'],
220 'SectionBreak@compile' => ['render_item_section_break'],
221 'SubmitButton@compile' => ['render_item_submit_button'],
222 'SelectCountry@compile' => ['render_item_select_country'],
223 'TermsAndConditions@compile' => ['render_item_terms_and_condition'],
224
225 'Checkable@compile' => [
226 'render_item_input_radio',
227 'render_item_input_checkbox',
228 ],
229
230 'Text@compile' => [
231 'render_item_input_url',
232 'render_item_input_text',
233 'render_item_input_email',
234 'render_item_input_number',
235 'render_item_input_hidden',
236 'render_item_input_password',
237 ],
238 ];
239
240 $path = 'FluentForm\App\Services\FormBuilder\Components\\';
241 foreach ($actionMappings as $handler => $actions) {
242 foreach ($actions as $action) {
243 $this->app->addAction($action, function() use ($path, $handler) {
244 list($class, $method) = $this->app->parseHandler($path.$handler);
245 call_user_func_array(array($class, $method), func_get_args());
246 }, 10, 2);
247 }
248 }
249 }
250
251 /**
252 * Register dynamic value shortcode parser (filter default value)
253 * @return void
254 */
255 public function addFluentFormDefaultValueParser()
256 {
257 $this->app->addFilter('fluentform_parse_default_value', function($value, $form) {
258 return EditorShortcodeParser::filter($value, $form);
259 }, 10, 2);
260 }
261
262 /**
263 * Register filter to check whether the form is renderable
264 * @return mixed
265 */
266 public function addIsRenderableFilter()
267 {
268 $this->app->addFilter('fluentform_is_form_renderable', function($isRenderable, $form) {
269 $checkables = array('limitNumberOfEntries', 'scheduleForm', 'requireLogin');
270 foreach ($form->settings['restrictions'] as $key => $restrictions) {
271 if (in_array($key, $checkables)) {
272 if (!($isRenderable['status'] = $this->{$key}($restrictions, $form, $isRenderable))) {
273 return $isRenderable;
274 }
275 }
276 }
277
278 return $isRenderable;
279 }, 10, 2);
280 }
281
282 /**
283 * Check if limit is set on form submits and it's valid yet
284 * @param array $restrictions
285 * @return bool
286 */
287 private function limitNumberOfEntries($restrictions, $form, &$isRenderable)
288 {
289 if (!$restrictions['enabled']) {
290 return true;
291 }
292
293 $col = 'created_at';
294 $period = $restrictions['period'];
295 $maxAllowedEntries = $restrictions['numberOfEntries'];
296 $query = wpFluent()->table('fluentform_submissions')
297 ->where('form_id', $form->id)
298 ->where('status', '!=', 'trashed');
299
300 if ($period == 'day') {
301 $year = "YEAR(`{$col}`) = YEAR(NOW())";
302 $month = "MONTH(`{$col}`) = MONTH(NOW())";
303 $day = "DAY(`{$col}`) = DAY(NOW())";
304 $query->where(wpFluent()->raw("{$year} AND {$month} AND {$day}"));
305 } elseif ($period == 'week') {
306 $query->where(
307 wpFluent()->raw("YEARWEEK(`{$col}`, 1) = YEARWEEK(CURDATE(), 1)")
308 );
309 } elseif ($period == 'month') {
310 $year = "YEAR(`{$col}`) = YEAR(NOW())";
311 $month = "MONTH(`{$col}`) = MONTH(NOW())";
312 $query->where(wpFluent()->raw("{$year} AND {$month}"));
313 } elseif ($period == 'year') {
314 $query->where(wpFluent()->raw("YEAR(`{$col}`) = YEAR(NOW())"));
315 }
316
317 if (!($isAllowed = ($query->count() < $maxAllowedEntries))) {
318 $isRenderable['message'] = $restrictions['limitReachedMsg'];
319 }
320
321 return $isAllowed;
322 }
323
324 /**
325 * Check if form has scheduled date and open for submission
326 * @param array $restrictions
327 * @return bool
328 */
329 private function scheduleForm($restrictions, $form, &$isRenderable)
330 {
331 if (!$restrictions['enabled']) {
332 return true;
333 }
334
335 $time = time();
336 $start = strtotime($restrictions['start']);
337 $end = strtotime($restrictions['end']);
338
339 if ($time < $start) {
340 $isRenderable['message'] = $restrictions['pendingMsg'];
341 return false;
342 }
343
344 if ($time >= $end) {
345 $isRenderable['message'] = $restrictions['expiredMsg'];
346 return false;
347 }
348
349 return true;
350 }
351
352 /**
353 * * Check if form requires loged in user and user is logged in
354 * @param array $restrictions
355 * @return bool
356 */
357 private function requireLogin($restrictions, $form, &$isRenderable)
358 {
359 if (!$restrictions['enabled']) {
360 return true;
361 }
362
363 if (!($isLoggedIn = is_user_logged_in())) {
364 $isRenderable['message'] = $restrictions['requireLoginMsg'];
365 }
366
367 return $isLoggedIn;
368 }
369
370 /**
371 * Register fluentform_submission_inserted action
372 * @return void
373 */
374 public function addFluentformSubmissionInsertedFilter()
375 {
376 $this->app->addAction(
377 'fluentform_submission_inserted', function($insertId, $data, $form) {
378
379 $notifications = wpFluent()
380 ->table('fluentform_form_meta')
381 ->where('form_id', $form->id)
382 ->where('meta_key', 'notifications')
383 ->get();
384
385 $enabledNotifications = array();
386 foreach ($notifications as $key => $notification) {
387 $notification = json_decode($notification->value, true);
388 if ($notification['enabled'] && ConditionAssesor::evaluate($notification, $data)) {
389 $enabledNotifications[] = $notification;
390 }
391 }
392
393 if($enabledNotifications) {
394 $enabledNotifications = MessageShortCodeParser::parseMessageShortCode(
395 $enabledNotifications, $insertId, $data, $form
396 );
397
398 $notifier = $this->app->make(
399 'FluentForm\App\Services\FormBuilder\Notifications\EmailNotification'
400 );
401
402 foreach ($enabledNotifications as $notification) {
403 $notifier->notify($notification, $data, $form);
404 }
405 }
406
407 }, 10, 3);
408 }
409
410 /**
411 * Add inline scripts [Add localized script using same var]
412 * @param array $vars
413 * @param int $form_id
414 * @return void
415 */
416 private function addInlineVars($vars, $form_id) {
417 if (function_exists('wp_add_inline_script')) {
418 wp_add_inline_script(
419 'fluent-form-submission',
420 'window.fluentFormVars.forms["fluentform_'.$form_id.'"] = '.$vars.';'
421 );
422 } else {
423 add_action('wp_footer', function () use ($vars, $form_id) {
424 ?>
425 <script type="text/javascript">
426 window.fluentFormVars.forms["fluentform_<?php echo $form_id;?>"] = <?php echo $vars; ?>;
427 </script>
428 <?php
429 }, 100);
430 }
431 }
432 }
433