Akismet.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) || ! class_exists( 'NF_Abstracts_Action' ) ) {
  3. exit;
  4. }
  5. /**
  6. * Class NF_Actions_Akismet
  7. */
  8. final class NF_Actions_Akismet extends NF_Abstracts_Action {
  9. /**
  10. * @var string
  11. */
  12. protected $_name = 'akismet';
  13. /**
  14. * @var array
  15. */
  16. protected $_tags = array( 'spam', 'filtering', 'akismet' );
  17. /**
  18. * @var string
  19. */
  20. protected $_timing = 'normal';
  21. /**
  22. * @var int
  23. */
  24. protected $_priority = '10';
  25. /**
  26. * Constructor
  27. */
  28. public function __construct() {
  29. parent::__construct();
  30. $this->_nicename = __( 'Akismet Anti-Spam', 'ninja-forms' );
  31. $settings = Ninja_Forms::config( 'ActionAkismetSettings' );
  32. $this->_settings = array_merge( $this->_settings, $settings );
  33. add_filter( 'ninja_forms_action_type_settings', array( $this, 'maybe_remove_action' ) );
  34. }
  35. /**
  36. * Remove the action registration if Akismet functions not available.
  37. *
  38. * @param array $action_type_settings
  39. *
  40. * @return array
  41. */
  42. public function maybe_remove_action( $action_type_settings ) {
  43. if ( ! $this->akismet_available() ) {
  44. unset( $action_type_settings[ $this->_name ] );
  45. }
  46. return $action_type_settings;
  47. }
  48. /**
  49. * Is Akismet installed and connected with a valid key?
  50. *
  51. * @return bool
  52. */
  53. protected function akismet_available() {
  54. if ( ! is_callable( array( 'Akismet', 'get_api_key' ) ) ) {
  55. // Not installed and activated
  56. return false;
  57. }
  58. $akismet_key = Akismet::get_api_key();
  59. if ( empty( $akismet_key ) ) {
  60. // No key entered
  61. return false;
  62. }
  63. return 'valid' === Akismet::verify_key( $akismet_key );
  64. }
  65. /**
  66. * Process the action
  67. *
  68. * @param array $action_settings
  69. * @param int $form_id
  70. * @param array $data
  71. *
  72. * @return array
  73. */
  74. public function process( $action_settings, $form_id, $data ) {
  75. if ( ! $this->akismet_available() ) {
  76. return $data;
  77. }
  78. if ( $this->is_submission_spam( $action_settings['name'], $action_settings['email'], $action_settings['url'], $action_settings['message'] ) ) {
  79. $data['errors']['form']['spam'] = __( 'There was an error trying to send your message. Please try again later', 'ninja-forms' );
  80. }
  81. return $data;
  82. }
  83. /**
  84. * Verify submission
  85. *
  86. * @param $name
  87. * @param $email
  88. * @param $url
  89. * @param $message
  90. *
  91. * @return bool
  92. */
  93. protected function is_submission_spam( $name, $email, $url, $message ) {
  94. $body_request = array(
  95. 'blog' => get_option( 'home' ),
  96. 'blog_lang' => get_locale(),
  97. 'permalink' => get_permalink(),
  98. 'comment_type' => 'contact-form',
  99. 'comment_author' => $name,
  100. 'comment_author_email' => $email,
  101. 'comment_author_url' => $url,
  102. 'comment_content' => $message,
  103. 'user_agent' => ( isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : null ),
  104. );
  105. if ( method_exists( 'Akismet', 'http_post' ) ) {
  106. $body_request['user_ip'] = Akismet::get_ip_address();
  107. $response = Akismet::http_post( build_query( $body_request ), 'comment-check' );
  108. } else {
  109. global $akismet_api_host, $akismet_api_port;
  110. $body_request['user_ip'] = ( isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null );
  111. $response = akismet_http_post( build_query( $body_request ), $akismet_api_host, '/1.1/comment-check', $akismet_api_port );
  112. }
  113. if ( ! empty( $response ) && isset( $response[1] ) && 'true' == trim( $response[1] ) ) {
  114. // Spam!
  115. return true;
  116. }
  117. return false;
  118. }
  119. }