eventsmanager

em_booking_validate with Akismet

May 21st, 2026
3,396
1
Never
3
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 1 0
  1. function filter_em_bookings_with_akismet( $result, $EM_Booking ) {
  2. // If the booking has already failed previous validation checks, skip Akismet to save API calls
  3. if ( ! $result ) {
  4. return $result;
  5. }
  6.  
  7. // Double-check that Akismet is active on the site
  8. if ( ! is_plugin_active( 'akismet/akismet.php' ) && ! class_exists( 'Akismet' ) ) {
  9. return $result;
  10. }
  11.  
  12. // 1. Gather the attendee's submission data from the booking object
  13. $person_name = $EM_Booking->get_person()->display_name;
  14. $person_email = $EM_Booking->get_person()->user_email;
  15.  
  16. // Fallback to POST data if the guest user object isn't fully hydrated yet
  17. if ( empty( $person_name ) ) {
  18. $person_name = sanitize_text_field( $_POST['user_name'] ?? ($_POST['first_name'] ?? '') . ' ' . ($_POST['last_name'] ?? '') );
  19. }
  20. if ( empty( $person_email ) ) {
  21. $person_email = sanitize_email( $_POST['user_email'] ?? '' );
  22. }
  23.  
  24. // Compile custom booking form text notes/comments to pass as content
  25. $booking_notes = sanitize_textarea_field( $_POST['booking_comment'] ?? '' );
  26.  
  27. // 2. Format the payload for Akismet's API
  28. $query_string = array(
  29. 'blog' => get_option( 'home' ),
  30. 'user_ip' => $_SERVER['REMOTE_ADDR'] ?? '',
  31. 'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
  32. 'referrer' => $_SERVER['HTTP_REFERER'] ?? '',
  33. 'comment_type' => 'performance-booking', // Identifies the context to Akismet
  34. 'comment_author' => $person_name,
  35. 'comment_author_email' => $person_email,
  36. 'comment_content' => $booking_notes,
  37. );
  38.  
  39. // 3. Request verification from Akismet
  40. $response = Akismet::http_post( http_build_query( $query_string ), 'comment-check' );
  41.  
  42. // 4. Handle Akismet's verdict
  43. if ( isset( $response[1] ) && trim( $response[1] ) === 'true' ) {
  44. // Log it locally if you need to monitor false positives
  45. error_log( "Akismet blocked a booking submission from: " . $person_email );
  46.  
  47. // Add an error message to display on the booking form front-end
  48. $EM_Booking->add_error( __( 'Our automated system flagged this submission as spam. If this is an error, please contact us directly.', 'events-manager' ) );
  49. return false;
  50. }
  51.  
  52. return $result;
  53. }
  54. add_filter( 'em_booking_validate', 'filter_em_bookings_with_akismet', 99, 2 );
Advertisement