| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Form; |
| 4 |
|
| 5 |
use FluentForm\App\Helpers\Helper; |
| 6 |
use FluentForm\App\Modules\Acl\Acl; |
| 7 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 8 |
|
| 9 |
class AkismetHandler |
| 10 |
{ |
| 11 |
public static function isEnabled() |
| 12 |
{ |
| 13 |
if (!self::isPluginEnabled()) { |
| 14 |
return false; |
| 15 |
} |
| 16 |
|
| 17 |
$settings = get_option('_fluentform_global_form_settings'); |
| 18 |
return $settings && ArrayHelper::get($settings, 'misc.akismet_status') == 'yes'; |
| 19 |
} |
| 20 |
|
| 21 |
public static function isPluginEnabled() |
| 22 |
{ |
| 23 |
$exists = method_exists('Akismet', 'http_post'); |
| 24 |
if ($exists) { |
| 25 |
return !!\Akismet::get_api_key(); |
| 26 |
} |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
public static function isSpamSubmission($formData, $form) |
| 31 |
{ |
| 32 |
global $akismet_api_host, $akismet_api_port; |
| 33 |
$fields = self::getAkismetFields($formData, $form); |
| 34 |
$response = \Akismet::http_post( $fields, 'comment-check' ); |
| 35 |
|
| 36 |
return ArrayHelper::get($response, 1) == 'true'; |
| 37 |
} |
| 38 |
|
| 39 |
protected static function getAkismetFields($data, $form) |
| 40 |
{ |
| 41 |
$app = wpFluentForm(); |
| 42 |
$ip = $app->request->getIp(); |
| 43 |
|
| 44 |
$info = [ |
| 45 |
'comment_type' => 'contact-form', |
| 46 |
'comment_author' => '', |
| 47 |
'comment_author_email' => '', |
| 48 |
'comment_content' => '', |
| 49 |
'contact_form_subject' => $form->title, |
| 50 |
'comment_author_IP' => $ip, |
| 51 |
'permalink' => urlencode($data['_wp_http_referer']), |
| 52 |
'user_ip' => preg_replace('/[^0-9., ]/', '', $ip), |
| 53 |
'user_agent' => $app->request->header('USER_AGENT'), |
| 54 |
'blog' => get_option('home') |
| 55 |
]; |
| 56 |
|
| 57 |
$maps = [ |
| 58 |
'input_name' => 'comment_author', |
| 59 |
'input_email' => 'comment_author_email', |
| 60 |
'textarea' => 'comment_content' |
| 61 |
]; |
| 62 |
$inputs = FormFieldsParser::getInputs($form, ['attributes']); |
| 63 |
|
| 64 |
foreach ($inputs as $input) { |
| 65 |
$element = ArrayHelper::get($input, 'element'); |
| 66 |
$key = ArrayHelper::get($input, 'attributes.name'); |
| 67 |
if (isset($maps[$element]) && !$info[$maps[$element]]) { |
| 68 |
$value = ArrayHelper::get($data, $key); |
| 69 |
if ($value) { |
| 70 |
if (is_array($value)) { |
| 71 |
$value = implode(' ', $value); |
| 72 |
} |
| 73 |
$info[$maps[$element]] = $value; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
$info = apply_filters('fluentform_akismet_fields', $info, $data, $form); |
| 79 |
|
| 80 |
return http_build_query($info); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
} |