Skip to content
Open
Show file tree
Hide file tree
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
32 changes: 28 additions & 4 deletions includes/Core/Email_Reporting/Email_Log_Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,30 @@ public function process( $post_id, $frequency, array $shared_payloads = array()
return;
}

$sections = $this->build_sections_for_log( $email_log, $user, $raw_payload );
$user_locale = get_user_locale( $user );
$switched_locale = switch_to_locale( $user_locale );

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes specced in the IB didn't fix the issue - translations also occur via the methods Email_Template_Formatter::build_template_payload() and Email_Report_Sender::send().

Moving locale switching and restoration to Email_Log_Processor::process() is a correct fix for the issue.


$this->build_and_send_email( $post_id, $email_log, $user, $raw_payload, $frequency, $date_range );

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

/**
* Builds and sends an email.
*
* @since n.e.x.t.
*
* @param int $post_id Email log post ID.
* @param WP_Post $email_log Email log post.
* @param WP_User $user User receiving the report.
* @param array $raw_payload Raw payload.
* @param string $frequency Frequency slug.
* @param array $date_range Date range.
*/
private function build_and_send_email( $post_id, $email_log, $user, $raw_payload, $frequency, $date_range ) {
$sections = $this->build_sections_for_log( $email_log, $raw_payload );
if ( is_wp_error( $sections ) ) {
$this->mark_failed( $post_id, $sections );
return;
Expand All @@ -140,6 +163,7 @@ public function process( $post_id, $frequency, array $shared_payloads = array()
$template_data = isset( $template_payload['template_data'] ) ? $template_payload['template_data'] : array();

$send_result = $this->report_sender->send( $user, $sections_payload, $template_data );

if ( is_wp_error( $send_result ) ) {
$this->mark_failed( $post_id, $send_result );
return;
Expand Down Expand Up @@ -257,14 +281,14 @@ private function get_date_range_for_log( WP_Post $email_log ) {
* Builds sections for a log payload.
*
* @since 1.170.0
* @since n.e.x.t Removed $user parameter, locale switching is now handled by this class.
*
* @param WP_Post $email_log Email log post.
* @param WP_User $user User receiving the report.
* @param array $raw_payload Raw payload.
* @return array|WP_Error Sections array or WP_Error.
*/
private function build_sections_for_log( WP_Post $email_log, WP_User $user, $raw_payload ) {
$sections = $this->template_formatter->build_sections( $raw_payload, $email_log, $user );
private function build_sections_for_log( WP_Post $email_log, $raw_payload ) {
$sections = $this->template_formatter->build_sections( $raw_payload, $email_log );

if ( is_wp_error( $sections ) ) {
return $sections;
Expand Down
68 changes: 29 additions & 39 deletions includes/Core/Email_Reporting/Email_Report_Section_Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,65 +110,55 @@ public function __construct( Context $context, ?Email_Report_Payload_Processor $
* Build one or more section parts from raw payloads for a module.
*
* @since 1.167.0
* @since n.e.x.t Removed $user_locale parameter, locale switching is now handled by the Email_Log_Processor.
*
* @param string $module_slug Module slug (e.g. analytics-4).
* @param array $raw_sections_payloads Raw reports payloads.
* @param string $user_locale User locale (e.g. en_US).
* @param \WP_Post $email_log Optional. Email log post instance containing date metadata.
* @return Email_Report_Data_Section_Part[] Section parts for the provided module.
* @throws \Exception If an error occurs while building sections.
*/
public function build_sections( $module_slug, $raw_sections_payloads, $user_locale, $email_log = null ) {
public function build_sections( $module_slug, $raw_sections_payloads, $email_log = null ) {
if ( is_object( $raw_sections_payloads ) ) {
$raw_sections_payloads = (array) $raw_sections_payloads;
}

$sections = array();
$switched_locale = switch_to_locale( $user_locale );
$log_date_range = Email_Log::get_date_range_from_log( $email_log );
$this->current_period_length = $this->calculate_period_length_from_date_range( $log_date_range );

try {
$section_payloads = $this->extract_sections_from_payloads( $module_slug, $raw_sections_payloads );

if ( is_wp_error( $section_payloads ) ) {
// Surface payload build failures directly so callers receive the original module error context.
return $section_payloads;
}

foreach ( $section_payloads as $section_payload ) {
list( $labels, $values, $trends, $event_names ) = $this->normalize_section_payload_components( $section_payload );

$date_range = $log_date_range ?: $this->report_processor->compute_date_range( $section_payload['date_range'] ?? null );

$section = new Email_Report_Data_Section_Part(
$section_payload['section_key'] ?? 'section',
array(
'title' => $section_payload['title'] ?? '',
'labels' => $labels,
'event_names' => $event_names,
'values' => $values,
'trends' => $trends,
'dimensions' => $section_payload['dimensions'] ?? array(),
'dimension_values' => $section_payload['dimension_values'] ?? array(),
'date_range' => $date_range,
'dashboard_link' => $this->format_dashboard_link( $module_slug ),
)
);

if ( $section->is_empty() ) {
continue;
}
if ( is_wp_error( $section_payloads ) ) {
// Surface payload build failures directly so callers receive the original module error context.
return $section_payloads;
}

$sections[] = $section;
}
} catch ( \Exception $exception ) {
if ( $switched_locale ) {
restore_previous_locale();
foreach ( $section_payloads as $section_payload ) {
list( $labels, $values, $trends, $event_names ) = $this->normalize_section_payload_components( $section_payload );

$date_range = $log_date_range ?: $this->report_processor->compute_date_range( $section_payload['date_range'] ?? null );

$section = new Email_Report_Data_Section_Part(
$section_payload['section_key'] ?? 'section',
array(
'title' => $section_payload['title'] ?? '',
'labels' => $labels,
'event_names' => $event_names,
'values' => $values,
'trends' => $trends,
'dimensions' => $section_payload['dimensions'] ?? array(),
'dimension_values' => $section_payload['dimension_values'] ?? array(),
'date_range' => $date_range,
'dashboard_link' => $this->format_dashboard_link( $module_slug ),
)
);

if ( $section->is_empty() ) {
continue;
}

// Re-throw exception to the caller to prevent this email from being sent.
throw $exception;
$sections[] = $section;
}

$this->current_period_length = null;
Expand Down
8 changes: 3 additions & 5 deletions includes/Core/Email_Reporting/Email_Template_Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,14 @@ public function __construct( Context $context, Email_Report_Section_Builder $sec
* Builds sections from raw payload grouped by module.
*
* @since 1.170.0
* @since n.e.x.t Removed $user parameter, locale switching is now handled by the Email_Log_Processor.
*
* @param array $raw_payload Raw payload.
* @param WP_Post $email_log Email log post.
* @param WP_User $user User receiving the report.
* @return array|WP_Error Sections array or WP_Error.
*/
public function build_sections( $raw_payload, WP_Post $email_log, WP_User $user ) {
$sections = array();
$user_locale = get_user_locale( $user );
public function build_sections( $raw_payload, WP_Post $email_log ) {
$sections = array();

if ( ! is_array( $raw_payload ) ) {
return $sections;
Expand All @@ -109,7 +108,6 @@ public function build_sections( $raw_payload, WP_Post $email_log, WP_User $user
$module_sections = $this->section_builder->build_sections(
$module_slug,
array( $module_payload ),
$user_locale,
$email_log
);
} catch ( \Throwable $exception ) {
Expand Down
Loading
Loading