Implements hook_page_alter().

Fichier

advagg_mod/advagg_mod.module, line 594

Code

function advagg_mod_page_alter() {
    // Skip if advagg is disabled.
    if (!advagg_enabled()) {
        return;
    }
    // Return early if this setting is disabled.
    list(, , , , , , , , , , $css_defer) = advagg_mod_get_lists();
    if (empty($css_defer)) {
        return;
    }
    if (variable_get('advagg_mod_css_defer_visibility', ADVAGG_MOD_VISIBILITY_LISTED) != ADVAGG_MOD_VISIBILITY_FILE_CONTROLLED) {
        return;
    }
    // Get critical css file.
    list(, , $inline_strings) = advagg_mod_find_critical_css_file();
    $preload_from_inline_css = array();
    $domains_from_inline_css = array();
    // Add inline critical css for front page.
    if (!empty($inline_strings[0])) {
        // Extract url() references to add to the preloaded links.
        $matches = array();
        // Match url ( "' ... '" ).
        $pattern = '/url\\s*\\(\\s*[\'"]?(.+?)[\'"]?\\s*\\)/i';
        preg_match_all($pattern, $inline_strings[0], $matches);
        if (!empty($matches[1])) {
            foreach ($matches[1] as $key => $url) {
                $parsed = parse_url($url);
                // Remove data URIs.
                if (!empty($parsed['scheme']) && $parsed['scheme'] === 'data') {
                    unset($matches[1][$key]);
                    continue;
                }
                // Remote paths without a period.
                if (empty($parsed['path']) || strpos($parsed['path'], '.') === FALSE) {
                    unset($matches[1][$key]);
                    continue;
                }
                if (isset($parsed['host'])) {
                    $domains_from_inline_css[] = $url;
                }
            }
            $preload_from_inline_css = $matches[1];
        }
        // Add critical css.
        drupal_add_css('advagg_mod_critical_css', array(
            'data' => $inline_strings[0],
            'type' => 'inline',
            'group' => CSS_SYSTEM - 1,
            'weight' => -50000,
            'movable' => FALSE,
            'critical-css' => TRUE,
        ));
        // Add critical css js loader.
        advagg_mod_add_loadcss_js_lib();
    }
    // Add in domain prefetch.
    $domains = array();
    if (!empty($inline_strings[1])) {
        $domains = preg_split("/\\r\\n|\\r|\\n/", $inline_strings[1]);
    }
    $domains = array_merge($domains, $domains_from_inline_css);
    // Remove duplicates and empty sets.
    $domains = array_filter(array_unique($domains));
    if (!empty($domains)) {
        foreach ($domains as $domain) {
            advagg_add_dns_prefetch(trim($domain));
        }
    }
    // Add in files to preload.
    $preload = array();
    if (!empty($inline_strings[2])) {
        $preload = preg_split("/\\r\\n|\\r|\\n/", $inline_strings[2]);
    }
    $preload = array_merge($preload, $preload_from_inline_css);
    // Remove duplicates and empty sets.
    $preload = array_filter(array_unique($preload));
    if (!empty($preload)) {
        $preload_array = array();
        $counter = 0;
        foreach ($preload as $value) {
            if (empty($value)) {
                $counter++;
                continue;
            }
            if (stripos($value, 'as: ') === 0) {
                $preload_array[$counter]['as'] = trim(substr($value, 4));
            }
            elseif (stripos($value, 'type: ') === 0) {
                $preload_array[$counter]['type'] = trim(substr($value, 6));
            }
            elseif (stripos($value, 'media: ') === 0) {
                $preload_array[$counter]['media'] = trim(substr($value, 7));
            }
            elseif (stripos($value, 'crossorigin: ') === 0) {
                $preload_array[$counter]['crossorigin'] = trim(substr($value, 13));
            }
            elseif (stripos($value, 'url: ') === 0) {
                if (!empty($preload_array[$counter]['url'])) {
                    $counter++;
                }
                $preload_array[$counter]['url'] = trim(substr($value, 4));
            }
            else {
                if (!empty($preload_array[$counter]['url'])) {
                    $counter++;
                }
                $preload_array[$counter]['url'] = trim($value);
            }
        }
        foreach ($preload_array as $values) {
            // Skip if url is not set.
            if (empty($values['url'])) {
                continue;
            }
            $url = $values['url'];
            $media = '';
            if (!empty($values['media'])) {
                $media = $values['media'];
            }
            $as = '';
            if (!empty($values['as'])) {
                $as = $values['as'];
            }
            $type = '';
            if (!empty($values['type'])) {
                $type = $values['type'];
            }
            $crossorigin = NULL;
            if (!empty($values['crossorigin'])) {
                $crossorigin = $values['crossorigin'];
            }
            advagg_add_preload_link($url, $media, $as, $type, $crossorigin);
        }
    }
}