aDriv4 - MANAGER
Edit File: jquery_update.module
<?php /** * @file * Updates Drupal to use the latest version of jQuery. */ define('JQUERY_UPDATE_DEFAULT_JQUERY_VERSION', '1.12'); /** * Implements hook_help(). */ function jquery_update_help($path, $arg) { switch ($path) { // Help for another path in the block module. case 'admin/config/development/jquery_update': return '<p>' . t('Configure how <a href="@jquery">jQuery</a> behaves on the site. Select which jQuery version, the compression level and whether or not to use a CDN.', array( '@jquery' => 'http://jquery.com', )) . '</p>'; } } /** * Implements hook_library(). */ function jquery_update_library() { // Register libraries available in the external directory. $path = drupal_get_path('module', 'jquery_update') . '/ui/external'; $libraries['qunit'] = array( 'title' => 'QUnit', 'js' => array( $path . '/qunit.js' => array( 'group' => JS_LIBRARY, 'weight' => 2, ), ), 'css' => array( $path . '/qunit.css' => array(), ), 'version' => '1.11.0', ); $libraries['jquery_update.ajax.fix'] = array( 'title' => 'jQuery Update Version Fix', 'js' => array( drupal_get_path('module', 'jquery_update') . '/js/jquery_update.js' => array( 'group' => JS_LIBRARY, 'weight' => 3, ), ), 'version' => '0.0.1', ); $libraries['jquery_update.browser.fix'] = array( 'title' => 'jQuery Update Browser Fix', 'js' => array( drupal_get_path('module', 'jquery_update') . '/js/jquery_browser.js' => array( 'group' => JS_LIBRARY, 'weight' => 3, ), ), 'version' => '0.0.1', ); $libraries['jquery_update.position.fix'] = array( 'title' => 'jQuery Update Position Fix', 'js' => array( drupal_get_path('module', 'jquery_update') . '/js/jquery_position.js' => array( 'group' => JS_THEME, 'weight' => 2, ), ), 'version' => '0.0.1', ); $libraries['jquery.metadata'] = array( 'title' => 'QUnit', 'js' => array( $path . '/jquery.metadata.js' => array( 'group' => JS_LIBRARY, 'weight' => 2, ), ), 'version' => '4187', ); $libraries['jquery.bgiframe'] = array( 'title' => 'bgiframe', 'website' => 'http://docs.jquery.com/Plugins/bgiframe', 'js' => array( $path . '/jquery.bgiframe.js' => array( 'group' => JS_LIBRARY, 'weight' => 2, ), ), 'version' => '2.1.2', ); return $libraries; } /** * Implements hook_library_alter(). * * {@inheritdoc} */ function jquery_update_library_alter(&$libraries, $module) { // Immediately return if not modifying the system libraries. if ($module !== 'system') { return; } $path = drupal_get_path('module', 'jquery_update'); $min = variable_get('jquery_update_compression_type', 'min') == 'none' ? '' : '.min'; $jquery_version = variable_get('jquery_update_jquery_version', JQUERY_UPDATE_DEFAULT_JQUERY_VERSION); // Make sure we inject either the minified or uncompressed version as desired. $cdn = variable_get('jquery_update_jquery_cdn', 'none'); // Replace jQuery with the alternative version. $theme_version = theme_get_setting('jquery_update_jquery_version'); if ($theme_version && version_compare($jquery_version, $theme_version, '!=')) { $jquery_version = $theme_version; } // If the ajax version is set then that one always win. if (!empty($_POST['ajax_page_state']['jquery_version']) && !empty($_POST['ajax_page_state']['jquery_version_token'])) { $ajax_version = $_POST['ajax_page_state']['jquery_version']; $token = $_POST['ajax_page_state']['jquery_version_token']; $allowed_versions = array('default') + jquery_update_get_versions(); if (in_array($ajax_version, $allowed_versions) && drupal_valid_token($token, $ajax_version)) { $jquery_version = $ajax_version; } } // Always add a new jquery_version array to ajaxPageState. // This is what we used to determine which version to use // for any ajax callback. $libraries['drupal.ajax']['js'][] = array( 'data' => array( 'ajaxPageState' => array( 'jquery_version' => $jquery_version, 'jquery_version_token' => drupal_get_token($jquery_version), ), ), 'type' => 'setting', ); $libraries['drupal.ajax']['dependencies'][] = array('jquery_update', 'jquery_update.ajax.fix'); // Don't replace anything if Drupal provided jQuery should be used. if ('default' == $jquery_version) { return; } jquery_update_jquery_replace($libraries, $cdn, $path, $min, $jquery_version); $jqueryui_custom_version = (bool) variable_get('jquery_update_custom_version_jqueryui', FALSE); // Replace jQuery UI with CDN or local files. If from a CDN include all of // jQuery UI. if ($jqueryui_custom_version || version_compare($jquery_version, '1.6', '>=')) { jquery_update_jqueryui_replace($libraries, $cdn, $path, $min); } // Add jquery-cookie plugin. jquery_update_jquery_cookie_replace($libraries, $path, $min); // Add jquery.form plugin. jquery_update_jquery_form_replace($libraries, $path, $min, $jquery_version); // Add jQuery.migrate plugin, if needed. jquery_update_jquery_migrate_replace($libraries, $path, $min, $jquery_version); } /** * Implements hook_menu(). */ function jquery_update_menu() { $items['admin/config/development/jquery_update'] = array( 'title' => 'jQuery Update', 'description' => 'Configure settings related to the jQuery upgrade, the library path and compression.', 'page callback' => 'drupal_get_form', 'page arguments' => array('jquery_update_settings_form'), 'access arguments' => array('administer jquery update'), 'file' => 'jquery_update.admin.inc', ); $items['admin/config/development/jquery_update/refresh-version-info'] = array( 'title' => 'Refresh jQuery Update latest version info', 'description' => "Update jQuery Update's info about the latest versions of its jQuery libraries.", 'page callback' => 'jquery_update_refresh_version_info', 'access arguments' => array('administer jquery update'), 'file' => 'jquery_update.admin.inc', ); return $items; } /** * Implements hook_form_FORM_ID_alter(). */ function jquery_update_form_system_theme_settings_alter(&$form, $form_state) { // Ignore global theme settings. if (empty($form_state['build_info']['args'][0])) { return; } $form['jquery_update'] = array( '#type' => 'fieldset', '#title' => t('jQuery Update'), '#description' => t('You can optionally select a different version of jQuery to use for pages that are rendered using this theme. This is useful for administrative based themes.'), '#access' => user_access('administer jquery update'), ); $form['jquery_update']['jquery_update_jquery_version'] = array( '#type' => 'select', '#title' => t('Theme specific jQuery version'), '#options' => jquery_update_get_version_options(), '#default_value' => theme_get_setting('jquery_update_jquery_version', $form_state['build_info']['args'][0]), ); } /** * Retrieve the jQuery versions available by this module. * * @return array * The available jQuery versions. */ function jquery_update_get_versions() { // Use the advanced drupal_static() pattern, since this has the potential // to be called very often. static $drupal_static_fast; if (!isset($drupal_static_fast)) { $drupal_static_fast['versions'] = &drupal_static(__FUNCTION__, drupal_map_assoc(array( // 1.x. '1.12', // 2.x. '2.2', ))); } return $drupal_static_fast['versions']; } /** * Retrieve the jQuery versions available by this module as select options. * * @param bool $empty * Toggle on whether or not to return an empty option, which will default * to the site wide default setting. * * @return array * The available jQuery versions used to populate a select input. */ function jquery_update_get_version_options($empty = TRUE) { $options = array_merge(array( '' => t('Site default (!version)', array( '!version' => variable_get('jquery_update_jquery_version', JQUERY_UPDATE_DEFAULT_JQUERY_VERSION), )), 'default' => t('1.4 (Drupal core)'), ), jquery_update_get_supported_version_options()); if (!$empty) { unset($options['']); } return $options; } /** * Generate an array of jQuery versions including any custom version. */ function jquery_update_get_supported_version_options() { $options = array(); foreach (jquery_update_get_versions() as $version) { $options[$version] = $version; } $custom_jquery = variable_get('jquery_update_custom_version_jquery', FALSE); if (!empty($custom_jquery)) { $options[$custom_jquery] = $custom_jquery . ' ' . t('(Custom)'); } return $options; } /** * Update jQuery to the CDN or local path. * * @param array $javascript * The library definition array as seen in hook_library_alter(). * @param string $cdn * The name of the CDN option to use. Possible options are: * - none * - google * - microsoft. * @param string $path * The path to the module where replacements can be found. * @param string $min * The '.min' to include in the file name if we are requesting a minified * version. * @param string $version * The jQuery version to be used. */ function jquery_update_jquery_replace(array &$javascript, $cdn, $path, $min, $version) { $custom_path = variable_get('jquery_update_custom_path_jquery', FALSE); $custom_version = variable_get('jquery_update_custom_version_jquery', FALSE); if (!empty($custom_path) && $version == $custom_version) { $javascript['jquery']['js']['misc/jquery.js']['data'] = $custom_path; $javascript['jquery']['js']['misc/jquery.js']['type'] = url_is_external($custom_path) ? 'external' : 'file'; if (!empty($custom_version)) { $javascript['jquery']['version'] = $custom_version; jquery_update_add_jquery_browser_fix($javascript, $custom_version); jquery_update_add_jquery_position_fix($javascript, $custom_version); } return; } // In case we've been passed an obsolete jQuery version ensure that the // versions stored in settings are all supported, and map the current request // to a supported version. $supported_versions = jquery_update_get_versions(); if (!in_array($version, $supported_versions)) { _jquery_update_convert_settings_to_supported_versions(); // The global default and any theme-specific versions should have been // updated but we still need to replace the version passed to this function. $version = _jquery_update_map_to_supported_version($version); } // Make sure to use the latest version in given branch. $trueversion = NULL; switch ($version) { case '1.12': $trueversion = '1.12.4'; break; case '2.2': $trueversion = '2.2.4'; break; } $javascript['jquery']['version'] = $trueversion; // Check for CDN support. switch ($cdn) { case 'google': $javascript['jquery']['js']['misc/jquery.js']['data'] = '//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'] = '//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'] = '//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'; break; } jquery_update_add_jquery_browser_fix($javascript, $version); jquery_update_add_jquery_position_fix($javascript, $version); } /** * Add a workaround for deprecated jQuery.browser. */ function jquery_update_add_jquery_browser_fix(&$libraries, $jquery_version) { if (version_compare($jquery_version, '1.9', '>=')) { $libraries['jquery']['dependencies'][] = array( 'jquery_update', 'jquery_update.browser.fix', ); } } /** * Add a workaround for .position() behaviour change. */ function jquery_update_add_jquery_position_fix(&$libraries, $jquery_version) { if (version_compare($jquery_version, '3.3', '>=')) { $libraries['jquery']['dependencies'][] = array( 'jquery_update', 'jquery_update.position.fix', ); } } /** * Add the local fallback in case jQuery from the CDN is unavailable. * * @param array $javascript * The $libraries array as seen in hook_library_alter() * @param string $path * The path to the module where replacements can be found. * @param string $min * The '.min' to include in the file name if we are requesting a minified * version. * @param string $version * The jQuery version to be used. */ function jquery_update_jquery_backup(array &$javascript, $path, $min, $version) { $javascript['jquery']['js'][] = array( 'data' => 'window.jQuery || document.write("<script src=\'' . base_path() . $path . '/replace/jquery/' . $version . '/jquery' . $min . '.js\'>\x3C/script>")', 'type' => 'inline', 'group' => JS_LIBRARY, 'weight' => -19.999999999, ); } /** * Enable and configure the jQuery Migrate Plugin. * * @param array $libraries * The library definition array as seen in hook_library_alter(). * @param string $path * The path to the module where replacements can be found. * @param string $min * The '.min' to include in the file name if we are requesting a minified * version. * @param string $version * The jQuery version to be used. */ function jquery_update_jquery_migrate_replace(array &$libraries, $path, $min, $version) { // Immediately return if jQuery Migrate isn't enabled or jQuery version // isn't at least 1.9 or higher. if (!variable_get('jquery_update_jquery_migrate_enable', FALSE) || !version_compare($version, '1.9', '>=')) { return; } $custom_path = variable_get('jquery_update_custom_path_migrate', FALSE); if (version_compare($version, '3.0', '>=')) { $migrate_version = '3'; if (empty($custom_path)) { // jQuery 3.x and Migrate 3.x are only supported via a custom path. return; } } else { $migrate_version = '1'; } $file = $path . '/replace/jquery-migrate/' . $migrate_version . '/jquery-migrate' . $min . '.js'; // Note: this adds jQuery Migrate to the "system" module's library definition. $libraries['jquery.migrate'] = array( 'title' => 'jQuery Migrate', 'website' => 'http://plugins.jquery.com/migrate', 'version' => $migrate_version, 'js' => array( $file => array( 'group' => JS_LIBRARY, // Ensure weight is higher than jQuery. 'weight' => -19.8, ), ), ); // Configure the jQuery Migrate plugin. // Note: This must be done after jQuery has loaded, but before the jQuery // migrate plugin has loaded. $data = 'jQuery.migrateMute=' . (variable_get('jquery_update_jquery_migrate_warnings', FALSE) ? 'false' : 'true') . ';'; $data .= 'jQuery.migrateTrace=' . (variable_get('jquery_update_jquery_migrate_trace', FALSE) ? 'true' : 'false') . ';'; $libraries['jquery.migrate']['js'][] = array( 'data' => $data, 'type' => 'inline', 'group' => JS_LIBRARY, // Ensure weight is lower than jQuery Migrate. 'weight' => -19.899999999, ); // Check for CDN support. if (empty($custom_path)) { if (variable_get('jquery_update_jquery_migrate_cdn', 'none') === 'jquery') { $default_migrate_version = ($migrate_version === '3') ? '3.0.0' : '1.4.1'; $jquery_migrate_cdn_version = variable_get('jquery_update_custom_version_jquery_migrate', FALSE); if ($jquery_migrate_cdn_version !== FALSE) { $migrate_version = $jquery_migrate_cdn_version; } else { $migrate_version = $default_migrate_version; } $libraries['jquery.migrate']['js'][$file]['data'] = '//code.jquery.com/jquery-migrate-' . $migrate_version . $min . '.js'; $libraries['jquery.migrate']['js'][$file]['type'] = 'external'; jquery_update_jquery_migrate_backup($libraries, $path, $min, $default_migrate_version); } } else { $libraries['jquery.migrate']['js'][$file]['data'] = $custom_path; if (strpos($custom_path, '//') !== FALSE) { $libraries['jquery.migrate']['js'][$file]['type'] = 'external'; } $custom_version = variable_get('jquery_update_custom_version_jquery_migrate', FALSE); if (!empty($custom_version)) { $libraries['jquery.migrate']['version'] = $custom_version; } } // Add jQuery Migrate as a dependency to jQuery. // Note: this is fine as the weight set above ensures it loads after jQuery. $libraries['jquery']['dependencies'][] = array('system', 'jquery.migrate'); } /** * Add local fallback in case the jQuery Migrate Plugin from CDN is unavailable. * * @param array $javascript * The $libraries array as seen in hook_library_alter() * @param string $path * The path to the module where replacements can be found. * @param string $min * The '.min' to include in the file name if we are requesting a minified * version. * @param string $migrate_version * The jQuery Migrate version to be used. */ function jquery_update_jquery_migrate_backup(array &$javascript, $path, $min, $migrate_version) { $javascript['jquery.migrate']['js'][] = array( 'data' => 'window.jQuery && window.jQuery.migrateWarnings || document.write("<script src=\'' . base_path() . $path . '/replace/jquery-migrate/' . $migrate_version . '/jquery-migrate' . $min . '.js\'>\x3C/script>")', 'type' => 'inline', 'group' => JS_LIBRARY, 'weight' => -19.7999999999, ); } /** * Update jQuery UI to the CDN or local path. * * @param array $javascript * The library definition array as seen in hook_library_alter(). * @param string $cdn * The name of the CDN option to use. Possible options are: * - none * - google * - microsoft. * @param string $path * The path to the module where replacements can be found. * @param string $min * The '.min' to include in the file name if we are requesting a minified * version. */ function jquery_update_jqueryui_replace(array &$javascript, $cdn, $path, $min) { $jqueryui_version = variable_get('jquery_update_custom_version_jqueryui', FALSE); if (empty($jqueryui_version)) { $jqueryui_version = '1.10.2'; } // Add new components. $javascript['ui.menu'] = array( 'title' => 'jQuery UI: Menu', 'website' => 'http://jqueryui.com/demos/menu/', 'version' => $jqueryui_version, 'js' => array('misc/ui/jquery.ui.menu.min.js' => array()), 'css' => array('misc/ui/jquery.ui.menu.css' => array()), 'dependencies' => array( array('system', 'ui.widget'), array('system', 'ui.position'), ), ); $javascript['ui.spinner'] = array( 'title' => 'jQuery UI: Spinner', 'website' => 'http://jqueryui.com/demos/spinner/', 'version' => $jqueryui_version, 'js' => array('misc/ui/jquery.ui.spinner.min.js' => array()), 'css' => array('misc/ui/jquery.ui.spinner.css' => array()), 'dependencies' => array( array('system', 'ui.widget'), array('system', 'ui.button'), ), ); $javascript['ui.tooltip'] = array( 'title' => 'jQuery UI: Tooltip', 'website' => 'http://jqueryui.com/demos/tooltip/', 'version' => $jqueryui_version, 'js' => array('misc/ui/jquery.ui.tooltip.min.js' => array()), 'css' => array('misc/ui/jquery.ui.tooltip.css' => array()), 'dependencies' => array( array('system', 'ui.widget'), array('system', 'ui.position'), ), ); // Fix dependencies. $javascript['ui.autocomplete']['dependencies'][] = array('system', 'ui.menu'); // Replace all CSS files. $names = drupal_map_assoc(array( 'ui.accordion', 'ui.autocomplete', 'ui.button', 'ui.datepicker', 'ui.dialog', 'ui.progressbar', 'ui.resizable', 'ui.selectable', 'ui.slider', 'ui.tabs', 'ui.menu', 'ui.spinner', 'ui.tooltip', )); $names['ui'] = 'ui.core'; $csspath = $path . '/replace/ui/themes/base/' . (($min == '.min') ? 'minified/' : ''); foreach ($names as $name => $file) { $javascript[$name]['css']["misc/ui/jquery.$file.css"]['data'] = $csspath . 'jquery.' . $file . $min . '.css'; } // Make sure ui.theme is replaced as well. $javascript['ui']['css']['misc/ui/jquery.ui.theme.css']['data'] = $csspath . 'jquery.ui.theme' . $min . '.css'; // Replace jQuery UI's JavaScript, beginning by defining the mapping. $names = drupal_map_assoc(array( 'ui.accordion', 'ui.autocomplete', 'ui.button', 'ui.datepicker', 'ui.dialog', 'ui.draggable', 'ui.droppable', 'ui.mouse', 'ui.position', 'ui.progressbar', 'ui.resizable', 'ui.selectable', 'ui.slider', 'ui.sortable', 'ui.tabs', 'ui.widget', 'ui.spinner', 'ui.menu', 'ui.tooltip', )); $names['ui'] = 'ui.core'; // map[library_hook] = array(core_fn, updated_fn) $names['effects'] = array('effects.core', 'ui.effect'); $names = jquery_update_make_library_hook_to_file_name_segment_map_for_effects($names); $custom_path = variable_get('jquery_update_custom_path_jqueryui', FALSE); if (!empty($custom_path)) { jquery_update_jqueryui_cdn($custom_path, $javascript, $path, $min, $names, $jqueryui_version); return; } switch ($cdn) { case 'google': $cdn = '//ajax.googleapis.com/ajax/libs/jqueryui/' . $jqueryui_version . '/jquery-ui' . $min . '.js'; jquery_update_jqueryui_cdn($cdn, $javascript, $path, $min, $names, $jqueryui_version); jquery_update_jqueryui_backup($javascript, $path, $min); break; case 'microsoft': $cdn = '//ajax.aspnetcdn.com/ajax/jquery.ui/' . $jqueryui_version . '/jquery-ui' . $min . '.js'; jquery_update_jqueryui_cdn($cdn, $javascript, $path, $min, $names, $jqueryui_version); jquery_update_jqueryui_backup($javascript, $path, $min); break; case 'jquery': $cdn = '//code.jquery.com/ui/' . $jqueryui_version . '/jquery-ui' . $min . '.js'; jquery_update_jqueryui_cdn($cdn, $javascript, $path, $min, $names, $jqueryui_version); jquery_update_jqueryui_backup($javascript, $path, $min); break; case 'none': jquery_update_jqueryui_local($javascript, $path, $min, $names); break; } } /** * Create a mapping from system.module library hooks to file name segments. * * @param array $map * Optional. If given, append to it. * * @return array * The keys are library hooks and the values are each arrays of 2 file name * segments as values. The first file name segment can be used to reach Drupal * core's jQuery UI effect files, and the second file name segment can be used * to construct a path to the equivalent replacement jQuery UI effect file * provided by jquery_update.module. */ function jquery_update_make_library_hook_to_file_name_segment_map_for_effects(array $map = array()) { $effect_names = array( 'blind', 'bounce', 'clip', 'drop', 'explode', 'fade', 'fold', 'highlight', 'pulsate', 'scale', 'shake', 'slide', 'transfer', ); foreach ($effect_names as $effect_name) { $library_hook = 'effects.' . $effect_name; // Yes, for the effect files, this is indeed identical. $file_name_segment_core = $library_hook; $file_name_segment_updated = 'ui.effect-' . $effect_name; $map[$library_hook] = array($file_name_segment_core, $file_name_segment_updated); } return $map; } /** * Add the local fallback in case jQuery UI from the CDN is unavailable. * * @param array $javascript * The $libraries array as seen in hook_library_alter() * @param string $path * The path to the module where replacements can be found. * @param string $min * The '.min' to include in the file name if we are requesting a minified * version. */ function jquery_update_jqueryui_backup(array &$javascript, $path, $min) { $js_path = ($min == '.min') ? '/replace/ui/ui/minified/jquery-ui.min.js' : '/replace/ui/ui/jquery-ui.js'; $javascript['ui']['js'][] = array( 'data' => 'window.jQuery.ui || document.write("<script src=\'' . base_path() . $path . $js_path . '\'>\x3C/script>")', 'type' => 'inline', 'group' => JS_LIBRARY, 'weight' => -10.999999999, ); } /** * Handle when jQuery UI is updated to the cdn version. * * @param string $cdn * The name of the CDN option to use. Possible options are: * - none * - google * - microsoft. * @param array $javascript * The $libraries array as seen in hook_library_alter() * @param string $path * The path to the module where replacements can be found. * @param string $min * The '.min' to include in the file name if we are requesting a minified * version. * * @param array $names * An array mapping jquery ui parts to their file names. */ function jquery_update_jqueryui_cdn($cdn, array &$javascript, $path, $min, $names, $jqueryui_version) { // Construct the jQuery UI path and replace the JavaScript. $jspath = $path . '/replace/ui/ui/' . ($min == '.min' ? 'minified/' : ''); foreach ($names as $name => $file) { list($file_core, $file_updated) = is_array($file) ? $file : array($file, $file); $corefile = 'misc/ui/jquery.' . $file_core . '.min.js'; // Remove the core files. unset($javascript[$name]['js'][$corefile]); $javascript[$name]['version'] = $jqueryui_version; } // UI is used by all of UI. Add the js cdn here. $javascript['ui']['js'][$cdn] = array( 'data' => $cdn, 'type' => url_is_external($cdn) ? 'external' : 'file', 'group' => JS_LIBRARY, 'weight' => -11, ); // The cdn puts jQuery UI core and the jQuery UI Effects library in the same // file, but the latter can normally be used without the former. So we need // to add a dependency to guarantee that code which uses the Effects library // has the file loaded regardless of whether they are also using jQuery UI // core. $javascript['effects']['dependencies'][] = array('system', 'ui'); } /** * Handle when jQuery UI is updated to the local version. * * @param array $javascript * The $libraries array as seen in hook_library_alter() * @param string $path * The path to the module where replacements can be found. * @param string $min * The '.min' to include in the file name if we are requesting a minified * version. * @param array $names * An array mapping jquery ui parts to their file names. */ function jquery_update_jqueryui_local(array &$javascript, $path, $min, array $names) { // Construct the jQuery UI path and replace the JavaScript. $jspath = $path . '/replace/ui/ui/' . ($min == '.min' ? 'minified/' : ''); foreach ($names as $name => $file) { list($file_core, $file_updated) = is_array($file) ? $file : array($file, $file); $corefile = 'misc/ui/jquery.' . $file_core . '.min.js'; $javascript[$name]['js'][$corefile]['data'] = $jspath . 'jquery.' . $file_updated . $min . '.js'; $javascript[$name]['version'] = '1.10.2'; } } /** * Implements hook_permission(). */ function jquery_update_permission() { return array( 'administer jquery update' => array( 'title' => t('Administer jQuery Update'), 'restricted access' => TRUE, 'description' => t('Perform administration tasks for jQuery Update.'), ), ); } /** * Enable and configure the jQuery Cookie Plugin. * * @param array $libraries * The library definition array as seen in hook_library_alter(). * @param string $path * The path to the module where replacements can be found. * @param string $min * The '.min' to include in the file name if we are requesting a minified * version. */ function jquery_update_jquery_cookie_replace(array &$libraries, $path, $min) { $custom_path = variable_get('jquery_update_custom_path_cookie', FALSE); if (empty($custom_path)) { // Replace the jQuery Cookie plugin. $libraries['cookie']['js']['misc/jquery.cookie.js']['data'] = $path . '/replace/ui/external/jquery.cookie.js'; // Noting the version based on git commit as no version number is available. $libraries['cookie']['version'] = '67fb34f6a866c40d0570'; } else { $libraries['cookie']['js']['misc/jquery.cookie.js']['data'] = $custom_path; $libraries['cookie']['js']['misc/jquery.cookie.js']['type'] = url_is_external($custom_path) ? 'external' : 'file'; $version = variable_get('jquery_update_custom_version_jquery_cookie', FALSE); if ($version !== FALSE) { $libraries['cookie']['version'] = $version; } } } /** * Enable and configure the jQuery Form Plugin. * * @param array $libraries * The library definition array as seen in hook_library_alter(). * @param string $path * The path to the module where replacements can be found. * @param string $min * The '.min' to include in the file name if we are requesting a minified * version. * @param string $jquery_version * The jQuery version to be used. */ function jquery_update_jquery_form_replace(array &$libraries, $path, $min, $jquery_version) { $custom_path = variable_get('jquery_update_custom_path_form', FALSE); if (empty($custom_path)) { // Replace jQuery Form. $libraries['jquery.form']['website'] = 'https://github.com/jquery-form/form'; $jquery_form_versions = array( // jQuery Form 4, prior to version 4.2.1, had a serious regression that // broke Drupal's AJAX system because it didn't deserialize "+" back into // spaces which would cause triggering button values to not match in PHP. // @see https://www.drupal.org/node/2860158 '4.2.1' => '1.7', ); foreach ($jquery_form_versions as $jquery_form_version => $compatibility) { if (version_compare($jquery_version, $compatibility, '>=')) { $libraries['jquery.form']['js']['misc/jquery.form.js']['data'] = $path . '/replace/jquery.form/4/jquery.form' . $min . '.js'; $libraries['jquery.form']['version'] = $jquery_form_version; break; } } } else { $libraries['jquery.form']['js']['misc/jquery.form.js']['data'] = $custom_path; $libraries['jquery.form']['js']['misc/jquery.form.js']['type'] = url_is_external($custom_path) ? 'external' : 'file'; $version = variable_get('jquery_update_custom_version_jquery_form', FALSE); if ($version !== FALSE) { $libraries['jquery.form']['version'] = $version; } } } /** * Query and store latest versions of jQuery libraries. * * @return string * A version string e.g. "3.6.1" or FALSE */ function jquery_update_latest_version($library = 'jquery', $refresh = FALSE) { $check_enabled = variable_get('jquery_update_check_latest_versions', TRUE); $library_variable_name = _jquery_update_clean_library_name($library); $version = variable_get('jquery_update_latest_version_' . $library_variable_name, FALSE); if ($check_enabled && ($refresh || empty($version))) { $r = drupal_http_request('https://api.cdnjs.com/libraries/' . $library . '?fields=version'); if ($r->code == 200) { $old_version = $version; $version = json_decode($r->data)->version; if ($version != $old_version) { watchdog('jquery_update', 'Updated latest %library version to %version', array('%library' => $library, '%version' => $version)); variable_set('jquery_update_latest_version_' . $library_variable_name, $version); } } } return $version; } /** * Implements hook_cron(). */ function jquery_update_cron() { _jquery_update_refresh_version_info(); } /** * Refresh latest version info via cron or manually. */ function _jquery_update_refresh_version_info($refresh = FALSE) { $last_update = variable_get('jquery_update_latest_versions_checked', FALSE); if ($refresh || !$last_update || (REQUEST_TIME - $last_update) >= 24 * 60 * 60) { $libraries = array( 'jquery', 'jqueryui', 'jquery-migrate', 'jquery.form', 'jquery-cookie', ); foreach ($libraries as $library) { $version = jquery_update_latest_version($library, TRUE); } variable_set('jquery_update_latest_versions_checked', REQUEST_TIME); } } /** * Helper to clean up library names for use as Drupal variables. */ function _jquery_update_clean_library_name($library) { $search = array('.', '-'); return str_replace($search, '_', $library); } /** * Check any custom versions against latest version info. * * @return array * An array of libraries for which updates are available (empty array if none) */ function _jquery_update_check_available_updates() { $results = array(); // Nothing to do if checking of latest versions is not enabled. if (!variable_get('jquery_update_check_latest_versions', TRUE)) { return $results; } $libraries = array( 'jquery', 'jqueryui', 'jquery-migrate', 'jquery.form', 'jquery-cookie', ); foreach ($libraries as $library) { $results[$library] = _jquery_update_check_available_update($library); } return array_keys(array_filter($results)); } /** * Check a specific library for available updates. */ function _jquery_update_check_available_update($library) { $library_variable_name = _jquery_update_clean_library_name($library); $custom_version = variable_get('jquery_update_custom_version_' . $library_variable_name, FALSE); $latest_version = variable_get('jquery_update_latest_version_' . $library_variable_name, FALSE); if (!$custom_version || !$latest_version) { return FALSE; } return version_compare($custom_version, $latest_version, '<'); } /** * Helper to map existing jQuery versions to one of the supported versions. * * This is for the upgrade path from previous releases of jQuery Update and is * not intended for use with custom versions. * * @param string $version * The MAJOR.MINOR jQuery version to map. */ function _jquery_update_map_to_supported_version($version) { if ($version == 'default') { return $version; } if (version_compare($version, '2', '<')) { return '1.12'; } if (version_compare($version, '1', '>')) { return '2.2'; } return FALSE; } /** * Helper to convert all jQuery version settings to a supported version. */ function _jquery_update_convert_settings_to_supported_versions() { $jquery_version = variable_get('jquery_update_jquery_version', JQUERY_UPDATE_DEFAULT_JQUERY_VERSION); $mapped_version = _jquery_update_map_to_supported_version($jquery_version); if ($jquery_version != $mapped_version) { variable_set('jquery_update_jquery_version', $mapped_version); watchdog('jquery_update', 'jquery_update_jquery_version updated from @before to @after', array('@before' => $jquery_version, '@after' => $mapped_version), WATCHDOG_WARNING); } $themes = list_themes(); foreach ($themes as $theme_key => $theme) { $theme_settings = variable_get('theme_' . $theme_key . '_settings', array()); if (in_array('jquery_update_jquery_version', array_keys($theme_settings))) { $theme_jquery_version = $theme_settings['jquery_update_jquery_version']; $mapped_theme_jquery_version = _jquery_update_map_to_supported_version($theme_jquery_version); if ($theme_jquery_version != $mapped_theme_jquery_version) { $theme_settings['jquery_update_jquery_version'] = $mapped_theme_jquery_version; variable_set('theme_' . $theme_key . '_settings', $theme_settings); watchdog('jquery_update', 'theme settings for @theme: jquery_update_jquery_version updated from @before to @after', array( '@theme' => $theme_key, '@before' => $theme_jquery_version, '@after' => $mapped_theme_jquery_version, ), WATCHDOG_WARNING); } } } }