Make WordPress Core

Changeset 1792


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.

Location:
trunk
Files:
2 edited

Legend:

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

    r1758 r1792  
    549549}
    550550
    551 function get_theme_data($theme_file) {
    552         $theme_data = implode('', file($theme_file));
    553         preg_match("|Theme Name:(.*)|i", $theme_data, $theme_name);
    554         preg_match("|Theme URI:(.*)|i", $theme_data, $theme_uri);
    555         preg_match("|Description:(.*)|i", $theme_data, $description);
    556         preg_match("|Author:(.*)|i", $theme_data, $author_name);
    557         preg_match("|Author URI:(.*)|i", $theme_data, $author_uri);
    558         preg_match("|Template:(.*)|i", $theme_data, $template);
    559         if ( preg_match("|Version:(.*)|i", $theme_data, $version) )
    560                 $version = $version[1];
    561         else
    562                 $version ='';
    563 
    564         $description = wptexturize($description[1]);
    565 
    566         $name = $theme_name[1];
    567         $name = trim($name);
    568         $theme = $name;
    569         if ('' != $theme_uri[1] && '' != $name) {
    570                 $theme = __("<a href='{$theme_uri[1]}' title='Visit theme homepage'>{$theme}</a>");
    571         }
    572 
    573         if ('' == $author_uri[1]) {
    574                 $author = $author_name[1];
    575         } else {
    576                 $author = __("<a href='{$author_uri[1]}' title='Visit author homepage'>{$author_name[1]}</a>");
    577         }
    578 
    579         return array('Name' => $name, 'Title' => $theme, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template[1]);
    580 }
    581 
    582 function get_themes() {
    583         $themes = array();
    584         $theme_loc = 'wp-content/themes';
    585         $theme_root = ABSPATH . $theme_loc;
    586 
    587         // Files in wp-content/themes directory
    588         $themes_dir = @ dir($theme_root);
    589         if ($themes_dir) {
    590                 while(($theme_dir = $themes_dir->read()) !== false) {
    591                         if (is_dir($theme_root . '/' . $theme_dir)) {
    592                                 $stylish_dir = @ dir($theme_root . '/' . $theme_dir);
    593                                 while(($theme_file = $stylish_dir->read()) !== false) {
    594                                         if ( $theme_file == 'style.css' ) {
    595                                                 $theme_files[] = $theme_dir . '/' . $theme_file;
    596                                         }
    597                                 }
    598                         }
    599                 }
    600         }
    601 
    602         $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');
    603 
    604         // Get the files for the default template.
    605         $default_template_files = array();
    606         {
    607                 $dirs = array('', 'wp-content');
    608                 foreach ($dirs as $dir) {
    609                         $template_dir = @ dir(ABSPATH . $dir);
    610                         while(($file = $template_dir->read()) !== false) {
    611                                 if ( !preg_match('|^\.+$|', $file) && in_array($file, $default_files))
    612                                         $default_template_files[] = trim("$dir/$file", '/');
    613                         }
    614                 }
    615         }
    616 
    617         // Get the files for the default stylesheet.
    618         $default_stylesheet_files = array();
    619         {
    620                 $stylesheet_dir = @ dir(ABSPATH);
    621                 while(($file = $stylesheet_dir->read()) !== false) {
    622                         if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file))
    623                                 $default_stylesheet_files[] = "$file";
    624                 }
    625         }
    626        
    627         // The default theme always exists.
    628         $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' => '');
    629 
    630         if (!$themes_dir || !$theme_files) {
    631                 return $themes;
    632         }
    633 
    634         sort($theme_files);
    635 
    636         foreach($theme_files as $theme_file) {
    637                 $theme_data = get_theme_data("$theme_root/$theme_file");
    638          
    639                 $name = $theme_data['Name'];
    640                 $title = $theme_data['Title'];
    641                 $description = wptexturize($theme_data['Description']);
    642                 $version = $theme_data['Version'];
    643                 $author = $theme_data['Author'];
    644                 $template = $theme_data['Template'];
    645                 $stylesheet = dirname($theme_file);
    646 
    647                 if (empty($template)) {
    648                         if (file_exists(dirname("$theme_root/$theme_file/index.php"))) {
    649                                 $template = dirname($theme_file);
    650                         } else {
    651                                 continue;
    652                         }
    653                 }
    654 
    655                 $template = trim($template);
    656 
    657                 if (($template != 'default') && (! file_exists("$theme_root/$template/index.php"))) {
    658                         continue;
    659                 }
    660 
    661                 if (empty($name)) {
    662                         $name = dirname($theme_file);
    663                         $title = $name;
    664                 }
    665                
    666                 $stylesheet_files = array();
    667                 if ($stylesheet != 'default') {
    668                         $stylesheet_dir = @ dir("$theme_root/$stylesheet");
    669                         if ($stylesheet_dir) {
    670                                 while(($file = $stylesheet_dir->read()) !== false) {
    671                                         if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file) )
    672                                                 $stylesheet_files[] = "$theme_loc/$stylesheet/$file";
    673                                 }
    674                         }
    675                 } else {
    676                         $stylesheet_files = $default_stylesheet_files;
    677                 }
    678 
    679                 $template_files = array();             
    680                 if ($template != 'default') {
    681                         $template_dir = @ dir("$theme_root/$template");
    682                         if ($template_dir) {
    683                                 while(($file = $template_dir->read()) !== false) {
    684                                         if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) )
    685                                                 $template_files[] = "$theme_loc/$template/$file";
    686                                 }
    687                         }
    688                 } else {
    689                         $template_files = $default_template_files;
    690                 }
    691 
    692                 $template_dir = dirname($template_files[0]);
    693                 $stylesheet_dir = dirname($stylesheet_files[0]);
    694 
    695                 if (empty($template_dir)) $template_dir = '/';
    696                 if (empty($stylesheet_dir)) $stylesheet_dir = '/';
    697                
    698                 $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);
    699         }
    700 
    701         // Resolve theme dependencies.
    702         $theme_names = array_keys($themes);
    703 
    704         foreach ($theme_names as $theme_name) {
    705                 $themes[$theme_name]['Parent Theme'] = '';
    706                 if ($themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template']) {
    707                         foreach ($theme_names as $parent_theme_name) {
    708                                 if (($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template'])) {
    709                                         $themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name'];
    710                                         break;
    711                                 }
    712                         }
    713                 }
    714         }
    715 
    716         return $themes;
    717 }
    718 
    719 function get_current_theme() {
    720         $themes = get_themes();
    721         $theme_names = array_keys($themes);
    722         $current_template = get_settings('template');
    723         $current_stylesheet = get_settings('stylesheet');
    724         $current_theme = 'Default';
    725 
    726         if ($themes) {
    727                 foreach ($theme_names as $theme_name) {
    728                         if ($themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
    729                                         $themes[$theme_name]['Template'] == $current_template) {
    730                                 $current_theme = $themes[$theme_name]['Name'];
    731                         }
    732                 }
    733         }
    734 
    735         return $current_theme;
    736 }
    737 
    738551function validate_current_theme() {
    739552        $theme_loc = 'wp-content/themes';
  • 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.