View source
<?php
define('TYPEKIT_API_BASE_URL', 'https://typekit.com/api/v1/json/');
function typekit_api_fontyourface_info() {
return array(
'name' => 'Typekit',
'url' => 'http://typekit.com/',
);
return $info;
}
function typekit_api_fontyourface_import() {
$logging = variable_get('fontyourface_detailed_logging', FALSE);
$kits = typekit_api_get_kits();
if ($logging) {
watchdog('@font-your-face', 'Imported Typekit kits: @kits', array(
'@kits' => print_r($kits, TRUE),
), WATCHDOG_INFO);
}
foreach ($kits as $kit) {
$kit_info = typekit_api_get_kit($kit->id);
if ($logging) {
watchdog('@font-your-face', 'Imported Typekit kit info: @kit', array(
'@kit' => print_r($kit_info, TRUE),
), WATCHDOG_INFO);
}
if (typekit_api_kit_matches_domain($kit_info, $_SERVER['HTTP_HOST'])) {
foreach ($kit_info->families as $family_info) {
foreach ($family_info->variations as $variant_index => $variant_id) {
$variant = typekit_api_get_variant($family_info->id, $variant_id);
if ($logging) {
watchdog('@font-your-face', 'Imported Typekit variant info: @variant', array(
'@variant' => print_r($variant, TRUE),
), WATCHDOG_INFO);
}
$metadata = array(
'typekit_id' => $variant->id,
'variant' => $variant->font_variant,
'kit' => $kit_info->id,
);
$font = new stdClass();
$font->name = $variant->name;
$font->url = 'http://typekit.com/fonts/' . $family_info->slug . '#' . $variant_id;
$font->provider = 'typekit_api';
$font->metadata = serialize($metadata);
$font->css_family = implode(', ', $family_info->css_names);
$font->css_style = $variant->font_style;
$font->css_weight = $variant->font_weight;
$font->foundry = $variant->foundry->name;
$font->foundry_url = 'http://typekit.com/foundries/' . $variant->foundry->slug;
$font->tags = array();
fontyourface_save_font($font);
}
}
}
elseif ($logging) {
watchdog('@font-your-face', 'Typekit kit did not match current domain, @domain', array(
'@domain' => $_SERVER['HTTP_HOST'],
), WATCHDOG_INFO);
}
}
}
function typekit_api_preprocess_page(&$vars) {
if (!empty($vars['fontyourface'])) {
$kits = array();
foreach ($vars['fontyourface'] as $used_font) {
if ($used_font->provider == 'typekit_api') {
$metadata = unserialize($used_font->metadata);
$kits[$metadata['kit']] = TRUE;
}
}
if (count($kits) > 0) {
$base = 'http://use.typekit.com/';
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$base = 'https://use.typekit.com/';
}
foreach (array_keys($kits) as $kit) {
$script = '<script type="text/javascript" src="' . $base . $kit . '.js"></script>';
drupal_set_html_head($script);
$vars['head'] = drupal_get_html_head();
}
drupal_add_js('try{Typekit.load();}catch(e){}', 'inline');
$vars['scripts'] = drupal_get_js();
}
}
}
function typekit_api_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'fontyourface_settings_form') {
$form['typekit_api'] = array(
'#type' => 'fieldset',
'#title' => 'Typekit',
'#weight' => -1,
'typekit_api_token' => array(
'#type' => 'textfield',
'#title' => t('TypeKit API token'),
'#description' => t('Provided at ') . l('Typekit.com/account/tokens', 'https://typekit.com/account/tokens'),
'#default_value' => variable_get('typekit_api_token', ''),
),
'typekit_api_save' => array(
'#type' => 'submit',
'#value' => 'Update Typekit',
),
);
$form['#submit'][] = 'typekit_api_token_submit';
}
}
function typekit_api_token_submit($form, &$form_state) {
if ($form_state['clicked_button']['#parents'][0] == 'typekit_api_save') {
variable_set('typekit_api_token', $form_state['values']['typekit_api_token']);
drupal_set_message(t('Typeit API token updated.'));
}
}
function typekit_api_fontyourface_view($font, $text) {
$output = '';
$sizes = array(
32,
24,
18,
14,
12,
10,
);
foreach ($sizes as $size) {
$output .= '<div style="' . fontyourface_font_css($font) . ' font-size: ' . $size . 'px; line-height: ' . $size . 'px;">' . $text . '</div>';
}
return $output;
}
function typekit_api_fontyourface_preview($font) {
return '<span style="' . fontyourface_font_css($font) . ' font-size: 24px;">' . $font->name . '</span>';
}
function typekit_api_token_headers() {
return array(
'X-Typekit-Token' => variable_get('typekit_api_token', ''),
);
}
function typekit_api_get_kits() {
$logging = variable_get('fontyourface_detailed_logging', FALSE);
$kits = array();
$response = drupal_http_request(TYPEKIT_API_BASE_URL . 'kits', typekit_api_token_headers());
if ($response->code == 200) {
$data = json_decode($response->data);
$kits = $data->kits;
}
else {
drupal_set_message(t('There was an error importing kit list from Typekit.'), 'error');
if ($logging) {
watchdog('@font-your-face', 'Invalid drupal_http_request response: @response', array(
'@response' => print_r($response, TRUE),
), WATCHDOG_INFO);
}
}
return $kits;
}
function typekit_api_get_kit($kit_id) {
$logging = variable_get('fontyourface_detailed_logging', FALSE);
$response = drupal_http_request(TYPEKIT_API_BASE_URL . 'kits/' . $kit_id, typekit_api_token_headers());
if ($response->code == 200) {
$data = json_decode($response->data);
$kit = $data->kit;
}
else {
drupal_set_message(t('There was an error importing a kit (@kit) from Typekit.', array(
'@kit' => $kit_id,
)), 'error');
if ($logging) {
watchdog('@font-your-face', 'Invalid drupal_http_request response: @response', array(
'@response' => print_r($response, TRUE),
), WATCHDOG_INFO);
}
}
return $kit;
}
function typekit_api_kit_matches_domain($kit, $domain) {
$domain = drupal_strtolower($domain);
foreach ($kit->domains as $kit_domain) {
$kit_domain = drupal_strtolower($kit_domain);
if ($kit_domain == $domain) {
return TRUE;
}
else {
$regex = '#' . str_replace(array(
'.',
'*',
), array(
'\\.',
'.*',
), $kit_domain) . '#';
if (preg_match($regex, $domain)) {
return TRUE;
}
}
}
return FALSE;
}
function typekit_api_get_variant($family_id, $variant_id) {
$logging = variable_get('fontyourface_detailed_logging', FALSE);
$response = drupal_http_request(TYPEKIT_API_BASE_URL . 'families/' . $family_id . '/' . $variant_id, typekit_api_token_headers());
if ($response->code == 200) {
$data = json_decode($response->data);
$variant = $data->variation;
}
else {
drupal_set_message(t('There was an error importing a variant (@kit, @variant) from Typekit.', array(
'@kit' => $kit_id,
'@variant' => $variant_id,
)), 'error');
if ($logging) {
watchdog('@font-your-face', 'Invalid drupal_http_request response: @response', array(
'@response' => print_r($response, TRUE),
), WATCHDOG_INFO);
}
}
return $variant;
}