Move get_theme*() from admin-functions to functions so that templates and plugins can use them.

git-svn-id: http://svn.automattic.com/wordpress/trunk@1792 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
rboren 2004-10-14 03:54:57 +00:00
parent a53659c2ab
commit 3472b445a2
2 changed files with 205 additions and 187 deletions

View File

@ -548,193 +548,6 @@ if ( !strstr($_SERVER['HTTP_USER_AGENT'], 'Safari') ) :
endif;
}
function get_theme_data($theme_file) {
$theme_data = implode('', file($theme_file));
preg_match("|Theme Name:(.*)|i", $theme_data, $theme_name);
preg_match("|Theme URI:(.*)|i", $theme_data, $theme_uri);
preg_match("|Description:(.*)|i", $theme_data, $description);
preg_match("|Author:(.*)|i", $theme_data, $author_name);
preg_match("|Author URI:(.*)|i", $theme_data, $author_uri);
preg_match("|Template:(.*)|i", $theme_data, $template);
if ( preg_match("|Version:(.*)|i", $theme_data, $version) )
$version = $version[1];
else
$version ='';
$description = wptexturize($description[1]);
$name = $theme_name[1];
$name = trim($name);
$theme = $name;
if ('' != $theme_uri[1] && '' != $name) {
$theme = __("<a href='{$theme_uri[1]}' title='Visit theme homepage'>{$theme}</a>");
}
if ('' == $author_uri[1]) {
$author = $author_name[1];
} else {
$author = __("<a href='{$author_uri[1]}' title='Visit author homepage'>{$author_name[1]}</a>");
}
return array('Name' => $name, 'Title' => $theme, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template[1]);
}
function get_themes() {
$themes = array();
$theme_loc = 'wp-content/themes';
$theme_root = ABSPATH . $theme_loc;
// Files in wp-content/themes directory
$themes_dir = @ dir($theme_root);
if ($themes_dir) {
while(($theme_dir = $themes_dir->read()) !== false) {
if (is_dir($theme_root . '/' . $theme_dir)) {
$stylish_dir = @ dir($theme_root . '/' . $theme_dir);
while(($theme_file = $stylish_dir->read()) !== false) {
if ( $theme_file == 'style.css' ) {
$theme_files[] = $theme_dir . '/' . $theme_file;
}
}
}
}
}
$default_files = array(get_settings('blogfilename'), 'wp-comments.php', 'wp-comments-popup.php', 'wp-comments-post.php', 'wp-footer.php', 'wp-header.php', 'wp-sidebar.php', 'footer.php', 'header.php', 'sidebar.php');
// Get the files for the default template.
$default_template_files = array();
{
$dirs = array('', 'wp-content');
foreach ($dirs as $dir) {
$template_dir = @ dir(ABSPATH . $dir);
while(($file = $template_dir->read()) !== false) {
if ( !preg_match('|^\.+$|', $file) && in_array($file, $default_files))
$default_template_files[] = trim("$dir/$file", '/');
}
}
}
// Get the files for the default stylesheet.
$default_stylesheet_files = array();
{
$stylesheet_dir = @ dir(ABSPATH);
while(($file = $stylesheet_dir->read()) !== false) {
if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file))
$default_stylesheet_files[] = "$file";
}
}
// The default theme always exists.
$themes['Default'] = array('Name' => 'Default', 'Title' => 'WordPress Default', 'Description' => 'The default theme included with WordPress.', 'Author' => 'Dave Shea', 'Version' => '1.3', 'Template' => 'default', 'Stylesheet' => 'default', 'Template Files' => $default_template_files, 'Stylesheet Files' => $default_stylesheet_files, 'Template Dir' => '/', 'Stylesheet Dir' => '/', 'Parent Theme' => '');
if (!$themes_dir || !$theme_files) {
return $themes;
}
sort($theme_files);
foreach($theme_files as $theme_file) {
$theme_data = get_theme_data("$theme_root/$theme_file");
$name = $theme_data['Name'];
$title = $theme_data['Title'];
$description = wptexturize($theme_data['Description']);
$version = $theme_data['Version'];
$author = $theme_data['Author'];
$template = $theme_data['Template'];
$stylesheet = dirname($theme_file);
if (empty($template)) {
if (file_exists(dirname("$theme_root/$theme_file/index.php"))) {
$template = dirname($theme_file);
} else {
continue;
}
}
$template = trim($template);
if (($template != 'default') && (! file_exists("$theme_root/$template/index.php"))) {
continue;
}
if (empty($name)) {
$name = dirname($theme_file);
$title = $name;
}
$stylesheet_files = array();
if ($stylesheet != 'default') {
$stylesheet_dir = @ dir("$theme_root/$stylesheet");
if ($stylesheet_dir) {
while(($file = $stylesheet_dir->read()) !== false) {
if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file) )
$stylesheet_files[] = "$theme_loc/$stylesheet/$file";
}
}
} else {
$stylesheet_files = $default_stylesheet_files;
}
$template_files = array();
if ($template != 'default') {
$template_dir = @ dir("$theme_root/$template");
if ($template_dir) {
while(($file = $template_dir->read()) !== false) {
if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) )
$template_files[] = "$theme_loc/$template/$file";
}
}
} else {
$template_files = $default_template_files;
}
$template_dir = dirname($template_files[0]);
$stylesheet_dir = dirname($stylesheet_files[0]);
if (empty($template_dir)) $template_dir = '/';
if (empty($stylesheet_dir)) $stylesheet_dir = '/';
$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);
}
// Resolve theme dependencies.
$theme_names = array_keys($themes);
foreach ($theme_names as $theme_name) {
$themes[$theme_name]['Parent Theme'] = '';
if ($themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template']) {
foreach ($theme_names as $parent_theme_name) {
if (($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template'])) {
$themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name'];
break;
}
}
}
}
return $themes;
}
function get_current_theme() {
$themes = get_themes();
$theme_names = array_keys($themes);
$current_template = get_settings('template');
$current_stylesheet = get_settings('stylesheet');
$current_theme = 'Default';
if ($themes) {
foreach ($theme_names as $theme_name) {
if ($themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
$themes[$theme_name]['Template'] == $current_template) {
$current_theme = $themes[$theme_name]['Name'];
}
}
}
return $current_theme;
}
function validate_current_theme() {
$theme_loc = 'wp-content/themes';
$theme_root = ABSPATH . $theme_loc;

View File

@ -1850,6 +1850,211 @@ function get_template_directory() {
return $template;
}
function get_theme_data($theme_file) {
$theme_data = implode('', file($theme_file));
preg_match("|Theme Name:(.*)|i", $theme_data, $theme_name);
preg_match("|Theme URI:(.*)|i", $theme_data, $theme_uri);
preg_match("|Description:(.*)|i", $theme_data, $description);
preg_match("|Author:(.*)|i", $theme_data, $author_name);
preg_match("|Author URI:(.*)|i", $theme_data, $author_uri);
preg_match("|Template:(.*)|i", $theme_data, $template);
if ( preg_match("|Version:(.*)|i", $theme_data, $version) )
$version = $version[1];
else
$version ='';
$description = wptexturize($description[1]);
$name = $theme_name[1];
$name = trim($name);
$theme = $name;
if ('' != $theme_uri[1] && '' != $name) {
$theme = __("<a href='{$theme_uri[1]}' title='Visit theme homepage'>{$theme}</a>");
}
if ('' == $author_uri[1]) {
$author = $author_name[1];
} else {
$author = __("<a href='{$author_uri[1]}' title='Visit author homepage'>{$author_name[1]}</a>");
}
return array('Name' => $name, 'Title' => $theme, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template[1]);
}
function get_themes() {
global $wp_themes;
if (isset($wp_themes)) {
return $wp_themes;
}
$themes = array();
$theme_loc = 'wp-content/themes';
$theme_root = ABSPATH . $theme_loc;
// Files in wp-content/themes directory
$themes_dir = @ dir($theme_root);
if ($themes_dir) {
while(($theme_dir = $themes_dir->read()) !== false) {
if (is_dir($theme_root . '/' . $theme_dir)) {
$stylish_dir = @ dir($theme_root . '/' . $theme_dir);
while(($theme_file = $stylish_dir->read()) !== false) {
if ( $theme_file == 'style.css' ) {
$theme_files[] = $theme_dir . '/' . $theme_file;
}
}
}
}
}
$default_files = array(get_settings('blogfilename'), 'wp-comments.php', 'wp-comments-popup.php', 'wp-comments-post.php', 'wp-footer.php', 'wp-header.php', 'wp-sidebar.php', 'footer.php', 'header.php', 'sidebar.php');
// Get the files for the default template.
$default_template_files = array();
{
$dirs = array('', 'wp-content');
foreach ($dirs as $dir) {
$template_dir = @ dir(ABSPATH . $dir);
while(($file = $template_dir->read()) !== false) {
if ( !preg_match('|^\.+$|', $file) && in_array($file, $default_files))
$default_template_files[] = trim("$dir/$file", '/');
}
}
}
// Get the files for the default stylesheet.
$default_stylesheet_files = array();
{
$stylesheet_dir = @ dir(ABSPATH);
while(($file = $stylesheet_dir->read()) !== false) {
if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file))
$default_stylesheet_files[] = "$file";
}
}
// The default theme always exists.
$themes['Default'] = array('Name' => 'Default', 'Title' => 'WordPress Default', 'Description' => 'The default theme included with WordPress.', 'Author' => 'Dave Shea', 'Version' => '1.3', 'Template' => 'default', 'Stylesheet' => 'default', 'Template Files' => $default_template_files, 'Stylesheet Files' => $default_stylesheet_files, 'Template Dir' => '/', 'Stylesheet Dir' => '/', 'Parent Theme' => '');
if (!$themes_dir || !$theme_files) {
return $themes;
}
sort($theme_files);
foreach($theme_files as $theme_file) {
$theme_data = get_theme_data("$theme_root/$theme_file");
$name = $theme_data['Name'];
$title = $theme_data['Title'];
$description = wptexturize($theme_data['Description']);
$version = $theme_data['Version'];
$author = $theme_data['Author'];
$template = $theme_data['Template'];
$stylesheet = dirname($theme_file);
if (empty($template)) {
if (file_exists(dirname("$theme_root/$theme_file/index.php"))) {
$template = dirname($theme_file);
} else {
continue;
}
}
$template = trim($template);
if (($template != 'default') && (! file_exists("$theme_root/$template/index.php"))) {
continue;
}
if (empty($name)) {
$name = dirname($theme_file);
$title = $name;
}
$stylesheet_files = array();
if ($stylesheet != 'default') {
$stylesheet_dir = @ dir("$theme_root/$stylesheet");
if ($stylesheet_dir) {
while(($file = $stylesheet_dir->read()) !== false) {
if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file) )
$stylesheet_files[] = "$theme_loc/$stylesheet/$file";
}
}
} else {
$stylesheet_files = $default_stylesheet_files;
}
$template_files = array();
if ($template != 'default') {
$template_dir = @ dir("$theme_root/$template");
if ($template_dir) {
while(($file = $template_dir->read()) !== false) {
if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) )
$template_files[] = "$theme_loc/$template/$file";
}
}
} else {
$template_files = $default_template_files;
}
$template_dir = dirname($template_files[0]);
$stylesheet_dir = dirname($stylesheet_files[0]);
if (empty($template_dir)) $template_dir = '/';
if (empty($stylesheet_dir)) $stylesheet_dir = '/';
$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);
}
// Resolve theme dependencies.
$theme_names = array_keys($themes);
foreach ($theme_names as $theme_name) {
$themes[$theme_name]['Parent Theme'] = '';
if ($themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template']) {
foreach ($theme_names as $parent_theme_name) {
if (($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template'])) {
$themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name'];
break;
}
}
}
}
$wp_themes = $themes;
return $themes;
}
function get_theme($theme) {
$themes = get_themes();
if (array_key_exists($theme, $themes)) {
return $themes[$theme];
}
return NULL;
}
function get_current_theme() {
$themes = get_themes();
$theme_names = array_keys($themes);
$current_template = get_settings('template');
$current_stylesheet = get_settings('stylesheet');
$current_theme = 'Default';
if ($themes) {
foreach ($theme_names as $theme_name) {
if ($themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
$themes[$theme_name]['Template'] == $current_template) {
$current_theme = $themes[$theme_name]['Name'];
}
}
}
return $current_theme;
}
function get_page_template() {
global $wp_query;