| 1 | <?php |
| 2 | |
| 3 | /* WP_Rewrite API |
| 4 | *******************************************************************************/ |
| 5 | |
| 6 | //Add a straight rewrite rule |
| 7 | function add_rewrite_rule($regex, $redirect) { |
| 8 | global $wp_rewrite; |
| 9 | $wp_rewrite->add_extra_rule($regex, $redirect); |
| 10 | } |
| 11 | |
| 12 | //Add a new tag (like %postname%) |
| 13 | //warning: you must call this on init or earlier, otherwise the query var addition stuff won't work |
| 14 | function add_rewrite_tag($tagname, $regex) { |
| 15 | //validation |
| 16 | if (strlen($tagname) < 3 || $tagname{0} != '%' || $tagname{strlen($tagname)-1} != '%') { |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | $qv = trim($tagname, '%'); |
| 21 | |
| 22 | global $wp_rewrite, $wp; |
| 23 | $wp->add_extra_qv($qv); |
| 24 | $wp_rewrite->add_rewrite_tag($tagname, $regex, $qv . '='); |
| 25 | } |
| 26 | |
| 27 | //Add a new feed type like /atom1/ |
| 28 | function add_feed($feedname, $filename) { |
| 29 | global $wp_rewrite; |
| 30 | if (!in_array($feedname, $wp_rewrite->feeds)) { //override the file if it is |
| 31 | $wp_rewrite->feeds[] = $feedname; |
| 32 | } |
| 33 | $wp_rewrite->feed_files[$feedname] = $filename; |
| 34 | } |
| 35 | |
| 36 | define('EP_PERMALINK', 1 ); |
| 37 | define('EP_DATE', 2 ); |
| 38 | define('EP_ROOT', 4 ); |
| 39 | define('EP_COMMENTS', 8 ); |
| 40 | define('EP_SEARCH', 16 ); |
| 41 | define('EP_CATEGORIES', 32 ); |
| 42 | define('EP_AUTHORS', 64 ); |
| 43 | define('EP_PAGES', 128); |
| 44 | //pseudo-places |
| 45 | define('EP_NONE', 0 ); |
| 46 | define('EP_ALL', 255); |
| 47 | |
| 48 | //and an endpoint, like /trackback/ |
| 49 | function add_endpoint($name, $places) { |
| 50 | global $wp_rewrite; |
| 51 | $wp_rewrite->add_endpoint($name, $func, $places); |
| 52 | } |
| 53 | |
| 54 | /* WP_Rewrite class |
| 55 | *******************************************************************************/ |
| 56 | |
| 57 | class WP_Rewrite { |
| 58 | var $permalink_structure; |
| 59 | var $category_base; |
| 60 | var $category_structure; |
| 61 | var $author_base = 'author'; |
| 62 | var $author_structure; |
| 63 | var $date_structure; |
| 64 | var $page_structure; |
| 65 | var $search_base = 'search'; |
| 66 | var $search_structure; |
| 67 | var $comments_base = 'comments'; |
| 68 | var $feed_base = 'feed'; |
| 69 | var $comments_feed_structure; |
| 70 | var $feed_structure; |
| 71 | var $front; |
| 72 | var $root = ''; |
| 73 | var $index = 'index.php'; |
| 74 | var $matches = ''; |
| 75 | var $rules; |
| 76 | var $extra_rules; //those not generated by the class, see add_rewrite_rule() |
| 77 | var $non_wp_rules; //rules that don't redirect to WP's index.php |
| 78 | var $endpoints; |
| 79 | var $use_verbose_rules = false; |
| 80 | var $rewritecode = |
| 81 | array( |
| 82 | '%year%', |
| 83 | '%monthnum%', |
| 84 | '%day%', |
| 85 | '%hour%', |
| 86 | '%minute%', |
| 87 | '%second%', |
| 88 | '%postname%', |
| 89 | '%post_id%', |
| 90 | '%category%', |
| 91 | '%author%', |
| 92 | '%pagename%', |
| 93 | '%search%' |
| 94 | ); |
| 95 | |
| 96 | var $rewritereplace = |
| 97 | array( |
| 98 | '([0-9]{4})', |
| 99 | '([0-9]{1,2})', |
| 100 | '([0-9]{1,2})', |
| 101 | '([0-9]{1,2})', |
| 102 | '([0-9]{1,2})', |
| 103 | '([0-9]{1,2})', |
| 104 | '([^/]+)', |
| 105 | '([0-9]+)', |
| 106 | '(.+?)', |
| 107 | '([^/]+)', |
| 108 | '([^/]+)', |
| 109 | '(.+)' |
| 110 | ); |
| 111 | |
| 112 | var $queryreplace = |
| 113 | array ( |
| 114 | 'year=', |
| 115 | 'monthnum=', |
| 116 | 'day=', |
| 117 | 'hour=', |
| 118 | 'minute=', |
| 119 | 'second=', |
| 120 | 'name=', |
| 121 | 'p=', |
| 122 | 'category_name=', |
| 123 | 'author_name=', |
| 124 | 'pagename=', |
| 125 | 's=' |
| 126 | ); |
| 127 | |
| 128 | var $feeds = array ( 'feed', 'rdf', 'rss', 'rss2', 'atom' ); |
| 129 | |
| 130 | //the filenames aren't actually used in WP_Rewrite but seems a convenient place as any to store them |
| 131 | var $feed_files = array ( |
| 132 | 'rdf' => 'wp-rdf.php', |
| 133 | 'rss' => 'wp-rss.php', |
| 134 | 'rss2' => 'wp-rss2.php', |
| 135 | 'atom' =>'wp-atom.php' |
| 136 | ); |
| 137 | |
| 138 | function using_permalinks() { |
| 139 | if (empty($this->permalink_structure)) |
| 140 | return false; |
| 141 | else |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | function using_index_permalinks() { |
| 146 | if (empty($this->permalink_structure)) { |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | // If the index is not in the permalink, we're using mod_rewrite. |
| 151 | if (preg_match('#^/*' . $this->index . '#', $this->permalink_structure)) { |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | function using_mod_rewrite_permalinks() { |
| 159 | if ( $this->using_permalinks() && ! $this->using_index_permalinks()) |
| 160 | return true; |
| 161 | else |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | function preg_index($number) { |
| 166 | $match_prefix = '$'; |
| 167 | $match_suffix = ''; |
| 168 | |
| 169 | if (! empty($this->matches)) { |
| 170 | $match_prefix = '$' . $this->matches . '['; |
| 171 | $match_suffix = ']'; |
| 172 | } |
| 173 | |
| 174 | return "$match_prefix$number$match_suffix"; |
| 175 | } |
| 176 | |
| 177 | function page_rewrite_rules() { |
| 178 | $uris = get_settings('page_uris'); |
| 179 | $attachment_uris = get_settings('page_attachment_uris'); |
| 180 | |
| 181 | $rewrite_rules = array(); |
| 182 | $page_structure = $this->get_page_permastruct(); |
| 183 | if( is_array( $attachment_uris ) ) { |
| 184 | foreach ($attachment_uris as $uri => $pagename) { |
| 185 | $this->add_rewrite_tag('%pagename%', "($uri)", 'attachment='); |
| 186 | $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES)); |
| 187 | } |
| 188 | } |
| 189 | if( is_array( $uris ) ) { |
| 190 | foreach ($uris as $uri => $pagename) { |
| 191 | $this->add_rewrite_tag('%pagename%', "($uri)", 'pagename='); |
| 192 | $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES)); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return $rewrite_rules; |
| 197 | } |
| 198 | |
| 199 | function get_date_permastruct() { |
| 200 | if (isset($this->date_structure)) { |
| 201 | return $this->date_structure; |
| 202 | } |
| 203 | |
| 204 | if (empty($this->permalink_structure)) { |
| 205 | $this->date_structure = ''; |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | // The date permalink must have year, month, and day separated by slashes. |
| 210 | $endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%'); |
| 211 | |
| 212 | $this->date_structure = ''; |
| 213 | $date_endian = ''; |
| 214 | |
| 215 | foreach ($endians as $endian) { |
| 216 | if (false !== strpos($this->permalink_structure, $endian)) { |
| 217 | $date_endian= $endian; |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if ( empty($date_endian) ) |
| 223 | $date_endian = '%year%/%monthnum%/%day%'; |
| 224 | |
| 225 | // Do not allow the date tags and %post_id% to overlap in the permalink |
| 226 | // structure. If they do, move the date tags to $front/date/. |
| 227 | $front = $this->front; |
| 228 | preg_match_all('/%.+?%/', $this->permalink_structure, $tokens); |
| 229 | $tok_index = 1; |
| 230 | foreach ($tokens[0] as $token) { |
| 231 | if ( ($token == '%post_id%') && ($tok_index <= 3) ) { |
| 232 | $front = $front . 'date/'; |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | $this->date_structure = $front . $date_endian; |
| 238 | |
| 239 | return $this->date_structure; |
| 240 | } |
| 241 | |
| 242 | function get_year_permastruct() { |
| 243 | $structure = $this->get_date_permastruct($this->permalink_structure); |
| 244 | |
| 245 | if (empty($structure)) { |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | $structure = str_replace('%monthnum%', '', $structure); |
| 250 | $structure = str_replace('%day%', '', $structure); |
| 251 | |
| 252 | $structure = preg_replace('#/+#', '/', $structure); |
| 253 | |
| 254 | return $structure; |
| 255 | } |
| 256 | |
| 257 | function get_month_permastruct() { |
| 258 | $structure = $this->get_date_permastruct($this->permalink_structure); |
| 259 | |
| 260 | if (empty($structure)) { |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | $structure = str_replace('%day%', '', $structure); |
| 265 | |
| 266 | $structure = preg_replace('#/+#', '/', $structure); |
| 267 | |
| 268 | return $structure; |
| 269 | } |
| 270 | |
| 271 | function get_day_permastruct() { |
| 272 | return $this->get_date_permastruct($this->permalink_structure); |
| 273 | } |
| 274 | |
| 275 | function get_category_permastruct() { |
| 276 | if (isset($this->category_structure)) { |
| 277 | return $this->category_structure; |
| 278 | } |
| 279 | |
| 280 | if (empty($this->permalink_structure)) { |
| 281 | $this->category_structure = ''; |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | if (empty($this->category_base)) |
| 286 | $this->category_structure = $this->front . 'category/'; |
| 287 | else |
| 288 | $this->category_structure = $this->category_base . '/'; |
| 289 | |
| 290 | $this->category_structure .= '%category%'; |
| 291 | |
| 292 | return $this->category_structure; |
| 293 | } |
| 294 | |
| 295 | function get_author_permastruct() { |
| 296 | if (isset($this->author_structure)) { |
| 297 | return $this->author_structure; |
| 298 | } |
| 299 | |
| 300 | if (empty($this->permalink_structure)) { |
| 301 | $this->author_structure = ''; |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | $this->author_structure = $this->front . $this->author_base . '/%author%'; |
| 306 | |
| 307 | return $this->author_structure; |
| 308 | } |
| 309 | |
| 310 | function get_search_permastruct() { |
| 311 | if (isset($this->search_structure)) { |
| 312 | return $this->search_structure; |
| 313 | } |
| 314 | |
| 315 | if (empty($this->permalink_structure)) { |
| 316 | $this->search_structure = ''; |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | $this->search_structure = $this->root . $this->search_base . '/%search%'; |
| 321 | |
| 322 | return $this->search_structure; |
| 323 | } |
| 324 | |
| 325 | function get_page_permastruct() { |
| 326 | if (isset($this->page_structure)) { |
| 327 | return $this->page_structure; |
| 328 | } |
| 329 | |
| 330 | if (empty($this->permalink_structure)) { |
| 331 | $this->page_structure = ''; |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | $this->page_structure = $this->root . '%pagename%'; |
| 336 | |
| 337 | return $this->page_structure; |
| 338 | } |
| 339 | |
| 340 | function get_feed_permastruct() { |
| 341 | if (isset($this->feed_structure)) { |
| 342 | return $this->feed_structure; |
| 343 | } |
| 344 | |
| 345 | if (empty($this->permalink_structure)) { |
| 346 | $this->feed_structure = ''; |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | $this->feed_structure = $this->root . $this->feed_base . '/%feed%'; |
| 351 | |
| 352 | return $this->feed_structure; |
| 353 | } |
| 354 | |
| 355 | function get_comment_feed_permastruct() { |
| 356 | if (isset($this->comment_feed_structure)) { |
| 357 | return $this->comment_feed_structure; |
| 358 | } |
| 359 | |
| 360 | if (empty($this->permalink_structure)) { |
| 361 | $this->comment_feed_structure = ''; |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | $this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%'; |
| 366 | |
| 367 | return $this->comment_feed_structure; |
| 368 | } |
| 369 | |
| 370 | function add_rewrite_tag($tag, $pattern, $query) { |
| 371 | // If the tag already exists, replace the existing pattern and query for |
| 372 | // that tag, otherwise add the new tag, pattern, and query to the end of |
| 373 | // the arrays. |
| 374 | $position = array_search($tag, $this->rewritecode); |
| 375 | if (FALSE !== $position && NULL !== $position) { |
| 376 | $this->rewritereplace[$position] = $pattern; |
| 377 | $this->queryreplace[$position] = $query; |
| 378 | } else { |
| 379 | $this->rewritecode[] = $tag; |
| 380 | $this->rewritereplace[] = $pattern; |
| 381 | $this->queryreplace[] = $query; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | //the main WP_Rewrite function. generate the rules from permalink structure |
| 386 | function generate_rewrite_rules($permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true) { |
| 387 | //build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/? |
| 388 | $feedregex2 = ''; |
| 389 | foreach ($this->feeds as $feed_name) { |
| 390 | $feedregex2 .= $feed_name . '|'; |
| 391 | } |
| 392 | $feedregex2 = '(' . trim($feedregex2, '|') . ')/?$'; |
| 393 | //$feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom |
| 394 | //and <permalink>/atom are both possible |
| 395 | $feedregex = $this->feed_base . '/' . $feedregex2; |
| 396 | |
| 397 | //build a regex to match the trackback and page/xx parts of URLs |
| 398 | $trackbackregex = 'trackback/?$'; |
| 399 | $pageregex = 'page/?([0-9]{1,})/?$'; |
| 400 | |
| 401 | //build up an array of endpoint regexes to append => queries to append |
| 402 | if ($endpoints) { |
| 403 | $ep_query_append = array (); |
| 404 | foreach ($this->endpoints as $endpoint) { |
| 405 | //match everything after the endpoint name, but allow for nothing to appear there |
| 406 | $epmatch = $endpoint[1] . '(/(.*))?/?$'; |
| 407 | //this will be appended on to the rest of the query for each dir |
| 408 | $epquery = '&' . $endpoint[1] . '=' . $this->preg_index(2); |
| 409 | $ep_query_append[$epmatch] = array ( $endpoint[0], $epquery ); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | //get everything up to the first rewrite tag |
| 414 | $front = substr($permalink_structure, 0, strpos($permalink_structure, '%')); |
| 415 | //build an array of the tags (note that said array ends up being in $tokens[0]) |
| 416 | preg_match_all('/%.+?%/', $permalink_structure, $tokens); |
| 417 | |
| 418 | $num_tokens = count($tokens[0]); |
| 419 | |
| 420 | $index = $this->index; //probably 'index.php' |
| 421 | $feedindex = $index; |
| 422 | $trackbackindex = $index; |
| 423 | //build a list from the rewritecode and queryreplace arrays, that will look something like |
| 424 | //tagname=$matches[i] where i is the current $i |
| 425 | for ($i = 0; $i < $num_tokens; ++$i) { |
| 426 | if (0 < $i) { |
| 427 | $queries[$i] = $queries[$i - 1] . '&'; |
| 428 | } |
| 429 | |
| 430 | $query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1); |
| 431 | $queries[$i] .= $query_token; |
| 432 | } |
| 433 | |
| 434 | //get the structure, minus any cruft (stuff that isn't tags) at the front |
| 435 | $structure = $permalink_structure; |
| 436 | if ($front != '/') { |
| 437 | $structure = str_replace($front, '', $structure); |
| 438 | } |
| 439 | //create a list of dirs to walk over, making rewrite rules for each level |
| 440 | //so for example, a $structure of /%year%/%month%/%postname% would create |
| 441 | //rewrite rules for /%year%/, /%year%/%month%/ and /%year%/%month%/%postname% |
| 442 | $structure = trim($structure, '/'); |
| 443 | if ($walk_dirs) { |
| 444 | $dirs = explode('/', $structure); |
| 445 | } else { |
| 446 | $dirs[] = $structure; |
| 447 | } |
| 448 | $num_dirs = count($dirs); |
| 449 | |
| 450 | //strip slashes from the front of $front |
| 451 | $front = preg_replace('|^/+|', '', $front); |
| 452 | |
| 453 | //the main workhorse loop |
| 454 | $post_rewrite = array(); |
| 455 | $struct = $front; |
| 456 | for ($j = 0; $j < $num_dirs; ++$j) { |
| 457 | //get the struct for this dir, and trim slashes off the front |
| 458 | $struct .= $dirs[$j] . '/'; //accumulate. see comment near explode('/', $structure) above |
| 459 | $struct = ltrim($struct, '/'); |
| 460 | //replace tags with regexes |
| 461 | $match = str_replace($this->rewritecode, $this->rewritereplace, $struct); |
| 462 | //make a list of tags, and store how many there are in $num_toks |
| 463 | $num_toks = preg_match_all('/%.+?%/', $struct, $toks); |
| 464 | //get the 'tagname=$matches[i]' |
| 465 | $query = $queries[$num_toks - 1]; |
| 466 | |
| 467 | //create query for /page/xx |
| 468 | $pagematch = $match . $pageregex; |
| 469 | $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1); |
| 470 | |
| 471 | //create query for /feed/(feed|atom|rss|rss2|rdf) |
| 472 | $feedmatch = $match . $feedregex; |
| 473 | $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1); |
| 474 | |
| 475 | //create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex) |
| 476 | $feedmatch2 = $match . $feedregex2; |
| 477 | $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1); |
| 478 | |
| 479 | //if asked to, turn the feed queries into comment feed ones |
| 480 | if ($forcomments) { |
| 481 | $feedquery .= '&withcomments=1'; |
| 482 | $feedquery2 .= '&withcomments=1'; |
| 483 | } |
| 484 | |
| 485 | //start creating the array of rewrites for this dir |
| 486 | $rewrite = array(); |
| 487 | if ($feed) //...adding on /feed/ regexes => queries |
| 488 | $rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2); |
| 489 | if ($paged) //...and /page/xx ones |
| 490 | $rewrite = array_merge($rewrite, array($pagematch => $pagequery)); |
| 491 | |
| 492 | //if we've got some tags in this dir |
| 493 | if ($num_toks) { |
| 494 | $post = false; |
| 495 | $page = false; |
| 496 | |
| 497 | //check to see if this dir is permalink-level: i.e. the structure specifies an |
| 498 | //individual post. Do this by checking it contains at least one of 1) post name, |
| 499 | //2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and |
| 500 | //minute all present). Set these flags now as we need them for the endpoints. |
| 501 | if (strstr($struct, '%postname%') || strstr($struct, '%post_id%') |
| 502 | || strstr($struct, '%pagename%') |
| 503 | || (strstr($struct, '%year%') && strstr($struct, '%monthnum%') && strstr($struct, '%day%') && strstr($struct, '%hour%') && strstr($struct, '%minute') && strstr($struct, '%second%'))) { |
| 504 | $post = true; |
| 505 | if ( strstr($struct, '%pagename%') ) |
| 506 | $page = true; |
| 507 | } |
| 508 | |
| 509 | //do endpoints |
| 510 | if ($endpoints) { |
| 511 | foreach ($ep_query_append as $regex => $ep) { |
| 512 | //add the endpoints on if 1) it's a permalink and there's flags for a permalink or 2) |
| 513 | //it's not a permalink (then we don't do any flag checking as it should have happened in |
| 514 | //rewrite_rules()) |
| 515 | if ($ep[0] & $ep_mask) { |
| 516 | $rewrite[$match . $regex] = $index . '?' . $query . $ep[1]; |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | //if we're creating rules for a permalink |
| 522 | if ($post) { |
| 523 | $post = true; |
| 524 | //create query and regex for trackback |
| 525 | $trackbackmatch = $match . $trackbackregex; |
| 526 | $trackbackquery = $trackbackindex . '?' . $query . '&tb=1'; |
| 527 | //trim slashes from the end of the regex for this dir |
| 528 | $match = rtrim($match, '/'); |
| 529 | //get rid of brackets |
| 530 | $submatchbase = str_replace(array('(',')'),'',$match); |
| 531 | |
| 532 | //add a rule for at attachements, which take the form of <permalink>/some-text |
| 533 | $sub1 = $submatchbase . '/([^/]+)/'; |
| 534 | $sub1tb = $sub1 . $trackbackregex; //add trackback regex |
| 535 | $sub1feed = $sub1 . $feedregex; //and /feed/(atom|...) |
| 536 | $sub1feed2 = $sub1 . $feedregex2; //and (feed|atom...) |
| 537 | //add an ? as we don't have to match that last slash, and finally a $ so we |
| 538 | //match to the end of the URL |
| 539 | $sub1 .= '?$'; |
| 540 | |
| 541 | //add another rule to match attachments in the explicit form: |
| 542 | //<permalink>/attachment/some-text |
| 543 | $sub2 = $submatchbase . '/attachment/([^/]+)/'; |
| 544 | $sub2tb = $sub2 . $trackbackregex; //and add trackbacks, |
| 545 | $sub2feed = $sub2 . $feedregex; //feeds, |
| 546 | $sub2feed2 = $sub2 . $feedregex2; //and feeds again on to this |
| 547 | $sub2 .= '?$'; //add a ?$ as above |
| 548 | |
| 549 | //create queries for these extra tag-ons we've just dealt with |
| 550 | $subquery = $index . '?attachment=' . $this->preg_index(1); |
| 551 | $subtbquery = $subquery . '&tb=1'; |
| 552 | $subfeedquery = $subquery . '&feed=' . $this->preg_index(2); |
| 553 | |
| 554 | //allow URLs like <permalink>/2 for <permalink>/page/2 |
| 555 | $match = $match . '(/[0-9]+)?/?$'; |
| 556 | $query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1); |
| 557 | } else { //not matching a permalink so this is a lot simpler |
| 558 | //close the match and finalise the query |
| 559 | $match .= '?$'; |
| 560 | $query = $index . '?' . $query; |
| 561 | } |
| 562 | |
| 563 | //create the final array for this dir by joining the $rewrite array (which currently |
| 564 | //only contains rules/queries for trackback, pages etc) to the main regex/query for |
| 565 | //this dir |
| 566 | $rewrite = array_merge($rewrite, array($match => $query)); |
| 567 | |
| 568 | //if we're matching a permalink, add those extras (attachments etc) on |
| 569 | if ($post) { |
| 570 | //add trackback |
| 571 | $rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite); |
| 572 | |
| 573 | //add regexes/queries for attachments, attachment trackbacks and so on |
| 574 | if ( ! $page ) //require <permalink>/attachment/stuff form for pages because of confusion with subpages |
| 575 | $rewrite = array_merge($rewrite, array($sub1 => $subquery, $sub1tb => $subtbquery, $sub1feed => $subfeedquery, $sub1feed2 => $subfeedquery)); |
| 576 | $rewrite = array_merge($rewrite, array($sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery)); |
| 577 | } |
| 578 | } |
| 579 | //add the rules for this dir to the accumulating $post_rewrite |
| 580 | $post_rewrite = array_merge($rewrite, $post_rewrite); |
| 581 | } |
| 582 | return $post_rewrite; //the finished rules. phew! |
| 583 | } |
| 584 | |
| 585 | function generate_rewrite_rule($permalink_structure, $walk_dirs = false) { |
| 586 | return $this->generate_rewrite_rules($permalink_structure, EP_NONE, false, false, false, $walk_dirs); |
| 587 | } |
| 588 | |
| 589 | /* rewrite_rules |
| 590 | * Construct rewrite matches and queries from permalink structure. |
| 591 | * Returns an associate array of matches and queries. |
| 592 | */ |
| 593 | function rewrite_rules() { |
| 594 | $rewrite = array(); |
| 595 | |
| 596 | if (empty($this->permalink_structure)) { |
| 597 | return $rewrite; |
| 598 | } |
| 599 | |
| 600 | // Post |
| 601 | $post_rewrite = $this->generate_rewrite_rules($this->permalink_structure, EP_PERMALINK); |
| 602 | $post_rewrite = apply_filters('post_rewrite_rules', $post_rewrite); |
| 603 | |
| 604 | // Date |
| 605 | $date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct(), EP_DATE); |
| 606 | $date_rewrite = apply_filters('date_rewrite_rules', $date_rewrite); |
| 607 | |
| 608 | // Root |
| 609 | $root_rewrite = $this->generate_rewrite_rules($this->root . '/', EP_ROOT); |
| 610 | $root_rewrite = apply_filters('root_rewrite_rules', $root_rewrite); |
| 611 | |
| 612 | // Comments |
| 613 | $comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, EP_COMMENTS, true, true, true, false); |
| 614 | $comments_rewrite = apply_filters('comments_rewrite_rules', $comments_rewrite); |
| 615 | |
| 616 | // Search |
| 617 | $search_structure = $this->get_search_permastruct(); |
| 618 | $search_rewrite = $this->generate_rewrite_rules($search_structure, EP_SEARCH); |
| 619 | $search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite); |
| 620 | |
| 621 | // Categories |
| 622 | $category_rewrite = $this->generate_rewrite_rules($this->get_category_permastruct(), EP_CATEGORIES); |
| 623 | $category_rewrite = apply_filters('category_rewrite_rules', $category_rewrite); |
| 624 | |
| 625 | // Authors |
| 626 | $author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct(), EP_AUTHORS); |
| 627 | $author_rewrite = apply_filters('author_rewrite_rules', $author_rewrite); |
| 628 | |
| 629 | // Pages |
| 630 | $page_rewrite = $this->page_rewrite_rules(); |
| 631 | $page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite); |
| 632 | |
| 633 | // Put them together. |
| 634 | $this->rules = array_merge($page_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $this->extra_rules); |
| 635 | |
| 636 | do_action('generate_rewrite_rules', array(&$this)); |
| 637 | $this->rules = apply_filters('rewrite_rules_array', $this->rules); |
| 638 | |
| 639 | return $this->rules; |
| 640 | } |
| 641 | |
| 642 | function wp_rewrite_rules() { |
| 643 | $this->rules = get_option('rewrite_rules'); |
| 644 | if ( empty($this->rules) ) { |
| 645 | $this->matches = 'matches'; |
| 646 | $this->rewrite_rules(); |
| 647 | update_option('rewrite_rules', $this->rules); |
| 648 | } |
| 649 | |
| 650 | return $this->rules; |
| 651 | } |
| 652 | |
| 653 | function mod_rewrite_rules() { |
| 654 | if ( ! $this->using_permalinks()) { |
| 655 | return ''; |
| 656 | } |
| 657 | |
| 658 | $site_root = parse_url(get_settings('siteurl')); |
| 659 | $site_root = trailingslashit($site_root['path']); |
| 660 | |
| 661 | $home_root = parse_url(get_settings('home')); |
| 662 | $home_root = trailingslashit($home_root['path']); |
| 663 | |
| 664 | $rules = "<IfModule mod_rewrite.c>\n"; |
| 665 | $rules .= "RewriteEngine On\n"; |
| 666 | $rules .= "RewriteBase $home_root\n"; |
| 667 | |
| 668 | //add in the rules that don't redirect to WP's index.php (and thus shouldn't be handled by WP at all) |
| 669 | foreach ($this->non_wp_rules as $match => $query) { |
| 670 | // Apache 1.3 does not support the reluctant (non-greedy) modifier. |
| 671 | $match = str_replace('.+?', '.+', $match); |
| 672 | |
| 673 | // If the match is unanchored and greedy, prepend rewrite conditions |
| 674 | // to avoid infinite redirects and eclipsing of real files. |
| 675 | if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) { |
| 676 | //nada. |
| 677 | } |
| 678 | |
| 679 | $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n"; |
| 680 | } |
| 681 | |
| 682 | if ($this->use_verbose_rules) { |
| 683 | $this->matches = ''; |
| 684 | $rewrite = $this->rewrite_rules(); |
| 685 | $num_rules = count($rewrite); |
| 686 | $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" . |
| 687 | "RewriteCond %{REQUEST_FILENAME} -d\n" . |
| 688 | "RewriteRule ^.*$ - [S=$num_rules]\n"; |
| 689 | |
| 690 | foreach ($rewrite as $match => $query) { |
| 691 | // Apache 1.3 does not support the reluctant (non-greedy) modifier. |
| 692 | $match = str_replace('.+?', '.+', $match); |
| 693 | |
| 694 | // If the match is unanchored and greedy, prepend rewrite conditions |
| 695 | // to avoid infinite redirects and eclipsing of real files. |
| 696 | if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) { |
| 697 | //nada. |
| 698 | } |
| 699 | |
| 700 | if (strstr($query, $this->index)) { |
| 701 | $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n"; |
| 702 | } else { |
| 703 | $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n"; |
| 704 | } |
| 705 | } |
| 706 | } else { |
| 707 | $rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" . |
| 708 | "RewriteCond %{REQUEST_FILENAME} !-d\n" . |
| 709 | "RewriteRule . {$home_root}{$this->index} [L]\n"; |
| 710 | } |
| 711 | |
| 712 | $rules .= "</IfModule>\n"; |
| 713 | |
| 714 | $rules = apply_filters('mod_rewrite_rules', $rules); |
| 715 | $rules = apply_filters('rewrite_rules', $rules); // Deprecated |
| 716 | |
| 717 | return $rules; |
| 718 | } |
| 719 | |
| 720 | //Add a straight rewrite rule |
| 721 | function add_rule($regex, $redirect) { |
| 722 | //get everything up to the first ? |
| 723 | $index = (strpos($redirect, '?') == false ? strlen($redirect) : strpos($redirect, '?')); |
| 724 | $front = substr($redirect, 0, $index); |
| 725 | if ($front != $this->index) { //it doesn't redirect to WP's index.php |
| 726 | $this->add_external_rule($regex, $redirect); |
| 727 | } else { |
| 728 | $this->extra_rules[$regex] = $redirect; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | //add a rule that doesn't redirect to index.php |
| 733 | function add_external_rule($regex, $redirect) { |
| 734 | $this->non_wp_rules[$regex] = $redirect; |
| 735 | } |
| 736 | |
| 737 | //add an endpoint, like /trackback/, to be inserted after certain URL types (specified in $places) |
| 738 | function add_endpoint($name, $places) { |
| 739 | global $wp; |
| 740 | $this->endpoints[] = array ( $places, $name ); |
| 741 | $wp->add_query_var($name); |
| 742 | } |
| 743 | |
| 744 | function flush_rules() { |
| 745 | generate_page_uri_index(); |
| 746 | delete_option('rewrite_rules'); |
| 747 | $this->wp_rewrite_rules(); |
| 748 | if ( function_exists('save_mod_rewrite_rules') ) |
| 749 | save_mod_rewrite_rules(); |
| 750 | } |
| 751 | |
| 752 | function init() { |
| 753 | $this->extra_rules = $this->non_wp_rules = $this->endpoints = array(); |
| 754 | $this->permalink_structure = get_settings('permalink_structure'); |
| 755 | $this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%')); |
| 756 | $this->root = ''; |
| 757 | if ($this->using_index_permalinks()) { |
| 758 | $this->root = $this->index . '/'; |
| 759 | } |
| 760 | $this->category_base = get_settings('category_base'); |
| 761 | unset($this->category_structure); |
| 762 | unset($this->author_structure); |
| 763 | unset($this->date_structure); |
| 764 | unset($this->page_structure); |
| 765 | unset($this->search_structure); |
| 766 | unset($this->feed_structure); |
| 767 | unset($this->comment_feed_structure); |
| 768 | } |
| 769 | |
| 770 | function set_permalink_structure($permalink_structure) { |
| 771 | if ($permalink_structure != $this->permalink_structure) { |
| 772 | update_option('permalink_structure', $permalink_structure); |
| 773 | $this->init(); |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | function set_category_base($category_base) { |
| 778 | if ($category_base != $this->category_base) { |
| 779 | update_option('category_base', $category_base); |
| 780 | $this->init(); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | function WP_Rewrite() { |
| 785 | $this->init(); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | ?> |
| 790 | No newline at end of file |