1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Provides frequently used functions that get theme info, settings and
|
6
|
* other data.
|
7
|
*/
|
8
|
|
9
|
/**
|
10
|
* Retrieve a setting for the current theme or for a given theme.
|
11
|
*
|
12
|
* AT's stripped down optimized version of theme_get_setting().
|
13
|
* Why? Because it wayfasterbetter, emphasis on the faster which is really the
|
14
|
* only valid reason to do something as stupid as this, and it is faster,
|
15
|
* considerably faster. Doing this wiped 100ms off page generation time.
|
16
|
*
|
17
|
* @param $setting_name
|
18
|
* @param null $theme
|
19
|
* @see http://api.drupal.org/api/drupal/includes!theme.inc/function/theme_get_setting/7
|
20
|
*/
|
21
|
function at_get_setting($setting_name, $theme = NULL) {
|
22
|
$cache = &drupal_static(__FUNCTION__, array());
|
23
|
|
24
|
// If no key is given, use the current theme if we can determine it.
|
25
|
if (!isset($theme)) {
|
26
|
$theme = !empty($GLOBALS['theme_key']) ? $GLOBALS['theme_key'] : '';
|
27
|
}
|
28
|
|
29
|
if (empty($cache[$theme])) {
|
30
|
|
31
|
// Get the values for the theme-specific settings from the .info files
|
32
|
if ($theme) {
|
33
|
$themes = list_themes();
|
34
|
$theme_object = $themes[$theme];
|
35
|
|
36
|
// Create a list which includes the current theme and all its base themes.
|
37
|
if (isset($theme_object->base_themes)) {
|
38
|
$theme_keys = array_keys($theme_object->base_themes);
|
39
|
$theme_keys[] = $theme;
|
40
|
}
|
41
|
else {
|
42
|
$theme_keys = array($theme);
|
43
|
}
|
44
|
|
45
|
foreach ($theme_keys as $theme_key) {
|
46
|
if (!empty($themes[$theme_key]->info['settings'])) {
|
47
|
$cache[$theme] = $themes[$theme_key]->info['settings'];
|
48
|
}
|
49
|
}
|
50
|
|
51
|
// Get the saved theme-specific settings from the database.
|
52
|
$cache[$theme] = array_merge($cache[$theme], variable_get('theme_' . $theme . '_settings', array()));
|
53
|
}
|
54
|
}
|
55
|
|
56
|
return isset($cache[$theme][$setting_name]) ? $cache[$theme][$setting_name] : NULL;
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* Return the info file array for a particular theme, usually the active theme.
|
61
|
* Simple wrapper function for list_themes().
|
62
|
*
|
63
|
* @param $theme_name
|
64
|
*/
|
65
|
function at_get_info($theme_name) {
|
66
|
$info = &drupal_static(__FUNCTION__, array());
|
67
|
if (empty($info)) {
|
68
|
$lt = list_themes();
|
69
|
foreach ($lt as $key => $value) {
|
70
|
if ($theme_name == $key) {
|
71
|
$info = $lt[$theme_name]->info;
|
72
|
}
|
73
|
}
|
74
|
}
|
75
|
|
76
|
return $info;
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
* Returns an array keyed by theme name.
|
81
|
*
|
82
|
* Return the all the info file data for a particular theme including base
|
83
|
* themes. Parts of this function are shamelessly ripped from Drupal core's
|
84
|
* _drupal_theme_initialize().
|
85
|
*
|
86
|
* @param $theme_name, usually the active theme.
|
87
|
*/
|
88
|
function at_get_info_trail($theme_name) {
|
89
|
$info_trail = &drupal_static(__FUNCTION__, array());
|
90
|
if (empty($info_trail)) {
|
91
|
$lt = list_themes();
|
92
|
|
93
|
// First check for base themes and get info
|
94
|
$base_theme = array();
|
95
|
$ancestor = $theme_name;
|
96
|
while ($ancestor && isset($lt[$ancestor]->base_theme)) {
|
97
|
$ancestor = $lt[$ancestor]->base_theme;
|
98
|
$base_theme[] = $lt[$ancestor];
|
99
|
}
|
100
|
foreach ($base_theme as $base) {
|
101
|
$info_trail[$base->name]['info'] = $base->info;
|
102
|
}
|
103
|
|
104
|
// Now the active theme
|
105
|
$info_trail[$theme_name]['info'] = $lt[$theme_name]->info;
|
106
|
}
|
107
|
|
108
|
return $info_trail;
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Return lists of CSS files to unset.
|
113
|
*
|
114
|
* This returns a very small amount of data (< 10kb), but is very expensive
|
115
|
* to get which is why its cached in the database. Without caching the theme
|
116
|
* settings page takes at least 8 seconds to load if the Unset CSS extension
|
117
|
* is enabled, and thats no fun.
|
118
|
*
|
119
|
* @param $theme_name, usually the active theme.
|
120
|
*/
|
121
|
function at_get_css_files($theme_name) {
|
122
|
$styles = &drupal_static(__FUNCTION__, array());
|
123
|
if (empty($styles)) {
|
124
|
if ($cache = cache_get('at_get_css_files')) {
|
125
|
$styles = $cache->data;
|
126
|
}
|
127
|
else {
|
128
|
|
129
|
// Contrib module
|
130
|
$module_data = _system_rebuild_module_data();
|
131
|
foreach ($module_data as $module => $data) {
|
132
|
//if ($data->status && !empty($data->info['stylesheets'])) {
|
133
|
if (module_exists($module) && !empty($data->info['stylesheets'])) {
|
134
|
foreach ($data->info['stylesheets'] as $media => $content) {
|
135
|
foreach ($content as $file) {
|
136
|
$styles[$file] = array(
|
137
|
'type' => 'contrib',
|
138
|
'source' => $module,
|
139
|
);
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
}
|
144
|
|
145
|
// Libraries
|
146
|
if (module_exists('libraries')) {
|
147
|
$libraries = libraries_get_libraries();
|
148
|
$extension = 'css';
|
149
|
foreach ($libraries as $lib) {
|
150
|
$files = file_scan_directory($lib, '/\.' . $extension . '$/', array('recurse' => TRUE));
|
151
|
$libraries[$lib] = $files;
|
152
|
}
|
153
|
foreach ($libraries as $lib_key => $library) {
|
154
|
if (is_array($library) && !empty($library)) {
|
155
|
foreach ($library as $lib_file) {
|
156
|
$styles[$lib_file->uri] = array(
|
157
|
'type' => 'library',
|
158
|
'source' => $lib_file->uri,
|
159
|
);
|
160
|
}
|
161
|
}
|
162
|
}
|
163
|
}
|
164
|
|
165
|
// Theme info defined files
|
166
|
$info_data = at_get_info_trail($theme_name);
|
167
|
foreach ($info_data as $provider => $theme_info) {
|
168
|
foreach ($theme_info as $this_info => $info) {
|
169
|
if ($this_info == 'info') {
|
170
|
// AT Core defined
|
171
|
if (isset($info['unset_core'])) {
|
172
|
foreach ($info['unset_core'] as $module_path => $description) {
|
173
|
$styles[$module_path] = array(
|
174
|
'type' => 'unset_core',
|
175
|
'source' => $description,
|
176
|
);
|
177
|
}
|
178
|
}
|
179
|
// Active theme defined explicit excludes
|
180
|
if (isset($info['unset_css'])) {
|
181
|
foreach ($info['unset_css'] as $module_path => $description) {
|
182
|
$styles[$module_path] = array(
|
183
|
'type' => 'user_defined',
|
184
|
'source' => $description,
|
185
|
);
|
186
|
}
|
187
|
}
|
188
|
// Active theme and ancestors, never show in admin
|
189
|
foreach ($info['stylesheets'] as $media => $stylesheets) {
|
190
|
foreach ($stylesheets as $file => $path) {
|
191
|
$styles[$path] = array(
|
192
|
'type' => 'theme',
|
193
|
'source' => $provider,
|
194
|
);
|
195
|
}
|
196
|
}
|
197
|
}
|
198
|
}
|
199
|
}
|
200
|
|
201
|
cache_set('at_get_css_files', $styles, 'cache');
|
202
|
}
|
203
|
}
|
204
|
|
205
|
return $styles;
|
206
|
}
|
207
|
|
208
|
/**
|
209
|
* Return a device context.
|
210
|
* Wrapper for mobile_detect_get_object() and
|
211
|
* browscap_get_browser().
|
212
|
*/
|
213
|
function at_get_browser() {
|
214
|
$browser = &drupal_static(__FUNCTION__, array());
|
215
|
if (empty($browser)) {
|
216
|
$browser['is_mobile'] = NULL;
|
217
|
$browser['is_tablet'] = NULL;
|
218
|
|
219
|
if (function_exists('mobile_detect_get_object')) {
|
220
|
$detect = mobile_detect_get_object();
|
221
|
if ($detect) {
|
222
|
$browser['is_mobile'] = $detect->isMobile();
|
223
|
$browser['is_tablet'] = $detect->isTablet();
|
224
|
}
|
225
|
}
|
226
|
else if (function_exists('browscap_get_browser')) {
|
227
|
$browscap = browscap_get_browser();
|
228
|
if (isset($browscap['ismobiledevice'])) {
|
229
|
if (filter_var($browscap['ismobiledevice'], FILTER_VALIDATE_BOOLEAN) == TRUE) {
|
230
|
$browser['is_mobile'] = TRUE;
|
231
|
}
|
232
|
}
|
233
|
}
|
234
|
}
|
235
|
|
236
|
return $browser;
|
237
|
}
|