You are here

function twitter_block_cache in Twitter Block 7.2

Download/Synchronize/Cache widget code file locally.

Parameters

$sync_cached_file: Synchronize widget code and update if remote file have changed.

Return value

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

2 calls to twitter_block_cache()
twitter_block_block_view in ./twitter_block.module
Implements hook_block_view().
twitter_block_cron in ./twitter_block.module
Implements hook_cron().
4 string references to 'twitter_block_cache'
twitter_block_admin_settings_form in ./twitter_block.admin.inc
Implements hook_admin_settings() for module settings configuration.
twitter_block_block_view in ./twitter_block.module
Implements hook_block_view().
twitter_block_cron in ./twitter_block.module
Implements hook_cron().
twitter_block_uninstall in ./twitter_block.install
Implements hook_uninstall().

File

./twitter_block.module, line 512
A module to provide simple Twitter blocks using the Twitter Search API.

Code

function twitter_block_cache($sync_cached_file = FALSE) {
  $location = 'http://platform.twitter.com/widgets.js';
  $path = 'public://twitter_block';
  $file_destination = $path . '/' . basename($location);
  if (!file_exists($file_destination) || $sync_cached_file) {

    // Download the latest widget code.
    $result = drupal_http_request($location);
    if ($result->code == 200) {
      if (file_exists($file_destination)) {

        // Synchronize widget code and and replace local file if outdated.
        $data_hash_local = drupal_hash_base64(file_get_contents($file_destination));
        $data_hash_remote = drupal_hash_base64($result->data);

        // Check that the files directory is writable.
        if ($data_hash_local != $data_hash_remote && file_prepare_directory($path)) {

          // Save updated widget code file to disk.
          file_unmanaged_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);
          watchdog('twitter_block', 'Locally cached widget code file has been updated.', array(), WATCHDOG_INFO);

          // Change query-strings on css/js files to enforce reload for all users.
          _drupal_flush_css_js();
        }
      }
      else {

        // Check that the files directory is writable.
        if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {

          // There is no need to flush JS here as core refreshes JS caches
          // automatically, if new files are added.
          file_unmanaged_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);
          watchdog('twitter_block', 'Locally cached widget code file has been saved.', array(), WATCHDOG_INFO);

          // Return the local JS file path.
          return file_create_url($file_destination);
        }
      }
    }
  }
  else {

    // Return the local JS file path.
    return file_create_url($file_destination);
  }
}