[wp-cvs] wordpress/wp-includes classes.php, 1.26, 1.27 functions.php, 1.217, 1.218 template-functions-category.php, 1.41, 1.42 template-functions-links.php, 1.44, 1.45

Ryan Boren rboren at users.sourceforge.net
Fri Dec 3 02:38:13 UTC 2004


Update of /cvsroot/cafelog/wordpress/wp-includes
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16685/wp-includes

Modified Files:
	classes.php functions.php template-functions-category.php 
	template-functions-links.php 
Log Message:
Move rewrite and permalink functions into WP_Rewrite class.

Index: template-functions-links.php
===================================================================
RCS file: /cvsroot/cafelog/wordpress/wp-includes/template-functions-links.php,v
retrieving revision 1.44
retrieving revision 1.45
diff -C2 -d -r1.44 -r1.45
*** template-functions-links.php	19 Nov 2004 20:54:16 -0000	1.44
--- template-functions-links.php	3 Dec 2004 02:38:11 -0000	1.45
***************
*** 85,89 ****
  
  function get_page_link($id = false) {
! 	global $post;
  
  	if (! $id) {
--- 85,89 ----
  
  function get_page_link($id = false) {
! 	global $post, $wp_rewrite;
  
  	if (! $id) {
***************
*** 95,99 ****
  	if ('' != $permalink) {
  		$link = get_page_uri($id);
! 		if (using_index_permalinks()) {
  			$link = 'index.php/' . $link;
  		}
--- 95,99 ----
  	if ('' != $permalink) {
  		$link = get_page_uri($id);
! 		if ($wp_rewrite->using_index_permalinks()) {
  			$link = 'index.php/' . $link;
  		}
***************
*** 107,116 ****
  
  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;
--- 107,116 ----
  
  function get_year_link($year) {
! 	global $querystring_start, $querystring_equal, $wp_rewrite;
      if (!$year) $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
! 		$yearlink = $wp_rewrite->get_year_permastruct();
      if (!empty($yearlink)) {
          $yearlink = str_replace('%year%', $year, $yearlink);
!         return get_settings('home') . trailingslashit($yearlink);
      } else {
          return get_settings('home') .'/'. get_settings('blogfilename') .$querystring_start.'m'.$querystring_equal.$year;
***************
*** 119,130 ****
  
  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));
! 		$monthlink = 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;
      } else {
          return get_settings('home') .'/'. get_settings('blogfilename') .$querystring_start.'m'.$querystring_equal.$year.zeroise($month, 2);
--- 119,130 ----
  
  function get_month_link($year, $month) {
!     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 = $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') . trailingslashit($monthlink);
      } else {
          return get_settings('home') .'/'. get_settings('blogfilename') .$querystring_start.'m'.$querystring_equal.$year.zeroise($month, 2);
***************
*** 133,147 ****
  
  function get_day_link($year, $month, $day) {
!     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 (!$day) $day = gmdate('j', time()+(get_settings('gmt_offset') * 3600));
  
! 		$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);
!         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);
--- 133,147 ----
  
  function get_day_link($year, $month, $day) {
!     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 = $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') . trailingslashit($daylink);
      } else {
          return get_settings('home') .'/'. get_settings('blogfilename') .$querystring_start.'m'.$querystring_equal.$year.zeroise($month, 2).zeroise($day, 2);
***************
*** 150,153 ****
--- 150,154 ----
  
  function get_feed_link($feed='rss2') {
+ 	global $wp_rewrite;
      $do_perma = 0;
      $feed_url = get_settings('siteurl');
***************
*** 160,164 ****
          $index = 'index.php';
          $prefix = '';
!         if (using_index_permalinks()) {
              $feed_url .= '/' . $index;
          }
--- 161,165 ----
          $index = 'index.php';
          $prefix = '';
!         if ($wp_rewrite->using_index_permalinks()) {
              $feed_url .= '/' . $index;
          }
***************
*** 417,420 ****
--- 418,423 ----
  
  function get_pagenum_link($pagenum = 1){
+ 	global $wp_rewrite;
+ 
     $qstr = $_SERVER['REQUEST_URI'];
  
***************
*** 454,458 ****
  
  	 // If it's not a path info permalink structure, trim the index.
! 	 if (! using_index_permalinks()) {
  	   $qstr = preg_replace("#/*" . $index . "/*#", '/', $qstr);
  	 } else {
--- 457,461 ----
  
  	 // If it's not a path info permalink structure, trim the index.
! 	 if (! $wp_rewrite->using_index_permalinks()) {
  	   $qstr = preg_replace("#/*" . $index . "/*#", '/', $qstr);
  	 } else {

Index: functions.php
===================================================================
RCS file: /cvsroot/cafelog/wordpress/wp-includes/functions.php,v
retrieving revision 1.217
retrieving revision 1.218
diff -C2 -d -r1.217 -r1.218
*** functions.php	28 Nov 2004 19:53:11 -0000	1.217
--- functions.php	3 Dec 2004 02:38:11 -0000	1.218
***************
*** 1306,1339 ****
  }
  
- 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;
--- 1306,1309 ----
***************
*** 1355,1677 ****
  }
  
- 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;
--- 1325,1328 ----

Index: classes.php
===================================================================
RCS file: /cvsroot/cafelog/wordpress/wp-includes/classes.php,v
retrieving revision 1.26
retrieving revision 1.27
diff -C2 -d -r1.26 -r1.27
*** classes.php	1 Dec 2004 06:52:26 -0000	1.26
--- classes.php	3 Dec 2004 02:38:11 -0000	1.27
***************
*** 699,701 ****
--- 699,1097 ----
  }
  
+ 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();
+ }
+ 
  ?>
\ No newline at end of file

Index: template-functions-category.php
===================================================================
RCS file: /cvsroot/cafelog/wordpress/wp-includes/template-functions-category.php,v
retrieving revision 1.41
retrieving revision 1.42
diff -C2 -d -r1.41 -r1.42
*** template-functions-category.php	15 Nov 2004 06:00:21 -0000	1.41
--- template-functions-category.php	3 Dec 2004 02:38:11 -0000	1.42
***************
*** 26,51 ****
  
  function get_category_link($echo = false, $category_id, $category_nicename) {
!     global $wpdb, $post, $querystring_start, $querystring_equal, $cache_categories;
      $cat_ID = $category_id;
!     $permalink_structure = get_settings('permalink_structure');
      
!     if ('' == $permalink_structure) {
          $file = get_settings('home') . '/' . get_settings('blogfilename');
!         $link = $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 . '/';
!     }
  
!     if ($echo) echo $link;
!     return $link;
  }
  
--- 26,45 ----
  
  function get_category_link($echo = false, $category_id, $category_nicename) {
! 	global $wpdb, $wp_rewrite, $post, $querystring_start, $querystring_equal, $cache_categories;
      $cat_ID = $category_id;
!     $catlink = $wp_rewrite->get_category_permastruct();
      
!     if (empty($catlink)) {
          $file = get_settings('home') . '/' . get_settings('blogfilename');
!         $catlink = $file.$querystring_start.'cat'.$querystring_equal.$cat_ID;
      } else {
! 			$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 . '/';
  
! 			$catlink = str_replace('%category%', $category_nicename, $catlink);
! 			$catlink = get_settings('home') . trailingslashit($catlink);
!     }
!     if ($echo) echo $catlink;
!     return $catlink;
  }
  




More information about the cvs mailing list