Ticket #10467: register_theme_directory-003.patch
File register_theme_directory-003.patch, 17.9 KB (added by , 16 years ago) |
---|
-
Users/andy/Sites/wptrunk/wp-includes/theme.php
34 34 */ 35 35 function get_stylesheet_directory() { 36 36 $stylesheet = get_stylesheet(); 37 $stylesheet_dir = get_theme_root() . "/$stylesheet"; 38 return apply_filters('stylesheet_directory', $stylesheet_dir, $stylesheet); 37 $theme_roots = get_option( 'theme_roots' ); 38 39 if ( $theme_roots[$stylesheet] ) 40 add_filter( 'theme_root', create_function( '', 'return "' . WP_CONTENT_DIR . '/' . $theme_roots[$stylesheet] . '";' ) ); 41 42 $theme_root = get_theme_root(); 43 $stylesheet_dir = "$theme_root/$stylesheet"; 44 45 return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root ); 39 46 } 40 47 41 48 /** … … 47 54 */ 48 55 function get_stylesheet_directory_uri() { 49 56 $stylesheet = get_stylesheet(); 50 $stylesheet_dir_uri = get_theme_root_uri() . "/$stylesheet"; 51 return apply_filters('stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet); 57 $theme_roots = get_option( 'theme_roots' ); 58 59 if ( $theme_roots[$stylesheet] ) 60 add_filter( 'theme_root_uri', create_function( '', 'return "' . content_url( $theme_roots[$stylesheet] ) . '";' ) ); 61 62 $theme_root_uri = get_theme_root_uri(); 63 $stylesheet_dir_uri = "$theme_root_uri/$stylesheet"; 64 65 return apply_filters( 'stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet, $theme_root_uri ); 52 66 } 53 67 54 68 /** … … 123 137 */ 124 138 function get_template_directory() { 125 139 $template = get_template(); 126 $template_dir = get_theme_root() . "/$template"; 127 return apply_filters('template_directory', $template_dir, $template); 140 $theme_roots = get_option( 'theme_roots' ); 141 142 if ( $theme_roots[$template] ) 143 add_filter( 'theme_root', create_function( '', 'return "' . WP_CONTENT_DIR . '/' . $theme_roots[$template] . '";' ) ); 144 145 $theme_root = get_theme_root(); 146 $template_dir = "$theme_root/$template"; 147 148 return apply_filters( 'template_directory', $template_dir, $template, $theme_root ); 128 149 } 129 150 130 151 /** … … 137 158 */ 138 159 function get_template_directory_uri() { 139 160 $template = get_template(); 140 $template_dir_uri = get_theme_root_uri() . "/$template"; 141 return apply_filters('template_directory_uri', $template_dir_uri, $template); 161 $theme_roots = get_option( 'theme_roots' ); 162 163 if ( $theme_roots[$template] ) 164 add_filter( 'theme_root_uri', create_function( '', 'return "' . content_url( $theme_roots[$template] ) . '";' ) ); 165 166 $theme_root_uri = get_theme_root_uri(); 167 $template_dir_uri = "$theme_root_uri/$template"; 168 169 return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri ); 142 170 } 143 171 144 172 /** … … 253 281 if ( isset($wp_themes) ) 254 282 return $wp_themes; 255 283 256 $themes = array(); 257 $wp_broken_themes = array(); 258 $theme_loc = $theme_root = get_theme_root(); 259 if ( '/' != WP_CONTENT_DIR ) // don't want to replace all forward slashes, see Trac #4541 260 $theme_loc = str_replace(WP_CONTENT_DIR, '', $theme_root); 284 /* Register wp-content/themes as a theme directory */ 285 register_theme_directory( 'themes' ); 261 286 262 // Files in wp-content/themes directory and one subdir down 263 $themes_dir = @ opendir($theme_root); 264 if ( !$themes_dir ) 287 if ( !$theme_files = search_theme_directories() ) 265 288 return false; 289 290 asort( $theme_files ); 291 292 foreach ( (array) $theme_files as $theme_file ) { 293 $theme_root = $theme_file['theme_root']; 294 $theme_file = $theme_file['theme_file']; 266 295 267 while ( ($theme_dir = readdir($themes_dir)) !== false ) {268 if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {269 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' )270 continue;271 $stylish_dir = @ opendir($theme_root . '/' . $theme_dir);272 $found_stylesheet = false;273 while ( ($theme_file = readdir($stylish_dir)) !== false ) {274 if ( $theme_file == 'style.css' ) {275 $theme_files[] = $theme_dir . '/' . $theme_file;276 $found_stylesheet = true;277 break;278 }279 }280 @closedir($stylish_dir);281 if ( !$found_stylesheet ) { // look for themes in that dir282 $subdir = "$theme_root/$theme_dir";283 $subdir_name = $theme_dir;284 $theme_subdir = @ opendir( $subdir );285 while ( ($theme_dir = readdir($theme_subdir)) !== false ) {286 if ( is_dir( $subdir . '/' . $theme_dir) && is_readable($subdir . '/' . $theme_dir) ) {287 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' )288 continue;289 $stylish_dir = @ opendir($subdir . '/' . $theme_dir);290 $found_stylesheet = false;291 while ( ($theme_file = readdir($stylish_dir)) !== false ) {292 if ( $theme_file == 'style.css' ) {293 $theme_files[] = $subdir_name . '/' . $theme_dir . '/' . $theme_file;294 $found_stylesheet = true;295 break;296 }297 }298 @closedir($stylish_dir);299 }300 }301 @closedir($theme_subdir);302 $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));303 }304 }305 }306 if ( is_dir( $theme_dir ) )307 @closedir( $theme_dir );308 309 if ( !$themes_dir || !$theme_files )310 return $themes;311 312 sort($theme_files);313 314 foreach ( (array) $theme_files as $theme_file ) {315 296 if ( !is_readable("$theme_root/$theme_file") ) { 316 297 $wp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.')); 317 298 continue; … … 346 327 else 347 328 continue; 348 329 } 330 331 $template = trim( $template ); 349 332 350 $template = trim($template);351 352 333 if ( !file_exists("$theme_root/$template/index.php") ) { 353 334 $parent_dir = dirname(dirname($theme_file)); 354 335 if ( file_exists("$theme_root/$parent_dir/$template/index.php") ) { 355 $template = "$ parent_dir/$template";336 $template = "$theme_root/$parent_dir/$template"; 356 337 } else { 357 $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.')); 358 continue; 338 /** 339 * The parent theme doesn't exist in the current theme's folder or sub folder 340 * so lets use the theme root for the parent template. 341 */ 342 $parent_theme_root = $theme_files[$template]['theme_root']; 343 if ( file_exists( "$parent_theme_root/$template/index.php" ) ) { 344 $template = "$parent_theme_root/$template"; 345 } else { 346 $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.')); 347 continue; 348 } 349 359 350 } 351 } else { 352 $template = trim( $theme_root . '/' . $template ); 360 353 } 361 354 362 355 $stylesheet_files = array(); 363 356 $template_files = array(); 364 357 … … 367 360 while ( ($file = $stylesheet_dir->read()) !== false ) { 368 361 if ( !preg_match('|^\.+$|', $file) ) { 369 362 if ( preg_match('|\.css$|', $file) ) 370 $stylesheet_files[] = "$theme_ loc/$stylesheet/$file";363 $stylesheet_files[] = "$theme_root/$stylesheet/$file"; 371 364 elseif ( preg_match('|\.php$|', $file) ) 372 $template_files[] = "$theme_ loc/$stylesheet/$file";365 $template_files[] = "$theme_root/$stylesheet/$file"; 373 366 } 374 367 } 375 368 @ $stylesheet_dir->close(); 376 369 } 377 370 378 $template_dir = @ dir("$t heme_root/$template");371 $template_dir = @ dir("$template"); 379 372 if ( $template_dir ) { 380 373 while ( ($file = $template_dir->read()) !== false ) { 381 374 if ( preg_match('|^\.+$|', $file) ) 382 375 continue; 383 376 if ( preg_match('|\.php$|', $file) ) { 384 $template_files[] = "$t heme_loc/$template/$file";385 } elseif ( is_dir("$t heme_root/$template/$file") ) {386 $template_subdir = @ dir("$t heme_root/$template/$file");377 $template_files[] = "$template/$file"; 378 } elseif ( is_dir("$template/$file") ) { 379 $template_subdir = @ dir("$template/$file"); 387 380 while ( ($subfile = $template_subdir->read()) !== false ) { 388 381 if ( preg_match('|^\.+$|', $subfile) ) 389 382 continue; 390 383 if ( preg_match('|\.php$|', $subfile) ) 391 $template_files[] = "$t heme_loc/$template/$file/$subfile";384 $template_files[] = "$template/$file/$subfile"; 392 385 } 393 386 @ $template_subdir->close(); 394 387 } … … 422 415 } 423 416 } 424 417 425 $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, 'Tags' => $theme_data['Tags']); 418 $theme_roots[$stylesheet] = str_replace( WP_CONTENT_DIR, '', $theme_root ); 419 $themes[$name] = array( 'Name' => $name, 'Title' => $title, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => basename( $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, 'Tags' => $theme_data['Tags'], 'Theme Root' => $theme_root, 'Theme Root URI' => str_replace( WP_CONTENT_DIR, content_url(), $theme_root ) ); 426 420 } 427 421 428 / / Resolve theme dependencies.429 $theme_names = array_keys( $themes);422 /* Resolve theme dependencies. */ 423 $theme_names = array_keys( $themes ); 430 424 425 /* Store theme roots in the DB */ 426 update_option( 'theme_roots', $theme_roots ); 427 431 428 foreach ( (array) $theme_names as $theme_name ) { 432 429 $themes[$theme_name]['Parent Theme'] = ''; 433 430 if ( $themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template'] ) { … … 441 438 } 442 439 443 440 $wp_themes = $themes; 444 441 445 442 return $themes; 446 443 } 447 444 … … 499 496 } 500 497 501 498 /** 499 * Register a directory that contains themes relative to the content directory. 500 * 501 * @since 2.9.0 502 * 503 * @return bool 504 */ 505 function register_theme_directory( $directory ) { 506 global $wp_theme_directories; 507 508 /* The theme directory should be relative to the content directory */ 509 $registered_directory = WP_CONTENT_DIR . '/' . $directory; 510 511 /* If this folder does not exist, return and do not register */ 512 if ( !file_exists( $registered_directory ) ) 513 return false; 514 515 $wp_theme_directories[] = $registered_directory; 516 517 return true; 518 } 519 520 /** 521 * Search all registered theme directories for complete and valid themes. 522 * 523 * @since 2.9.0 524 * 525 * @return array Valid themes found 526 */ 527 function search_theme_directories() { 528 global $wp_theme_directories, $wp_broken_themes; 529 530 if ( empty( $wp_theme_directories ) ) 531 return false; 532 533 $theme_files = array(); 534 $wp_broken_themes = array(); 535 536 /* Loop the registered theme directories and extract all themes */ 537 foreach ( (array) $wp_theme_directories as $theme_root ) { 538 $theme_loc = $theme_root; 539 540 /* We don't want to replace all forward slashes, see Trac #4541 */ 541 if ( '/' != WP_CONTENT_DIR ) 542 $theme_loc = str_replace(WP_CONTENT_DIR, '', $theme_root); 543 544 /* Files in the root of the current theme directory and one subdir down */ 545 $themes_dir = @ opendir($theme_root); 546 547 if ( !$themes_dir ) 548 return false; 549 550 while ( ($theme_dir = readdir($themes_dir)) !== false ) { 551 if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) { 552 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' ) 553 continue; 554 555 $stylish_dir = @ opendir($theme_root . '/' . $theme_dir); 556 $found_stylesheet = false; 557 558 while ( ($theme_file = readdir($stylish_dir)) !== false ) { 559 if ( $theme_file == 'style.css' ) { 560 $theme_files[$theme_dir] = array( 'theme_file' => $theme_dir . '/' . $theme_file, 'theme_root' => $theme_root ); 561 $found_stylesheet = true; 562 break; 563 } 564 } 565 @closedir($stylish_dir); 566 567 if ( !$found_stylesheet ) { // look for themes in that dir 568 $subdir = "$theme_root/$theme_dir"; 569 $subdir_name = $theme_dir; 570 $theme_subdir = @ opendir( $subdir ); 571 572 while ( ($theme_dir = readdir($theme_subdir)) !== false ) { 573 if ( is_dir( $subdir . '/' . $theme_dir) && is_readable($subdir . '/' . $theme_dir) ) { 574 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' ) 575 continue; 576 577 $stylish_dir = @ opendir($subdir . '/' . $theme_dir); 578 $found_stylesheet = false; 579 580 while ( ($theme_file = readdir($stylish_dir)) !== false ) { 581 if ( $theme_file == 'style.css' ) { 582 $theme_files[$theme_dir] = array( 'theme_file' => $subdir_name . '/' . $theme_dir . '/' . $theme_file, 'theme_root' => $theme_root ); 583 $found_stylesheet = true; 584 break; 585 } 586 } 587 @closedir($stylish_dir); 588 } 589 } 590 @closedir($theme_subdir); 591 592 $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.')); 593 } 594 } 595 } 596 if ( is_dir( $theme_dir ) ) 597 @closedir( $theme_dir ); 598 } 599 600 return $theme_files; 601 } 602 603 /** 502 604 * Retrieve path to themes directory. 503 605 * 504 606 * Does not have trailing slash. -
Users/andy/Sites/wptrunk/wp-admin/includes/theme.php
28 28 $ct->description = $themes[$current_theme]['Description']; 29 29 $ct->author = $themes[$current_theme]['Author']; 30 30 $ct->tags = $themes[$current_theme]['Tags']; 31 $ct->theme_root = $themes[$current_theme]['Theme Root']; 32 $ct->theme_root_uri = $themes[$current_theme]['Theme Root URI']; 31 33 return $ct; 32 34 } 33 35 -
Users/andy/Sites/wptrunk/wp-admin/themes.php
132 132 <h3><?php _e('Current Theme'); ?></h3> 133 133 <div id="current-theme"> 134 134 <?php if ( $ct->screenshot ) : ?> 135 <img src="<?php echo content_url($ct->stylesheet_dir . '/' . $ct->screenshot); ?>" alt="<?php _e('Current theme preview'); ?>" />135 <img src="<?php echo $ct->theme_root_uri . '/' . $ct->stylesheet . '/' . $ct->screenshot; ?>" alt="<?php _e('Current theme preview'); ?>" /> 136 136 <?php endif; ?> 137 137 <h4><?php 138 138 /* translators: 1: theme title, 2: theme version, 3: theme author */ 139 139 printf(__('%1$s %2$s by %3$s'), $ct->title, $ct->version, $ct->author) ; ?></h4> 140 140 <p class="theme-description"><?php echo $ct->description; ?></p> 141 141 <?php if ($ct->parent_theme) { ?> 142 <p><?php printf(__('The template files are located in <code>%2$s</code>. The stylesheet files are located in <code>%3$s</code>. <strong>%4$s</strong> uses templates from <strong>%5$s</strong>. Changes made to the templates will affect both themes.'), $ct->title, $ct->template_dir, $ct->stylesheet_dir, $ct->title, $ct->parent_theme); ?></p>142 <p><?php printf(__('The template files are located in <code>%2$s</code>. The stylesheet files are located in <code>%3$s</code>. <strong>%4$s</strong> uses templates from <strong>%5$s</strong>. Changes made to the templates will affect both themes.'), $ct->title, str_replace( WP_CONTENT_DIR, '', $ct->template_dir ), str_replace( WP_CONTENT_DIR, '', $ct->stylesheet_dir ), $ct->title, $ct->parent_theme); ?></p> 143 143 <?php } else { ?> 144 <p><?php printf(__('All of this theme’s files are located in <code>%2$s</code>.'), $ct->title, $ct->template_dir, $ct->stylesheet_dir); ?></p>144 <p><?php printf(__('All of this theme’s files are located in <code>%2$s</code>.'), $ct->title, str_replace( WP_CONTENT_DIR, '', $ct->template_dir ), str_replace( WP_CONTENT_DIR, '', $ct->stylesheet_dir ) ); ?></p> 145 145 <?php } ?> 146 146 <?php if ( $ct->tags ) : ?> 147 147 <p><?php _e('Tags:'); ?> <?php echo join(', ', $ct->tags); ?></p> … … 203 203 $stylesheet_dir = $themes[$theme_name]['Stylesheet Dir']; 204 204 $template_dir = $themes[$theme_name]['Template Dir']; 205 205 $parent_theme = $themes[$theme_name]['Parent Theme']; 206 $theme_root = $themes[$theme_name]['Theme Root']; 207 $theme_root_uri = $themes[$theme_name]['Theme Root URI']; 206 208 $preview_link = esc_url(get_option('home') . '/'); 207 209 if ( is_ssl() ) 208 210 $preview_link = str_replace( 'http://', 'https://', $preview_link ); … … 223 225 ?> 224 226 <a href="<?php echo $preview_link; ?>" class="<?php echo $thickbox_class; ?> screenshot"> 225 227 <?php if ( $screenshot ) : ?> 226 <img src="<?php echo content_url($stylesheet_dir . '/' . $screenshot); ?>" alt="" />228 <img src="<?php echo $theme_root_uri . '/' . $stylesheet . '/' . $screenshot; ?>" alt="" /> 227 229 <?php endif; ?> 228 230 </a> 229 231 <h3><?php … … 233 235 <span class='action-links'><?php echo $actions ?></span> 234 236 <?php if ($parent_theme) { 235 237 /* translators: 1: theme title, 2: template dir, 3: stylesheet_dir, 4: theme title, 5: parent_theme */ ?> 236 <p><?php printf(__('The template files are located in <code>%2$s</code>. The stylesheet files are located in <code>%3$s</code>. <strong>%4$s</strong> uses templates from <strong>%5$s</strong>. Changes made to the templates will affect both themes.'), $title, $template_dir, $stylesheet_dir, $title, $parent_theme); ?></p>238 <p><?php printf(__('The template files are located in <code>%2$s</code>. The stylesheet files are located in <code>%3$s</code>. <strong>%4$s</strong> uses templates from <strong>%5$s</strong>. Changes made to the templates will affect both themes.'), $title, str_replace( WP_CONTENT_DIR, '', $template_dir ), str_replace( WP_CONTENT_DIR, '', $stylesheet_dir ), $title, $parent_theme); ?></p> 237 239 <?php } else { ?> 238 <p><?php printf(__('All of this theme’s files are located in <code>%2$s</code>.'), $title, $template_dir, $stylesheet_dir); ?></p>240 <p><?php printf(__('All of this theme’s files are located in <code>%2$s</code>.'), $title, str_replace( WP_CONTENT_DIR, '', $template_dir ), str_replace( WP_CONTENT_DIR, '', $stylesheet_dir ) ); ?></p> 239 241 <?php } ?> 240 242 <?php if ( $tags ) : ?> 241 243 <p><?php _e('Tags:'); ?> <?php echo join(', ', $tags); ?></p>