PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/icwp-optionshandler-commentsfilter.php

https://github.com/stackgrinder/wp-simple-firewall
PHP | 269 lines | 220 code | 22 blank | 27 comment | 9 complexity | aacaaa992f153c41529d0cfc3ad43f0a MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (c) 2014 iControlWP <support@icontrolwp.com>
  4. * All rights reserved.
  5. *
  6. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  7. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  8. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  9. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  10. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  11. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  12. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  13. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  15. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. */
  17. require_once( dirname(__FILE__).'/icwp-optionshandler-base.php' );
  18. if ( !class_exists('ICWP_OptionsHandler_CommentsFilter') ):
  19. class ICWP_OptionsHandler_CommentsFilter extends ICWP_OptionsHandler_Base_Wpsf {
  20. const StoreName = 'commentsfilter_options';
  21. const DefaultCommentCooldown = 30; //seconds.
  22. const DefaultCommentExpire = 600; //seconds.
  23. public function __construct( $oPluginVo ) {
  24. parent::__construct( $oPluginVo, self::StoreName );
  25. $this->sFeatureName = _wpsf__('Comments Filter');
  26. $this->sFeatureSlug = 'comments_filter';
  27. }
  28. /**
  29. * @return bool|void
  30. */
  31. public function defineOptions() {
  32. $aBase = array(
  33. 'section_title' => sprintf( _wpsf__( 'Enable Plugin Feature: %s' ), _wpsf__('SPAM Comments Protection Filter') ),
  34. 'section_options' => array(
  35. array(
  36. 'enable_comments_filter',
  37. '',
  38. 'N',
  39. 'checkbox',
  40. _wpsf__( 'Enable Comments Filter' ),
  41. _wpsf__( 'Enable (or Disable) The SPAM Comments Protection Filter Feature' ),
  42. sprintf( _wpsf__( 'Checking/Un-Checking this option will completely turn on/off the whole %s feature.' ), _wpsf__('SPAM Comments Protection Filter') ),
  43. '<a href="http://icwp.io/3z" target="_blank">'._wpsf__( 'more info' ).'</a>'
  44. .' | <a href="http://icwp.io/wpsf04" target="_blank">'._wpsf__( 'blog' ).'</a>'
  45. )
  46. )
  47. );
  48. $aHumanSpam = array(
  49. 'section_title' => sprintf( _wpsf__( '%s Comment SPAM Protection Filter' ), _wpsf__('Human') ),
  50. 'section_options' => array(
  51. array(
  52. 'enable_comments_human_spam_filter',
  53. '',
  54. 'N',
  55. 'checkbox',
  56. _wpsf__( 'Human SPAM Filter' ),
  57. _wpsf__( 'Enable (or Disable) The Human SPAM Filter Feature.' ),
  58. _wpsf__( 'Scans the content of WordPress comments for keywords that are indicative of SPAM and marks the comment according to your preferred setting below.' ),
  59. '<a href="http://icwp.io/57" target="_blank">'._wpsf__( 'more info' ).'</a>'
  60. ),
  61. array(
  62. 'enable_comments_human_spam_filter_items',
  63. '',
  64. $this->getHumanSpamFilterItems( true ),
  65. $this->getHumanSpamFilterItems(),
  66. _wpsf__( 'Comment Filter Items' ),
  67. _wpsf__( 'Select The Items To Scan For SPAM' ),
  68. _wpsf__( 'When a user submits a comment, only the selected parts of the comment data will be scanned for SPAM content.' ).' '.sprintf( _wpsf__('Recommended: %s'), _wpsf__('All') ),
  69. '<a href="http://icwp.io/58" target="_blank">'._wpsf__( 'more info' ).'</a>'
  70. ),
  71. array(
  72. 'comments_default_action_human_spam',
  73. '',
  74. 'spam',
  75. $this->getSpamHandlingResponses(),
  76. _wpsf__( 'Default SPAM Action' ),
  77. _wpsf__( 'How To Categorise Comments When Identified To Be SPAM' ),
  78. sprintf( _wpsf__( 'When a comment is detected as being SPAM from %s, the comment will be categorised based on this setting.' ), '<span style"text-decoration:underline;">'._wpsf__('a human commenter').'</span>' ),
  79. '<a href="http://icwp.io/59" target="_blank">'._wpsf__( 'more info' ).'</a>'
  80. )
  81. ),
  82. );
  83. $aGasp = array(
  84. 'section_title' => sprintf( _wpsf__( '%s Comment SPAM Protection Filter' ), _wpsf__('Automatic Bot') ),
  85. 'section_options' => array(
  86. array(
  87. 'enable_comments_gasp_protection',
  88. '',
  89. 'Y',
  90. 'checkbox',
  91. _wpsf__( 'GASP Protection' ),
  92. _wpsf__( 'Add Growmap Anti Spambot Protection to your comments' ),
  93. _wpsf__( 'Taking the lead from the original GASP plugin for WordPress, we have extended it to include advanced spam-bot protection.' ),
  94. '<a href="http://icwp.io/3n" target="_blank">'._wpsf__( 'more info' ).'</a>'
  95. .' | <a href="http://icwp.io/2n" target="_blank">'._wpsf__( 'blog' ).'</a>'
  96. ),
  97. array(
  98. 'comments_default_action_spam_bot',
  99. '',
  100. 'trash',
  101. $this->getSpamHandlingResponses(),
  102. _wpsf__( 'Default SPAM Action' ),
  103. _wpsf__( 'How To Categorise Comments When Identified To Be SPAM' ),
  104. sprintf( _wpsf__( 'When a comment is detected as being SPAM from %s, the comment will be categorised based on this setting.' ), '<span style"text-decoration:underline;">'._wpsf__('an automatic bot').'</span>' ),
  105. '<a href="http://icwp.io/59" target="_blank">'._wpsf__( 'more info' ).'</a>'
  106. ),
  107. array(
  108. 'enable_comments_gasp_protection_for_logged_in',
  109. '',
  110. 'N',
  111. 'checkbox',
  112. _wpsf__( 'Include Logged-In Users' ),
  113. _wpsf__( 'You may also enable GASP for logged in users' ),
  114. _wpsf__( 'Since logged-in users would be expected to be vetted already, this is off by default.' )
  115. ),
  116. array(
  117. 'comments_cooldown_interval',
  118. '',
  119. '30',
  120. 'integer',
  121. _wpsf__( 'Comments Cooldown' ),
  122. _wpsf__( 'Limit posting comments to X seconds after the page has loaded' ),
  123. _wpsf__( "By forcing a comments cooldown period, you restrict a Spambot's ability to post mutliple times to your posts." ),
  124. '<a href="http://icwp.io/3o" target="_blank">'._wpsf__( 'more info' ).'</a>'
  125. ),
  126. array(
  127. 'comments_token_expire_interval',
  128. '',
  129. '600',
  130. 'integer',
  131. _wpsf__( 'Comment Token Expire' ),
  132. _wpsf__( 'A visitor has X seconds within which to post a comment' ),
  133. _wpsf__( "Default: 600 seconds (10 minutes). Each visitor is given a unique 'Token' so they can comment. This restricts spambots, but we need to force these tokens to expire and at the same time not bother the visitors." ),
  134. '<a href="http://icwp.io/3o" target="_blank">'._wpsf__( 'more info' ).'</a>'
  135. )
  136. )
  137. );
  138. $aCustomMessages = array(
  139. 'section_title' => sprintf( _wpsf__( 'Customize Messages Shown To User' ), _wpsf__('Automatic Bot') ),
  140. 'section_options' => array(
  141. array(
  142. 'custom_message_checkbox',
  143. '',
  144. _wpsf__( "I'm not a spammer" ),
  145. 'text',
  146. _wpsf__( 'Custom Checkbox Message' ),
  147. _wpsf__( 'If you want a custom checkbox message, please provide this here' ),
  148. _wpsf__( "You can customise the message beside the checkbox." )
  149. .'<br />'.sprintf( _wpsf__( 'Default Message: %s' ), _wpsf__("Please check the box to confirm you're not a spammer") ),
  150. '<a href="http://icwp.io/3p" target="_blank">'._wpsf__( 'more info' ).'</a>'
  151. ),
  152. array(
  153. 'custom_message_alert',
  154. '',
  155. _wpsf__( "Please check the box to confirm you're not a spammer" ),
  156. 'text',
  157. _wpsf__( 'Custom Alert Message' ),
  158. _wpsf__( 'If you want a custom alert message, please provide this here' ),
  159. _wpsf__( "This alert message is displayed when a visitor attempts to submit a comment without checking the box." )
  160. .'<br />'.sprintf( _wpsf__( 'Default Message: %s' ), _wpsf__("Please check the box to confirm you're not a spammer") ),
  161. '<a href="http://icwp.io/3p" target="_blank">'._wpsf__( 'more info' ).'</a>'
  162. ),
  163. array(
  164. 'custom_message_comment_wait',
  165. '',
  166. _wpsf__( "Please wait %s seconds before posting your comment" ),
  167. 'text',
  168. _wpsf__( 'Custom Wait Message' ),
  169. _wpsf__( 'If you want a custom submit-button wait message, please provide this here.' ),
  170. _wpsf__( "Where you see the '%s' this will be the number of seconds. You must ensure you include 1, and only 1, of these." )
  171. .'<br />'.sprintf( _wpsf__( 'Default Message: %s' ), _wpsf__('Please wait %s seconds before posting your comment') ),
  172. '<a href="http://icwp.io/3p" target="_blank">'._wpsf__( 'more info' ).'</a>'
  173. ),
  174. array(
  175. 'custom_message_comment_reload',
  176. '',
  177. _wpsf__( "Please reload this page to post a comment" ),
  178. 'text',
  179. _wpsf__( 'Custom Reload Message' ),
  180. _wpsf__( 'If you want a custom message when the comment token has expired, please provide this here.' ),
  181. _wpsf__( 'This message is displayed on the submit-button when the comment token is expired' )
  182. .'<br />'.sprintf( _wpsf__( 'Default Message: %s' ), _wpsf__("Please reload this page to post a comment") ),
  183. '<a href="http://icwp.io/3p" target="_blank">'._wpsf__( 'more info' ).'</a>'
  184. )
  185. )
  186. );
  187. $this->m_aOptions = array(
  188. $aBase,
  189. $aHumanSpam,
  190. $aGasp,
  191. $aCustomMessages
  192. );
  193. }
  194. /**
  195. * This is the point where you would want to do any options verification
  196. */
  197. protected function doPrePluginOptionsSave() {
  198. $nCommentCooldown = $this->getOpt( 'comments_cooldown_interval' );
  199. if ( $nCommentCooldown < 0 ) {
  200. $nCommentCooldown = 0;
  201. }
  202. $nCommentTokenExpire = $this->getOpt( 'comments_token_expire_interval' );
  203. if ( $nCommentTokenExpire < 0 ) {
  204. $nCommentTokenExpire = 0;
  205. }
  206. if ( $nCommentTokenExpire != 0 && $nCommentCooldown > $nCommentTokenExpire ) {
  207. $nCommentCooldown = self::DefaultCommentCooldown;
  208. $nCommentTokenExpire = self::DefaultCommentExpire;
  209. }
  210. $this->setOpt( 'comments_cooldown_interval', $nCommentCooldown );
  211. $this->setOpt( 'comments_token_expire_interval', $nCommentTokenExpire );
  212. $aCommentsFilters = $this->getOpt( 'enable_comments_human_spam_filter_items' );
  213. if ( empty($aCommentsFilters) || !is_array( $aCommentsFilters ) ) {
  214. $this->setOpt( 'enable_comments_human_spam_filter_items', $this->getHumanSpamFilterItems( true ) );
  215. }
  216. }
  217. /**
  218. * @return array
  219. */
  220. protected function getSpamHandlingResponses() {
  221. return array( 'select',
  222. array( 0, _wpsf__( 'Mark As Pending Moderation' ) ),
  223. array( 'spam', _wpsf__( 'Mark As SPAM' ) ),
  224. array( 'trash', _wpsf__( 'Move To Trash' ) ),
  225. array( 'reject', _wpsf__( 'Reject And Redirect' ) )
  226. );
  227. }
  228. /**
  229. *
  230. */
  231. protected function getHumanSpamFilterItems( $fAsDefaults = false ) {
  232. $aFilterItems = array( 'type' => 'multiple_select',
  233. 'author_name' => _wpsf__('Author Name'),
  234. 'author_email' => _wpsf__('Author Email'),
  235. 'comment_content' => _wpsf__('Comment Content'),
  236. 'url' => _wpsf__('URL'),
  237. 'ip_address' => _wpsf__('IP Address'),
  238. 'user_agent' => _wpsf__('Browser User Agent')
  239. );
  240. if ( $fAsDefaults ) {
  241. unset($aFilterItems['type']);
  242. return array_keys($aFilterItems);
  243. }
  244. return $aFilterItems;
  245. }
  246. }
  247. endif;