Projet

Général

Profil

Paste
Télécharger (6,99 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / themes / adaptivetheme / at_core / inc / get.inc @ 74f6bef0

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
          foreach ($data->info['stylesheets'] as $media => $content) {
134
            foreach ($content as $file) {
135
              $styles[$file] = array(
136
                'type' => 'contrib',
137
                'source' => $module,
138
              );
139
            }
140
          }
141
        }
142
      }
143

    
144
      // Libraries
145
      if (module_exists('libraries')) {
146
        $libraries = libraries_get_libraries();
147
        $extension = 'css';
148
        foreach ($libraries as $lib) {
149
          $files = file_scan_directory($lib, '/\.' . $extension . '$/', array('recurse' => TRUE));
150
          $libraries[$lib] = $files;
151
        }
152
        foreach ($libraries as $lib_key => $library) {
153
          if (is_array($library) && !empty($library)) {
154
            foreach ($library as $lib_file) {
155
              $styles[$lib_file->uri] = array(
156
                'type' => 'library',
157
                'source' => $lib_file->uri,
158
              );
159
            }
160
          }
161
        }
162
      }
163

    
164
      // Theme info defined files
165
      $info_data = at_get_info_trail($theme_name);
166
      foreach ($info_data as $provider => $theme_info) {
167
        foreach ($theme_info as $this_info => $info) {
168
          if ($this_info == 'info') {
169
            // AT Core defined
170
            if (isset($info['unset_core'])) {
171
              foreach ($info['unset_core'] as $module_path => $description) {
172
                $styles[$module_path] = array(
173
                  'type' => 'unset_core',
174
                  'source' => $description,
175
                );
176
              }
177
            }
178
            // Active theme defined explicit excludes
179
            if (isset($info['unset_css'])) {
180
              foreach ($info['unset_css'] as $module_path => $description) {
181
                $styles[$module_path] = array(
182
                  'type' => 'user_defined',
183
                  'source' => $description,
184
                );
185
              }
186
            }
187
            // Active theme and ancestors, never show in admin
188
            foreach ($info['stylesheets'] as $media => $stylesheets) {
189
              foreach ($stylesheets as $file => $path) {
190
                $styles[$path] = array(
191
                  'type' => 'theme',
192
                  'source' => $provider,
193
                );
194
              }
195
            }
196
          }
197
        }
198
      }
199

    
200
      cache_set('at_get_css_files', $styles, 'cache');
201
    }
202
  }
203

    
204
  return $styles;
205
}
206

    
207
/**
208
 * Return a device context.
209
 * Wrapper for mobile_detect_get_object() and
210
 * browscap_get_browser().
211
 */
212
function at_get_browser() {
213
  $browser = &drupal_static(__FUNCTION__, array());
214
  if (empty($browser)) {
215
    $browser['is_mobile'] = NULL;
216
    $browser['is_tablet'] = NULL;
217

    
218
    if (function_exists('mobile_detect_get_object')) {
219
      $detect = mobile_detect_get_object();
220
      if ($detect) {
221
        $browser['is_mobile'] = $detect->isMobile();
222
        $browser['is_tablet'] = $detect->isTablet();
223
      }
224
    }
225
    else if (function_exists('browscap_get_browser')) {
226
      $browscap = browscap_get_browser();
227
      if ($browscap) {
228
        if (filter_var($browscap['ismobiledevice'], FILTER_VALIDATE_BOOLEAN) == TRUE) {
229
          $browser['is_mobile'] = TRUE;
230
        }
231
      }
232
    }
233
  }
234

    
235
  return $browser;
236
}