PHP Cross Reference of WordPress Subversion HEAD |
| [ Index ] [ Classes ] [ Functions ] [ Variables ] [ Constants ] |
[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_option('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 = 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_locale_stylesheet_uri() { 29 global $wp_locale; 30 $stylesheet_dir_uri = get_stylesheet_directory_uri(); 31 $dir = get_stylesheet_directory(); 32 $locale = get_locale(); 33 if ( file_exists("$dir/$locale.css") ) 34 $stylesheet_uri = "$stylesheet_dir_uri/$locale.css"; 35 elseif ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") ) 36 $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css"; 37 else 38 $stylesheet_uri = ''; 39 return apply_filters('locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri); 40 } 41 42 function get_template() { 43 return apply_filters('template', get_option('template')); 44 } 45 46 function get_template_directory() { 47 $template = get_template(); 48 $template_dir = get_theme_root() . "/$template"; 49 return apply_filters('template_directory', $template_dir, $template); 50 } 51 52 function get_template_directory_uri() { 53 $template = get_template(); 54 $template_dir_uri = get_theme_root_uri() . "/$template"; 55 return apply_filters('template_directory_uri', $template_dir_uri, $template); 56 } 57 58 function get_theme_data( $theme_file ) { 59 $themes_allowed_tags = array( 60 'a' => array( 61 'href' => array(),'title' => array() 62 ), 63 'abbr' => array( 64 'title' => array() 65 ), 66 'acronym' => array( 67 'title' => array() 68 ), 69 'code' => array(), 70 'em' => array(), 71 'strong' => array() 72 ); 73 74 $theme_data = implode( '', file( $theme_file ) ); 75 $theme_data = str_replace ( '\r', '\n', $theme_data ); 76 preg_match( '|Theme Name:(.*)$|mi', $theme_data, $theme_name ); 77 preg_match( '|Theme URI:(.*)$|mi', $theme_data, $theme_uri ); 78 preg_match( '|Description:(.*)$|mi', $theme_data, $description ); 79 preg_match( '|Author:(.*)$|mi', $theme_data, $author_name ); 80 preg_match( '|Author URI:(.*)$|mi', $theme_data, $author_uri ); 81 preg_match( '|Template:(.*)$|mi', $theme_data, $template ); 82 83 if ( preg_match( '|Version:(.*)|i', $theme_data, $version ) ) 84 $version = wp_kses( trim( $version[1] ), $themes_allowed_tags ); 85 else 86 $version = ''; 87 88 if ( preg_match('|Status:(.*)|i', $theme_data, $status) ) 89 $status = wp_kses( trim( $status[1] ), $themes_allowed_tags ); 90 else 91 $status = 'publish'; 92 93 $name = $theme = wp_kses( trim( $theme_name[1] ), $themes_allowed_tags ); 94 $theme_uri = clean_url( trim( $theme_uri[1] ) ); 95 $description = wptexturize( wp_kses( trim( $description[1] ), $themes_allowed_tags ) ); 96 $template = wp_kses( trim( $template[1] ), $themes_allowed_tags ); 97 98 $author_uri = clean_url( trim( $author_uri[1] ) ); 99 100 if ( empty( $author_uri[1] ) ) { 101 $author = wp_kses( trim( $author_name[1] ), $themes_allowed_tags ); 102 } else { 103 $author = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $author_uri, __( 'Visit author homepage' ), wp_kses( trim( $author_name[1] ), $themes_allowed_tags ) ); 104 } 105 106 return array( 'Name' => $name, 'Title' => $theme, 'URI' => $theme_uri, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Status' => $status ); 107 } 108 109 function get_themes() { 110 global $wp_themes, $wp_broken_themes; 111 112 if ( isset($wp_themes) ) 113 return $wp_themes; 114 115 $themes = array(); 116 $wp_broken_themes = array(); 117 $theme_loc = $theme_root = get_theme_root(); 118 if ( '/' != ABSPATH ) // don't want to replace all forward slashes, see Trac #4541 119 $theme_loc = str_replace(ABSPATH, '', $theme_root); 120 121 // Files in wp-content/themes directory and one subdir down 122 $themes_dir = @ opendir($theme_root); 123 if ( !$themes_dir ) 124 return false; 125 126 while ( ($theme_dir = readdir($themes_dir)) !== false ) { 127 if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) { 128 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' ) 129 continue; 130 $stylish_dir = @ opendir($theme_root . '/' . $theme_dir); 131 $found_stylesheet = false; 132 while ( ($theme_file = readdir($stylish_dir)) !== false ) { 133 if ( $theme_file == 'style.css' ) { 134 $theme_files[] = $theme_dir . '/' . $theme_file; 135 $found_stylesheet = true; 136 break; 137 } 138 } 139 @closedir($stylish_dir); 140 if ( !$found_stylesheet ) { // look for themes in that dir 141 $subdir = "$theme_root/$theme_dir"; 142 $subdir_name = $theme_dir; 143 $theme_subdir = @ opendir( $subdir ); 144 while ( ($theme_dir = readdir($theme_subdir)) !== false ) { 145 if ( is_dir( $subdir . '/' . $theme_dir) && is_readable($subdir . '/' . $theme_dir) ) { 146 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' ) 147 continue; 148 $stylish_dir = @ opendir($subdir . '/' . $theme_dir); 149 $found_stylesheet = false; 150 while ( ($theme_file = readdir($stylish_dir)) !== false ) { 151 if ( $theme_file == 'style.css' ) { 152 $theme_files[] = $subdir_name . '/' . $theme_dir . '/' . $theme_file; 153 $found_stylesheet = true; 154 break; 155 } 156 } 157 @closedir($stylish_dir); 158 } 159 } 160 @closedir($theme_subdir); 161 $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.')); 162 } 163 } 164 } 165 @closedir($theme_dir); 166 167 if ( !$themes_dir || !$theme_files ) 168 return $themes; 169 170 sort($theme_files); 171 172 foreach ( (array) $theme_files as $theme_file ) { 173 if ( !is_readable("$theme_root/$theme_file") ) { 174 $wp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.')); 175 continue; 176 } 177 178 $theme_data = get_theme_data("$theme_root/$theme_file"); 179 180 $name = $theme_data['Name']; 181 $title = $theme_data['Title']; 182 $description = wptexturize($theme_data['Description']); 183 $version = $theme_data['Version']; 184 $author = $theme_data['Author']; 185 $template = $theme_data['Template']; 186 $stylesheet = dirname($theme_file); 187 188 $screenshot = false; 189 foreach ( array('png', 'gif', 'jpg', 'jpeg') as $ext ) { 190 if (file_exists("$theme_root/$stylesheet/screenshot.$ext")) { 191 $screenshot = "screenshot.$ext"; 192 break; 193 } 194 } 195 196 if ( empty($name) ) { 197 $name = dirname($theme_file); 198 $title = $name; 199 } 200 201 if ( empty($template) ) { 202 if ( file_exists(dirname("$theme_root/$theme_file/index.php")) ) 203 $template = dirname($theme_file); 204 else 205 continue; 206 } 207 208 $template = trim($template); 209 210 if ( !file_exists("$theme_root/$template/index.php") ) { 211 $parent_dir = dirname(dirname($theme_file)); 212 if ( file_exists("$theme_root/$parent_dir/$template/index.php") ) { 213 $template = "$parent_dir/$template"; 214 } else { 215 $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.')); 216 continue; 217 } 218 } 219 220 $stylesheet_files = array(); 221 $stylesheet_dir = @ dir("$theme_root/$stylesheet"); 222 if ( $stylesheet_dir ) { 223 while ( ($file = $stylesheet_dir->read()) !== false ) { 224 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file) ) 225 $stylesheet_files[] = "$theme_loc/$stylesheet/$file"; 226 } 227 } 228 229 $template_files = array(); 230 $template_dir = @ dir("$theme_root/$template"); 231 if ( $template_dir ) { 232 while(($file = $template_dir->read()) !== false) { 233 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) ) 234 $template_files[] = "$theme_loc/$template/$file"; 235 } 236 } 237 238 $template_dir = dirname($template_files[0]); 239 $stylesheet_dir = dirname($stylesheet_files[0]); 240 241 if ( empty($template_dir) ) 242 $template_dir = '/'; 243 if ( empty($stylesheet_dir) ) 244 $stylesheet_dir = '/'; 245 246 // Check for theme name collision. This occurs if a theme is copied to 247 // a new theme directory and the theme header is not updated. Whichever 248 // theme is first keeps the name. Subsequent themes get a suffix applied. 249 // The Default and Classic themes always trump their pretenders. 250 if ( isset($themes[$name]) ) { 251 if ( ('WordPress Default' == $name || 'WordPress Classic' == $name) && 252 ('default' == $stylesheet || 'classic' == $stylesheet) ) { 253 // If another theme has claimed to be one of our default themes, move 254 // them aside. 255 $suffix = $themes[$name]['Stylesheet']; 256 $new_name = "$name/$suffix"; 257 $themes[$new_name] = $themes[$name]; 258 $themes[$new_name]['Name'] = $new_name; 259 } else { 260 $name = "$name/$stylesheet"; 261 } 262 } 263 264 $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); 265 } 266 267 // Resolve theme dependencies. 268 $theme_names = array_keys($themes); 269 270 foreach ( (array) $theme_names as $theme_name ) { 271 $themes[$theme_name]['Parent Theme'] = ''; 272 if ( $themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template'] ) { 273 foreach ( (array) $theme_names as $parent_theme_name ) { 274 if ( ($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template']) ) { 275 $themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name']; 276 break; 277 } 278 } 279 } 280 } 281 282 $wp_themes = $themes; 283 284 return $themes; 285 } 286 287 function get_theme($theme) { 288 $themes = get_themes(); 289 290 if ( array_key_exists($theme, $themes) ) 291 return $themes[$theme]; 292 293 return NULL; 294 } 295 296 function get_current_theme() { 297 if ( $theme = get_option('current_theme') ) 298 return $theme; 299 300 $themes = get_themes(); 301 $theme_names = array_keys($themes); 302 $current_template = get_option('template'); 303 $current_stylesheet = get_option('stylesheet'); 304 $current_theme = 'WordPress Default'; 305 306 if ( $themes ) { 307 foreach ( (array) $theme_names as $theme_name ) { 308 if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet && 309 $themes[$theme_name]['Template'] == $current_template ) { 310 $current_theme = $themes[$theme_name]['Name']; 311 break; 312 } 313 } 314 } 315 316 update_option('current_theme', $current_theme); 317 318 return $current_theme; 319 } 320 321 function get_theme_root() { 322 return apply_filters('theme_root', ABSPATH . "wp-content/themes"); 323 } 324 325 function get_theme_root_uri() { 326 return apply_filters('theme_root_uri', get_option('siteurl') . "/wp-content/themes", get_option('siteurl')); 327 } 328 329 function get_query_template($type) { 330 $template = ''; 331 if ( file_exists(TEMPLATEPATH . "/{$type}.php") ) 332 $template = TEMPLATEPATH . "/{$type}.php"; 333 334 return apply_filters("{$type}_template", $template); 335 } 336 337 function get_404_template() { 338 return get_query_template('404'); 339 } 340 341 function get_archive_template() { 342 return get_query_template('archive'); 343 } 344 345 function get_author_template() { 346 return get_query_template('author'); 347 } 348 349 function get_category_template() { 350 $template = ''; 351 if ( file_exists(TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php') ) 352 $template = TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php'; 353 elseif ( file_exists(TEMPLATEPATH . "/category.php") ) 354 $template = TEMPLATEPATH . "/category.php"; 355 356 return apply_filters('category_template', $template); 357 } 358 359 function get_tag_template() { 360 $template = ''; 361 if ( file_exists(TEMPLATEPATH . "/tag-" . get_query_var('tag') . '.php') ) 362 $template = TEMPLATEPATH . "/tag-" . get_query_var('tag') . '.php'; 363 elseif ( file_exists(TEMPLATEPATH . "/tag.php") ) 364 $template = TEMPLATEPATH . "/tag.php"; 365 366 return apply_filters('tag_template', $template); 367 } 368 369 370 function get_date_template() { 371 return get_query_template('date'); 372 } 373 374 function get_home_template() { 375 $template = ''; 376 377 if ( file_exists(TEMPLATEPATH . "/home.php") ) 378 $template = TEMPLATEPATH . "/home.php"; 379 elseif ( file_exists(TEMPLATEPATH . "/index.php") ) 380 $template = TEMPLATEPATH . "/index.php"; 381 382 return apply_filters('home_template', $template); 383 } 384 385 function get_page_template() { 386 global $wp_query; 387 388 $id = (int) $wp_query->post->ID; 389 $template = get_post_meta($id, '_wp_page_template', true); 390 391 if ( 'default' == $template ) 392 $template = ''; 393 394 if ( !empty($template) && file_exists(TEMPLATEPATH . "/$template") ) 395 $template = TEMPLATEPATH . "/$template"; 396 elseif ( file_exists(TEMPLATEPATH . "/page.php") ) 397 $template = TEMPLATEPATH . "/page.php"; 398 else 399 $template = ''; 400 401 return apply_filters('page_template', $template); 402 } 403 404 function get_paged_template() { 405 return get_query_template('paged'); 406 } 407 408 function get_search_template() { 409 return get_query_template('search'); 410 } 411 412 function get_single_template() { 413 return get_query_template('single'); 414 } 415 416 function get_attachment_template() { 417 global $posts; 418 $type = explode('/', $posts[0]->post_mime_type); 419 if ( $template = get_query_template($type[0]) ) 420 return $template; 421 elseif ( $template = get_query_template($type[1]) ) 422 return $template; 423 elseif ( $template =