[wp-hackers] Make WP_Rewrite eaiser to use

Peter Westwood peter.westwood at ftwr.co.uk
Sun Feb 5 20:56:28 GMT 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Owen Winkler wrote:
> David House wrote:
>> Reading through the recent thread concerning Scott Merril's troubles
>> with WP_Rewrite [1], I thought that we really should automate a lot of
>> what is in the final plugin [2]. People (well, Owen) have been asking
>> for a while that WP_Rewrite should be easier to use. I'm not proposing
>> a full rewrite of WP_Rewrite, but I do think we should provide more
>> API to help the mortal plugin authors understand it.
> 
> Well, a full rewrite of WP_Rewrite seems impractical, but the code that
> is there is nigh incomprehensible.  I think much of the problem with
> adding new rewrite rules is that it is very difficult to look at the
> code, watch execution through it, and locate the best place to insert
> anything new.
> 
> Perhaps adding a couple of extra API hooks for making rewriting easier
> would be beneficial to the many plugin developers who have recently
> expressed an interest in adding their own rules.  This doesn't solve the
> more fundamental problem that the extant code is hard to read and, by
> extension, maintain.
> 

I think a rewrite of WP_Rewrite should be possible.

I am getting to know it quite well at the moment trying to put together
a patch for this old trac ticket - http://trac.wordpress.org/ticket/301
(current inprogress patch attached)

I was considering looking at refactoring the code after I had this finished.

I have nearly working code which allows configuration of:

Author, Search, Comments and Feed permalinks. Getting the feed ones
working is somewhat of a pain but I am nearly there.

If we are to work on WP_Rewrite adding an api for registering generic
endpoint types looks like a good idea.  This would be some way of
registering the query var, permalink string and input/output handler
this could then in theory be used by both the core code and plugins for
things like feeds, trackbacks, ajax endpoints etc.

What do people think of this.

It would probably simplfy WP_Rewrite if it is made generic - it contains
a lot of duplicate code at present and also aid in the removal of some
of the feed types from the core to plugins and ideally the registration
functions described above should allow you to override someone elses
handler - this could have been used for things like the atom feeds where
the builtin  atom 0.3 feed could have easily been replaced by a plugin.

westi
- --
Peter Westwood
http://blog.ftwr.co.uk
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFD5mZ8VPRdzag0AcURAhGRAKCF+fmW9TcKu3oU4MhZLDmMRSCOVACdEa2r
HhD/fmTnFQ7GG25tJjr3IaI=
=nqDP
-----END PGP SIGNATURE-----
-------------- next part --------------
Index: wp-includes/classes.php
===================================================================
--- wp-includes/classes.php	(revision 3504)
+++ wp-includes/classes.php	(working copy)
@@ -862,14 +862,14 @@
 	var $permalink_structure;
 	var $category_base;
 	var $category_structure;
-	var $author_base = 'author';
+	var $author_base = 'writer';
 	var $author_structure;
 	var $date_structure;
 	var $page_structure;
-	var $search_base = 'search';
+	var $search_base = 'find';
 	var $search_structure;
-	var $comments_base = 'comments';
-	var $feed_base = 'feed';
+	var $comments_base = 'discussion';
+	var $feed_base = 'syndicate';
 	var $comments_feed_structure;
 	var $feed_structure;
 	var $front;
@@ -1095,8 +1095,13 @@
 			return false;
 		}
 
-		$this->author_structure = $this->front . $this->author_base . '/%author%';
+		if (empty($this->author_base))
+			$this->author_structure = $this->front . 'author/';
+		else
+			$this->author_structure = $this->front . $this->author_base . '/';
 
+		$this->author_structure .= '%author%';
+
 		return $this->author_structure;
 	}
 
@@ -1110,7 +1115,12 @@
 			return false;
 		}
 
-		$this->search_structure = $this->root . $this->search_base . '/%search%';
+		if (empty($this->search_base))
+			$this->search_structure = $this->front . 'search/';
+		else
+			$this->search_structure = $this->search_base . '/';
+		
+		$this->search_structure .= '%search%';
 
 		return $this->search_structure;
 	}
@@ -1140,7 +1150,12 @@
 			return false;
 		}
 
-		$this->feed_structure = $this->root . $this->feed_base . '/%feed%';
+		if (empty($this->feed_base))
+			$this->feed_structure = $this->front . 'feed/';
+		else
+			$this->feed_structure = $this->front . $this->feed_base . '/';
+		
+		$this->feed_structure .= '%feed%';
 
 		return $this->feed_structure;
 	}
@@ -1155,8 +1170,19 @@
 			return false;
 		}
 
-		$this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%';
 
+		if (empty($this->comments_base))
+			$this->comment_feed_structure = $this->front . 'comments/';
+		else
+			$this->comment_feed_structure = $this->front . $this->comments_base . '/';
+
+		if (empty($this->feed_base))
+			$this->comment_feed_structure .= 'feed/';
+		else
+			$this->comment_feed_structure .= $this->feed_base . '/';
+		
+		$this->comment_feed_structure .= '%feed%';
+
 		return $this->comment_feed_structure;
 	}
 
@@ -1178,10 +1204,10 @@
 	function generate_rewrite_rules($permalink_structure, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true) {
 		$feedregex2 = '';
 		foreach ($this->feeds as $feed_name) {
-			$feedregex2 .= $feed_name . '|';
+			$feedregex2 .= '/' . $feed_name . '|';
 		}
-		$feedregex2 = '(' . trim($feedregex2, '|') .  ')/?$';
-		$feedregex = $this->feed_base  . '/' . $feedregex2;
+		$feedregex2 = '(|' . trim($feedregex2, '|') .  ')/?$';
+		$feedregex = $this->feed_base . $feedregex2;
 
 		$trackbackregex = 'trackback/?$';
 		$pageregex = 'page/?([0-9]{1,})/?$';
@@ -1485,6 +1511,8 @@
 		// Fetch the rewrite rules.
 		$rewrite = $wp_rewrite->wp_rewrite_rules();
 
+		print_r($rewrite);
+
 		if (! empty($rewrite)) {
 			// If we match a rewrite rule, this will be cleared.
 			$error = '404';
@@ -1538,12 +1566,16 @@
 				if ((! empty($req_uri)) && (strpos($match, $req_uri) === 0) && ($req_uri != $request)) {
 					$request_match = $req_uri . '/' . $request;
 				}
+				
+				echo "$match : $request_match \n";
 
 				if (preg_match("!^$match!", $request_match, $matches) ||
 					preg_match("!^$match!", urldecode($request_match), $matches)) {
 					// Got a match.
 					$this->matched_rule = $match;
 
+					print_r($matches);
+
 					// Trim the query of everything up to the '?'.
 					$query = preg_replace("!^.+\?!", '', $query);
 
@@ -1601,6 +1633,8 @@
 
 		if ( isset($error) )
 			$this->query_vars['error'] = $error;
+
+		print_r($this->query_vars);
 	}
 
 	function send_headers() {
Index: wp-admin/options-permalink.php
===================================================================
--- wp-admin/options-permalink.php	(revision 3504)
+++ wp-admin/options-permalink.php	(working copy)
@@ -75,6 +75,10 @@
 	
 $permalink_structure = get_settings('permalink_structure');
 $category_base = get_settings('category_base');
+$author_base = get_settings('author_base');
+$search_base = get_settings('search_base');
+$comments_base = get_settings('comments_base');
+$feed_base = get_settings('feed_base');
 
 if ( (!file_exists($home_path.'.htaccess') && is_writable($home_path)) || is_writable($home_path.'.htaccess') )
 	$writable = true;
@@ -155,7 +159,24 @@
 <?php endif; ?>
 	<p> 
   <?php _e('Category base'); ?>: <input name="category_base" type="text" class="code"  value="<?php echo $category_base; ?>" size="30" /> 
-     </p> 
+     </p>
+<?php if ($is_apache) : ?>
+	<p><?php _e('If you like, you may enter a custom prefix for some of your other URIs here including Author, Search, Comments and Feed. For example, <code>/discussion/</code> could be used for your comments to make your links like <code>http://example.org/1980/01/01/my-post/discussion/</code>. If you leave this blank the default will be used.') ?></p>
+<?php else : ?>
+	<p><?php _e('If you like, you may enter a custom prefix for some of your other URIs here including Author, Search, Comments and Feed. For example, <code>/discussion/</code> could be used for your comments to make your links like <code>http://example.org/index.php/1980/01/01/my-post/discussion/</code>. If you leave this blank the default will be used.') ?></p>
+<?php endif; ?>
+	<p> 
+  <?php _e('Author base'); ?>: <input name="author_base" type="text" class="code"  value="<?php echo $author_base; ?>" size="30" /> 
+    </p>
+	<p>
+  <?php _e('Search base'); ?>: <input name="search_base" type="text" class="code"  value="<?php echo $search_base; ?>" size="30" /> 
+    </p>
+	<p>
+  <?php _e('Comments base'); ?>: <input name="comments_base" type="text" class="code"  value="<?php echo $comments_base; ?>" size="30" /> 
+    </p>
+	<p>
+  <?php _e('Feed base'); ?>: <input name="feed_base" type="text" class="code"  value="<?php echo $feed_base; ?>" size="30" /> 
+    </p>
     <p class="submit"> 
       <input type="submit" name="submit" value="<?php _e('Update Permalink Structure &raquo;') ?>" /> 
     </p> 


More information about the wp-hackers mailing list