@@ -88,6 +88,14 @@ function pmpro_get_spam_activity( $ip = null ) {
88
88
* @return bool True if the tracking of activity was successful, or false if IP could not be determined.
89
89
*/
90
90
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.
91
99
if ( empty ( $ ip ) ) {
92
100
$ ip = pmpro_get_ip ();
93
101
}
@@ -189,4 +197,31 @@ function pmpro_disable_checkout_for_spammers( $required_fields ) {
189
197
190
198
return $ required_fields ;
191
199
}
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