From c0454519c1316294650dc3d71e1afaceb228e0bc Mon Sep 17 00:00:00 2001
From: Pierre Buyle <buyle@pheromone.ca>
Date: Wed, 15 Jan 2014 10:48:17 -0500
Subject: [PATCH] Issue #1969244 by emcniece: Wrong jQuery version on some
 pages, depending on permissions and settings

---
 jquery_update.module |  126 ++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 101 insertions(+), 25 deletions(-)

diff --git a/jquery_update.module b/jquery_update.module
index 3b62d97..a4d6e89 100644
--- a/jquery_update.module
+++ b/jquery_update.module
@@ -75,23 +75,24 @@ function jquery_update_library() {
  * Implements hook_library_alter().
  */
 function jquery_update_library_alter(&$javascript, $module) {
+  global $theme;
+
   $path = drupal_get_path('module', 'jquery_update');
   $version = variable_get('jquery_update_jquery_version', '1.10');
 
 
   // Modified System Library.
-  if ($module === 'system') {
+  if ($module === 'system' && $version) {
 
     // Make sure we inject either the minified or uncompressed version as desired.
     $min = variable_get('jquery_update_compression_type', 'min') == 'none' ? '' : '.min';
     $cdn = variable_get('jquery_update_jquery_cdn', 'none');
 
     // Replace jQuery with the alternative version.
-    $admin_version = variable_get('jquery_update_jquery_admin_version', '');
-
-    if (!empty($admin_version) && path_is_admin(current_path())) {
-      if (version_compare($version, $admin_version, '!=')) {
-        $version = $admin_version;
+    $theme_version = theme_get_setting('jquery_update_version', $theme);
+    if ($theme_version) {
+      if (version_compare($version, $theme_version, '!=')) {
+        $version = $theme_version;
       }
     }
 
@@ -162,34 +163,60 @@ function jquery_update_settings_form() {
   $form['version_options'] = array(
     '#type' => 'fieldset',
     '#title' => t('Version options'),
+    '#description' => t('You can override the jQuery version on a per-theme basis on each themes settings page.'),
   );
 
+  $default_version = variable_get('jquery_update_jquery_version', '1.10');
   $form['version_options']['jquery_update_jquery_version'] = array(
     '#type' => 'select',
     '#title' => t('Default jQuery Version'),
-    '#options' => array(
-      '1.5' => '1.5',
-      '1.7' => '1.7',
-      '1.8' => '1.8',
-      '1.9' => '1.9',
-      '1.10' => '1.10',
-    ),
+    '#options' => jquery_update_get_versions(),
     '#default_value' => variable_get('jquery_update_jquery_version', '1.10'),
     '#description' => t('Select which jQuery version to use by default.'),
   );
 
-  $form['version_options']['jquery_update_jquery_admin_version'] = array(
-    '#type' => 'select',
-    '#title' => t('Alternate jQuery version for administrative pages'),
-    '#options' => array(
-      '' => t('Use the default'),
-      '1.5' => '1.5',
-      '1.7' => '1.7',
-      '1.8' => '1.8',
-      '1.10' => '1.10',
-    ),
-    '#default_value' => variable_get('jquery_update_jquery_admin_version', ''),
-    '#description' => t('Optionally select a different version of jQuery to use on administrative pages.'),
+  $themes = list_themes();
+  $header = array(t('Theme'), t('Status'), t('jQuery version'), t(''));
+  $rows = array();
+  $themes_collapsed = TRUE;
+  // Go through all themes.
+  foreach ($themes as $theme_name => $theme) {
+    // Skip disabled themes, but only if they are not configured as admin
+    // theme. This is an inconsitency in drupal core, that you can select a
+    // disabled theme as admin theme.
+    if (!$theme->status && variable_get('admin_theme', FALSE) != $theme_name) {
+      continue;
+    }
+    // Retrieve the version jQuery for this theme.
+    $theme_version = theme_get_setting('jquery_update_version', $theme_name);
+    if (empty($theme_version)) {
+      $theme_version = $default_version;
+    }
+    // Decide whether the override table should be shown.
+    if ($theme_version != $default_version) {
+      $themes_collapsed = FALSE;
+    }
+    $uri = 'admin/appearance/settings/' . $theme_name;
+    $rows[] = array(
+      $theme->info['name'],
+      $theme_version != $default_version ? t('Overridden') : t('Default'),
+      $theme_version,
+      l(t('Change'), $uri, array(
+        'fragment' => 'edit-jquery-update-version',
+        'query' => drupal_get_destination(),
+      )),
+    );
+  }
+
+  $form['version_options']['themes'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Theme specific versions'),
+    '#collapsible' => TRUE,
+    '#collapsed' => $themes_collapsed,
+  );
+  $form['version_options']['themes']['overrides'] = array(
+    '#type' => 'markup',
+    '#markup' => theme('table', array('header' => $header, 'rows' => $rows)),
   );
 
   $form['jquery_update_compression_type'] = array(
@@ -218,6 +245,55 @@ function jquery_update_settings_form() {
 }
 
 /**
+ * Implements hook_form_FORM_ID_alter().
+ */
+function jquery_update_form_system_theme_settings_alter(&$form, $form_state) {
+  if ($form['var']['#value'] == 'theme_settings') {
+    // global theme settings page
+    return;
+  }
+  $theme_name = $form_state['build_info']['args'][0];
+  $themes = list_themes();
+
+  $default_version = variable_get('jquery_update_jquery_version', '1.10');
+  $theme_version = theme_get_setting('jquery_update_version', $theme_name);
+
+  $form_key = $theme_name . '_jquery_update';
+  $form['options_settings'][$form_key] = array(
+    '#type' => 'fieldset',
+    '#title' => t('jQuery Update'),
+  );
+  $form['options_settings'][$form_key]['jquery_update_version'] = array(
+    '#type' => 'select',
+    '#title' => t('jQuery version for theme %theme_name', array(
+      '%theme_name' => $themes[$theme_name]->info['name'],
+    )),
+    '#options' => array(
+      '' => t('Site wide default (!version)', array('!version' => $default_version)),
+    ) + jquery_update_get_versions(),
+    '#default_value' => empty($theme_version) ? '' : $theme_version,
+    '#description' => t('Optionally select a different version of jQuery to use for pages that are rendered using the %theme_name theme.', array(
+      '%theme_name' => $themes[$theme_name]->info['name'],
+    )),
+  );
+}
+
+/**
+ * Retrieve the jQuery versions availabile by this module.
+ *
+ * @return array The available jQuery versions
+ */
+function jquery_update_get_versions() {
+  return array(
+    '1.5' => '1.5',
+    '1.7' => '1.7',
+    '1.8' => '1.8',
+    '1.9' => '1.9',
+    '1.10' => '1.10',
+  );
+}
+
+/**
  * Update jQuery to the CDN or local path.
  *
  * @param array $javascript
-- 
1.7.10.4

