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