Make WordPress Core


Ignore:
Timestamp:
05/31/2004 09:34:00 PM (20 years ago)
Author:
rboren
Message:

More rewrite rule refactoring. Move the permalink parser into generate_rewrite_rules() and make it handle categories and authors.

File:
1 edited

Legend:

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

    r1373 r1374  
    11631163}
    11641164
    1165 /* rewrite_rules
    1166  * Construct rewrite matches and queries from permalink structure.
    1167  * matches - The name of the match array to use in the query strings.
    1168  *           If empty, $1, $2, $3, etc. are used.
    1169  * Returns an associate array of matches and queries.
    1170  */
    1171 function rewrite_rules($matches = '', $permalink_structure = '') {
    1172 
    1173     function preg_index($number, $matches = '') {
    1174         $match_prefix = '$';
    1175         $match_suffix = '';
    1176        
    1177         if (! empty($matches)) {
    1178             $match_prefix = '$' . $matches . '[';
    1179                                                $match_suffix = ']';
    1180         }       
    1181        
    1182         return "$match_prefix$number$match_suffix";       
    1183     }
     1165function preg_index($number, $matches = '') {
     1166    $match_prefix = '$';
     1167    $match_suffix = '';
    11841168   
    1185     $rewrite = array();
    1186 
    1187     if (empty($permalink_structure)) {
    1188         $permalink_structure = get_settings('permalink_structure');
    1189        
    1190         if (empty($permalink_structure)) {
    1191             return $rewrite;
    1192         }
    1193     }
    1194 
     1169    if (! empty($matches)) {
     1170        $match_prefix = '$' . $matches . '[';
     1171        $match_suffix = ']';
     1172    }       
     1173   
     1174    return "$match_prefix$number$match_suffix";       
     1175}
     1176
     1177function generate_rewrite_rules($permalink_structure = '') {
    11951178    $rewritecode =
    11961179    array(
     
    12021185    '%second%',
    12031186    '%postname%',
    1204     '%post_id%'
     1187    '%post_id%',
     1188    '%category%',
     1189    '%author%'
    12051190    );
    12061191
     
    12141199    '([0-9]{1,2})',
    12151200    '([_0-9a-z-]+)',
    1216     '([0-9]+)'
     1201    '([0-9]+)',
     1202    '(.*)',
     1203    '([_0-9a-z-]+)'
    12171204    );
    12181205
     
    12261213    'second=',
    12271214    'name=',
    1228     'p='
     1215    'p=',
     1216    'category_name=',
     1217    'author_name='
    12291218    );
    12301219
     
    12901279    }
    12911280
     1281    return $post_rewrite;
     1282}
     1283
     1284/* rewrite_rules
     1285 * Construct rewrite matches and queries from permalink structure.
     1286 * matches - The name of the match array to use in the query strings.
     1287 *           If empty, $1, $2, $3, etc. are used.
     1288 * Returns an associate array of matches and queries.
     1289 */
     1290function rewrite_rules($matches = '', $permalink_structure = '') {
     1291    $rewrite = array();
     1292
     1293    if (empty($permalink_structure)) {
     1294        $permalink_structure = get_settings('permalink_structure');
     1295       
     1296        if (empty($permalink_structure)) {
     1297            return $rewrite;
     1298        }
     1299    }
     1300
     1301    $post_rewrite = generate_rewrite_rules($permalink_structure);
     1302
    12921303    // If the permalink does not have year, month, and day, we need to create a
    12931304    // separate archive rule.
    1294     // TODO:  Need to write separate rules for each component of the permalink.
    12951305    $doarchive = false;
    12961306    if (! (strstr($permalink_structure, '%year') && strstr($permalink_structure, '%monthnum') && strstr($permalink_structure, '%day')) ) {
    12971307        $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     }
     1308        $archive_structure = $front . '%year%/%monthnum%/%day%/';
     1309        $archive_rewrite =  generate_rewrite_rules($archive_structure);
     1310    }
     1311
     1312    $feedregex = '(feed|rdf|rss|rss2|atom)/?$';
     1313    $pageregex = 'page/?([0-9]{1,})/?$';
     1314    $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));   
    13011315
    13021316    // Site feed
     
    13041318    $sitefeedquery = 'wp-feed.php?feed=' . preg_index(1, $matches);
    13051319
    1306     $sitepagematch = $pageregex;
    1307     $sitepagequery = 'index.php?paged=' . preg_index(1, $matches);
    1308 
    13091320    // Site comment feed
    13101321    $sitecommentfeedmatch = 'comments/feed/?([_0-9a-z-]+)?/?$';
    13111322    $sitecommentfeedquery = 'wp-feed.php?feed=' . preg_index(1, $matches) . '&withcomments=1';
    13121323
    1313     // Code for nice categories and authors.
    1314     if ( '' == get_settings('category_base') )
    1315         $catmatch = $front . 'category/';
    1316     else
    1317         $catmatch = get_settings('category_base') . '/';
    1318     $catmatch = preg_replace('|^/+|', '', $catmatch);
    1319    
    1320     $catfeedmatch = $catmatch . '(.*)/' . $feedregex;
    1321     $catfeedquery = 'wp-feed.php?category_name=' . preg_index(1, $matches) . '&feed=' . preg_index(2, $matches);
    1322 
    1323     $catpagematch = $catmatch . '(.*)/' . $pageregex;
    1324     $catpagequery = 'index.php?category_name=' . preg_index(1, $matches) . '&paged=' . preg_index(2, $matches);
    1325 
    1326     $catmatch = $catmatch . '?(.*)';
    1327     $catquery = 'index.php?category_name=' . preg_index(1, $matches);
    1328 
    1329     $authormatch = $front . 'author/';
    1330     $authormatch = preg_replace('|^/+|', '', $authormatch);
    1331 
    1332     $authorfeedmatch = $authormatch . '(.*)/' . $feedregex;
    1333     $authorfeedquery = 'wp-feed.php?author_name=' . preg_index(1, $matches) . '&feed=' . preg_index(2, $matches);
    1334 
    1335     $authorpagematch = $authormatch . '(.*)/' . $pageregex;
    1336     $authorpagequery = 'index.php?author_name=' . preg_index(1, $matches) . '&paged=' . preg_index(2, $matches);
    1337 
    1338     $authormatch = $authormatch . '?(.*)';
    1339     $authorquery = 'index.php?author_name=' . preg_index(1, $matches);
    1340 
    1341     $rewrite = array(
     1324    // Site page
     1325    $sitepagematch = $pageregex;
     1326    $sitepagequery = 'index.php?paged=' . preg_index(1, $matches);
     1327
     1328    $site_rewrite = array(
    13421329                     $sitefeedmatch => $sitefeedquery,
    13431330                     $sitecommentfeedmatch => $sitecommentfeedquery,
    13441331                     $sitepagematch => $sitepagequery,
    1345                      $catfeedmatch => $catfeedquery,
    1346                      $catpagematch => $catpagequery,
    1347                      $catmatch => $catquery,
    1348                      $authorfeedmatch => $authorfeedquery,
    1349                      $authorpagematch => $authorpagequery,
    1350                      $authormatch => $authorquery
    13511332                     );
    13521333
    1353     $rewrite = $rewrite + $post_rewrite;
    1354 
     1334    // Categories
     1335    if ( '' == get_settings('category_base') )
     1336        $category_structure = $front . 'category/';
     1337    else
     1338        $category_structure = get_settings('category_base') . '/';
     1339
     1340    $category_structure = $category_structure . '%category%';
     1341    $category_rewrite = generate_rewrite_rules($category_structure);
     1342
     1343    // Authors
     1344    $author_structure = $front . 'author/%author%';
     1345    $author_rewrite = generate_rewrite_rules($author_structure);
     1346
     1347
     1348    // Put them together.
     1349    $rewrite = $site_rewrite + $category_rewrite + $author_rewrite + $post_rewrite;
     1350
     1351    // Add on archive rewrite rules if needed.
    13551352    if ($doarchive) {
    1356         $rewrite = $rewrite + array($archivematch => $archivequery);
     1353        $rewrite = $rewrite + $archive_rewrite;
    13571354    }
    13581355
Note: See TracChangeset for help on using the changeset viewer.