View source
<?php
define('TYPEKIT_API_BASE_URL', 'https://typekit.com/api/v1/json/');
function typekit_api_menu() {
$items = array();
$items['admin/build/themes/fontyourface/typekit/import'] = array(
'title' => 'Typekit Import',
'page callback' => 'typekit_api_import',
'access arguments' => array(
'administer @font-your-face',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function typekit_api_import() {
typekit_api_cron();
drupal_goto($_GET['destination']);
}
function typekit_api_cron() {
$last_updated = variable_get('typekit_api_last_update', 0);
if (time() - 60 * 60 * 24 > $last_updated) {
$kits = typekit_api_get_kits();
foreach ($kits as $kit) {
$kit_info = typekit_api_get_kit($kit->id);
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);
$variant_record = new stdClass();
$variant_record->typekit_id = $variant->id;
$variant_record->name = $variant->name;
$variant_record->css_name = $family_info->css_names[$variant_index];
$variant_record->style = $variant->font_style;
$variant_record->variant = $variant->font_variant;
$variant_record->weight = $variant->font_weight;
$variant_record->foundry = $variant->foundry->name;
$variant_record->family = $variant->family->name;
$variant_record->kit = $kit_info->id;
$results = db_query("SELECT vid FROM {typekit_api_variant} WHERE typekit_id = '%s'", $variant_record->typekit_id);
if ($result = db_fetch_object($results)) {
$variant_record->vid = $result->vid;
drupal_write_record('typekit_api_variant', $variant_record, array(
'vid',
));
}
else {
drupal_write_record('typekit_api_variant', $variant_record);
}
}
}
}
}
variable_set('typekit_api_last_update', time());
}
}
function typekit_api_preprocess_page(&$vars) {
if (!empty($vars['fontyourface'])) {
$links = array();
$fonts = typekit_api_list(FALSE);
$kits = array();
foreach ($vars['fontyourface'] as $used_font) {
if ($used_font->provider == 'typekit_api') {
$kits[$fonts[$used_font->group_name]['fonts'][$used_font->name]['kit']] = TRUE;
}
}
if (count($kits) > 0) {
foreach (array_keys($kits) as $kit) {
$script = '<script type="text/javascript" src="http://use.typekit.com/' . $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', ''),
),
);
}
}
function typekit_api_fontyourface_info() {
$info = array(
'typekit_api' => array(
'name' => 'Typekit',
'url' => 'http://typekit.com/',
'fonts' => typekit_api_list(),
),
);
return $info;
}
function typekit_api_list($show_error = TRUE) {
$fonts = array();
$results = db_query('SELECT * FROM {typekit_api_variant}');
while ($result = db_fetch_object($results)) {
list($family, $variant) = explode(':', $result->typekit_id);
if (!isset($fonts[$result->family])) {
$fonts[$result->family] = array(
'path' => urlencode($family),
'fonts' => array(),
);
}
$fonts[$result->family]['fonts'][$result->name] = array(
'name' => $result->name,
'path' => urlencode($variant),
'css_name' => $result->css_name,
'style' => $result->style,
'variant' => $result->variant,
'weight' => $result->weight,
'foundry' => $result->foundry,
'kit' => $result->kit,
);
}
if (count($fonts) == 0 && $show_error) {
drupal_set_message(t('No Typekit fonts have been imported yet. !importlink or wait until the next cron run. Note: make sure your Typekit kits are setup to use on this domain and include whatever fonts you would like to use.', array(
'!importlink' => l(t('Import now'), 'admin/build/themes/fontyourface/typekit/import', array(
'query' => drupal_get_destination(),
)),
)));
}
return $fonts;
}
function typekit_api_fontyourface_css($used_font) {
$css = array(
'@font-face' => '',
'font-family' => '',
'font-weight' => '',
'font-style' => '',
'font-variant' => '',
);
$list = typekit_api_list(FALSE);
$font = $list[$used_font->group_name]['fonts'][$used_font->name];
$css['font-family'] = $font['css_name'];
$css['font-weight'] = $font['weight'];
$css['font-style'] = $font['style'];
$css['font-variant'] = $font['variant'];
return $css;
}
function typekit_api_fontyourface_view($font, $text) {
$output = '';
$sizes = array(
32,
24,
18,
14,
12,
10,
);
foreach ($sizes as $size) {
$output .= '<div style="' . typekit_api_font_css($font) . ' font-size: ' . $size . 'px; line-height: ' . $size . 'px;">' . $text . '</div>';
}
return $output;
}
function typekit_api_fontyourface_preview($font) {
return '<span style="' . typekit_api_font_css($font) . ' font- font-size: 24px;">' . $font['name'] . '</span>';
}
function typekit_api_font_css($font) {
return 'font-family: ' . $font['css_name'] . '; font-variant: ' . $font['variant'] . '; font-weight: ' . $font['weight'] . '; font-style: ' . $font['style'] . ';';
}
function typekit_api_token_headers() {
return array(
'X-Typekit-Token' => variable_get('typekit_api_token', ''),
);
}
function typekit_api_get_kits() {
$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;
}
return $kits;
}
function typekit_api_get_kit($kit_id) {
$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;
}
return $kit;
}
function typekit_api_kit_matches_domain($kit, $domain) {
foreach ($kit->domains as $kit_domain) {
if ($kit_domain == $domain) {
return TRUE;
}
}
return FALSE;
}
function typekit_api_get_variant($family_id, $variant_id) {
$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;
}
return $variant;
}