- //the main WP_Rewrite function. generate the rules from permalink structure
- function generate_rewrite_rules($permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true) {
- //build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/?
- $feedregex2 = '';
- foreach ($this->feeds as $feed_name) {
- $feedregex2 .= $feed_name . '|';
- }
- $feedregex2 = '(' . trim($feedregex2, '|') . ')/?$';
- //$feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom
- //and <permalink>/atom are both possible
- $feedregex = $this->feed_base . '/' . $feedregex2;
-
- //build a regex to match the trackback and page/xx parts of URLs
- $trackbackregex = 'trackback/?$';
- $pageregex = 'page/?([0-9]{1,})/?$';
-
- //build up an array of endpoint regexes to append => queries to append
- if ($endpoints) {
- $ep_query_append = array ();
- foreach ($this->endpoints as $endpoint) {
- //match everything after the endpoint name, but allow for nothing to appear there
- $epmatch = $endpoint[1] . '(/(.*))?/?$';
- //this will be appended on to the rest of the query for each dir
- $epquery = '&' . $endpoint[1] . '=';
- $ep_query_append[$epmatch] = array ( $endpoint[0], $epquery );
- }
- }
-
- //get everything up to the first rewrite tag
- $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
- //build an array of the tags (note that said array ends up being in $tokens[0])
- preg_match_all('/%.+?%/', $permalink_structure, $tokens);
-
- $num_tokens = count($tokens[0]);
-
- $index = $this->index; //probably 'index.php'
- $feedindex = $index;
- $trackbackindex = $index;
- //build a list from the rewritecode and queryreplace arrays, that will look something like
- //tagname=$matches[i] where i is the current $i
- 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;
- }
-
- //get the structure, minus any cruft (stuff that isn't tags) at the front
- $structure = $permalink_structure;
- if ($front != '/') {
- $structure = str_replace($front, '', $structure);
- }
- //create a list of dirs to walk over, making rewrite rules for each level
- //so for example, a $structure of /%year%/%month%/%postname% would create
- //rewrite rules for /%year%/, /%year%/%month%/ and /%year%/%month%/%postname%
- $structure = trim($structure, '/');
- if ($walk_dirs) {
- $dirs = explode('/', $structure);
- } else {
- $dirs[] = $structure;
- }
- $num_dirs = count($dirs);
-
- //strip slashes from the front of $front
- $front = preg_replace('|^/+|', '', $front);
-
- //the main workhorse loop
- $post_rewrite = array();
- $struct = $front;
- for ($j = 0; $j < $num_dirs; ++$j) {
- //get the struct for this dir, and trim slashes off the front
- $struct .= $dirs[$j] . '/'; //accumulate. see comment near explode('/', $structure) above
- $struct = ltrim($struct, '/');
- //replace tags with regexes
- $match = str_replace($this->rewritecode, $this->rewritereplace, $struct);
- //make a list of tags, and store how many there are in $num_toks
- $num_toks = preg_match_all('/%.+?%/', $struct, $toks);
- //get the 'tagname=$matches[i]'
- $query = $queries[$num_toks - 1];
-
- //set up $ep_mask_specific which is used to match more specific URL types
- switch ($dirs[$j]) {
- case '%year%': $ep_mask_specific = EP_YEAR; break;
- case '%monthnum%': $ep_mask_specific = EP_MONTH; break;
- case '%day%': $ep_mask_specific = EP_DAY; break;
- }
-
- //create query for /page/xx
- $pagematch = $match . $pageregex;
- $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1);
-
- //create query for /feed/(feed|atom|rss|rss2|rdf)
- $feedmatch = $match . $feedregex;
- $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
-
- //create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex)
- $feedmatch2 = $match . $feedregex2;
- $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
-
- //if asked to, turn the feed queries into comment feed ones
- if ($forcomments) {
- $feedquery .= '&withcomments=1';
- $feedquery2 .= '&withcomments=1';
- }