Make WordPress Core

Changeset 1868


Ignore:
Timestamp:
11/19/2004 08:54:16 PM (19 years ago)
Author:
rboren
Message:

Permalink love. Wrap mod_rewrite rules in a conditional. Make sure date permalinks are sane. Add get_year_link().

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/options-permalink.php

    r1818 r1868  
    3636$category_base = get_settings('category_base');
    3737
     38get_date_permastruct();
    3839
    3940generate_page_rewrite_rules();
  • trunk/wp-includes/functions.php

    r1867 r1868  
    13311331
    13321332
    1333 function page_permastruct() {
    1334     $permalink_structure = get_settings('permalink_structure');
    1335        
    1336     if (empty($permalink_structure)) {
    1337         return '';
    1338     }
    1339 
    1340     $index = 'index.php';
    1341     $prefix = '';
    1342     if (using_index_permalinks()) {
    1343         $prefix = $index . '/';
    1344     }
    1345 
    1346     return '/' . $prefix . 'site/%pagename%';   
    1347 }
    1348 
    13491333function get_page_uri($page) {
    13501334    global $wpdb;
     
    13731357
    13741358    return $rewrite_rules;
     1359}
     1360
     1361function get_date_permastruct($permalink_structure = '') {
     1362    if (empty($permalink_structure)) {
     1363        $permalink_structure = get_settings('permalink_structure');
     1364   
     1365        if (empty($permalink_structure)) {
     1366            return false;
     1367        }
     1368    }
     1369       
     1370    $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));   
     1371        // The date permalink must have year, month, and day separated by slashes.
     1372        $endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%');
     1373
     1374        $date_structure = '';
     1375
     1376        foreach ($endians as $endian) {
     1377            if (false !== strpos($permalink_structure, $endian)) {
     1378                $date_structure = $front . $endian;
     1379                break;
     1380            }
     1381        }
     1382
     1383        if (empty($date_structure)) {
     1384                $date_structure = $front . '%year%/%monthnum%/%day%';
     1385        }
     1386
     1387        return $date_structure;
     1388}
     1389
     1390function get_year_permastruct($permalink_structure = '') {
     1391    $structure = get_date_permastruct($permalink_structure);
     1392
     1393    if (empty($structure)) {
     1394        return false;
     1395    }
     1396
     1397    $structure = str_replace('%monthnum%', '', $structure);
     1398    $structure = str_replace('%day%', '', $structure);
     1399
     1400    $structure = preg_replace('#/+#', '/', $structure);
     1401
     1402    return $structure;
     1403}
     1404
     1405function get_month_permastruct($permalink_structure = '') {
     1406    $structure = get_date_permastruct($permalink_structure);
     1407
     1408    if (empty($structure)) {
     1409        return false;
     1410    }
     1411
     1412    $structure = str_replace('%day%', '', $structure);
     1413
     1414    $structure = preg_replace('#/+#', '/', $structure);
     1415
     1416    return $structure;
     1417}
     1418
     1419function get_day_permastruct($permalink_structure = '') {
     1420    return get_date_permastruct($permalink_structure);
    13751421}
    13761422
     
    15201566    }
    15211567
    1522     // If the permalink does not have year, month, and day, we need to create a
    1523     // separate archive rule.
    1524     $doarchive = false;
    1525     if (! (strstr($permalink_structure, '%year%') && strstr($permalink_structure, '%monthnum%') && strstr($permalink_structure, '%day%')) ||
    1526         preg_match('/%category%.*(%year%|%monthnum%|%day%)/', $permalink_structure)) {
    1527         $doarchive = true;
    1528         $archive_structure = $front . '%year%/%monthnum%/%day%/';
    1529         $archive_rewrite =  generate_rewrite_rules($archive_structure, $matches);
    1530     }
     1568        // Generate date rules.
     1569        $date_rewrite = generate_rewrite_rules(get_date_permastruct($permalink_structure), $matches);
    15311570
    15321571    // Site feed
     
    15731612
    15741613    // Put them together.
    1575     $rewrite = $pages_rewrite + $site_rewrite + $page_rewrite + $search_rewrite + $category_rewrite + $author_rewrite;
    1576 
    1577     // Add on archive rewrite rules if needed.
    1578     if ($doarchive) {
    1579         $rewrite = $rewrite + $archive_rewrite;
    1580     }
     1614    $rewrite = $pages_rewrite + $site_rewrite + $page_rewrite + $search_rewrite + $category_rewrite + $author_rewrite + $date_rewrite;
    15811615
    15821616    $rewrite = $rewrite + $post_rewrite;
     
    15991633    $rules .= "RewriteBase $home_root\n";
    16001634    $rewrite = rewrite_rules('', $permalink_structure);
     1635
     1636    $num_rules = count($rewrite);
     1637    $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" .
     1638        "RewriteCond %{REQUEST_FILENAME} -d\n" .
     1639        "RewriteRule ^.*$ - [S=$num_rules]\n";
     1640
    16011641    foreach ($rewrite as $match => $query) {
    16021642        // Apache 1.3 does not support the reluctant (non-greedy) modifier.
     
    16051645        // If the match is unanchored and greedy, prepend rewrite conditions
    16061646        // to avoid infinite redirects and eclipsing of real files.
    1607         if ($match == '(.+)/?$') {
    1608             $rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" .
    1609                 "RewriteCond %{REQUEST_FILENAME} !-d\n";
     1647        if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
     1648            //nada.
    16101649        }
    16111650
  • trunk/wp-includes/template-functions-links.php

    r1860 r1868  
    106106}
    107107
     108function get_year_link($year) {
     109    global $querystring_start, $querystring_equal;
     110    if (!$year) $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
     111        $yearlink = get_year_permastruct();
     112    if (!empty($yearlink)) {
     113        $yearlink = str_replace('%year%', $year, $yearlink);
     114        return get_settings('home') . $yearlink;
     115    } else {
     116        return get_settings('home') .'/'. get_settings('blogfilename') .$querystring_start.'m'.$querystring_equal.$year;
     117    }
     118}
     119
    108120function get_month_link($year, $month) {
    109121    global $querystring_start, $querystring_equal;
    110122    if (!$year) $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
    111123    if (!$month) $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
    112     if ('' != get_settings('permalink_structure')) {
    113         $permalink = get_settings('permalink_structure');
    114 
    115         // If the permalink structure does not contain year and month, make
    116         // one that does.
    117         if (! (strstr($permalink, '%year%') && strstr($permalink, '%monthnum%'))
    118             || preg_match('/%category%.*(%year%|%monthnum%|%day%)/', $permalink)) {
    119             $front = substr($permalink, 0, strpos($permalink, '%'));
    120             $permalink = $front . '%year%/%monthnum%/';
    121         }
    122 
    123         $off = strpos($permalink, '%monthnum%');
    124         $offset = $off + 11;
    125         $monthlink = substr($permalink, 0, $offset);
    126         if ('/' != substr($monthlink, -1)) $monthlink = substr($monthlink, 0, -1);
     124        $monthlink = get_month_permastruct();
     125    if (!empty($monthlink)) {
    127126        $monthlink = str_replace('%year%', $year, $monthlink);
    128127        $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
    129         $monthlink = str_replace('%post_id%', '', $monthlink);
    130         $monthlink = str_replace('%category%', '', $monthlink);
    131128        return get_settings('home') . $monthlink;
    132129    } else {
     
    140137    if (!$month) $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
    141138    if (!$day) $day = gmdate('j', time()+(get_settings('gmt_offset') * 3600));
    142     if ('' != get_settings('permalink_structure')) {
    143         $permalink = get_settings('permalink_structure');
    144 
    145         // If the permalink structure does not contain year, month, and day,
    146         // make one that does.
    147         if (! (strstr($permalink, '%year%') && strstr($permalink, '%monthnum%')&& strstr($permalink, '%day%'))
    148             || preg_match('/%category%.*(%year%|%monthnum%|%day%)/', $permalink)) {
    149             $front = substr($permalink, 0, strpos($permalink, '%'));
    150             $permalink = $front . '%year%/%monthnum%/%day%/';
    151         }
    152 
    153         $off = strpos($permalink, '%day%');
    154         $offset = $off + 6;
    155         $daylink = substr($permalink, 0, $offset);
    156         if ('/' != substr($daylink, -1)) $daylink = substr($daylink, 0, -1);
     139
     140        $daylink = get_day_permastruct();
     141    if (!empty($daylink)) {
    157142        $daylink = str_replace('%year%', $year, $daylink);
    158143        $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
    159144        $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
    160         $daylink = str_replace('%post_id%', '', $daylink);
    161         $daylink = str_replace('%category%', '', $daylink);
    162145        return get_settings('home') . $daylink;
    163146    } else {
Note: See TracChangeset for help on using the changeset viewer.