Skip to content
Snippets Groups Projects
Verified Commit 02c52430 authored by Andrei Mondoc's avatar Andrei Mondoc Committed by Kevin Cristiano
Browse files

add suport for 'civicrm_alterExternUrl' hook

parent 3c06f1c0
No related branches found
No related tags found
No related merge requests found
......@@ -25,6 +25,14 @@ class Mailing_Hooks {
*/
public $open_endpoint;
/**
* The parsed WordPress REST url.
*
* @since 1.0
* @var array
*/
public $parsed_rest_url;
/**
* Constructor.
*
......@@ -36,6 +44,8 @@ class Mailing_Hooks {
$this->open_endpoint = rest_url( 'civicrm/v3/open' );
$this->parsed_rest_url = parse_url( rest_url() );
}
/**
......@@ -47,6 +57,38 @@ class Mailing_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'] )
->withPath( "{$this->parsed_rest_url['path']}civicrm/v3/url" );
}
if ( $path == 'extern/open' ) {
$url = $url
->withHost( $this->parsed_rest_url['host'] )
->withPath( "{$this->parsed_rest_url['path']}civicrm/v3/open" );
}
}
/**
......@@ -62,11 +104,15 @@ class Mailing_Hooks {
*/
public function do_mailing_urls( &$params, $context ) {
if ( $context == 'civimail' ) {
if ( in_array( $context, [ 'civimail', 'flexmailer' ] ) ) {
$params['html'] = $this->replace_html_mailing_tracking_urls( $params['html'] );
$params['html'] = $this->is_mail_tracking_url_alterable( $params['html'] )
? $this->replace_html_mailing_tracking_urls( $params['html'] )
: $params['html'];
$params['text'] = $this->replace_text_mailing_tracking_urls( $params['text'] );
$params['text'] = $this->is_mail_tracking_url_alterable( $params['text'] )
? $this->replace_text_mailing_tracking_urls( $params['text'] )
: $params['text'];
}
......@@ -133,4 +179,19 @@ class Mailing_Hooks {
}
/**
* 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( string $content ) {
return strpos( $content, 'civicrm/extern/url.php' ) || strpos( $content, 'civicrm/extern/open.php' );
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment