<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[19743] trunk/wp-includes/rewrite.php: Upgrade add_permastruct() to allow more control over WP_Rewrite::generate_rewrite_rules().</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/19743">19743</a></dd>
<dt>Author</dt> <dd>duck_</dd>
<dt>Date</dt> <dd>2012-01-24 18:27:18 +0000 (Tue, 24 Jan 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Upgrade add_permastruct() to allow more control over WP_Rewrite::generate_rewrite_rules(). See <a href="http://core.trac.wordpress.org/ticket/16092">#16092</a>.

The third argument is now a configuration array that mirrors the parameters of generate_rewrite_rules()
and allows for add_permastruct() specific args (i.e. with_front). The full configuration is stored in
WP_Rewrite::$extra_permastructs to be used by WP_Rewrite::rewrite_rules().</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesrewritephp">trunk/wp-includes/rewrite.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesrewritephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/rewrite.php (19742 => 19743)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/rewrite.php        2012-01-23 20:10:01 UTC (rev 19742)
+++ trunk/wp-includes/rewrite.php        2012-01-24 18:27:18 UTC (rev 19743)
</span><span class="lines">@@ -52,11 +52,19 @@
</span><span class="cx">  *
</span><span class="cx">  * @param string $name Name for permalink structure.
</span><span class="cx">  * @param string $struct Permalink structure.
</span><del>- * @param bool $with_front Prepend front base to permalink structure.
</del><ins>+ * @param array $args Optional configuration for building the rules from the permalink structure,
+ *     see {@link WP_Rewrite::add_permastruct()} for full details.
</ins><span class="cx">  */
</span><del>-function add_permastruct( $name, $struct, $with_front = true, $ep_mask = EP_NONE ) {
</del><ins>+function add_permastruct( $name, $struct, $args = array() ) {
</ins><span class="cx">         global $wp_rewrite;
</span><del>-        return $wp_rewrite-&gt;add_permastruct( $name, $struct, $with_front, $ep_mask );
</del><ins>+
+        // backwards compatibility for the old parameters: $with_front and $ep_mask
+        if ( ! is_array( $args ) )
+                $args = array( 'with_front' =&gt; $args );
+        if ( func_num_args() == 4 )
+                $args['ep_mask'] = func_get_arg( 3 );
+
+        return $wp_rewrite-&gt;add_permastruct( $name, $struct, $args );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -993,7 +1001,7 @@
</span><span class="cx">                         return false;
</span><span class="cx"> 
</span><span class="cx">                 if ( isset($this-&gt;extra_permastructs[$name]) )
</span><del>-                        return $this-&gt;extra_permastructs[$name][0];
</del><ins>+                        return $this-&gt;extra_permastructs[$name]['struct'];
</ins><span class="cx"> 
</span><span class="cx">                 return false;
</span><span class="cx">         }
</span><span class="lines">@@ -1518,11 +1526,15 @@
</span><span class="cx">                 $page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite);
</span><span class="cx"> 
</span><span class="cx">                 // Extra permastructs
</span><del>-                foreach ( $this-&gt;extra_permastructs as $permastructname =&gt; $permastruct ) {
-                        if ( is_array($permastruct) )
-                                $rules = $this-&gt;generate_rewrite_rules($permastruct[0], $permastruct[1]);
-                        else
-                                $rules = $this-&gt;generate_rewrite_rules($permastruct, EP_NONE);
</del><ins>+                foreach ( $this-&gt;extra_permastructs as $permastructname =&gt; $struct ) {
+                        if ( is_array( $struct ) ) {
+                                if ( count( $struct ) == 2 )
+                                        $rules = $this-&gt;generate_rewrite_rules( $struct[0], $struct[1] );
+                                else
+                                        $rules = $this-&gt;generate_rewrite_rules( $struct['struct'], $struct['ep_mask'], $struct['paged'], $struct['feed'], $struct['forcomments'], $struct['walk_dirs'], $struct['endpoints'] );
+                        } else {
+                                $rules = $this-&gt;generate_rewrite_rules( $struct );
+                        }
</ins><span class="cx"> 
</span><span class="cx">                         $rules = apply_filters($permastructname . '_rewrite_rules', $rules);
</span><span class="cx">                         if ( 'post_tag' == $permastructname )
</span><span class="lines">@@ -1817,24 +1829,58 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><del>-         * Add permalink structure.
</del><ins>+         * Add a new permalink structure.
</ins><span class="cx">          *
</span><del>-         * These are added along with the extra rewrite rules that are merged to the
-         * top.
</del><ins>+         * A permalink structure (permastruct) is an abstract definition of a set of rewrite rules; it
+         * is an easy way of expressing a set of regular expressions that rewrite to a set of query strings.
+         * The new permastruct is added to the {@link WP_Rewrite::$extra_permastructs} array. When the
+         * rewrite rules are built by {@link WP_Rewrite::rewrite_rules()} all of these extra permastructs
+         * are passed to {@link WP_Rewrite::generate_rewrite_rules()} which transforms them into the
+         * regular expressions that many love to hate.
</ins><span class="cx">          *
</span><ins>+         * The $args parameter gives you control over how {@link WP_Rewrite::generate_rewrite_rules()}
+         * works on the new permastruct.
+         *
</ins><span class="cx">          * @since 2.5.0
</span><span class="cx">          * @access public
</span><span class="cx">          *
</span><span class="cx">          * @param string $name Name for permalink structure.
</span><del>-         * @param string $struct Permalink structure.
-         * @param bool $with_front Prepend front base to permalink structure.
</del><ins>+         * @param string $struct Permalink structure (e.g. category/%category%)
+         * @param array $args Optional configuration for building the rules from the permalink structure:
+         *     - with_front (bool) - Should the structure be prepended with WP_Rewrite::$front? Default is true.
+         *     - ep_mask (int) - Endpoint mask defining what endpoints are added to the structure. Default is EP_NONE.
+         *     - paged (bool) - Should archive pagination rules be added for the structure? Default is true.
+         *     - feed (bool) - Should feed rewrite rules be added for the structure? Default is true.
+         *     - forcomments (bool) - Should the feed rules be a query for a comments feed? Default is false.
+         *     - walk_dirs (bool) - Should the 'directories' making up the structure be walked over and rewrite
+         *                          rules built for each in turn? Default is true.
+         *     - endpoints (bool) - Should endpoints be applied to the generated rewrite rules? Default is true.
</ins><span class="cx">          */
</span><del>-        function add_permastruct($name, $struct, $with_front = true, $ep_mask = EP_NONE) {
-                if ( $with_front )
</del><ins>+        function add_permastruct( $name, $struct, $args = array() ) {
+                // backwards compatibility for the old parameters: $with_front and $ep_mask
+                if ( ! is_array( $args ) )
+                        $args = array( 'with_front' =&gt; $args );
+                if ( func_num_args() == 4 )
+                        $args['ep_mask'] = func_get_arg( 3 );
+
+                $defaults = array(
+                        'with_front' =&gt; true,
+                        'ep_mask' =&gt; EP_NONE,
+                        'paged' =&gt; true,
+                        'feed' =&gt; true,
+                        'forcomments' =&gt; false,
+                        'walk_dirs' =&gt; true,
+                        'endpoints' =&gt; true,
+                );
+                $args = wp_parse_args( $args, $defaults );
+
+                if ( $args['with_front'] )
</ins><span class="cx">                         $struct = $this-&gt;front . $struct;
</span><span class="cx">                 else
</span><span class="cx">                         $struct = $this-&gt;root . $struct;
</span><del>-                $this-&gt;extra_permastructs[$name] = array($struct, $ep_mask);
</del><ins>+                $args['struct'] = $struct;
+
+                $this-&gt;extra_permastructs[ $name ] = $args;
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span></span></pre>
</div>
</div>

</body>
</html>