Util_Theme.php
3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace W3TC;
class Util_Theme {
static public function get( $themename ) {
$wp_themes = Util_Theme::get_themes();
if ( is_array( $wp_themes ) && array_key_exists( $themename, $wp_themes ) )
return $wp_themes[ $themename ];
return array();
}
static public function get_current_theme_name() {
return wp_get_theme()->get( 'Name' );
}
static public function get_current_theme() {
return wp_get_theme();
}
static public function get_themes() {
global $wp_themes;
if ( isset( $wp_themes ) )
return $wp_themes;
$themes = wp_get_themes();
$wp_themes = array();
foreach ( $themes as $theme ) {
$name = $theme->get( 'Name' );
if ( isset( $wp_themes[$name] ) )
$wp_themes[$name . '/' . $theme->get_stylesheet()] = $theme;
else
$wp_themes[$name] = $theme;
}
return $wp_themes;
}
/**
* Returns theme key
*
* @param string $theme_root
* @param string $template
* @param string $stylesheet
* @return string
*/
static public function get_theme_key( $theme_root, $template, $stylesheet ) {
$theme_path = ltrim( str_replace( WP_CONTENT_DIR, '',
Util_Environment::normalize_path( $theme_root ) ), '/' );
return substr( md5( $theme_path . $template . $stylesheet ), 0, 5 );
}
/**
* Returns themes array
*
* @return array
*/
static public function get_themes_by_key() {
$themes = array();
$wp_themes = Util_Theme::get_themes();
foreach ( $wp_themes as $wp_theme ) {
$theme_key = Util_Theme::get_theme_key( $wp_theme['Theme Root'], $wp_theme['Template'], $wp_theme['Stylesheet'] );
$themes[$theme_key] = $wp_theme['Name'];
}
return $themes;
}
/**
* Returns minify groups
*
* @param string $theme_name
* @return array
*/
static public function get_theme_templates( $theme_name ) {
$groups = array(
'default' => __( 'All Templates', 'w3-total-cache' )
);
$templates = Util_Theme::get_theme_files( $theme_name );
foreach ( $templates as $template ) {
$basename = basename( $template, '.php' );
$groups[$basename] = ucfirst( $basename );
}
return $groups;
}
/**
* Returns array of theme groups
*
* @param string $theme_name
* @return array
*/
static public function get_theme_files( $theme_name ) {
$patterns = array(
'404',
'search',
'taxonomy(-.*)?',
'front-page',
'home',
'index',
'(image|video|text|audio|application).*',
'attachment',
'single(-.*)?',
'page(-.*)?',
'category(-.*)?',
'tag(-.*)?',
'author(-.*)?',
'date',
'archive',
'comments-popup',
'paged'
);
$templates = array();
$theme = Util_Theme::get( $theme_name );
if ( $theme && isset( $theme['Template Files'] ) ) {
$template_files = (array) $theme['Template Files'];
foreach ( $template_files as $template_file ) {
/**
* Check file name
*/
$template = basename( $template_file, '.php' );
foreach ( $patterns as $pattern ) {
$regexp = '~^' . $pattern . '$~';
if ( preg_match( $regexp, $template ) ) {
$templates[] = $template_file;
continue 2;
}
}
/**
* Check get_header function call
*/
$template_content = @file_get_contents( $template_file );
if ( $template_content && preg_match( '~\s*get_header[0-9_]*\s*\(~', $template_content ) ) {
$templates[] = $template_file;
}
}
sort( $templates );
reset( $templates );
}
return $templates;
}
}