Newer
Older
1
2
3
4
5
6
7
8
9
10
11
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
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* CiviCRM Mailing_Hooks class.
*
* @since 0.1
*/
namespace CiviCRM_WP_REST\Civi;
class Mailing_Hooks {
/**
* Mailing Url endpoint.
*
* @since 0.1
* @var string
*/
public $url_endpoint;
/**
* Mailing Open endpoint.
*
* @since 0.1
* @var string
*/
public $open_endpoint;
/**
* Constructor.
*
* @since 0.1
*/
public function __construct() {
$this->url_endpoint = rest_url( 'civicrm/v3/url' );
$this->open_endpoint = rest_url( 'civicrm/v3/open' );
}
/**
* Register hooks.
*
* @since 0.1
*/
public function register_hooks() {
add_filter( 'civicrm_alterMailParams', [ $this, 'do_mailing_urls' ], 10, 2 );
}
/**
* 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 ( $context == 'civimail' ) {
$params['html'] = $this->replace_html_mailing_tracking_urls( $params['html'] );
$params['text'] = $this->replace_text_mailing_tracking_urls( $params['text'] );
}
return $params;
}
/**
* Replace html mailing tracking urls.
*
* @since 0.1
* @param string $contnet The mailing content
* @return string $content The mailing content
*/
public function replace_html_mailing_tracking_urls( string $content ) {
$doc = \phpQuery::newDocument( $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 $contnet 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 );
return $content;
}
}