View source
<?php
define('JS_SETTINGS', -90);
_magic_initialize_magic_vars();
function _magic_initialize_magic_vars() {
global $conf;
if (isset($conf['magic']) && is_array($conf['magic'])) {
foreach ($conf['magic'] as $theme_key => $settings) {
$settings_var = 'theme_' . $theme_key . '_settings';
$conf[$settings_var] = isset($conf[$settings_var]) ? array_merge($conf[$settings_var], $settings) : $settings;
}
}
}
function magic_menu() {
$items = array();
$items['admin/appearance/settings/%magic_theme/export'] = array(
'title' => 'Export theme settings',
'description' => 'Export theme settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'magic_export_settings',
3,
),
'access arguments' => array(
'administer themes',
),
'file' => 'includes/magic.export.inc',
);
return $items;
}
function magic_help($path, $arg) {
$output = '';
switch ($path) {
case 'admin/help#magic':
$output = '';
$output .= '<p>' . t('All Drupal sites need some magic, so give yours some! Magic is a set of tools for front-end best practices and general front-end goodies to make your life as a front-end developer happier.') . '</p>';
$output .= '<h2>' . t('Theme Settings Export') . '</h2>';
$output .= '<p>' . t('To ensure the most flexibility, Drupal pulls your theme settings from a variety of places before returning the current setting to the module or theme requesting it. It will first pull from the base theme\'s .info file, then the active theme\'s .info file, then the global theme settings form, then the active theme\'s setting form. Each check will override the previous, so the active theme settings form settings will always override the theme\'s .info file.') . '</p>';
$output .= '<p>' . t('To facilitate settings export, magic provides two methods of exporting your theme settings to version control. First, you can export your settings to a new .info file, although it should be noted that this will be overridden once settings are saved within the database. To keep control of variables within the database, you can either use !features and !strongarm to maintain the code, or add a $conf[] variable within your settings.php. Currently, we are working on exporting the $conf[] variable needed to put it within your settings.php.', array(
'!features' => l(t('Features'), 'http://drupal.org/project/features'),
'!strongarm' => l(t('Strongarm'), 'http://drupal.org/project/strongarm'),
)) . '</p>';
$output .= '<h2>' . t('Advanced CSS / JS Aggregation') . '</h2>';
$output .= '<p>' . t('Documentation coming soon!') . '</p>';
$output .= '<h2>' . t('Selective CSS / JS Removal') . '</h2>';
$output .= '<p>' . t('Documentation coming soon!') . '</p>';
break;
}
return $output;
}
function magic_module_implements_alter(&$implementations, $hook) {
switch ($hook) {
case 'css_alter':
case 'js_alter':
$group = $implementations['magic'];
unset($implementations['magic']);
$implementations['magic'] = $group;
if (isset($implementations['locale'])) {
$group = $implementations['locale'];
unset($implementations['locale']);
$implementations['locale'] = $group;
}
break;
case 'form_system_theme_settings_alter':
$group = $implementations['magic'];
unset($implementations['magic']);
$implementations = array(
'magic' => $group,
) + $implementations;
break;
}
}
function magic_theme_load($theme) {
if (array_key_exists($theme, list_themes())) {
return $theme;
}
return FALSE;
}
function magic_system_themes_page_alter(&$info) {
foreach (array(
'enabled',
'disabled',
) as $status) {
if (empty($info[$status])) {
continue;
}
foreach ($info[$status] as &$item) {
$item->operations[] = array(
'title' => t('Export'),
'href' => "admin/appearance/settings/{$item->name}/export",
'attributes' => array(
'title' => t('Export theme settings'),
),
);
}
}
}
function magic_form_system_theme_settings_alter(&$form, &$form_state) {
if (empty($form_state['build_info']['args'][0])) {
return;
}
module_load_include('inc', 'magic', 'includes/magic.settings');
$form_state['build_info']['files'][] = drupal_get_path('module', 'magic') . '/includes/magic.settings.inc';
magic_form_system_theme_settings($form, $form_state, $form_state['build_info']['args'][0]);
if (module_exists('magic_dev')) {
magic_dev_form_system_theme_settings($form, $form_state, $form_state['build_info']['args'][0]);
}
}
function magic_theme_registry_alter(&$registry) {
if (($index = array_search('template_process_html', $registry['html']['process functions'], TRUE)) !== FALSE) {
array_splice($registry['html']['process functions'], $index, 1, 'magic_template_process_html_override');
}
}
function magic_template_process_html_override(&$variables) {
$variables['page_top'] = drupal_render($variables['page']['page_top']);
$variables['page_bottom'] = drupal_render($variables['page']['page_bottom']);
$variables['page'] = $variables['page']['#children'];
$variables['head'] = drupal_get_html_head();
$variables['css'] = drupal_add_css();
$variables['styles'] = drupal_get_css();
if (theme_get_setting('magic_experimental_js')) {
module_load_include('inc', 'magic', 'includes/scripts-experimental');
$variables['page_bottom'] .= magic_experimental_js('footer');
$variables['scripts'] = magic_experimental_js('header');
}
elseif (theme_get_setting('magic_footer_js')) {
module_load_include('inc', 'magic', 'includes/scripts');
$variables['page_bottom'] .= magic_get_js('footer');
$variables['scripts'] = magic_get_js('header');
}
else {
$variables['page_bottom'] .= drupal_get_js('footer');
$variables['scripts'] = drupal_get_js();
}
}
function magic_clear_cache($theme = FALSE, $types = array()) {
if ($theme) {
if (empty($types) || $types['css'] == TRUE) {
cache_clear_all("{$theme}:css:", 'cache_magic', TRUE);
}
if (empty($types) || $types['js'] == TRUE) {
cache_clear_all("{$theme}:js:", 'cache_magic', TRUE);
}
}
else {
cache_clear_all(NULL, 'cache_magic');
}
}
function magic_flush_caches() {
return array(
'cache_magic',
);
}
function magic_css_alter(&$css) {
magic_css_js_alter($css, 'css');
}
function magic_js_alter(&$js) {
magic_css_js_alter($js, 'js');
}
function magic_css_js_alter(&$data, $type = 'css') {
$setting = "magic_{$type}_excludes";
if (!($excludes = theme_get_setting($setting))) {
return;
}
$hash = hash('md4', serialize($data));
$cid = "{$GLOBALS['theme_key']}:{$type}:{$hash}";
if ($cache = cache_get($cid, 'cache_magic')) {
$data = $cache->data;
return;
}
module_load_include('inc', 'magic', 'includes/magic.assets');
$regex_cid = "{$GLOBALS['theme_key']}:{$type}:runtime_excludes";
if (!($cache = cache_get($regex_cid, 'cache_magic'))) {
$items = array_filter(array_map('trim', explode("\n", $excludes)));
$steps = magic_assets_regex_steps($items);
cache_set($regex_cid, $steps, 'cache_magic', CACHE_TEMPORARY);
}
else {
$steps = $cache->data;
}
magic_assets_exclude($data, $steps);
cache_set($cid, $data, 'cache_magic', CACHE_TEMPORARY);
}