| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Form; |
| 4 |
|
| 5 |
use FluentForm\App\Helpers\Helper; |
| 6 |
use FluentForm\Framework\Foundation\Application; |
| 7 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 8 |
|
| 9 |
class HoneyPot |
| 10 |
{ |
| 11 |
private $app; |
| 12 |
|
| 13 |
public function __construct(Application $application) |
| 14 |
{ |
| 15 |
$this->app = $application; |
| 16 |
} |
| 17 |
|
| 18 |
public function renderHoneyPot($form) |
| 19 |
{ |
| 20 |
if (!$this->isEnabled($form->id)) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
$fieldName = $this->getFieldName($form->id); |
| 25 |
$fieldId = 'ff_' . $form->id . '_item_sf' ; |
| 26 |
$labels = ['Newsletter', 'Updates', 'Contact', 'Subscribe', 'Notify']; |
| 27 |
$randomLabel = $labels[array_rand($labels)]; |
| 28 |
?> |
| 29 |
<div |
| 30 |
style="display: none!important; position: absolute!important; transform: translateX(1000%)!important;" |
| 31 |
class="ff-el-group ff-hpsf-container" |
| 32 |
> |
| 33 |
<div class="ff-el-input--label asterisk-right"> |
| 34 |
<label for="<?php echo esc_attr($fieldId); ?>" aria-label="<?php echo esc_attr($randomLabel); ?>"> |
| 35 |
<?php echo esc_html($randomLabel); ?> |
| 36 |
</label> |
| 37 |
</div> |
| 38 |
<div class="ff-el-input--content"> |
| 39 |
<input type="text" |
| 40 |
name="<?php echo esc_attr($fieldName); ?>" |
| 41 |
class="ff-el-form-control" |
| 42 |
id="<?php echo esc_attr($fieldId); ?>" |
| 43 |
/> |
| 44 |
</div> |
| 45 |
</div> |
| 46 |
<?php |
| 47 |
} |
| 48 |
|
| 49 |
public function verify($insertData, $requestData, $formId) |
| 50 |
{ |
| 51 |
if (!$this->isEnabled($formId) || ( |
| 52 |
Helper::isConversionForm($formId) && |
| 53 |
ArrayHelper::isTrue($requestData, 'isFFConversational') |
| 54 |
)) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
$honeyPotName = $this->getFieldName($formId); |
| 59 |
|
| 60 |
if ( |
| 61 |
!ArrayHelper::exists($requestData, $honeyPotName) || |
| 62 |
!empty(ArrayHelper::get($requestData, $honeyPotName)) |
| 63 |
) { |
| 64 |
$message = apply_filters( |
| 65 |
'fluentform/honeypot_spam_message', |
| 66 |
__('Sorry! You can not submit this form at this moment!', 'fluentform'), |
| 67 |
$formId |
| 68 |
); |
| 69 |
wp_send_json(['errors' => $message], 422); |
| 70 |
} |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
public function isEnabled($formId = false) |
| 75 |
{ |
| 76 |
$option = get_option('_fluentform_global_form_settings'); |
| 77 |
$status = 'yes' == ArrayHelper::get($option, 'misc.honeypotStatus'); |
| 78 |
return apply_filters('fluentform/honeypot_status', $status, $formId); |
| 79 |
} |
| 80 |
|
| 81 |
private function getFieldName($formId) |
| 82 |
{ |
| 83 |
$honeyPotName = 'item_' . $formId . '__fluent_sf'; |
| 84 |
return apply_filters('fluentform/honeypot_name', $honeyPotName, $formId); |
| 85 |
} |
| 86 |
} |
| 87 |
|