function advagg_relocate_get_remote_font_data in Advanced CSS/JS Aggregation 7.2
Gets external CSS files; caches it and returns css font rules.
Parameters
string $url: URL of the CSS file to import.
array $aggregate_settings: Array of settings.
Return value
array Array of font data.
2 calls to advagg_relocate_get_remote_font_data()
- advagg_relocate_css_alter in advagg_relocate/
advagg_relocate.module - Implements hook_css_alter().
- _advagg_relocate_callback in advagg_relocate/
advagg_relocate.advagg.inc - Gets external CSS files and puts the contents of it in the aggregate.
File
- advagg_relocate/
advagg_relocate.advagg.inc, line 691 - Advanced aggregation relocate module.
Code
function advagg_relocate_get_remote_font_data($url, array $aggregate_settings) {
// Set default settings if needed.
$font_type_defaults = array(
'woff2' => 'woff2',
'woff' => 'woff',
'ttf' => 'ttf',
);
if (!isset($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers'])) {
$aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers'] = variable_get('advagg_relocate_css_inline_import_browsers', $font_type_defaults);
}
// Make sure advagg_relocate_css_inline_import_browsers is an array.
if (!is_array($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers'])) {
$aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers'] = array(
$aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers'] => $aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers'],
);
}
// Use defaults if no matches for known font types.
if (!isset($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']['woff2']) && !isset($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']['woff']) && !isset($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']['ttf']) && !isset($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']['eot']) && !isset($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']['svg'])) {
$aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers'] += $font_type_defaults;
}
// Set arguments for drupal_http_request().
$options = array(
'headers' => array(
'Accept-Encoding' => 'gzip, deflate',
'Connection' => 'close',
'Referer' => $GLOBALS['base_root'] . request_uri(),
),
'timeout' => 8,
'version' => '1.0',
);
if (function_exists('brotli_uncompress')) {
$options['headers']['Accept-Encoding'] .= ', br';
}
// If protocol relative, force https.
if (strpos($url, '//') === 0) {
$url = advagg_force_https_path($url);
}
// Build CID.
$aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers'] = array_filter($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']);
$fonts = implode(',', $aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']);
$cid = "advagg_relocate_css_inline_import:{$fonts}:{$url}";
// Try local cache.
$cached_data = cache_get($cid, 'cache_advagg_info');
if (!empty($cached_data->data[0])) {
if ($cached_data->expire >= REQUEST_TIME) {
return $cached_data->data[0];
}
else {
// Set header for 304 response.
// $options['headers']['If-None-Match'] = $response->headers['etag'];.
$options['headers']['If-Modified-Since'] = gmdate('D, d M Y H:i:s T', $cached_data->created);
}
}
// Get external data.
$responses = array();
if (module_exists('httprl')) {
// Get ttf.
if (!empty($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']['ttf'])) {
$options['#font-type'] = 'ttf';
httprl_request($url . '#ttf', $options);
}
// Get eot.
if (!empty($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']['eot'])) {
$options['#font-type'] = 'eot';
$options['headers']['User-Agent'] = 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)';
httprl_request($url . '#eot', $options);
}
// Get svg.
if (!empty($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']['svg'])) {
$options['#font-type'] = 'svg';
$options['headers']['User-Agent'] = 'Mozilla/5.0 (iPad; U; CPU OS 3_2_2 like Mac OS X; nl-nl) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B500 Safari/531.21.10';
httprl_request($url . '#svg', $options);
}
// Get woff.
if (!empty($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']['woff'])) {
$options['#font-type'] = 'woff';
$options['headers']['User-Agent'] = 'Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)';
httprl_request($url . '#woff', $options);
}
// Get woff2.
if (!empty($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']['woff2'])) {
$options['#font-type'] = 'woff2';
$options['headers']['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1';
httprl_request($url . '#woff2', $options);
}
$responses = httprl_send_request();
}
if (empty($responses)) {
// Get ttf.
if (!empty($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']['ttf'])) {
$options['#font-type'] = 'ttf';
$responses['ttf'] = drupal_http_request($url . '#ttf', $options);
if (!isset($responses['ttf']->options)) {
$responses['ttf']->options = $options;
}
if (!isset($responses[$url]->url)) {
$responses['ttf']->url = $url . '#ttf';
}
}
// Get eot.
if (!empty($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']['eot'])) {
$options['#font-type'] = 'eot';
$options['headers']['User-Agent'] = 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)';
$responses['eot'] = drupal_http_request($url . '#eot', $options);
if (!isset($responses['eot']->options)) {
$responses['eot']->options = $options;
}
if (!isset($responses[$url]->url)) {
$responses['eot']->url = $url . '#eot';
}
}
// Get svg.
if (!empty($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']['svg'])) {
$options['#font-type'] = 'svg';
$options['headers']['User-Agent'] = 'Mozilla/5.0 (iPad; U; CPU OS 3_2_2 like Mac OS X; nl-nl) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B500 Safari/531.21.10';
$responses['svg'] = drupal_http_request($url . '#svg', $options);
if (!isset($responses['svg']->options)) {
$responses['svg']->options = $options;
}
if (!isset($responses[$url]->url)) {
$responses['svg']->url = $url . '#svg';
}
}
// Get woff.
if (!empty($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']['woff'])) {
$options['#font-type'] = 'woff';
$options['headers']['User-Agent'] = 'Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)';
$responses['woff'] = drupal_http_request($url . '#woff', $options);
if (!isset($responses['woff']->options)) {
$responses['woff']->options = $options;
}
if (!isset($responses[$url]->url)) {
$responses['woff']->url = $url . '#woff';
}
}
// Get woff2.
if (!empty($aggregate_settings['variables']['advagg_relocate_css_inline_import_browsers']['woff2'])) {
$options['#font-type'] = 'woff2';
$options['headers']['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1';
$responses['woff2'] = drupal_http_request($url . '#woff2', $options);
if (!isset($responses['woff2']->options)) {
$responses['woff2']->options = $options;
}
if (!isset($responses[$url]->url)) {
$responses['woff2']->url = $url . '#woff2';
}
}
}
// Try failures again.
advagg_relocate_try_failures_again($responses);
// Parse data.
$font_faces = array();
$ttl = 0;
foreach ($responses as $key => $response) {
if ($response->code == 304 && !empty($cached_data->data[0])) {
// This might need to be better handled in the future.
return $cached_data->data[0];
}
// Set the font type if not set.
if (empty($response->options['#font-type'])) {
if (!is_numeric($key)) {
$response->options['#font-type'] = $key;
}
else {
continue;
}
}
if ($response->code != 200 && $response->code != 201 && $response->code != 202 && $response->code != 206) {
return array();
}
if (empty($response->data)) {
return array();
}
advagg_relocate_process_http_request($response, 'font');
$ttl = max($ttl, $response->ttl);
// Parse the CSS.
$font_face = advagg_relocate_parse_css_font_face($response->data, array(
'font-family',
'font-style',
'font-weight',
'src',
), $response->options['#font-type']);
// Format into a better data structure and combine.
foreach ($font_face as $k => $values) {
if (!isset($font_faces[$k])) {
$font_faces[$k] = $font_face[$k];
continue;
}
foreach ($values as $index => $value) {
if (!in_array($value, $font_faces[$k])) {
if ($index === $response->options['#font-type']) {
$font_faces[$k][$index] = $values[$index];
}
else {
$font_faces[$k][] = $values[$index];
}
}
}
}
}
// Save data to the cache.
if (!empty($font_faces)) {
cache_set($cid, array(
$font_faces,
$responses,
), 'cache_advagg_info', REQUEST_TIME + $ttl);
}
return $font_faces;
}