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/',
'base_path' => 'http://typekit.com/fonts/',
);
return $info;
}
function typekit_api_fontyourface_import() {
$success = TRUE;
$kits = typekit_api_get_kits();
fontyourface_log('Imported Typekit kits: @kits', array(
'@kits' => print_r($kits, TRUE),
));
foreach ($kits as $kit) {
$kit_info = typekit_api_get_kit($kit->id);
fontyourface_log('Imported Typekit kit info: @kit', array(
'@kit' => print_r($kit_info, TRUE),
));
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);
fontyourface_log('Imported Typekit variant info: @variant', array(
'@variant' => print_r($variant, TRUE),
));
$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->enabled = 1;
$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);
}
}
}
else {
$success = FALSE;
fontyourface_log('Typekit kit did not match current domain, @domain', array(
'@domain' => $_SERVER['HTTP_HOST'],
));
}
}
return $success;
}
function typekit_api_preprocess_html(&$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) {
drupal_add_js($base . $kit . '.js', array(
'type' => 'external',
));
}
drupal_add_js('try{Typekit.load();}catch(e){}', array(
'type' => 'inline',
));
}
}
}
function typekit_api_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'fontyourface_ui_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' => t('Save TypeKit API token'),
),
);
if (isset($form['providers']['typekit_api_import'])) {
$form['typekit_api']['typekit_api_import'] = $form['providers']['typekit_api_import'];
unset($form['providers']['typekit_api_import']);
}
$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_preview($font, $text = NULL, $size = 18) {
$output = '';
if ($text == NULL) {
$text = $font->name;
}
if ($size == 'all') {
$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>';
}
}
else {
$output = '<span style="' . fontyourface_font_css($font) . ' font-size: ' . $size . 'px; line-height: ' . $size . 'px;">' . $text . '</span>';
}
return $output;
}
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', array(
'headers' => 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');
fontyourface_log('Invalid drupal_http_request response: @response', array(
'@response' => print_r($response, TRUE),
));
}
return $kits;
}
function typekit_api_get_kit($kit_id) {
$response = drupal_http_request(TYPEKIT_API_BASE_URL . 'kits/' . $kit_id, array(
'headers' => 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');
fontyourface_log('Invalid drupal_http_request response: @response', array(
'@response' => print_r($response, TRUE),
));
}
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) {
$response = drupal_http_request(TYPEKIT_API_BASE_URL . 'families/' . $family_id . '/' . $variant_id, array(
'headers' => 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');
fontyourface_log('Invalid drupal_http_request response: @response', array(
'@response' => print_r($response, TRUE),
));
}
return $variant;
}