Make WordPress Core


Ignore:
Timestamp:
10/14/2004 03:54:57 AM (22 years ago)
Author:
rboren
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r1789 r1792  
    18511851}
    18521852
     1853function get_theme_data($theme_file) {
     1854        $theme_data = implode('', file($theme_file));
     1855        preg_match("|Theme Name:(.*)|i", $theme_data, $theme_name);
     1856        preg_match("|Theme URI:(.*)|i", $theme_data, $theme_uri);
     1857        preg_match("|Description:(.*)|i", $theme_data, $description);
     1858        preg_match("|Author:(.*)|i", $theme_data, $author_name);
     1859        preg_match("|Author URI:(.*)|i", $theme_data, $author_uri);
     1860        preg_match("|Template:(.*)|i", $theme_data, $template);
     1861        if ( preg_match("|Version:(.*)|i", $theme_data, $version) )
     1862                $version = $version[1];
     1863        else
     1864                $version ='';
     1865
     1866        $description = wptexturize($description[1]);
     1867
     1868        $name = $theme_name[1];
     1869        $name = trim($name);
     1870        $theme = $name;
     1871        if ('' != $theme_uri[1] && '' != $name) {
     1872                $theme = __("<a href='{$theme_uri[1]}' title='Visit theme homepage'>{$theme}</a>");
     1873        }
     1874
     1875        if ('' == $author_uri[1]) {
     1876                $author = $author_name[1];
     1877        } else {
     1878                $author = __("<a href='{$author_uri[1]}' title='Visit author homepage'>{$author_name[1]}</a>");
     1879        }
     1880
     1881        return array('Name' => $name, 'Title' => $theme, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template[1]);
     1882}
     1883
     1884function get_themes() {
     1885        global $wp_themes;
     1886
     1887        if (isset($wp_themes)) {
     1888                return $wp_themes;
     1889        }
     1890
     1891        $themes = array();
     1892        $theme_loc = 'wp-content/themes';
     1893        $theme_root = ABSPATH . $theme_loc;
     1894
     1895        // Files in wp-content/themes directory
     1896        $themes_dir = @ dir($theme_root);
     1897        if ($themes_dir) {
     1898                while(($theme_dir = $themes_dir->read()) !== false) {
     1899                        if (is_dir($theme_root . '/' . $theme_dir)) {
     1900                                $stylish_dir = @ dir($theme_root . '/' . $theme_dir);
     1901                                while(($theme_file = $stylish_dir->read()) !== false) {
     1902                                        if ( $theme_file == 'style.css' ) {
     1903                                                $theme_files[] = $theme_dir . '/' . $theme_file;
     1904                                        }
     1905                                }
     1906                        }
     1907                }
     1908        }
     1909
     1910        $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');
     1911
     1912        // Get the files for the default template.
     1913        $default_template_files = array();
     1914        {
     1915                $dirs = array('', 'wp-content');
     1916                foreach ($dirs as $dir) {
     1917                        $template_dir = @ dir(ABSPATH . $dir);
     1918                        while(($file = $template_dir->read()) !== false) {
     1919                                if ( !preg_match('|^\.+$|', $file) && in_array($file, $default_files))
     1920                                        $default_template_files[] = trim("$dir/$file", '/');
     1921                        }
     1922                }
     1923        }
     1924
     1925        // Get the files for the default stylesheet.
     1926        $default_stylesheet_files = array();
     1927        {
     1928                $stylesheet_dir = @ dir(ABSPATH);
     1929                while(($file = $stylesheet_dir->read()) !== false) {
     1930                        if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file))
     1931                                $default_stylesheet_files[] = "$file";
     1932                }
     1933        }
     1934       
     1935        // The default theme always exists.
     1936        $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' => '');
     1937
     1938        if (!$themes_dir || !$theme_files) {
     1939                return $themes;
     1940        }
     1941
     1942        sort($theme_files);
     1943
     1944        foreach($theme_files as $theme_file) {
     1945                $theme_data = get_theme_data("$theme_root/$theme_file");
     1946         
     1947                $name = $theme_data['Name'];
     1948                $title = $theme_data['Title'];
     1949                $description = wptexturize($theme_data['Description']);
     1950                $version = $theme_data['Version'];
     1951                $author = $theme_data['Author'];
     1952                $template = $theme_data['Template'];
     1953                $stylesheet = dirname($theme_file);
     1954
     1955                if (empty($template)) {
     1956                        if (file_exists(dirname("$theme_root/$theme_file/index.php"))) {
     1957                                $template = dirname($theme_file);
     1958                        } else {
     1959                                continue;
     1960                        }
     1961                }
     1962
     1963                $template = trim($template);
     1964
     1965                if (($template != 'default') && (! file_exists("$theme_root/$template/index.php"))) {
     1966                        continue;
     1967                }
     1968
     1969                if (empty($name)) {
     1970                        $name = dirname($theme_file);
     1971                        $title = $name;
     1972                }
     1973               
     1974                $stylesheet_files = array();
     1975                if ($stylesheet != 'default') {
     1976                        $stylesheet_dir = @ dir("$theme_root/$stylesheet");
     1977                        if ($stylesheet_dir) {
     1978                                while(($file = $stylesheet_dir->read()) !== false) {
     1979                                        if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file) )
     1980                                                $stylesheet_files[] = "$theme_loc/$stylesheet/$file";
     1981                                }
     1982                        }
     1983                } else {
     1984                        $stylesheet_files = $default_stylesheet_files;
     1985                }
     1986
     1987                $template_files = array();             
     1988                if ($template != 'default') {
     1989                        $template_dir = @ dir("$theme_root/$template");
     1990                        if ($template_dir) {
     1991                                while(($file = $template_dir->read()) !== false) {
     1992                                        if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) )
     1993                                                $template_files[] = "$theme_loc/$template/$file";
     1994                                }
     1995                        }
     1996                } else {
     1997                        $template_files = $default_template_files;
     1998                }
     1999
     2000                $template_dir = dirname($template_files[0]);
     2001                $stylesheet_dir = dirname($stylesheet_files[0]);
     2002
     2003                if (empty($template_dir)) $template_dir = '/';
     2004                if (empty($stylesheet_dir)) $stylesheet_dir = '/';
     2005               
     2006                $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);
     2007        }
     2008
     2009        // Resolve theme dependencies.
     2010        $theme_names = array_keys($themes);
     2011
     2012        foreach ($theme_names as $theme_name) {
     2013                $themes[$theme_name]['Parent Theme'] = '';
     2014                if ($themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template']) {
     2015                        foreach ($theme_names as $parent_theme_name) {
     2016                                if (($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template'])) {
     2017                                        $themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name'];
     2018                                        break;
     2019                                }
     2020                        }
     2021                }
     2022        }
     2023
     2024        $wp_themes = $themes;
     2025
     2026        return $themes;
     2027}
     2028
     2029function get_theme($theme) {
     2030        $themes = get_themes();
     2031
     2032        if (array_key_exists($theme, $themes)) {
     2033                return $themes[$theme];
     2034        }
     2035
     2036        return NULL;
     2037}
     2038
     2039function get_current_theme() {
     2040        $themes = get_themes();
     2041        $theme_names = array_keys($themes);
     2042        $current_template = get_settings('template');
     2043        $current_stylesheet = get_settings('stylesheet');
     2044        $current_theme = 'Default';
     2045
     2046        if ($themes) {
     2047                foreach ($theme_names as $theme_name) {
     2048                        if ($themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
     2049                                        $themes[$theme_name]['Template'] == $current_template) {
     2050                                $current_theme = $themes[$theme_name]['Name'];
     2051                        }
     2052                }
     2053        }
     2054
     2055        return $current_theme;
     2056}
     2057
    18532058function get_page_template() {
    18542059        global $wp_query;
Note: See TracChangeset for help on using the changeset viewer.