Newer
Older
<?php
/**
* CiviCRM Mailing_Hooks class.
*
* @since 0.1
*/
namespace CiviCRM_WP_REST\Civi;
class Mailing_Hooks {
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* @var string
* Mailing Url endpoint.
* @since 0.1
*/
public $url_endpoint;
/**
* @var string
* Mailing Open endpoint.
* @since 0.1
*/
public $open_endpoint;
/**
* @var array
* The parsed WordPress REST url.
* @since 1.0
*/
public $parsed_rest_url;
/**
* Constructor.
*
* @since 0.1
*/
public function __construct() {
$this->url_endpoint = rest_url('civicrm/v3/url');
$this->open_endpoint = rest_url('civicrm/v3/open');
$this->parsed_rest_url = parse_url(rest_url());
}
/**
* Register hooks.
*
* @since 0.1
*/
public function register_hooks() {
add_filter('civicrm_alterMailParams', [$this, 'do_mailing_urls'], 10, 2);
add_filter('civicrm_alterExternUrl', [$this, 'alter_mailing_extern_urls'], 10, 6);
}
/**
* Replaces the open, and click
* tracking URLs for a mailing (CiviMail)
* with thier REST counterparts.
*
* @uses 'civicrm_alterExternUrl' filter
*
* @param \GuzzleHttp\Psr7\Uri $url
* @param string|NULL $path
* @param string|NULL $query
* @param string|NULL $fragment
* @param bool|NULL $absolute
* @param bool|NULL $isSSL
*/
public function alter_mailing_extern_urls(&$url, $path, $query, $fragment, $absolute, $isSSL) {
if ($path == 'extern/url') {
$url = $url
->withHost($this->parsed_rest_url['host'])
->withQuery($query)
->withPath("{$this->parsed_rest_url['path']}civicrm/v3/url");
}
if ($path == 'extern/open') {
$url = $url
->withHost($this->parsed_rest_url['host'])
->withQuery($query)
->withPath("{$this->parsed_rest_url['path']}civicrm/v3/open");
}
}
/**
* Filters the mailing html and replaces calls to 'extern/url.php' and
* 'extern/open.php' with their REST counterparts 'civicrm/v3/url' and 'civicrm/v3/open'.
*
* @uses 'civicrm_alterMailParams'
*
* @since 0.1
* @param array &$params Mail params
* @param string $context The Context
* @return array $params The filtered Mail params
*/
public function do_mailing_urls(&$params, $context) {
if (in_array($context, ['civimail', 'flexmailer'])) {
if (!empty($params['html'])) {
$params['html'] = $this->is_mail_tracking_url_alterable($params['html'])
? $this->replace_html_mailing_tracking_urls($params['html'])
: $params['html'];
}
if (!empty($params['text'])) {
$params['text'] = $this->is_mail_tracking_url_alterable($params['text'])
? $this->replace_text_mailing_tracking_urls($params['text'])
: $params['text'];
}
}
return $params;
}
/**
* Replace html mailing tracking urls.
*
* @since 0.1
* @param string $content The mailing content
* @return string $content The mailing content
*/
public function replace_html_mailing_tracking_urls(string $content) {
foreach ($doc['[href*="civicrm/extern/url.php"], [src*="civicrm/extern/open.php"]'] as $element) {
$href = pq($element)->attr('href');
$src = pq($element)->attr('src');
// replace extern/url
if (strpos($href, 'civicrm/extern/url.php')) {
$query_string = strstr($href, '?');
pq($element)->attr('href', $this->url_endpoint . $query_string);
}
// replace extern/open
if (strpos($src, 'civicrm/extern/open.php')) {
$query_string = strstr($src, '?');
pq($element)->attr('src', $this->open_endpoint . $query_string);
}
unset($href, $src, $query_string);
}
return $doc->html();
}
/**
* Replace text mailing tracking urls.
*
* @since 0.1
* @param string $content The mailing content
* @return string $content The mailing content
*/
public function replace_text_mailing_tracking_urls(string $content) {
// replace extern url
$content = preg_replace('/http.*civicrm\/extern\/url\.php/i', $this->url_endpoint, $content);
// replace open url
$content = preg_replace('/http.*civicrm\/extern\/open\.php/i', $this->open_endpoint, $content);
/**
* Checks whether for a given mail
* content (text or html) the tracking URLs
* are alterable/need to be altered.
*
* @since 0.1
* @param string $content The mail content (text or html)
* @return bool $is_alterable
*/
public function is_mail_tracking_url_alterable($content) {
return strpos($content, 'civicrm/extern/url.php') || strpos($content, 'civicrm/extern/open.php');
}