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

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