Make WordPress Core


Ignore:
Timestamp:
05/31/2004 05:22:25 PM (20 years ago)
Author:
rboren
Message:

Revamp rewrite rule generation. Add clean page links. Credit to Jaykul for get_pagenum_link().

File:
1 edited

Legend:

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

    r1366 r1373  
    12071207    $rewritereplace =
    12081208    array(
    1209     '([0-9]{4})?',
    1210     '([0-9]{1,2})?',
    1211     '([0-9]{1,2})?',
    1212     '([0-9]{1,2})?',
    1213     '([0-9]{1,2})?',
    1214     '([0-9]{1,2})?',
    1215     '([_0-9a-z-]+)?',
    1216     '([0-9]+)?'
     1209    '([0-9]{4})',
     1210    '([0-9]{1,2})',
     1211    '([0-9]{1,2})',
     1212    '([0-9]{1,2})',
     1213    '([0-9]{1,2})',
     1214    '([0-9]{1,2})',
     1215    '([_0-9a-z-]+)',
     1216    '([0-9]+)'
    12171217    );
    12181218
     
    12291229    );
    12301230
    1231 
    1232     $match = str_replace('/', '/?', $permalink_structure);
    1233     $match = preg_replace('|/[?]|', '', $match, 1);
    1234 
    1235     $match = str_replace($rewritecode, $rewritereplace, $match);
    1236     $match = preg_replace('|[?]|', '', $match, 1);
    1237 
    1238     $feedmatch = trailingslashit(str_replace('?/?', '/', $match));
    1239     $trackbackmatch = $feedmatch;
    1240 
     1231    $feedregex = '(feed|rdf|rss|rss2|atom)/?$';
     1232    $trackbackregex = 'trackback/?$';
     1233    $pageregex = 'page/?([0-9]{1,})/?$';
     1234
     1235    $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));   
    12411236    preg_match_all('/%.+?%/', $permalink_structure, $tokens);
    12421237
    1243     $query = 'index.php?';
    1244     $feedquery = 'wp-feed.php?';
    1245     $trackbackquery = 'wp-trackback.php?';
    1246     for ($i = 0; $i < count($tokens[0]); ++$i) {
     1238    $num_tokens = count($tokens[0]);
     1239
     1240    $index = 'index.php';
     1241    $feedindex = 'wp-feed.php';
     1242    $trackbackindex = 'wp-trackback.php';
     1243    for ($i = 0; $i < $num_tokens; ++$i) {
    12471244             if (0 < $i) {
    1248                  $query .= '&';
    1249                  $feedquery .= '&';
    1250                  $trackbackquery .= '&';
     1245                 $queries[$i] = $queries[$i - 1] . '&';
    12511246             }
    12521247             
    12531248             $query_token = str_replace($rewritecode, $queryreplace, $tokens[0][$i]) . preg_index($i+1, $matches);
    1254              $query .= $query_token;
    1255              $feedquery .= $query_token;
    1256              $trackbackquery .= $query_token;
     1249             $queries[$i] .= $query_token;
    12571250             }
    1258     ++$i;
    1259 
    1260     // Add post paged stuff
    1261     $match .= '([0-9]+)?/?$';
    1262     $query .= '&page=' . preg_index($i, $matches);
    1263 
    1264     // Add post feed stuff
    1265     $feedregex = '(feed|rdf|rss|rss2|atom)/?$';
    1266     $feedmatch .= $feedregex;
    1267     $feedquery .= '&feed=' . preg_index($i, $matches);
    1268 
    1269     // Add post trackback stuff
    1270     $trackbackregex = 'trackback/?$';
    1271     $trackbackmatch .= $trackbackregex;
     1251
     1252    $structure = str_replace($front, '', $permalink_structure);
     1253    $structure = trim($structure, '/');
     1254    $dirs = explode('/', $structure);
     1255    $num_dirs = count($dirs);
     1256
     1257    $front = preg_replace('|^/+|', '', $front);
     1258
     1259    $post_rewrite = array();
     1260    $struct = $front;
     1261    for ($j = 0; $j < $num_dirs; ++$j) {
     1262        $struct .= $dirs[$j] . '/';
     1263        $match = str_replace($rewritecode, $rewritereplace, $struct);
     1264        $num_toks = preg_match_all('/%.+?%/', $struct, $toks);
     1265        $query = $queries[$num_toks - 1];
     1266
     1267        $pagematch = $match . $pageregex;
     1268        $pagequery = $index . '?' . $query . '&paged=' . preg_index($num_toks + 1, $matches);
     1269
     1270        $feedmatch = $match . $feedregex;
     1271        $feedquery = $feedindex . '?' . $query . '&feed=' . preg_index($num_toks + 1, $matches);
     1272
     1273        $post = 0;
     1274        if (strstr($struct, '%postname%') || strstr($struct, '%post_id%')) {
     1275                $post = 1;
     1276                $trackbackmatch = $match . $trackbackregex;
     1277                $trackbackquery = $trackbackindex . '?' . $query;
     1278                $match = $match . '?([0-9]+)?/?$';
     1279                $query = $index . '?' . $query . '&page=' . preg_index($num_toks + 1, $matches);
     1280        } else {
     1281            $match .= '?';
     1282            $query = $index . '?' . $query;
     1283        }
     1284       
     1285        $post_rewrite = array($feedmatch => $feedquery, $pagematch => $pagequery, $match => $query) + $post_rewrite;
     1286
     1287        if ($post) {
     1288            $post_rewrite = array($trackbackmatch => $trackbackquery) + $post_rewrite;
     1289        }
     1290    }
     1291
     1292    // If the permalink does not have year, month, and day, we need to create a
     1293    // separate archive rule.
     1294    // TODO:  Need to write separate rules for each component of the permalink.
     1295    $doarchive = false;
     1296    if (! (strstr($permalink_structure, '%year') && strstr($permalink_structure, '%monthnum') && strstr($permalink_structure, '%day')) ) {
     1297        $doarchive = true;
     1298        $archivematch = $front . '([0-9]{4})/?([0-9]{1,2})?/?([0-9]{1,2})?/?$';
     1299        $archivequery =  'index.php?year=' . preg_index(1, $matches) . '&monthnum=' . preg_index(2, $matches) . '&day=' . preg_index(3, $matches) ;
     1300    }
    12721301
    12731302    // Site feed
     
    12751304    $sitefeedquery = 'wp-feed.php?feed=' . preg_index(1, $matches);
    12761305
     1306    $sitepagematch = $pageregex;
     1307    $sitepagequery = 'index.php?paged=' . preg_index(1, $matches);
     1308
    12771309    // Site comment feed
    12781310    $sitecommentfeedmatch = 'comments/feed/?([_0-9a-z-]+)?/?$';
    12791311    $sitecommentfeedquery = 'wp-feed.php?feed=' . preg_index(1, $matches) . '&withcomments=1';
    12801312
    1281     // Code for nice categories and authors, currently not very flexible
    1282     $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
     1313    // Code for nice categories and authors.
    12831314    if ( '' == get_settings('category_base') )
    12841315        $catmatch = $front . 'category/';
     
    12901321    $catfeedquery = 'wp-feed.php?category_name=' . preg_index(1, $matches) . '&feed=' . preg_index(2, $matches);
    12911322
     1323    $catpagematch = $catmatch . '(.*)/' . $pageregex;
     1324    $catpagequery = 'index.php?category_name=' . preg_index(1, $matches) . '&paged=' . preg_index(2, $matches);
     1325
    12921326    $catmatch = $catmatch . '?(.*)';
    12931327    $catquery = 'index.php?category_name=' . preg_index(1, $matches);
     
    12991333    $authorfeedquery = 'wp-feed.php?author_name=' . preg_index(1, $matches) . '&feed=' . preg_index(2, $matches);
    13001334
     1335    $authorpagematch = $authormatch . '(.*)/' . $pageregex;
     1336    $authorpagequery = 'index.php?author_name=' . preg_index(1, $matches) . '&paged=' . preg_index(2, $matches);
     1337
    13011338    $authormatch = $authormatch . '?(.*)';
    13021339    $authorquery = 'index.php?author_name=' . preg_index(1, $matches);
    13031340
    13041341    $rewrite = array(
     1342                     $sitefeedmatch => $sitefeedquery,
     1343                     $sitecommentfeedmatch => $sitecommentfeedquery,
     1344                     $sitepagematch => $sitepagequery,
    13051345                     $catfeedmatch => $catfeedquery,
     1346                     $catpagematch => $catpagequery,
    13061347                     $catmatch => $catquery,
    13071348                     $authorfeedmatch => $authorfeedquery,
    1308                      $authormatch => $authorquery,
    1309                      $match => $query,
    1310                      $feedmatch => $feedquery,
    1311                      $trackbackmatch => $trackbackquery,
    1312                      $sitefeedmatch => $sitefeedquery,
    1313                      $sitecommentfeedmatch => $sitecommentfeedquery
     1349                     $authorpagematch => $authorpagequery,
     1350                     $authormatch => $authorquery
    13141351                     );
     1352
     1353    $rewrite = $rewrite + $post_rewrite;
     1354
     1355    if ($doarchive) {
     1356        $rewrite = $rewrite + array($archivematch => $archivequery);
     1357    }
    13151358
    13161359    return $rewrite;
Note: See TracChangeset for help on using the changeset viewer.