Add referral id URL decorator

[MAILPOET-2182]
This commit is contained in:
Rostislav Wolny
2019-08-06 15:09:13 +02:00
committed by M. Shull
parent 42df4e5d97
commit a23bc07349
2 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,30 @@
<?php
namespace MailPoet\Referrals;
use MailPoet\Settings\SettingsController;
use MailPoet\WP\Functions as WPFunctions;
if (!defined('ABSPATH')) exit;
class UrlDecorator {
/** @var WPFunctions */
private $wp;
/** @var SettingsController */
private $settings;
function __construct(WPFunctions $wp, SettingsController $settings) {
$this->wp = $wp;
$this->settings = $settings;
}
function decorate($url) {
$referral_id = $this->settings->get(ReferralDetector::REFERRAL_SETTING_NAME, null);
if ($referral_id === null) {
return $url;
}
return $this->wp->addQueryArg('ref', $referral_id, $url);
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace MailPoet\Test\Router;
use MailPoet\Referrals\ReferralDetector;
use MailPoet\Referrals\UrlDecorator;
use MailPoet\Settings\SettingsController;
use MailPoet\WP\Functions as WPFunctions;
class UrlDecoratorTest extends \MailPoetTest {
/** @var SettingsController */
private $settings;
/** @var UrlDecorator */
private $url_decorator;
function _before() {
parent::_before();
$this->settings = new SettingsController();
$this->url_decorator = new UrlDecorator(WPFunctions::get(), $this->settings);
}
function testItDoesntDoAnythingWhenNoReferralId() {
$this->settings->set(ReferralDetector::REFERRAL_SETTING_NAME, null);
$url = 'http://example.com';
expect($this->url_decorator->decorate($url))->equals($url);
}
function testItCorrectlyAddsReferralId() {
$this->settings->set(ReferralDetector::REFERRAL_SETTING_NAME, 'abcdefgh');
expect($this->url_decorator->decorate('http://example.com/'))
->equals('http://example.com/?ref=abcdefgh');
expect($this->url_decorator->decorate('http://example.com/?param=value'))
->equals('http://example.com/?param=value&ref=abcdefgh');
expect($this->url_decorator->decorate('http://example.com/?param=value#hash/?param=val'))
->equals('http://example.com/?param=value&ref=abcdefgh#hash/?param=val');
}
}