[wp-cvs] wordpress/wp-includes functions.php,1.108,1.109
Ryan Boren
rboren at users.sourceforge.net
Mon May 31 16:34:03 CDT 2004
Update of /cvsroot/cafelog/wordpress/wp-includes
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4345/wp-includes
Modified Files:
functions.php
Log Message:
More rewrite rule refactoring. Move the permalink parser into generate_rewrite_rules() and make it handle categories and authors.
Index: functions.php
===================================================================
RCS file: /cvsroot/cafelog/wordpress/wp-includes/functions.php,v
retrieving revision 1.108
retrieving revision 1.109
diff -C2 -d -r1.108 -r1.109
*** functions.php 31 May 2004 17:22:25 -0000 1.108
--- functions.php 31 May 2004 21:34:00 -0000 1.109
***************
*** 1163,1196 ****
}
! /* 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 = '') {
!
! function preg_index($number, $matches = '') {
! $match_prefix = '$';
! $match_suffix = '';
!
! if (! empty($matches)) {
! $match_prefix = '$' . $matches . '[';
! $match_suffix = ']';
! }
!
! return "$match_prefix$number$match_suffix";
! }
! $rewrite = array();
!
! if (empty($permalink_structure)) {
! $permalink_structure = get_settings('permalink_structure');
!
! if (empty($permalink_structure)) {
! return $rewrite;
! }
! }
$rewritecode =
array(
--- 1163,1179 ----
}
! function preg_index($number, $matches = '') {
! $match_prefix = '$';
! $match_suffix = '';
! if (! empty($matches)) {
! $match_prefix = '$' . $matches . '[';
! $match_suffix = ']';
! }
!
! return "$match_prefix$number$match_suffix";
! }
+ function generate_rewrite_rules($permalink_structure = '') {
$rewritecode =
array(
***************
*** 1202,1206 ****
'%second%',
'%postname%',
! '%post_id%'
);
--- 1185,1191 ----
'%second%',
'%postname%',
! '%post_id%',
! '%category%',
! '%author%'
);
***************
*** 1214,1218 ****
'([0-9]{1,2})',
'([_0-9a-z-]+)',
! '([0-9]+)'
);
--- 1199,1205 ----
'([0-9]{1,2})',
'([_0-9a-z-]+)',
! '([0-9]+)',
! '(.*)',
! '([_0-9a-z-]+)'
);
***************
*** 1226,1230 ****
'second=',
'name=',
! 'p='
);
--- 1213,1219 ----
'second=',
'name=',
! 'p=',
! 'category_name=',
! 'author_name='
);
***************
*** 1290,1358 ****
}
// If the permalink does not have year, month, and day, we need to create a
// separate archive rule.
- // TODO: Need to write separate rules for each component of the permalink.
$doarchive = false;
if (! (strstr($permalink_structure, '%year') && strstr($permalink_structure, '%monthnum') && strstr($permalink_structure, '%day')) ) {
$doarchive = true;
! $archivematch = $front . '([0-9]{4})/?([0-9]{1,2})?/?([0-9]{1,2})?/?$';
! $archivequery = 'index.php?year=' . preg_index(1, $matches) . '&monthnum=' . preg_index(2, $matches) . '&day=' . preg_index(3, $matches) ;
}
// Site feed
$sitefeedmatch = 'feed/?([_0-9a-z-]+)?/?$';
$sitefeedquery = 'wp-feed.php?feed=' . preg_index(1, $matches);
- $sitepagematch = $pageregex;
- $sitepagequery = 'index.php?paged=' . preg_index(1, $matches);
-
// Site comment feed
$sitecommentfeedmatch = 'comments/feed/?([_0-9a-z-]+)?/?$';
$sitecommentfeedquery = 'wp-feed.php?feed=' . preg_index(1, $matches) . '&withcomments=1';
! // Code for nice categories and authors.
! if ( '' == get_settings('category_base') )
! $catmatch = $front . 'category/';
! else
! $catmatch = get_settings('category_base') . '/';
! $catmatch = preg_replace('|^/+|', '', $catmatch);
!
! $catfeedmatch = $catmatch . '(.*)/' . $feedregex;
! $catfeedquery = 'wp-feed.php?category_name=' . preg_index(1, $matches) . '&feed=' . preg_index(2, $matches);
!
! $catpagematch = $catmatch . '(.*)/' . $pageregex;
! $catpagequery = 'index.php?category_name=' . preg_index(1, $matches) . '&paged=' . preg_index(2, $matches);
!
! $catmatch = $catmatch . '?(.*)';
! $catquery = 'index.php?category_name=' . preg_index(1, $matches);
!
! $authormatch = $front . 'author/';
! $authormatch = preg_replace('|^/+|', '', $authormatch);
!
! $authorfeedmatch = $authormatch . '(.*)/' . $feedregex;
! $authorfeedquery = 'wp-feed.php?author_name=' . preg_index(1, $matches) . '&feed=' . preg_index(2, $matches);
!
! $authorpagematch = $authormatch . '(.*)/' . $pageregex;
! $authorpagequery = 'index.php?author_name=' . preg_index(1, $matches) . '&paged=' . preg_index(2, $matches);
!
! $authormatch = $authormatch . '?(.*)';
! $authorquery = 'index.php?author_name=' . preg_index(1, $matches);
! $rewrite = array(
$sitefeedmatch => $sitefeedquery,
$sitecommentfeedmatch => $sitecommentfeedquery,
$sitepagematch => $sitepagequery,
- $catfeedmatch => $catfeedquery,
- $catpagematch => $catpagequery,
- $catmatch => $catquery,
- $authorfeedmatch => $authorfeedquery,
- $authorpagematch => $authorpagequery,
- $authormatch => $authorquery
);
! $rewrite = $rewrite + $post_rewrite;
if ($doarchive) {
! $rewrite = $rewrite + array($archivematch => $archivequery);
}
--- 1279,1355 ----
}
+ 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;
+ }
+ }
+
+ $post_rewrite = generate_rewrite_rules($permalink_structure);
+
// 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')) ) {
$doarchive = true;
! $archive_structure = $front . '%year%/%monthnum%/%day%/';
! $archive_rewrite = generate_rewrite_rules($archive_structure);
}
+ $feedregex = '(feed|rdf|rss|rss2|atom)/?$';
+ $pageregex = 'page/?([0-9]{1,})/?$';
+ $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
+
// Site feed
$sitefeedmatch = 'feed/?([_0-9a-z-]+)?/?$';
$sitefeedquery = 'wp-feed.php?feed=' . preg_index(1, $matches);
// Site comment feed
$sitecommentfeedmatch = 'comments/feed/?([_0-9a-z-]+)?/?$';
$sitecommentfeedquery = 'wp-feed.php?feed=' . preg_index(1, $matches) . '&withcomments=1';
! // Site page
! $sitepagematch = $pageregex;
! $sitepagequery = 'index.php?paged=' . preg_index(1, $matches);
! $site_rewrite = array(
$sitefeedmatch => $sitefeedquery,
$sitecommentfeedmatch => $sitecommentfeedquery,
$sitepagematch => $sitepagequery,
);
! // 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);
+
+ // Authors
+ $author_structure = $front . 'author/%author%';
+ $author_rewrite = generate_rewrite_rules($author_structure);
+
+
+ // Put them together.
+ $rewrite = $site_rewrite + $category_rewrite + $author_rewrite + $post_rewrite;
+
+ // Add on archive rewrite rules if needed.
if ($doarchive) {
! $rewrite = $rewrite + $archive_rewrite;
}
More information about the cvs
mailing list