Skip to content

Commit

Permalink
Merge pull request #922 from carstingaxion/fix/fragment-of-752-send-e…
Browse files Browse the repository at this point in the history
…mails-in-users-locale

Respect users locale and custom date- and time-formats in sent emails
  • Loading branch information
mauteri authored Sep 26, 2024
2 parents 00db293 + 2d2d8c6 commit 0bab53e
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions includes/core/classes/class-event-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,28 +250,43 @@ public function send_emails( int $post_id, array $send, string $message ): bool
return false;
}

$members = $this->get_members( $send, $post_id );
/* translators: %s: event title. */
$subject = sprintf( __( '📅 %s', 'gatherpress' ), get_the_title( $post_id ) );
$body = Utility::render_template(
sprintf( '%s/includes/templates/admin/emails/event-email.php', GATHERPRESS_CORE_PATH ),
array(
'event_id' => $post_id,
'message' => $message,
),
);
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
$subject = stripslashes_deep( html_entity_decode( $subject, ENT_QUOTES, 'UTF-8' ) );
// Keep the currently logged-in user.
$current_user = wp_get_current_user();

$members = $this->get_members( $send, $post_id );
foreach ( $members as $member ) {
if ( '0' === get_user_meta( $member->ID, 'gatherpress_event_updates_opt_in', true ) ) {
continue;
}

if ( $member->user_email ) {
$to = $member->user_email;
$to = $member->user_email;
$switched_locale = switch_to_user_locale( $member->ID );

// Set the current user to the actual member to mail to,
// to make sure the GatherPress filters for date- and time- format, as well as the users timezone,
// are recognized by the functions inside render_template().
wp_set_current_user( $member->ID );

/* translators: %s: event title. */
$subject = sprintf( _x( '📅 %s', 'Email subject for event updates', 'gatherpress' ), get_the_title( $post_id ) );
$body = Utility::render_template(
sprintf( '%s/includes/templates/admin/emails/event-email.php', GATHERPRESS_CORE_PATH ),
array(
'event_id' => $post_id,
'message' => $message,
),
);
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
$subject = stripslashes_deep( html_entity_decode( $subject, ENT_QUOTES, 'UTF-8' ) );

// Reset the current user to the editor sending the email.
wp_set_current_user( $current_user->ID );

wp_mail( $to, $subject, $body, $headers );

if ( $switched_locale ) {
restore_previous_locale();
}
}
}

Expand Down

0 comments on commit 0bab53e

Please sign in to comment.