View source
<?php
define('JQUERY_UPDATE_REPLACE_PATH', drupal_get_path('module', 'jquery_update') . '/replace');
function jquery_update_get_replacements() {
return array(
'module' => array(
'misc/farbtastic/farbtastic.js' => 'farbtastic.js',
),
);
}
function jquery_update_theme_registry_alter(&$theme_registry) {
if (isset($theme_registry['page'])) {
if ($key = array_search('jquery_update_preprocess_page', $theme_registry['page']['preprocess functions'])) {
unset($theme_registry['page']['preprocess functions'][$key]);
}
$theme_registry['page']['preprocess functions'][] = 'jquery_update_preprocess_page';
}
}
function jquery_update_preprocess_page(&$variables) {
if (!empty($variables['scripts'])) {
if (variable_get('jquery_update_replace', TRUE) || variable_get('jquery_update_compression_type', 'pack') != 'pack') {
$scripts = drupal_add_js();
$new_jquery = array(
jquery_update_jquery_path() => $scripts['core']['misc/jquery.js'],
);
$scripts['core'] = array_merge($new_jquery, $scripts['core']);
unset($scripts['core']['misc/jquery.js']);
foreach (jquery_update_get_replacements() as $type => $replacements) {
foreach ($replacements as $find => $replace) {
if (isset($scripts[$type][$find])) {
$replace = JQUERY_UPDATE_REPLACE_PATH . '/' . $replace;
$scripts[$type][$replace] = $scripts[$type][$find];
unset($scripts[$type][$find]);
}
}
}
$variables['scripts'] = drupal_get_js('header', $scripts);
}
}
}
function jquery_update_get_version($jquery_path = NULL) {
$version = 0;
$pattern = '# * jQuery ([0-9\\.a-z]+) - New Wave Javascript#';
if (is_null($jquery_path)) {
$jquery_path = jquery_update_jquery_path();
}
$jquery = file_get_contents($jquery_path);
if (preg_match($pattern, $jquery, $matches)) {
$version = $matches[1];
}
return $version;
}
function jquery_update_flush_caches() {
$jquery_update_version = jquery_update_get_version();
$jquery_core_version = jquery_update_get_version('misc/jquery.js');
$replace = version_compare($jquery_core_version, $jquery_update_version, '<');
variable_set('jquery_update_replace', $replace);
}
function jquery_update_menu() {
$items['admin/settings/jquery_update'] = array(
'title' => 'jQuery Update',
'description' => 'Configure settings for jQuery Update module.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'jquery_update_settings',
),
'access arguments' => array(
'administer site configuration',
),
);
return $items;
}
function jquery_update_settings() {
$form['#submit'][] = 'drupal_clear_js_cache';
$form['#submit'][] = 'jquery_update_flush_caches';
$form['jquery_update_compression_type'] = array(
'#type' => 'radios',
'#title' => t('jQuery compression type'),
'#options' => array(
'pack' => t('Packed'),
'min' => t('Minified'),
'none' => t('None'),
),
'#default_value' => variable_get('jquery_update_compression_type', 'pack'),
'#description' => t('<cite>From <a href="http://docs.jquery.com/Downloading_jQuery">docs.jquery.com</a>:</cite> <q>The minified version, while having a larger file size than the packed version, is generally the best version to use on production deployments. The packed version requires non-trivial client-side processing time to uncompress the code.</q> "None" is provided for debugging purposes, but generally one of the compression options is recommended.'),
);
return system_settings_form($form);
}
function jquery_update_jquery_path() {
$jquery_file = array(
'none' => 'jquery.js',
'pack' => 'jquery.packed.js',
'min' => 'jquery.min.js',
);
return JQUERY_UPDATE_REPLACE_PATH . '/' . $jquery_file[variable_get('jquery_update_compression_type', 'pack')];
}