PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.18
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.18
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 / Hooks / Common.php

Common.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 4.3.18, at app/Hooks/Common.php

474 lines 15.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use FluentForm\App\Helpers\Helper;
4 use FluentForm\App\Modules\Component\Component;
5
6 /**
7 * Declare common actions/filters/shortcodes
8 */
9
10 /**
11 * App instance
12 *
13 * @var $app \FluentForm\Framework\Foundation\Application
14 */
15
16 add_action('save_post', function ($post_id) use ($app) {
17 $post_content = $app->request->get('post_content');
18 if ($post_content) {
19 $post_content = wp_kses_post(wp_unslash($post_content));
20 } else {
21 $post = get_post($post_id);
22 $post_content = $post->post_content;
23 }
24
25 $shortcodeIds = Helper::getShortCodeIds(
26 $post_content,
27 'fluentform',
28 'id'
29 );
30
31 $shortcodeModalIds = Helper::getShortCodeIds(
32 $post_content,
33 'fluentform_modal',
34 'form_id'
35 );
36
37 if ($shortcodeModalIds) {
38 $shortcodeIds = array_merge($shortcodeIds, $shortcodeModalIds);
39 }
40
41 if ($shortcodeIds) {
42 update_post_meta($post_id, '_has_fluentform', $shortcodeIds);
43 } elseif (get_post_meta($post_id, '_has_fluentform', true)) {
44 update_post_meta($post_id, '_has_fluentform', []);
45 }
46 });
47
48 $component = new Component($app);
49 $component->addRendererActions();
50 $component->addFluentFormShortCode();
51 $component->addFluentFormDefaultValueParser();
52
53 $component->addFluentformSubmissionInsertedFilter();
54 $component->addIsRenderableFilter();
55 $component->registerInputSanitizers();
56
57 add_action('wp', function () use ($app) {
58 // @todo: We will remove the fluentform_pages check from April 2021
59 $fluentFormPages = $app->request->get('fluent_forms_pages') || $app->request->get('fluentform_pages');
60
61 if ($fluentFormPages) {
62 add_action('wp_enqueue_scripts', function () use ($app) {
63 wp_enqueue_script('jquery');
64 wp_enqueue_script(
65 'fluent_forms_global',
66 $app->publicUrl('js/fluent_forms_global.js'),
67 ['jquery'],
68 FLUENTFORM_VERSION,
69 true
70 );
71 wp_localize_script('fluent_forms_global', 'fluent_forms_global_var', [
72 'fluent_forms_admin_nonce' => wp_create_nonce('fluent_forms_admin_nonce'),
73 'ajaxurl' => admin_url('admin-ajax.php'),
74 ]);
75 wp_enqueue_style('fluent-form-styles');
76 $form = wpFluent()->table('fluentform_forms')->find(intval($app->request->get('preview_id')));
77 if (apply_filters('fluentform_load_default_public', true, $form)) {
78 wp_enqueue_style('fluentform-public-default');
79 }
80 wp_enqueue_script('fluent-form-submission');
81 wp_enqueue_style('fluent-form-preview', $app->publicUrl('css/preview.css'));
82 });
83
84 (new \FluentForm\App\Modules\ProcessExteriorModule())->handleExteriorPages();
85 }
86 }, 1);
87
88 $elements = [
89 'select',
90 'input_checkbox',
91 'input_radio',
92 'address',
93 'select_country',
94 'gdpr_agreement',
95 'terms_and_condition',
96 ];
97
98 foreach ($elements as $element) {
99 $event = 'fluentform_response_render_' . $element;
100 $app->addFilter($event, function ($response, $field, $form_id, $isLabel = false) {
101 $element = $field['element'];
102
103 if ('address' == $element && !empty($response->country)) {
104 $countryList = getFluentFormCountryList();
105 if (isset($countryList[$response->country])) {
106 $response->country = $countryList[$response->country];
107 }
108 }
109
110 if ('select_country' == $element) {
111 $countryList = getFluentFormCountryList();
112 if (isset($countryList[$response])) {
113 $response = $countryList[$response];
114 }
115 }
116
117 if (in_array($field['element'], array('gdpr_agreement', 'terms_and_condition'))) {
118 if (!empty($response) && $response == 'on') {
119 $response = __('Accepted', 'fluentform');
120 } else {
121 $response = __('Declined', 'fluentform');
122 }
123 }
124
125 if ($response && $isLabel && in_array($element, ['select', 'input_radio']) && !is_array($response)) {
126 if (!isset($field['options'])) {
127 $field['options'] = [];
128 foreach (\FluentForm\Framework\Helpers\ArrayHelper::get($field, 'raw.settings.advanced_options', []) as $option) {
129 $field['options'][$option['value']] = $option['label'];
130 }
131 }
132 if (isset($field['options'][$response])) {
133 return $field['options'][$response];
134 }
135 }
136
137 if (in_array($element, ['select', 'input_checkbox']) && is_array($response)) {
138 return \FluentForm\App\Modules\Form\FormDataParser::formatCheckBoxValues($response, $field, $isLabel);
139 }
140
141 return \FluentForm\App\Modules\Form\FormDataParser::formatValue($response);
142 }, 10, 4);
143 }
144
145 $app->addFilter('fluentform_response_render_textarea', function ($value, $field, $formId, $isHtml) {
146 $value = $value ? nl2br($value) : $value;
147
148 if (!$isHtml || !$value) {
149 return $value;
150 }
151 return '<span style="white-space: pre-line">' . $value . '</span>';
152 }, 10, 4);
153
154 $app->addFilter('fluentform_response_render_input_file', function ($response, $field, $form_id, $isHtml = false) {
155 return \FluentForm\App\Modules\Form\FormDataParser::formatFileValues($response, $isHtml, $form_id);
156 }, 10, 4);
157
158 $app->addFilter('fluentform_response_render_input_image', function ($response, $field, $form_id, $isHtml = false) {
159 return \FluentForm\App\Modules\Form\FormDataParser::formatImageValues($response, $isHtml, $form_id);
160 }, 10, 4);
161
162 $app->addFilter('fluentform_response_render_input_repeat', function ($response, $field, $form_id) {
163 return \FluentForm\App\Modules\Form\FormDataParser::formatRepeatFieldValue($response, $field, $form_id);
164 }, 10, 3);
165
166 $app->addFilter('fluentform_response_render_tabular_grid', function ($response, $field, $form_id, $isHtml = false) {
167 return \FluentForm\App\Modules\Form\FormDataParser::formatTabularGridFieldValue($response, $field, $form_id, $isHtml);
168 }, 10, 4);
169
170 $app->addFilter('fluentform_response_render_input_name', function ($response) {
171 return \FluentForm\App\Modules\Form\FormDataParser::formatName($response);
172 }, 10, 1);
173
174 $app->addFilter('fluentform_response_render_input_password', function ($value, $field, $formId) {
175 if (Helper::shouldHidePassword($formId)) {
176 $value = str_repeat('*', 6) . ' ' . __('(truncated)', 'fluentform');
177 }
178
179 return $value;
180 }, 10, 3);
181
182 $app->addFilter('fluentform_filter_insert_data', function ($data) {
183 $settings = get_option('_fluentform_global_form_settings', false);
184 if (is_array($settings) && isset($settings['misc'])) {
185 if (isset($settings['misc']['isIpLogingDisabled'])) {
186 if ($settings['misc']['isIpLogingDisabled']) {
187 unset($data['ip']);
188 }
189 }
190 }
191 return $data;
192 });
193
194 // Register api response log hooks
195 $app->addAction(
196 'fluentform_after_submission_api_response_success',
197 'fluentform_after_submission_api_response_success',
198 10,
199 6
200 );
201
202 $app->addAction(
203 'fluentform_after_submission_api_response_failed',
204 'fluentform_after_submission_api_response_failed',
205 10,
206 6
207 );
208
209 function fluentform_after_submission_api_response_success($form, $entryId, $data, $feed, $res, $msg = '')
210 {
211 try {
212 $isDev = 'production' != wpFluentForm()->getEnv();
213 if (!apply_filters('fluentform_api_success_log', $isDev, $form, $feed)) {
214 return;
215 }
216
217 wpFluent()->table('fluentform_submission_meta')->insert([
218 'response_id' => $entryId,
219 'form_id' => $form->id,
220 'meta_key' => 'api_log',
221 'value' => $msg,
222 'name' => $feed->formattedValue['name'],
223 'status' => 'success',
224 'created_at' => current_time('mysql'),
225 'updated_at' => current_time('mysql'),
226 ]);
227 } catch (Exception $e) {
228 error_log($e->getMessage());
229 }
230 }
231
232 function fluentform_after_submission_api_response_failed($form, $entryId, $data, $feed, $res, $msg = '')
233 {
234 try {
235 $isDev = 'production' != wpFluentForm()->getEnv();
236 if (!apply_filters('fluentform_api_failed_log', $isDev, $form, $feed)) {
237 return;
238 }
239
240 wpFluent()->table('fluentform_submission_meta')->insert([
241 'response_id' => $entryId,
242 'form_id' => $form->id,
243 'meta_key' => 'api_log',
244 'value' => json_encode($res),
245 'name' => $feed->formattedValue['name'],
246 'status' => 'failed',
247 'created_at' => current_time('mysql'),
248 'updated_at' => current_time('mysql'),
249 ]);
250 } catch (Exception $e) {
251 error_log($e->getMessage());
252 }
253 }
254
255 $app->bindInstance(
256 'fluentFormAsyncRequest',
257 new \FluentForm\App\Services\WPAsync\FluentFormAsyncRequest($app),
258 'FluentFormAsyncRequest',
259 'FluentForm\App\Services\WPAsync\FluentFormAsyncRequest'
260 );
261
262 $app->addFilter('fluentform-disabled_analytics', function ($status) {
263 $settings = get_option('_fluentform_global_form_settings');
264 if (isset($settings['misc']['isAnalyticsDisabled']) && $settings['misc']['isAnalyticsDisabled']) {
265 return true;
266 }
267 return $status;
268 });
269
270 $app->addAction('fluentform_before_form_render', function ($form) {
271 do_action('fluentform_load_form_assets', $form->id);
272 });
273
274 $app->addAction('fluentform_load_form_assets', function ($formId) {
275 // check if alreaded loaded
276 if (!in_array($formId, \FluentForm\App\Helpers\Helper::$loadedForms)) {
277 (new \FluentForm\App\Modules\Form\Settings\FormCssJs())->addCssJs($formId);
278 \FluentForm\App\Helpers\Helper::$loadedForms[] = $formId;
279 $selectedStyle = \FluentForm\App\Helpers\Helper::getFormMeta($formId, '_ff_selected_style');
280
281 if ($selectedStyle) {
282 do_action('fluentform_init_custom_stylesheet', $selectedStyle, $formId);
283 }
284 }
285 });
286
287 $app->addAction('fluentform_submission_inserted', function ($insertId, $formData, $form) use ($app) {
288 $notificationManager = new \FluentForm\App\Services\Integrations\GlobalNotificationManager($app);
289 $notificationManager->globalNotify($insertId, $formData, $form);
290 }, 10, 3);
291
292 $app->addAction('init', function () use ($app) {
293 new \FluentForm\App\Services\Integrations\MailChimp\MailChimpIntegration($app);
294 });
295
296 $app->addAction('fluentform_form_element_start', function ($form) use ($app) {
297 $honeyPot = new \FluentForm\App\Modules\Form\HoneyPot($app);
298 $honeyPot->renderHoneyPot($form);
299 });
300
301 $app->addAction('fluentform_before_insert_submission', function ($insertData, $requestData, $form) use ($app) {
302 $honeyPot = new \FluentForm\App\Modules\Form\HoneyPot($app);
303 $honeyPot->verify($insertData, $requestData, $form->id);
304 }, 9, 3);
305
306 add_action('ff_log_data', function ($data) use ($app) {
307 $dataLogger = new \FluentForm\App\Modules\Logger\DataLogger($app);
308 $dataLogger->log($data);
309 });
310
311 // permision based filters
312 add_filter('fluentform_permission_callback', function ($status, $permission) {
313 return (new \FluentForm\App\Modules\Acl\RoleManager())->currentUserFormFormCapability();
314 }, 10, 2);
315
316 // widgets
317 add_action('widgets_init', function () {
318 register_widget('FluentForm\App\Modules\Widgets\SidebarWidgets');
319 });
320
321 add_action('wp', function () {
322 global $post;
323
324 if (!is_a($post, 'WP_Post')) {
325 return;
326 }
327
328 $fluentFormIds = get_post_meta($post->ID, '_has_fluentform', true);
329
330 if ($fluentFormIds && is_array($fluentFormIds)) {
331 foreach ($fluentFormIds as $formId) {
332 do_action('fluentform_load_form_assets', $formId);
333 }
334 }
335 });
336
337 add_filter('fluentform_validate_input_item_input_email', ['\FluentForm\App\Helpers\Helper', 'isUniqueValidation'], 10, 5);
338 add_filter('fluentform_validate_input_item_input_text', ['\FluentForm\App\Helpers\Helper', 'isUniqueValidation'], 10, 5);
339
340 add_filter('cron_schedules', function ($schedules) {
341 $schedules['ff_every_five_minutes'] = [
342 'interval' => 300,
343 'display' => esc_html__('Every 5 minutes (FluentForm)', 'fluentform'),
344 ];
345 return $schedules;
346 }, 10, 1);
347
348 add_action('fluentform_do_scheduled_tasks', 'fluentFormHandleScheduledTasks');
349 add_action('fluentform_do_email_report_scheduled_tasks', 'fluentFormHandleScheduledEmailReport');
350
351 add_action('ff_integration_action_result', function ($feed, $status, $note = '') {
352 if (!isset($feed['scheduled_action_id']) || !$status) {
353 return;
354 }
355 if (!$note) {
356 $note = $status;
357 }
358
359 if (strlen($note) > 255) {
360 if (function_exists('mb_substr')) {
361 $note = mb_substr($note, 0, 251) . '...';
362 } else {
363 $note = substr($note, 0, 251) . '...';
364 }
365 }
366
367 $actionId = intval($feed['scheduled_action_id']);
368 wpFluent()->table('ff_scheduled_actions')
369 ->where('id', $actionId)
370 ->update([
371 'status' => $status,
372 'note' => $note,
373 ]);
374 }, 10, 3);
375
376 add_action('fluentform_global_notify_completed', function ($insertId, $form) use ($app) {
377 if (strpos($form->form_fields, '"element":"input_password"') && apply_filters('fluentform_truncate_password_values', true, $form->id)) {
378 // we have password
379 (new \FluentForm\App\Services\Integrations\GlobalNotificationManager($app))->cleanUpPassword($insertId, $form);
380 }
381 }, 10, 2);
382
383 /*
384 * Elementor Block Init
385 */
386
387 if (defined('ELEMENTOR_VERSION')) {
388 new \FluentForm\App\Modules\Widgets\ElementorWidget($app);
389 }
390 /*
391 * Oxygen Widget Init
392 */
393
394 add_action('init', function () {
395 if (class_exists('OxyEl')) {
396 if (file_exists(FLUENTFORM_DIR_PATH . 'app/Modules/Widgets/OxygenWidget.php')) {
397 new FluentForm\App\Modules\Widgets\OxygenWidget();
398 }
399 }
400 });
401
402 (new FluentForm\App\Services\Integrations\Slack\SlackNotificationActions($app))->register();
403
404 /*
405 * Smartcode parser shortcodes
406 */
407
408 add_filter('ff_will_return_html', function ($result, $integration, $key) {
409 $dictionary = [
410 'notifications' => ['message'],
411 ];
412
413 if (!isset($dictionary[$integration])) {
414 return $result;
415 }
416
417 if (in_array($key, $dictionary[$integration])) {
418 return true;
419 }
420
421 return $result;
422 }, 10, 3);
423
424 $app->addFilter('fluentform_response_render_input_number', function ($response, $field, $form_id, $isHtml = false) {
425 if (!$response || !$isHtml) {
426 return $response;
427 }
428 $fieldSettings = \FluentForm\Framework\Helpers\ArrayHelper::get($field, 'raw.settings');
429 $formatter = \FluentForm\Framework\Helpers\ArrayHelper::get($fieldSettings, 'numeric_formatter');
430 if (!$formatter) {
431 return $response;
432 }
433 return \FluentForm\App\Helpers\Helper::getNumericFormatted($response, $formatter);
434 }, 10, 4);
435
436 new \FluentForm\App\Services\FormBuilder\Components\CustomSubmitButton();
437
438 if (function_exists('register_block_type')) {
439 register_block_type('fluentfom/guten-block', [
440 'render_callback' => function ($atts) {
441 if (empty($atts['formId'])) {
442 return '';
443 }
444
445 $className = \FluentForm\Framework\Helpers\ArrayHelper::get($atts, 'className');
446
447 if ($className) {
448 $classes = explode(' ', $className);
449 $className = '';
450 if (!empty($classes)) {
451 foreach ($classes as $class) {
452 $className .= sanitize_html_class($class) . ' ';
453 }
454 }
455 }
456 $type = \FluentForm\App\Helpers\Helper::isConversionForm($atts['formId']) ? 'conversational' : '';
457 return do_shortcode('[fluentform css_classes="' . $className . ' ff_guten_block" id="' . $atts['formId'] . '" type="' . $type . '"]');
458 },
459 'attributes' => [
460 'formId' => [
461 'type' => 'string',
462 ],
463 'className' => [
464 'type' => 'string',
465 ],
466 ],
467 ]);
468 }
469
470 // require the CLI
471 if (defined('WP_CLI') && WP_CLI) {
472 \WP_CLI::add_command('fluentform', '\FluentForm\App\Modules\CLI\Commands');
473 }
474