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
53 changes: 1 addition & 52 deletions includes/Core/Email_Reporting/Batch_Error_Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class Batch_Error_Notifier {
const NON_SENDABLE_CATEGORIES = array(
'sending_error',
'cron_scheduler_error',
'other_error',
);

/**
Expand All @@ -47,7 +46,6 @@ class Batch_Error_Notifier {
const CATEGORY_CONTENT_MAP = array(
'permissions_error' => 'permissions',
'report_error' => 'report',
'network_error' => 'network',
'server_error' => 'server',
);

Expand Down Expand Up @@ -271,7 +269,7 @@ private function build_template_data( $content_key ) {
$title = Content_Map::get_title( 'error-email' );
}

$body_args = $this->get_body_args( $content_key );
$body_args = Content_Map::get_body_args( $content_key, $this->golinks );
$body = Content_Map::get_body_with_args( $content_key, $body_args );
$domain = $this->get_site_domain();

Expand Down Expand Up @@ -299,55 +297,6 @@ private function build_template_data( $content_key ) {
);
}

/**
* Gets sprintf arguments for Content_Map body placeholders.
*
* Maps each content key to the URLs that fill its `%s` / `%1$s` / `%2$s`
* placeholders. Keys without placeholders return an empty array.
*
* @since 1.175.0
*
* @param string $content_key Content_Map key.
* @return array Ordered sprintf arguments for the body paragraphs.
*/
private function get_body_args( $content_key ) {
$link_style = 'color:#108080;';
$help_url = add_query_arg( 'doc', 'email-reporting', 'https://sitekit.withgoogle.com/support/' );

// URLs are internally generated (golinks, add_query_arg) and safe.
// Escaping is handled by wp_kses() in the HTML template and
// wp_strip_all_tags() in the plain text renderer.
switch ( $content_key ) {
case 'error-email-report-search-console':
$settings_url = add_query_arg( 'module', 'search-console', $this->golinks->get_url( 'settings' ) );
return array(
'<a class="link" href="' . $settings_url . '" style="' . $link_style . '">',
'</a>',
'<a class="link" href="' . $help_url . '" style="' . $link_style . '">',
'</a>',
);

case 'error-email-report-analytics-4':
$settings_url = add_query_arg( 'module', 'analytics-4', $this->golinks->get_url( 'settings' ) );
return array(
'<a class="link" href="' . $settings_url . '" style="' . $link_style . '">',
'</a>',
'<a class="link" href="' . $help_url . '" style="' . $link_style . '">',
'</a>',
);

case 'error-email-permissions-search-console':
case 'error-email-permissions-analytics-4':
return array(
'<a class="link" href="' . $help_url . '" style="' . $link_style . '">',
'</a>',
);

default:
return array();
}
}

/**
* Gets the site domain including subdirectory context.
*
Expand Down
64 changes: 57 additions & 7 deletions includes/Core/Email_Reporting/Content_Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Google\Site_Kit\Core\Email_Reporting;

use Google\Site_Kit\Core\Golinks\Golinks;

/**
* Class for mapping email template content (titles and body).
*
Expand Down Expand Up @@ -155,10 +157,13 @@ public static function get_graphic_config( $template_name ) {
protected static function get_all_titles() {
return array(
/* translators: 1: opening anchor tag with mailto link, 2: inviter email address, 3: closing anchor tag */
'invitation-email' => __( '%1$s%2$s%3$s invited you to receive periodic performance reports', 'google-site-kit' ),
'subscription-confirmation' => __( 'Success! You’re subscribed to Site Kit reports', 'google-site-kit' ),
'error-email' => __( 'Email reports are failing to send', 'google-site-kit' ),
'error-email-cron-scheduler' => __( 'Email reports are failing to send', 'google-site-kit' ),
'invitation-email' => __( '%1$s%2$s%3$s invited you to receive periodic performance reports', 'google-site-kit' ),
'subscription-confirmation' => __( 'Success! You’re subscribed to Site Kit reports', 'google-site-kit' ),
'error-email' => __( 'Email reports are failing to send', 'google-site-kit' ),
'error-email-permissions-search-console' => __( 'Action needed: your Site Kit report couldn’t be generated', 'google-site-kit' ),
'error-email-permissions-analytics-4' => __( 'Action needed: your Site Kit report couldn’t be generated', 'google-site-kit' ),
'error-email-report-search-console' => __( 'Action needed: your Site Kit report couldn’t be generated', 'google-site-kit' ),
'error-email-report-analytics-4' => __( 'Action needed: your Site Kit report couldn’t be generated', 'google-site-kit' ),
);
}

Expand All @@ -184,9 +189,6 @@ protected static function get_all_bodies() {
'error-email' => array(
__( 'We were unable to generate your report due to a server error. To fix this, contact your host. Report delivery will automatically resume once the issue is resolved.', 'google-site-kit' ),
),
'error-email-cron-scheduler' => array(
__( 'We were unable to deliver your report, likely due to a WP-Cron configuration error in your WordPress site’s system settings. To fix this, contact your administrator or get help. Report delivery will automatically resume once the issue is resolved.', 'google-site-kit' ),
),
// Opening/closing tag placeholders keep inline styles and HTML
// out of translation strings. Inline color styles are required
// because many email clients strip or ignore CSS classes.
Expand All @@ -212,4 +214,52 @@ protected static function get_all_bodies() {
),
);
}

/**
* Gets sprintf arguments for body placeholders.
*
* Maps each content key to the styled anchor tags that fill its
* `%s` / `%1$s` / `%2$s` placeholders. Keys without placeholders
* return an empty array.
*
* @since n.e.x.t
*
* @param string $content_key Content key (e.g. 'error-email-report-analytics-4').
* @param Golinks $golinks Golinks instance for building URLs.
* @return array Ordered sprintf arguments for the body paragraphs.
*/
public static function get_body_args( $content_key, Golinks $golinks ) {
$link_style = 'color:#108080;';
$help_url = add_query_arg( 'doc', 'email-reporting', 'https://sitekit.withgoogle.com/support/' );

switch ( $content_key ) {
case 'error-email-report-search-console':
$settings_url = add_query_arg( 'module', 'search-console', $golinks->get_url( 'settings' ) );
return array(
'<a class="link" href="' . $settings_url . '" style="' . $link_style . '">',
'</a>',
'<a class="link" href="' . $help_url . '" style="' . $link_style . '">',
'</a>',
);

case 'error-email-report-analytics-4':
$settings_url = add_query_arg( 'module', 'analytics-4', $golinks->get_url( 'settings' ) );
return array(
'<a class="link" href="' . $settings_url . '" style="' . $link_style . '">',
'</a>',
'<a class="link" href="' . $help_url . '" style="' . $link_style . '">',
'</a>',
);

case 'error-email-permissions-search-console':
case 'error-email-permissions-analytics-4':
return array(
'<a class="link" href="' . $help_url . '" style="' . $link_style . '">',
'</a>',
);

default:
return array();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public function data_non_sendable_categories() {
return array(
'sending_error' => array( 'sending_error' ),
'cron_scheduler_error' => array( 'cron_scheduler_error' ),
'other_error' => array( 'other_error' ),
);
}

Expand Down Expand Up @@ -133,7 +132,6 @@ public function data_sendable_categories() {
return array(
'permissions_error' => array( 'permissions_error', 'error-email' ),
'report_error' => array( 'report_error', 'error-email' ),
'network_error' => array( 'network_error', 'error-email' ),
'server_error' => array( 'server_error', 'error-email' ),
);
}
Expand Down Expand Up @@ -305,8 +303,7 @@ public function test_uses_module_specific_content_when_available( $category_id,
self::factory()->user->create( array( 'role' => 'administrator' ) );
$this->set_up_batch_with_category_and_module( $category_id, $module_slug );

// Module-specific titles were removed; all error emails use the generic error-email title.
$expected_subject = Content_Map::get_title( 'error-email' );
$expected_subject = Content_Map::get_title( $expected_content_key );

$this->batch_query->expects( $this->once() )
->method( 'mark_batch_admin_notified' )
Expand Down Expand Up @@ -358,9 +355,9 @@ public function test_falls_back_to_generic_error_when_no_module_match() {

public function test_falls_back_to_generic_error_for_unknown_module() {
self::factory()->user->create( array( 'role' => 'administrator' ) );
$this->set_up_batch_with_category_and_module( 'network_error', 'some-unknown-module' );
$this->set_up_batch_with_category_and_module( 'server_error', 'some-unknown-module' );

// No module-specific body for network_error + some-unknown-module, falls back to generic error-email.
// No module-specific body for server_error + some-unknown-module, falls back to generic error-email.
$expected_subject = Content_Map::get_title( 'error-email' );

$this->email_sender->method( 'build_headers' )->willReturn( array() );
Expand Down
Loading