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

git-svn-id: http://svn.automattic.com/wordpress/trunk@1868 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
rboren 2004-11-19 20:54:16 +00:00
parent 742a9d549f
commit 9872261202
3 changed files with 90 additions and 67 deletions

View File

@ -35,6 +35,7 @@ if ( isset($_POST) ) {
$permalink_structure = get_settings('permalink_structure');
$category_base = get_settings('category_base');
get_date_permastruct();
generate_page_rewrite_rules();

View File

@ -1330,22 +1330,6 @@ function preg_index($number, $matches = '') {
}
function page_permastruct() {
$permalink_structure = get_settings('permalink_structure');
if (empty($permalink_structure)) {
return '';
}
$index = 'index.php';
$prefix = '';
if (using_index_permalinks()) {
$prefix = $index . '/';
}
return '/' . $prefix . 'site/%pagename%';
}
function get_page_uri($page) {
global $wpdb;
$page = $wpdb->get_row("SELECT post_name, post_parent FROM $wpdb->posts WHERE ID = '$page'");
@ -1374,6 +1358,68 @@ function page_rewrite_rules() {
return $rewrite_rules;
}
function get_date_permastruct($permalink_structure = '') {
if (empty($permalink_structure)) {
$permalink_structure = get_settings('permalink_structure');
if (empty($permalink_structure)) {
return false;
}
}
$front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
// The date permalink must have year, month, and day separated by slashes.
$endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%');
$date_structure = '';
foreach ($endians as $endian) {
if (false !== strpos($permalink_structure, $endian)) {
$date_structure = $front . $endian;
break;
}
}
if (empty($date_structure)) {
$date_structure = $front . '%year%/%monthnum%/%day%';
}
return $date_structure;
}
function get_year_permastruct($permalink_structure = '') {
$structure = get_date_permastruct($permalink_structure);
if (empty($structure)) {
return false;
}
$structure = str_replace('%monthnum%', '', $structure);
$structure = str_replace('%day%', '', $structure);
$structure = preg_replace('#/+#', '/', $structure);
return $structure;
}
function get_month_permastruct($permalink_structure = '') {
$structure = get_date_permastruct($permalink_structure);
if (empty($structure)) {
return false;
}
$structure = str_replace('%day%', '', $structure);
$structure = preg_replace('#/+#', '/', $structure);
return $structure;
}
function get_day_permastruct($permalink_structure = '') {
return get_date_permastruct($permalink_structure);
}
function generate_rewrite_rules($permalink_structure = '', $matches = '') {
$rewritecode =
array(
@ -1519,15 +1565,8 @@ function rewrite_rules($matches = '', $permalink_structure = '') {
$prefix = $index . '/';
}
// If the permalink does not have year, month, and day, we need to create a
// separate archive rule.
$doarchive = false;
if (! (strstr($permalink_structure, '%year%') && strstr($permalink_structure, '%monthnum%') && strstr($permalink_structure, '%day%')) ||
preg_match('/%category%.*(%year%|%monthnum%|%day%)/', $permalink_structure)) {
$doarchive = true;
$archive_structure = $front . '%year%/%monthnum%/%day%/';
$archive_rewrite = generate_rewrite_rules($archive_structure, $matches);
}
// Generate date rules.
$date_rewrite = generate_rewrite_rules(get_date_permastruct($permalink_structure), $matches);
// Site feed
$sitefeedmatch = $prefix . 'feed/?([_0-9a-z-]+)?/?$';
@ -1572,12 +1611,7 @@ function rewrite_rules($matches = '', $permalink_structure = '') {
$pages_rewrite = page_rewrite_rules();
// Put them together.
$rewrite = $pages_rewrite + $site_rewrite + $page_rewrite + $search_rewrite + $category_rewrite + $author_rewrite;
// Add on archive rewrite rules if needed.
if ($doarchive) {
$rewrite = $rewrite + $archive_rewrite;
}
$rewrite = $pages_rewrite + $site_rewrite + $page_rewrite + $search_rewrite + $category_rewrite + $author_rewrite + $date_rewrite;
$rewrite = $rewrite + $post_rewrite;
@ -1598,15 +1632,20 @@ function mod_rewrite_rules ($permalink_structure) {
$rules .= "RewriteEngine On\n";
$rules .= "RewriteBase $home_root\n";
$rewrite = rewrite_rules('', $permalink_structure);
$num_rules = count($rewrite);
$rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" .
"RewriteCond %{REQUEST_FILENAME} -d\n" .
"RewriteRule ^.*$ - [S=$num_rules]\n";
foreach ($rewrite as $match => $query) {
// Apache 1.3 does not support the reluctant (non-greedy) modifier.
$match = str_replace('.+?', '.+', $match);
// If the match is unanchored and greedy, prepend rewrite conditions
// to avoid infinite redirects and eclipsing of real files.
if ($match == '(.+)/?$') {
$rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" .
"RewriteCond %{REQUEST_FILENAME} !-d\n";
if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
//nada.
}
if (strstr($query, 'index.php')) {

View File

@ -105,29 +105,26 @@ function get_page_link($id = false) {
return $link;
}
function get_year_link($year) {
global $querystring_start, $querystring_equal;
if (!$year) $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
$yearlink = get_year_permastruct();
if (!empty($yearlink)) {
$yearlink = str_replace('%year%', $year, $yearlink);
return get_settings('home') . $yearlink;
} else {
return get_settings('home') .'/'. get_settings('blogfilename') .$querystring_start.'m'.$querystring_equal.$year;
}
}
function get_month_link($year, $month) {
global $querystring_start, $querystring_equal;
if (!$year) $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
if (!$month) $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
if ('' != get_settings('permalink_structure')) {
$permalink = get_settings('permalink_structure');
// If the permalink structure does not contain year and month, make
// one that does.
if (! (strstr($permalink, '%year%') && strstr($permalink, '%monthnum%'))
|| preg_match('/%category%.*(%year%|%monthnum%|%day%)/', $permalink)) {
$front = substr($permalink, 0, strpos($permalink, '%'));
$permalink = $front . '%year%/%monthnum%/';
}
$off = strpos($permalink, '%monthnum%');
$offset = $off + 11;
$monthlink = substr($permalink, 0, $offset);
if ('/' != substr($monthlink, -1)) $monthlink = substr($monthlink, 0, -1);
$monthlink = get_month_permastruct();
if (!empty($monthlink)) {
$monthlink = str_replace('%year%', $year, $monthlink);
$monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
$monthlink = str_replace('%post_id%', '', $monthlink);
$monthlink = str_replace('%category%', '', $monthlink);
return get_settings('home') . $monthlink;
} else {
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) {
if (!$year) $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
if (!$month) $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
if (!$day) $day = gmdate('j', time()+(get_settings('gmt_offset') * 3600));
if ('' != get_settings('permalink_structure')) {
$permalink = get_settings('permalink_structure');
// If the permalink structure does not contain year, month, and day,
// make one that does.
if (! (strstr($permalink, '%year%') && strstr($permalink, '%monthnum%')&& strstr($permalink, '%day%'))
|| preg_match('/%category%.*(%year%|%monthnum%|%day%)/', $permalink)) {
$front = substr($permalink, 0, strpos($permalink, '%'));
$permalink = $front . '%year%/%monthnum%/%day%/';
}
$off = strpos($permalink, '%day%');
$offset = $off + 6;
$daylink = substr($permalink, 0, $offset);
if ('/' != substr($daylink, -1)) $daylink = substr($daylink, 0, -1);
$daylink = get_day_permastruct();
if (!empty($daylink)) {
$daylink = str_replace('%year%', $year, $daylink);
$daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
$daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
$daylink = str_replace('%post_id%', '', $daylink);
$daylink = str_replace('%category%', '', $daylink);
return get_settings('home') . $daylink;
} else {
return get_settings('home') .'/'. get_settings('blogfilename') .$querystring_start.'m'.$querystring_equal.$year.zeroise($month, 2).zeroise($day, 2);