OpenTracking.php
74 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Newsletter\Renderer\PostProcess; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Newsletter\Links\Links; |
| 9 | use MailPoet\Newsletter\Renderer\Renderer; |
| 10 | use MailPoet\Util\pQuery\pQuery; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | |
| 13 | class OpenTracking { |
| 14 | public static function process($template) { |
| 15 | $DOM = new pQuery(); |
| 16 | $DOM = $DOM->parseStr($template); |
| 17 | $template = $DOM->select('body'); |
| 18 | self::appendToDomNodes($template, self::getTrackingImageHtml()); |
| 19 | return $DOM->__toString(); |
| 20 | } |
| 21 | |
| 22 | public static function getTrackingImageHtml(): string { |
| 23 | // url is a temporary data tag that will be further replaced with |
| 24 | // the proper track API URL during sending |
| 25 | return sprintf('<img alt="" class="" src="%s"/>', Links::DATA_TAG_OPEN); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Removes the whole open-tracking <img> element, not just the data tag — |
| 30 | * leaving an empty src would make email clients resolve the base URL or |
| 31 | * show a broken image. |
| 32 | * |
| 33 | * CNIL/Garante: for subscribers without tracking consent the read |
| 34 | * operation must not happen at all on future emails, so the pixel never |
| 35 | * ships. We first try an exact match on getTrackingImageHtml(); if pQuery |
| 36 | * re-serialised the tag (attribute order, self-closing style) we fall back |
| 37 | * to a regex that removes any <img> whose src is the open data tag. |
| 38 | */ |
| 39 | public static function removeTrackingImage(string $template): string { |
| 40 | $exact = str_replace(self::getTrackingImageHtml(), '', $template); |
| 41 | if ($exact !== $template) { |
| 42 | return $exact; |
| 43 | } |
| 44 | // Fallback: remove the whole <img …> element that carries the open data |
| 45 | // tag as its src, regardless of attribute order/quoting/self-closing. |
| 46 | $pattern = '/<img\b[^>]*\bsrc=("|\')' . preg_quote(Links::DATA_TAG_OPEN, '/') . '\1[^>]*>/i'; |
| 47 | $result = preg_replace($pattern, '', $template); |
| 48 | return is_string($result) ? $result : $template; |
| 49 | } |
| 50 | |
| 51 | public static function addTrackingImage() { |
| 52 | WPFunctions::get()->addFilter(Renderer::FILTER_POST_PROCESS, function ($template) { |
| 53 | return OpenTracking::process($template); |
| 54 | }); |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | private static function appendToDomNodes($template, $openTrackingImage): void { |
| 59 | // Preserve backward compatibility with pQuery::html() |
| 60 | // by processing an array of DomNodes |
| 61 | if (!empty($template)) { |
| 62 | $template = is_array($template) ? $template : [$template]; |
| 63 | array_map( |
| 64 | function ($item) use ($openTrackingImage) { |
| 65 | $itemHtml = $item->toString(true, true, 1); |
| 66 | $item->html($itemHtml . $openTrackingImage); |
| 67 | return $item; |
| 68 | }, |
| 69 | $template |
| 70 | ); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 |