Skip to content

Commit 856c837

Browse files
authored
Merge pull request #3208 from dparker1005/track-discount-code-spam-v3
Tracking spam when using an invalid discount code
2 parents b210fa9 + cbac151 commit 856c837

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

includes/spam.php

+36-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ function pmpro_get_spam_activity( $ip = null ) {
8888
* @return bool True if the tracking of activity was successful, or false if IP could not be determined.
8989
*/
9090
function pmpro_track_spam_activity( $ip = null ) {
91+
// Make sure that we only track spam a maximum of once per page load.
92+
static $already_tracked = false;
93+
if ( $already_tracked ) {
94+
return false;
95+
}
96+
$already_tracked = true;
97+
98+
// Get the IP address if it's not provided.
9199
if ( empty( $ip ) ) {
92100
$ip = pmpro_get_ip();
93101
}
@@ -189,4 +197,31 @@ function pmpro_disable_checkout_for_spammers( $required_fields ) {
189197

190198
return $required_fields;
191199
}
192-
add_filter( 'pmpro_required_billing_fields', 'pmpro_disable_checkout_for_spammers' );
200+
add_filter( 'pmpro_required_billing_fields', 'pmpro_disable_checkout_for_spammers' );
201+
202+
/**
203+
* Track spam when trying to apply discount codes.
204+
*
205+
* @param bool $okay true if code check is okay or false if there was an error.
206+
* @param object $dbcode Object containing code data from the database row.
207+
*/
208+
function pmpro_check_discount_code_spam_check( $okay, $dbcode ) {
209+
// Bail if Spam Protection is disabled.
210+
$spamprotection = get_option( 'pmpro_spamprotection' );
211+
if ( empty( $spamprotection ) ) {
212+
return $okay;
213+
}
214+
215+
// If we already know that the visitor is a spammer, we don't need to check again.
216+
if ( pmpro_is_spammer() ) {
217+
// Returning a string is considered returning an error message.
218+
return __( 'Suspicious activity detected. Try again in a few minutes.', 'paid-memberships-pro' );
219+
}
220+
221+
// If the discount code is not a valid code on the site, track the activity.
222+
if ( empty( $dbcode->id ) ) {
223+
pmpro_track_spam_activity();
224+
}
225+
return $okay;
226+
}
227+
add_filter( 'pmpro_check_discount_code', 'pmpro_check_discount_code_spam_check', 10, 2 );

0 commit comments

Comments
 (0)