acceptance.php
10 years ago
akismet.php
11 years ago
checkbox.php
10 years ago
count.php
11 years ago
date.php
10 years ago
file.php
10 years ago
flamingo.php
10 years ago
jetpack.php
10 years ago
listo.php
12 years ago
number.php
10 years ago
quiz.php
10 years ago
really-simple-captcha.php
10 years ago
recaptcha.php
10 years ago
response.php
11 years ago
select.php
10 years ago
submit.php
10 years ago
text.php
10 years ago
textarea.php
10 years ago
akismet.php
143 lines
| 1 | <?php |
| 2 | /** |
| 3 | ** Akismet Filter |
| 4 | ** Akismet API: http://akismet.com/development/api/ |
| 5 | **/ |
| 6 | |
| 7 | add_filter( 'wpcf7_spam', 'wpcf7_akismet' ); |
| 8 | |
| 9 | function wpcf7_akismet( $spam ) { |
| 10 | if ( $spam ) { |
| 11 | return $spam; |
| 12 | } |
| 13 | |
| 14 | if ( ! wpcf7_akismet_is_available() ) { |
| 15 | return false; |
| 16 | } |
| 17 | |
| 18 | if ( ! $params = wpcf7_akismet_submitted_params() ) { |
| 19 | return false; |
| 20 | } |
| 21 | |
| 22 | $c = array(); |
| 23 | |
| 24 | $c['comment_author'] = $params['author']; |
| 25 | $c['comment_author_email'] = $params['author_email']; |
| 26 | $c['comment_author_url'] = $params['author_url']; |
| 27 | $c['comment_content'] = $params['content']; |
| 28 | |
| 29 | $c['blog'] = get_option( 'home' ); |
| 30 | $c['blog_lang'] = get_locale(); |
| 31 | $c['blog_charset'] = get_option( 'blog_charset' ); |
| 32 | $c['user_ip'] = $_SERVER['REMOTE_ADDR']; |
| 33 | $c['user_agent'] = $_SERVER['HTTP_USER_AGENT']; |
| 34 | $c['referrer'] = $_SERVER['HTTP_REFERER']; |
| 35 | |
| 36 | // http://blog.akismet.com/2012/06/19/pro-tip-tell-us-your-comment_type/ |
| 37 | $c['comment_type'] = 'contact-form'; |
| 38 | |
| 39 | if ( $permalink = get_permalink() ) { |
| 40 | $c['permalink'] = $permalink; |
| 41 | } |
| 42 | |
| 43 | $ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' ); |
| 44 | |
| 45 | foreach ( $_SERVER as $key => $value ) { |
| 46 | if ( ! in_array( $key, (array) $ignore ) ) |
| 47 | $c["$key"] = $value; |
| 48 | } |
| 49 | |
| 50 | return wpcf7_akismet_comment_check( $c ); |
| 51 | } |
| 52 | |
| 53 | function wpcf7_akismet_is_available() { |
| 54 | if ( is_callable( array( 'Akismet', 'get_api_key' ) ) ) { // Akismet v3.0+ |
| 55 | return (bool) Akismet::get_api_key(); |
| 56 | } |
| 57 | |
| 58 | if ( function_exists( 'akismet_get_key' ) ) { |
| 59 | return (bool) akismet_get_key(); |
| 60 | } |
| 61 | |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | function wpcf7_akismet_submitted_params() { |
| 66 | $params = array( |
| 67 | 'author' => '', |
| 68 | 'author_email' => '', |
| 69 | 'author_url' => '', |
| 70 | 'content' => '' ); |
| 71 | |
| 72 | $has_akismet_option = false; |
| 73 | |
| 74 | foreach ( (array) $_POST as $key => $val ) { |
| 75 | if ( '_wpcf7' == substr( $key, 0, 6 ) || '_wpnonce' == $key ) { |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | if ( is_array( $val ) ) { |
| 80 | $val = implode( ', ', wpcf7_array_flatten( $val ) ); |
| 81 | } |
| 82 | |
| 83 | $val = trim( $val ); |
| 84 | |
| 85 | if ( 0 == strlen( $val ) ) { |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | if ( $tags = wpcf7_scan_shortcode( array( 'name' => $key ) ) ) { |
| 90 | $tag = $tags[0]; |
| 91 | $tag = new WPCF7_Shortcode( $tag ); |
| 92 | |
| 93 | $akismet = $tag->get_option( 'akismet', |
| 94 | '(author|author_email|author_url)', true ); |
| 95 | |
| 96 | if ( $akismet ) { |
| 97 | $has_akismet_option = true; |
| 98 | |
| 99 | if ( 'author' == $akismet ) { |
| 100 | $params[$akismet] = trim( $params[$akismet] . ' ' . $val ); |
| 101 | } elseif ( '' == $params[$akismet] ) { |
| 102 | $params[$akismet] = $val; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | $params['content'] .= "\n\n" . $val; |
| 108 | } |
| 109 | |
| 110 | if ( ! $has_akismet_option ) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | $params['content'] = trim( $params['content'] ); |
| 115 | |
| 116 | return $params; |
| 117 | } |
| 118 | |
| 119 | function wpcf7_akismet_comment_check( $comment ) { |
| 120 | global $akismet_api_host, $akismet_api_port; |
| 121 | |
| 122 | $spam = false; |
| 123 | $query_string = wpcf7_build_query( $comment ); |
| 124 | |
| 125 | if ( is_callable( array( 'Akismet', 'http_post' ) ) ) { // Akismet v3.0+ |
| 126 | $response = Akismet::http_post( $query_string, 'comment-check' ); |
| 127 | } else { |
| 128 | $response = akismet_http_post( $query_string, $akismet_api_host, |
| 129 | '/1.1/comment-check', $akismet_api_port ); |
| 130 | } |
| 131 | |
| 132 | if ( 'true' == $response[1] ) { |
| 133 | $spam = true; |
| 134 | } |
| 135 | |
| 136 | if ( $submission = WPCF7_Submission::get_instance() ) { |
| 137 | $submission->akismet = array( 'comment' => $comment, 'spam' => $spam ); |
| 138 | } |
| 139 | |
| 140 | return apply_filters( 'wpcf7_akismet_comment_check', $spam, $comment ); |
| 141 | } |
| 142 | |
| 143 | ?> |