| 1 |
<?php
|
| 2 |
// $Id: jquery_update.module,v 1.7 2009/04/23 18:49:07 mfer Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Updates Drupal to use the latest version of jQuery.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* The path to the jQuery files that need to be replaced.
|
| 11 |
*/
|
| 12 |
define('JQUERY_UPDATE_REPLACE_PATH', drupal_get_path('module', 'jquery_update') . '/replace');
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Return the version of jQuery that is installed.
|
| 16 |
*
|
| 17 |
* This can be used by other modules' hook_requirements() to ensure that the
|
| 18 |
* proper version of jQuery is installed.
|
| 19 |
*
|
| 20 |
* @see version_compare
|
| 21 |
*/
|
| 22 |
function jquery_update_get_version($jquery_path = NULL) {
|
| 23 |
$version = 0;
|
| 24 |
$pattern = '# * jQuery JavaScript Library v([0-9\.a-z]+)#';
|
| 25 |
|
| 26 |
// No file is passed in so default to the file included with this module.
|
| 27 |
if (is_null($jquery_path)) {
|
| 28 |
$jquery_path = jquery_update_jquery_path();
|
| 29 |
}
|
| 30 |
|
| 31 |
// Return the version provided by jQuery Update.
|
| 32 |
$jquery = file_get_contents($jquery_path);
|
| 33 |
if (preg_match($pattern, $jquery, $matches)) {
|
| 34 |
$version = $matches[1];
|
| 35 |
}
|
| 36 |
|
| 37 |
return $version;
|
| 38 |
}
|
| 39 |
|
| 40 |
/**
|
| 41 |
* Implementation of hook_js_alter().
|
| 42 |
*/
|
| 43 |
function jquery_update_js_alter(&$javascript) {
|
| 44 |
// Only replace jquery.js if the version in jQuery Update is newer.
|
| 45 |
if (variable_get('jquery_update_replace', TRUE)) {
|
| 46 |
$javascript['misc/jquery.js']['data'] = jquery_update_jquery_path();
|
| 47 |
}
|
| 48 |
}
|
| 49 |
|
| 50 |
/**
|
| 51 |
* Implementation of hook_flush_caches().
|
| 52 |
*/
|
| 53 |
function jquery_update_flush_caches() {
|
| 54 |
// Find the versions of jQuery provided by core and this module.
|
| 55 |
$jquery_update_version = jquery_update_get_version();
|
| 56 |
$jquery_core_version = jquery_update_get_version('misc/jquery.js');
|
| 57 |
|
| 58 |
// Set a variable according to whether core's version needs to be replaced.
|
| 59 |
$replace = version_compare($jquery_core_version, $jquery_update_version, '<');
|
| 60 |
variable_set('jquery_update_replace', $replace);
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Implementation of hook_menu().
|
| 65 |
*/
|
| 66 |
function jquery_update_menu() {
|
| 67 |
$items['admin/settings/jquery_update'] = array(
|
| 68 |
'title' => 'jQuery Update',
|
| 69 |
'description' => 'Configure settings for jQuery Update module.',
|
| 70 |
'page callback' => 'drupal_get_form',
|
| 71 |
'page arguments' => array('jquery_update_settings'),
|
| 72 |
'access arguments' => array('administer site configuration'),
|
| 73 |
);
|
| 74 |
return $items;
|
| 75 |
}
|
| 76 |
|
| 77 |
/**
|
| 78 |
* Admin settings form.
|
| 79 |
*/
|
| 80 |
function jquery_update_settings() {
|
| 81 |
// Clear the javascript cache when the setting is updated and check version of jquery file.
|
| 82 |
$form['#submit'][] = 'drupal_clear_js_cache';
|
| 83 |
$form['#submit'][] = 'jquery_update_flush_caches';
|
| 84 |
|
| 85 |
$form['jquery_update_compression_type'] = array(
|
| 86 |
'#type' => 'radios',
|
| 87 |
'#title' => t('Choose jQuery compression level'),
|
| 88 |
'#options' => array(
|
| 89 |
'min' => t('Production (Minified)'),
|
| 90 |
'none' => t('Development (Uncompressed Code)'),
|
| 91 |
),
|
| 92 |
'#default_value' => variable_get('jquery_update_compression_type', 'min'),
|
| 93 |
);
|
| 94 |
|
| 95 |
return system_settings_form($form);
|
| 96 |
}
|
| 97 |
|
| 98 |
/**
|
| 99 |
* Return the path to the jQuery file.
|
| 100 |
*/
|
| 101 |
function jquery_update_jquery_path() {
|
| 102 |
$jquery_file = array('none' => 'jquery.js', 'min' => 'jquery.min.js');
|
| 103 |
return JQUERY_UPDATE_REPLACE_PATH . '/' . $jquery_file[variable_get('jquery_update_compression_type', 'min')];
|
| 104 |
}
|