Move rewrite and permalink functions into WP_Rewrite class.
git-svn-id: http://svn.automattic.com/wordpress/trunk@1908 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
6f7674778f
commit
b1a4a04126
|
@ -490,7 +490,7 @@ function extract_from_markers($filename, $marker) {
|
|||
}
|
||||
|
||||
function save_mod_rewrite_rules() {
|
||||
global $is_apache;
|
||||
global $is_apache, $wp_rewrite;
|
||||
$home = get_settings('home');
|
||||
if ( $home != '' && $home != get_settings('siteurl') ) {
|
||||
$home_path = parse_url($home);
|
||||
|
@ -506,15 +506,13 @@ function save_mod_rewrite_rules() {
|
|||
else
|
||||
$writable = false;
|
||||
|
||||
$permalink_structure = get_settings('permalink_structure');
|
||||
|
||||
if ( strstr($permalink_structure, 'index.php') ) // If they're using
|
||||
if ($wp_rewrite->using_index_permalinks())
|
||||
$usingpi = true;
|
||||
else
|
||||
$usingpi = false;
|
||||
|
||||
if ( $writable && !$usingpi && $is_apache ) {
|
||||
$rules = explode("\n", mod_rewrite_rules($permalink_structure));
|
||||
$rules = explode("\n", $wp_rewrite->mod_rewrite_rules());
|
||||
insert_with_markers($home_path.'.htaccess', 'WordPress', $rules);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,22 +21,20 @@ if ( isset($_POST) ) {
|
|||
$permalink_structure = $_POST['permalink_structure'];
|
||||
if (! empty($permalink_structure) )
|
||||
$permalink_structure = preg_replace('#/+#', '/', '/' . $_POST['permalink_structure']);
|
||||
update_option('permalink_structure', $permalink_structure);
|
||||
$wp_rewrite->set_permalink_structure($permalink_structure);
|
||||
}
|
||||
|
||||
if ( isset($_POST['category_base']) ) {
|
||||
$category_base = $_POST['category_base'];
|
||||
if (! empty($category_base) )
|
||||
$category_base = preg_replace('#/+#', '/', '/' . $_POST['category_base']);
|
||||
update_option('category_base', $category_base);
|
||||
$wp_rewrite->set_category_base($category_base);
|
||||
}
|
||||
}
|
||||
|
||||
$permalink_structure = get_settings('permalink_structure');
|
||||
$category_base = get_settings('category_base');
|
||||
|
||||
get_date_permastruct();
|
||||
|
||||
generate_page_rewrite_rules();
|
||||
|
||||
if ( (!file_exists($home_path.'.htaccess') && is_writable($home_path)) || is_writable($home_path.'.htaccess') )
|
||||
|
@ -44,7 +42,7 @@ if ( (!file_exists($home_path.'.htaccess') && is_writable($home_path)) || is_wri
|
|||
else
|
||||
$writable = false;
|
||||
|
||||
if ( strstr($permalink_structure, 'index.php') ) // If they're using
|
||||
if ($wp_rewrite->using_index_permalinks())
|
||||
$usingpi = true;
|
||||
else
|
||||
$usingpi = false;
|
||||
|
@ -106,7 +104,7 @@ save_mod_rewrite_rules();
|
|||
<p><?php _e('If your <code>.htaccess</code> was <a href="http://codex.wordpress.org/Make_a_Directory_Writable">writable</a> we could do this automatically, but it isn’t so these are the mod_rewrite rules you should have in your <code>.htaccess</code> file. Click in the field and press <kbd>CTRL + a</kbd> to select all.') ?></p>
|
||||
<form action="options-permalink.php" method="post">
|
||||
<p>
|
||||
<textarea rows="5" style="width: 98%;" name="rules"><?php echo mod_rewrite_rules($permalink_structure); ?>
|
||||
<textarea rows="5" style="width: 98%;" name="rules"><?php echo $wp_rewrite->mod_rewrite_rules(); ?>
|
||||
</textarea>
|
||||
</p>
|
||||
<?php endif; ?>
|
||||
|
|
|
@ -18,7 +18,7 @@ if ((isset($_GET['error']) && $_GET['error'] == '404') ||
|
|||
$error = '404';
|
||||
|
||||
// Fetch the rewrite rules.
|
||||
$rewrite = rewrite_rules('matches');
|
||||
$rewrite = $wp_rewrite->wp_rewrite_rules();
|
||||
|
||||
if (! empty($rewrite)) {
|
||||
$pathinfo = $_SERVER['PATH_INFO'];
|
||||
|
|
|
@ -698,4 +698,400 @@ class retrospam_mgr {
|
|||
|
||||
}
|
||||
|
||||
class WP_Rewrite {
|
||||
var $permalink_structure;
|
||||
var $category_base;
|
||||
var $category_structure;
|
||||
var $date_structure;
|
||||
var $front;
|
||||
var $prefix = '';
|
||||
var $index = 'index.php';
|
||||
var $matches = '';
|
||||
var $rewritecode =
|
||||
array(
|
||||
'%year%',
|
||||
'%monthnum%',
|
||||
'%day%',
|
||||
'%hour%',
|
||||
'%minute%',
|
||||
'%second%',
|
||||
'%postname%',
|
||||
'%post_id%',
|
||||
'%category%',
|
||||
'%author%',
|
||||
'%pagename%',
|
||||
'%search%'
|
||||
);
|
||||
|
||||
var $rewritereplace =
|
||||
array(
|
||||
'([0-9]{4})',
|
||||
'([0-9]{1,2})',
|
||||
'([0-9]{1,2})',
|
||||
'([0-9]{1,2})',
|
||||
'([0-9]{1,2})',
|
||||
'([0-9]{1,2})',
|
||||
'([^/]+)',
|
||||
'([0-9]+)',
|
||||
'(.+?)',
|
||||
'([^/]+)',
|
||||
'([^/]+)',
|
||||
'(.+)'
|
||||
);
|
||||
|
||||
var $queryreplace =
|
||||
array (
|
||||
'year=',
|
||||
'monthnum=',
|
||||
'day=',
|
||||
'hour=',
|
||||
'minute=',
|
||||
'second=',
|
||||
'name=',
|
||||
'p=',
|
||||
'category_name=',
|
||||
'author_name=',
|
||||
'pagename=',
|
||||
's='
|
||||
);
|
||||
|
||||
function using_index_permalinks() {
|
||||
if (empty($this->permalink_structure)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the index is not in the permalink, we're using mod_rewrite.
|
||||
if (preg_match('#^/*index.php#', $this->permalink_structure)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function preg_index($number) {
|
||||
$match_prefix = '$';
|
||||
$match_suffix = '';
|
||||
|
||||
if (! empty($this->matches)) {
|
||||
$match_prefix = '$' . $this->matches . '[';
|
||||
$match_suffix = ']';
|
||||
}
|
||||
|
||||
return "$match_prefix$number$match_suffix";
|
||||
}
|
||||
|
||||
function page_rewrite_rules() {
|
||||
$uris = get_settings('page_uris');
|
||||
|
||||
$rewrite_rules = array();
|
||||
if( is_array( $uris ) )
|
||||
{
|
||||
foreach ($uris as $uri => $pagename) {
|
||||
$rewrite_rules += array($uri . '/?$' => "index.php?pagename=" . urldecode($pagename));
|
||||
}
|
||||
}
|
||||
|
||||
return $rewrite_rules;
|
||||
}
|
||||
|
||||
function get_date_permastruct() {
|
||||
if (isset($this->date_structure)) {
|
||||
return $this->date_structure;
|
||||
}
|
||||
|
||||
if (empty($this->permalink_structure)) {
|
||||
$this->date_structure = '';
|
||||
return false;
|
||||
}
|
||||
|
||||
// The date permalink must have year, month, and day separated by slashes.
|
||||
$endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%');
|
||||
|
||||
$this->date_structure = '';
|
||||
|
||||
foreach ($endians as $endian) {
|
||||
if (false !== strpos($this->permalink_structure, $endian)) {
|
||||
$this->date_structure = $this->front . $endian;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($this->date_structure)) {
|
||||
$this->date_structure = $front . '%year%/%monthnum%/%day%';
|
||||
}
|
||||
|
||||
return $this->date_structure;
|
||||
}
|
||||
|
||||
function get_year_permastruct() {
|
||||
$structure = $this->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() {
|
||||
$structure = $this->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() {
|
||||
return $this->get_date_permastruct($permalink_structure);
|
||||
}
|
||||
|
||||
function get_category_permastruct() {
|
||||
if (isset($this->category_structure)) {
|
||||
return $this->category_structure;
|
||||
}
|
||||
|
||||
if (empty($this->permalink_structure)) {
|
||||
$this->category_structure = '';
|
||||
return false;
|
||||
}
|
||||
|
||||
if (empty($this->category_base))
|
||||
$this->category_structure = $this->front . 'category/';
|
||||
else
|
||||
$this->category_structure = $this->category_base . '/';
|
||||
|
||||
$this->category_structure .= '%category%';
|
||||
|
||||
return $this->category_structure;
|
||||
}
|
||||
|
||||
function generate_rewrite_rules($permalink_structure = '', $forcomments = false) {
|
||||
$feedregex2 = '(feed|rdf|rss|rss2|atom)/?$';
|
||||
$feedregex = 'feed/' . $feedregex2;
|
||||
|
||||
$trackbackregex = 'trackback/?$';
|
||||
$pageregex = 'page/?([0-9]{1,})/?$';
|
||||
|
||||
$front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
|
||||
preg_match_all('/%.+?%/', $permalink_structure, $tokens);
|
||||
|
||||
$num_tokens = count($tokens[0]);
|
||||
|
||||
$index = $this->index;
|
||||
$feedindex = $index;
|
||||
$trackbackindex = $index;
|
||||
for ($i = 0; $i < $num_tokens; ++$i) {
|
||||
if (0 < $i) {
|
||||
$queries[$i] = $queries[$i - 1] . '&';
|
||||
}
|
||||
|
||||
$query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1);
|
||||
$queries[$i] .= $query_token;
|
||||
}
|
||||
|
||||
$structure = $permalink_structure;
|
||||
if ($front != '/') {
|
||||
$structure = str_replace($front, '', $structure);
|
||||
}
|
||||
$structure = trim($structure, '/');
|
||||
$dirs = explode('/', $structure);
|
||||
$num_dirs = count($dirs);
|
||||
|
||||
$front = preg_replace('|^/+|', '', $front);
|
||||
|
||||
$post_rewrite = array();
|
||||
$struct = $front;
|
||||
for ($j = 0; $j < $num_dirs; ++$j) {
|
||||
$struct .= $dirs[$j] . '/';
|
||||
$struct = ltrim($struct, '/');
|
||||
$match = str_replace($this->rewritecode, $this->rewritereplace, $struct);
|
||||
$num_toks = preg_match_all('/%.+?%/', $struct, $toks);
|
||||
$query = $queries[$num_toks - 1];
|
||||
|
||||
$pagematch = $match . $pageregex;
|
||||
$pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1);
|
||||
|
||||
$feedmatch = $match . $feedregex;
|
||||
$feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
|
||||
|
||||
$feedmatch2 = $match . $feedregex2;
|
||||
$feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
|
||||
|
||||
if ($forcomments) {
|
||||
$feedquery .= '&withcomments=1';
|
||||
$feedquery2 .= '&withcomments=1';
|
||||
}
|
||||
|
||||
$rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2, $pagematch => $pagequery);
|
||||
|
||||
if ($num_toks) {
|
||||
$post = 0;
|
||||
if (strstr($struct, '%postname%') || strstr($struct, '%post_id%')
|
||||
|| (strstr($struct, '%year%') && strstr($struct, '%monthnum%') && strstr($struct, '%day%') && strstr($struct, '%hour%') && strstr($struct, '%minute') && strstr($struct, '%second%'))) {
|
||||
$post = 1;
|
||||
$trackbackmatch = $match . $trackbackregex;
|
||||
$trackbackquery = $trackbackindex . '?' . $query . '&tb=1';
|
||||
$match = $match . '?([0-9]+)?/?$';
|
||||
$query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1);
|
||||
} else {
|
||||
$match .= '?$';
|
||||
$query = $index . '?' . $query;
|
||||
}
|
||||
|
||||
$rewrite = $rewrite + array($match => $query);
|
||||
|
||||
if ($post) {
|
||||
$rewrite = array($trackbackmatch => $trackbackquery) + $rewrite;
|
||||
}
|
||||
}
|
||||
|
||||
$post_rewrite = $rewrite + $post_rewrite;
|
||||
}
|
||||
|
||||
return $post_rewrite;
|
||||
}
|
||||
|
||||
/* rewrite_rules
|
||||
* Construct rewrite matches and queries from permalink structure.
|
||||
* Returns an associate array of matches and queries.
|
||||
*/
|
||||
function rewrite_rules() {
|
||||
$rewrite = array();
|
||||
|
||||
if (empty($this->permalink_structure)) {
|
||||
return $rewrite;
|
||||
}
|
||||
|
||||
// Post
|
||||
$post_rewrite = $this->generate_rewrite_rules($this->permalink_structure);
|
||||
|
||||
// Date
|
||||
$date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct());
|
||||
|
||||
// Root
|
||||
$root_rewrite = $this->generate_rewrite_rules($this->prefix . '/');
|
||||
|
||||
// Comments
|
||||
$comments_rewrite = $this->generate_rewrite_rules($this->prefix . 'comments', true);
|
||||
|
||||
// Search
|
||||
$search_structure = $this->prefix . "search/%search%";
|
||||
$search_rewrite = $this->generate_rewrite_rules($search_structure);
|
||||
|
||||
// Categories
|
||||
$category_rewrite = $this->generate_rewrite_rules($this->get_category_permastruct());
|
||||
|
||||
// Authors
|
||||
$author_structure = $this->front . 'author/%author%';
|
||||
$author_rewrite = $this->generate_rewrite_rules($author_structure);
|
||||
|
||||
// Pages
|
||||
$page_rewrite = $this->page_rewrite_rules();
|
||||
|
||||
// Deprecated style static pages
|
||||
$page_structure = $this->prefix . 'site/%pagename%';
|
||||
$old_page_rewrite = $this->generate_rewrite_rules($page_structure);
|
||||
|
||||
// Put them together.
|
||||
$this->rewrite = $page_rewrite + $root_rewrite + $comments_rewrite + $old_page_rewrite + $search_rewrite + $category_rewrite + $author_rewrite + $date_rewrite + $post_rewrite;
|
||||
|
||||
$this->rewrite = apply_filters('rewrite_rules_array', $this->rewrite);
|
||||
return $this->rewrite;
|
||||
}
|
||||
|
||||
function wp_rewrite_rules() {
|
||||
$this->matches = 'matches';
|
||||
return $this->rewrite_rules();
|
||||
}
|
||||
|
||||
function mod_rewrite_rules () {
|
||||
$site_root = str_replace('http://', '', trim(get_settings('siteurl')));
|
||||
$site_root = preg_replace('|([^/]*)(.*)|i', '$2', $site_root);
|
||||
if ('/' != substr($site_root, -1)) $site_root = $site_root . '/';
|
||||
|
||||
$home_root = str_replace('http://', '', trim(get_settings('home')));
|
||||
$home_root = preg_replace('|([^/]*)(.*)|i', '$2', $home_root);
|
||||
if ('/' != substr($home_root, -1)) $home_root = $home_root . '/';
|
||||
|
||||
$rules = "<IfModule mod_rewrite.c>\n";
|
||||
$rules .= "RewriteEngine On\n";
|
||||
$rules .= "RewriteBase $home_root\n";
|
||||
$this->matches = '';
|
||||
$rewrite = $this->rewrite_rules();
|
||||
|
||||
$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 == '(.+)/?$' || $match == '([^/]+)/?$' ) {
|
||||
//nada.
|
||||
}
|
||||
|
||||
if (strstr($query, 'index.php')) {
|
||||
$rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
|
||||
} else {
|
||||
$rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n";
|
||||
}
|
||||
}
|
||||
$rules .= "</IfModule>\n";
|
||||
|
||||
$rules = apply_filters('rewrite_rules', $rules);
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
function init() {
|
||||
$this->permalink_structure = get_settings('permalink_structure');
|
||||
$this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%'));
|
||||
$this->prefix = '';
|
||||
if ($this->using_index_permalinks()) {
|
||||
$this->prefix = $this->index . '/';
|
||||
}
|
||||
$this->category_base = get_settings('category_base');
|
||||
unset($this->category_structure);
|
||||
unset($this->date_structure);
|
||||
}
|
||||
|
||||
function set_permalink_structure($permalink_structure) {
|
||||
if ($permalink_structure != $this->permalink_structure) {
|
||||
update_option('permalink_structure', $permalink_structure);
|
||||
$this->init();
|
||||
}
|
||||
}
|
||||
|
||||
function set_category_base($category_base) {
|
||||
if ($category_base != $this->category_base) {
|
||||
update_option('category_base', $category_base);
|
||||
$this->init();
|
||||
}
|
||||
}
|
||||
|
||||
function WP_Rewrite() {
|
||||
$this->init();
|
||||
}
|
||||
}
|
||||
|
||||
// Make a global instance.
|
||||
if (! isset($wp_rewrite)) {
|
||||
$wp_rewrite = new WP_Rewrite();
|
||||
}
|
||||
|
||||
?>
|
|
@ -1305,36 +1305,6 @@ function remove_action($tag, $function_to_remove, $priority = 10) {
|
|||
remove_filter($tag, $function_to_remove, $priority);
|
||||
}
|
||||
|
||||
function using_index_permalinks($permalink_structure = '') {
|
||||
if (empty($permalink_structure)) {
|
||||
$permalink_structure = get_settings('permalink_structure');
|
||||
|
||||
if (empty($permalink_structure)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// If the index is not in the permalink, we're using mod_rewrite.
|
||||
if (preg_match('#^/*index.php#', $permalink_structure)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function preg_index($number, $matches = '') {
|
||||
$match_prefix = '$';
|
||||
$match_suffix = '';
|
||||
|
||||
if (! empty($matches)) {
|
||||
$match_prefix = '$' . $matches . '[';
|
||||
$match_suffix = ']';
|
||||
}
|
||||
|
||||
return "$match_prefix$number$match_suffix";
|
||||
}
|
||||
|
||||
|
||||
function get_page_uri($page) {
|
||||
global $wpdb;
|
||||
$page = $wpdb->get_row("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = '$page'");
|
||||
|
@ -1354,325 +1324,6 @@ function get_page_uri($page) {
|
|||
return $uri;
|
||||
}
|
||||
|
||||
function page_rewrite_rules() {
|
||||
$uris = get_settings('page_uris');
|
||||
|
||||
$rewrite_rules = array();
|
||||
if( is_array( $uris ) )
|
||||
{
|
||||
foreach ($uris as $uri => $pagename) {
|
||||
$rewrite_rules += array($uri . '/?$' => "index.php?pagename=" . urldecode($pagename));
|
||||
}
|
||||
}
|
||||
|
||||
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 = '', $forcomments = false) {
|
||||
$rewritecode =
|
||||
array(
|
||||
'%year%',
|
||||
'%monthnum%',
|
||||
'%day%',
|
||||
'%hour%',
|
||||
'%minute%',
|
||||
'%second%',
|
||||
'%postname%',
|
||||
'%post_id%',
|
||||
'%category%',
|
||||
'%author%',
|
||||
'%pagename%',
|
||||
'%search%'
|
||||
);
|
||||
|
||||
$rewritereplace =
|
||||
array(
|
||||
'([0-9]{4})',
|
||||
'([0-9]{1,2})',
|
||||
'([0-9]{1,2})',
|
||||
'([0-9]{1,2})',
|
||||
'([0-9]{1,2})',
|
||||
'([0-9]{1,2})',
|
||||
'([^/]+)',
|
||||
'([0-9]+)',
|
||||
'(.+?)',
|
||||
'([^/]+)',
|
||||
'([^/]+)',
|
||||
'(.+)'
|
||||
);
|
||||
|
||||
$queryreplace =
|
||||
array (
|
||||
'year=',
|
||||
'monthnum=',
|
||||
'day=',
|
||||
'hour=',
|
||||
'minute=',
|
||||
'second=',
|
||||
'name=',
|
||||
'p=',
|
||||
'category_name=',
|
||||
'author_name=',
|
||||
'pagename=',
|
||||
's='
|
||||
);
|
||||
|
||||
$feedregex2 = '(feed|rdf|rss|rss2|atom)/?$';
|
||||
$feedregex = 'feed/' . $feedregex2;
|
||||
|
||||
$trackbackregex = 'trackback/?$';
|
||||
$pageregex = 'page/?([0-9]{1,})/?$';
|
||||
|
||||
$front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
|
||||
preg_match_all('/%.+?%/', $permalink_structure, $tokens);
|
||||
|
||||
$num_tokens = count($tokens[0]);
|
||||
|
||||
$index = 'index.php';
|
||||
$feedindex = $index;
|
||||
$trackbackindex = $index;
|
||||
for ($i = 0; $i < $num_tokens; ++$i) {
|
||||
if (0 < $i) {
|
||||
$queries[$i] = $queries[$i - 1] . '&';
|
||||
}
|
||||
|
||||
$query_token = str_replace($rewritecode, $queryreplace, $tokens[0][$i]) . preg_index($i+1, $matches);
|
||||
$queries[$i] .= $query_token;
|
||||
}
|
||||
|
||||
$structure = $permalink_structure;
|
||||
if ($front != '/') {
|
||||
$structure = str_replace($front, '', $structure);
|
||||
}
|
||||
$structure = trim($structure, '/');
|
||||
$dirs = explode('/', $structure);
|
||||
$num_dirs = count($dirs);
|
||||
|
||||
$front = preg_replace('|^/+|', '', $front);
|
||||
|
||||
$post_rewrite = array();
|
||||
$struct = $front;
|
||||
for ($j = 0; $j < $num_dirs; ++$j) {
|
||||
$struct .= $dirs[$j] . '/';
|
||||
$struct = ltrim($struct, '/');
|
||||
$match = str_replace($rewritecode, $rewritereplace, $struct);
|
||||
$num_toks = preg_match_all('/%.+?%/', $struct, $toks);
|
||||
$query = $queries[$num_toks - 1];
|
||||
|
||||
$pagematch = $match . $pageregex;
|
||||
$pagequery = $index . '?' . $query . '&paged=' . preg_index($num_toks + 1, $matches);
|
||||
|
||||
$feedmatch = $match . $feedregex;
|
||||
$feedquery = $feedindex . '?' . $query . '&feed=' . preg_index($num_toks + 1, $matches);
|
||||
|
||||
$feedmatch2 = $match . $feedregex2;
|
||||
$feedquery2 = $feedindex . '?' . $query . '&feed=' . preg_index($num_toks + 1, $matches);
|
||||
|
||||
if ($forcomments) {
|
||||
$feedquery .= '&withcomments=1';
|
||||
$feedquery2 .= '&withcomments=1';
|
||||
}
|
||||
|
||||
$rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2, $pagematch => $pagequery);
|
||||
|
||||
if ($num_toks) {
|
||||
$post = 0;
|
||||
if (strstr($struct, '%postname%') || strstr($struct, '%post_id%')
|
||||
|| (strstr($struct, '%year%') && strstr($struct, '%monthnum%') && strstr($struct, '%day%') && strstr($struct, '%hour%') && strstr($struct, '%minute') && strstr($struct, '%second%'))) {
|
||||
$post = 1;
|
||||
$trackbackmatch = $match . $trackbackregex;
|
||||
$trackbackquery = $trackbackindex . '?' . $query . '&tb=1';
|
||||
$match = $match . '?([0-9]+)?/?$';
|
||||
$query = $index . '?' . $query . '&page=' . preg_index($num_toks + 1, $matches);
|
||||
} else {
|
||||
$match .= '?$';
|
||||
$query = $index . '?' . $query;
|
||||
}
|
||||
|
||||
$rewrite = $rewrite + array($match => $query);
|
||||
|
||||
if ($post) {
|
||||
$rewrite = array($trackbackmatch => $trackbackquery) + $rewrite;
|
||||
}
|
||||
}
|
||||
|
||||
$post_rewrite = $rewrite + $post_rewrite;
|
||||
}
|
||||
|
||||
return $post_rewrite;
|
||||
}
|
||||
|
||||
/* rewrite_rules
|
||||
* Construct rewrite matches and queries from permalink structure.
|
||||
* matches - The name of the match array to use in the query strings.
|
||||
* If empty, $1, $2, $3, etc. are used.
|
||||
* Returns an associate array of matches and queries.
|
||||
*/
|
||||
function rewrite_rules($matches = '', $permalink_structure = '') {
|
||||
$rewrite = array();
|
||||
|
||||
if (empty($permalink_structure)) {
|
||||
$permalink_structure = get_settings('permalink_structure');
|
||||
|
||||
if (empty($permalink_structure)) {
|
||||
return $rewrite;
|
||||
}
|
||||
}
|
||||
|
||||
$front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
|
||||
$index = 'index.php';
|
||||
$prefix = '';
|
||||
if (using_index_permalinks($permalink_structure)) {
|
||||
$prefix = $index . '/';
|
||||
}
|
||||
|
||||
// Post
|
||||
$post_rewrite = generate_rewrite_rules($permalink_structure, $matches);
|
||||
|
||||
// Date
|
||||
$date_rewrite = generate_rewrite_rules(get_date_permastruct($permalink_structure), $matches);
|
||||
|
||||
// Root
|
||||
$root_rewrite = generate_rewrite_rules($prefix . '/', $matches);
|
||||
|
||||
// Comments
|
||||
$comments_rewrite = generate_rewrite_rules($prefix . 'comments', $matches, true);
|
||||
|
||||
// Search
|
||||
$search_structure = $prefix . "search/%search%";
|
||||
$search_rewrite = generate_rewrite_rules($search_structure, $matches);
|
||||
|
||||
// Categories
|
||||
if ( '' == get_settings('category_base') )
|
||||
$category_structure = $front . 'category/';
|
||||
else
|
||||
$category_structure = get_settings('category_base') . '/';
|
||||
|
||||
$category_structure = $category_structure . '%category%';
|
||||
$category_rewrite = generate_rewrite_rules($category_structure, $matches);
|
||||
|
||||
// Authors
|
||||
$author_structure = $front . 'author/%author%';
|
||||
$author_rewrite = generate_rewrite_rules($author_structure, $matches);
|
||||
|
||||
// Pages
|
||||
$page_rewrite = page_rewrite_rules();
|
||||
|
||||
// Deprecated style static pages
|
||||
$page_structure = $prefix . 'site/%pagename%';
|
||||
$old_page_rewrite = generate_rewrite_rules($page_structure, $matches);
|
||||
|
||||
// Put them together.
|
||||
$rewrite = $page_rewrite + $root_rewrite + $comments_rewrite + $old_page_rewrite + $search_rewrite + $category_rewrite + $author_rewrite + $date_rewrite + $post_rewrite;
|
||||
|
||||
$rewrite = apply_filters('rewrite_rules_array', $rewrite);
|
||||
return $rewrite;
|
||||
}
|
||||
|
||||
function mod_rewrite_rules ($permalink_structure) {
|
||||
$site_root = str_replace('http://', '', trim(get_settings('siteurl')));
|
||||
$site_root = preg_replace('|([^/]*)(.*)|i', '$2', $site_root);
|
||||
if ('/' != substr($site_root, -1)) $site_root = $site_root . '/';
|
||||
|
||||
$home_root = str_replace('http://', '', trim(get_settings('home')));
|
||||
$home_root = preg_replace('|([^/]*)(.*)|i', '$2', $home_root);
|
||||
if ('/' != substr($home_root, -1)) $home_root = $home_root . '/';
|
||||
|
||||
$rules = "<IfModule mod_rewrite.c>\n";
|
||||
$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 == '(.+)/?$' || $match == '([^/]+)/?$' ) {
|
||||
//nada.
|
||||
}
|
||||
|
||||
if (strstr($query, 'index.php')) {
|
||||
$rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
|
||||
} else {
|
||||
$rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n";
|
||||
}
|
||||
}
|
||||
$rules .= "</IfModule>\n";
|
||||
|
||||
$rules = apply_filters('rewrite_rules', $rules);
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
function get_posts($args) {
|
||||
global $wpdb;
|
||||
parse_str($args, $r);
|
||||
|
|
|
@ -25,28 +25,22 @@ function get_the_category($id = false) {
|
|||
}
|
||||
|
||||
function get_category_link($echo = false, $category_id, $category_nicename) {
|
||||
global $wpdb, $post, $querystring_start, $querystring_equal, $cache_categories;
|
||||
global $wpdb, $wp_rewrite, $post, $querystring_start, $querystring_equal, $cache_categories;
|
||||
$cat_ID = $category_id;
|
||||
$permalink_structure = get_settings('permalink_structure');
|
||||
$catlink = $wp_rewrite->get_category_permastruct();
|
||||
|
||||
if ('' == $permalink_structure) {
|
||||
if (empty($catlink)) {
|
||||
$file = get_settings('home') . '/' . get_settings('blogfilename');
|
||||
$link = $file.$querystring_start.'cat'.$querystring_equal.$cat_ID;
|
||||
$catlink = $file.$querystring_start.'cat'.$querystring_equal.$cat_ID;
|
||||
} else {
|
||||
$category_nicename = $cache_categories[$category_id]->category_nicename;
|
||||
// Get any static stuff from the front
|
||||
$front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
|
||||
if ( '' == get_settings('category_base') ) :
|
||||
$link = get_settings('home') . $front . 'category/';
|
||||
else :
|
||||
$link = get_settings('home') . get_settings('category_base') . '/';
|
||||
endif;
|
||||
if ($parent=$cache_categories[$category_id]->category_parent) $link .= get_category_parents($parent, FALSE, '/', TRUE);
|
||||
$link .= $category_nicename . '/';
|
||||
}
|
||||
$category_nicename = $cache_categories[$category_id]->category_nicename;
|
||||
if ($parent=$cache_categories[$category_id]->category_parent) $category_nicename = get_category_parents($parent, FALSE, '/', TRUE) . $category_nicename . '/';
|
||||
|
||||
if ($echo) echo $link;
|
||||
return $link;
|
||||
$catlink = str_replace('%category%', $category_nicename, $catlink);
|
||||
$catlink = get_settings('home') . trailingslashit($catlink);
|
||||
}
|
||||
if ($echo) echo $catlink;
|
||||
return $catlink;
|
||||
}
|
||||
|
||||
function get_category_rss_link($echo = false, $category_id, $category_nicename) {
|
||||
|
|
|
@ -84,7 +84,7 @@ function get_permalink($id = false) {
|
|||
}
|
||||
|
||||
function get_page_link($id = false) {
|
||||
global $post;
|
||||
global $post, $wp_rewrite;
|
||||
|
||||
if (! $id) {
|
||||
$id = $post->ID;
|
||||
|
@ -94,7 +94,7 @@ function get_page_link($id = false) {
|
|||
|
||||
if ('' != $permalink) {
|
||||
$link = get_page_uri($id);
|
||||
if (using_index_permalinks()) {
|
||||
if ($wp_rewrite->using_index_permalinks()) {
|
||||
$link = 'index.php/' . $link;
|
||||
}
|
||||
$link = get_settings('home') . "/$link/";
|
||||
|
@ -106,49 +106,50 @@ function get_page_link($id = false) {
|
|||
}
|
||||
|
||||
function get_year_link($year) {
|
||||
global $querystring_start, $querystring_equal;
|
||||
global $querystring_start, $querystring_equal, $wp_rewrite;
|
||||
if (!$year) $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
|
||||
$yearlink = get_year_permastruct();
|
||||
$yearlink = $wp_rewrite->get_year_permastruct();
|
||||
if (!empty($yearlink)) {
|
||||
$yearlink = str_replace('%year%', $year, $yearlink);
|
||||
return get_settings('home') . $yearlink;
|
||||
return get_settings('home') . trailingslashit($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;
|
||||
global $querystring_start, $querystring_equal, $wp_rewrite;
|
||||
if (!$year) $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
|
||||
if (!$month) $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
|
||||
$monthlink = get_month_permastruct();
|
||||
$monthlink = $wp_rewrite->get_month_permastruct();
|
||||
if (!empty($monthlink)) {
|
||||
$monthlink = str_replace('%year%', $year, $monthlink);
|
||||
$monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
|
||||
return get_settings('home') . $monthlink;
|
||||
return get_settings('home') . trailingslashit($monthlink);
|
||||
} else {
|
||||
return get_settings('home') .'/'. get_settings('blogfilename') .$querystring_start.'m'.$querystring_equal.$year.zeroise($month, 2);
|
||||
}
|
||||
}
|
||||
|
||||
function get_day_link($year, $month, $day) {
|
||||
global $querystring_start, $querystring_equal;
|
||||
global $querystring_start, $querystring_equal, $wp_rewrite;
|
||||
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));
|
||||
|
||||
$daylink = get_day_permastruct();
|
||||
$daylink = $wp_rewrite->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);
|
||||
return get_settings('home') . $daylink;
|
||||
return get_settings('home') . trailingslashit($daylink);
|
||||
} else {
|
||||
return get_settings('home') .'/'. get_settings('blogfilename') .$querystring_start.'m'.$querystring_equal.$year.zeroise($month, 2).zeroise($day, 2);
|
||||
}
|
||||
}
|
||||
|
||||
function get_feed_link($feed='rss2') {
|
||||
global $wp_rewrite;
|
||||
$do_perma = 0;
|
||||
$feed_url = get_settings('siteurl');
|
||||
$comment_feed_url = $feed_url;
|
||||
|
@ -159,7 +160,7 @@ function get_feed_link($feed='rss2') {
|
|||
$feed_url = get_settings('home');
|
||||
$index = 'index.php';
|
||||
$prefix = '';
|
||||
if (using_index_permalinks()) {
|
||||
if ($wp_rewrite->using_index_permalinks()) {
|
||||
$feed_url .= '/' . $index;
|
||||
}
|
||||
|
||||
|
@ -416,6 +417,8 @@ function next_post($format='%', $next='next post: ', $title='yes', $in_same_cat=
|
|||
}
|
||||
|
||||
function get_pagenum_link($pagenum = 1){
|
||||
global $wp_rewrite;
|
||||
|
||||
$qstr = $_SERVER['REQUEST_URI'];
|
||||
|
||||
$page_querystring = "paged";
|
||||
|
@ -453,7 +456,7 @@ function get_pagenum_link($pagenum = 1){
|
|||
$permalink = 1;
|
||||
|
||||
// If it's not a path info permalink structure, trim the index.
|
||||
if (! using_index_permalinks()) {
|
||||
if (! $wp_rewrite->using_index_permalinks()) {
|
||||
$qstr = preg_replace("#/*" . $index . "/*#", '/', $qstr);
|
||||
} else {
|
||||
// If using path info style permalinks, make sure the index is in
|
||||
|
|
Loading…
Reference in New Issue