Skip to content
Merged
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
9 changes: 9 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ Lets a FluentCRM email send From a founder address instead of hello@, for person
- **After a Gravity SMTP update,** re-check: `Connector_Base::get_from()` still reads the forced From from settings, `Opts_Data_Store::get_opts()` still calls `get_option( 'gravitysmtp_<name>' )`, `Mail_Handler::mail()` and `Connector_Base::init()` still pass the original headers through the `wp_mail` filter, `Event_Model::update()` still fires `gravitysmtp_after_mail_updated`, and `Mail_Handler::mail()` still honors `[ 'force' => true, 'connector' => … ]` from `gravitysmtp_connector_for_sending`. A `GRAVITYSMTP_<CONNECTOR>_FROM_EMAIL` constant would override all of this.
- Unit tests: `tests/unit/FounderSenderTest.php`.

## Personal email template (`classes/Email/PersonalTemplate.php`)

Three kinds of FluentCRM email: **marketing** (any stock template), **transactional** (FluentCRM's "Mark This as a Transactional Email": no footer, no `List-Unsubscribe` header), and **personal**, the "Personal" template this plugin adds to the template picker (the second icon tab in the editor's Email-Body sidebar).

- A Personal email is sent as the body and the preheader only: no template styling, no footer or "view in browser" link, no `List-Unsubscribe` header (`fluent_crm/enable_unsub_header`), and no open pixel or click-tracking redirects. Tracking is turned off in `fluentcrm_email_body_text`, which FluentCRM runs before it adds click tracking and the pixel; the template's own render hook runs too late for that.
- It has no unsubscribe link at all, so it is for personal check-ins (founder emails), not promotions.
- **The editor preview still draws FluentCRM's footer.** The block editor builds that preview from its own footer settings, not the template; the sent email has none. Check a real send (Mailpit locally) rather than the preview.
- Template ID `gk_personal`. Filter `customcrm/personal_template/enabled` turns it off; with no render callback, FluentCRM's template filter returns the bare body, so emails already saved with it would go out unstyled but with tracking and the unsubscribe header back on.

## Copy rules (Zack)

- **Ask, don't order.** When the email asks the reader to do something for us, say please: "If someone else approves purchases, please forward them this email." Offers of help stay as written ("just reply and we'll sort it out").
Expand Down
146 changes: 146 additions & 0 deletions classes/Email/PersonalTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

namespace CustomCRM\Email;

/**
* A "Personal" design template: the email reads like a typed one. It is sent with no template
* styling, no footer or "view in browser" link, no `List-Unsubscribe` header (so Gmail shows no
* "Unsubscribe" link), and no open or click tracking.
*
* It has no unsubscribe link at all: use it for personal check-ins only.
*/
class PersonalTemplate {

public const ID = 'gk_personal';

/**
* Adds the hooks.
*/
public static function register(): void {
add_filter( 'fluent_crm/email_design_templates', [ self::class, 'addTemplate' ], 20, 1 );
add_filter( 'fluent_crm/email-design-template-' . self::ID, [ self::class, 'render' ], 10, 3 );
// Runs before FluentCRM adds click tracking and the open pixel (CampaignEmail::getEmailBody()).
add_filter( 'fluentcrm_email_body_text', [ self::class, 'disableTracking' ], 999, 3 );
add_filter( 'fluent_crm/enable_unsub_header', [ self::class, 'disableUnsubscribeHeader' ], 20, 4 );
}

/**
* Adds "Personal" to FluentCRM's template picker.
*
* @param mixed $templates Templates keyed by ID.
* @return mixed
*/
public static function addTemplate( $templates ) {
if ( ! is_array( $templates ) ) {
return $templates;
}

$templates[ self::ID ] = [
'id' => self::ID,
'label' => __( 'Personal', 'fluent-crm-custom-features' ),
'image' => self::image(),
// The editor's style panel expects a config; "Plain Left" is the closest look.
'config' => $templates['classic']['config'] ?? [],
'use_gutenberg' => true,
];

return $templates;
}

/**
* The email's HTML: the body, and the preheader when one is set. Nothing else.
*
* @param string $emailBody Body before the template.
* @param array $templateData `email_body` (parsed blocks) and `preHeader`.
* @param mixed $campaign The campaign being sent.
* @return string
*/
public static function render( $emailBody, $templateData = [], $campaign = null ) {
$body = is_array( $templateData ) && isset( $templateData['email_body'] ) ? (string) $templateData['email_body'] : (string) $emailBody;
$pre_header = is_array( $templateData ) && isset( $templateData['preHeader'] ) ? trim( (string) $templateData['preHeader'] ) : '';

$hidden_pre_header = '' === $pre_header
? ''
: '<span style="display:none;font-size:0;line-height:0;max-height:0;max-width:0;opacity:0;overflow:hidden;visibility:hidden;mso-hide:all;">' . esc_html( $pre_header ) . '</span>';

return '<!doctype html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body>'
. $hidden_pre_header
. '<div dir="ltr">' . $body . '</div>'
. '</body></html>';
}

/**
* Turns off open and click tracking for a Personal email, in memory, before FluentCRM applies it.
*
* @param string $emailBody Parsed body.
* @param mixed $subscriber Recipient.
* @param mixed $emailModel The CampaignEmail being sent.
* @return string
*/
public static function disableTracking( $emailBody, $subscriber = null, $emailModel = null ) {
try {
$campaign = self::campaignOf( $emailModel );
if ( null === $campaign ) {
return $emailBody;
}

$settings = is_array( $campaign->settings ) ? $campaign->settings : [];
$settings['open_tracker'] = 'no';
$settings['click_tracker'] = 'no';
$campaign->settings = $settings;
} catch ( \Throwable $e ) {
error_log( 'PersonalTemplate::disableTracking: ' . $e->getMessage() );
}

return $emailBody;
}

/**
* Leaves the `List-Unsubscribe` headers off a Personal email.
*
* @param mixed $enabled Whether FluentCRM adds the headers.
* @param mixed $data Email data.
* @param mixed $subscriber Recipient.
* @param mixed $emailModel The CampaignEmail being sent.
* @return mixed
*/
public static function disableUnsubscribeHeader( $enabled, $data = null, $subscriber = null, $emailModel = null ) {
try {
return null === self::campaignOf( $emailModel ) ? $enabled : false;
} catch ( \Throwable $e ) {
error_log( 'PersonalTemplate::disableUnsubscribeHeader: ' . $e->getMessage() );
return $enabled;
}
}

/**
* The email's campaign when it uses this template, else null.
*
* @param mixed $emailModel A CampaignEmail.
* @return object|null
*/
private static function campaignOf( $emailModel ) {
$campaign = is_object( $emailModel ) ? ( $emailModel->campaign ?? null ) : null;
if ( ! is_object( $campaign ) ) {
return null;
}

$uses_template = self::ID === ( $campaign->design_template ?? '' );

return $uses_template ? $campaign : null;
}

/**
* Picker thumbnail: a few lines of left-aligned text, like a typed email.
*/
private static function image(): string {
$svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 90"><rect width="120" height="90" fill="#fff"/>'
. '<rect x="12" y="14" width="40" height="5" rx="2" fill="#9ca3af"/>'
. '<rect x="12" y="28" width="96" height="4" rx="2" fill="#d1d5db"/>'
. '<rect x="12" y="38" width="88" height="4" rx="2" fill="#d1d5db"/>'
. '<rect x="12" y="48" width="70" height="4" rx="2" fill="#d1d5db"/>'
. '<rect x="12" y="66" width="24" height="5" rx="2" fill="#9ca3af"/></svg>';

return 'data:image/svg+xml;base64,' . base64_encode( $svg );
}
}
4 changes: 4 additions & 0 deletions fluent-crm-custom-features.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ function () {
if ( defined( 'FLUENTCRM' ) && apply_filters( 'customcrm/founder_sender/enabled', true ) ) {
\CustomCRM\Email\FounderSender::register();
}

if ( defined( 'FLUENTCRM' ) && apply_filters( 'customcrm/personal_template/enabled', true ) ) {
\CustomCRM\Email\PersonalTemplate::register();
}
},
20
);
Expand Down
102 changes: 102 additions & 0 deletions tests/unit/PersonalTemplateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

use CustomCRM\Email\PersonalTemplate;
use PHPUnit\Framework\TestCase;

class PersonalTemplateTest extends TestCase {

protected function setUp(): void {
$GLOBALS['test_filters'] = [];
}

private function email( string $template, array $settings = [] ) {
$campaign = new stdClass();
$campaign->design_template = $template;
$campaign->settings = $settings;

$email = new stdClass();
$email->campaign = $campaign;

return $email;
}

public function test_register_adds_template_render_tracking_and_header_hooks() {
PersonalTemplate::register();

foreach ( [ 'fluent_crm/email_design_templates', 'fluent_crm/email-design-template-gk_personal', 'fluentcrm_email_body_text', 'fluent_crm/enable_unsub_header' ] as $hook ) {
$this->assertArrayHasKey( $hook, $GLOBALS['test_filters'], $hook );
}
}

public function test_template_is_added_to_the_picker_with_the_plain_left_config() {
$templates = PersonalTemplate::addTemplate( [
'simple' => [ 'id' => 'simple' ],
'classic' => [ 'id' => 'classic', 'config' => [ 'body_bg_color' => '#fff' ] ],
] );

$this->assertSame( [ 'simple', 'classic', 'gk_personal' ], array_keys( $templates ) );
$this->assertSame( 'Personal', $templates['gk_personal']['label'] );
$this->assertTrue( $templates['gk_personal']['use_gutenberg'] );
$this->assertSame( [ 'body_bg_color' => '#fff' ], $templates['gk_personal']['config'] );
$this->assertStringStartsWith( 'data:image/svg+xml;base64,', $templates['gk_personal']['image'] );
}

public function test_template_list_that_is_not_an_array_is_returned_unchanged() {
$this->assertNull( PersonalTemplate::addTemplate( null ) );
}

public function test_render_has_the_body_and_no_footer_or_browser_link() {
$html = PersonalTemplate::render( 'raw', [
'email_body' => '<p>Hi Sam,</p>',
'preHeader' => '',
'footer_text' => '<p>PO Box 5 <a href="https://example.com/unsubscribe">Unsubscribe</a></p>',
] );

$this->assertStringContainsString( '<p>Hi Sam,</p>', $html );
$this->assertStringNotContainsString( 'Unsubscribe', $html );
$this->assertStringNotContainsString( 'crm_global_email_footer', $html );
$this->assertStringNotContainsString( 'web_preview_url', $html );
$this->assertStringNotContainsString( 'display:none', $html );
}

public function test_render_hides_an_escaped_preheader() {
$html = PersonalTemplate::render( '', [ 'email_body' => '<p>x</p>', 'preHeader' => 'Quick <question>' ] );

$this->assertStringContainsString( 'display:none', $html );
$this->assertStringContainsString( 'Quick &lt;question&gt;', $html );
}

public function test_personal_email_has_open_and_click_tracking_turned_off() {
$email = $this->email( 'gk_personal', [ 'open_tracker' => 'yes', 'other' => 1 ] );

$this->assertSame( 'body', PersonalTemplate::disableTracking( 'body', null, $email ) );
$this->assertSame( 'no', $email->campaign->settings['open_tracker'] );
$this->assertSame( 'no', $email->campaign->settings['click_tracker'] );
$this->assertSame( 1, $email->campaign->settings['other'] );
}

public function test_other_templates_keep_their_tracking() {
$email = $this->email( 'simple', [ 'open_tracker' => 'yes' ] );

PersonalTemplate::disableTracking( 'body', null, $email );

$this->assertSame( [ 'open_tracker' => 'yes' ], $email->campaign->settings );
}

public function test_email_without_a_campaign_is_left_alone() {
$email = new stdClass();
$email->campaign = null;

$this->assertSame( 'body', PersonalTemplate::disableTracking( 'body', null, $email ) );
$this->assertSame( 'body', PersonalTemplate::disableTracking( 'body', null, null ) );
$this->assertTrue( PersonalTemplate::disableUnsubscribeHeader( true, [], null, $email ) );
}

public function test_personal_email_gets_no_unsubscribe_header() {
$this->assertFalse( PersonalTemplate::disableUnsubscribeHeader( true, [], null, $this->email( 'gk_personal' ) ) );
}

public function test_other_templates_keep_the_unsubscribe_header() {
$this->assertTrue( PersonalTemplate::disableUnsubscribeHeader( true, [], null, $this->email( 'simple' ) ) );
}
}
Loading