You are here

function addthis_cache_js in AddThis 6.3

Download and cache the AddThis JS file locally and return its path.

Parameters

$location: The full URL to the external JavaScript file.

Return value

mixed The path to the local JavaScript file on success, boolean FALSE on failure.

1 call to addthis_cache_js()
addthis_footer in ./addthis.module
Implements hook_footer().
3 string references to 'addthis_cache_js'
addthis_config_form in ./addthis.admin.inc
Return a form object for default AddThis settings.
addthis_footer in ./addthis.module
Implements hook_footer().
addthis_update_6301 in ./addthis.install
Convert obsolete "icon_32x32" variable to use "addthis_toolbox_classes". Move cache_js setting out of addthis_config. Also force a menu rebuild to expose new administration pages.

File

./addthis.module, line 384
Provides integration with the AddThis.com bookmarking & sharing service.

Code

function addthis_cache_js($location) {

  // If the previous cached file is more than a day old, delete it.
  if (time() - variable_get('addthis_last_cache', 0) >= 86400) {
    addthis_clear_js();
  }

  // Check for a cached version. Get one if it doesn't exist.
  $directory = file_directory_path() . '/addthis';
  $file_destination = $directory . '/' . basename($location);
  if (!file_exists($file_destination)) {
    $result = drupal_http_request($location);

    // Make sure we got the file and that the files directory is writable.
    if ($result->code == 200 && file_check_directory($directory, FILE_CREATE_DIRECTORY)) {
      variable_set('addthis_last_cache', time());
      return file_save_data($result->data, $directory . '/' . basename($location), FILE_EXISTS_REPLACE);
    }
    else {
      return FALSE;
    }
  }
  else {
    return $file_destination;
  }
}