You are here

function _simplenews_statistics_replace_url in Simplenews Statistics 7

Same name and namespace in other branches
  1. 6.3 simplenews_statistics.module \_simplenews_statistics_replace_url()
  2. 6 simplenews_statistics.module \_simplenews_statistics_replace_url()
  3. 6.2 simplenews_statistics.module \_simplenews_statistics_replace_url()
  4. 7.2 simplenews_statistics.module \_simplenews_statistics_replace_url()

Alter link to go through statistics.

1 call to _simplenews_statistics_replace_url()
_simplenews_statistics_parse_links in ./simplenews_statistics.module
Helper function to parse links in the body.

File

./simplenews_statistics.module, line 280
Main simplenews statistics file.

Code

function _simplenews_statistics_replace_url($url, $nid, $snid) {

  // Do not replace anchor links.
  $fragment_position = substr($url, 0, 1);
  if ($fragment_position == '#') {
    return $url;
  }

  // Do not replace 'mailto:' links unless it is configured.
  $track_mailto = variable_get('simplenews_statistics_track_mailto', 1);
  if ($track_mailto == 0) {
    if (substr($url, 0, 7) == 'mailto:') {
      return $url;
    }
  }

  // Do not replace unsubscribe links.
  if (strpos($url, '/newsletter/confirm/remove/') !== FALSE) {
    return $url;
  }

  // Do not replace links that should be excluded.
  $exclude = variable_get('simplenews_statistics_exclude', '');
  if ($exclude && drupal_match_path($url, $exclude)) {
    return $url;
  }

  // Get the url record from the database. Uses Drupal's static caching if
  // available. Create a new record in database and cache if there isn't one.
  $url_record = simplenews_statistics_get_url($url, $nid);
  if ($url_record == FALSE) {
    $url_record = simplenews_statistics_set_url($url, $nid);
  }
  $urlid = $url_record->urlid;

  // Call possible encoders for urlid & snid in modules implementing the hook.
  $hook = 'simplenews_statistics_encode';
  foreach (module_implements($hook) as $module) {
    $function = $module . '_' . $hook;
    if (function_exists($function)) {
      $urlid = $function($urlid, 'urlid');
      $snid = $function($snid, 'snid');
    }
  }
  return url('track/click/' . $urlid . '/' . $snid, array(
    'absolute' => TRUE,
  ));
}