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

974 lines 31.8 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
487 $formBuilder = $this->app->make('formBuilder');
488 $output = $formBuilder->build($form, $instanceCssClass . ' ff-form-loading', $instanceCssClass);
489 $output = $this->replaceEditorSmartCodes($output, $form);
490
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
540 $form_vars = array(
541 'id' => $form->id,
542 'settings' => $formSettings,
543 'form_instance' => $instanceCssClass,
544 'form_id_selector' => 'fluentform_' . $form->id,
545 'rules' => $formBuilder->validationRules
546 );
547
548 if ($conditionals = $formBuilder->conditions) {
549 $form_vars['conditionals'] = $conditionals;
550 }
551
552 if ($form->has_payment) {
553 do_action('fluentform_rending_payment_form', $form);
554 }
555
556 $otherScripts = '';
557 ob_start();
558 ?>
559 <script type="text/javascript">
560 window.fluent_form_<?php echo $instanceCssClass; ?> = <?php echo json_encode($form_vars);?>;
561 <?php if(wp_doing_ajax()): ?>
562 function initFFInstance_<?php echo $form_vars['id']; ?>() {
563 if(!window.fluentFormApp) {
564 console.log('No fluentFormApp found');
565 return;
566 }
567 var ajax_formInstance = window.fluentFormApp(jQuery('form.<?php echo $form_vars['form_instance']; ?>'));
568 if (ajax_formInstance) {
569 ajax_formInstance.initFormHandlers();
570 }
571 }
572
573 initFFInstance_<?php echo $form_vars['id']; ?>();
574 <?php endif; ?>
575 </script>
576 <?php
577 $this->addInlineVars();
578 $otherScripts .= ob_get_clean();
579
580 if (!apply_filters('fluentform-disabled_analytics', false)) {
581 if (!Acl::hasAnyFormPermission($form->id)) {
582 (new \FluentForm\App\Modules\Form\Analytics($this->app))->record($form->id);
583 }
584 }
585 return $output . $otherScripts;
586 }
587
588 /**
589 * Process the output HTML to generate the default values.
590 *
591 * @param string $output
592 * @param \stdClass $form
593 * @return string
594 */
595 public function replaceEditorSmartCodes($output, $form)
596 {
597 // Get the patterns for default values from the output HTML string.
598 // The example of a pattern would be for user ID: {user.ID}
599 preg_match_all('/{(.*?)}/', $output, $matches);
600 $patterns = array_unique($matches[0]);
601
602
603 $attrDefaultValues = [];
604
605 foreach ($patterns as $pattern) {
606 // The default value for each pattern will be resolved here.
607 $attrDefaultValues[$pattern] = apply_filters('fluentform_parse_default_value', $pattern, $form);
608 }
609
610 // Raising an event so that others can hook into it and modify the default values later.
611 $attrDefaultValues = (array)apply_filters('fluentform_parse_default_values', $attrDefaultValues);
612
613 if (isset($attrDefaultValues['{payment_total}'])) {
614 $attrDefaultValues['{payment_total}'] = '<span class="ff_order_total"></span>';
615 }
616
617 // Finally, replace the patterns with the replacements and return the output HTML.
618 return str_replace(array_keys($attrDefaultValues), array_values($attrDefaultValues), $output);
619 }
620
621 /**
622 * Register renderer actions for compiling each element
623 *
624 * @return void
625 */
626 public function addRendererActions()
627 {
628 $actionMappings = [
629 'Select@compile' => ['fluentform_render_item_select'],
630 'Rating@compile' => ['fluentform_render_item_ratings'],
631 'Address@compile' => ['fluentform_render_item_address'],
632 'Name@compile' => ['fluentform_render_item_input_name'],
633 'TextArea@compile' => ['fluentform_render_item_textarea'],
634 'DateTime@compile' => ['fluentform_render_item_input_date'],
635 'Recaptcha@compile' => ['fluentform_render_item_recaptcha'],
636 'Container@compile' => ['fluentform_render_item_container'],
637 'CustomHtml@compile' => ['fluentform_render_item_custom_html'],
638 'SectionBreak@compile' => ['fluentform_render_item_section_break'],
639 'SubmitButton@compile' => ['fluentform_render_item_submit_button'],
640 'SelectCountry@compile' => ['fluentform_render_item_select_country'],
641
642 'TermsAndConditions@compile' => [
643 'fluentform_render_item_terms_and_condition',
644 'fluentform_render_item_gdpr_agreement'
645 ],
646
647 'TabularGrid@compile' => [
648 'fluentform_render_item_tabular_grid'
649 ],
650
651 'Checkable@compile' => [
652 'fluentform_render_item_input_radio',
653 'fluentform_render_item_input_checkbox',
654 ],
655
656 'Text@compile' => [
657 'fluentform_render_item_input_url',
658 'fluentform_render_item_input_text',
659 'fluentform_render_item_input_email',
660 'fluentform_render_item_input_number',
661 'fluentform_render_item_input_hidden',
662 'fluentform_render_item_input_password',
663 ],
664 ];
665
666 $path = 'FluentForm\App\Services\FormBuilder\Components\\';
667 foreach ($actionMappings as $handler => $actions) {
668 foreach ($actions as $action) {
669 $this->app->addAction($action, function () use ($path, $handler) {
670 list($class, $method) = $this->app->parseHandler($path . $handler);
671 call_user_func_array(array($class, $method), func_get_args());
672 }, 10, 2);
673 }
674 }
675 }
676
677 /**
678 * Register dynamic value shortcode parser (filter default value)
679 *
680 * @return void
681 */
682 public function addFluentFormDefaultValueParser()
683 {
684 $this->app->addFilter('fluentform_parse_default_value', function ($value, $form) {
685 return EditorShortcodeParser::filter($value, $form);
686 }, 10, 2);
687 }
688
689 /**
690 * Register filter to check whether the form is renderable
691 *
692 * @return mixed
693 */
694 public function addIsRenderableFilter()
695 {
696 $this->app->addFilter('fluentform_is_form_renderable', function ($isRenderable, $form) {
697 $checkables = array('limitNumberOfEntries', 'scheduleForm', 'requireLogin');
698
699 foreach ($form->settings['restrictions'] as $key => $restrictions) {
700 if (in_array($key, $checkables)) {
701 $isRenderable['status'] = $this->{$key}($restrictions, $form, $isRenderable);
702 if (!$isRenderable['status']) {
703 $isRenderable['status'] = false;
704 return $isRenderable;
705 }
706 }
707 }
708
709 return $isRenderable;
710 }, 10, 2);
711 }
712
713 /**
714 * Check if limit is set on form submits and it's valid yet
715 *
716 * @param array $restrictions
717 *
718 * @return bool
719 */
720 private function limitNumberOfEntries($restrictions, $form, &$isRenderable)
721 {
722 if (!$restrictions['enabled']) {
723 return true;
724 }
725
726 $col = 'created_at';
727 $period = $restrictions['period'];
728 $maxAllowedEntries = $restrictions['numberOfEntries'];
729
730 if (!$maxAllowedEntries) {
731 return true;
732 }
733
734 $query = wpFluent()->table('fluentform_submissions')
735 ->where('form_id', $form->id)
736 ->where('status', '!=', 'trashed');
737
738 if ($period == 'day') {
739 $year = "YEAR(`{$col}`) = YEAR(NOW())";
740 $month = "MONTH(`{$col}`) = MONTH(NOW())";
741 $day = "DAY(`{$col}`) = DAY(NOW())";
742 $query->where(wpFluent()->raw("{$year} AND {$month} AND {$day}"));
743 } elseif ($period == 'week') {
744 $query->where(
745 wpFluent()->raw("YEARWEEK(`{$col}`, 1) = YEARWEEK(CURDATE(), 1)")
746 );
747 } elseif ($period == 'month') {
748 $year = "YEAR(`{$col}`) = YEAR(NOW())";
749 $month = "MONTH(`{$col}`) = MONTH(NOW())";
750 $query->where(wpFluent()->raw("{$year} AND {$month}"));
751 } elseif ($period == 'year') {
752 $query->where(wpFluent()->raw("YEAR(`{$col}`) = YEAR(NOW())"));
753 } else if ($period == 'per_user_ip') {
754 $ip = $this->app->request->getIp();
755 $query->where('ip', $ip);
756 } else if ($period == 'per_user_id') {
757 $userId = get_current_user_id();
758 if (!$userId) {
759 return true;
760 }
761 $query->where('user_id', $userId);
762 }
763
764 $count = $query->count();
765
766
767 if ($count >= $maxAllowedEntries) {
768 $isRenderable['message'] = $restrictions['limitReachedMsg'];
769 return false;
770 }
771
772 return true;
773 }
774
775 /**
776 * Check if form has scheduled date and open for submission
777 *
778 * @param array $restrictions
779 *
780 * @return bool
781 */
782 private function scheduleForm($restrictions, $form, &$isRenderable)
783 {
784 if (!$restrictions['enabled']) {
785 return true;
786 }
787
788 $time = time();
789 $start = strtotime($restrictions['start']);
790 $end = strtotime($restrictions['end']);
791
792 if ($time < $start) {
793 $isRenderable['message'] = $restrictions['pendingMsg'];
794
795 return false;
796 }
797
798 if ($time >= $end) {
799 $isRenderable['message'] = $restrictions['expiredMsg'];
800
801 return false;
802 }
803
804 return true;
805 }
806
807 /**
808 * * Check if form requires loged in user and user is logged in
809 *
810 * @param array $restrictions
811 *
812 * @return bool
813 */
814 private function requireLogin($restrictions, $form, &$isRenderable)
815 {
816 if (!$restrictions['enabled']) {
817 return true;
818 }
819
820 if (!($isLoggedIn = is_user_logged_in())) {
821 $isRenderable['message'] = $restrictions['requireLoginMsg'];
822 }
823
824 return $isLoggedIn;
825 }
826
827 /**
828 * Register fluentform_submission_inserted actions
829 *
830 * @return void
831 */
832 public function addFluentformSubmissionInsertedFilter()
833 {
834 (new EmailNotificationActions($this->app))->register();
835 }
836
837 /**
838 * Add inline scripts [Add localized script using same var]
839 *
840 * @return void
841 */
842 private function addInlineVars()
843 {
844 if (!defined('ELEMENTOR_PRO_VERSION')) {
845 return '';
846 }
847
848 $actionName = 'wp_footer';
849 if (is_admin()) {
850 $actionName = 'admin_footer';
851 }
852
853 add_action($actionName, function () {
854 ?>
855 <script type="text/javascript">
856 <?php if(defined('ELEMENTOR_PRO_VERSION')): ?>
857 jQuery(document).on('elementor/popup/show', function (event, id, instance) {
858 var ffForms = jQuery('#elementor-popup-modal-' + id).find('form.frm-fluent-form');
859 if (ffForms.length) {
860 jQuery.each(ffForms, function (index, ffForm) {
861 jQuery(ffForm).trigger('reInitExtras');
862 jQuery(document).trigger('ff_reinit', [ffForm]);
863 });
864 }
865 });
866 <?php endif; ?>
867 </script>
868 <?php
869 }, 999);
870 return '';
871 }
872
873 private function getDatei18n()
874 {
875 $i18n = array(
876 'previousMonth' => __('Previous Month', 'fluentform'),
877 'nextMonth' => __('Next Month', 'fluentform'),
878 'months' => [
879 'shorthand' => [
880 __('Jan', 'fluentform'),
881 __('Feb', 'fluentform'),
882 __('Mar', 'fluentform'),
883 __('Apr', 'fluentform'),
884 __('May', 'fluentform'),
885 __('Jun', 'fluentform'),
886 __('Jul', 'fluentform'),
887 __('Aug', 'fluentform'),
888 __('Sep', 'fluentform'),
889 __('Oct', 'fluentform'),
890 __('Nov', 'fluentform'),
891 __('Dec', 'fluentform')
892 ],
893 'longhand' => [
894 __('January', 'fluentform'),
895 __('February', 'fluentform'),
896 __('March', 'fluentform'),
897 __('April', 'fluentform'),
898 __('May', 'fluentform'),
899 __('June', 'fluentform'),
900 __('July', 'fluentform'),
901 __('August', 'fluentform'),
902 __('September', 'fluentform'),
903 __('October', 'fluentform'),
904 __('November', 'fluentform'),
905 __('December', 'fluentform')
906 ]
907 ],
908 'weekdays' => [
909 'longhand' => array(
910 __('Sunday', 'fluentform'),
911 __('Monday', 'fluentform'),
912 __('Tuesday', 'fluentform'),
913 __('Wednesday', 'fluentform'),
914 __('Thursday', 'fluentform'),
915 __('Friday', 'fluentform'),
916 __('Saturday', 'fluentform')
917 ),
918 'shorthand' => array(
919 __('Sun', 'fluentform'),
920 __('Mon', 'fluentform'),
921 __('Tue', 'fluentform'),
922 __('Wed', 'fluentform'),
923 __('Thu', 'fluentform'),
924 __('Fri', 'fluentform'),
925 __('Sat', 'fluentform')
926 )
927 ],
928 'daysInMonth' => [
929 31,
930 28,
931 31,
932 30,
933 31,
934 30,
935 31,
936 31,
937 30,
938 31,
939 30,
940 31
941 ],
942 'rangeSeparator' => __(' to ', 'fluentform'),
943 'weekAbbreviation' => __('Wk', 'fluentform'),
944 'scrollTitle' => __('Scroll to increment', 'fluentform'),
945 'toggleTitle' => __('Click to toggle', 'fluentform'),
946 'amPM' => [
947 __('AM', 'fluentform'),
948 __('PM', 'fluentform')
949 ],
950 'yearAriaLabel' => __('Year', 'fluentform')
951 );
952
953 return apply_filters('fluentform/date_i18n', $i18n);
954 }
955
956 protected function maybeHasAdvandedFields($form, $formBuilder)
957 {
958 $advancedFields = [
959 'step_start',
960 'repeater_field',
961 'ratings',
962 'form_step',
963 'input_file',
964 'input_image',
965 'net_promoter_score',
966 'featured_image'
967 ];
968
969 if ($formBuilder->conditions || array_intersect($formBuilder->fieldLists, $advancedFields)) {
970 wp_enqueue_script('fluentform-advanced');
971 }
972 }
973
974 }