diff --git a/jquery_update.install b/jquery_update.install index a4e9168..62a17a6 100644 --- a/jquery_update.install +++ b/jquery_update.install @@ -6,7 +6,7 @@ */ /** - * Implementation of hook_requirements(). + * Implements hook_requirements(). */ function jquery_update_requirements($phase) { $requirements = array(); @@ -26,7 +26,7 @@ function jquery_update_requirements($phase) { } /** - * Implementation of hook_uninstall(). + * Implements hook_uninstall(). */ function jquery_update_uninstall() { variable_del('jquery_update_compression_type'); diff --git a/jquery_update.module b/jquery_update.module index 18932a3..02001e1 100644 --- a/jquery_update.module +++ b/jquery_update.module @@ -5,6 +5,9 @@ * Updates Drupal to use the latest version of jQuery. */ +// Remove tracking from all administrative pages, see http://drupal.org/node/34970. +define('JQUERY_UPDATE_PAGES', "admin\nadmin/*\nbatch\nnode/add*\nnode/*/*\nuser/*/*"); + /** * Implements hook_help(). */ @@ -61,7 +64,53 @@ function jquery_update_library() { } /** - * Implementation of hook_library_alter(). + * Based on visibility setting this function returns TRUE if jquery update should + * be added to the current page and otherwise FALSE. + */ +function _jquery_update_visibility_pages() { + static $page_match; + + // Cache visibility result if function is called more than once. + if (!isset($page_match)) { + + $visibility = variable_get('jquery_update_visibility_pages', 0); + $setting_pages = variable_get('jquery_update_pages', JQUERY_UPDATE_PAGES); + + // Match path if necessary. + if (!empty($setting_pages)) { + // Convert path to lowercase. This allows comparison of the same path + // with different case. Ex: /Page, /page, /PAGE. + $pages = drupal_strtolower($setting_pages); + if ($visibility < 2) { + // Convert the Drupal path to lowercase + $path = drupal_strtolower(drupal_get_path_alias($_GET['q'])); + // Compare the lowercase internal and lowercase path alias (if any). + $page_match = drupal_match_path($path, $pages); + if ($path != $_GET['q']) { + $page_match = $page_match || drupal_match_path($_GET['q'], $pages); + } + // When $visibility has a value of 0, the tracking code is displayed on + // all pages except those listed in $pages. When set to 1, it + // is displayed only on those pages listed in $pages. + $page_match = !($visibility xor $page_match); + } + elseif (module_exists('php')) { + $page_match = php_eval($setting_pages); + } + else { + $page_match = FALSE; + } + } + else { + $page_match = TRUE; + } + + } + return $page_match; +} + +/** + * Implements hook_library_alter(). */ function jquery_update_library_alter(&$javascript, $module) { @@ -78,6 +127,12 @@ function jquery_update_library_alter(&$javascript, $module) { // Replace jQuery with the latest version. $version = variable_get('jquery_update_jquery_version', '1.5'); + + // If a different version is specified for administration pages, use it instead. + if (!_jquery_update_visibility_pages()) { + $version = '1.5'; + } + jquery_update_jquery_replace($javascript, $cdn, $path, $min, $version); // Replace jQuery UI with CDN or local files. If from a CDN include all of jQuery UI. @@ -114,7 +169,7 @@ function jquery_update_menu() { } /** - * Implementation of hook_form_FORM_ID(). + * Implements hook_form_FORM_ID(). */ function jquery_update_settings_form() { $form['jquery_update_jquery_version'] = array( @@ -127,6 +182,7 @@ function jquery_update_settings_form() { '#default_value' => variable_get('jquery_update_jquery_version', '1.5'), '#description' => t('Select which jQuery version branch to use.'), ); + $form['jquery_update_compression_type'] = array( '#type' => 'radios', '#title' => t('jQuery compression level'), @@ -136,6 +192,7 @@ function jquery_update_settings_form() { ), '#default_value' => variable_get('jquery_update_compression_type', 'min'), ); + $form['jquery_update_jquery_cdn'] = array( '#type' => 'select', '#title' => t('jQuery and jQuery UI CDN'), @@ -149,6 +206,47 @@ function jquery_update_settings_form() { '#description' => t('Use jQuery and jQuery UI from a CDN. If the CDN is not available the local version of jQuery and jQuery UI will be used.'), ); + // Page specific visibility configurations. + $php_access = user_access('use PHP for tracking visibility'); + $visibility = variable_get('jquery_update_visibility_pages', 0); + $pages = variable_get('jquery_update_pages', JQUERY_UPDATE_PAGES); + + if ($visibility == 2 && !$php_access) { + $form = array(); + $form['visibility'] = array('#type' => 'value', '#value' => 2); + $form['pages'] = array('#type' => 'value', '#value' => $pages); + } + else { + $options = array( + t('Every page except the listed pages'), + t('The listed pages only'), + ); + $description = t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '')); + + if (module_exists('php') && $php_access) { + $options[] = t('Pages on which this PHP code returns TRUE (experts only)'); + $title = t('Pages or PHP code'); + $description .= ' ' . t('If the PHP option is chosen, enter PHP code between %php. Note that executing incorrect PHP code can break your Drupal site.', array('%php' => '')); + } + else { + $title = t('Pages'); + } + $form['jquery_update_visibility_pages'] = array( + '#type' => 'radios', + '#title' => t('Update jQuery on specific pages'), + '#options' => $options, + '#default_value' => $visibility, + ); + $form['jquery_update_pages'] = array( + '#type' => 'textarea', + '#title' => $title, + '#title_display' => 'invisible', + '#default_value' => $pages, + '#description' => $description, + '#rows' => 10, + ); + } + return system_settings_form($form); } @@ -179,25 +277,25 @@ function jquery_update_jquery_replace(&$javascript, $cdn, $path, $min, $version) $javascript['jquery']['version'] = $trueversion; // Check for CDN support. - switch($cdn) { + switch ($cdn) { case 'google': - $javascript['jquery']['js']['misc/jquery.js']['data'] = 'https://ajax.googleapis.com/ajax/libs/jquery/'. $trueversion . '/jquery' . $min . '.js'; + $javascript['jquery']['js']['misc/jquery.js']['data'] = 'https://ajax.googleapis.com/ajax/libs/jquery/' . $trueversion . '/jquery' . $min . '.js'; $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external'; jquery_update_jquery_backup($javascript, $path, $min, $version); break; case 'microsoft': - $javascript['jquery']['js']['misc/jquery.js']['data'] = 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-'. $trueversion . $min . '.js'; + $javascript['jquery']['js']['misc/jquery.js']['data'] = 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-' . $trueversion . $min . '.js'; $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external'; jquery_update_jquery_backup($javascript, $path, $min, $version); break; case 'jquery': - $javascript['jquery']['js']['misc/jquery.js']['data'] = 'http://code.jquery.com/jquery-'. $trueversion . $min . '.js'; + $javascript['jquery']['js']['misc/jquery.js']['data'] = 'http://code.jquery.com/jquery-' . $trueversion . $min . '.js'; $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external'; jquery_update_jquery_backup($javascript, $path, $min, $version); break; case 'none': default: - $javascript['jquery']['js']['misc/jquery.js']['data'] = $path . '/replace/jquery/'. $version . '/jquery' . $min . '.js'; + $javascript['jquery']['js']['misc/jquery.js']['data'] = $path . '/replace/jquery/' . $version . '/jquery' . $min . '.js'; break; } } @@ -216,7 +314,7 @@ function jquery_update_jquery_replace(&$javascript, $cdn, $path, $min, $version) */ function jquery_update_jquery_backup(&$javascript, $path, $min, $version) { $javascript['jquery']['js'][] = array( - 'data' => 'window.jQuery || document.write("