'header', 'media' => 'all', 'basename' => NULL, 'external' => FALSE, 'preprocess' => TRUE, 'every_page' => TRUE, 'type' => 'file', 'group' => CSS_THEME, 'condition-string' => NULL, 'browsers' => array(), //'use_core' => FALSE, 'attributes' => NULL, 'weight' => NULL, //Not supported, only to prevent PHP NOTICE 'cache' => TRUE, //'defer' => FALSE ); foreach ($info['stylesheets-extended'] as $file => $file_info) if (!empty($file_info) && is_array($file_info)) { $file_info = array_merge($file_info_default, $file_info); if (preg_match('%http:\/\/%si', $file)) { $file_info['external'] = TRUE; } if (!isset($file_info['media']) || empty($file_info['media'])) { $file_info['media'] = 'all'; } //if (!$file_info['condition-string'] && !$file_info['external'] && !$file_info['attributes']) { //$info['stylesheets'][$file_info['media']][] = $file; //unset($info['stylesheets-extended'][$file]); //} else { $info['stylesheets-extended'][$file] = $file_info; //} } $file_info_default = array( 'scope' => 'header', 'external' => FALSE, 'every_page' => TRUE, 'type' => 'file', 'group' => JS_THEME, 'weight' => NULL, 'condition-string' => NULL, 'browsers' => array(), 'defer' => FALSE, 'cache' => TRUE, 'preprocess' => TRUE, //'use_core' => FALSE, 'attributes' => NULL, 'variant' => NULL //for use with type library ); foreach ($info['scripts-extended'] as $file => $file_info) if (!empty($file_info) && is_array($file_info)) { $file_info = array_merge($file_info_default, $file_info); if (preg_match('%http:\/\/%si', $file)) { $file_info['external'] = TRUE; } if (!$file_info['condition-string'] && !$file_info['external'] && !$file_info['attributes']) { $file_info['use_core'] = TRUE; } $info['scripts-extended'][$file] = $file_info; } } } /** * Implements MODULE_preprocess_html(); adds conditional CSS from the .info file. */ function css_js_total_control_preprocess_html(&$vars, $hook) { // Make a list of base themes and the current theme. $themes = $GLOBALS['base_theme_info']; $themes[] = $GLOBALS['theme_info']; foreach (array_keys($themes) as $key) { $theme_path = dirname($themes[$key]->filename) . '/'; $stuff_to_add = array(); if (isset($themes[$key]->info['stylesheets-extended'])) { $stuff_to_add['stylesheets-extended'] = $themes[$key]->info['stylesheets-extended']; } if (isset($themes[$key]->info['scripts-extended'])) { $stuff_to_add['scripts-extended'] = $themes[$key]->info['scripts-extended']; } if (!empty($stuff_to_add)) { foreach ($stuff_to_add as $stuff_type => $stuff) { foreach ($stuff as $item_key => $item_info) { $item_path = $theme_path . $item_key; $data = $item_path; if ($item_info['type'] == 'inline') { $data = file_get_contents($item_path); } elseif ($item_info['type'] == 'library') { $data = $item_key; } elseif ($item_info['type'] == 'setting') { $data = array($item_key => $item_info['setting']); } elseif (strpos($item_key, 'http:') !== FALSE) { $item_info['type'] = 'external'; $data = $item_key; } if ($stuff_type == 'stylesheets-extended') { switch ($item_info['group']) { case 'system': $item_info['group'] = CSS_SYSTEM; break; case 'theme': $item_info['group'] = CSS_THEME; break; case 'default': $item_info['group'] = CSS_DEFAULT; break; } $options = array( 'type' => $item_info['type'], 'basename' => $item_info['basename'], 'group' => $item_info['group'], 'every_page' => $item_info['every_page'], 'weight' => $item_info['weight'], 'media' => $item_info['media'], 'preprocess' => $item_info['preprocess'], 'browsers' => array( 'IE' => (isset($item_info['condition-string'])) ? $item_info['condition-string'] : TRUE, '!IE' => (isset($item_info['condition-string']) && strpos($item_info['condition-string'], '!IE') === FALSE)? FALSE : TRUE, ), //Extended Parameters 'attributes' => (isset($item_info['attributes']) || isset($item_info['attributes']['id']))? array('id' => $item_info['attributes']['id']) : NULL ); drupal_add_css( $data, $options ); } if ($stuff_type == 'scripts-extended') { switch ($item_info['group']) { case 'library': $item_info['group'] = JS_LIBRARY; break; case 'theme': $item_info['group'] = JS_THEME; break; case 'default': $item_info['group'] = JS_DEFAULT; break; } if ($item_info['type'] == 'library') { if (module_exists('libraries')) { if ($item_info['variant']) { $item_info['variant'] = NULL; } // Load easing library libraries_load($data); } } else { $options = array( 'type' => $item_info['type'], 'scope' => $item_info['scope'], 'group' => $item_info['group'], 'every_page' => $item_info['every_page'], 'weight' => $item_info['weight'], 'cache' => $item_info['cache'], 'defer' => $item_info['defer'], 'preprocess' => $item_info['preprocess'], 'browsers' => array( 'IE' => (isset($item_info['condition-string'])) ? $item_info['condition-string'] : TRUE, '!IE' => (isset($item_info['condition-string']) && strpos($item_info['condition-string'], '!IE') === FALSE)? FALSE : TRUE, ), //Extended Parameters 'attributes' => (isset($item_info['attributes']))? $item_info['attributes'] : NULL ); drupal_add_js( $data, $options ); } } } } } } } /** * Implements MODULE_preprocess_maintenance_page(). */ function css_js_total_control_preprocess_maintenance_page(&$vars, $hook) { //conditional_styles_preprocess_html($vars, $hook); } /** * Implements HOOK_enable(). */ function css_js_total_control_enable() { // Flush the .info file cache, the page cache and the theme registry. drupal_flush_all_caches(); } /** * Applies filtering rules files on real-time * Implements hook_js_alter * * @param mixed $javascript */ function css_js_total_control_js_alter(&$javascript) { $info = _css_js_total_control_get_theme_info(); $filters = _css_js_total_control_get_theme_filters($info); if ($filters) { $files_tmp = $javascript; $n = 0; foreach ($files_tmp as $file => $file_info) { $n++; $file_info['file_type'] = 'stylesheet'; if (_css_js_total_control_filter_css_js(array($file => $file_info), $filters)) { unset($files_tmp[$file]); } } $javascript = $files_tmp; } } /** * Applies filtering rules files on real-time * Implements hook_css_alter * * @param mixed $css */ function css_js_total_control_css_alter(&$css) { $info = _css_js_total_control_get_theme_info(); $filters = _css_js_total_control_get_theme_filters($info); if ($filters) { $files_tmp = $css; $n = 0; foreach ($files_tmp as $file => $file_info) { $n++; $file_info['file_type'] = 'stylesheet'; if (_css_js_total_control_filter_css_js(array($file => $file_info), $filters)) { unset($files_tmp[$file]); } } $css = $files_tmp; } } /** * Add css js files added through new ini parameters to the Drupal css/js array list * */ function _css_js_total_control_append_css_js() { $theme = $GLOBALS['theme']; $var_name = 'css_js_total_control_' . $theme . '_info'; $info = variable_get($var_name, ''); drupal_add_js(array('theme' => array('path' => css_js_total_control_get_theme_path())), 'setting'); //$filters = array(); if ($info) { $info = unserialize($info); $files = $info['files']; foreach ($files as $file => $file_info) { $data = $file_info['path'] . '/' . $file; if ($file_info['type'] == 'inline') { $data = $file_info['source']; } if ($file_info['type'] == 'file') { $file_info['type'] = $file_info['group']; } if ($file_info['file_type'] == 'stylesheet' && !isset($file_info['condition-string'])) { drupal_add_css( $data, $type = $file_info['type'], $media = $file_info['media'], $preprocess = $file_info['preprocess'] ); } if ($file_info['file_type'] == 'javascript' && !isset($file_info['condition-string'])) { drupal_add_js( $data, $type = $file_info['type'], $scope = $file_info['scope'], $defer = $file_info['defer'], $cache = $file_info['cache'], $preprocess = $file_info['preprocess'] ); } } } } /** * Filter js/css files that are going to be included according to rules * * @param $files * @param $filters * @return bool|null */ function _css_js_total_control_filter_css_js($files, $filters) { if ($filters) { $filtered = NULL; $field_value = NULL; foreach ($files as $file => $file_info) { #--(Begin)--> Filter style and scripts if ($filters[$file_info['file_type']]) { if ($filters[$file_info['file_type']]['type'] == 'whitelist') { $filtered = TRUE; } else { $filtered = FALSE; } $item_filtered = NULL; foreach ($filters[$file_info['file_type']]['rules'] as $rule) { if (!isset($rule['field']) || $rule['field'] == 'file_address') { $field_value = $file; } elseif (isset($rule['field'])) { $field_value = $file_info[$rule['field']]; } if ($rule['function'] == 'wildcard') { if (fnmatch($rule['pattern'], $field_value)) { if ($filters[$file_info['file_type']]['type'] == 'whitelist') { $item_filtered = FALSE; } else { $item_filtered = TRUE; } } } if ($rule['function'] == 'regular_expression') { if (preg_match($rule['pattern'], $field_value)) { if ($filters[$file_info['file_type']]['type'] == 'whitelist') { $item_filtered = FALSE; } else { $item_filtered = TRUE; } } } if (!is_null($item_filtered)) { $filtered = $item_filtered; break; } } } #--(End)--> Filter style and scripts } return $filtered; } return FALSE; } /** * Get more information about the current theme, it will be used for parsing, and applying * css/js configurations on info file * the result is actually the .info file content * * @return array */ function _css_js_total_control_get_theme_info() { $info = $GLOBALS['theme_info']; if ($info) { $info = $GLOBALS['theme_info']->info; } return $info; } /** * Return the path to the current theme * * @return string */ function css_js_total_control_get_theme_path() { $theme_path = base_path() . path_to_theme(); return $theme_path . '/'; } /** * Find info lines related to filter system and convert them into an structured array * * @param $info * @return array */ function _css_js_total_control_get_theme_filters($info) { static $filters; if (!$filters && $info) { foreach (array('scripts' => 'javascript', 'stylesheets' => 'stylesheet') as $_file_type => $file_type) { if (isset($info[$_file_type . '-settings']) && isset($info[$_file_type . '-settings']['filter']) && !empty($info[$_file_type . '-settings']['filter'])) { $filters[$file_type] = $info[$_file_type . '-settings']['filter']; if (!isset($filters[$file_type])) { $filters[$file_type] = 'blacklist'; } } } } return $filters; }