Make WordPress Core


Ignore:
Timestamp:
12/03/2004 02:38:11 AM (20 years ago)
Author:
rboren
Message:

Move rewrite and permalink functions into WP_Rewrite class.

File:
1 edited

Legend:

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

    r1905 r1908  
    699699}
    700700
     701class WP_Rewrite {
     702    var $permalink_structure;
     703    var $category_base;
     704    var $category_structure;
     705    var $date_structure;
     706    var $front;
     707    var $prefix = '';
     708    var $index = 'index.php';
     709    var $matches = '';
     710    var $rewritecode =
     711        array(
     712                    '%year%',
     713                    '%monthnum%',
     714                    '%day%',
     715                    '%hour%',
     716                    '%minute%',
     717                    '%second%',
     718                    '%postname%',
     719                    '%post_id%',
     720                    '%category%',
     721                    '%author%',
     722                    '%pagename%',
     723                    '%search%'
     724                    );
     725
     726    var $rewritereplace =
     727        array(
     728                    '([0-9]{4})',
     729                    '([0-9]{1,2})',
     730                    '([0-9]{1,2})',
     731                    '([0-9]{1,2})',
     732                    '([0-9]{1,2})',
     733                    '([0-9]{1,2})',
     734                    '([^/]+)',
     735                    '([0-9]+)',
     736                    '(.+?)',
     737                    '([^/]+)',
     738                    '([^/]+)',
     739                    '(.+)'
     740                    );
     741
     742    var $queryreplace =
     743        array (
     744                     'year=',
     745                     'monthnum=',
     746                     'day=',
     747                     'hour=',
     748                     'minute=',
     749                     'second=',
     750                     'name=',
     751                     'p=',
     752                     'category_name=',
     753                     'author_name=',
     754                     'pagename=',
     755                     's='
     756                     );
     757
     758    function using_index_permalinks() {
     759    if (empty($this->permalink_structure)) {
     760            return false;
     761    }
     762
     763    // If the index is not in the permalink, we're using mod_rewrite.
     764    if (preg_match('#^/*index.php#', $this->permalink_structure)) {
     765      return true;
     766    }
     767   
     768    return false;
     769    }
     770
     771    function preg_index($number) {
     772    $match_prefix = '$';
     773    $match_suffix = '';
     774   
     775    if (! empty($this->matches)) {
     776            $match_prefix = '$' . $this->matches . '[';
     777            $match_suffix = ']';
     778    }       
     779   
     780    return "$match_prefix$number$match_suffix";       
     781    }
     782
     783    function page_rewrite_rules() {
     784        $uris = get_settings('page_uris');
     785
     786        $rewrite_rules = array();
     787        if( is_array( $uris ) )
     788            {
     789                foreach ($uris as $uri => $pagename) {
     790                    $rewrite_rules += array($uri . '/?$' => "index.php?pagename=" . urldecode($pagename));
     791                }
     792            }
     793
     794        return $rewrite_rules;
     795    }
     796
     797    function get_date_permastruct() {
     798        if (isset($this->date_structure)) {
     799            return $this->date_structure;
     800        }
     801
     802    if (empty($this->permalink_structure)) {
     803            $this->date_structure = '';
     804            return false;
     805        }
     806       
     807        // The date permalink must have year, month, and day separated by slashes.
     808        $endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%');
     809
     810        $this->date_structure = '';
     811
     812        foreach ($endians as $endian) {
     813            if (false !== strpos($this->permalink_structure, $endian)) {
     814                $this->date_structure = $this->front . $endian;
     815                break;
     816            }
     817        }
     818
     819        if (empty($this->date_structure)) {
     820            $this->date_structure = $front . '%year%/%monthnum%/%day%';
     821        }
     822
     823        return $this->date_structure;
     824    }
     825
     826    function get_year_permastruct() {
     827        $structure = $this->get_date_permastruct($permalink_structure);
     828
     829        if (empty($structure)) {
     830            return false;
     831        }
     832
     833        $structure = str_replace('%monthnum%', '', $structure);
     834        $structure = str_replace('%day%', '', $structure);
     835
     836        $structure = preg_replace('#/+#', '/', $structure);
     837
     838        return $structure;
     839    }
     840
     841    function get_month_permastruct() {
     842        $structure = $this->get_date_permastruct($permalink_structure);
     843
     844        if (empty($structure)) {
     845            return false;
     846        }
     847
     848        $structure = str_replace('%day%', '', $structure);
     849
     850        $structure = preg_replace('#/+#', '/', $structure);
     851
     852        return $structure;
     853    }
     854
     855    function get_day_permastruct() {
     856        return $this->get_date_permastruct($permalink_structure);
     857    }
     858
     859    function get_category_permastruct() {
     860        if (isset($this->category_structure)) {
     861            return $this->category_structure;
     862        }
     863
     864    if (empty($this->permalink_structure)) {
     865            $this->category_structure = '';
     866            return false;
     867        }
     868
     869        if (empty($this->category_base))
     870            $this->category_structure = $this->front . 'category/';
     871        else
     872            $this->category_structure = $this->category_base . '/';
     873
     874        $this->category_structure .= '%category%';
     875       
     876        return $this->category_structure;
     877    }
     878
     879    function generate_rewrite_rules($permalink_structure = '', $forcomments = false) {
     880        $feedregex2 = '(feed|rdf|rss|rss2|atom)/?$';
     881        $feedregex = 'feed/' . $feedregex2;
     882
     883        $trackbackregex = 'trackback/?$';
     884        $pageregex = 'page/?([0-9]{1,})/?$';
     885       
     886        $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
     887        preg_match_all('/%.+?%/', $permalink_structure, $tokens);
     888
     889        $num_tokens = count($tokens[0]);
     890
     891        $index = $this->index;
     892        $feedindex = $index;
     893        $trackbackindex = $index;
     894        for ($i = 0; $i < $num_tokens; ++$i) {
     895            if (0 < $i) {
     896                $queries[$i] = $queries[$i - 1] . '&';
     897            }
     898             
     899            $query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1);
     900            $queries[$i] .= $query_token;
     901        }
     902
     903        $structure = $permalink_structure;
     904        if ($front != '/') {
     905            $structure = str_replace($front, '', $structure);
     906        }
     907        $structure = trim($structure, '/');
     908        $dirs = explode('/', $structure);
     909        $num_dirs = count($dirs);
     910
     911        $front = preg_replace('|^/+|', '', $front);
     912
     913        $post_rewrite = array();
     914        $struct = $front;
     915        for ($j = 0; $j < $num_dirs; ++$j) {
     916            $struct .= $dirs[$j] . '/';
     917            $struct = ltrim($struct, '/');
     918            $match = str_replace($this->rewritecode, $this->rewritereplace, $struct);
     919            $num_toks = preg_match_all('/%.+?%/', $struct, $toks);
     920            $query = $queries[$num_toks - 1];
     921
     922            $pagematch = $match . $pageregex;
     923            $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1);
     924
     925            $feedmatch = $match . $feedregex;
     926            $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
     927
     928            $feedmatch2 = $match . $feedregex2;
     929            $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
     930
     931            if ($forcomments) {
     932                $feedquery .= '&withcomments=1';
     933                $feedquery2 .= '&withcomments=1';
     934            }
     935               
     936            $rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2, $pagematch => $pagequery);
     937
     938            if ($num_toks) {
     939                $post = 0;
     940                if (strstr($struct, '%postname%') || strstr($struct, '%post_id%')
     941                        || (strstr($struct, '%year%') &&  strstr($struct, '%monthnum%') && strstr($struct, '%day%') && strstr($struct, '%hour%') && strstr($struct, '%minute') && strstr($struct, '%second%'))) {
     942                    $post = 1;
     943                    $trackbackmatch = $match . $trackbackregex;
     944                    $trackbackquery = $trackbackindex . '?' . $query . '&tb=1';
     945                    $match = $match . '?([0-9]+)?/?$';
     946                    $query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1);
     947                } else {
     948                    $match .= '?$';
     949                    $query = $index . '?' . $query;
     950                }
     951                       
     952                $rewrite = $rewrite + array($match => $query);
     953
     954                if ($post) {
     955                    $rewrite = array($trackbackmatch => $trackbackquery) + $rewrite;
     956                }
     957            }
     958
     959            $post_rewrite = $rewrite + $post_rewrite;
     960        }
     961
     962        return $post_rewrite;
     963    }
     964
     965    /* rewrite_rules
     966     * Construct rewrite matches and queries from permalink structure.
     967     * Returns an associate array of matches and queries.
     968     */
     969    function rewrite_rules() {
     970        $rewrite = array();
     971
     972        if (empty($this->permalink_structure)) {
     973            return $rewrite;
     974        }
     975
     976        // Post
     977        $post_rewrite = $this->generate_rewrite_rules($this->permalink_structure);
     978
     979        // Date
     980        $date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct());
     981       
     982        // Root
     983        $root_rewrite = $this->generate_rewrite_rules($this->prefix . '/');
     984
     985        // Comments
     986        $comments_rewrite = $this->generate_rewrite_rules($this->prefix . 'comments', true);
     987
     988        // Search
     989        $search_structure = $this->prefix . "search/%search%";
     990        $search_rewrite = $this->generate_rewrite_rules($search_structure);
     991
     992        // Categories
     993        $category_rewrite = $this->generate_rewrite_rules($this->get_category_permastruct());
     994
     995        // Authors
     996        $author_structure = $this->front . 'author/%author%';
     997        $author_rewrite = $this->generate_rewrite_rules($author_structure);
     998
     999        // Pages
     1000        $page_rewrite = $this->page_rewrite_rules();
     1001
     1002        // Deprecated style static pages
     1003        $page_structure = $this->prefix . 'site/%pagename%';
     1004        $old_page_rewrite = $this->generate_rewrite_rules($page_structure);
     1005
     1006        // Put them together.
     1007        $this->rewrite = $page_rewrite + $root_rewrite + $comments_rewrite + $old_page_rewrite + $search_rewrite + $category_rewrite + $author_rewrite + $date_rewrite + $post_rewrite;
     1008
     1009        $this->rewrite = apply_filters('rewrite_rules_array', $this->rewrite);
     1010        return $this->rewrite;
     1011    }
     1012
     1013    function wp_rewrite_rules() {
     1014        $this->matches = 'matches';
     1015        return $this->rewrite_rules();
     1016    }
     1017
     1018    function mod_rewrite_rules () {
     1019        $site_root = str_replace('http://', '', trim(get_settings('siteurl')));
     1020        $site_root = preg_replace('|([^/]*)(.*)|i', '$2', $site_root);
     1021        if ('/' != substr($site_root, -1)) $site_root = $site_root . '/';
     1022   
     1023        $home_root = str_replace('http://', '', trim(get_settings('home')));
     1024        $home_root = preg_replace('|([^/]*)(.*)|i', '$2', $home_root);
     1025        if ('/' != substr($home_root, -1)) $home_root = $home_root . '/';
     1026
     1027        $rules = "<IfModule mod_rewrite.c>\n";
     1028        $rules .= "RewriteEngine On\n";
     1029        $rules .= "RewriteBase $home_root\n";
     1030        $this->matches = '';
     1031        $rewrite = $this->rewrite_rules();
     1032
     1033        $num_rules = count($rewrite);
     1034        $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" .
     1035            "RewriteCond %{REQUEST_FILENAME} -d\n" .
     1036            "RewriteRule ^.*$ - [S=$num_rules]\n";
     1037
     1038        foreach ($rewrite as $match => $query) {
     1039            // Apache 1.3 does not support the reluctant (non-greedy) modifier.
     1040            $match = str_replace('.+?', '.+', $match);
     1041
     1042            // If the match is unanchored and greedy, prepend rewrite conditions
     1043            // to avoid infinite redirects and eclipsing of real files.
     1044            if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
     1045                //nada.
     1046            }
     1047
     1048            if (strstr($query, 'index.php')) {
     1049                $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
     1050            } else {
     1051                $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n";
     1052            }
     1053        }
     1054        $rules .= "</IfModule>\n";
     1055
     1056        $rules = apply_filters('rewrite_rules', $rules);
     1057
     1058        return $rules;
     1059    }
     1060
     1061    function init() {
     1062        $this->permalink_structure = get_settings('permalink_structure');
     1063        $this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%'));     
     1064        $this->prefix = '';
     1065        if ($this->using_index_permalinks()) {
     1066            $this->prefix = $this->index . '/';
     1067        }
     1068        $this->category_base = get_settings('category_base');
     1069        unset($this->category_structure);
     1070        unset($this->date_structure);       
     1071    }
     1072
     1073    function set_permalink_structure($permalink_structure) {
     1074        if ($permalink_structure != $this->permalink_structure) {
     1075            update_option('permalink_structure', $permalink_structure);
     1076            $this->init();
     1077        }
     1078    }
     1079
     1080    function set_category_base($category_base) {
     1081        if ($category_base != $this->category_base) {
     1082            update_option('category_base', $category_base);
     1083            $this->init();
     1084        }
     1085    }
     1086
     1087    function WP_Rewrite() {
     1088        $this->init();
     1089    }
     1090}
     1091
     1092// Make a global instance.
     1093if (! isset($wp_rewrite)) {
     1094    $wp_rewrite = new WP_Rewrite();
     1095}
     1096
    7011097?>
Note: See TracChangeset for help on using the changeset viewer.