-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
executable file
·64 lines (57 loc) · 2.11 KB
/
plugin.php
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
<?php
/*
Plugin Name: Referrer Plugin
Plugin URI: http://github.com/IWAtech/yourls-referrer-plugin
Description: Takes care of a few custom requirements (including campaign tracking based on referrer) of a project
Version: 1.0
Author: kl4n4
Author URI: http://github.com/kl4n4
*/
// No direct call
if( !defined( 'YOURLS_ABSPATH' ) ) die();
define('KEYWORD_REGEX', "/(at|de|us)-([a-z0-9-]{8})/i");
define('LOCATION_REGEX', "/updatemi.com/i");
define('REFERRER_REGEX', "/(facebook).com|(twitter).com/i");
$requested_locale = null;
$tracking_campaign = null;
function referrer_get_locale_string($locale_short) {
$country_code = strtoupper($locale_short);
if(in_array($country_code, ['AT', 'DE'])) {
return 'de_' . $country_code;
} elseif(in_array($country_code, ['US'])) {
return 'en_' . $country_code;
}
return null;
}
function referrer_sanitize_string( $valid, $string ) {
global $requested_locale;
if(preg_match(KEYWORD_REGEX, $valid, $matches) > 0 && count($matches) >= 3) {
$requested_locale = referrer_get_locale_string($matches[1]);
return $matches[2];
}
return $valid;
}
function referrer_redirect_location( $location, $code ) {
global $requested_locale, $tracking_campaign;
if(strpos($location, 'updatemi.com/') !== false) {
if($_SERVER['HTTP_REFERER'] && preg_match(REFERRER_REGEX, $_SERVER['HTTP_REFERER'], $matches) > 0) {
if(array_key_exists(1, $matches) && !empty($matches[1])) {
$tracking_campaign = $matches[1];
}
}
$query_params = array(
'pk_campaign' => 'share'
);
if($tracking_campaign) {
$query_params['pk_kwd'] = $tracking_campaign;
}
if($requested_locale) {
$query_params['locale'] = $requested_locale;
}
$query_clue = strpos($location, '?') === false ? '?' : '&';
return $location . $query_clue . http_build_query($query_params);
}
return $location;
}
yourls_add_filter( 'sanitize_string', 'referrer_sanitize_string' );
yourls_add_filter( 'redirect_location', 'referrer_redirect_location' );