| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') or die; |
| 4 |
|
| 5 |
/** |
| 6 |
* All registered filter's handlers should be in app\Hooks\Handlers, |
| 7 |
* addFilter is similar to add_filter and addCustomFlter is just a |
| 8 |
* wrapper over add_filter which will add a prefix to the hook name |
| 9 |
* using the plugin slug to make it unique in all wordpress plugins, |
| 10 |
* ex: $app->addCustomFilter('foo', ['FooHandler', 'handleFoo']) is |
| 11 |
* equivalent to add_filter('slug-foo', ['FooHandler', 'handleFoo']). |
| 12 |
*/ |
| 13 |
|
| 14 |
/** |
| 15 |
* @var $app FluentForm\Framework\Foundation\Application |
| 16 |
*/ |
| 17 |
|
| 18 |
add_filter('fluentform/addons_extra_menu', function ($menus) { |
| 19 |
$menus['fluentform_pdf'] = __('Fluent Forms PDF', 'fluentform'); |
| 20 |
|
| 21 |
return $menus; |
| 22 |
}, 99, 1); |
| 23 |
|
| 24 |
//Add file upload location in global settings |
| 25 |
add_filter('fluentform/get_global_settings_values', function ($values, $key) { |
| 26 |
if (is_array($key)) { |
| 27 |
if (in_array('_fluentform_global_form_settings', $key)) { |
| 28 |
$values['file_upload_optoins'] = FluentForm\App\Helpers\Helper::fileUploadLocations(); |
| 29 |
} |
| 30 |
|
| 31 |
if (in_array('_fluentform_turnstile_details', $key)) { |
| 32 |
$values = FluentForm\App\Modules\Turnstile\Turnstile::ensureSettings($values); |
| 33 |
} |
| 34 |
|
| 35 |
if (in_array('_fluentform_global_default_message_setting_fields', $key)) { |
| 36 |
$values['_fluentform_global_default_message_setting_fields'] = \FluentForm\App\Helpers\Helper::globalDefaultMessageSettingFields(); |
| 37 |
} |
| 38 |
|
| 39 |
if (in_array('_fluentform_global_form_settings', $key) && |
| 40 |
!\FluentForm\Framework\Helpers\ArrayHelper::isTrue($values, '_fluentform_global_form_settings.default_messages') |
| 41 |
) { |
| 42 |
$values['_fluentform_global_form_settings']['default_messages'] = \FluentForm\App\Helpers\Helper::getAllGlobalDefaultMessages(); |
| 43 |
} |
| 44 |
|
| 45 |
// Ensure _fluentform_default_style_template has default structure if not set |
| 46 |
if (in_array('_fluentform_default_style_template', $key)) { |
| 47 |
if (empty($values['_fluentform_default_style_template']) || $values['_fluentform_default_style_template'] === false) { |
| 48 |
$values['_fluentform_default_style_template'] = [ |
| 49 |
'enabled' => 'no', |
| 50 |
'custom_css' => '', |
| 51 |
'styler_enabled' => 'no', |
| 52 |
'styler_theme' => '', |
| 53 |
]; |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return $values; |
| 59 |
}, 10, 2); |
| 60 |
|
| 61 |
//Enables recaptcha validation when autoload recaptcha enabled for all forms |
| 62 |
|
| 63 |
add_action('fluentform/before_form_validation',function (){ |
| 64 |
$autoIncludeRecaptcha = [ |
| 65 |
[ |
| 66 |
'type' => 'hcaptcha', |
| 67 |
'is_disabled' => !get_option('_fluentform_hCaptcha_keys_status', false), |
| 68 |
], |
| 69 |
[ |
| 70 |
'type' => 'recaptcha', |
| 71 |
'is_disabled' => !get_option('_fluentform_reCaptcha_keys_status', false), |
| 72 |
], |
| 73 |
[ |
| 74 |
'type' => 'turnstile', |
| 75 |
'is_disabled' => !get_option('_fluentform_turnstile_keys_status', false), |
| 76 |
], |
| 77 |
]; |
| 78 |
foreach ($autoIncludeRecaptcha as $input) { |
| 79 |
if ($input['is_disabled']) { |
| 80 |
continue; |
| 81 |
} |
| 82 |
|
| 83 |
add_filter('fluentform/has_' . $input['type'], function () use ($input) { |
| 84 |
$option = get_option('_fluentform_global_form_settings'); |
| 85 |
$autoload = \FluentForm\Framework\Helpers\ArrayHelper::get($option, 'misc.autoload_captcha'); |
| 86 |
$type = \FluentForm\Framework\Helpers\ArrayHelper::get($option, 'misc.captcha_type'); |
| 87 |
|
| 88 |
if ($autoload && $type == $input['type']) { |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
return false; |
| 93 |
}); |
| 94 |
} |
| 95 |
}); |
| 96 |
|
| 97 |
/* |
| 98 |
* Push captcha in all forms when enabled from global settings |
| 99 |
*/ |
| 100 |
$app->addFilter('fluentform/rendering_form', function ($form) { |
| 101 |
$option = get_option('_fluentform_global_form_settings'); |
| 102 |
$enabled = \FluentForm\Framework\Helpers\ArrayHelper::get($option, 'misc.autoload_captcha'); |
| 103 |
if (!$enabled) { |
| 104 |
return $form; |
| 105 |
} |
| 106 |
$type = \FluentForm\Framework\Helpers\ArrayHelper::get($option, 'misc.captcha_type'); |
| 107 |
$reCaptcha = [ |
| 108 |
'element' => 'recaptcha', |
| 109 |
'attributes' => [ |
| 110 |
'name' => 'recaptcha', |
| 111 |
], |
| 112 |
]; |
| 113 |
$hCaptcha = [ |
| 114 |
'element' => 'hcaptcha', |
| 115 |
'attributes' => [ |
| 116 |
'name' => 'hcaptcha', |
| 117 |
], |
| 118 |
]; |
| 119 |
$turnstile = [ |
| 120 |
'element' => 'turnstile', |
| 121 |
'attributes' => [ |
| 122 |
'name' => 'turnstile', |
| 123 |
], |
| 124 |
]; |
| 125 |
|
| 126 |
if ('recaptcha' == $type) { |
| 127 |
$captcha = $reCaptcha; |
| 128 |
} elseif ('hcaptcha' == $type) { |
| 129 |
$captcha = $hCaptcha; |
| 130 |
} elseif ('turnstile' == $type) { |
| 131 |
$captcha = $turnstile; |
| 132 |
} |
| 133 |
if (!isset($captcha)) { |
| 134 |
return $form; |
| 135 |
} |
| 136 |
// place recaptcha below custom submit button |
| 137 |
$hasCustomSubmit = false; |
| 138 |
foreach ($form->fields['fields'] as $index => $field) { |
| 139 |
if (in_array($field['element'], ['recaptcha', 'hcaptcha', 'turnstile'])) { |
| 140 |
\FluentForm\Framework\Helpers\ArrayHelper::forget($form->fields['fields'], $index); |
| 141 |
} |
| 142 |
if ('custom_submit_button' == $field['element']) { |
| 143 |
$hasCustomSubmit = true; |
| 144 |
array_splice($form->fields['fields'], $index, 0, [$captcha]); |
| 145 |
break; |
| 146 |
} |
| 147 |
} |
| 148 |
if (!$hasCustomSubmit) { |
| 149 |
$form->fields['fields'][] = $captcha; |
| 150 |
} |
| 151 |
|
| 152 |
return $form; |
| 153 |
}, 10, 1); |
| 154 |
|
| 155 |
$fluentformElements = [ |
| 156 |
'select', |
| 157 |
'input_checkbox', |
| 158 |
'input_radio', |
| 159 |
'address', |
| 160 |
'select_country', |
| 161 |
'gdpr_agreement', |
| 162 |
'terms_and_condition', |
| 163 |
'dynamic_field', |
| 164 |
'multi_payment_component' |
| 165 |
]; |
| 166 |
|
| 167 |
foreach ($fluentformElements as $fluentformElement) { |
| 168 |
$fluentformEvent = 'fluentform/response_render_' . $fluentformElement; |
| 169 |
$app->addFilter($fluentformEvent, function ($response, $field, $form_id, $isHtml = false) { |
| 170 |
$element = $field['element']; |
| 171 |
$isHtml = apply_filters("fluentform/format_{$element}_response_as_html", $isHtml, $response, $field); |
| 172 |
|
| 173 |
if ('dynamic_field' == $element) { |
| 174 |
$dynamicFetchValue = 'yes' == \FluentForm\Framework\Helpers\ArrayHelper::get($field, 'raw.settings.dynamic_fetch'); |
| 175 |
if ($dynamicFetchValue) { |
| 176 |
$field = apply_filters('fluentform/dynamic_field_re_fetch_result_and_resolve_value', $field); |
| 177 |
} |
| 178 |
$attrType = \FluentForm\Framework\Helpers\ArrayHelper::get($field, 'raw.attributes.type'); |
| 179 |
if ('radio' == $attrType) { |
| 180 |
$element = 'input_radio'; |
| 181 |
} elseif ('checkbox' == $attrType) { |
| 182 |
$element = 'input_checkbox'; |
| 183 |
} elseif ('select' == $attrType) { |
| 184 |
$element = 'select'; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if ('address' == $element && !empty($response->country)) { |
| 189 |
$countryList = getFluentFormCountryList(); |
| 190 |
if (isset($countryList[$response->country])) { |
| 191 |
$response->country = $countryList[$response->country]; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
if ('address' == $element) { |
| 196 |
unset($response->latitude, $response->longitude); |
| 197 |
} |
| 198 |
|
| 199 |
if ('select_country' == $element) { |
| 200 |
$countryList = getFluentFormCountryList(); |
| 201 |
if (isset($countryList[$response])) { |
| 202 |
$response = $countryList[$response]; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
if (in_array($field['element'], ['gdpr_agreement', 'terms_and_condition'])) { |
| 207 |
if (!empty($response) && 'on' == $response) { |
| 208 |
$response = __('Accepted', 'fluentform'); |
| 209 |
} else { |
| 210 |
$response = __('Declined', 'fluentform'); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
if ($response && ($isHtml || defined('FLUENTFORM_RENDERING_ENTRIES')) && in_array($element, ['select', 'input_radio']) && !is_array($response)) { |
| 215 |
if (!isset($field['options'])) { |
| 216 |
$field['options'] = \FluentForm\App\Helpers\Helper::advancedOptionsValueLabelMap( |
| 217 |
\FluentForm\Framework\Helpers\ArrayHelper::get($field, 'raw.settings.advanced_options', []) |
| 218 |
); |
| 219 |
} |
| 220 |
if (isset($field['options'][$response])) { |
| 221 |
return $field['options'][$response]; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
if (in_array($element, ['select', 'input_checkbox', 'multi_payment_component']) && is_array($response)) { |
| 226 |
return \FluentForm\App\Modules\Form\FormDataParser::formatCheckBoxValues($response, $field, $isHtml); |
| 227 |
} |
| 228 |
|
| 229 |
return \FluentForm\App\Modules\Form\FormDataParser::formatValue($response); |
| 230 |
}, 10, 4); |
| 231 |
} |
| 232 |
|
| 233 |
/* |
| 234 |
* Validation rule wise resolve global validation message. |
| 235 |
* |
| 236 |
*/ |
| 237 |
$fluentformRules = [ |
| 238 |
"required", |
| 239 |
"email", |
| 240 |
"numeric", |
| 241 |
"min", |
| 242 |
"max", |
| 243 |
"digits", |
| 244 |
"url", |
| 245 |
"allowed_image_types", |
| 246 |
"allowed_file_types", |
| 247 |
"max_file_size", |
| 248 |
"max_file_count", |
| 249 |
"valid_phone_number", |
| 250 |
]; |
| 251 |
foreach ($fluentformRules as $fluentformRuleName) { |
| 252 |
$app->addFilter('fluentform/get_global_message_' . $fluentformRuleName, |
| 253 |
function ($message) use ($fluentformRuleName) { |
| 254 |
return \FluentForm\App\Helpers\Helper::getGlobalDefaultMessage($fluentformRuleName); |
| 255 |
} |
| 256 |
); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
$app->addFilter('fluentform/response_render_textarea', function ($value, $field, $formId, $isHtml) { |
| 261 |
if (is_array($value) || is_object($value)) { |
| 262 |
$value = fluentImplodeRecursive(', ', array_filter(array_values((array) $value))); |
| 263 |
} |
| 264 |
|
| 265 |
$value = $value ? nl2br($value) : $value; |
| 266 |
|
| 267 |
if (!$isHtml || !$value) { |
| 268 |
return $value; |
| 269 |
} |
| 270 |
|
| 271 |
return nl2br($value); |
| 272 |
}, 10, 4); |
| 273 |
|
| 274 |
$app->addFilter('fluentform/response_render_input_file', function ($response, $field, $form_id, $isHtml = false) { |
| 275 |
return \FluentForm\App\Modules\Form\FormDataParser::formatFileValues($response, $isHtml, $form_id); |
| 276 |
}, 10, 4); |
| 277 |
|
| 278 |
$app->addFilter('fluentform/response_render_input_image', function ($response, $field, $form_id, $isHtml = false) { |
| 279 |
return \FluentForm\App\Modules\Form\FormDataParser::formatImageValues($response, $isHtml, $form_id); |
| 280 |
}, 10, 4); |
| 281 |
|
| 282 |
$app->addFilter('fluentform/response_render_input_repeat', function ($response, $field, $form_id) { |
| 283 |
return \FluentForm\App\Modules\Form\FormDataParser::formatRepeatFieldValue($response, $field, $form_id); |
| 284 |
}, 10, 3); |
| 285 |
|
| 286 |
$app->addFilter('fluentform/response_render_tabular_grid', function ($response, $field, $form_id, $isHtml = false) { |
| 287 |
return \FluentForm\App\Modules\Form\FormDataParser::formatTabularGridFieldValue($response, $field, $form_id, $isHtml); |
| 288 |
}, 10, 4); |
| 289 |
|
| 290 |
$app->addFilter('fluentform/response_render_input_name', function ($response) { |
| 291 |
return \FluentForm\App\Modules\Form\FormDataParser::formatName($response); |
| 292 |
}, 10, 1); |
| 293 |
|
| 294 |
$app->addFilter('fluentform/response_render_input_password', function ($value, $field, $formId) { |
| 295 |
if (\FluentForm\App\Helpers\Helper::shouldHidePassword($formId)) { |
| 296 |
$value = str_repeat('*', 6) . ' ' . __('(truncated)', 'fluentform'); |
| 297 |
} |
| 298 |
|
| 299 |
return $value; |
| 300 |
}, 10, 3); |
| 301 |
|
| 302 |
$app->addFilter('fluentform/filter_insert_data', function ($data) { |
| 303 |
$settings = get_option('_fluentform_global_form_settings', false); |
| 304 |
if (is_array($settings) && isset($settings['misc'])) { |
| 305 |
if (isset($settings['misc']['isIpLogingDisabled'])) { |
| 306 |
if ($settings['misc']['isIpLogingDisabled']) { |
| 307 |
unset($data['ip']); |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
return $data; |
| 313 |
}); |
| 314 |
|
| 315 |
$app->addFilter('fluentform/disabled_analytics', function ($status) { |
| 316 |
$settings = get_option('_fluentform_global_form_settings'); |
| 317 |
if (isset($settings['misc']['isAnalyticsDisabled']) && !$settings['misc']['isAnalyticsDisabled']) { |
| 318 |
return false; |
| 319 |
} |
| 320 |
|
| 321 |
return $status; |
| 322 |
}); |
| 323 |
|
| 324 |
// permission based filters |
| 325 |
$app->addFilter('fluentform/permission_callback', function ($status, $permission) { |
| 326 |
return \FluentForm\App\Modules\Acl\Acl::getCurrentUserCapability(); |
| 327 |
}, 10, 2); |
| 328 |
|
| 329 |
// Get current user allowed form ids, if current user has specific form permission |
| 330 |
$app->addFilter('fluentform/current_user_allowed_forms', function ($form){ |
| 331 |
return \FluentForm\App\Services\Manager\FormManagerService::getUserAllowedFormsScope(); |
| 332 |
}); |
| 333 |
|
| 334 |
$app->addFilter('fluentform/validate_input_item_input_email', ['\FluentForm\App\Helpers\Helper', 'isUniqueValidation'], 10, 5); |
| 335 |
|
| 336 |
$app->addFilter('fluentform/validate_input_item_input_text', ['\FluentForm\App\Helpers\Helper', 'isUniqueValidation'], 10, 5); |
| 337 |
|
| 338 |
$app->addFilter('fluentform/will_return_html', function ($result, $integration, $key) { |
| 339 |
$dictionary = [ |
| 340 |
'notifications' => ['message'], |
| 341 |
'pdfFeed' => apply_filters('fluentform/pdf_html_format', []) |
| 342 |
]; |
| 343 |
|
| 344 |
if (!isset($dictionary[$integration])) { |
| 345 |
return $result; |
| 346 |
} |
| 347 |
|
| 348 |
if (in_array($key, $dictionary[$integration])) { |
| 349 |
return true; |
| 350 |
} |
| 351 |
|
| 352 |
return $result; |
| 353 |
}, 10, 3); |
| 354 |
|
| 355 |
$app->addFilter('fluentform/response_render_input_number', function ($response, $field, $form_id, $isHtml = false) { |
| 356 |
if (!$response || !$isHtml) { |
| 357 |
return $response; |
| 358 |
} |
| 359 |
$fieldSettings = \FluentForm\Framework\Helpers\ArrayHelper::get($field, 'raw.settings'); |
| 360 |
$formatter = \FluentForm\Framework\Helpers\ArrayHelper::get($fieldSettings, 'numeric_formatter'); |
| 361 |
if (!$formatter) { |
| 362 |
return $response; |
| 363 |
} |
| 364 |
|
| 365 |
return \FluentForm\App\Helpers\Helper::getNumericFormatted($response, $formatter); |
| 366 |
}, 10, 4); |
| 367 |
|
| 368 |
$app->addFilter( |
| 369 |
'fluentform/create_default_settings', |
| 370 |
function($defaultSettings) { |
| 371 |
if (!isset($defaultSettings['restrictions']['restrictForm'])) { |
| 372 |
$defaultSettings['restrictions']['restrictForm'] = [ |
| 373 |
'enabled' => false, |
| 374 |
'fields' => [ |
| 375 |
'ip' => [ |
| 376 |
'status' => false, |
| 377 |
'values' => '', |
| 378 |
'message' => __('Sorry! You can\'t submit a form from your IP address.', 'fluentform'), |
| 379 |
'validation_type' => 'fail_on_condition_met' |
| 380 |
], |
| 381 |
'country' => [ |
| 382 |
'status' => false, |
| 383 |
'values' => [], |
| 384 |
'message' => __('Sorry! You can\'t submit a form the country you are residing.', 'fluentform'), |
| 385 |
'validation_type' => 'fail_on_condition_met' |
| 386 |
], |
| 387 |
'keywords' => [ |
| 388 |
'status' => false, |
| 389 |
'values' => '', |
| 390 |
'message' => __('Sorry! Your submission contains some restricted keywords.', 'fluentform') |
| 391 |
], |
| 392 |
] |
| 393 |
]; |
| 394 |
} |
| 395 |
|
| 396 |
if (!isset($defaultSettings['conv_form_per_step_save'])) { |
| 397 |
$defaultSettings['conv_form_per_step_save'] = false; |
| 398 |
} |
| 399 |
|
| 400 |
if (!isset($defaultSettings['conv_form_resume_from_last_step'])) { |
| 401 |
$defaultSettings['conv_form_resume_from_last_step'] = false; |
| 402 |
} |
| 403 |
|
| 404 |
return $defaultSettings; |
| 405 |
} |
| 406 |
); |
| 407 |
|
| 408 |
// render badge toggle in form editor in case of recaptcha v3 |
| 409 |
$app->addFilter('fluentform/editor_element_settings_placement', function($placements, $form) { |
| 410 |
$recaptchaDetails = get_option('_fluentform_reCaptcha_details'); |
| 411 |
if (!$recaptchaDetails) { |
| 412 |
return $placements; |
| 413 |
} |
| 414 |
|
| 415 |
$isRecaptchaV3 = \FluentForm\Framework\Helpers\ArrayHelper::get($recaptchaDetails, 'api_version') === 'v3_invisible'; |
| 416 |
if (!$isRecaptchaV3) { |
| 417 |
return $placements; |
| 418 |
} |
| 419 |
|
| 420 |
$placements['recaptcha']['general'][] = 'render_recaptcha_v3_badge'; |
| 421 |
return $placements; |
| 422 |
}, 10, 2); |
| 423 |
|
| 424 |
|
| 425 |
/* |
| 426 |
* Remove this after WP Fusion Update their plugin |
| 427 |
*/ |
| 428 |
add_filter('fluentform/is_integration_enabled_wpfusion', '__return_true'); |
| 429 |
|