1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176:
<?php
class Inbound_Akismet {
function __construct() {
self::load_hooks();
}
private function load_hooks() {
add_action( 'inbound_check_if_spam', array(__CLASS__, 'check_is_spam' ), 10, 2 );
}
public static function check_is_spam( $is_spam = false, $lead_data ) {
if (!Leads_Settings::get_setting('inbound_forms_enable_akismet', '1' )) {
return;
}
$api_key = Inbound_Akismet::get_api_key();
if (!$api_key) {
return false;
}
$params = Inbound_Akismet::prepare_params( $lead_data );
$is_spam = Inbound_Akismet::api_check( $params );
if (!$is_spam) {
return false;
}
else {
return true;
}
}
public static function api_check( $params ) {
global $akismet_api_host, $akismet_api_port;
if (!isset($params['comment_content'])) {
return;
}
$spam = false;
$query_string = '';
foreach ( $params as $key => $data ) {
$query_string .= $key . '=' . urlencode( wp_unslash( (string) $data ) ) . '&';
}
if ( is_callable( array( 'Akismet', 'http_post' ) ) ) {
$response = Akismet::http_post( $query_string, 'comment-check' );
} else {
$response = akismet_http_post( $query_string, $akismet_api_host,
'/1.1/comment-check', $akismet_api_port );
}
if ( 'true' == $response[1] ) {
return true;
}
return false;
}
public static function get_api_key() {
if ( is_callable( array( 'Akismet', 'get_api_key' ) ) ) {
return (bool) Akismet::get_api_key();
}
if ( function_exists( 'akismet_get_key' ) ) {
return (bool) akismet_get_key();
}
return false;
}
public static function prepare_params( $lead_data ) {
$first_name = (isset($lead_data['wpleads_first_name'])) ? $lead_data['wpleads_first_name'] : '';
$last_name = (isset($lead_data['wpleads_last_name'])) ? $lead_data['wpleads_last_name'] : '';
$email_address = (isset($lead_data['wpleads_email_address'])) ? $lead_data['wpleads_email_address'] : '';
$content = Inbound_Akismet::detect_content( $lead_data );
$params = array(
'comment_author' => $first_name . ' ' . $last_name,
'comment_author_email' => $email_address,
'comment_content' => $content
);
$params['blog'] = get_option( 'home' );
$params['blog_lang'] = get_locale();
$params['blog_charset'] = get_option( 'blog_charset' );
$params['user_ip'] = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] );
$params['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
$params['referrer'] = $_SERVER['HTTP_REFERER'];
$params['permalink'] = $_SERVER['HTTP_REFERER'];
$params['comment_type'] = 'contact-form';
return $params;
}
public static function detect_content( $lead_data ) {
if (isset($lead_data['form_input_values'])) {
$form_submit_values = json_decode( stripslashes($lead_data['form_input_values']), true );
if (!is_array($form_submit_values)) {
$form_submit_values = array();
}
if (isset($form_submit_values['wpleads_notes'])) {
return $form_submit_values['wpleads_notes'];
}
foreach ( $form_submit_values as $key => $value ) {
if ( !is_array( $value ) && substr_count( $value, "\n" ) > 1 ) {
return $value;
}
}
}
if (isset($lead_data['wpleads_notes'])) {
return $lead_data['wpleads_notes'];
}
foreach ( $lead_data as $key => $value ) {
if ( !is_array( $value ) && substr_count( $value, "\n" ) > 1 ) {
return $value;
}
}
return '';
}
}
new Inbound_Akismet();