From 5164d38cb5d018f4a577caca0025b863b213585d Mon Sep 17 00:00:00 2001 From: Mark Jaquith Date: Thu, 15 Feb 2007 07:07:12 +0000 Subject: [PATCH] Consistent use or disuse of trailing slashes in URLs according to user preference. props MathiasBynens. fixes #1485 git-svn-id: https://develop.svn.wordpress.org/trunk@4886 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/category-template.php | 4 ++-- wp-includes/comment-template.php | 2 +- wp-includes/feed.php | 6 +++--- wp-includes/link-template.php | 35 ++++++++++++++++++++++++------- wp-includes/rewrite.php | 2 ++ 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/wp-includes/category-template.php b/wp-includes/category-template.php index 94b66e2463..f33446cc6d 100644 --- a/wp-includes/category-template.php +++ b/wp-includes/category-template.php @@ -32,10 +32,10 @@ function get_category_link($category_id) { $category_nicename = $category->category_nicename; if ( $parent = $category->category_parent ) - $category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename . '/'; + $category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename; $catlink = str_replace('%category%', $category_nicename, $catlink); - $catlink = get_option('home') . trailingslashit($catlink); + $catlink = get_option('home') . user_trailingslashit($catlink); } return apply_filters('category_link', $catlink, $category_id); } diff --git a/wp-includes/comment-template.php b/wp-includes/comment-template.php index 38587dae5d..d57aff5f72 100644 --- a/wp-includes/comment-template.php +++ b/wp-includes/comment-template.php @@ -226,7 +226,7 @@ function get_trackback_url() { $tb_url = get_option('siteurl') . '/wp-trackback.php?p=' . $id; if ( '' != get_option('permalink_structure') ) - $tb_url = trailingslashit(get_permalink()) . 'trackback/'; + $tb_url = trailingslashit(get_permalink()) . user_trailingslashit('trackback'); return $tb_url; } diff --git a/wp-includes/feed.php b/wp-includes/feed.php index 6c57fc5e7d..04f54d9a87 100644 --- a/wp-includes/feed.php +++ b/wp-includes/feed.php @@ -99,7 +99,7 @@ function comments_rss($commentsrssfilename = '') { global $id; if ( '' != get_option('permalink_structure') ) - $url = trailingslashit( get_permalink() ) . 'feed/'; + $url = trailingslashit( get_permalink() ) . user_trailingslashit('feed'); else $url = get_option('home') . "/$commentsrssfilename?feed=rss2&p=$id"; @@ -115,7 +115,7 @@ function get_author_rss_link($echo = false, $author_id, $author_nicename) { $link = get_option('home') . '?feed=rss2&author=' . $author_id; } else { $link = get_author_posts_url($author_id, $author_nicename); - $link = $link . "feed/"; + $link = $link . user_trailingslashit('feed'); } $link = apply_filters('author_feed_link', $link); @@ -133,7 +133,7 @@ function get_category_rss_link($echo = false, $cat_ID, $category_nicename) { $link = get_option('home') . '?feed=rss2&cat=' . $cat_ID; } else { $link = get_category_link($cat_ID); - $link = $link . "feed/"; + $link = $link . user_trailingslashit('feed/'); } $link = apply_filters('category_feed_link', $link); diff --git a/wp-includes/link-template.php b/wp-includes/link-template.php index 20b9132759..f576129f8b 100644 --- a/wp-includes/link-template.php +++ b/wp-includes/link-template.php @@ -11,6 +11,23 @@ function permalink_link() { // For backwards compatibility } +/** + * Conditionally adds a trailing slash if the permalink structure + * has a trailing slash, strips the trailing slash if not + * @global object Uses $wp_rewrite + * @param $string string a URL with or without a trailing slash + * @return string + */ +function user_trailingslashit($string) { + global $wp_rewrite; + if ( $wp_rewrite->use_trailing_slashes ) + $string = trailingslashit($string); + else + $string = preg_replace('|/$|', '', $string); // untrailing slash + return $string; +} + + function permalink_anchor($mode = 'id') { global $post; switch ( strtolower($mode) ) { @@ -116,7 +133,8 @@ function _get_page_link( $id = false ) { if ( '' != $pagestruct && 'draft' != $post->post_status ) { $link = get_page_uri($id); $link = str_replace('%pagename%', $link, $pagestruct); - $link = get_option('home') . "/$link/"; + $link = get_option('home') . "/$link"; + $link = user_trailingslashit($link); } else { $link = get_option('home') . "/?page_id=$id"; } @@ -158,7 +176,7 @@ function get_year_link($year) { $yearlink = $wp_rewrite->get_year_permastruct(); if ( !empty($yearlink) ) { $yearlink = str_replace('%year%', $year, $yearlink); - return apply_filters('year_link', get_option('home') . trailingslashit($yearlink), $year); + return apply_filters('year_link', get_option('home') . user_trailingslashit($yearlink), $year); } else { return apply_filters('year_link', get_option('home') . '/?m=' . $year, $year); } @@ -174,7 +192,7 @@ function get_month_link($year, $month) { if ( !empty($monthlink) ) { $monthlink = str_replace('%year%', $year, $monthlink); $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink); - return apply_filters('month_link', get_option('home') . trailingslashit($monthlink), $year, $month); + return apply_filters('month_link', get_option('home') . user_trailingslashit($monthlink), $year, $month); } else { return apply_filters('month_link', get_option('home') . '/?m=' . $year . zeroise($month, 2), $year, $month); } @@ -194,7 +212,7 @@ function get_day_link($year, $month, $day) { $daylink = str_replace('%year%', $year, $daylink); $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink); $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink); - return apply_filters('day_link', get_option('home') . trailingslashit($daylink), $year, $month, $day); + return apply_filters('day_link', get_option('home') . user_trailingslashit($daylink), $year, $month, $day); } else { return apply_filters('day_link', get_option('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day); } @@ -217,8 +235,8 @@ function get_feed_link($feed='rss2') { $feed = ''; $permalink = str_replace('%feed%', $feed, $permalink); - $permalink = preg_replace('#/+#', '/', "/$permalink/"); - $output = get_option('home') . $permalink; + $permalink = preg_replace('#/+#', '/', "/$permalink"); + $output = get_option('home') . user_trailingslashit($permalink); } else { if ( false !== strpos($feed, 'comments_') ) $feed = str_replace('comments_', 'comments-', $feed); @@ -435,12 +453,13 @@ function get_pagenum_link($pagenum = 1) { $qstr = preg_replace('|^/+|', '', $qstr); if ( $permalink ) - $qstr = trailingslashit($qstr); + $qstr = user_trailingslashit($qstr); $qstr = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', trailingslashit( get_option('home') ) . $qstr ); // showing /page/1/ or ?paged=1 is redundant if ( 1 === $pagenum ) { - $qstr = str_replace('page/1/', '', $qstr); // for mod_rewrite style + $qstr = str_replace(user_trailingslashit('index.php/page/1'), '', $qstr); // for PATHINFO style + $qstr = str_replace(user_trailingslashit('page/1'), '', $qstr); // for mod_rewrite style $qstr = remove_query_arg('paged', $qstr); // for query style } return $qstr; diff --git a/wp-includes/rewrite.php b/wp-includes/rewrite.php index f6283b40a1..95a1b1af93 100644 --- a/wp-includes/rewrite.php +++ b/wp-includes/rewrite.php @@ -148,6 +148,7 @@ function url_to_postid($url) { class WP_Rewrite { var $permalink_structure; + var $use_trailing_slashes; var $category_base; var $category_structure; var $author_base = 'author'; @@ -876,6 +877,7 @@ class WP_Rewrite { unset($this->search_structure); unset($this->feed_structure); unset($this->comment_feed_structure); + $this->use_trailing_slashes = ( substr($this->permalink_structure, -1, 1) == '/' ) ? true : false; } function set_permalink_structure($permalink_structure) {