Akismet.php
3.43 KB
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
<?php
if ( ! defined( 'ABSPATH' ) || ! class_exists( 'NF_Abstracts_Action' ) ) {
exit;
}
/**
* Class NF_Actions_Akismet
*/
final class NF_Actions_Akismet extends NF_Abstracts_Action {
/**
* @var string
*/
protected $_name = 'akismet';
/**
* @var array
*/
protected $_tags = array( 'spam', 'filtering', 'akismet' );
/**
* @var string
*/
protected $_timing = 'normal';
/**
* @var int
*/
protected $_priority = '10';
/**
* Constructor
*/
public function __construct() {
parent::__construct();
$this->_nicename = esc_html__( 'Akismet Anti-Spam', 'ninja-forms' );
$settings = Ninja_Forms::config( 'ActionAkismetSettings' );
$this->_settings = array_merge( $this->_settings, $settings );
add_filter( 'ninja_forms_action_type_settings', array( $this, 'maybe_remove_action' ) );
}
/**
* Remove the action registration if Akismet functions not available.
*
* @param array $action_type_settings
*
* @return array
*/
public function maybe_remove_action( $action_type_settings ) {
if ( ! $this->akismet_available() ) {
unset( $action_type_settings[ $this->_name ] );
}
return $action_type_settings;
}
/**
* Is Akismet installed and connected with a valid key?
*
* @return bool
*/
protected function akismet_available() {
if ( ! is_callable( array( 'Akismet', 'get_api_key' ) ) ) {
// Not installed and activated
return false;
}
$akismet_key = Akismet::get_api_key();
if ( empty( $akismet_key ) ) {
// No key entered
return false;
}
return 'valid' === Akismet::verify_key( $akismet_key );
}
/**
* Process the action
*
* @param array $action_settings
* @param int $form_id
* @param array $data
*
* @return array
*/
public function process( $action_settings, $form_id, $data ) {
if ( ! $this->akismet_available() ) {
return $data;
}
if ( $this->is_submission_spam( $action_settings['name'], $action_settings['email'], $action_settings['url'], $action_settings['message'] ) ) {
$data['errors']['form']['spam'] = esc_html__( 'There was an error trying to send your message. Please try again later', 'ninja-forms' );
}
return $data;
}
/**
* Verify submission
*
* @param $name
* @param $email
* @param $url
* @param $message
*
* @return bool
*/
protected function is_submission_spam( $name, $email, $url, $message ) {
$body_request = array(
'blog' => get_option( 'home' ),
'blog_lang' => get_locale(),
'permalink' => get_permalink(),
'comment_type' => 'contact-form',
'comment_author' => $name,
'comment_author_email' => $email,
'comment_author_url' => $url,
'comment_content' => $message,
'user_agent' => ( isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : null ),
);
if ( method_exists( 'Akismet', 'http_post' ) ) {
$body_request['user_ip'] = Akismet::get_ip_address();
$response = Akismet::http_post( build_query( $body_request ), 'comment-check' );
} else {
global $akismet_api_host, $akismet_api_port;
$body_request['user_ip'] = ( isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null );
$response = akismet_http_post( build_query( $body_request ), $akismet_api_host, '/1.1/comment-check', $akismet_api_port );
}
if ( ! empty( $response ) && isset( $response[1] ) && 'true' == trim( $response[1] ) ) {
// Spam!
return true;
}
return false;
}
}