You are here

typekit_api.module in @font-your-face 6

File

modules/typekit_api/typekit_api.module
View source
<?php

define('TYPEKIT_API_BASE_URL', 'https://typekit.com/api/v1/json/');

/**
 * Implements hook_menu()
 */
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;
}

// typekit_api_menu

/**
 * Callback for import.
 */
function typekit_api_import() {
  typekit_api_cron();
  drupal_goto($_GET['destination']);
}

// typekit_api_import

/**
 * Implements hook_cron().
 */
function typekit_api_cron() {
  $last_updated = variable_get('typekit_api_last_update', 0);
  if (time() - 60 * 60 * 24 > $last_updated) {

    // update every 24 hours
    $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)) {

              // Already in database, update it.
              $variant_record->vid = $result->vid;
              drupal_write_record('typekit_api_variant', $variant_record, array(
                'vid',
              ));
            }
            else {

              // Not already in database, add it.
              drupal_write_record('typekit_api_variant', $variant_record);
            }

            // if
          }

          // foreach
        }

        // foreach
      }

      // if
    }

    // foreach
    variable_set('typekit_api_last_update', time());
  }

  // if
}

// typekit_api_cron

/**
 * Implements template_preprocess_page().
 */
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
    }

    // foreach
    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();
      }

      // foreach
      drupal_add_js('try{Typekit.load();}catch(e){}', 'inline');
      $vars['scripts'] = drupal_get_js();
    }

    // if
  }

  // if
}

// typekit_api_preprocess_page

/**
 * Implements hook_form_alter().
 */
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', ''),
      ),
    );
  }

  // if
}

// typekit_api_form_alter

/**
 * Implements hook_fontyourface_info().
 */
function typekit_api_fontyourface_info() {
  $info = array(
    'typekit_api' => array(
      'name' => 'Typekit',
      'url' => 'http://typekit.com/',
      'fonts' => typekit_api_list(),
    ),
  );
  return $info;
}

// typekit_api_fontyourface_info

/**
 * Provides a list of fonts for @font-your-face.
 */
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(),
      );
    }

    // if
    $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,
    );
  }

  // while
  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(),
      )),
    )));
  }

  // if
  return $fonts;
}

// typekit_api_list

/**
 * Implements hook_fontyourface_css().
 */
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;
}

// typekit_api_fontyourface_css

/**
 * Implements hook_fontyourface_view().
 */
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>';
  }

  // foreach
  return $output;
}

// typekit_api_fontyourface_view

/**
 * Implements hook_fontyourface_preview().
 */
function typekit_api_fontyourface_preview($font) {
  return '<span style="' . typekit_api_font_css($font) . ' font- font-size: 24px;">' . $font['name'] . '</span>';
}

// typekit_api_fontyourface_preview

/**
 * Creates CSS from font details.
 */
function typekit_api_font_css($font) {
  return 'font-family: ' . $font['css_name'] . '; font-variant: ' . $font['variant'] . '; font-weight: ' . $font['weight'] . '; font-style: ' . $font['style'] . ';';
}

// typekit_api_font_css

/**
 * Provides header with token.
 */
function typekit_api_token_headers() {
  return array(
    'X-Typekit-Token' => variable_get('typekit_api_token', ''),
  );
}

// typekit_api_token_headers

/**
 * Gets list of kits from API.
 */
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;
  }

  // if
  return $kits;
}

// typekit_api_get_kits

/**
 * Get a specific kit from API based on ID.
 */
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;
  }

  // if
  return $kit;
}

// typekit_api_get_kit

/**
 * Checks if a given kit matches a given domain.
 */
function typekit_api_kit_matches_domain($kit, $domain) {
  foreach ($kit->domains as $kit_domain) {
    if ($kit_domain == $domain) {
      return TRUE;
    }

    // if
  }

  // foreach
  return FALSE;
}

// typekit_api_kit_matches_domain

/**
 * Get a specific variant from API based on family and variant IDs.
 */
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;
  }

  // if
  return $variant;
}

// typekit_api_get_variant

Functions

Namesort descending Description
typekit_api_cron Implements hook_cron().
typekit_api_fontyourface_css Implements hook_fontyourface_css().
typekit_api_fontyourface_info Implements hook_fontyourface_info().
typekit_api_fontyourface_preview Implements hook_fontyourface_preview().
typekit_api_fontyourface_view Implements hook_fontyourface_view().
typekit_api_font_css Creates CSS from font details.
typekit_api_form_alter Implements hook_form_alter().
typekit_api_get_kit Get a specific kit from API based on ID.
typekit_api_get_kits Gets list of kits from API.
typekit_api_get_variant Get a specific variant from API based on family and variant IDs.
typekit_api_import Callback for import.
typekit_api_kit_matches_domain Checks if a given kit matches a given domain.
typekit_api_list Provides a list of fonts for @font-your-face.
typekit_api_menu Implements hook_menu()
typekit_api_preprocess_page Implements template_preprocess_page().
typekit_api_token_headers Provides header with token.

Constants