BP_Email_Recipient

Represents a recipient that an email will be sent to.

Defined (1)

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

/bp-core/classes/class-bp-email-recipient.php  
  1. class BP_Email_Recipient { 
  2.  
  3. /** 
  4. * Recipient's email address. 
  5. * @since 2.5.0 
  6. * @var string 
  7. */ 
  8. protected $address = ''; 
  9.  
  10. /** 
  11. * Recipient's name. 
  12. * @since 2.5.0 
  13. * @var string 
  14. */ 
  15. protected $name = ''; 
  16.  
  17. /** 
  18. * Optional. A `WP_User` object relating to this recipient. 
  19. * @since 2.5.0 
  20. * @var WP_User 
  21. */ 
  22. protected $user_object = null; 
  23.  
  24. /** 
  25. * Constructor. 
  26. * @since 2.5.0 
  27. * @param string|array|int|WP_User $email_or_user Either a email address, user ID, WP_User object,  
  28. * or an array containing any combination of the above. 
  29. * @param string $name Optional. If $email_or_user is a string, this is the recipient's name. 
  30. */ 
  31. public function __construct( $email_or_user, $name = '' ) { 
  32. $name = sanitize_text_field( $name ); 
  33.  
  34. // User ID, email address or WP_User object. 
  35. if ( is_int( $email_or_user ) || ( is_string( $email_or_user ) && is_email( $email_or_user ) ) || is_object( $email_or_user ) ) { 
  36. // We already have a WP user. 
  37. if ( is_object( $email_or_user ) ) { 
  38. $this->user_object = $email_or_user; 
  39.  
  40. // Query for WP user by user ID. 
  41. } elseif ( is_int( $email_or_user ) ) { 
  42. $this->user_object = get_user_by( 'id', $email_or_user ); 
  43.  
  44. // Set email address. 
  45. if ( empty( $this->user_object ) && is_email( $email_or_user ) ) { 
  46. $address = $email_or_user; 
  47.  
  48. // Array or miscellaneous string. 
  49. } else { 
  50. if ( ! is_array( $email_or_user ) ) { 
  51. $email_or_user = array( $email_or_user => $name ); 
  52.  
  53. // Handle numeric arrays. 
  54. if ( is_int( key( $email_or_user ) ) ) { 
  55. $address = current( $email_or_user ); 
  56. } else { 
  57. $address = key( $email_or_user ); 
  58. $name = current( $email_or_user ); 
  59.  
  60. // Set address if we have one. 
  61. if ( ! empty( $address ) ) { 
  62. $this->address = sanitize_email( $address ); 
  63.  
  64. // Still no user object; try to query user by email address. 
  65. if ( empty( $this->user_object ) ) { 
  66. $this->get_user( 'search-email' ); 
  67.  
  68. // We have a user object; so set address and name from DB. 
  69. if ( $this->user_object ) { 
  70. // This is escaped with esc_html in bp_core_get_user_displayname() 
  71. $wp_name = wp_specialchars_decode( bp_core_get_user_displayname( $this->user_object->ID ), ENT_QUOTES ); 
  72.  
  73. $this->address = $this->user_object->user_email; 
  74. $this->name = sanitize_text_field( $wp_name ); 
  75.  
  76.  
  77. // Custom name override. 
  78. if ( $name ) { 
  79. $this->name = $name; 
  80.  
  81. /** 
  82. * Fires inside __construct() method for BP_Email_Recipient class. 
  83. * @since 2.5.0 
  84. * @param string|array|int|WP_User $email_or_user Either a email address, user ID, WP_User object,  
  85. * or an array containing any combination of the above. 
  86. * @param string $name If $email_or_user is a string, this is the recipient's name. 
  87. * @param BP_Email_Recipient $this Current instance of the email type class. 
  88. */ 
  89. do_action( 'bp_email_recipient', $email_or_user, $name, $this ); 
  90.  
  91. /** 
  92. * Get recipient's address. 
  93. * @since 2.5.0 
  94. * @return string 
  95. */ 
  96. public function get_address() { 
  97.  
  98. /** 
  99. * Filters the recipient's address before it's returned. 
  100. * @since 2.5.0 
  101. * @param string $address Recipient's address. 
  102. * @param BP_Email $recipient $this Current instance of the email recipient class. 
  103. */ 
  104. return apply_filters( 'bp_email_recipient_get_address', $this->address, $this ); 
  105.  
  106. /** 
  107. * Get recipient's name. 
  108. * @since 2.5.0 
  109. * @return string 
  110. */ 
  111. public function get_name() { 
  112.  
  113. /** 
  114. * Filters the recipient's name before it's returned. 
  115. * @since 2.5.0 
  116. * @param string $name Recipient's name. 
  117. * @param BP_Email $recipient $this Current instance of the email recipient class. 
  118. */ 
  119. return apply_filters( 'bp_email_recipient_get_name', $this->name, $this ); 
  120.  
  121. /** 
  122. * Get WP_User object for this recipient. 
  123. * @since 2.5.0 
  124. * @param string $transform Optional. How to transform the return value. 
  125. * Accepts 'raw' (default) or 'search-email'. 
  126. * @return WP_User|null WP_User object, or null if not set. 
  127. */ 
  128. public function get_user( $transform = 'raw' ) { 
  129.  
  130. // If transform "search-email", find the WP_User if not already set. 
  131. if ( $transform === 'search-email' && ! $this->user_object && $this->address ) { 
  132. $this->user_object = get_user_by( 'email', $this->address ); 
  133.  
  134. /** 
  135. * Filters the WP_User object for this recipient before it's returned. 
  136. * @since 2.5.0 
  137. * @param WP_User $name WP_User object for this recipient, or null if not set. 
  138. * @param string $transform Optional. How the return value was transformed. 
  139. * Accepts 'raw' (default) or 'search-email'. 
  140. * @param BP_Email $recipient $this Current instance of the email recipient class. 
  141. */ 
  142. return apply_filters( 'bp_email_recipient_get_user', $this->user_object, $transform, $this );