You are here

function resource_hints_init in Resource Hints 7.2

Same name and namespace in other branches
  1. 7 resource_hints.module \resource_hints_init()

Implements hook_init().

File

./resource_hints.module, line 45
Modules hooks for the resource hints module.

Code

function resource_hints_init() {
  $dns_prefetch_control = variable_get('resource_hints_dns_prefetch_control', RESOURCE_HINTS_DNS_PREFETCH_ENABLED);
  $dns_prefetch_control = check_plain($dns_prefetch_control);
  $dns_prefetch_output = variable_get("resource_hints_dns_prefetch_output", RESOURCE_HINTS_OUTPUT_LINK_HEADER);
  if ($dns_prefetch_output === RESOURCE_HINTS_OUTPUT_LINK_HEADER) {
    drupal_add_http_header('X-DNS-Prefetch-Control', $dns_prefetch_control);
  }
  else {
    drupal_add_html_head(array(
      '#tag' => 'meta',
      '#attributes' => array(
        'http-equiv' => 'x-dns-prefetch-control',
        'content' => $dns_prefetch_control,
      ),
    ), __FUNCTION__);
  }
  if ($dns_prefetch_control === RESOURCE_HINTS_DNS_PREFETCH_ENABLED) {
    foreach (explode(PHP_EOL, variable_get('resource_hints_dns_prefetch_resources', '')) as $value) {
      $value = drupal_strip_dangerous_protocols(trim($value));
      if ($value) {
        if ($dns_prefetch_output === RESOURCE_HINTS_OUTPUT_LINK_HEADER) {
          drupal_add_http_header('Link', "<{$value}>; rel=\"dns-prefetch\"", TRUE);
        }
        else {
          drupal_add_html_head_link(array(
            'rel' => 'dns-prefetch',
            'href' => $value,
          ), FALSE);
        }
      }
    }
  }
  $hint_types = array(
    'preconnect' => 'resource_hints_preconnect',
    'prefetch' => 'resource_hints_prefetch',
    'prerender' => 'resource_hints_prerender',
  );
  foreach ($hint_types as $rel => $variable_prefix) {
    foreach (explode(PHP_EOL, variable_get("{$variable_prefix}_resources", '')) as $value) {
      $value = check_url(trim($value));
      if ($value) {
        $output = variable_get("{$variable_prefix}_output", RESOURCE_HINTS_OUTPUT_LINK_HEADER);
        if ($output === RESOURCE_HINTS_OUTPUT_LINK_HEADER) {
          drupal_add_http_header('Link', "<{$value}>; rel=\"{$rel}\"", TRUE);
        }
        else {
          drupal_add_html_head_link(array(
            'rel' => $rel,
            'href' => $value,
          ), FALSE);
        }
      }
    }
  }
}