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

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

987 lines 32.4 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\Helpers\Helper;
6 use FluentForm\App\Modules\Acl\Acl;
7 use FluentForm\App\Services\FormBuilder\EditorShortcodeParser;
8 use FluentForm\App\Services\FormBuilder\Notifications\EmailNotificationActions;
9 use FluentForm\Framework\Foundation\Application;
10 use FluentForm\Framework\Helpers\ArrayHelper;
11
12 class Component
13 {
14 /**
15 * FluentForm\Framework\Foundation\Application
16 *
17 * @var $app
18 */
19 protected $app = null;
20
21 /**
22 * Biuld the instance of this class
23 *
24 * @param \FluentForm\Framework\Foundation\Application $app
25 */
26 public function __construct(Application $app)
27 {
28 $this->app = $app;
29 }
30
31 public function registerScripts()
32 {
33 $app = $this->app;
34
35 // We will just register the scripts here. We will not load any scripts from here
36
37 $fluentFormPublicCss = $app->publicUrl('css/fluent-forms-public.css');
38 $fluentFormPublicDefaultCss = $app->publicUrl('css/fluentform-public-default.css');
39
40 if (is_rtl()) {
41 $fluentFormPublicCss = $app->publicUrl('css/fluent-forms-public-rtl.css');
42 $fluentFormPublicDefaultCss = $app->publicUrl('css/fluentform-public-default-rtl.css');
43 }
44
45 wp_register_style(
46 'fluent-form-styles',
47 $fluentFormPublicCss,
48 array(),
49 FLUENTFORM_VERSION
50 );
51
52 wp_register_style(
53 'fluentform-public-default',
54 $fluentFormPublicDefaultCss,
55 array(),
56 FLUENTFORM_VERSION
57 );
58
59 wp_register_script(
60 'fluent-form-submission',
61 $app->publicUrl('js/form-submission.js'),
62 array('jquery'),
63 FLUENTFORM_VERSION,
64 true
65 );
66
67 wp_register_script(
68 'fluentform-advanced',
69 $app->publicUrl('js/fluentform-advanced.js'),
70 array('jquery'),
71 FLUENTFORM_VERSION,
72 true
73 );
74
75 // Date Pickckr Style
76 wp_register_style(
77 'flatpickr',
78 $app->publicUrl('libs/flatpickr/flatpickr.min.css')
79 );
80
81 // Date Pickckr Script
82 wp_register_script(
83 'flatpickr',
84 $app->publicUrl('libs/flatpickr/flatpickr.js'),
85 array('jquery'),
86 false,
87 true
88 );
89
90 wp_register_script(
91 'choices',
92 $app->publicUrl('libs/choices/choices.min.js'),
93 array(),
94 '9.0.1',
95 true
96 );
97
98 wp_register_style(
99 'ff_choices',
100 $app->publicUrl('css/choices.css'),
101 [],
102 FLUENTFORM_VERSION
103 );
104
105 do_action('fluentform_scripts_registered');
106
107 $this->maybeLoadFluentFormStyles();
108 }
109
110 protected function maybeLoadFluentFormStyles()
111 {
112 global $post;
113
114 if (!$post) {
115 return;
116 }
117
118 $fluentFormIds = get_post_meta($post->ID, '_has_fluentform', true);
119 $hasFluentformMeta = is_a($post, 'WP_Post') && $fluentFormIds;
120
121 if ($hasFluentformMeta || apply_filters('fluentform_load_styles', false, $post)) {
122 wp_enqueue_style('fluent-form-styles');
123 wp_enqueue_style('fluentform-public-default');
124 do_action('fluentform_pre_load_scripts', $post);
125 wp_enqueue_script('fluent-form-submission');
126 }
127 }
128
129 /**
130 * Get all the available components
131 *
132 * @return void
133 * @throws \Exception
134 * @throws \FluentForm\Framework\Exception\UnResolveableEntityException
135 */
136 public function index()
137 {
138 $this->app->doAction(
139 'fluent_editor_init',
140 $components = $this->app->make('components')
141 );
142
143 $editorComponents = $components->sort()->toArray();
144 $editorComponents = $this->app->applyFilters('fluent_editor_components', $editorComponents);
145 $countries = $this->app->load($this->app->appPath('Services/FormBuilder/CountryNames.php'));
146
147 wp_send_json_success(array(
148 'countries' => $countries,
149 'components' => $editorComponents,
150 'disabled_components' => $this->getDisabledComponents()
151 ));
152 }
153
154
155 /**
156 * Get disabled components
157 *
158 * @return array
159 */
160 private function getDisabledComponents()
161 {
162 $isReCaptchaDisabled = !get_option('_fluentform_reCaptcha_keys_status', false);
163
164 $disabled = array(
165 'recaptcha' => array(
166 'contentComponent' => 'recaptcha',
167 'disabled' => $isReCaptchaDisabled
168 ),
169 'input_image' => array(
170 'disabled' => true
171 ),
172 'input_file' => array(
173 'disabled' => true
174 ),
175 'shortcode' => array(
176 'disabled' => true
177 ),
178 'action_hook' => array(
179 'disabled' => true
180 ),
181 'form_step' => array(
182 'disabled' => true
183 )
184 );
185
186 if (!defined('FLUENTFORMPRO')) {
187 $disabled['ratings'] = array(
188 'disabled' => true
189 );
190 $disabled['tabular_grid'] = array(
191 'disabled' => true
192 );
193 $disabled['phone'] = array(
194 'disabled' => true
195 );
196
197 $disabled['net_promoter_score'] = array(
198 'disabled' => true
199 );
200
201 $disabled['repeater_field'] = array(
202 'disabled' => true
203 );
204
205 $disabled['custom_submit_button'] = array(
206 'disabled' => true
207 );
208
209 $disabled['rangeslider'] = array(
210 'disabled' => true
211 );
212
213 $disabled['color-picker'] = array(
214 'disabled' => true
215 );
216
217 $disabled['multi_payment_component'] = array(
218 'disabled' => true,
219 'is_payment' => true
220 );
221 $disabled['custom_payment_component'] = array(
222 'disabled' => true,
223 'is_payment' => true
224 );
225
226 $disabled['item_quantity_component'] = array(
227 'disabled' => true,
228 'is_payment' => true
229 );
230
231 $disabled['payment_method'] = array(
232 'disabled' => true,
233 'is_payment' => true
234 );
235 }
236 return $this->app->applyFilters('fluentform_disabled_components', $disabled);
237 }
238
239 /**
240 * Get available shortcodes for editor
241 *
242 * @return void
243 * @throws \Exception
244 */
245 public function getEditorShortcodes()
246 {
247 $editor_shortcodes = fluentFormEditorShortCodes();
248 wp_send_json_success(['shortcodes' => $editor_shortcodes], 200);
249 }
250
251 /**
252 * Get all available shortcodes for editor
253 *
254 * @return void
255 * @throws \Exception
256 */
257 public function getAllEditorShortcodes()
258 {
259 wp_send_json(fluentFormGetAllEditorShortCodes(
260 $this->app->request->get('formId')
261 ), 200);
262 }
263
264 /**
265 * Register the form renderer shortcode
266 *
267 * @return void
268 */
269 public function addFluentFormShortCode()
270 {
271
272 add_action('wp_enqueue_scripts', array($this, 'registerScripts'), 999);
273
274 $this->app->addShortCode('fluentform', function ($atts, $content) {
275 $shortcodeDefaults = apply_filters('fluentform_shortcode_defaults', array(
276 'id' => null,
277 'title' => null,
278 'permission' => '',
279 'permission_message' => __('Sorry, You do not have permission to view this form', 'fluentform')
280 ), $atts);
281
282 $atts = shortcode_atts($shortcodeDefaults, $atts);
283 return $this->renderForm($atts);
284 });
285
286 $this->app->addShortCode('fluentform_info', function ($atts) {
287 $shortcodeDefaults = apply_filters('fluentform_info_shortcode_defaults', array(
288 'id' => null, // This is the form id
289 'info' => 'submission_count', // submission_count | created_at | updated_at | payment_total
290 'status' => 'all', // get submission cound of a particular entry status favourites | unread | read
291 'with_trashed' => 'no', // yes | no
292 'substract_from' => 0, // [fluentform_info id="2" info="submission_count" substract_from="20"]
293 'hide_on_zero' => 'no',
294 'payment_status' => 'paid', // it can be all / specific payment status
295 'currency_formatted' => 'yes',
296 'date_format' => ''
297 ), $atts);
298
299 $atts = shortcode_atts($shortcodeDefaults, $atts);
300 $formId = $atts['id'];
301 $form = wpFluent()->table('fluentform_forms')->find($formId);
302
303 if (!$form) {
304 return '';
305 }
306
307 if ($atts['info'] == 'submission_count') {
308 $countQuery = wpFluent()->table('fluentform_submissions')
309 ->where('form_id', $formId);
310
311 if ($atts['status'] != 'trashed' && $atts['with_trashed'] == 'no') {
312 $countQuery = $countQuery->where('status', '!=', 'trashed');
313 }
314
315 if ($atts['status'] == 'all') {
316 // ...
317 } else if ($atts['status'] == 'favourites') {
318 $countQuery = $countQuery->where('is_favourite', '=', 1);
319 } else {
320 $countQuery = $countQuery->where('status', '=', sanitize_text_field($atts['status']));
321 }
322
323 $total = $countQuery->count();
324
325 if ($atts['substract_from']) {
326 $total = intval($atts['substract_from']) - $total;
327 }
328
329 if ($atts['hide_on_zero'] == 'yes' && !$total || $total < 0) {
330 return '';
331 }
332
333 return $total;
334 } else if ($atts['info'] == 'created_at') {
335 if ($atts['date_format']) {
336 $dateFormat = $atts['date_format'];
337 } else {
338 $dateFormat = get_option('date_format') . ' ' . get_option('time_format');
339 }
340 return date($dateFormat, strtotime($form->created_at));
341 } else if ($atts['info'] == 'updated_at') {
342 if ($atts['date_format']) {
343 $dateFormat = $atts['date_format'];
344 } else {
345 $dateFormat = get_option('date_format') . ' ' . get_option('time_format');
346 }
347 return date($dateFormat, strtotime($form->updated_at));
348 } else if ($atts['info'] == 'payment_total') {
349
350 if (!defined('FLUENTFORMPRO')) {
351 return '';
352 }
353
354 global $wpdb;
355 $countQuery = wpFluent()
356 ->table('fluentform_submissions')
357 ->select(wpFluent()->raw('SUM(total_paid) as payment_total'))
358 ->where('form_id', $formId);
359
360 if ($atts['status'] != 'trashed' && $atts['with_trashed'] == 'no') {
361 $countQuery = $countQuery->where('status', '!=', 'trashed');
362 }
363
364 if ($atts['status'] == 'all') {
365 // ...
366 } else if ($atts['status'] == 'favourites') {
367 $countQuery = $countQuery->where('is_favourite', '=', 1);
368 } else {
369 $countQuery = $countQuery->where('status', '=', sanitize_text_field($atts['status']));
370 }
371
372 if ($atts['payment_status'] == 'all') {
373 // ...
374 } else if ($atts['payment_status']) {
375 $countQuery = $countQuery->where('payment_status', '=', sanitize_text_field($atts['payment_status']));
376 }
377
378 $row = $countQuery->first();
379
380 $total = 0;
381 if ($row) {
382 $total = $row->payment_total;
383 }
384
385 if ($atts['substract_from']) {
386 $total = intval($atts['substract_from'] * 100) - $total;
387 }
388
389 if ($atts['hide_on_zero'] == 'yes' && !$total) {
390 return '';
391 }
392
393 if ($atts['currency_formatted'] == 'yes') {
394 $currency = \FluentFormPro\Payments\PaymentHelper::getFormCurrency($formId);
395 return \FluentFormPro\Payments\PaymentHelper::formatMoney($total, $currency);
396 }
397
398 if (!$total) {
399 return 0;
400 }
401
402 return $total / 100;
403 }
404
405 return '';
406 });
407
408 $this->app->addShortCode('ff_get', function ($atts) {
409 $atts = shortcode_atts(array(
410 'param' => '',
411 ), $atts);
412 if ($atts['param'] && isset($_GET[$atts['param']])) {
413 $value = $_GET[$atts['param']];
414 if (is_array($value)) {
415 return implode(', ', $value);
416 }
417 return esc_html($value);
418 }
419 return '';
420 });
421
422 }
423
424 public function renderForm($atts)
425 {
426 $form_id = $atts['id'];
427
428 if ($form_id) {
429 $form = wpFluent()->table('fluentform_forms')->find($form_id);
430 } else if ($formTitle = $atts['title']) {
431 $form = wpFluent()->table('fluentform_forms')->where('title', $formTitle)->first();
432 } else {
433 return;
434 }
435
436 if (!$form) {
437 return;
438 }
439
440 if (!empty($atts['permission'])) {
441 if (!current_user_can($atts['permission'])) {
442 return "<div id='ff_form_{$form->id}' class='ff_form_not_render'>{$atts['permission_message']}</div>";
443 }
444 }
445
446 if(is_feed()) {
447 global $post;
448 $feedText = sprintf( __( 'The form can be filled in the actual <a href="%s">website url</a>.', 'fluentform' ), get_permalink($post));
449 $feedText = apply_filters('fluentform_shortcode_feed_text', $feedText, $form);
450 return $feedText;
451 }
452
453 $formSettings = wpFluent()
454 ->table('fluentform_form_meta')
455 ->where('form_id', $form_id)
456 ->where('meta_key', 'formSettings')
457 ->first();
458
459 if (!$formSettings) {
460 return;
461 }
462
463 $form->fields = json_decode($form->form_fields, true);
464
465 if (!$form->fields['fields']) {
466 return;
467 }
468
469 $form->settings = json_decode($formSettings->value, true);
470 $form = $this->app->applyFilters('fluentform_rendering_form', $form);
471
472 $isRenderable = array(
473 'status' => true,
474 'message' => ''
475 );
476
477 $isRenderable = $this->app->applyFilters('fluentform_is_form_renderable', $isRenderable, $form);
478
479 if (is_array($isRenderable) && !$isRenderable['status']) {
480 return "<div id='ff_form_{$form->id}' class='ff_form_not_render'>{$isRenderable['message']}</div>";
481 }
482
483 $instanceCssClass = Helper::getFormInstaceClass($form->id);
484
485 $form->instance_css_class = $instanceCssClass;
486 $form->instance_index = Helper::$formInstance;
487
488 $formBuilder = $this->app->make('formBuilder');
489 $output = $formBuilder->build($form, $instanceCssClass . ' ff-form-loading', $instanceCssClass);
490 $output = $this->replaceEditorSmartCodes($output, $form);
491
492 if (!wp_script_is('fluent-form-submission', 'registered')) {
493 $this->registerScripts();
494 }
495
496 wp_enqueue_style('fluent-form-styles');
497 if (apply_filters('fluentform_load_default_public', true, $form)) {
498 wp_enqueue_style('fluentform-public-default');
499 }
500 /*
501 * We will load fluentform-advanced if the form has certain fields or feature
502 */
503 $this->maybeHasAdvandedFields($form, $formBuilder);
504 wp_enqueue_script('fluent-form-submission');
505
506 $stepText = __('Step %activeStep% of %totalStep% - %stepTitle%', 'fluentform');
507 $stepText = apply_filters('fluentform_step_string', $stepText);
508 $vars = apply_filters('fluentform_global_form_vars', array(
509 'ajaxUrl' => admin_url('admin-ajax.php'),
510 'forms' => array(),
511 'step_text' => $stepText,
512 'is_rtl' => is_rtl(),
513 'date_i18n' => $this->getDatei18n(),
514 'pro_version' => (defined('FLUENTFORMPRO_VERSION')) ? FLUENTFORMPRO_VERSION : false,
515 'fluentform_version' => FLUENTFORM_VERSION,
516 'force_init' => false,
517 'stepAnimationDuration' => 350,
518 'upload_completed_txt' => __('100% Completed', 'fluentform'),
519 'upload_start_txt' => __('0% Completed', 'fluentform'),
520 'uploading_txt' => __('Uploading', 'fluentform'),
521 'choice_js_vars' => [
522 'noResultsText' => __('No results found', 'fluentform'),
523 'loadingText' => __('Loading...', 'fluentform'),
524 'noChoicesText' => __('No choices to choose from', 'fluentform'),
525 'itemSelectText' => __('Press to select', 'fluentform'),
526 ]
527 ));
528
529 wp_localize_script('fluent-form-submission', 'fluentFormVars', $vars);
530
531 $formSettings = $form->settings;
532
533 $formSettings = ArrayHelper::only($formSettings, ['layout', 'id']);
534
535 $formSettings['restrictions']['denyEmptySubmission'] = [
536 'enabled' => false
537 ];
538
539 $form_vars = array(
540 'id' => $form->id,
541 'settings' => $formSettings,
542 'form_instance' => $instanceCssClass,
543 'form_id_selector' => 'fluentform_' . $form->id,
544 'rules' => $formBuilder->validationRules
545 );
546
547 if ($conditionals = $formBuilder->conditions) {
548 $form_vars['conditionals'] = $conditionals;
549 }
550
551 if ($form->has_payment) {
552 do_action('fluentform_rending_payment_form', $form);
553 }
554
555 $otherScripts = '';
556 ob_start();
557 ?>
558 <script type="text/javascript">
559 window.fluent_form_<?php echo $instanceCssClass; ?> = <?php echo json_encode($form_vars);?>;
560 <?php if(wp_doing_ajax()): ?>
561 function initFFInstance_<?php echo $form_vars['id']; ?>() {
562 if(!window.fluentFormApp) {
563 console.log('No fluentFormApp found');
564 return;
565 }
566 var ajax_formInstance = window.fluentFormApp(jQuery('form.<?php echo $form_vars['form_instance']; ?>'));
567 if (ajax_formInstance) {
568 ajax_formInstance.initFormHandlers();
569 }
570 }
571
572 initFFInstance_<?php echo $form_vars['id']; ?>();
573 <?php endif; ?>
574 </script>
575 <?php
576 $this->addInlineVars();
577 $otherScripts .= ob_get_clean();
578
579 if (!apply_filters('fluentform-disabled_analytics', false)) {
580 if (!Acl::hasAnyFormPermission($form->id)) {
581 (new \FluentForm\App\Modules\Form\Analytics($this->app))->record($form->id);
582 }
583 }
584 return $output . $otherScripts;
585 }
586
587 /**
588 * Process the output HTML to generate the default values.
589 *
590 * @param string $output
591 * @param \stdClass $form
592 * @return string
593 */
594 public function replaceEditorSmartCodes($output, $form)
595 {
596 // Get the patterns for default values from the output HTML string.
597 // The example of a pattern would be for user ID: {user.ID}
598 preg_match_all('/{(.*?)}/', $output, $matches);
599 $patterns = array_unique($matches[0]);
600
601
602 $attrDefaultValues = [];
603
604 foreach ($patterns as $pattern) {
605 // The default value for each pattern will be resolved here.
606 $attrDefaultValues[$pattern] = apply_filters('fluentform_parse_default_value', $pattern, $form);
607 }
608
609 // Raising an event so that others can hook into it and modify the default values later.
610 $attrDefaultValues = (array)apply_filters('fluentform_parse_default_values', $attrDefaultValues);
611
612 if (isset($attrDefaultValues['{payment_total}'])) {
613 $attrDefaultValues['{payment_total}'] = '<span class="ff_order_total"></span>';
614 }
615
616 // Finally, replace the patterns with the replacements and return the output HTML.
617 return str_replace(array_keys($attrDefaultValues), array_values($attrDefaultValues), $output);
618 }
619
620 /**
621 * Register renderer actions for compiling each element
622 *
623 * @return void
624 */
625 public function addRendererActions()
626 {
627 $actionMappings = [
628 'Select@compile' => ['fluentform_render_item_select'],
629 'Rating@compile' => ['fluentform_render_item_ratings'],
630 'Address@compile' => ['fluentform_render_item_address'],
631 'Name@compile' => ['fluentform_render_item_input_name'],
632 'TextArea@compile' => ['fluentform_render_item_textarea'],
633 'DateTime@compile' => ['fluentform_render_item_input_date'],
634 'Recaptcha@compile' => ['fluentform_render_item_recaptcha'],
635 'Container@compile' => ['fluentform_render_item_container'],
636 'CustomHtml@compile' => ['fluentform_render_item_custom_html'],
637 'SectionBreak@compile' => ['fluentform_render_item_section_break'],
638 'SubmitButton@compile' => ['fluentform_render_item_submit_button'],
639 'SelectCountry@compile' => ['fluentform_render_item_select_country'],
640
641 'TermsAndConditions@compile' => [
642 'fluentform_render_item_terms_and_condition',
643 'fluentform_render_item_gdpr_agreement'
644 ],
645
646 'TabularGrid@compile' => [
647 'fluentform_render_item_tabular_grid'
648 ],
649
650 'Checkable@compile' => [
651 'fluentform_render_item_input_radio',
652 'fluentform_render_item_input_checkbox',
653 ],
654
655 'Text@compile' => [
656 'fluentform_render_item_input_url',
657 'fluentform_render_item_input_text',
658 'fluentform_render_item_input_email',
659 'fluentform_render_item_input_number',
660 'fluentform_render_item_input_hidden',
661 'fluentform_render_item_input_password',
662 ],
663 ];
664
665 $path = 'FluentForm\App\Services\FormBuilder\Components\\';
666 foreach ($actionMappings as $handler => $actions) {
667 foreach ($actions as $action) {
668 $this->app->addAction($action, function () use ($path, $handler) {
669 list($class, $method) = $this->app->parseHandler($path . $handler);
670 call_user_func_array(array($class, $method), func_get_args());
671 }, 10, 2);
672 }
673 }
674 }
675
676 /**
677 * Register dynamic value shortcode parser (filter default value)
678 *
679 * @return void
680 */
681 public function addFluentFormDefaultValueParser()
682 {
683 $this->app->addFilter('fluentform_parse_default_value', function ($value, $form) {
684 return EditorShortcodeParser::filter($value, $form);
685 }, 10, 2);
686 }
687
688 /**
689 * Register filter to check whether the form is renderable
690 *
691 * @return mixed
692 */
693 public function addIsRenderableFilter()
694 {
695 $this->app->addFilter('fluentform_is_form_renderable', function ($isRenderable, $form) {
696 $checkables = array('limitNumberOfEntries', 'scheduleForm', 'requireLogin');
697
698 foreach ($form->settings['restrictions'] as $key => $restrictions) {
699 if (in_array($key, $checkables)) {
700 $isRenderable['status'] = $this->{$key}($restrictions, $form, $isRenderable);
701 if (!$isRenderable['status']) {
702 $isRenderable['status'] = false;
703 return $isRenderable;
704 }
705 }
706 }
707
708 return $isRenderable;
709 }, 10, 2);
710 }
711
712 /**
713 * Check if limit is set on form submits and it's valid yet
714 *
715 * @param array $restrictions
716 *
717 * @return bool
718 */
719 private function limitNumberOfEntries($restrictions, $form, &$isRenderable)
720 {
721 if (!$restrictions['enabled']) {
722 return true;
723 }
724
725 $col = 'created_at';
726 $period = $restrictions['period'];
727 $maxAllowedEntries = $restrictions['numberOfEntries'];
728
729 if (!$maxAllowedEntries) {
730 return true;
731 }
732
733 $query = wpFluent()->table('fluentform_submissions')
734 ->where('form_id', $form->id)
735 ->where('status', '!=', 'trashed');
736
737 if ($period == 'day') {
738 $year = "YEAR(`{$col}`) = YEAR(NOW())";
739 $month = "MONTH(`{$col}`) = MONTH(NOW())";
740 $day = "DAY(`{$col}`) = DAY(NOW())";
741 $query->where(wpFluent()->raw("{$year} AND {$month} AND {$day}"));
742 } elseif ($period == 'week') {
743 $query->where(
744 wpFluent()->raw("YEARWEEK(`{$col}`, 1) = YEARWEEK(CURDATE(), 1)")
745 );
746 } elseif ($period == 'month') {
747 $year = "YEAR(`{$col}`) = YEAR(NOW())";
748 $month = "MONTH(`{$col}`) = MONTH(NOW())";
749 $query->where(wpFluent()->raw("{$year} AND {$month}"));
750 } elseif ($period == 'year') {
751 $query->where(wpFluent()->raw("YEAR(`{$col}`) = YEAR(NOW())"));
752 } else if ($period == 'per_user_ip') {
753 $ip = $this->app->request->getIp();
754 $query->where('ip', $ip);
755 } else if ($period == 'per_user_id') {
756 $userId = get_current_user_id();
757 if (!$userId) {
758 return true;
759 }
760 $query->where('user_id', $userId);
761 }
762
763 $count = $query->count();
764
765
766 if ($count >= $maxAllowedEntries) {
767 $isRenderable['message'] = $restrictions['limitReachedMsg'];
768 return false;
769 }
770
771 return true;
772 }
773
774 /**
775 * Check if form has scheduled date and open for submission
776 *
777 * @param array $restrictions
778 *
779 * @return bool
780 */
781 private function scheduleForm($restrictions, $form, &$isRenderable)
782 {
783 if (!$restrictions['enabled']) {
784 return true;
785 }
786
787 $time = time();
788 $start = strtotime($restrictions['start']);
789 $end = strtotime($restrictions['end']);
790
791 if ($time < $start) {
792 $isRenderable['message'] = $restrictions['pendingMsg'];
793
794 return false;
795 }
796
797 if ($time >= $end) {
798 $isRenderable['message'] = $restrictions['expiredMsg'];
799
800 return false;
801 }
802
803 return true;
804 }
805
806 /**
807 * * Check if form requires loged in user and user is logged in
808 *
809 * @param array $restrictions
810 *
811 * @return bool
812 */
813 private function requireLogin($restrictions, $form, &$isRenderable)
814 {
815 if (!$restrictions['enabled']) {
816 return true;
817 }
818
819 if (!($isLoggedIn = is_user_logged_in())) {
820 $isRenderable['message'] = $restrictions['requireLoginMsg'];
821 }
822
823 return $isLoggedIn;
824 }
825
826 /**
827 * Register fluentform_submission_inserted actions
828 *
829 * @return void
830 */
831 public function addFluentformSubmissionInsertedFilter()
832 {
833 (new EmailNotificationActions($this->app))->register();
834 }
835
836 /**
837 * Add inline scripts [Add localized script using same var]
838 *
839 * @return void
840 */
841 private function addInlineVars()
842 {
843 if (!defined('ELEMENTOR_PRO_VERSION')) {
844 return '';
845 }
846
847 $actionName = 'wp_footer';
848 if (is_admin()) {
849 $actionName = 'admin_footer';
850 }
851
852 add_action($actionName, function () {
853 ?>
854 <script type="text/javascript">
855 <?php if(defined('ELEMENTOR_PRO_VERSION')): ?>
856 jQuery(document).on('elementor/popup/show', function (event, id, instance) {
857 var ffForms = jQuery('#elementor-popup-modal-' + id).find('form.frm-fluent-form');
858 if (ffForms.length) {
859 jQuery.each(ffForms, function (index, ffForm) {
860 jQuery(ffForm).trigger('reInitExtras');
861 jQuery(document).trigger('ff_reinit', [ffForm]);
862 });
863 }
864 });
865 <?php endif; ?>
866 </script>
867 <?php
868 }, 999);
869 return '';
870 }
871
872 private function getDatei18n()
873 {
874 $i18n = array(
875 'previousMonth' => __('Previous Month', 'fluentform'),
876 'nextMonth' => __('Next Month', 'fluentform'),
877 'months' => [
878 'shorthand' => [
879 __('Jan', 'fluentform'),
880 __('Feb', 'fluentform'),
881 __('Mar', 'fluentform'),
882 __('Apr', 'fluentform'),
883 __('May', 'fluentform'),
884 __('Jun', 'fluentform'),
885 __('Jul', 'fluentform'),
886 __('Aug', 'fluentform'),
887 __('Sep', 'fluentform'),
888 __('Oct', 'fluentform'),
889 __('Nov', 'fluentform'),
890 __('Dec', 'fluentform')
891 ],
892 'longhand' => [
893 __('January', 'fluentform'),
894 __('February', 'fluentform'),
895 __('March', 'fluentform'),
896 __('April', 'fluentform'),
897 __('May', 'fluentform'),
898 __('June', 'fluentform'),
899 __('July', 'fluentform'),
900 __('August', 'fluentform'),
901 __('September', 'fluentform'),
902 __('October', 'fluentform'),
903 __('November', 'fluentform'),
904 __('December', 'fluentform')
905 ]
906 ],
907 'weekdays' => [
908 'longhand' => array(
909 __('Sunday', 'fluentform'),
910 __('Monday', 'fluentform'),
911 __('Tuesday', 'fluentform'),
912 __('Wednesday', 'fluentform'),
913 __('Thursday', 'fluentform'),
914 __('Friday', 'fluentform'),
915 __('Saturday', 'fluentform')
916 ),
917 'shorthand' => array(
918 __('Sun', 'fluentform'),
919 __('Mon', 'fluentform'),
920 __('Tue', 'fluentform'),
921 __('Wed', 'fluentform'),
922 __('Thu', 'fluentform'),
923 __('Fri', 'fluentform'),
924 __('Sat', 'fluentform')
925 )
926 ],
927 'daysInMonth' => [
928 31,
929 28,
930 31,
931 30,
932 31,
933 30,
934 31,
935 31,
936 30,
937 31,
938 30,
939 31
940 ],
941 'rangeSeparator' => __(' to ', 'fluentform'),
942 'weekAbbreviation' => __('Wk', 'fluentform'),
943 'scrollTitle' => __('Scroll to increment', 'fluentform'),
944 'toggleTitle' => __('Click to toggle', 'fluentform'),
945 'amPM' => [
946 __('AM', 'fluentform'),
947 __('PM', 'fluentform')
948 ],
949 'yearAriaLabel' => __('Year', 'fluentform')
950 );
951
952 return apply_filters('fluentform/date_i18n', $i18n);
953 }
954
955 protected function maybeHasAdvandedFields($form, $formBuilder)
956 {
957 $advancedFields = [
958 'step_start',
959 'repeater_field',
960 'ratings',
961 'form_step',
962 'input_file',
963 'input_image',
964 'net_promoter_score',
965 'featured_image'
966 ];
967
968 if ($formBuilder->conditions || array_intersect($formBuilder->fieldLists, $advancedFields)) {
969 wp_enqueue_script('fluentform-advanced');
970 }
971 }
972
973 public function registerInputSanitizers()
974 {
975 add_filter('fluentform_input_data_input_number', array($this, 'getNumericInputValue'), 10, 2);
976 add_filter('fluentform_input_data_custom_payment_component', array($this, 'getNumericInputValue'), 10, 2);
977 }
978
979 public function getNumericInputValue($value, $field)
980 {
981 $formatter = ArrayHelper::get($field, 'raw.settings.numeric_formatter');
982 if(!$formatter) {
983 return $value;
984 }
985 return Helper::getNumericValue($value, $formatter);
986 }
987 }