bp_email_unsubscribe_handler

Handles unsubscribing user from notification emails.

Description

bp_email_unsubscribe_handler(); 

Usage

  1. if ( !function_exists( 'bp_email_unsubscribe_handler' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bp_email_unsubscribe_handler(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-functions.php  
  1. function bp_email_unsubscribe_handler() { 
  2. $raw_email_type = ! empty( $_GET['nt'] ) ? $_GET['nt'] : ''; 
  3. $raw_hash = ! empty( $_GET['nh'] ) ? $_GET['nh'] : ''; 
  4. $raw_user_id = ! empty( $_GET['uid'] ) ? absint( $_GET['uid'] ) : 0; 
  5. $new_hash = hash_hmac( 'sha1', "{$raw_email_type}:{$raw_user_id}", bp_email_get_salt() ); 
  6.  
  7. // Check required values. 
  8. if ( ! $raw_user_id || ! $raw_email_type || ! $raw_hash || ! array_key_exists( $raw_email_type, $emails ) ) { 
  9. $redirect_to = site_url( 'wp-login.php' ); 
  10. $result_msg = __( 'Something has gone wrong.', buddypress ); 
  11. $unsub_msg = __( 'Please log in and go to your settings to unsubscribe from notification emails.', buddypress ); 
  12.  
  13. // Check valid hash. 
  14. } elseif ( ! hash_equals( $new_hash, $raw_hash ) ) { 
  15. $redirect_to = site_url( 'wp-login.php' ); 
  16. $result_msg = __( 'Something has gone wrong.', buddypress ); 
  17. $unsub_msg = __( 'Please log in and go to your settings to unsubscribe from notification emails.', buddypress ); 
  18.  
  19. // Don't let authenticated users unsubscribe other users' email notifications. 
  20. } elseif ( is_user_logged_in() && get_current_user_id() !== $raw_user_id ) { 
  21. $result_msg = __( 'Something has gone wrong.', buddypress ); 
  22. $unsub_msg = __( 'Please go to your notifications settings to unsubscribe from emails.', buddypress ); 
  23.  
  24. if ( bp_is_active( 'settings' ) ) { 
  25. $redirect_to = sprintf( 
  26. '%s%s/notifications/',  
  27. bp_core_get_user_domain( get_current_user_id() ),  
  28. ); 
  29. } else { 
  30. $redirect_to = bp_core_get_user_domain( get_current_user_id() ); 
  31.  
  32. } else { 
  33. if ( bp_is_active( 'settings' ) ) { 
  34. $redirect_to = sprintf( 
  35. '%s%s/notifications/',  
  36. bp_core_get_user_domain( $raw_user_id ),  
  37. ); 
  38. } else { 
  39. $redirect_to = bp_core_get_user_domain( $raw_user_id ); 
  40.  
  41. // Unsubscribe. 
  42. $meta_key = $emails[ $raw_email_type ]['unsubscribe']['meta_key']; 
  43. bp_update_user_meta( $raw_user_id, $meta_key, 'no' ); 
  44.  
  45. $result_msg = $emails[ $raw_email_type ]['unsubscribe']['message']; 
  46. $unsub_msg = __( 'You can change this or any other email notification preferences in your email settings.', buddypress ); 
  47.  
  48. $message = sprintf( 
  49. '%1$s <a href="%2$s">%3$s</a>',  
  50. $result_msg,  
  51. esc_url( $redirect_to ),  
  52. esc_html( $unsub_msg ) 
  53. ); 
  54.  
  55. bp_core_add_message( $message ); 
  56.  
  57. exit;