Drupal 7 create custom token

In one of my recent tasks I had to create custom token to show current page url inside ads targeting. I will share my code.

**
 * Implements hook_token_info().
 */
function yourmodule_token_info() {
  // Define new token type
  $info['types']['custom_ads'] = array(
    'name' => t('Custom ads'),
    'description' => t('Tokens related to ads implementation.'),
  );
  $info['tokens']['custom_ads']['curl'] = array(
    'name' => t('Custom ads curl'),
    'description' => t('Custom ads current page url.'),
  );

  return $info;
}

/**
 * Implements hook_tokens().
 */
function yourmodule_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

  if ($type == 'custom_ads') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'curl':
          global $base_url;
          $current_url = yourmodule_encode_targeting($base_url . request_uri());
          $replacements[$original] = $current_url;
          break;
      }
    }
  }

  return $replacements;
}

/**
 * GPT requires targeting parameters to escape keys and values
 * https://support.google.com/dfp_premium/answer/1697712?hl=en
 */
function yourmodule_encode_targeting($url) {
  $encoded_url = rawurlencode($url);
  
  $exceptions = array(
    '-' => '%2D',
    '_' => '%5F',
    '.' => '%2E',
    '~' => '%7E',
  );
  
  foreach($exceptions as $key => $value) {
    $encoded_url = str_replace($key, $value, $encoded_url);
  }
  
  return $encoded_url;
}