| [ Index ] |
WordPress Source Cross Reference |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Theme/template/stylesheet functions. 4 */ 5 6 function get_stylesheet() { 7 return apply_filters('stylesheet', get_settings('stylesheet')); 8 } 9 10 function get_stylesheet_directory() { 11 $stylesheet = get_stylesheet(); 12 $stylesheet_dir = get_theme_root() . "/$stylesheet"; 13 return apply_filters('stylesheet_directory', $stylesheet_dir, $stylesheet); 14 } 15 16 function get_stylesheet_directory_uri() { 17 $stylesheet = rawurlencode( get_stylesheet() ); 18 $stylesheet_dir_uri = get_theme_root_uri() . "/$stylesheet"; 19 return apply_filters('stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet); 20 } 21 22 function get_stylesheet_uri() { 23 $stylesheet_dir_uri = get_stylesheet_directory_uri(); 24 $stylesheet_uri = $stylesheet_dir_uri . "/style.css"; 25 return apply_filters('stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri); 26 } 27 28 function get_template() { 29 return apply_filters('template', get_settings('template')); 30 } 31 32 function get_template_directory() { 33 $template = get_template(); 34 $template_dir = get_theme_root() . "/$template"; 35 return apply_filters('template_directory', $template_dir, $template); 36 } 37 38 function get_template_directory_uri() { 39 $template = get_template(); 40 $template_dir_uri = get_theme_root_uri() . "/$template"; 41 return apply_filters('template_directory_uri', $template_dir_uri, $template); 42 } 43 44 function get_theme_data($theme_file) { 45 $theme_data = implode('', file($theme_file)); 46 preg_match("|Theme Name:(.*)|i", $theme_data, $theme_name); 47 preg_match("|Theme URI:(.*)|i", $theme_data, $theme_uri); 48 preg_match("|Description:(.*)|i", $theme_data, $description); 49 preg_match("|Author:(.*)|i", $theme_data, $author_name); 50 preg_match("|Author URI:(.*)|i", $theme_data, $author_uri); 51 preg_match("|Template:(.*)|i", $theme_data, $template); 52 if ( preg_match("|Version:(.*)|i", $theme_data, $version) ) 53 $version = $version[1]; 54 else 55 $version =''; 56 if ( preg_match("|Status:(.*)|i", $theme_data, $status) ) 57 $status = $status[1]; 58 else 59 $status ='publish'; 60 61 $description = wptexturize($description[1]); 62 63 $name = $theme_name[1]; 64 $name = trim($name); 65 $theme = $name; 66 67 if ( '' == $author_uri[1] ) { 68 $author = $author_name[1]; 69 } else { 70 $author = '<a href="' . $author_uri[1] . '" title="' . __('Visit author homepage') . '">' . $author_name[1] . '</a>'; 71 } 72 73 return array('Name' => $name, 'Title' => $theme, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template[1], 'Status' => $status); 74 } 75 76 function get_themes() { 77 global $wp_themes; 78 global $wp_broken_themes; 79 80 if ( isset($wp_themes) ) 81 return $wp_themes; 82 83 $themes = array(); 84 $wp_broken_themes = array(); 85 $theme_root = get_theme_root(); 86 $theme_loc = str_replace(ABSPATH, '', $theme_root); 87 88 // Files in wp-content/themes directory 89 $themes_dir = @ dir($theme_root); 90 if ( $themes_dir ) { 91 while(($theme_dir = $themes_dir->read()) !== false) { 92 if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) { 93 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' ) { 94 continue; 95 } 96 $stylish_dir = @ dir($theme_root . '/' . $theme_dir); 97 $found_stylesheet = false; 98 while (($theme_file = $stylish_dir->read()) !== false) { 99 if ( $theme_file == 'style.css' ) { 100 $theme_files[] = $theme_dir . '/' . $theme_file; 101 $found_stylesheet = true; 102 break; 103 } 104 } 105 if ( !$found_stylesheet ) { 106 $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.')); 107 } 108 } 109 } 110 } 111 112 if ( !$themes_dir || !$theme_files ) { 113 return $themes; 114 } 115 116 sort($theme_files); 117 118 foreach($theme_files as $theme_file) { 119 if ( ! is_readable("$theme_root/$theme_file") ) { 120 $wp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.')); 121 continue; 122 } 123 124 $theme_data = get_theme_data("$theme_root/$theme_file"); 125 126 $name = $theme_data['Name']; 127 $title = $theme_data['Title']; 128 $description = wptexturize($theme_data['Description']); 129 $version = $theme_data['Version']; 130 $author = $theme_data['Author']; 131 $template = $theme_data['Template']; 132 $stylesheet = dirname($theme_file); 133 134 foreach (array('png', 'gif', 'jpg', 'jpeg') as $ext) { 135 if (file_exists("$theme_root/$stylesheet/screenshot.$ext")) { 136 $screenshot = "screenshot.$ext"; 137 break; 138 } 139 } 140 141 if ( empty($name) ) { 142 $name = dirname($theme_file); 143 $title = $name; 144 } 145 146 if ( empty($template) ) { 147 if ( file_exists(dirname("$theme_root/$theme_file/index.php")) ) { 148 $template = dirname($theme_file); 149 } else { 150 continue; 151 } 152 } 153 154 $template = trim($template); 155 156 if ( !file_exists("$theme_root/$template/index.php") ) { 157 $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.')); 158 continue; 159 } 160 161 $stylesheet_files = array(); 162 $stylesheet_dir = @ dir("$theme_root/$stylesheet"); 163 if ( $stylesheet_dir ) { 164 while(($file = $stylesheet_dir->read()) !== false) { 165 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file) ) 166 $stylesheet_files[] = "$theme_loc/$stylesheet/$file"; 167 } 168 } 169 170 $template_files = array(); 171 $template_dir = @ dir("$theme_root/$template"); 172 if ( $template_dir ) { 173 while(($file = $template_dir->read()) !== false) { 174 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) ) 175 $template_files[] = "$theme_loc/$template/$file"; 176 } 177 } 178 179 $template_dir = dirname($template_files[0]); 180 $stylesheet_dir = dirname($stylesheet_files[0]); 181 182 if ( empty($template_dir) ) 183 $template_dir = '/'; 184 if ( empty($stylesheet_dir) ) 185 $stylesheet_dir = '/'; 186 187 // Check for theme name collision. This occurs if a theme is copied to 188 // a new theme directory and the theme header is not updated. Whichever 189 // theme is first keeps the name. Subsequent themes get a suffix applied. 190 // The Default and Classic themes always trump their pretenders. 191 if ( isset($themes[$name]) ) { 192 if ( ('WordPress Default' == $name || 'WordPress Classic' == $name) && 193 ('default' == $stylesheet || 'classic' == $stylesheet) ) { 194 // If another theme has claimed to be one of our default themes, move 195 // them aside. 196 $suffix = $themes[$name]['Stylesheet']; 197 $new_name = "$name/$suffix"; 198 $themes[$new_name] = $themes[$name]; 199 $themes[$new_name]['Name'] = $new_name; 200 } else { 201 $name = "$name/$stylesheet"; 202 } 203 } 204 205 $themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Stylesheet' => $stylesheet, 'Template Files' => $template_files, 'Stylesheet Files' => $stylesheet_files, 'Template Dir' => $template_dir, 'Stylesheet Dir' => $stylesheet_dir, 'Status' => $theme_data['Status'], 'Screenshot' => $screenshot); 206 } 207 208 // Resolve theme dependencies. 209 $theme_names = array_keys($themes); 210 211 foreach ($theme_names as $theme_name) { 212 $themes[$theme_name]['Parent Theme'] = ''; 213 if ( $themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template'] ) { 214 foreach ($theme_names as $parent_theme_name) { 215 if ( ($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template']) ) { 216 $themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name']; 217 break; 218 } 219 } 220 } 221 } 222 223 $wp_themes = $themes; 224 225 return $themes; 226 } 227 228 function get_theme($theme) { 229 $themes = get_themes(); 230 231 if ( array_key_exists($theme, $themes) ) 232 return $themes[$theme]; 233 234 return NULL; 235 } 236 237 function get_current_theme() { 238 $themes = get_themes(); 239 $theme_names = array_keys($themes); 240 $current_template = get_settings('template'); 241 $current_stylesheet = get_settings('stylesheet'); 242 $current_theme = 'WordPress Default'; 243 244 if ( $themes ) { 245 foreach ($theme_names as $theme_name) { 246 if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet && 247 $themes[$theme_name]['Template'] == $current_template ) { 248 $current_theme = $themes[$theme_name]['Name']; 249 break; 250 } 251 } 252 } 253 254 return $current_theme; 255 } 256 257 function get_theme_root() { 258 return apply_filters('theme_root', ABSPATH . "wp-content/themes"); 259 } 260 261 function get_theme_root_uri() { 262 return apply_filters('theme_root_uri', get_settings('siteurl') . "/wp-content/themes", get_settings('siteurl')); 263 } 264 265 function get_query_template($type) { 266 $template = ''; 267 if ( file_exists(TEMPLATEPATH . "/{$type}.php") ) 268 $template = TEMPLATEPATH . "/{$type}.php"; 269 270 return apply_filters("{$type}_template", $template); 271 } 272 273 function get_404_template() { 274 return get_query_template('404'); 275 } 276 277 function get_archive_template() { 278 return get_query_template('archive'); 279 } 280 281 function get_author_template() { 282 return get_query_template('author'); 283 } 284 285 function get_category_template() { 286 $template = ''; 287 if ( file_exists(TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php') ) 288 $template = TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php'; 289 else if ( file_exists(TEMPLATEPATH . "/category.php") ) 290 $template = TEMPLATEPATH . "/category.php"; 291 292 return apply_filters('category_template', $template); 293 } 294 295 function get_date_template() { 296 return get_query_template('date'); 297 } 298 299 function get_home_template() { 300 $template = ''; 301 302 if ( file_exists(TEMPLATEPATH . "/home.php") ) 303 $template = TEMPLATEPATH . "/home.php"; 304 else if ( file_exists(TEMPLATEPATH . "/index.php") ) 305 $template = TEMPLATEPATH . "/index.php"; 306 307 return apply_filters('home_template', $template); 308 } 309 310 function get_page_template() { 311 global $wp_query; 312 313 $id = $wp_query->post->ID; 314 $template = get_post_meta($id, '_wp_page_template', true); 315 316 if ( 'default' == $template ) 317 $template = ''; 318 319 if ( ! empty($template) && file_exists(TEMPLATEPATH . "/$template") ) 320 $template = TEMPLATEPATH . "/$template"; 321 else if ( file_exists(TEMPLATEPATH . "/page.php") ) 322 $template = TEMPLATEPATH . "/page.php"; 323 else 324 $template = ''; 325 326 return apply_filters('page_template', $template); 327 } 328 329 function get_paged_template() { 330 return get_query_template('paged'); 331 } 332 333 function get_search_template() { 334 return get_query_template('search'); 335 } 336 337 function get_single_template() { 338 return get_query_template('single'); 339 } 340 341 function get_attachment_template() { 342 global $posts; 343 $type = explode('/', $posts[0]->post_mime_type); 344 if ( $template = get_query_template($type[0]) ) 345 return $template; 346 elseif ( $template = get_query_template($type[1]) ) 347 return $template; 348 elseif ( $template = get_query_template("$type[0]_$type[1]") ) 349 return $template; 350 else 351 return get_query_template('attachment'); 352 } 353 354 function get_comments_popup_template() { 355 if ( file_exists( TEMPLATEPATH . '/comments-popup.php') ) 356 $template = TEMPLATEPATH . '/comments-popup.php'; 357 else 358 $template = get_theme_root() . '/default/comments-popup.php'; 359 360 return apply_filters('comments_popup_template', $template); 361 } 362 363 function load_template($file) { 364 global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query, 365 $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment; 366 367 extract($wp_query->query_vars); 368 369 require_once($file); 370 } 371 372 function validate_current_theme() { 373 if ((get_template() != 'default') && (!file_exists(get_template_directory() . '/index.php'))) { 374 update_option('template', 'default'); 375 update_option('stylesheet', 'default'); 376 do_action('switch_theme', 'Default'); 377 return false; 378 } 379 380 if ((get_stylesheet() != 'default') && (!file_exists(get_template_directory() . '/style.css'))) { 381 update_option('template', 'default'); 382 update_option('stylesheet', 'default'); 383 do_action('switch_theme', 'Default'); 384 return false; 385 } 386 387 return true; 388 } 389 390 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat Jul 15 11:57:04 2006 | Courtesy of Taragana |