Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect users locale and custom date- and time-formats in sent emails #922

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading