Add link filters for bug 743 (hat tip to morganiq). More abstraction and flexibility in WP_Rewrite.

git-svn-id: http://svn.automattic.com/wordpress/trunk@2152 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
rboren 2005-01-26 22:46:40 +00:00
parent 10792f3274
commit 84f2d4264c
3 changed files with 115 additions and 72 deletions

View File

@ -723,8 +723,16 @@ class WP_Rewrite {
var $permalink_structure; var $permalink_structure;
var $category_base; var $category_base;
var $category_structure; var $category_structure;
var $author_base = 'author';
var $author_structure; var $author_structure;
var $date_structure; var $date_structure;
var $page_structure;
var $search_base = 'search';
var $search_structure;
var $comments_base = 'comments';
var $feed_base = 'feed';
var $comments_feed_structure;
var $feed_structure;
var $front; var $front;
var $root = ''; var $root = '';
var $index = 'index.php'; var $index = 'index.php';
@ -791,7 +799,7 @@ class WP_Rewrite {
} }
// If the index is not in the permalink, we're using mod_rewrite. // If the index is not in the permalink, we're using mod_rewrite.
if (preg_match('#^/*index.php#', $this->permalink_structure)) { if (preg_match('#^/*' . $this->index . '#', $this->permalink_structure)) {
return true; return true;
} }
@ -821,7 +829,7 @@ class WP_Rewrite {
$uris = get_settings('page_uris'); $uris = get_settings('page_uris');
$rewrite_rules = array(); $rewrite_rules = array();
$page_structure = '/%pagename%'; $page_structure = $this->get_page_permastruct();
if( is_array( $uris ) ) if( is_array( $uris ) )
{ {
foreach ($uris as $uri => $pagename) { foreach ($uris as $uri => $pagename) {
@ -925,11 +933,71 @@ class WP_Rewrite {
return false; return false;
} }
$this->author_structure = $this->front . 'author/%author%'; $this->author_structure = $this->front . $this->author_base . '/%author%';
return $this->author_structure; return $this->author_structure;
} }
function get_search_permastruct() {
if (isset($this->search_structure)) {
return $this->search_structure;
}
if (empty($this->permalink_structure)) {
$this->search_structure = '';
return false;
}
$this->search_structure = $this->root . $this->search_base . '/%search%';
return $this->search_structure;
}
function get_page_permastruct() {
if (isset($this->page_structure)) {
return $this->page_structure;
}
if (empty($this->permalink_structure)) {
$this->page_structure = '';
return false;
}
$this->page_structure = $this->root . '%pagename%';
return $this->page_structure;
}
function get_feed_permastruct() {
if (isset($this->feed_structure)) {
return $this->feed_structure;
}
if (empty($this->permalink_structure)) {
$this->feed_structure = '';
return false;
}
$this->feed_structure = $this->root . $this->feed_base . '/%feed%';
return $this->feed_structure;
}
function get_comment_feed_permastruct() {
if (isset($this->comment_feed_structure)) {
return $this->comment_feed_structure;
}
if (empty($this->permalink_structure)) {
$this->comment_feed_structure = '';
return false;
}
$this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%';
return $this->comment_feed_structure;
}
function add_rewrite_tag($tag, $pattern, $query) { function add_rewrite_tag($tag, $pattern, $query) {
// If the tag already exists, replace the existing pattern and query for // If the tag already exists, replace the existing pattern and query for
// that tag, otherwise add the new tag, pattern, and query to the end of // that tag, otherwise add the new tag, pattern, and query to the end of
@ -1068,11 +1136,11 @@ class WP_Rewrite {
$root_rewrite = apply_filters('root_rewrite_rules', $root_rewrite); $root_rewrite = apply_filters('root_rewrite_rules', $root_rewrite);
// Comments // Comments
$comments_rewrite = $this->generate_rewrite_rules($this->root . 'comments',true, true, true); $comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, true, true, true);
$comments_rewrite = apply_filters('comments_rewrite_rules', $comments_rewrite); $comments_rewrite = apply_filters('comments_rewrite_rules', $comments_rewrite);
// Search // Search
$search_structure = $this->root . "search/%search%"; $search_structure = $this->get_search_permastruct();
$search_rewrite = $this->generate_rewrite_rules($search_structure); $search_rewrite = $this->generate_rewrite_rules($search_structure);
$search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite); $search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite);
@ -1134,7 +1202,7 @@ class WP_Rewrite {
//nada. //nada.
} }
if (strstr($query, 'index.php')) { if (strstr($query, $this->index)) {
$rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n"; $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
} else { } else {
$rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n"; $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n";
@ -1158,7 +1226,11 @@ class WP_Rewrite {
$this->category_base = get_settings('category_base'); $this->category_base = get_settings('category_base');
unset($this->category_structure); unset($this->category_structure);
unset($this->author_structure); unset($this->author_structure);
unset($this->date_structure); unset($this->date_structure);
unset($this->page_structure);
unset($this->search_structure);
unset($this->feed_structure);
unset($this->comment_feed_structure);
} }
function set_permalink_structure($permalink_structure) { function set_permalink_structure($permalink_structure) {

View File

@ -74,10 +74,10 @@ function get_permalink($id = false) {
$author, $author,
$idpost->post_name, $idpost->post_name,
); );
return get_settings('home') . str_replace($rewritecode, $rewritereplace, $permalink); return apply_filters('post_link', get_settings('home') . str_replace($rewritecode, $rewritereplace, $permalink));
} else { // if they're not using the fancy permalink option } else { // if they're not using the fancy permalink option
$permalink = get_settings('home') . '/?p=' . $idpost->ID; $permalink = get_settings('home') . '/?p=' . $idpost->ID;
return $permalink; return apply_filters('post_link', $permalink);
} }
} }
@ -88,19 +88,17 @@ function get_page_link($id = false) {
$id = $post->ID; $id = $post->ID;
} }
$permalink = get_settings('permalink_structure'); $pagestruct = $wp_rewrite->get_page_permastruct();
if ('' != $permalink) { if ('' != $pagestruct) {
$link = get_page_uri($id); $link = get_page_uri($id);
if ($wp_rewrite->using_index_permalinks()) { $link = str_replace('%pagename%', $link, $pagestruct);
$link = 'index.php/' . $link;
}
$link = get_settings('home') . "/$link/"; $link = get_settings('home') . "/$link/";
} else { } else {
$link = get_settings('home') . "/index.php?page_id=$id"; $link = get_settings('home') . "/?page_id=$id";
} }
return $link; return apply_filters('page_link', $link);
} }
function get_year_link($year) { function get_year_link($year) {
@ -109,9 +107,9 @@ function get_year_link($year) {
$yearlink = $wp_rewrite->get_year_permastruct(); $yearlink = $wp_rewrite->get_year_permastruct();
if (!empty($yearlink)) { if (!empty($yearlink)) {
$yearlink = str_replace('%year%', $year, $yearlink); $yearlink = str_replace('%year%', $year, $yearlink);
return get_settings('home') . trailingslashit($yearlink); return apply_filters('year_link', get_settings('home') . trailingslashit($yearlink));
} else { } else {
return get_settings('home') .'/'. $querystring_start.'m'.$querystring_equal.$year; return apply_filters('year_link', get_settings('home') .'/'. $querystring_start.'m'.$querystring_equal.$year);
} }
} }
@ -123,9 +121,9 @@ function get_month_link($year, $month) {
if (!empty($monthlink)) { if (!empty($monthlink)) {
$monthlink = str_replace('%year%', $year, $monthlink); $monthlink = str_replace('%year%', $year, $monthlink);
$monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink); $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
return get_settings('home') . trailingslashit($monthlink); return apply_filters('month_link', get_settings('home') . trailingslashit($monthlink));
} else { } else {
return get_settings('home') .'/'. $querystring_start.'m'.$querystring_equal.$year.zeroise($month, 2); return apply_filters('month_link', get_settings('home') .'/'. $querystring_start.'m'.$querystring_equal.$year.zeroise($month, 2));
} }
} }
@ -140,68 +138,39 @@ function get_day_link($year, $month, $day) {
$daylink = str_replace('%year%', $year, $daylink); $daylink = str_replace('%year%', $year, $daylink);
$daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink); $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
$daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink); $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
return get_settings('home') . trailingslashit($daylink); return apply_filters('day_link', get_settings('home') . trailingslashit($daylink));
} else { } else {
return get_settings('home') .'/'. $querystring_start.'m'.$querystring_equal.$year.zeroise($month, 2).zeroise($day, 2); return apply_filters('day_link', get_settings('home') .'/'. $querystring_start.'m'.$querystring_equal.$year.zeroise($month, 2).zeroise($day, 2));
} }
} }
function get_feed_link($feed='rss2') { function get_feed_link($feed='rss2') {
global $wp_rewrite; global $wp_rewrite;
$do_perma = 0; $do_perma = 0;
$feed_url = get_settings('siteurl'); $feed_url = get_settings('siteurl');
$comment_feed_url = $feed_url; $comment_feed_url = $feed_url;
$permalink = get_settings('permalink_structure'); $permalink = $wp_rewrite->get_feed_permastruct();
if ('' != $permalink) { if ('' != $permalink) {
$do_perma = 1; if ( false !== strpos($feed, 'comments_') ) {
$feed_url = get_settings('home'); $feed = str_replace('comments_', '', $feed);
$index = 'index.php'; $permalink = $wp_rewrite->get_comment_feed_permastruct();
$prefix = ''; }
if ($wp_rewrite->using_index_permalinks()) {
$feed_url .= '/' . $index;
}
$comment_feed_url = $feed_url; if ( 'rss2' == $feed )
$feed_url .= '/feed'; $feed = '';
$comment_feed_url .= '/comments/feed';
}
switch($feed) { $permalink = str_replace('%feed%', $feed, $permalink);
case 'rdf': $output = get_settings('home') . "/$permalink/";
$output = $feed_url .'/wp-rdf.php'; $output = preg_replace('#/+#', '/', $output);
if ($do_perma) { } else {
$output = $feed_url . '/rdf/'; if ( false !== strpos($feed, 'comments_') )
} $feed = str_replace('comments_', 'comments', $feed);
break;
case 'rss':
$output = $feed_url . '/wp-rss.php';
if ($do_perma) {
$output = $feed_url . '/rss/';
}
break;
case 'atom':
$output = $feed_url .'/wp-atom.php';
if ($do_perma) {
$output = $feed_url . '/atom/';
}
break;
case 'comments_rss2':
$output = $feed_url .'/wp-commentsrss2.php';
if ($do_perma) {
$output = $comment_feed_url . '/';
}
break;
case 'rss2':
default:
$output = $feed_url .'/wp-rss2.php';
if ($do_perma) {
$output = $feed_url . '/';
}
break;
}
return $output; $output = get_settings('siteurl') . "/wp-{$feed}.php";
}
return apply_filters('feed_link', $output);
} }
function edit_post_link($link = 'Edit This', $before = '', $after = '') { function edit_post_link($link = 'Edit This', $before = '', $after = '') {

View File

@ -114,4 +114,6 @@ function shutdown_action_hook() {
} }
register_shutdown_function('shutdown_action_hook'); register_shutdown_function('shutdown_action_hook');
// Everything is loaded.
do_action('init', '');
?> ?>