Merge pull request #17736 from civicrm/5.27
[civicrm-core.git] / ext / flexmailer / src / ClickTracker / TextClickTracker.php
CommitLineData
bdf67e28
SL
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11namespace Civi\FlexMailer\ClickTracker;
12
13class TextClickTracker implements ClickTrackerInterface {
14
15 public function filterContent($msg, $mailing_id, $queue_id) {
16 return self::replaceTextUrls($msg,
17 function ($url) use ($mailing_id, $queue_id) {
18 if (strpos($url, '{') !== FALSE) {
19 return $url;
20 }
21 return \CRM_Mailing_BAO_TrackableURL::getTrackerURL($url, $mailing_id,
22 $queue_id);
23 }
24 );
25 }
26
27 /**
28 * Find any URLs and replace them.
29 *
30 * @param string $text
31 * @param callable $replace
32 * Function(string $oldUrl) => string $newUrl.
33 * @return mixed
34 * String, text.
35 */
36 public static function replaceTextUrls($text, $replace) {
37 $callback = function ($matches) use ($replace) {
38 // ex: $matches[0] == 'http://foo.com'
39 return $replace($matches[0]);
40 };
41 // Find any HTTP(S) URLs in the text.
42 // return preg_replace_callback('/\b(?:(?:https?):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i', $callback, $tex
43 return preg_replace_callback('/\b(?:(?:https?):\/\/)[-A-Z0-9+&@#\/%=~_|$?!:,.{}\[\];]*[A-Z0-9+&@#\/%=~_|${}\[\];]/i',
44 $callback, $text);
45 }
46
47}