-
Notifications
You must be signed in to change notification settings - Fork 328
Closed
Labels
P0High priorityHigh priorityPHPTeam SIssues for Squad 1Issues for Squad 1Type: EnhancementImprovement of an existing featureImprovement of an existing feature
Description
Feature Description
Create a generalised Email class to abstract:
- Combine template layout to HTML
- Collect and apply to, subject, headers etc for mail sending
- Send via wp_mail
- Monitor and return errors using wp_mail_failed hook
This class should not be email report specific, allowing separate templates in future for other plugin emails. The namespace and structure should reflect this.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance Criteria
- A new, reusable class named
Google\Site_Kit\Core\Util\Emailis created to abstract the logic for sending emails. - The class includes a method to generate HTML email content by combining a template layout with provided data.
- No complete template needs to be implemented as part of this ticket. This will be worked on in Create email report base template #11549, Design email template sections (1/2) #11550 and Design email template sections (2/2) #11551 The method should be a scaffold that accepts a template key and data, and returns an HTML string (e.g., using a minimal placeholder template for testing the class functionality).
- The design must allow for different keyed templates to be used for future emails with a different layout.
- The class contains a method responsible for building and setting email properties, including the recipient address (
to), subject line, and headers, to provide towp_mail.- Email Header preserves the site’s configured ‘From’ email address; only the display name is overridden to
Site Kit {site_url}(using the existing address returned by WordPress).
- Email Header preserves the site’s configured ‘From’ email address; only the display name is overridden to
- The class implements a method that utilizes the WordPress
wp_mailfunction to send the email. - The class is capable of capturing and handling errors that occur during the email sending process by monitoring the
wp_mail_failedhook.- Error monitoring using the
wp_mail_failedhook must be explicitly added before thewp_mailcall and removed immediately after thewp_mailcall to ensure the error tracking is correctly scoped only to the current email send attempt.
- Error monitoring using the
- If
wp_mailreturnsfalseor an error is caught via the hook, the method should return or communicate the error state.
Implementation Brief
- Add
Google\Site_Kit\Core\Email_Reporting\Sections_Mapwhich returns an array describing the layout sections, for example:
return array(
'is_my_site_helping_my_business_grow' => array(
'title' => esc_html__( 'Is my site helping my business grow?', 'google-site-kit' ),
'icon' => 'icon-name',
'section_parts' => array(
'total_conversion_events' => array(
'template' => 'metrics',
'data' => $payload['total_conversion_events'],
),
'products_added_to_cart' => array(
'template' => 'conversion-metrics',
'data' => $payload['products_added_to_cart'],
),
),
// … rest of the sections ...
);- Icons will map to a PNG stored in the assets folder (later bucket) keyed via name via a helper added in Create email report base template #11549.
- Map the sections based on the titles in Figma, and section parts based on their mapping in the report options classes added in Add report options builder classes for Analytics, AdSense and SC #11552
- Section part templates should match one of the dynamic parts
page-metrics,metricsorconversion-metricsmostly implemented in Create email report base template #11549 - You can include
get_sectionsmethod which returns the final array, which can be assembled from different methods each representing a section, which is finally merged into single array. Or it can just be returned as single array - Add
Google\Site_Kit\Core\Email_Reporting\Email_Template_Rendererclass- Add template rendering
render( $data )method - Get the config array from
Google\Site_Kit\Core\Email_Reporting\Sections_Mapclass (you can extract this into separate method likeget_sections_map) then render each section in order:
* Iterate the sections map, along with the data array when including the template part file. The passed$datawill have mapped data with keys named by each section part and data will holdEmail_Report_Data_Sectioninstance.
* Skip including section parts not mapped in the data request array - so sections to which user has no access are not included for example checkisset( $data[ $section_part_key ] ) - Resolve the main template file from
Google\Site_Kit\Core\Email_Reporting\templates. If missing, return empty string.- Wrap the
includeoftemplate.phpin an output buffer, and append the rendered HTML to a single string.
- Wrap the
- Add template rendering
- Create
Google\Site_Kit\Core\Util\Emailutility- Header builder (
build_headers)- Fetch the filtered From email via
wp_mail_from. Do not change the address. - Override the "from" header to
Site Kit. - Merge caller-supplied headers and return the final header array.
- Fetch the filtered From email via
- Add
Sendmethodsend( $to, $subject, $content, $headers = array() )- Wrap the
wp_mailcall with a scoped listener:- Reset
$this->last_errortonull. - Define a closure
$listener = function( WP_Error $error ) { $this->last_error = $error; };. - Call
add_action( 'wp_mail_failed', $listener );before invokingwp_mail(). - After
wp_mail()returns, immediately callremove_action( 'wp_mail_failed', $listener );so the hook only applies to this send. - If
wp_mail()returnedfalseor the listener captured an error, return thatWP_Error; otherwise returntrue. - Expose
get_last_error()that returns the storedWP_Erroror null in$this->last_errorso callers can inspect the most recent failure message.
- Reset
- Header builder (
Test Coverage
- Add tests for
Email_Template_Renderer:render()returns concatenated HTML whentemplate.phpand part files exist, and returns an empty string when the template key is missing.- Sections not present in
$dataare skipped (verify the markup omits them).
- Add tests for
Emailclass:build_headerskeeps the originalFromemail (mockwp_mail_from) and updates only the display name to Site Kit{host}; additional headers merge correctly.send()returns true whenwp_mailis stubbed to succeed and no failure hook fires.send()returns aWP_Errorand stores it viaget_last_error()when thewp_mail_failedhook is triggered or whenwp_mailreturns false.
QA Brief
- No QA required as this work is a backend change. The template will be QA'd in Create email report base template #11549.
Changelog entry
- Add a class to send emails.
Metadata
Metadata
Assignees
Labels
P0High priorityHigh priorityPHPTeam SIssues for Squad 1Issues for Squad 1Type: EnhancementImprovement of an existing featureImprovement of an existing feature