Simplenews Drupal 7 Module
Simplenews Change Un-subscribe URL to Shorten bitly URL
/**
* Implements hook_tokens_alter()
*/
function my_module_tokens_alter(&$replacements, $context) {
if (isset($context['type']) && $context['type'] == 'simplenews-subscriber') {
if (isset($replacements['[simplenews-subscriber:subscribe-url]'])) {
$subscribe_url = $replacements['[simplenews-subscriber:subscribe-url]'];
$subscription_short = my_module_get_bitly_url($subscribe_url);
if (valid_url($subscription_short)) {
$replacements['[simplenews-subscriber:subscribe-url]'] = $subscription_short;
}
}
if (isset($replacements['[simplenews-subscriber:unsubscribe-url]'])) {
$unsubscribe_url = $replacements['[simplenews-subscriber:unsubscribe-url]'];
$unsubscription_short = my_module_get_bitly_url($unsubscribe_url);
if (valid_url($unsubscription_short)) {
$replacements['[simplenews-subscriber:unsubscribe-url]'] = $unsubscription_short;
}
}
}
}
/**
* Helper function to convert site URL to shorten bitly URL
*/
function my_module_get_bitly_url($url) {
$login = MD_SIMPLENEWS_BITLY_USER;
$appkey = MD_SIMPLENEWS_BITLY_APIKEY;
$format = 'json';
$version = '2.0.1';
$bitly = 'http://api.bit.ly/shorten?version=' . $version . '&longUrl=' . urlencode($url) . '&login='. $login . '&apiKey=' . $appkey . '&format=' . $format;
$response = file_get_contents($bitly);
if (strtolower($format) == 'json') {
$json = @json_decode($response,true);
return $json['results'][$url]['shortUrl'];
}
else {
$xml = simplexml_load_string($response);
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
Just To Alter the Token Values :
function singpost_common_tokens_alter(&$replacements, $context) {
if (isset($context['type']) && $context['type'] == 'simplenews-subscriber') {
if (isset($replacements['[simplenews-subscriber:unsubscribe-url]'])) {
$str_host = ($_SERVER['HTTP_HOST']=='default')?gethostname():$_SERVER['HTTP_HOST'];
$unsubscribe_url = $replacements['[simplenews-subscriber:unsubscribe-url]'];
$unsubscribe_url = str_replace("http://default/", 'http://'.$str_host.'/', $unsubscribe_url);
if (isset($unsubscribe_url) && $unsubscribe_url!='') {
$replacements['[simplenews-subscriber:unsubscribe-url]'] = $unsubscribe_url;
}
}
}
}
Add new comment