Make WordPress Core

Changeset 1908


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

Move rewrite and permalink functions into WP_Rewrite class.

Location:
trunk
Files:
7 edited

Legend:

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

    r1894 r1908  
    491491
    492492function save_mod_rewrite_rules() {
    493         global $is_apache;
     493        global $is_apache, $wp_rewrite;
    494494        $home = get_settings('home');
    495495        if ( $home != '' && $home != get_settings('siteurl') ) {
    … …  
    507507                $writable = false;
    508508
    509         $permalink_structure = get_settings('permalink_structure');
    510 
    511         if ( strstr($permalink_structure, 'index.php') ) // If they're using
     509        if ($wp_rewrite->using_index_permalinks())
    512510                $usingpi = true;
    513511        else
    … …  
    515513
    516514        if ( $writable && !$usingpi && $is_apache ) {
    517                 $rules = explode("\n", mod_rewrite_rules($permalink_structure));
     515                $rules = explode("\n", $wp_rewrite->mod_rewrite_rules());
    518516                insert_with_markers($home_path.'.htaccess', 'WordPress', $rules);
    519517        }
  • trunk/wp-admin/options-permalink.php

    r1868 r1908  
    2222                if (! empty($permalink_structure) )
    2323                        $permalink_structure = preg_replace('#/+#', '/', '/' . $_POST['permalink_structure']);
    24                 update_option('permalink_structure', $permalink_structure);
     24                $wp_rewrite->set_permalink_structure($permalink_structure);
    2525        }
    2626       
    … …  
    2929                if (! empty($category_base) )
    3030                        $category_base = preg_replace('#/+#', '/', '/' . $_POST['category_base']);
    31                 update_option('category_base', $category_base);
     31                $wp_rewrite->set_category_base($category_base);
    3232        }
    3333}
    … …  
    3535$permalink_structure = get_settings('permalink_structure');
    3636$category_base = get_settings('category_base');
    37 
    38 get_date_permastruct();
    3937
    4038generate_page_rewrite_rules();
    … …  
    4543        $writable = false;
    4644
    47 if ( strstr($permalink_structure, 'index.php') ) // If they're using
     45if ($wp_rewrite->using_index_permalinks())
    4846        $usingpi = true;
    4947else
    … …  
    107105<form action="options-permalink.php" method="post">
    108106   <p>
    109 <textarea rows="5" style="width: 98%;" name="rules"><?php echo mod_rewrite_rules($permalink_structure); ?>
     107<textarea rows="5" style="width: 98%;" name="rules"><?php echo $wp_rewrite->mod_rewrite_rules(); ?>
    110108</textarea>
    111109    </p>
  • trunk/wp-blog-header.php

    r1904 r1908  
    1919
    2020        // Fetch the rewrite rules.
    21         $rewrite = rewrite_rules('matches');
     21        $rewrite = $wp_rewrite->wp_rewrite_rules();
    2222
    2323        if (! empty($rewrite)) {
  • 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?>
  • trunk/wp-includes/functions.php

    r1900 r1908  
    13061306}
    13071307
    1308 function using_index_permalinks($permalink_structure = '') {
    1309     if (empty($permalink_structure)) {
    1310         $permalink_structure = get_settings('permalink_structure');
    1311        
    1312         if (empty($permalink_structure)) {
    1313             return false;
    1314         }
    1315     }
    1316 
    1317     // If the index is not in the permalink, we're using mod_rewrite.
    1318     if (preg_match('#^/*index.php#', $permalink_structure)) {
    1319       return true;
    1320     }
    1321    
    1322     return false;
    1323 }
    1324 
    1325 function preg_index($number, $matches = '') {
    1326     $match_prefix = '$';
    1327     $match_suffix = '';
    1328    
    1329     if (! empty($matches)) {
    1330         $match_prefix = '$' . $matches . '[';
    1331         $match_suffix = ']';
    1332     }       
    1333    
    1334     return "$match_prefix$number$match_suffix";       
    1335 }
    1336 
    1337 
    13381308function get_page_uri($page) {
    13391309        global $wpdb;
    … …  
    13531323
    13541324        return $uri;
    1355 }
    1356 
    1357 function page_rewrite_rules() {
    1358         $uris = get_settings('page_uris');
    1359 
    1360         $rewrite_rules = array();
    1361         if( is_array( $uris ) )
    1362         {
    1363             foreach ($uris as $uri => $pagename) {
    1364                                                         $rewrite_rules += array($uri . '/?$' => "index.php?pagename=" . urldecode($pagename));
    1365             }
    1366         }
    1367 
    1368         return $rewrite_rules;
    1369 }
    1370 
    1371 function get_date_permastruct($permalink_structure = '') {
    1372     if (empty($permalink_structure)) {
    1373         $permalink_structure = get_settings('permalink_structure');
    1374        
    1375         if (empty($permalink_structure)) {
    1376             return false;
    1377         }
    1378     }
    1379                
    1380     $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));   
    1381                 // The date permalink must have year, month, and day separated by slashes.
    1382                 $endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%');
    1383 
    1384                 $date_structure = '';
    1385 
    1386                 foreach ($endians as $endian) {
    1387                         if (false !== strpos($permalink_structure, $endian)) {
    1388                                 $date_structure = $front . $endian;
    1389                                 break;
    1390                         }
    1391                 }
    1392 
    1393                 if (empty($date_structure)) {
    1394                                 $date_structure = $front . '%year%/%monthnum%/%day%';
    1395                 }
    1396 
    1397                 return $date_structure;
    1398 }
    1399 
    1400 function get_year_permastruct($permalink_structure = '') {
    1401         $structure = get_date_permastruct($permalink_structure);
    1402 
    1403         if (empty($structure)) {
    1404                 return false;
    1405         }
    1406 
    1407         $structure = str_replace('%monthnum%', '', $structure);
    1408         $structure = str_replace('%day%', '', $structure);
    1409 
    1410         $structure = preg_replace('#/+#', '/', $structure);
    1411 
    1412         return $structure;
    1413 }
    1414 
    1415 function get_month_permastruct($permalink_structure = '') {
    1416         $structure = get_date_permastruct($permalink_structure);
    1417 
    1418         if (empty($structure)) {
    1419                 return false;
    1420         }
    1421 
    1422         $structure = str_replace('%day%', '', $structure);
    1423 
    1424         $structure = preg_replace('#/+#', '/', $structure);
    1425 
    1426         return $structure;
    1427 }
    1428 
    1429 function get_day_permastruct($permalink_structure = '') {
    1430         return get_date_permastruct($permalink_structure);
    1431 }
    1432 
    1433 function generate_rewrite_rules($permalink_structure = '', $matches = '', $forcomments = false) {
    1434         $rewritecode =
    1435                 array(
    1436                                         '%year%',
    1437                                         '%monthnum%',
    1438                                         '%day%',
    1439                                         '%hour%',
    1440                                         '%minute%',
    1441                                         '%second%',
    1442                                         '%postname%',
    1443                                         '%post_id%',
    1444                                         '%category%',
    1445                                         '%author%',
    1446                                         '%pagename%',
    1447                                         '%search%'
    1448                                         );
    1449 
    1450         $rewritereplace =
    1451                 array(
    1452                                         '([0-9]{4})',
    1453                                         '([0-9]{1,2})',
    1454                                         '([0-9]{1,2})',
    1455                                         '([0-9]{1,2})',
    1456                                         '([0-9]{1,2})',
    1457                                         '([0-9]{1,2})',
    1458                                         '([^/]+)',
    1459                                         '([0-9]+)',
    1460                                         '(.+?)',
    1461                                         '([^/]+)',
    1462                                         '([^/]+)',
    1463                                         '(.+)'
    1464                                         );
    1465 
    1466         $queryreplace =
    1467                 array (
    1468                                          'year=',
    1469                                          'monthnum=',
    1470                                          'day=',
    1471                                          'hour=',
    1472                                          'minute=',
    1473                                          'second=',
    1474                                          'name=',
    1475                                          'p=',
    1476                                          'category_name=',
    1477                                          'author_name=',
    1478                                          'pagename=',
    1479                                          's='
    1480                                          );
    1481 
    1482         $feedregex2 = '(feed|rdf|rss|rss2|atom)/?$';
    1483         $feedregex = 'feed/' . $feedregex2;
    1484 
    1485         $trackbackregex = 'trackback/?$';
    1486         $pageregex = 'page/?([0-9]{1,})/?$';
    1487 
    1488         $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));   
    1489         preg_match_all('/%.+?%/', $permalink_structure, $tokens);
    1490 
    1491         $num_tokens = count($tokens[0]);
    1492 
    1493         $index = 'index.php';
    1494         $feedindex = $index;
    1495         $trackbackindex = $index;
    1496         for ($i = 0; $i < $num_tokens; ++$i) {
    1497                 if (0 < $i) {
    1498                         $queries[$i] = $queries[$i - 1] . '&';
    1499                 }
    1500              
    1501                 $query_token = str_replace($rewritecode, $queryreplace, $tokens[0][$i]) . preg_index($i+1, $matches);
    1502                 $queries[$i] .= $query_token;
    1503         }
    1504 
    1505         $structure = $permalink_structure;
    1506         if ($front != '/') {
    1507                 $structure = str_replace($front, '', $structure);
    1508         }
    1509         $structure = trim($structure, '/');
    1510         $dirs = explode('/', $structure);
    1511         $num_dirs = count($dirs);
    1512 
    1513         $front = preg_replace('|^/+|', '', $front);
    1514 
    1515         $post_rewrite = array();
    1516         $struct = $front;
    1517         for ($j = 0; $j < $num_dirs; ++$j) {
    1518                 $struct .= $dirs[$j] . '/';
    1519                 $struct = ltrim($struct, '/');
    1520                 $match = str_replace($rewritecode, $rewritereplace, $struct);
    1521                 $num_toks = preg_match_all('/%.+?%/', $struct, $toks);
    1522                 $query = $queries[$num_toks - 1];
    1523 
    1524                 $pagematch = $match . $pageregex;
    1525                 $pagequery = $index . '?' . $query . '&paged=' . preg_index($num_toks + 1, $matches);
    1526 
    1527                 $feedmatch = $match . $feedregex;
    1528                 $feedquery = $feedindex . '?' . $query . '&feed=' . preg_index($num_toks + 1, $matches);
    1529 
    1530                 $feedmatch2 = $match . $feedregex2;
    1531                 $feedquery2 = $feedindex . '?' . $query . '&feed=' . preg_index($num_toks + 1, $matches);
    1532 
    1533                 if ($forcomments) {
    1534                         $feedquery .= '&withcomments=1';
    1535                         $feedquery2 .= '&withcomments=1';
    1536                 }
    1537                                
    1538                 $rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2, $pagematch => $pagequery);
    1539 
    1540                 if ($num_toks) {
    1541                         $post = 0;
    1542                         if (strstr($struct, '%postname%') || strstr($struct, '%post_id%')
    1543                                         || (strstr($struct, '%year%') &&  strstr($struct, '%monthnum%') && strstr($struct, '%day%') && strstr($struct, '%hour%') && strstr($struct, '%minute') && strstr($struct, '%second%'))) {
    1544                                 $post = 1;
    1545                                 $trackbackmatch = $match . $trackbackregex;
    1546                                 $trackbackquery = $trackbackindex . '?' . $query . '&tb=1';
    1547                                 $match = $match . '?([0-9]+)?/?$';
    1548                                 $query = $index . '?' . $query . '&page=' . preg_index($num_toks + 1, $matches);
    1549                         } else {
    1550                                 $match .= '?$';
    1551                                 $query = $index . '?' . $query;
    1552                         }
    1553                                        
    1554                         $rewrite = $rewrite + array($match => $query);
    1555 
    1556                         if ($post) {
    1557                                 $rewrite = array($trackbackmatch => $trackbackquery) + $rewrite;
    1558                         }
    1559                 }
    1560 
    1561                 $post_rewrite = $rewrite + $post_rewrite;
    1562         }
    1563 
    1564         return $post_rewrite;
    1565 }
    1566 
    1567 /* rewrite_rules
    1568  * Construct rewrite matches and queries from permalink structure.
    1569  * matches - The name of the match array to use in the query strings.
    1570  *           If empty, $1, $2, $3, etc. are used.
    1571  * Returns an associate array of matches and queries.
    1572  */
    1573 function rewrite_rules($matches = '', $permalink_structure = '') {
    1574         $rewrite = array();
    1575 
    1576         if (empty($permalink_structure)) {
    1577                 $permalink_structure = get_settings('permalink_structure');
    1578        
    1579                 if (empty($permalink_structure)) {
    1580                         return $rewrite;
    1581                 }
    1582         }
    1583 
    1584         $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
    1585         $index = 'index.php';
    1586         $prefix = '';
    1587         if (using_index_permalinks($permalink_structure)) {
    1588                 $prefix = $index . '/';
    1589         }
    1590 
    1591         // Post
    1592         $post_rewrite = generate_rewrite_rules($permalink_structure, $matches);
    1593 
    1594         // Date
    1595         $date_rewrite = generate_rewrite_rules(get_date_permastruct($permalink_structure), $matches);
    1596                
    1597         // Root
    1598         $root_rewrite = generate_rewrite_rules($prefix . '/', $matches);
    1599 
    1600         // Comments
    1601         $comments_rewrite = generate_rewrite_rules($prefix . 'comments', $matches, true);
    1602 
    1603         // Search
    1604         $search_structure = $prefix . "search/%search%";
    1605         $search_rewrite = generate_rewrite_rules($search_structure, $matches);
    1606 
    1607         // Categories
    1608         if ( '' == get_settings('category_base') )
    1609                 $category_structure = $front . 'category/';
    1610         else
    1611                 $category_structure = get_settings('category_base') . '/';
    1612 
    1613         $category_structure = $category_structure . '%category%';
    1614         $category_rewrite = generate_rewrite_rules($category_structure, $matches);
    1615 
    1616         // Authors
    1617         $author_structure = $front . 'author/%author%';
    1618         $author_rewrite = generate_rewrite_rules($author_structure, $matches);
    1619 
    1620         // Pages
    1621         $page_rewrite = page_rewrite_rules();
    1622 
    1623         // Deprecated style static pages
    1624         $page_structure = $prefix . 'site/%pagename%';
    1625         $old_page_rewrite = generate_rewrite_rules($page_structure, $matches);
    1626 
    1627         // Put them together.
    1628         $rewrite = $page_rewrite + $root_rewrite + $comments_rewrite + $old_page_rewrite + $search_rewrite + $category_rewrite + $author_rewrite + $date_rewrite + $post_rewrite;
    1629 
    1630         $rewrite = apply_filters('rewrite_rules_array', $rewrite);
    1631         return $rewrite;
    1632 }
    1633 
    1634 function mod_rewrite_rules ($permalink_structure) {
    1635         $site_root = str_replace('http://', '', trim(get_settings('siteurl')));
    1636         $site_root = preg_replace('|([^/]*)(.*)|i', '$2', $site_root);
    1637         if ('/' != substr($site_root, -1)) $site_root = $site_root . '/';
    1638    
    1639         $home_root = str_replace('http://', '', trim(get_settings('home')));
    1640         $home_root = preg_replace('|([^/]*)(.*)|i', '$2', $home_root);
    1641         if ('/' != substr($home_root, -1)) $home_root = $home_root . '/';
    1642 
    1643         $rules = "<IfModule mod_rewrite.c>\n";
    1644         $rules .= "RewriteEngine On\n";
    1645         $rules .= "RewriteBase $home_root\n";
    1646         $rewrite = rewrite_rules('', $permalink_structure);
    1647 
    1648         $num_rules = count($rewrite);
    1649         $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" .
    1650                 "RewriteCond %{REQUEST_FILENAME} -d\n" .
    1651                 "RewriteRule ^.*$ - [S=$num_rules]\n";
    1652 
    1653         foreach ($rewrite as $match => $query) {
    1654                 // Apache 1.3 does not support the reluctant (non-greedy) modifier.
    1655                 $match = str_replace('.+?', '.+', $match);
    1656 
    1657                 // If the match is unanchored and greedy, prepend rewrite conditions
    1658                 // to avoid infinite redirects and eclipsing of real files.
    1659                 if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
    1660                         //nada.
    1661                 }
    1662 
    1663                 if (strstr($query, 'index.php')) {
    1664                         $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
    1665                 } else {
    1666                         $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n";
    1667                 }
    1668         }
    1669         $rules .= "</IfModule>\n";
    1670 
    1671         $rules = apply_filters('rewrite_rules', $rules);
    1672 
    1673         return $rules;
    16741325}
    16751326
  • trunk/wp-includes/template-functions-category.php

    r1853 r1908  
    2626
    2727function get_category_link($echo = false, $category_id, $category_nicename) {
    28     global $wpdb, $post, $querystring_start, $querystring_equal, $cache_categories;
     28        global $wpdb, $wp_rewrite, $post, $querystring_start, $querystring_equal, $cache_categories;
    2929    $cat_ID = $category_id;
    30     $permalink_structure = get_settings('permalink_structure');
     30    $catlink = $wp_rewrite->get_category_permastruct();
    3131   
    32     if ('' == $permalink_structure) {
     32    if (empty($catlink)) {
    3333        $file = get_settings('home') . '/' . get_settings('blogfilename');
    34         $link = $file.$querystring_start.'cat'.$querystring_equal.$cat_ID;
    35     } else {
    36                 $category_nicename = $cache_categories[$category_id]->category_nicename;
    37                 // Get any static stuff from the front
    38         $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
    39                 if ( '' == get_settings('category_base') ) :
    40                         $link = get_settings('home') . $front . 'category/';
    41                 else :
    42          $link = get_settings('home') . get_settings('category_base') . '/';
    43                 endif;
    44         if ($parent=$cache_categories[$category_id]->category_parent) $link .= get_category_parents($parent, FALSE, '/', TRUE);
    45         $link .= $category_nicename . '/';
    46     }
    47 
    48     if ($echo) echo $link;
    49     return $link;
     34        $catlink = $file.$querystring_start.'cat'.$querystring_equal.$cat_ID;
     35    } else {
     36                        $category_nicename = $cache_categories[$category_id]->category_nicename;
     37                        if ($parent=$cache_categories[$category_id]->category_parent) $category_nicename = get_category_parents($parent, FALSE, '/', TRUE) . $category_nicename . '/';
     38
     39                        $catlink = str_replace('%category%', $category_nicename, $catlink);
     40                        $catlink = get_settings('home') . trailingslashit($catlink);
     41    }
     42    if ($echo) echo $catlink;
     43    return $catlink;
    5044}
    5145
  • trunk/wp-includes/template-functions-links.php

    r1868 r1908  
    8585
    8686function get_page_link($id = false) {
    87         global $post;
     87        global $post, $wp_rewrite;
    8888
    8989        if (! $id) {
    … …  
    9595        if ('' != $permalink) {
    9696                $link = get_page_uri($id);
    97                 if (using_index_permalinks()) {
     97                if ($wp_rewrite->using_index_permalinks()) {
    9898                        $link = 'index.php/' . $link;
    9999                }
    … …  
    107107
    108108function get_year_link($year) {
    109     global $querystring_start, $querystring_equal;
     109        global $querystring_start, $querystring_equal, $wp_rewrite;
    110110    if (!$year) $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
    111                 $yearlink = get_year_permastruct();
     111                $yearlink = $wp_rewrite->get_year_permastruct();
    112112    if (!empty($yearlink)) {
    113113        $yearlink = str_replace('%year%', $year, $yearlink);
    114         return get_settings('home') . $yearlink;
     114        return get_settings('home') . trailingslashit($yearlink);
    115115    } else {
    116116        return get_settings('home') .'/'. get_settings('blogfilename') .$querystring_start.'m'.$querystring_equal.$year;
    … …  
    119119
    120120function get_month_link($year, $month) {
    121     global $querystring_start, $querystring_equal;
     121    global $querystring_start, $querystring_equal, $wp_rewrite;
    122122    if (!$year) $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
    123123    if (!$month) $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
    124                 $monthlink = get_month_permastruct();
     124                $monthlink = $wp_rewrite->get_month_permastruct();
    125125    if (!empty($monthlink)) {
    126126        $monthlink = str_replace('%year%', $year, $monthlink);
    127127        $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
    128         return get_settings('home') . $monthlink;
     128        return get_settings('home') . trailingslashit($monthlink);
    129129    } else {
    130130        return get_settings('home') .'/'. get_settings('blogfilename') .$querystring_start.'m'.$querystring_equal.$year.zeroise($month, 2);
    … …  
    133133
    134134function get_day_link($year, $month, $day) {
    135     global $querystring_start, $querystring_equal;
     135    global $querystring_start, $querystring_equal, $wp_rewrite;
    136136    if (!$year) $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
    137137    if (!$month) $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
    138138    if (!$day) $day = gmdate('j', time()+(get_settings('gmt_offset') * 3600));
    139139
    140                 $daylink = get_day_permastruct();
     140                $daylink = $wp_rewrite->get_day_permastruct();
    141141    if (!empty($daylink)) {
    142142        $daylink = str_replace('%year%', $year, $daylink);
    143143        $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
    144144        $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
    145         return get_settings('home') . $daylink;
     145        return get_settings('home') . trailingslashit($daylink);
    146146    } else {
    147147        return get_settings('home') .'/'. get_settings('blogfilename') .$querystring_start.'m'.$querystring_equal.$year.zeroise($month, 2).zeroise($day, 2);
    … …  
    150150
    151151function get_feed_link($feed='rss2') {
     152        global $wp_rewrite;
    152153    $do_perma = 0;
    153154    $feed_url = get_settings('siteurl');
    … …  
    160161        $index = 'index.php';
    161162        $prefix = '';
    162         if (using_index_permalinks()) {
     163        if ($wp_rewrite->using_index_permalinks()) {
    163164            $feed_url .= '/' . $index;
    164165        }
    … …  
    417418
    418419function get_pagenum_link($pagenum = 1){
     420        global $wp_rewrite;
     421
    419422   $qstr = $_SERVER['REQUEST_URI'];
    420423
    … …  
    454457
    455458         // If it's not a path info permalink structure, trim the index.
    456          if (! using_index_permalinks()) {
     459         if (! $wp_rewrite->using_index_permalinks()) {
    457460           $qstr = preg_replace("#/*" . $index . "/*#", '/', $qstr);
    458461         } else {
Note: See TracChangeset for help on using the changeset viewer.