Last active
April 28, 2016 04:18
-
-
Save elhardoum/0be27abbe405b808745670fef9b4f632 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function se_bbpm_add_spam_check() { | |
/** | |
* add spam check for users with bbp_spectator and bbp_participant roles | |
* Add more roles in the $targetRoles line or replace existing ones to match | |
* your targeted users | |
*/ | |
$targetRoles = array( 'bbp_participant', 'bbp_spectator', 'administrator'/*debugging*/ ); | |
$current_user = wp_get_current_user(); | |
foreach( $targetRoles as $role ) { | |
if( in_array( $role, $current_user->roles ) ) | |
return true; | |
} | |
return false; | |
} | |
add_action('bbpm_conversation_form_additional_fields', function() { | |
/** | |
* Follows is just an example on how to implement a simple captcha, | |
* You can set the answer in a cookie or database option and fetch | |
* it with a unique key, or store it somewhere else, use honeypot and | |
* spam hidden traps, reCaptcha, or simply a captcha image.. | |
*/ | |
if( ! se_bbpm_add_spam_check() ) | |
return; // not enabled for this user | |
$rand1 = rand(5,20); // generate random number | |
$rand2 = rand(1,9); // generate random number | |
$operations = array( | |
'+' => 'plus', | |
'-' => 'minus' | |
// add more as you wish, be careful with floats while *|/ | |
); | |
$operationRand = array_rand($operations); // get a random operation from operations list | |
?> | |
<p> | |
<label> | |
<strong style="display: inline;">Spam test:</strong> What's <?php echo $rand1 . ' ' . $operations[$operationRand] . ' ' . $rand2; ?>? <br/> | |
<input type="text" name="capCheck[value]" /> | |
<input type="hidden" name="capCheck[operation]" value="<?php echo $operations[$operationRand]; ?>" /> | |
<input type="hidden" name="capCheck[a]" value="<?php echo $rand1; ?>" /> | |
<input type="hidden" name="capCheck[b]" value="<?php echo $rand2; ?>" /> | |
</label> | |
</p> | |
<?php | |
}); | |
add_filter('bbpm_bail_sending_message', function( $bail ) { | |
echo '11'; | |
if( ! se_bbpm_add_spam_check() ) | |
return $bail; // not for this user | |
if( ! isset( $_POST['capCheck'] ) ) { | |
return true; // nothing set | |
} | |
if( ! is_array( $_POST['capCheck'] ) ) | |
return true; // no data is set | |
$operations = array( | |
'+' => 'plus', | |
'-' => 'minus' | |
// add more as you wish | |
); | |
$op = $_POST['capCheck']['operation']; // which operation used | |
$a = (int) $_POST['capCheck']['a']; // element a | |
$b = (int) $_POST['capCheck']['b']; // element b | |
$ans = false; | |
if( "minus" == $op ) { $ans = abs( $a - $b ); } | |
else if( "plus" == $op ) { $ans = abs( $a + $b ); } | |
// add more .. | |
else { return true; } | |
if( $ans == (int) $_POST['capCheck']['value'] ) { // drop (int) when working with non-integer strings | |
return false; // continue sending message | |
} else { | |
return true; | |
} | |
return $bail; // can be true|false | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment