Skip to content

Commit 83d24dc

Browse files
committed
Update tester plugin to add new templates.
1 parent eb96177 commit 83d24dc

7 files changed

Lines changed: 429 additions & 7 deletions

File tree

65.9 KB
Loading

includes/Core/Email_Reporting/Email_Template_Formatter.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,44 @@ private function calculate_period_length_from_range( $date_range ) {
271271
return $diff->days + 1;
272272
}
273273

274+
/**
275+
* Builds template data for invitation email rendering.
276+
*
277+
* @since n.e.x.t
278+
*
279+
* @param string $inviter_email Email address of the person sending the invitation.
280+
* @return array Template data for invitation email.
281+
*/
282+
public function prepare_invitation_template_data( $inviter_email ) {
283+
$site_domain = $this->get_site_domain();
284+
285+
return array(
286+
'subject' => sprintf(
287+
/* translators: 1: Inviter email address, 2: Site domain. */
288+
__( '%1$s invited you to receive periodic Site Kit performance reports for %2$s', 'google-site-kit' ),
289+
$inviter_email,
290+
$site_domain
291+
),
292+
'preheader' => sprintf(
293+
/* translators: %s: Inviter email address. */
294+
__( '%s invited you to receive periodic performance reports', 'google-site-kit' ),
295+
$inviter_email
296+
),
297+
'site' => array(
298+
'domain' => $site_domain,
299+
),
300+
'inviter_email' => $inviter_email,
301+
'learn_more_url' => 'https://sitekit.withgoogle.com/documentation/email-reports/',
302+
'primary_call_to_action' => array(
303+
'label' => __( 'Get your report', 'google-site-kit' ),
304+
'url' => admin_url( 'admin.php?page=googlesitekit-dashboard' ),
305+
),
306+
'footer' => array(
307+
'copy' => __( 'You received this email because your site admin invited you to use Site Kit email reports feature', 'google-site-kit' ),
308+
),
309+
);
310+
}
311+
274312
/**
275313
* Builds template data for rendering.
276314
*

includes/Core/Email_Reporting/Email_Template_Renderer.php

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,37 @@ class Email_Template_Renderer {
2727
*/
2828
const EMAIL_ASSETS_BASE_URL = 'https://storage.googleapis.com/pue-email-assets-dev/';
2929

30+
/**
31+
* Asset paths mapping.
32+
*
33+
* Maps asset filenames to their versioned folder paths.
34+
* Format: 'asset-filename.png' => 'version/template-name'
35+
*
36+
* @since n.e.x.t
37+
* @var array
38+
*/
39+
const EMAIL_ASSET_PATHS = array(
40+
// email-report assets (1.168.0).
41+
'site-kit-logo.png' => '1.168.0/email-report',
42+
'shooting-stars-graphic.png' => '1.168.0/email-report',
43+
'icon-conversions.png' => '1.168.0/email-report',
44+
'icon-growth.png' => '1.168.0/email-report',
45+
'icon-link-arrow.png' => '1.168.0/email-report',
46+
'icon-search.png' => '1.168.0/email-report',
47+
'icon-views.png' => '1.168.0/email-report',
48+
'icon-visitors.png' => '1.168.0/email-report',
49+
'conversions-timeline-green.png' => '1.168.0/email-report',
50+
'conversions-timeline-red.png' => '1.168.0/email-report',
51+
'notification-icon-star.png' => '1.168.0/email-report',
52+
// invitation-email assets (1.172.0).
53+
'invitation-envelope-graphic.png' => '1.172.0/invitation-email',
54+
);
55+
3056
/**
3157
* The sections map instance.
3258
*
3359
* @since 1.168.0
34-
* @var Sections_Map
60+
* @var Sections_Map|null
3561
*/
3662
protected $sections_map;
3763

@@ -57,24 +83,32 @@ class Email_Template_Renderer {
5783
* Constructor.
5884
*
5985
* @since 1.168.0
86+
* @since n.e.x.t Sections map is now optional for templates that don't use sections.
6087
*
61-
* @param Sections_Map $sections_map The sections map instance.
88+
* @param Sections_Map|null $sections_map The sections map instance, or null for simple templates.
6289
*/
63-
public function __construct( Sections_Map $sections_map ) {
90+
public function __construct( Sections_Map $sections_map = null ) {
6491
$this->sections_map = $sections_map;
6592
$this->templates_dir = realpath( __DIR__ . '/templates' );
6693
}
6794

6895
/**
6996
* Gets the full URL for an email asset.
7097
*
98+
* Constructs a URL following the folder structure:
99+
* {base_url}/{version}/{template-name}/{asset-filename}
100+
*
71101
* @since 1.168.0
102+
* @since n.e.x.t Updated to use asset-specific path mapping.
72103
*
73104
* @param string $asset_name The asset filename (e.g., 'icon-conversions.png').
74105
* @return string The full URL to the asset.
75106
*/
76107
public function get_email_asset_url( $asset_name ) {
77-
return self::EMAIL_ASSETS_BASE_URL . ltrim( $asset_name, '/' );
108+
$asset_name = ltrim( $asset_name, '/' );
109+
$asset_path = self::EMAIL_ASSET_PATHS[ $asset_name ] ?? '1.168.0/email-report';
110+
111+
return self::EMAIL_ASSETS_BASE_URL . $asset_path . '/' . $asset_name;
78112
}
79113

80114
/**
@@ -92,7 +126,7 @@ public function render( $template_name, $data ) {
92126
return '';
93127
}
94128

95-
$sections = $this->sections_map->get_sections();
129+
$sections = $this->sections_map ? $this->sections_map->get_sections() : array();
96130

97131
$shared_parts_dir = $this->templates_dir . '/parts';
98132
$template_parts_dir = $this->templates_dir . '/' . $template_name . '/parts';
@@ -171,13 +205,20 @@ protected function render_part_file( $file, $vars = array() ) {
171205
* Plain_Text_Formatter for formatting.
172206
*
173207
* @since 1.170.0
208+
* @since n.e.x.t Added support for invitation-email template.
174209
*
175-
* @param string $template_name The template name (unused for plain text, kept for API consistency with render()).
210+
* @param string $template_name The template name.
176211
* @param array $data The data to render (metadata like subject, preheader, etc.).
177212
* @return string The rendered plain text.
178213
*/
179214
public function render_text( $template_name, $data ) {
180-
$sections = $this->sections_map->get_sections();
215+
// Handle invitation email separately.
216+
if ( 'invitation-email' === $template_name ) {
217+
return Plain_Text_Formatter::format_invitation_email( $data );
218+
}
219+
220+
// Handle email-report template with sections.
221+
$sections = $this->sections_map ? $this->sections_map->get_sections() : array();
181222

182223
$output = Plain_Text_Formatter::format_header(
183224
$data['site']['domain'] ?? '',

includes/Core/Email_Reporting/Plain_Text_Formatter.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,57 @@ public static function format_header( $site_domain, $date_label ) {
4040
return implode( "\n", $lines );
4141
}
4242

43+
/**
44+
* Formats the invitation email as plain text.
45+
*
46+
* @since n.e.x.t
47+
*
48+
* @param array $data The invitation email data.
49+
* @return string Formatted plain text email.
50+
*/
51+
public static function format_invitation_email( $data ) {
52+
$site_domain = $data['site']['domain'] ?? '';
53+
$inviter_email = $data['inviter_email'] ?? '';
54+
$learn_more_url = $data['learn_more_url'] ?? '';
55+
$cta = $data['primary_call_to_action'] ?? array();
56+
$footer_copy = $data['footer']['copy'] ?? '';
57+
58+
$lines = array(
59+
__( 'Site Kit by Google', 'google-site-kit' ),
60+
'',
61+
$site_domain,
62+
'',
63+
sprintf(
64+
/* translators: %s: Email address of the person who sent the invitation */
65+
__( '%s invited you to receive periodic performance reports', 'google-site-kit' ),
66+
$inviter_email
67+
),
68+
'',
69+
__( 'Receive the most important insights about your site\'s performance, key trends, and tailored metrics, powered by Site Kit, directly in your inbox.', 'google-site-kit' ),
70+
'',
71+
self::format_link( __( 'Learn more', 'google-site-kit' ), $learn_more_url ),
72+
'',
73+
__( 'You can easily unsubscribe or change the reports frequency anytime from your Site Kit dashboard.', 'google-site-kit' ),
74+
'',
75+
str_repeat( '-', 50 ),
76+
'',
77+
);
78+
79+
// Primary CTA.
80+
if ( ! empty( $cta['url'] ) ) {
81+
$label = $cta['label'] ?? __( 'Get your report', 'google-site-kit' );
82+
$lines[] = self::format_link( $label, $cta['url'] );
83+
$lines[] = '';
84+
}
85+
86+
// Footer copy.
87+
if ( ! empty( $footer_copy ) ) {
88+
$lines[] = $footer_copy;
89+
}
90+
91+
return implode( "\n", $lines );
92+
}
93+
4394
/**
4495
* Formats a section based on its template type.
4596
*
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Header section for the invitation-email template.
4+
*
5+
* A simplified header with just the Site Kit logo, distinct from
6+
* the more complex report email header.
7+
*
8+
* @package Google\Site_Kit\Core\Email_Reporting
9+
* @copyright 2025 Google LLC
10+
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
11+
* @link https://sitekit.withgoogle.com
12+
*
13+
* @var callable $get_asset_url Function to generate asset URLs.
14+
*/
15+
16+
$logo_url = $get_asset_url( 'site-kit-logo.png' );
17+
?>
18+
<table role="presentation" width="100%">
19+
<tr>
20+
<td style="padding: 24px 0 16px 0; text-align: center;" align="center">
21+
<img src="<?php echo esc_url( $logo_url ); ?>" alt="<?php echo esc_attr__( 'Site Kit by Google', 'google-site-kit' ); ?>" width="79" height="22" style="display: inline-block;" />
22+
</td>
23+
</tr>
24+
</table>

0 commit comments

Comments
 (0)