acceptance.php
13 years ago
akismet.php
13 years ago
captcha.php
12 years ago
checkbox.php
13 years ago
date.php
13 years ago
file.php
12 years ago
flamingo.php
13 years ago
jetpack.php
13 years ago
number.php
13 years ago
quiz.php
13 years ago
response.php
15 years ago
select.php
12 years ago
special-mail-tags.php
13 years ago
submit.php
13 years ago
text.php
13 years ago
textarea.php
13 years ago
akismet.php
125 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 | if ( ! function_exists( 'akismet_get_key' ) || ! akismet_get_key() ) |
| 14 | return false; |
| 15 | |
| 16 | if ( ! $params = wpcf7_akismet_submitted_params() ) |
| 17 | return false; |
| 18 | |
| 19 | $c = array(); |
| 20 | |
| 21 | if ( ! empty( $params['author'] ) ) |
| 22 | $c['comment_author'] = $params['author']; |
| 23 | |
| 24 | if ( ! empty( $params['author_email'] ) ) |
| 25 | $c['comment_author_email'] = $params['author_email']; |
| 26 | |
| 27 | if ( ! empty( $params['author_url'] ) ) |
| 28 | $c['comment_author_url'] = $params['author_url']; |
| 29 | |
| 30 | if ( ! empty( $params['content'] ) ) |
| 31 | $c['comment_content'] = $params['content']; |
| 32 | |
| 33 | $c['blog'] = get_option( 'home' ); |
| 34 | $c['blog_lang'] = get_locale(); |
| 35 | $c['blog_charset'] = get_option( 'blog_charset' ); |
| 36 | $c['user_ip'] = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] ); |
| 37 | $c['user_agent'] = $_SERVER['HTTP_USER_AGENT']; |
| 38 | $c['referrer'] = $_SERVER['HTTP_REFERER']; |
| 39 | |
| 40 | // http://blog.akismet.com/2012/06/19/pro-tip-tell-us-your-comment_type/ |
| 41 | $c['comment_type'] = 'contact-form'; |
| 42 | |
| 43 | if ( $permalink = get_permalink() ) |
| 44 | $c['permalink'] = $permalink; |
| 45 | |
| 46 | $ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' ); |
| 47 | |
| 48 | foreach ( $_SERVER as $key => $value ) { |
| 49 | if ( ! in_array( $key, (array) $ignore ) ) |
| 50 | $c["$key"] = $value; |
| 51 | } |
| 52 | |
| 53 | return wpcf7_akismet_comment_check( $c ); |
| 54 | } |
| 55 | |
| 56 | function wpcf7_akismet_submitted_params() { |
| 57 | $params = array( |
| 58 | 'author' => '', |
| 59 | 'author_email' => '', |
| 60 | 'author_url' => '' ); |
| 61 | |
| 62 | $content = ''; |
| 63 | |
| 64 | $fes = wpcf7_scan_shortcode(); |
| 65 | |
| 66 | foreach ( $fes as $fe ) { |
| 67 | if ( ! isset( $fe['name'] ) || ! isset( $_POST[$fe['name']] ) ) |
| 68 | continue; |
| 69 | |
| 70 | $value = $_POST[$fe['name']]; |
| 71 | |
| 72 | if ( is_array( $value ) ) |
| 73 | $value = implode( ', ', wpcf7_array_flatten( $value ) ); |
| 74 | |
| 75 | $value = trim( $value ); |
| 76 | |
| 77 | $options = (array) $fe['options']; |
| 78 | |
| 79 | if ( preg_grep( '%^akismet:author$%', $options ) ) { |
| 80 | $params['author'] = trim( $params['author'] . ' ' . $value ); |
| 81 | |
| 82 | } elseif ( preg_grep( '%^akismet:author_email$%', $options ) ) { |
| 83 | if ( '' == $params['author_email'] ) |
| 84 | $params['author_email'] = $value; |
| 85 | |
| 86 | } elseif ( preg_grep( '%^akismet:author_url$%', $options ) ) { |
| 87 | if ( '' == $params['author_url'] ) |
| 88 | $params['author_url'] = $value; |
| 89 | } |
| 90 | |
| 91 | $content = trim( $content . "\n\n" . $value ); |
| 92 | } |
| 93 | |
| 94 | $params = array_filter( $params ); |
| 95 | |
| 96 | if ( ! $params ) |
| 97 | return false; |
| 98 | |
| 99 | $params['content'] = $content; |
| 100 | |
| 101 | return $params; |
| 102 | } |
| 103 | |
| 104 | function wpcf7_akismet_comment_check( $comment ) { |
| 105 | global $akismet_api_host, $akismet_api_port; |
| 106 | |
| 107 | $spam = false; |
| 108 | $query_string = ''; |
| 109 | |
| 110 | foreach ( $comment as $key => $data ) |
| 111 | $query_string .= $key . '=' . urlencode( stripslashes( (string) $data ) ) . '&'; |
| 112 | |
| 113 | $response = akismet_http_post( $query_string, |
| 114 | $akismet_api_host, '/1.1/comment-check', $akismet_api_port ); |
| 115 | |
| 116 | if ( 'true' == $response[1] ) |
| 117 | $spam = true; |
| 118 | |
| 119 | if ( $contact_form = wpcf7_get_current_contact_form() ) |
| 120 | $contact_form->akismet = array( 'comment' => $comment, 'spam' => $spam ); |
| 121 | |
| 122 | return apply_filters( 'wpcf7_akismet_comment_check', $spam, $comment ); |
| 123 | } |
| 124 | |
| 125 | ?> |