Skip to content

Commit 91e1e27

Browse files
committed
Permalink love. Wrap mod_rewrite rules in a conditional. Make sure date permalinks are sane. Add get_year_link().
git-svn-id: https://develop.svn.wordpress.org/trunk@1868 602fd350-edb4-49c9-b593-d223f7449a82
1 parent b04059c commit 91e1e27

3 files changed

Lines changed: 90 additions & 67 deletions

File tree

wp-admin/options-permalink.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
$permalink_structure = get_settings('permalink_structure');
3636
$category_base = get_settings('category_base');
3737

38+
get_date_permastruct();
3839

3940
generate_page_rewrite_rules();
4041

wp-includes/functions.php

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,22 +1330,6 @@ function preg_index($number, $matches = '') {
13301330
}
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-
13491333
function get_page_uri($page) {
13501334
global $wpdb;
13511335
$page = $wpdb->get_row("SELECT post_name, post_parent FROM $wpdb->posts WHERE ID = '$page'");
@@ -1374,6 +1358,68 @@ function page_rewrite_rules() {
13741358
return $rewrite_rules;
13751359
}
13761360

1361+
function 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+
1390+
function 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+
1405+
function 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+
1419+
function get_day_permastruct($permalink_structure = '') {
1420+
return get_date_permastruct($permalink_structure);
1421+
}
1422+
13771423
function generate_rewrite_rules($permalink_structure = '', $matches = '') {
13781424
$rewritecode =
13791425
array(
@@ -1519,15 +1565,8 @@ function rewrite_rules($matches = '', $permalink_structure = '') {
15191565
$prefix = $index . '/';
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
15331572
$sitefeedmatch = $prefix . 'feed/?([_0-9a-z-]+)?/?$';
@@ -1572,12 +1611,7 @@ function rewrite_rules($matches = '', $permalink_structure = '') {
15721611
$pages_rewrite = page_rewrite_rules();
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;
15831617

@@ -1598,15 +1632,20 @@ function mod_rewrite_rules ($permalink_structure) {
15981632
$rules .= "RewriteEngine On\n";
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.
16031643
$match = str_replace('.+?', '.+', $match);
16041644

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

16121651
if (strstr($query, 'index.php')) {

wp-includes/template-functions-links.php

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -105,29 +105,26 @@ function get_page_link($id = false) {
105105
return $link;
106106
}
107107

108+
function 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+
108120
function 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 {
133130
return get_settings('home') .'/'. get_settings('blogfilename') .$querystring_start.'m'.$querystring_equal.$year.zeroise($month, 2);
@@ -139,26 +136,12 @@ function get_day_link($year, $month, $day) {
139136
if (!$year) $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
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-
}
152139

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);
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 {
164147
return get_settings('home') .'/'. get_settings('blogfilename') .$querystring_start.'m'.$querystring_equal.$year.zeroise($month, 2).zeroise($day, 2);

0 commit comments

Comments
 (0)