Skip to content

Commit

Permalink
Tweak wp_mail only if email matches editor user, admin user, or site …
Browse files Browse the repository at this point in the history
…admin email
  • Loading branch information
crodriguezbrito committed Dec 13, 2024
1 parent fd11341 commit 4f19bb9
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions includes/Listeners/WPMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,40 @@ public function register_hooks() {
* @return void
*/
public function mail_succeeded( $mail_data ) {
$this->push(
'wp_mail',
array(
'label_key' => 'subject',
'subject' => $mail_data['subject'],
)
);

$send_event = false;
$site_admin_email = get_option( 'admin_email' );

$recipients = $mail_data['to'];

if ( ! is_array( $recipients ) ) {
$recipients = array( $recipients );
}

foreach ( $recipients as $email ) {
$email = trim( $email );

if ( $email === $site_admin_email ) {
$send_event = true;
break;
}

$user = get_user_by( 'email', $email );

if ( $user && ( in_array( 'administrator', $user->roles, true ) || in_array( 'editor', $user->roles, true ) ) ) {
$send_event = true;
break;
}
}

if ( $send_event ) {
$this->push(
'wp_mail',
array(
'label_key' => 'subject',
'subject' => $mail_data['subject'],
)
);
}
}
}

0 comments on commit 4f19bb9

Please sign in to comment.