View source
<?php
define('LABJS_EXCLUDE', '// LABjs exclusion');
function labjs_menu() {
$items = array();
$file_path = drupal_get_path('module', 'labjs') . '/includes';
$items['admin/settings/performance/default'] = array(
'title' => 'Performance',
'type' => MENU_DEFAULT_LOCAL_TASK,
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/settings/performance/labjs'] = array(
'title' => 'LABjs',
'description' => 'Configure the settings used to wrap JS blocks.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'labjs_admin_settings_form',
),
'access arguments' => array(
'administer site configuration',
),
'file path' => $file_path,
'file' => 'labjs.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 1,
);
return $items;
}
function labjs_theme_registry_alter(&$theme_registry) {
if (isset($theme_registry['page'])) {
if (count($theme_registry['page']['preprocess functions']) > 0) {
if ($key = array_search('labjs_preprocess_page', $theme_registry['page']['preprocess functions'])) {
unset($theme_registry['page']['preprocess functions'][$key]);
}
}
$theme_registry['page']['preprocess functions'][] = 'labjs_preprocess_page';
}
if (!(module_exists('advagg') && variable_get('advagg_enabled', ADVAGG_ENABLED) && variable_get('advagg_closure', ADVAGG_CLOSURE))) {
$theme_registry['closure']['function'] = 'labjs_closure';
}
}
function labjs_advagg_js_pre_alter(&$javascript) {
global $conf;
if (labjs_suppress() || empty($javascript)) {
return;
}
_labjs_patch_core($javascript);
module_load_include('inc', 'labjs', 'includes/labjs.advagg');
$conf['advagg_js_render_function'] = 'labjs_advagg_js_builder';
}
function labjs_preprocess_page(&$variables) {
if (labjs_suppress() || empty($variables['scripts'])) {
return;
}
$javascript = '';
$out = '';
$exception_mode = variable_get('labjs_exception_mode', 'whitelist') == 'whitelist' ? 'whitelist' : 'blacklist';
$list = explode("\n", str_replace(array(
"\r\n",
"\r",
), "\n", variable_get('labjs_exception_' . $exception_mode, '')));
$list = array_flip(array_filter(array_map('trim', $list)));
if (!module_exists('advagg') || !variable_get('advagg_enabled', ADVAGG_ENABLED)) {
$types = drupal_add_js(NULL, NULL, NULL);
_labjs_patch_core($types['header']);
_labjs_patch_core($types['footer']);
$variables['scripts'] = drupal_get_js('header', $types['header']);
}
if (!module_exists('advagg') || !variable_get('advagg_enabled', ADVAGG_ENABLED) || isset($_GET['advagg']) && $_GET['advagg'] == -1) {
$javascript = labjs_rewrite_js($variables['scripts']);
$closure = drupal_get_js('footer', $types['footer']);
$javascript_closure = labjs_rewrite_js($closure);
$out = _labjs_prepare_required_js();
$variables['scripts'] = '';
}
else {
$javascript_closure = '';
}
foreach ($variables as $key => &$value) {
if (!empty($value) && is_string($value) && ($exception_mode == 'whitelist') === isset($list[$key])) {
labjs_rewrite_js($value);
}
}
$scripts = _labjs_rewrite_js();
if (!empty($scripts)) {
$out .= '<script type="text/javascript">' . "\n" . '<!--//--><![CDATA[//><!--' . "\n";
$out .= "\$L = \$L.script([" . implode(",\n", $scripts) . "]);\n//--><!]]>\n</script>\n";
}
$variables['scripts'] .= $out . $javascript;
$variables['closure'] .= $javascript_closure . '<script type="text/javascript">' . "\n" . '<!--//--><![CDATA[//><!--' . "\n" . LABJS_EXCLUDE . "\n" . '$L = $L.wait(function() {Drupal.scriptsready=true;jQuery(document).trigger(\'scripts-ready\');});' . "\n" . "//--><!]]>\n" . "</script>\n";
}
function labjs_rewrite_js(&$javascript) {
$javascript = preg_replace_callback('#<script .+?</script>\\s*#s', '_labjs_rewrite_js', $javascript);
return $javascript;
}
function labjs_build_uri($path) {
if (module_exists('cdn')) {
$status = variable_get(CDN_STATUS_VARIABLE, CDN_DISABLED);
if (($status == CDN_ENABLED || $status == CDN_TESTING && user_access(CDN_PERM_ACCESS_TESTING)) && variable_get(CDN_THEME_LAYER_FALLBACK_VARIABLE, FALSE) == FALSE) {
return file_create_url($path);
}
}
return base_path() . $path;
}
function labjs_closure($main = 0) {
$footer = implode("\n", module_invoke_all('footer', $main));
if (labjs_suppress() || !(module_exists('advagg') && variable_get('advagg_enabled', ADVAGG_ENABLED) && variable_get('advagg_closure', ADVAGG_CLOSURE))) {
$footer .= drupal_get_js('footer');
}
return $footer;
}
function labjs_suppress($set = FALSE) {
static $suppress;
if ($set) {
$suppress = TRUE;
}
elseif (!isset($suppress)) {
$suppress = !variable_get('labjs_enabled', TRUE) || defined('MAINTENANCE_MODE');
if (!$suppress && ($pages = variable_get('labjs_pages_list', ''))) {
$path = drupal_get_path_alias($_GET['q']);
$page_match = drupal_match_path($path, $pages);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
}
$suppress = (variable_get('labjs_pages_choice', 0) xor $page_match);
}
}
return $suppress;
}
function _labjs_patch_core(&$javascript) {
$patches = array();
if (isset($javascript['core']['misc/drupal.js'])) {
$patches[] = array(
'region' => 'core',
'old' => 'misc/drupal.js',
'new' => drupal_get_path('module', 'labjs') . '/drupal.modified.js',
);
}
if (module_exists('jquery_update') && variable_get('jquery_update_replace', TRUE)) {
$patches[] = array(
'region' => 'core',
'old' => 'misc/jquery.js',
'new' => jquery_update_jquery_path(),
);
foreach (jquery_update_get_replacements() as $type => $replacements) {
foreach ($replacements as $find => $replace) {
$patches[] = array(
'region' => $type,
'old' => $find,
'new' => JQUERY_UPDATE_REPLACE_PATH . '/' . $replace,
);
}
}
}
_labjs_replace_javascript($javascript, $patches);
}
function _labjs_replace_javascript(&$javascript, $replacement) {
foreach ($replacement as $item) {
if (isset($javascript[$item['region']][$item['old']])) {
$keys = array_keys($javascript[$item['region']]);
$values = array_values($javascript[$item['region']]);
$position = array_search($item['old'], array_keys($javascript[$item['region']]));
$keys[$position] = $item['new'];
$javascript[$item['region']] = array_combine($keys, $values);
}
}
}
function _labjs_rewrite_js($matches = NULL) {
static $scripts = array();
if (!$matches) {
return $scripts;
}
else {
$exception = strpos($matches[0], 'rpxnow.com') !== FALSE;
if (preg_match('#type="text/javascript" (defer="defer" |)src="(.+?)"#', $matches[0], $match)) {
if (strpos($matches[0], 'tinymce/jscripts/tiny_mce/tiny_mce.js') == FALSE) {
$scripts[] = "\"{$match[2]}\"";
return '';
}
else {
return $matches[0];
}
}
else {
if (strpos($matches[0], LABJS_EXCLUDE) !== FALSE || $exception) {
return $matches[0];
}
$output = str_replace(array(
"<!--//--><![CDATA[//><!--",
"//--><!]]>",
), array(
"<!--//--><![CDATA[//><!--\n\$L = \$L.wait(function() {",
"});\n//--><!]]>",
), $matches[0]);
if (strpos($matches[0], "<!--//--><![CDATA[//><!--") === FALSE && variable_get('labjs_no_cdata', FALSE)) {
$output = preg_replace('#(<script.+?>)(.+?)(</script>)\\s*#s', '\\1$L = $L.wait(function() {\\2});\\3', $matches[0]);
}
return $output;
}
}
}
function _labjs_get_path() {
return drupal_get_path('module', 'labjs') . '/labjs.min.js';
}
function _labjs_prepare_required_js() {
return '<script type="text/javascript" src="' . labjs_build_uri(_labjs_get_path()) . '"></script>' . "\n" . '<script type="text/javascript">' . "\n" . '<!--//--><![CDATA[//><!--' . "\n" . "var \$L = \$LAB.setGlobalDefaults({AlwaysPreserveOrder:true});\n" . "//--><!]]>\n" . "</script>\n";
}