From 5620b6eb379b6c5b75dd4c3a5c3a6e9d6eee9df4 Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Thu, 1 Aug 2024 13:49:19 +0200 Subject: [PATCH 1/4] Load users locale before sending emails & reset afterwards --- .../core/classes/class-event-rest-api.php | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/includes/core/classes/class-event-rest-api.php b/includes/core/classes/class-event-rest-api.php index c89732e26..eca26bfb6 100644 --- a/includes/core/classes/class-event-rest-api.php +++ b/includes/core/classes/class-event-rest-api.php @@ -251,18 +251,6 @@ public function send_emails( int $post_id, array $send, string $message ): bool } $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' ) ); - foreach ( $members as $member ) { if ( '0' === get_user_meta( $member->ID, 'gatherpress_event_updates_opt_in', true ) ) { continue; @@ -270,8 +258,25 @@ public function send_emails( int $post_id, array $send, string $message ): bool if ( $member->user_email ) { $to = $member->user_email; + $switched_locale = switch_to_user_locale( $member->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' ) ); wp_mail( $to, $subject, $body, $headers ); + + if ( $switched_locale ) { + restore_previous_locale(); + } } } From 4a9629295fca756a6549516a2de2127367f85c4a Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Thu, 1 Aug 2024 14:14:13 +0200 Subject: [PATCH 2/4] 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(). --- includes/core/classes/class-event-rest-api.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/includes/core/classes/class-event-rest-api.php b/includes/core/classes/class-event-rest-api.php index eca26bfb6..c5469d27f 100644 --- a/includes/core/classes/class-event-rest-api.php +++ b/includes/core/classes/class-event-rest-api.php @@ -250,16 +250,23 @@ public function send_emails( int $post_id, array $send, string $message ): bool return false; } + // 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; $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( __( '📅 %s', 'gatherpress' ), get_the_title( $post_id ) ); $body = Utility::render_template( @@ -272,6 +279,9 @@ public function send_emails( int $post_id, array $send, string $message ): bool $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 ) { From a1250e25d1a3d7a5bcdedeb843a4136da7e955eb Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Thu, 26 Sep 2024 22:06:11 +0200 Subject: [PATCH 3/4] Fix for CS --- includes/core/classes/class-event-rest-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/core/classes/class-event-rest-api.php b/includes/core/classes/class-event-rest-api.php index c5469d27f..67caccba5 100644 --- a/includes/core/classes/class-event-rest-api.php +++ b/includes/core/classes/class-event-rest-api.php @@ -259,7 +259,7 @@ public function send_emails( int $post_id, array $send, string $message ): bool 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, From 2d2d8c6e15ba8d213d6839b836f19c458d72ae54 Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Thu, 26 Sep 2024 22:10:25 +0200 Subject: [PATCH 4/4] Add context to email subject string --- includes/core/classes/class-event-rest-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/core/classes/class-event-rest-api.php b/includes/core/classes/class-event-rest-api.php index 67caccba5..54c49adb1 100644 --- a/includes/core/classes/class-event-rest-api.php +++ b/includes/core/classes/class-event-rest-api.php @@ -268,7 +268,7 @@ public function send_emails( int $post_id, array $send, string $message ): bool wp_set_current_user( $member->ID ); /* translators: %s: event title. */ - $subject = sprintf( __( '📅 %s', 'gatherpress' ), get_the_title( $post_id ) ); + $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(