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
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Lets a FluentCRM email send From a founder address instead of hello@, for person

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.
- 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 two places, because the two send paths add it at different times. Campaigns and one-off emails: `fluentcrm_email_body_text` sets the campaign's trackers to `no` before FluentCRM adds click tracking. Automation emails: `FunnelCampaign::subscribe()` adds click tracking when the email is queued, from the cached site-wide setting, so the render hook swaps each `?ns_url=` link back to the URL stored in `fc_url_stores` (Smart Links stay tracked) and turns off open tracking on the campaign before FluentCRM adds the pixel.
- 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.
Expand Down
80 changes: 70 additions & 10 deletions classes/Email/PersonalTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@ public static function addTemplate( $templates ) {
*
* @param string $emailBody Body before the template.
* @param array $templateData `email_body` (parsed blocks) and `preHeader`.
* @param mixed $campaign The campaign being sent.
* @param mixed $campaign The campaign being sent; its open tracking is turned off.
* @return string
*/
public static function render( $emailBody, $templateData = [], $campaign = null ) {
// FluentCRM adds the open pixel after this, from this campaign object's settings.
self::turnOffTracking( $campaign );

$body = is_array( $templateData ) && isset( $templateData['email_body'] ) ? (string) $templateData['email_body'] : (string) $emailBody;
// Automation emails get click tracking when queued, before any per-email filter runs.
$body = self::restoreTrackedLinks( $body );
$pre_header = is_array( $templateData ) && isset( $templateData['preHeader'] ) ? trim( (string) $templateData['preHeader'] ) : '';

$hidden_pre_header = '' === $pre_header
Expand All @@ -79,15 +84,7 @@ public static function render( $emailBody, $templateData = [], $campaign = null
*/
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;
self::turnOffTracking( self::campaignOf( $emailModel ) );
} catch ( \Throwable $e ) {
error_log( 'PersonalTemplate::disableTracking: ' . $e->getMessage() );
}
Expand All @@ -113,6 +110,69 @@ public static function disableUnsubscribeHeader( $enabled, $data = null, $subscr
}
}

/**
* Sets the campaign's open and click tracking to off, in memory only.
*
* @param mixed $campaign Campaign model, or null.
*/
private static function turnOffTracking( $campaign ): void {
if ( ! is_object( $campaign ) ) {
return;
}

$settings = is_array( $campaign->settings ) ? $campaign->settings : [];
$settings['open_tracker'] = 'no';
$settings['click_tracker'] = 'no';
$campaign->settings = $settings;
}

/**
* Swaps each click-tracking link (`<site>?ns_url=<short>&mid=…`) back to the URL it stands for.
*
* Smart Links stay tracked: they only work through FluentCRM's redirect.
*
* @param string $html Email body.
* @return string
*/
public static function restoreTrackedLinks( string $html ): string {
if ( false === strpos( $html, 'ns_url=' ) ) {
return $html;
}

$restored = preg_replace_callback(
'/href=(["\'])([^"\']*ns_url=[^"\']*)\1/i',
function ( $match ) {
$query = (string) wp_parse_url( html_entity_decode( $match[2], ENT_QUOTES ), PHP_URL_QUERY );
parse_str( $query, $args );
$short = isset( $args['ns_url'] ) && is_string( $args['ns_url'] ) ? $args['ns_url'] : '';
$original = '' === $short ? null : self::originalUrl( $short );

$keep_tracked = null === $original || false !== strpos( $original, 'smart_url' );

return $keep_tracked ? $match[0] : 'href=' . $match[1] . esc_url( $original ) . $match[1];
},
$html
);

return is_string( $restored ) ? $restored : $html;
}

/**
* The URL FluentCRM stored for a click-tracking short code, or null.
*
* @param string $short Short code from `ns_url`.
*/
private static function originalUrl( string $short ): ?string {
if ( ! class_exists( '\FluentCrm\App\Models\UrlStores' ) ) {
return null;
}

$row = \FluentCrm\App\Models\UrlStores::getRowByShort( $short );
$url = is_object( $row ) && isset( $row->url ) ? (string) $row->url : '';

return '' === $url ? null : $url;
}

/**
* The email's campaign when it uses this template, else null.
*
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/PersonalTemplateTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
<?php

namespace FluentCrm\App\Models {
if ( ! class_exists( UrlStores::class ) ) {
/**
* Stand-in for FluentCRM's link table: short code => stored row.
*/
class UrlStores {
public static $rows = [];

public static function getRowByShort( $short ) {
return self::$rows[ $short ] ?? null;
}
}
}
}

namespace {

use CustomCRM\Email\PersonalTemplate;
use FluentCrm\App\Models\UrlStores;
use PHPUnit\Framework\TestCase;

class PersonalTemplateTest extends TestCase {
Expand Down Expand Up @@ -99,4 +117,46 @@ public function test_personal_email_gets_no_unsubscribe_header() {
public function test_other_templates_keep_the_unsubscribe_header() {
$this->assertTrue( PersonalTemplate::disableUnsubscribeHeader( true, [], null, $this->email( 'simple' ) ) );
}

public function test_render_turns_off_open_tracking_on_the_campaign() {
$campaign = new stdClass();
$campaign->settings = [ 'open_tracker' => 'yes' ];

PersonalTemplate::render( '', [ 'email_body' => '<p>x</p>' ], $campaign );

$this->assertSame( 'no', $campaign->settings['open_tracker'] );
$this->assertSame( 'no', $campaign->settings['click_tracker'] );
}

public function test_render_restores_links_tracked_when_an_automation_queued_the_email() {
UrlStores::$rows = [ 'aB3xYz' => (object) [ 'url' => 'https://www.gravitykit.com/docs/' ] ];

$html = PersonalTemplate::render( '', [ 'email_body' => '<p><a href="https://www.gravitykit.com?ns_url=aB3xYz&mid=1140857&fch=832dd570">Docs</a></p>' ] );

$this->assertStringContainsString( 'href="https://www.gravitykit.com/docs/"', $html );
$this->assertStringNotContainsString( 'ns_url', $html );
}

public function test_tracked_link_written_with_encoded_ampersands_is_restored() {
UrlStores::$rows = [ 'aB3xYz' => (object) [ 'url' => 'https://example.com/a?b=1' ] ];

$html = PersonalTemplate::restoreTrackedLinks( "<a href='https://www.gravitykit.com/?ns_url=aB3xYz&amp;mid=5'>x</a>" );

$this->assertSame( "<a href='https://example.com/a?b=1'>x</a>", $html );
}

public function test_unknown_short_code_and_smart_links_stay_tracked() {
UrlStores::$rows = [ 'smart1' => (object) [ 'url' => 'https://www.gravitykit.com/?fluentcrm=1&route=smart_url&slug=x' ] ];
$html = '<a href="https://www.gravitykit.com?ns_url=nope99&mid=1">a</a> <a href="https://www.gravitykit.com?ns_url=smart1&mid=1">b</a>';

$this->assertSame( $html, PersonalTemplate::restoreTrackedLinks( $html ) );
}

public function test_body_without_tracked_links_is_unchanged() {
$html = '<p><a href="https://www.gravitykit.com/docs/">Docs</a></p>';

$this->assertSame( $html, PersonalTemplate::restoreTrackedLinks( $html ) );
}
}

}
4 changes: 4 additions & 0 deletions tests/unit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ function sanitize_key( $key ) {
return preg_replace( '/[^a-z0-9_\-]/', '', strtolower( (string) $key ) );
}

function esc_url( $url ) {
return (string) $url;
}

function esc_url_raw( $url ) {
return (string) $url;
}
Expand Down
Loading