/wp-content/plugins/contact-form-7/modules/akismet.php

https://github.com/konscript/schroderpartners · PHP · 114 lines · 76 code · 34 blank · 4 comment · 29 complexity · e2c9792ac4b1d2e964b0ccea8ca0cdb8 MD5 · raw file

  1. <?php
  2. /**
  3. ** Akismet Filter
  4. **/
  5. add_filter( 'wpcf7_spam', 'wpcf7_akismet' );
  6. function wpcf7_akismet( $spam ) {
  7. global $akismet_api_host, $akismet_api_port;
  8. if ( ! function_exists( 'akismet_get_key' ) || ! akismet_get_key() )
  9. return false;
  10. $akismet_ready = false;
  11. $author = $author_email = $author_url = $content = '';
  12. $fes = wpcf7_scan_shortcode();
  13. foreach ( $fes as $fe ) {
  14. if ( ! isset( $fe['name'] ) || ! is_array( $fe['options'] ) )
  15. continue;
  16. if ( preg_grep( '%^akismet:author$%', $fe['options'] ) ) {
  17. $author .= ' ' . $_POST[$fe['name']];
  18. $author = trim( $author );
  19. $akismet_ready = true;
  20. }
  21. if ( preg_grep( '%^akismet:author_email$%', $fe['options'] ) && '' == $author_email ) {
  22. $author_email = trim( $_POST[$fe['name']] );
  23. $akismet_ready = true;
  24. }
  25. if ( preg_grep( '%^akismet:author_url$%', $fe['options'] ) && '' == $author_url ) {
  26. $author_url = trim( $_POST[$fe['name']] );
  27. $akismet_ready = true;
  28. }
  29. if ( '' != $content )
  30. $content .= "\n\n";
  31. $content .= $_POST[$fe['name']];
  32. }
  33. if ( ! $akismet_ready )
  34. return false;
  35. $c['blog'] = get_option( 'home' );
  36. $c['blog_lang'] = get_locale();
  37. $c['blog_charset'] = get_option( 'blog_charset' );
  38. $c['user_ip'] = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] );
  39. $c['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
  40. $c['referrer'] = $_SERVER['HTTP_REFERER'];
  41. $c['comment_type'] = 'contactform7';
  42. if ( $permalink = get_permalink() )
  43. $c['permalink'] = $permalink;
  44. if ( '' != $author )
  45. $c['comment_author'] = $author;
  46. if ( '' != $author_email )
  47. $c['comment_author_email'] = $author_email;
  48. if ( '' != $author_url )
  49. $c['comment_author_url'] = $author_url;
  50. if ( '' != $content )
  51. $c['comment_content'] = $content;
  52. $ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' );
  53. foreach ( $_SERVER as $key => $value ) {
  54. if ( ! in_array( $key, (array) $ignore ) )
  55. $c["$key"] = $value;
  56. }
  57. $query_string = '';
  58. foreach ( $c as $key => $data )
  59. $query_string .= $key . '=' . urlencode( stripslashes( (string) $data ) ) . '&';
  60. $response = akismet_http_post( $query_string,
  61. $akismet_api_host, '/1.1/comment-check', $akismet_api_port );
  62. if ( 'true' == $response[1] )
  63. $spam = true;
  64. return $spam;
  65. }
  66. /* Messages */
  67. add_filter( 'wpcf7_messages', 'wpcf7_akismet_messages' );
  68. function wpcf7_akismet_messages( $messages ) {
  69. return array_merge( $messages, array( 'akismet_says_spam' => array(
  70. 'description' => __( "Akismet judged the sending activity as spamming", 'wpcf7' ),
  71. 'default' => __( 'Failed to send your message. Please try later or contact the administrator by another method.', 'wpcf7' )
  72. ) ) );
  73. }
  74. add_filter( 'wpcf7_display_message', 'wpcf7_akismet_display_message', 10, 2 );
  75. function wpcf7_akismet_display_message( $message, $status ) {
  76. if ( 'spam' == $status && empty( $message ) )
  77. $message = wpcf7_get_message( 'akismet_says_spam' );
  78. return $message;
  79. }
  80. ?>