web/wp-includes/rewrite.php
branchwordpress
changeset 109 03b0d1493584
child 132 4d4862461b8d
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     1 <?php
       
     2 /**
       
     3  * WordPress Rewrite API
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Rewrite
       
     7  */
       
     8 
       
     9 /**
       
    10  * Add a straight rewrite rule.
       
    11  *
       
    12  * @see WP_Rewrite::add_rule() for long description.
       
    13  * @since 2.1.0
       
    14  *
       
    15  * @param string $regex Regular Expression to match request against.
       
    16  * @param string $redirect Page to redirect to.
       
    17  * @param string $after Optional, default is 'bottom'. Where to add rule, can also be 'top'.
       
    18  */
       
    19 function add_rewrite_rule($regex, $redirect, $after = 'bottom') {
       
    20 	global $wp_rewrite;
       
    21 	$wp_rewrite->add_rule($regex, $redirect, $after);
       
    22 }
       
    23 
       
    24 /**
       
    25  * Add a new tag (like %postname%).
       
    26  *
       
    27  * Warning: you must call this on init or earlier, otherwise the query var
       
    28  * addition stuff won't work.
       
    29  *
       
    30  * @since 2.1.0
       
    31  *
       
    32  * @param string $tagname
       
    33  * @param string $regex
       
    34  */
       
    35 function add_rewrite_tag($tagname, $regex) {
       
    36 	//validation
       
    37 	if (strlen($tagname) < 3 || $tagname{0} != '%' || $tagname{strlen($tagname)-1} != '%') {
       
    38 		return;
       
    39 	}
       
    40 
       
    41 	$qv = trim($tagname, '%');
       
    42 
       
    43 	global $wp_rewrite, $wp;
       
    44 	$wp->add_query_var($qv);
       
    45 	$wp_rewrite->add_rewrite_tag($tagname, $regex, $qv . '=');
       
    46 }
       
    47 
       
    48 /**
       
    49  * Add a new feed type like /atom1/.
       
    50  *
       
    51  * @since 2.1.0
       
    52  *
       
    53  * @param string $feedname
       
    54  * @param callback $function Callback to run on feed display.
       
    55  * @return string Feed action name.
       
    56  */
       
    57 function add_feed($feedname, $function) {
       
    58 	global $wp_rewrite;
       
    59 	if (!in_array($feedname, $wp_rewrite->feeds)) { //override the file if it is
       
    60 		$wp_rewrite->feeds[] = $feedname;
       
    61 	}
       
    62 	$hook = 'do_feed_' . $feedname;
       
    63 	// Remove default function hook
       
    64 	remove_action($hook, $hook, 10, 1);
       
    65 	add_action($hook, $function, 10, 1);
       
    66 	return $hook;
       
    67 }
       
    68 
       
    69 /**
       
    70  * Endpoint Mask for Permalink.
       
    71  *
       
    72  * @since 2.1.0
       
    73  */
       
    74 define('EP_PERMALINK', 1);
       
    75 
       
    76 /**
       
    77  * Endpoint Mask for Attachment.
       
    78  *
       
    79  * @since 2.1.0
       
    80  */
       
    81 define('EP_ATTACHMENT', 2);
       
    82 
       
    83 /**
       
    84  * Endpoint Mask for date.
       
    85  *
       
    86  * @since 2.1.0
       
    87  */
       
    88 define('EP_DATE', 4);
       
    89 
       
    90 /**
       
    91  * Endpoint Mask for year
       
    92  *
       
    93  * @since 2.1.0
       
    94  */
       
    95 define('EP_YEAR', 8);
       
    96 
       
    97 /**
       
    98  * Endpoint Mask for month.
       
    99  *
       
   100  * @since 2.1.0
       
   101  */
       
   102 define('EP_MONTH', 16);
       
   103 
       
   104 /**
       
   105  * Endpoint Mask for day.
       
   106  *
       
   107  * @since 2.1.0
       
   108  */
       
   109 define('EP_DAY', 32);
       
   110 
       
   111 /**
       
   112  * Endpoint Mask for root.
       
   113  *
       
   114  * @since 2.1.0
       
   115  */
       
   116 define('EP_ROOT', 64);
       
   117 
       
   118 /**
       
   119  * Endpoint Mask for comments.
       
   120  *
       
   121  * @since 2.1.0
       
   122  */
       
   123 define('EP_COMMENTS', 128);
       
   124 
       
   125 /**
       
   126  * Endpoint Mask for searches.
       
   127  *
       
   128  * @since 2.1.0
       
   129  */
       
   130 define('EP_SEARCH', 256);
       
   131 
       
   132 /**
       
   133  * Endpoint Mask for categories.
       
   134  *
       
   135  * @since 2.1.0
       
   136  */
       
   137 define('EP_CATEGORIES', 512);
       
   138 
       
   139 /**
       
   140  * Endpoint Mask for tags.
       
   141  *
       
   142  * @since 2.3.0
       
   143  */
       
   144 define('EP_TAGS', 1024);
       
   145 
       
   146 /**
       
   147  * Endpoint Mask for authors.
       
   148  *
       
   149  * @since 2.1.0
       
   150  */
       
   151 define('EP_AUTHORS', 2048);
       
   152 
       
   153 /**
       
   154  * Endpoint Mask for pages.
       
   155  *
       
   156  * @since 2.1.0
       
   157  */
       
   158 define('EP_PAGES', 4096);
       
   159 
       
   160 //pseudo-places
       
   161 /**
       
   162  * Endpoint Mask for default, which is nothing.
       
   163  *
       
   164  * @since 2.1.0
       
   165  */
       
   166 define('EP_NONE', 0);
       
   167 
       
   168 /**
       
   169  * Endpoint Mask for everything.
       
   170  *
       
   171  * @since 2.1.0
       
   172  */
       
   173 define('EP_ALL', 8191);
       
   174 
       
   175 /**
       
   176  * Add an endpoint, like /trackback/.
       
   177  *
       
   178  * The endpoints are added to the end of the request. So a request matching
       
   179  * "/2008/10/14/my_post/myep/", the endpoint will be "/myep/".
       
   180  *
       
   181  * Be sure to flush the rewrite rules (wp_rewrite->flush()) when your plugin gets
       
   182  * activated (register_activation_hook()) and deactivated (register_deactivation_hook())
       
   183  *
       
   184  * @since 2.1.0
       
   185  * @see WP_Rewrite::add_endpoint() Parameters and more description.
       
   186  * @uses $wp_rewrite
       
   187  *
       
   188  * @param unknown_type $name
       
   189  * @param unknown_type $places
       
   190  */
       
   191 function add_rewrite_endpoint($name, $places) {
       
   192 	global $wp_rewrite;
       
   193 	$wp_rewrite->add_endpoint($name, $places);
       
   194 }
       
   195 
       
   196 /**
       
   197  * Filter the URL base for taxonomies.
       
   198  *
       
   199  * To remove any manually prepended /index.php/.
       
   200  *
       
   201  * @access private
       
   202  * @since 2.6.0
       
   203  * @author Mark Jaquith
       
   204  *
       
   205  * @param string $base The taxonomy base that we're going to filter
       
   206  * @return string
       
   207  */
       
   208 function _wp_filter_taxonomy_base( $base ) {
       
   209 	if ( !empty( $base ) ) {
       
   210 		$base = preg_replace( '|^/index\.php/|', '', $base );
       
   211 		$base = trim( $base, '/' );
       
   212 	}
       
   213 	return $base;
       
   214 }
       
   215 
       
   216 /**
       
   217  * Examine a url and try to determine the post ID it represents.
       
   218  *
       
   219  * Checks are supposedly from the hosted site blog.
       
   220  *
       
   221  * @since 1.0.0
       
   222  *
       
   223  * @param string $url Permalink to check.
       
   224  * @return int Post ID, or 0 on failure.
       
   225  */
       
   226 function url_to_postid($url) {
       
   227 	global $wp_rewrite;
       
   228 
       
   229 	$url = apply_filters('url_to_postid', $url);
       
   230 
       
   231 	// First, check to see if there is a 'p=N' or 'page_id=N' to match against
       
   232 	if ( preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values) )	{
       
   233 		$id = absint($values[2]);
       
   234 		if ($id)
       
   235 			return $id;
       
   236 	}
       
   237 
       
   238 	// Check to see if we are using rewrite rules
       
   239 	$rewrite = $wp_rewrite->wp_rewrite_rules();
       
   240 
       
   241 	// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
       
   242 	if ( empty($rewrite) )
       
   243 		return 0;
       
   244 
       
   245 	// $url cleanup by Mark Jaquith
       
   246 	// This fixes things like #anchors, ?query=strings, missing 'www.',
       
   247 	// added 'www.', or added 'index.php/' that will mess up our WP_Query
       
   248 	// and return a false negative
       
   249 
       
   250 	// Get rid of the #anchor
       
   251 	$url_split = explode('#', $url);
       
   252 	$url = $url_split[0];
       
   253 
       
   254 	// Get rid of URL ?query=string
       
   255 	$url_split = explode('?', $url);
       
   256 	$url = $url_split[0];
       
   257 
       
   258 	// Add 'www.' if it is absent and should be there
       
   259 	if ( false !== strpos(get_option('home'), '://www.') && false === strpos($url, '://www.') )
       
   260 		$url = str_replace('://', '://www.', $url);
       
   261 
       
   262 	// Strip 'www.' if it is present and shouldn't be
       
   263 	if ( false === strpos(get_option('home'), '://www.') )
       
   264 		$url = str_replace('://www.', '://', $url);
       
   265 
       
   266 	// Strip 'index.php/' if we're not using path info permalinks
       
   267 	if ( !$wp_rewrite->using_index_permalinks() )
       
   268 		$url = str_replace('index.php/', '', $url);
       
   269 
       
   270 	if ( false !== strpos($url, get_option('home')) ) {
       
   271 		// Chop off http://domain.com
       
   272 		$url = str_replace(get_option('home'), '', $url);
       
   273 	} else {
       
   274 		// Chop off /path/to/blog
       
   275 		$home_path = parse_url(get_option('home'));
       
   276 		$home_path = $home_path['path'];
       
   277 		$url = str_replace($home_path, '', $url);
       
   278 	}
       
   279 
       
   280 	// Trim leading and lagging slashes
       
   281 	$url = trim($url, '/');
       
   282 
       
   283 	$request = $url;
       
   284 
       
   285 	// Done with cleanup
       
   286 
       
   287 	// Look for matches.
       
   288 	$request_match = $request;
       
   289 	foreach ($rewrite as $match => $query) {
       
   290 		// If the requesting file is the anchor of the match, prepend it
       
   291 		// to the path info.
       
   292 		if ( (! empty($url)) && (strpos($match, $url) === 0) && ($url != $request)) {
       
   293 			$request_match = $url . '/' . $request;
       
   294 		}
       
   295 
       
   296 		if ( preg_match("!^$match!", $request_match, $matches) ) {
       
   297 			// Got a match.
       
   298 			// Trim the query of everything up to the '?'.
       
   299 			$query = preg_replace("!^.+\?!", '', $query);
       
   300 
       
   301 			// Substitute the substring matches into the query.
       
   302 			$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
       
   303 			// Filter out non-public query vars
       
   304 			global $wp;
       
   305 			parse_str($query, $query_vars);
       
   306 			$query = array();
       
   307 			foreach ( (array) $query_vars as $key => $value ) {
       
   308 				if ( in_array($key, $wp->public_query_vars) )
       
   309 					$query[$key] = $value;
       
   310 			}
       
   311 			// Do the query
       
   312 			$query = new WP_Query($query);
       
   313 			if ( $query->is_single || $query->is_page )
       
   314 				return $query->post->ID;
       
   315 			else
       
   316 				return 0;
       
   317 		}
       
   318 	}
       
   319 	return 0;
       
   320 }
       
   321 
       
   322 /**
       
   323  * WordPress Rewrite Component.
       
   324  *
       
   325  * The WordPress Rewrite class writes the rewrite module rules to the .htaccess
       
   326  * file. It also handles parsing the request to get the correct setup for the
       
   327  * WordPress Query class.
       
   328  *
       
   329  * The Rewrite along with WP class function as a front controller for WordPress.
       
   330  * You can add rules to trigger your page view and processing using this
       
   331  * component. The full functionality of a front controller does not exist,
       
   332  * meaning you can't define how the template files load based on the rewrite
       
   333  * rules.
       
   334  *
       
   335  * @since 1.5.0
       
   336  */
       
   337 class WP_Rewrite {
       
   338 	/**
       
   339 	 * Default permalink structure for WordPress.
       
   340 	 *
       
   341 	 * @since 1.5.0
       
   342 	 * @access private
       
   343 	 * @var string
       
   344 	 */
       
   345 	var $permalink_structure;
       
   346 
       
   347 	/**
       
   348 	 * Whether to add trailing slashes.
       
   349 	 *
       
   350 	 * @since 2.2.0
       
   351 	 * @access private
       
   352 	 * @var bool
       
   353 	 */
       
   354 	var $use_trailing_slashes;
       
   355 
       
   356 	/**
       
   357 	 * Customized or default category permalink base ( example.com/xx/tagname ).
       
   358 	 *
       
   359 	 * @since 1.5.0
       
   360 	 * @access private
       
   361 	 * @var string
       
   362 	 */
       
   363 	var $category_base;
       
   364 
       
   365 	/**
       
   366 	 * Customized or default tag permalink base ( example.com/xx/tagname ).
       
   367 	 *
       
   368 	 * @since 2.3.0
       
   369 	 * @access private
       
   370 	 * @var string
       
   371 	 */
       
   372 	var $tag_base;
       
   373 
       
   374 	/**
       
   375 	 * Permalink request structure for categories.
       
   376 	 *
       
   377 	 * @since 1.5.0
       
   378 	 * @access private
       
   379 	 * @var string
       
   380 	 */
       
   381 	var $category_structure;
       
   382 
       
   383 	/**
       
   384 	 * Permalink request structure for tags.
       
   385 	 *
       
   386 	 * @since 2.3.0
       
   387 	 * @access private
       
   388 	 * @var string
       
   389 	 */
       
   390 	var $tag_structure;
       
   391 
       
   392 	/**
       
   393 	 * Permalink author request base ( example.com/author/authorname ).
       
   394 	 *
       
   395 	 * @since 1.5.0
       
   396 	 * @access private
       
   397 	 * @var string
       
   398 	 */
       
   399 	var $author_base = 'author';
       
   400 
       
   401 	/**
       
   402 	 * Permalink request structure for author pages.
       
   403 	 *
       
   404 	 * @since 1.5.0
       
   405 	 * @access private
       
   406 	 * @var string
       
   407 	 */
       
   408 	var $author_structure;
       
   409 
       
   410 	/**
       
   411 	 * Permalink request structure for dates.
       
   412 	 *
       
   413 	 * @since 1.5.0
       
   414 	 * @access private
       
   415 	 * @var string
       
   416 	 */
       
   417 	var $date_structure;
       
   418 
       
   419 	/**
       
   420 	 * Permalink request structure for pages.
       
   421 	 *
       
   422 	 * @since 1.5.0
       
   423 	 * @access private
       
   424 	 * @var string
       
   425 	 */
       
   426 	var $page_structure;
       
   427 
       
   428 	/**
       
   429 	 * Search permalink base ( example.com/search/query ).
       
   430 	 *
       
   431 	 * @since 1.5.0
       
   432 	 * @access private
       
   433 	 * @var string
       
   434 	 */
       
   435 	var $search_base = 'search';
       
   436 
       
   437 	/**
       
   438 	 * Permalink request structure for searches.
       
   439 	 *
       
   440 	 * @since 1.5.0
       
   441 	 * @access private
       
   442 	 * @var string
       
   443 	 */
       
   444 	var $search_structure;
       
   445 
       
   446 	/**
       
   447 	 * Comments permalink base.
       
   448 	 *
       
   449 	 * @since 1.5.0
       
   450 	 * @access private
       
   451 	 * @var string
       
   452 	 */
       
   453 	var $comments_base = 'comments';
       
   454 
       
   455 	/**
       
   456 	 * Feed permalink base.
       
   457 	 *
       
   458 	 * @since 1.5.0
       
   459 	 * @access private
       
   460 	 * @var string
       
   461 	 */
       
   462 	var $feed_base = 'feed';
       
   463 
       
   464 	/**
       
   465 	 * Comments feed request structure permalink.
       
   466 	 *
       
   467 	 * @since 1.5.0
       
   468 	 * @access private
       
   469 	 * @var string
       
   470 	 */
       
   471 	var $comments_feed_structure;
       
   472 
       
   473 	/**
       
   474 	 * Feed request structure permalink.
       
   475 	 *
       
   476 	 * @since 1.5.0
       
   477 	 * @access private
       
   478 	 * @var string
       
   479 	 */
       
   480 	var $feed_structure;
       
   481 
       
   482 	/**
       
   483 	 * Front URL path.
       
   484 	 *
       
   485 	 * The difference between the root property is that WordPress might be
       
   486 	 * located at example/WordPress/index.php, if permalinks are turned off. The
       
   487 	 * WordPress/index.php will be the front portion. If permalinks are turned
       
   488 	 * on, this will most likely be empty or not set.
       
   489 	 *
       
   490 	 * @since 1.5.0
       
   491 	 * @access private
       
   492 	 * @var string
       
   493 	 */
       
   494 	var $front;
       
   495 
       
   496 	/**
       
   497 	 * Root URL path to WordPress (without domain).
       
   498 	 *
       
   499 	 * The difference between front property is that WordPress might be located
       
   500 	 * at example.com/WordPress/. The root is the 'WordPress/' portion.
       
   501 	 *
       
   502 	 * @since 1.5.0
       
   503 	 * @access private
       
   504 	 * @var string
       
   505 	 */
       
   506 	var $root = '';
       
   507 
       
   508 	/**
       
   509 	 * Permalink to the home page.
       
   510 	 *
       
   511 	 * @since 1.5.0
       
   512 	 * @access public
       
   513 	 * @var string
       
   514 	 */
       
   515 	var $index = 'index.php';
       
   516 
       
   517 	/**
       
   518 	 * Request match string.
       
   519 	 *
       
   520 	 * @since 1.5.0
       
   521 	 * @access private
       
   522 	 * @var string
       
   523 	 */
       
   524 	var $matches = '';
       
   525 
       
   526 	/**
       
   527 	 * Rewrite rules to match against the request to find the redirect or query.
       
   528 	 *
       
   529 	 * @since 1.5.0
       
   530 	 * @access private
       
   531 	 * @var array
       
   532 	 */
       
   533 	var $rules;
       
   534 
       
   535 	/**
       
   536 	 * Additional rules added external to the rewrite class.
       
   537 	 *
       
   538 	 * Those not generated by the class, see add_rewrite_rule().
       
   539 	 *
       
   540 	 * @since 2.1.0
       
   541 	 * @access private
       
   542 	 * @var array
       
   543 	 */
       
   544 	var $extra_rules = array(); //
       
   545 
       
   546 	/**
       
   547 	 * Additional rules that belong at the beginning to match first.
       
   548 	 *
       
   549 	 * Those not generated by the class, see add_rewrite_rule().
       
   550 	 *
       
   551 	 * @since 2.3.0
       
   552 	 * @access private
       
   553 	 * @var array
       
   554 	 */
       
   555 	var $extra_rules_top = array(); //
       
   556 
       
   557 	/**
       
   558 	 * Rules that don't redirect to WP's index.php.
       
   559 	 *
       
   560 	 * These rules are written to the mod_rewrite portion of the .htaccess.
       
   561 	 *
       
   562 	 * @since 2.1.0
       
   563 	 * @access private
       
   564 	 * @var array
       
   565 	 */
       
   566 	var $non_wp_rules = array(); //
       
   567 
       
   568 	/**
       
   569 	 * Extra permalink structures.
       
   570 	 *
       
   571 	 * @since 2.1.0
       
   572 	 * @access private
       
   573 	 * @var array
       
   574 	 */
       
   575 	var $extra_permastructs = array();
       
   576 
       
   577 	/**
       
   578 	 * Endpoints permalinks
       
   579 	 *
       
   580 	 * @since unknown
       
   581 	 * @access private
       
   582 	 * @var array
       
   583 	 */
       
   584 	var $endpoints;
       
   585 
       
   586 	/**
       
   587 	 * Whether to write every mod_rewrite rule for WordPress.
       
   588 	 *
       
   589 	 * This is off by default, turning it on might print a lot of rewrite rules
       
   590 	 * to the .htaccess file.
       
   591 	 *
       
   592 	 * @since 2.0.0
       
   593 	 * @access public
       
   594 	 * @var bool
       
   595 	 */
       
   596 	var $use_verbose_rules = false;
       
   597 
       
   598 	/**
       
   599 	 * Whether to write every mod_rewrite rule for WordPress pages.
       
   600 	 *
       
   601 	 * @since 2.5.0
       
   602 	 * @access public
       
   603 	 * @var bool
       
   604 	 */
       
   605 	var $use_verbose_page_rules = true;
       
   606 
       
   607 	/**
       
   608 	 * Permalink structure search for preg_replace.
       
   609 	 *
       
   610 	 * @since 1.5.0
       
   611 	 * @access private
       
   612 	 * @var array
       
   613 	 */
       
   614 	var $rewritecode =
       
   615 		array(
       
   616 					'%year%',
       
   617 					'%monthnum%',
       
   618 					'%day%',
       
   619 					'%hour%',
       
   620 					'%minute%',
       
   621 					'%second%',
       
   622 					'%postname%',
       
   623 					'%post_id%',
       
   624 					'%category%',
       
   625 					'%tag%',
       
   626 					'%author%',
       
   627 					'%pagename%',
       
   628 					'%search%'
       
   629 					);
       
   630 
       
   631 	/**
       
   632 	 * Preg_replace values for the search, see {@link WP_Rewrite::$rewritecode}.
       
   633 	 *
       
   634 	 * @since 1.5.0
       
   635 	 * @access private
       
   636 	 * @var array
       
   637 	 */
       
   638 	var $rewritereplace =
       
   639 		array(
       
   640 					'([0-9]{4})',
       
   641 					'([0-9]{1,2})',
       
   642 					'([0-9]{1,2})',
       
   643 					'([0-9]{1,2})',
       
   644 					'([0-9]{1,2})',
       
   645 					'([0-9]{1,2})',
       
   646 					'([^/]+)',
       
   647 					'([0-9]+)',
       
   648 					'(.+?)',
       
   649 					'(.+?)',
       
   650 					'([^/]+)',
       
   651 					'([^/]+?)',
       
   652 					'(.+)'
       
   653 					);
       
   654 
       
   655 	/**
       
   656 	 * Search for the query to look for replacing.
       
   657 	 *
       
   658 	 * @since 1.5.0
       
   659 	 * @access private
       
   660 	 * @var array
       
   661 	 */
       
   662 	var $queryreplace =
       
   663 		array (
       
   664 					'year=',
       
   665 					'monthnum=',
       
   666 					'day=',
       
   667 					'hour=',
       
   668 					'minute=',
       
   669 					'second=',
       
   670 					'name=',
       
   671 					'p=',
       
   672 					'category_name=',
       
   673 					'tag=',
       
   674 					'author_name=',
       
   675 					'pagename=',
       
   676 					's='
       
   677 					);
       
   678 
       
   679 	/**
       
   680 	 * Supported default feeds.
       
   681 	 *
       
   682 	 * @since 1.5.0
       
   683 	 * @access private
       
   684 	 * @var array
       
   685 	 */
       
   686 	var $feeds = array ( 'feed', 'rdf', 'rss', 'rss2', 'atom' );
       
   687 
       
   688 	/**
       
   689 	 * Whether permalinks are being used.
       
   690 	 *
       
   691 	 * This can be either rewrite module or permalink in the HTTP query string.
       
   692 	 *
       
   693 	 * @since 1.5.0
       
   694 	 * @access public
       
   695 	 *
       
   696 	 * @return bool True, if permalinks are enabled.
       
   697 	 */
       
   698 	function using_permalinks() {
       
   699 		if (empty($this->permalink_structure))
       
   700 			return false;
       
   701 		else
       
   702 			return true;
       
   703 	}
       
   704 
       
   705 	/**
       
   706 	 * Whether permalinks are being used and rewrite module is not enabled.
       
   707 	 *
       
   708 	 * Means that permalink links are enabled and index.php is in the URL.
       
   709 	 *
       
   710 	 * @since 1.5.0
       
   711 	 * @access public
       
   712 	 *
       
   713 	 * @return bool
       
   714 	 */
       
   715 	function using_index_permalinks() {
       
   716 		if (empty($this->permalink_structure)) {
       
   717 			return false;
       
   718 		}
       
   719 
       
   720 		// If the index is not in the permalink, we're using mod_rewrite.
       
   721 		if (preg_match('#^/*' . $this->index . '#', $this->permalink_structure)) {
       
   722 			return true;
       
   723 		}
       
   724 
       
   725 		return false;
       
   726 	}
       
   727 
       
   728 	/**
       
   729 	 * Whether permalinks are being used and rewrite module is enabled.
       
   730 	 *
       
   731 	 * Using permalinks and index.php is not in the URL.
       
   732 	 *
       
   733 	 * @since 1.5.0
       
   734 	 * @access public
       
   735 	 *
       
   736 	 * @return bool
       
   737 	 */
       
   738 	function using_mod_rewrite_permalinks() {
       
   739 		if ( $this->using_permalinks() && ! $this->using_index_permalinks())
       
   740 			return true;
       
   741 		else
       
   742 			return false;
       
   743 	}
       
   744 
       
   745 	/**
       
   746 	 * Index for matches for usage in preg_*() functions.
       
   747 	 *
       
   748 	 * The format of the string is, with empty matches property value, '$NUM'.
       
   749 	 * The 'NUM' will be replaced with the value in the $number parameter. With
       
   750 	 * the matches property not empty, the value of the returned string will
       
   751 	 * contain that value of the matches property. The format then will be
       
   752 	 * '$MATCHES[NUM]', with MATCHES as the value in the property and NUM the
       
   753 	 * value of the $number parameter.
       
   754 	 *
       
   755 	 * @since 1.5.0
       
   756 	 * @access public
       
   757 	 *
       
   758 	 * @param int $number Index number.
       
   759 	 * @return string
       
   760 	 */
       
   761 	function preg_index($number) {
       
   762 		$match_prefix = '$';
       
   763 		$match_suffix = '';
       
   764 
       
   765 		if ( ! empty($this->matches) ) {
       
   766 			$match_prefix = '$' . $this->matches . '[';
       
   767 			$match_suffix = ']';
       
   768 		}
       
   769 
       
   770 		return "$match_prefix$number$match_suffix";
       
   771 	}
       
   772 
       
   773 	/**
       
   774 	 * Retrieve all page and attachments for pages URIs.
       
   775 	 *
       
   776 	 * The attachments are for those that have pages as parents and will be
       
   777 	 * retrieved.
       
   778 	 *
       
   779 	 * @since 2.5.0
       
   780 	 * @access public
       
   781 	 *
       
   782 	 * @return array Array of page URIs as first element and attachment URIs as second element.
       
   783 	 */
       
   784 	function page_uri_index() {
       
   785 		global $wpdb;
       
   786 
       
   787 		//get pages in order of hierarchy, i.e. children after parents
       
   788 		$posts = get_page_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page'"));
       
   789 		//now reverse it, because we need parents after children for rewrite rules to work properly
       
   790 		$posts = array_reverse($posts, true);
       
   791 
       
   792 		$page_uris = array();
       
   793 		$page_attachment_uris = array();
       
   794 
       
   795 		if ( !$posts )
       
   796 			return array( array(), array() );
       
   797 
       
   798 		foreach ($posts as $id => $post) {
       
   799 			// URL => page name
       
   800 			$uri = get_page_uri($id);
       
   801 			$attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id ));
       
   802 			if ( $attachments ) {
       
   803 				foreach ( $attachments as $attachment ) {
       
   804 					$attach_uri = get_page_uri($attachment->ID);
       
   805 					$page_attachment_uris[$attach_uri] = $attachment->ID;
       
   806 				}
       
   807 			}
       
   808 
       
   809 			$page_uris[$uri] = $id;
       
   810 		}
       
   811 
       
   812 		return array( $page_uris, $page_attachment_uris );
       
   813 	}
       
   814 
       
   815 	/**
       
   816 	 * Retrieve all of the rewrite rules for pages.
       
   817 	 *
       
   818 	 * If the 'use_verbose_page_rules' property is false, then there will only
       
   819 	 * be a single rewrite rule for pages for those matching '%pagename%'. With
       
   820 	 * the property set to true, the attachments and the pages will be added for
       
   821 	 * each individual attachment URI and page URI, respectively.
       
   822 	 *
       
   823 	 * @since 1.5.0
       
   824 	 * @access public
       
   825 	 *
       
   826 	 * @return array
       
   827 	 */
       
   828 	function page_rewrite_rules() {
       
   829 		$rewrite_rules = array();
       
   830 		$page_structure = $this->get_page_permastruct();
       
   831 
       
   832 		if ( ! $this->use_verbose_page_rules ) {
       
   833 			$this->add_rewrite_tag('%pagename%', "(.+?)", 'pagename=');
       
   834 			$rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
       
   835 			return $rewrite_rules;
       
   836 		}
       
   837 
       
   838 		$page_uris = $this->page_uri_index();
       
   839 		$uris = $page_uris[0];
       
   840 		$attachment_uris = $page_uris[1];
       
   841 
       
   842 		if( is_array( $attachment_uris ) ) {
       
   843 			foreach ($attachment_uris as $uri => $pagename) {
       
   844 				$this->add_rewrite_tag('%pagename%', "($uri)", 'attachment=');
       
   845 				$rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
       
   846 			}
       
   847 		}
       
   848 		if( is_array( $uris ) ) {
       
   849 			foreach ($uris as $uri => $pagename) {
       
   850 				$this->add_rewrite_tag('%pagename%', "($uri)", 'pagename=');
       
   851 				$rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
       
   852 			}
       
   853 		}
       
   854 
       
   855 		return $rewrite_rules;
       
   856 	}
       
   857 
       
   858 	/**
       
   859 	 * Retrieve date permalink structure, with year, month, and day.
       
   860 	 *
       
   861 	 * The permalink structure for the date, if not set already depends on the
       
   862 	 * permalink structure. It can be one of three formats. The first is year,
       
   863 	 * month, day; the second is day, month, year; and the last format is month,
       
   864 	 * day, year. These are matched against the permalink structure for which
       
   865 	 * one is used. If none matches, then the default will be used, which is
       
   866 	 * year, month, day.
       
   867 	 *
       
   868 	 * Prevents post ID and date permalinks from overlapping. In the case of
       
   869 	 * post_id, the date permalink will be prepended with front permalink with
       
   870 	 * 'date/' before the actual permalink to form the complete date permalink
       
   871 	 * structure.
       
   872 	 *
       
   873 	 * @since 1.5.0
       
   874 	 * @access public
       
   875 	 *
       
   876 	 * @return bool|string False on no permalink structure. Date permalink structure.
       
   877 	 */
       
   878 	function get_date_permastruct() {
       
   879 		if (isset($this->date_structure)) {
       
   880 			return $this->date_structure;
       
   881 		}
       
   882 
       
   883 		if (empty($this->permalink_structure)) {
       
   884 			$this->date_structure = '';
       
   885 			return false;
       
   886 		}
       
   887 
       
   888 		// The date permalink must have year, month, and day separated by slashes.
       
   889 		$endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%');
       
   890 
       
   891 		$this->date_structure = '';
       
   892 		$date_endian = '';
       
   893 
       
   894 		foreach ($endians as $endian) {
       
   895 			if (false !== strpos($this->permalink_structure, $endian)) {
       
   896 				$date_endian= $endian;
       
   897 				break;
       
   898 			}
       
   899 		}
       
   900 
       
   901 		if ( empty($date_endian) )
       
   902 			$date_endian = '%year%/%monthnum%/%day%';
       
   903 
       
   904 		// Do not allow the date tags and %post_id% to overlap in the permalink
       
   905 		// structure. If they do, move the date tags to $front/date/.
       
   906 		$front = $this->front;
       
   907 		preg_match_all('/%.+?%/', $this->permalink_structure, $tokens);
       
   908 		$tok_index = 1;
       
   909 		foreach ( (array) $tokens[0] as $token) {
       
   910 			if ( ($token == '%post_id%') && ($tok_index <= 3) ) {
       
   911 				$front = $front . 'date/';
       
   912 				break;
       
   913 			}
       
   914 			$tok_index++;
       
   915 		}
       
   916 
       
   917 		$this->date_structure = $front . $date_endian;
       
   918 
       
   919 		return $this->date_structure;
       
   920 	}
       
   921 
       
   922 	/**
       
   923 	 * Retrieve the year permalink structure without month and day.
       
   924 	 *
       
   925 	 * Gets the date permalink structure and strips out the month and day
       
   926 	 * permalink structures.
       
   927 	 *
       
   928 	 * @since 1.5.0
       
   929 	 * @access public
       
   930 	 *
       
   931 	 * @return bool|string False on failure. Year structure on success.
       
   932 	 */
       
   933 	function get_year_permastruct() {
       
   934 		$structure = $this->get_date_permastruct($this->permalink_structure);
       
   935 
       
   936 		if (empty($structure)) {
       
   937 			return false;
       
   938 		}
       
   939 
       
   940 		$structure = str_replace('%monthnum%', '', $structure);
       
   941 		$structure = str_replace('%day%', '', $structure);
       
   942 
       
   943 		$structure = preg_replace('#/+#', '/', $structure);
       
   944 
       
   945 		return $structure;
       
   946 	}
       
   947 
       
   948 	/**
       
   949 	 * Retrieve the month permalink structure without day and with year.
       
   950 	 *
       
   951 	 * Gets the date permalink structure and strips out the day permalink
       
   952 	 * structures. Keeps the year permalink structure.
       
   953 	 *
       
   954 	 * @since 1.5.0
       
   955 	 * @access public
       
   956 	 *
       
   957 	 * @return bool|string False on failure. Year/Month structure on success.
       
   958 	 */
       
   959 	function get_month_permastruct() {
       
   960 		$structure = $this->get_date_permastruct($this->permalink_structure);
       
   961 
       
   962 		if (empty($structure)) {
       
   963 			return false;
       
   964 		}
       
   965 
       
   966 		$structure = str_replace('%day%', '', $structure);
       
   967 
       
   968 		$structure = preg_replace('#/+#', '/', $structure);
       
   969 
       
   970 		return $structure;
       
   971 	}
       
   972 
       
   973 	/**
       
   974 	 * Retrieve the day permalink structure with month and year.
       
   975 	 *
       
   976 	 * Keeps date permalink structure with all year, month, and day.
       
   977 	 *
       
   978 	 * @since 1.5.0
       
   979 	 * @access public
       
   980 	 *
       
   981 	 * @return bool|string False on failure. Year/Month/Day structure on success.
       
   982 	 */
       
   983 	function get_day_permastruct() {
       
   984 		return $this->get_date_permastruct($this->permalink_structure);
       
   985 	}
       
   986 
       
   987 	/**
       
   988 	 * Retrieve the permalink structure for categories.
       
   989 	 *
       
   990 	 * If the category_base property has no value, then the category structure
       
   991 	 * will have the front property value, followed by 'category', and finally
       
   992 	 * '%category%'. If it does, then the root property will be used, along with
       
   993 	 * the category_base property value.
       
   994 	 *
       
   995 	 * @since 1.5.0
       
   996 	 * @access public
       
   997 	 *
       
   998 	 * @return bool|string False on failure. Category permalink structure.
       
   999 	 */
       
  1000 	function get_category_permastruct() {
       
  1001 		if (isset($this->category_structure)) {
       
  1002 			return $this->category_structure;
       
  1003 		}
       
  1004 
       
  1005 		if (empty($this->permalink_structure)) {
       
  1006 			$this->category_structure = '';
       
  1007 			return false;
       
  1008 		}
       
  1009 
       
  1010 		if (empty($this->category_base))
       
  1011 			$this->category_structure = trailingslashit( $this->front . 'category' );
       
  1012 		else
       
  1013 			$this->category_structure = trailingslashit( '/' . $this->root . $this->category_base );
       
  1014 
       
  1015 		$this->category_structure .= '%category%';
       
  1016 
       
  1017 		return $this->category_structure;
       
  1018 	}
       
  1019 
       
  1020 	/**
       
  1021 	 * Retrieve the permalink structure for tags.
       
  1022 	 *
       
  1023 	 * If the tag_base property has no value, then the tag structure will have
       
  1024 	 * the front property value, followed by 'tag', and finally '%tag%'. If it
       
  1025 	 * does, then the root property will be used, along with the tag_base
       
  1026 	 * property value.
       
  1027 	 *
       
  1028 	 * @since 2.3.0
       
  1029 	 * @access public
       
  1030 	 *
       
  1031 	 * @return bool|string False on failure. Tag permalink structure.
       
  1032 	 */
       
  1033 	function get_tag_permastruct() {
       
  1034 		if (isset($this->tag_structure)) {
       
  1035 			return $this->tag_structure;
       
  1036 		}
       
  1037 
       
  1038 		if (empty($this->permalink_structure)) {
       
  1039 			$this->tag_structure = '';
       
  1040 			return false;
       
  1041 		}
       
  1042 
       
  1043 		if (empty($this->tag_base))
       
  1044 			$this->tag_structure = trailingslashit( $this->front . 'tag' );
       
  1045 		else
       
  1046 			$this->tag_structure = trailingslashit( '/' . $this->root . $this->tag_base );
       
  1047 
       
  1048 		$this->tag_structure .= '%tag%';
       
  1049 
       
  1050 		return $this->tag_structure;
       
  1051 	}
       
  1052 
       
  1053 	/**
       
  1054 	 * Retrieve extra permalink structure by name.
       
  1055 	 *
       
  1056 	 * @since unknown
       
  1057 	 * @access public
       
  1058 	 *
       
  1059 	 * @param string $name Permalink structure name.
       
  1060 	 * @return string|bool False if not found. Permalink structure string.
       
  1061 	 */
       
  1062 	function get_extra_permastruct($name) {
       
  1063 		if ( isset($this->extra_permastructs[$name]) )
       
  1064 			return $this->extra_permastructs[$name];
       
  1065 		return false;
       
  1066 	}
       
  1067 
       
  1068 	/**
       
  1069 	 * Retrieve the author permalink structure.
       
  1070 	 *
       
  1071 	 * The permalink structure is front property, author base, and finally
       
  1072 	 * '/%author%'. Will set the author_structure property and then return it
       
  1073 	 * without attempting to set the value again.
       
  1074 	 *
       
  1075 	 * @since 1.5.0
       
  1076 	 * @access public
       
  1077 	 *
       
  1078 	 * @return string|bool False if not found. Permalink structure string.
       
  1079 	 */
       
  1080 	function get_author_permastruct() {
       
  1081 		if (isset($this->author_structure)) {
       
  1082 			return $this->author_structure;
       
  1083 		}
       
  1084 
       
  1085 		if (empty($this->permalink_structure)) {
       
  1086 			$this->author_structure = '';
       
  1087 			return false;
       
  1088 		}
       
  1089 
       
  1090 		$this->author_structure = $this->front . $this->author_base . '/%author%';
       
  1091 
       
  1092 		return $this->author_structure;
       
  1093 	}
       
  1094 
       
  1095 	/**
       
  1096 	 * Retrieve the search permalink structure.
       
  1097 	 *
       
  1098 	 * The permalink structure is root property, search base, and finally
       
  1099 	 * '/%search%'. Will set the search_structure property and then return it
       
  1100 	 * without attempting to set the value again.
       
  1101 	 *
       
  1102 	 * @since 1.5.0
       
  1103 	 * @access public
       
  1104 	 *
       
  1105 	 * @return string|bool False if not found. Permalink structure string.
       
  1106 	 */
       
  1107 	function get_search_permastruct() {
       
  1108 		if (isset($this->search_structure)) {
       
  1109 			return $this->search_structure;
       
  1110 		}
       
  1111 
       
  1112 		if (empty($this->permalink_structure)) {
       
  1113 			$this->search_structure = '';
       
  1114 			return false;
       
  1115 		}
       
  1116 
       
  1117 		$this->search_structure = $this->root . $this->search_base . '/%search%';
       
  1118 
       
  1119 		return $this->search_structure;
       
  1120 	}
       
  1121 
       
  1122 	/**
       
  1123 	 * Retrieve the page permalink structure.
       
  1124 	 *
       
  1125 	 * The permalink structure is root property, and '%pagename%'. Will set the
       
  1126 	 * page_structure property and then return it without attempting to set the
       
  1127 	 * value again.
       
  1128 	 *
       
  1129 	 * @since 1.5.0
       
  1130 	 * @access public
       
  1131 	 *
       
  1132 	 * @return string|bool False if not found. Permalink structure string.
       
  1133 	 */
       
  1134 	function get_page_permastruct() {
       
  1135 		if (isset($this->page_structure)) {
       
  1136 			return $this->page_structure;
       
  1137 		}
       
  1138 
       
  1139 		if (empty($this->permalink_structure)) {
       
  1140 			$this->page_structure = '';
       
  1141 			return false;
       
  1142 		}
       
  1143 
       
  1144 		$this->page_structure = $this->root . '%pagename%';
       
  1145 
       
  1146 		return $this->page_structure;
       
  1147 	}
       
  1148 
       
  1149 	/**
       
  1150 	 * Retrieve the feed permalink structure.
       
  1151 	 *
       
  1152 	 * The permalink structure is root property, feed base, and finally
       
  1153 	 * '/%feed%'. Will set the feed_structure property and then return it
       
  1154 	 * without attempting to set the value again.
       
  1155 	 *
       
  1156 	 * @since 1.5.0
       
  1157 	 * @access public
       
  1158 	 *
       
  1159 	 * @return string|bool False if not found. Permalink structure string.
       
  1160 	 */
       
  1161 	function get_feed_permastruct() {
       
  1162 		if (isset($this->feed_structure)) {
       
  1163 			return $this->feed_structure;
       
  1164 		}
       
  1165 
       
  1166 		if (empty($this->permalink_structure)) {
       
  1167 			$this->feed_structure = '';
       
  1168 			return false;
       
  1169 		}
       
  1170 
       
  1171 		$this->feed_structure = $this->root . $this->feed_base . '/%feed%';
       
  1172 
       
  1173 		return $this->feed_structure;
       
  1174 	}
       
  1175 
       
  1176 	/**
       
  1177 	 * Retrieve the comment feed permalink structure.
       
  1178 	 *
       
  1179 	 * The permalink structure is root property, comment base property, feed
       
  1180 	 * base and finally '/%feed%'. Will set the comment_feed_structure property
       
  1181 	 * and then return it without attempting to set the value again.
       
  1182 	 *
       
  1183 	 * @since 1.5.0
       
  1184 	 * @access public
       
  1185 	 *
       
  1186 	 * @return string|bool False if not found. Permalink structure string.
       
  1187 	 */
       
  1188 	function get_comment_feed_permastruct() {
       
  1189 		if (isset($this->comment_feed_structure)) {
       
  1190 			return $this->comment_feed_structure;
       
  1191 		}
       
  1192 
       
  1193 		if (empty($this->permalink_structure)) {
       
  1194 			$this->comment_feed_structure = '';
       
  1195 			return false;
       
  1196 		}
       
  1197 
       
  1198 		$this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%';
       
  1199 
       
  1200 		return $this->comment_feed_structure;
       
  1201 	}
       
  1202 
       
  1203 	/**
       
  1204 	 * Append or update tag, pattern, and query for replacement.
       
  1205 	 *
       
  1206 	 * If the tag already exists, replace the existing pattern and query for
       
  1207 	 * that tag, otherwise add the new tag, pattern, and query to the end of the
       
  1208 	 * arrays.
       
  1209 	 *
       
  1210 	 * @internal What is the purpose of this function again? Need to finish long
       
  1211 	 *           description.
       
  1212 	 *
       
  1213 	 * @since 1.5.0
       
  1214 	 * @access public
       
  1215 	 *
       
  1216 	 * @param string $tag Append tag to rewritecode property array.
       
  1217 	 * @param string $pattern Append pattern to rewritereplace property array.
       
  1218 	 * @param string $query Append query to queryreplace property array.
       
  1219 	 */
       
  1220 	function add_rewrite_tag($tag, $pattern, $query) {
       
  1221 		$position = array_search($tag, $this->rewritecode);
       
  1222 		if ( false !== $position && null !== $position ) {
       
  1223 			$this->rewritereplace[$position] = $pattern;
       
  1224 			$this->queryreplace[$position] = $query;
       
  1225 		} else {
       
  1226 			$this->rewritecode[] = $tag;
       
  1227 			$this->rewritereplace[] = $pattern;
       
  1228 			$this->queryreplace[] = $query;
       
  1229 		}
       
  1230 	}
       
  1231 
       
  1232 	/**
       
  1233 	 * Generate the rules from permalink structure.
       
  1234 	 *
       
  1235 	 * The main WP_Rewrite function for building the rewrite rule list. The
       
  1236 	 * contents of the function is a mix of black magic and regular expressions,
       
  1237 	 * so best just ignore the contents and move to the parameters.
       
  1238 	 *
       
  1239 	 * @since 1.5.0
       
  1240 	 * @access public
       
  1241 	 *
       
  1242 	 * @param string $permalink_structure The permalink structure.
       
  1243 	 * @param int $ep_mask Optional, default is EP_NONE. Endpoint constant, see EP_* constants.
       
  1244 	 * @param bool $paged Optional, default is true. Whether permalink request is paged.
       
  1245 	 * @param bool $feed Optional, default is true. Whether for feed.
       
  1246 	 * @param bool $forcomments Optional, default is false. Whether for comments.
       
  1247 	 * @param bool $walk_dirs Optional, default is true. Whether to create list of directories to walk over.
       
  1248 	 * @param bool $endpoints Optional, default is true. Whether endpoints are enabled.
       
  1249 	 * @return array Rewrite rule list.
       
  1250 	 */
       
  1251 	function generate_rewrite_rules($permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true) {
       
  1252 		//build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/?
       
  1253 		$feedregex2 = '';
       
  1254 		foreach ( (array) $this->feeds as $feed_name) {
       
  1255 			$feedregex2 .= $feed_name . '|';
       
  1256 		}
       
  1257 		$feedregex2 = '(' . trim($feedregex2, '|') .  ')/?$';
       
  1258 		//$feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom
       
  1259 		//and <permalink>/atom are both possible
       
  1260 		$feedregex = $this->feed_base  . '/' . $feedregex2;
       
  1261 
       
  1262 		//build a regex to match the trackback and page/xx parts of URLs
       
  1263 		$trackbackregex = 'trackback/?$';
       
  1264 		$pageregex = 'page/?([0-9]{1,})/?$';
       
  1265 		$commentregex = 'comment-page-([0-9]{1,})/?$';
       
  1266 
       
  1267 		//build up an array of endpoint regexes to append => queries to append
       
  1268 		if ($endpoints) {
       
  1269 			$ep_query_append = array ();
       
  1270 			foreach ( (array) $this->endpoints as $endpoint) {
       
  1271 				//match everything after the endpoint name, but allow for nothing to appear there
       
  1272 				$epmatch = $endpoint[1] . '(/(.*))?/?$';
       
  1273 				//this will be appended on to the rest of the query for each dir
       
  1274 				$epquery = '&' . $endpoint[1] . '=';
       
  1275 				$ep_query_append[$epmatch] = array ( $endpoint[0], $epquery );
       
  1276 			}
       
  1277 		}
       
  1278 
       
  1279 		//get everything up to the first rewrite tag
       
  1280 		$front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
       
  1281 		//build an array of the tags (note that said array ends up being in $tokens[0])
       
  1282 		preg_match_all('/%.+?%/', $permalink_structure, $tokens);
       
  1283 
       
  1284 		$num_tokens = count($tokens[0]);
       
  1285 
       
  1286 		$index = $this->index; //probably 'index.php'
       
  1287 		$feedindex = $index;
       
  1288 		$trackbackindex = $index;
       
  1289 		//build a list from the rewritecode and queryreplace arrays, that will look something like
       
  1290 		//tagname=$matches[i] where i is the current $i
       
  1291 		for ($i = 0; $i < $num_tokens; ++$i) {
       
  1292 			if (0 < $i) {
       
  1293 				$queries[$i] = $queries[$i - 1] . '&';
       
  1294 			} else {
       
  1295 				$queries[$i] = '';
       
  1296 			}
       
  1297 
       
  1298 			$query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1);
       
  1299 			$queries[$i] .= $query_token;
       
  1300 		}
       
  1301 
       
  1302 		//get the structure, minus any cruft (stuff that isn't tags) at the front
       
  1303 		$structure = $permalink_structure;
       
  1304 		if ($front != '/') {
       
  1305 			$structure = str_replace($front, '', $structure);
       
  1306 		}
       
  1307 		//create a list of dirs to walk over, making rewrite rules for each level
       
  1308 		//so for example, a $structure of /%year%/%month%/%postname% would create
       
  1309 		//rewrite rules for /%year%/, /%year%/%month%/ and /%year%/%month%/%postname%
       
  1310 		$structure = trim($structure, '/');
       
  1311 		if ($walk_dirs) {
       
  1312 			$dirs = explode('/', $structure);
       
  1313 		} else {
       
  1314 			$dirs[] = $structure;
       
  1315 		}
       
  1316 		$num_dirs = count($dirs);
       
  1317 
       
  1318 		//strip slashes from the front of $front
       
  1319 		$front = preg_replace('|^/+|', '', $front);
       
  1320 
       
  1321 		//the main workhorse loop
       
  1322 		$post_rewrite = array();
       
  1323 		$struct = $front;
       
  1324 		for ($j = 0; $j < $num_dirs; ++$j) {
       
  1325 			//get the struct for this dir, and trim slashes off the front
       
  1326 			$struct .= $dirs[$j] . '/'; //accumulate. see comment near explode('/', $structure) above
       
  1327 			$struct = ltrim($struct, '/');
       
  1328 			//replace tags with regexes
       
  1329 			$match = str_replace($this->rewritecode, $this->rewritereplace, $struct);
       
  1330 			//make a list of tags, and store how many there are in $num_toks
       
  1331 			$num_toks = preg_match_all('/%.+?%/', $struct, $toks);
       
  1332 			//get the 'tagname=$matches[i]'
       
  1333 			$query = ( isset($queries) && is_array($queries) ) ? $queries[$num_toks - 1] : '';
       
  1334 
       
  1335 			//set up $ep_mask_specific which is used to match more specific URL types
       
  1336 			switch ($dirs[$j]) {
       
  1337 				case '%year%': $ep_mask_specific = EP_YEAR; break;
       
  1338 				case '%monthnum%': $ep_mask_specific = EP_MONTH; break;
       
  1339 				case '%day%': $ep_mask_specific = EP_DAY; break;
       
  1340 			}
       
  1341 
       
  1342 			//create query for /page/xx
       
  1343 			$pagematch = $match . $pageregex;
       
  1344 			$pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1);
       
  1345 
       
  1346 			//create query for /comment-page-xx
       
  1347 			$commentmatch = $match . $commentregex;
       
  1348 			$commentquery = $index . '?' . $query . '&cpage=' . $this->preg_index($num_toks + 1);
       
  1349 
       
  1350 			if ( get_option('page_on_front') ) {
       
  1351 				//create query for Root /comment-page-xx
       
  1352 				$rootcommentmatch = $match . $commentregex;
       
  1353 				$rootcommentquery = $index . '?' . $query . '&page_id=' . get_option('page_on_front') . '&cpage=' . $this->preg_index($num_toks + 1);
       
  1354 			}
       
  1355 
       
  1356 			//create query for /feed/(feed|atom|rss|rss2|rdf)
       
  1357 			$feedmatch = $match . $feedregex;
       
  1358 			$feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
       
  1359 
       
  1360 			//create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex)
       
  1361 			$feedmatch2 = $match . $feedregex2;
       
  1362 			$feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
       
  1363 
       
  1364 			//if asked to, turn the feed queries into comment feed ones
       
  1365 			if ($forcomments) {
       
  1366 				$feedquery .= '&withcomments=1';
       
  1367 				$feedquery2 .= '&withcomments=1';
       
  1368 			}
       
  1369 
       
  1370 			//start creating the array of rewrites for this dir
       
  1371 			$rewrite = array();
       
  1372 			if ($feed) //...adding on /feed/ regexes => queries
       
  1373 				$rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2);
       
  1374 			if ($paged) //...and /page/xx ones
       
  1375 				$rewrite = array_merge($rewrite, array($pagematch => $pagequery));
       
  1376 
       
  1377 			//only on pages with comments add ../comment-page-xx/
       
  1378 			if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask || EP_NONE & $ep_mask )
       
  1379 				$rewrite = array_merge($rewrite, array($commentmatch => $commentquery));
       
  1380 			else if ( EP_ROOT & $ep_mask && get_option('page_on_front') )
       
  1381 				$rewrite = array_merge($rewrite, array($rootcommentmatch => $rootcommentquery));
       
  1382 
       
  1383 			//do endpoints
       
  1384 			if ($endpoints) {
       
  1385 				foreach ( (array) $ep_query_append as $regex => $ep) {
       
  1386 					//add the endpoints on if the mask fits
       
  1387 					if ($ep[0] & $ep_mask || $ep[0] & $ep_mask_specific) {
       
  1388 						$rewrite[$match . $regex] = $index . '?' . $query . $ep[1] . $this->preg_index($num_toks + 2);
       
  1389 					}
       
  1390 				}
       
  1391 			}
       
  1392 
       
  1393 			//if we've got some tags in this dir
       
  1394 			if ($num_toks) {
       
  1395 				$post = false;
       
  1396 				$page = false;
       
  1397 
       
  1398 				//check to see if this dir is permalink-level: i.e. the structure specifies an
       
  1399 				//individual post. Do this by checking it contains at least one of 1) post name,
       
  1400 				//2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and
       
  1401 				//minute all present). Set these flags now as we need them for the endpoints.
       
  1402 				if (strpos($struct, '%postname%') !== false || strpos($struct, '%post_id%') !== false
       
  1403 						|| strpos($struct, '%pagename%') !== false
       
  1404 						|| (strpos($struct, '%year%') !== false && strpos($struct, '%monthnum%') !== false && strpos($struct, '%day%') !== false && strpos($struct, '%hour%') !== false && strpos($struct, '%minute%') !== false && strpos($struct, '%second%') !== false)) {
       
  1405 					$post = true;
       
  1406 					if (strpos($struct, '%pagename%') !== false)
       
  1407 						$page = true;
       
  1408 				}
       
  1409 
       
  1410 				//if we're creating rules for a permalink, do all the endpoints like attachments etc
       
  1411 				if ($post) {
       
  1412 					$post = true;
       
  1413 					//create query and regex for trackback
       
  1414 					$trackbackmatch = $match . $trackbackregex;
       
  1415 					$trackbackquery = $trackbackindex . '?' . $query . '&tb=1';
       
  1416 					//trim slashes from the end of the regex for this dir
       
  1417 					$match = rtrim($match, '/');
       
  1418 					//get rid of brackets
       
  1419 					$submatchbase = str_replace(array('(',')'),'',$match);
       
  1420 
       
  1421 					//add a rule for at attachments, which take the form of <permalink>/some-text
       
  1422 					$sub1 = $submatchbase . '/([^/]+)/';
       
  1423 					$sub1tb = $sub1 . $trackbackregex; //add trackback regex <permalink>/trackback/...
       
  1424 					$sub1feed = $sub1 . $feedregex; //and <permalink>/feed/(atom|...)
       
  1425 					$sub1feed2 = $sub1 . $feedregex2; //and <permalink>/(feed|atom...)
       
  1426 					$sub1comment = $sub1 . $commentregex; //and <permalink>/comment-page-xx
       
  1427 					//add an ? as we don't have to match that last slash, and finally a $ so we
       
  1428 					//match to the end of the URL
       
  1429 
       
  1430 					//add another rule to match attachments in the explicit form:
       
  1431 					//<permalink>/attachment/some-text
       
  1432 					$sub2 = $submatchbase . '/attachment/([^/]+)/';
       
  1433 					$sub2tb = $sub2 . $trackbackregex; //and add trackbacks <permalink>/attachment/trackback
       
  1434 					$sub2feed = $sub2 . $feedregex;    //feeds, <permalink>/attachment/feed/(atom|...)
       
  1435 					$sub2feed2 = $sub2 . $feedregex2;  //and feeds again on to this <permalink>/attachment/(feed|atom...)
       
  1436 					$sub2comment = $sub2 . $commentregex; //and <permalink>/comment-page-xx
       
  1437 
       
  1438 					//create queries for these extra tag-ons we've just dealt with
       
  1439 					$subquery = $index . '?attachment=' . $this->preg_index(1);
       
  1440 					$subtbquery = $subquery . '&tb=1';
       
  1441 					$subfeedquery = $subquery . '&feed=' . $this->preg_index(2);
       
  1442 					$subcommentquery = $subquery . '&cpage=' . $this->preg_index(2);
       
  1443 
       
  1444 					//do endpoints for attachments
       
  1445 					if ( !empty($endpoint) ) { foreach ( (array) $ep_query_append as $regex => $ep ) {
       
  1446 						if ($ep[0] & EP_ATTACHMENT) {
       
  1447 							$rewrite[$sub1 . $regex] = $subquery . '?' . $ep[1] . $this->preg_index(2);
       
  1448 							$rewrite[$sub2 . $regex] = $subquery . '?' . $ep[1] . $this->preg_index(2);
       
  1449 						}
       
  1450 					} }
       
  1451 
       
  1452 					//now we've finished with endpoints, finish off the $sub1 and $sub2 matches
       
  1453 					$sub1 .= '?$';
       
  1454 					$sub2 .= '?$';
       
  1455 
       
  1456 					//allow URLs like <permalink>/2 for <permalink>/page/2
       
  1457 					$match = $match . '(/[0-9]+)?/?$';
       
  1458 					$query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1);
       
  1459 				} else { //not matching a permalink so this is a lot simpler
       
  1460 					//close the match and finalise the query
       
  1461 					$match .= '?$';
       
  1462 					$query = $index . '?' . $query;
       
  1463 				}
       
  1464 
       
  1465 				//create the final array for this dir by joining the $rewrite array (which currently
       
  1466 				//only contains rules/queries for trackback, pages etc) to the main regex/query for
       
  1467 				//this dir
       
  1468 				$rewrite = array_merge($rewrite, array($match => $query));
       
  1469 
       
  1470 				//if we're matching a permalink, add those extras (attachments etc) on
       
  1471 				if ($post) {
       
  1472 					//add trackback
       
  1473 					$rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite);
       
  1474 
       
  1475 					//add regexes/queries for attachments, attachment trackbacks and so on
       
  1476 					if ( ! $page ) //require <permalink>/attachment/stuff form for pages because of confusion with subpages
       
  1477 						$rewrite = array_merge($rewrite, array($sub1 => $subquery, $sub1tb => $subtbquery, $sub1feed => $subfeedquery, $sub1feed2 => $subfeedquery, $sub1comment => $subcommentquery));
       
  1478 					$rewrite = array_merge(array($sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery, $sub2comment => $subcommentquery), $rewrite);
       
  1479 				}
       
  1480 			} //if($num_toks)
       
  1481 			//add the rules for this dir to the accumulating $post_rewrite
       
  1482 			$post_rewrite = array_merge($rewrite, $post_rewrite);
       
  1483 		} //foreach ($dir)
       
  1484 		return $post_rewrite; //the finished rules. phew!
       
  1485 	}
       
  1486 
       
  1487 	/**
       
  1488 	 * Generate Rewrite rules with permalink structure and walking directory only.
       
  1489 	 *
       
  1490 	 * Shorten version of {@link WP_Rewrite::generate_rewrite_rules()} that
       
  1491 	 * allows for shorter list of parameters. See the method for longer
       
  1492 	 * description of what generating rewrite rules does.
       
  1493 	 *
       
  1494 	 * @uses WP_Rewrite::generate_rewrite_rules() See for long description and rest of parameters.
       
  1495 	 * @since 1.5.0
       
  1496 	 * @access public
       
  1497 	 *
       
  1498 	 * @param string $permalink_structure The permalink structure to generate rules.
       
  1499 	 * @param bool $walk_dirs Optional, default is false. Whether to create list of directories to walk over.
       
  1500 	 * @return array
       
  1501 	 */
       
  1502 	function generate_rewrite_rule($permalink_structure, $walk_dirs = false) {
       
  1503 		return $this->generate_rewrite_rules($permalink_structure, EP_NONE, false, false, false, $walk_dirs);
       
  1504 	}
       
  1505 
       
  1506 	/**
       
  1507 	 * Construct rewrite matches and queries from permalink structure.
       
  1508 	 *
       
  1509 	 * Runs the action 'generate_rewrite_rules' with the parameter that is an
       
  1510 	 * reference to the current WP_Rewrite instance to further manipulate the
       
  1511 	 * permalink structures and rewrite rules. Runs the 'rewrite_rules_array'
       
  1512 	 * filter on the full rewrite rule array.
       
  1513 	 *
       
  1514 	 * There are two ways to manipulate the rewrite rules, one by hooking into
       
  1515 	 * the 'generate_rewrite_rules' action and gaining full control of the
       
  1516 	 * object or just manipulating the rewrite rule array before it is passed
       
  1517 	 * from the function.
       
  1518 	 *
       
  1519 	 * @since 1.5.0
       
  1520 	 * @access public
       
  1521 	 *
       
  1522 	 * @return array An associate array of matches and queries.
       
  1523 	 */
       
  1524 	function rewrite_rules() {
       
  1525 		$rewrite = array();
       
  1526 
       
  1527 		if (empty($this->permalink_structure)) {
       
  1528 			return $rewrite;
       
  1529 		}
       
  1530 
       
  1531 		// robots.txt
       
  1532 		$robots_rewrite = array('robots\.txt$' => $this->index . '?robots=1');
       
  1533 
       
  1534 		//Default Feed rules - These are require to allow for the direct access files to work with permalink structure starting with %category%
       
  1535 		$default_feeds = array(	'.*wp-atom.php$'	=>	$this->index .'?feed=atom',
       
  1536 								'.*wp-rdf.php$'	=>	$this->index .'?feed=rdf',
       
  1537 								'.*wp-rss.php$'	=>	$this->index .'?feed=rss',
       
  1538 								'.*wp-rss2.php$'	=>	$this->index .'?feed=rss2',
       
  1539 								'.*wp-feed.php$'	=>	$this->index .'?feed=feed',
       
  1540 								'.*wp-commentsrss2.php$'	=>	$this->index . '?feed=rss2&withcomments=1');
       
  1541 
       
  1542 		// Post
       
  1543 		$post_rewrite = $this->generate_rewrite_rules($this->permalink_structure, EP_PERMALINK);
       
  1544 		$post_rewrite = apply_filters('post_rewrite_rules', $post_rewrite);
       
  1545 
       
  1546 		// Date
       
  1547 		$date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct(), EP_DATE);
       
  1548 		$date_rewrite = apply_filters('date_rewrite_rules', $date_rewrite);
       
  1549 
       
  1550 		// Root
       
  1551 		$root_rewrite = $this->generate_rewrite_rules($this->root . '/', EP_ROOT);
       
  1552 		$root_rewrite = apply_filters('root_rewrite_rules', $root_rewrite);
       
  1553 
       
  1554 		// Comments
       
  1555 		$comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, EP_COMMENTS, true, true, true, false);
       
  1556 		$comments_rewrite = apply_filters('comments_rewrite_rules', $comments_rewrite);
       
  1557 
       
  1558 		// Search
       
  1559 		$search_structure = $this->get_search_permastruct();
       
  1560 		$search_rewrite = $this->generate_rewrite_rules($search_structure, EP_SEARCH);
       
  1561 		$search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite);
       
  1562 
       
  1563 		// Categories
       
  1564 		$category_rewrite = $this->generate_rewrite_rules($this->get_category_permastruct(), EP_CATEGORIES);
       
  1565 		$category_rewrite = apply_filters('category_rewrite_rules', $category_rewrite);
       
  1566 
       
  1567 		// Tags
       
  1568 		$tag_rewrite = $this->generate_rewrite_rules($this->get_tag_permastruct(), EP_TAGS);
       
  1569 		$tag_rewrite = apply_filters('tag_rewrite_rules', $tag_rewrite);
       
  1570 
       
  1571 		// Authors
       
  1572 		$author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct(), EP_AUTHORS);
       
  1573 		$author_rewrite = apply_filters('author_rewrite_rules', $author_rewrite);
       
  1574 
       
  1575 		// Pages
       
  1576 		$page_rewrite = $this->page_rewrite_rules();
       
  1577 		$page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite);
       
  1578 
       
  1579 		// Extra permastructs
       
  1580 		foreach ( $this->extra_permastructs as $permastruct )
       
  1581 			$this->extra_rules_top = array_merge($this->extra_rules_top, $this->generate_rewrite_rules($permastruct, EP_NONE));
       
  1582 
       
  1583 		// Put them together.
       
  1584 		if ( $this->use_verbose_page_rules )
       
  1585 			$this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $page_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $tag_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $this->extra_rules);
       
  1586 		else
       
  1587 			$this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $tag_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules);
       
  1588 
       
  1589 		do_action_ref_array('generate_rewrite_rules', array(&$this));
       
  1590 		$this->rules = apply_filters('rewrite_rules_array', $this->rules);
       
  1591 
       
  1592 		return $this->rules;
       
  1593 	}
       
  1594 
       
  1595 	/**
       
  1596 	 * Retrieve the rewrite rules.
       
  1597 	 *
       
  1598 	 * The difference between this method and {@link
       
  1599 	 * WP_Rewrite::rewrite_rules()} is that this method stores the rewrite rules
       
  1600 	 * in the 'rewrite_rules' option and retrieves it. This prevents having to
       
  1601 	 * process all of the permalinks to get the rewrite rules in the form of
       
  1602 	 * caching.
       
  1603 	 *
       
  1604 	 * @since 1.5.0
       
  1605 	 * @access public
       
  1606 	 *
       
  1607 	 * @return array Rewrite rules.
       
  1608 	 */
       
  1609 	function wp_rewrite_rules() {
       
  1610 		$this->rules = get_transient('rewrite_rules');
       
  1611 		if ( empty($this->rules) ) {
       
  1612 			$this->matches = 'matches';
       
  1613 			$this->rewrite_rules();
       
  1614 			set_transient('rewrite_rules', $this->rules);
       
  1615 		}
       
  1616 
       
  1617 		return $this->rules;
       
  1618 	}
       
  1619 
       
  1620 	/**
       
  1621 	 * Retrieve mod_rewrite formatted rewrite rules to write to .htaccess.
       
  1622 	 *
       
  1623 	 * Does not actually write to the .htaccess file, but creates the rules for
       
  1624 	 * the process that will.
       
  1625 	 *
       
  1626 	 * Will add  the non_wp_rules property rules to the .htaccess file before
       
  1627 	 * the WordPress rewrite rules one.
       
  1628 	 *
       
  1629 	 * @since 1.5.0
       
  1630 	 * @access public
       
  1631 	 *
       
  1632 	 * @return string
       
  1633 	 */
       
  1634 	function mod_rewrite_rules() {
       
  1635 		if ( ! $this->using_permalinks()) {
       
  1636 			return '';
       
  1637 		}
       
  1638 
       
  1639 		$site_root = parse_url(get_option('siteurl'));
       
  1640 		if ( isset( $site_root['path'] ) ) {
       
  1641 			$site_root = trailingslashit($site_root['path']);
       
  1642 		}
       
  1643 
       
  1644 		$home_root = parse_url(get_option('home'));
       
  1645 		if ( isset( $home_root['path'] ) ) {
       
  1646 			$home_root = trailingslashit($home_root['path']);
       
  1647 		} else {
       
  1648 			$home_root = '/';
       
  1649 		}
       
  1650 
       
  1651 		$rules = "<IfModule mod_rewrite.c>\n";
       
  1652 		$rules .= "RewriteEngine On\n";
       
  1653 		$rules .= "RewriteBase $home_root\n";
       
  1654 
       
  1655 		//add in the rules that don't redirect to WP's index.php (and thus shouldn't be handled by WP at all)
       
  1656 		foreach ( (array) $this->non_wp_rules as $match => $query) {
       
  1657 			// Apache 1.3 does not support the reluctant (non-greedy) modifier.
       
  1658 			$match = str_replace('.+?', '.+', $match);
       
  1659 
       
  1660 			// If the match is unanchored and greedy, prepend rewrite conditions
       
  1661 			// to avoid infinite redirects and eclipsing of real files.
       
  1662 			if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
       
  1663 				//nada.
       
  1664 			}
       
  1665 
       
  1666 			$rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
       
  1667 		}
       
  1668 
       
  1669 		if ($this->use_verbose_rules) {
       
  1670 			$this->matches = '';
       
  1671 			$rewrite = $this->rewrite_rules();
       
  1672 			$num_rules = count($rewrite);
       
  1673 			$rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" .
       
  1674 				"RewriteCond %{REQUEST_FILENAME} -d\n" .
       
  1675 				"RewriteRule ^.*$ - [S=$num_rules]\n";
       
  1676 
       
  1677 			foreach ( (array) $rewrite as $match => $query) {
       
  1678 				// Apache 1.3 does not support the reluctant (non-greedy) modifier.
       
  1679 				$match = str_replace('.+?', '.+', $match);
       
  1680 
       
  1681 				// If the match is unanchored and greedy, prepend rewrite conditions
       
  1682 				// to avoid infinite redirects and eclipsing of real files.
       
  1683 				if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
       
  1684 					//nada.
       
  1685 				}
       
  1686 
       
  1687 				if (strpos($query, $this->index) !== false) {
       
  1688 					$rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
       
  1689 				} else {
       
  1690 					$rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n";
       
  1691 				}
       
  1692 			}
       
  1693 		} else {
       
  1694 			$rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" .
       
  1695 				"RewriteCond %{REQUEST_FILENAME} !-d\n" .
       
  1696 				"RewriteRule . {$home_root}{$this->index} [L]\n";
       
  1697 		}
       
  1698 
       
  1699 		$rules .= "</IfModule>\n";
       
  1700 
       
  1701 		$rules = apply_filters('mod_rewrite_rules', $rules);
       
  1702 		$rules = apply_filters('rewrite_rules', $rules);  // Deprecated
       
  1703 
       
  1704 		return $rules;
       
  1705 	}
       
  1706 
       
  1707 	/**
       
  1708 	 * Retrieve IIS7 URL Rewrite formatted rewrite rules to write to web.config file.
       
  1709 	 *
       
  1710 	 * Does not actually write to the web.config file, but creates the rules for
       
  1711 	 * the process that will.
       
  1712 	 *
       
  1713 	 * @since 2.8.0
       
  1714 	 * @access public
       
  1715 	 *
       
  1716 	 * @return string
       
  1717 	 */
       
  1718 	function iis7_url_rewrite_rules(){
       
  1719 
       
  1720 		if ( ! $this->using_permalinks()) {
       
  1721 			return '';
       
  1722 		}
       
  1723 		$rules  = "<rule name=\"wordpress\" patternSyntax=\"Wildcard\">\n";
       
  1724 		$rules .= "	<match url=\"*\" />\n";
       
  1725 		$rules .= "	<conditions>\n";
       
  1726 		$rules .= "		<add input=\"{REQUEST_FILENAME}\" matchType=\"IsFile\" negate=\"true\" />\n";
       
  1727 		$rules .= "		<add input=\"{REQUEST_FILENAME}\" matchType=\"IsDirectory\" negate=\"true\" />\n";
       
  1728 		$rules .= "	</conditions>\n";
       
  1729 		$rules .= "	<action type=\"Rewrite\" url=\"index.php\" />\n";
       
  1730 		$rules .= "</rule>";
       
  1731 
       
  1732 		$rules = apply_filters('iis7_url_rewrite_rules', $rules);
       
  1733 
       
  1734 		return $rules;
       
  1735 	}
       
  1736 
       
  1737 	/**
       
  1738 	 * Add a straight rewrite rule.
       
  1739 	 *
       
  1740 	 * Any value in the $after parameter that isn't 'bottom' will be placed at
       
  1741 	 * the top of the rules.
       
  1742 	 *
       
  1743 	 * @since 2.1.0
       
  1744 	 * @access public
       
  1745 	 *
       
  1746 	 * @param string $regex Regular expression to match against request.
       
  1747 	 * @param string $redirect URL regex redirects to when regex matches request.
       
  1748 	 * @param string $after Optional, default is bottom. Location to place rule.
       
  1749 	 */
       
  1750 	function add_rule($regex, $redirect, $after = 'bottom') {
       
  1751 		//get everything up to the first ?
       
  1752 		$index = (strpos($redirect, '?') == false ? strlen($redirect) : strpos($redirect, '?'));
       
  1753 		$front = substr($redirect, 0, $index);
       
  1754 		if ($front != $this->index) { //it doesn't redirect to WP's index.php
       
  1755 			$this->add_external_rule($regex, $redirect);
       
  1756 		} else {
       
  1757 			if ( 'bottom' == $after)
       
  1758 				$this->extra_rules = array_merge($this->extra_rules, array($regex => $redirect));
       
  1759 			else
       
  1760 				$this->extra_rules_top = array_merge($this->extra_rules_top, array($regex => $redirect));
       
  1761 			//$this->extra_rules[$regex] = $redirect;
       
  1762 		}
       
  1763 	}
       
  1764 
       
  1765 	/**
       
  1766 	 * Add a rule that doesn't redirect to index.php.
       
  1767 	 *
       
  1768 	 * Can redirect to any place.
       
  1769 	 *
       
  1770 	 * @since 2.1.0
       
  1771 	 * @access public
       
  1772 	 *
       
  1773 	 * @param string $regex Regular expression to match against request.
       
  1774 	 * @param string $redirect URL regex redirects to when regex matches request.
       
  1775 	 */
       
  1776 	function add_external_rule($regex, $redirect) {
       
  1777 		$this->non_wp_rules[$regex] = $redirect;
       
  1778 	}
       
  1779 
       
  1780 	/**
       
  1781 	 * Add an endpoint, like /trackback/.
       
  1782 	 *
       
  1783 	 * To be inserted after certain URL types (specified in $places).
       
  1784 	 *
       
  1785 	 * @since 2.1.0
       
  1786 	 * @access public
       
  1787 	 *
       
  1788 	 * @param string $name Name of endpoint.
       
  1789 	 * @param array $places URL types that endpoint can be used.
       
  1790 	 */
       
  1791 	function add_endpoint($name, $places) {
       
  1792 		global $wp;
       
  1793 		$this->endpoints[] = array ( $places, $name );
       
  1794 		$wp->add_query_var($name);
       
  1795 	}
       
  1796 
       
  1797 	/**
       
  1798 	 * Add permalink structure.
       
  1799 	 *
       
  1800 	 * These are added along with the extra rewrite rules that are merged to the
       
  1801 	 * top.
       
  1802 	 *
       
  1803 	 * @since unknown
       
  1804 	 * @access public
       
  1805 	 *
       
  1806 	 * @param string $name Name for permalink structure.
       
  1807 	 * @param string $struct Permalink structure.
       
  1808 	 * @param bool $with_front Prepend front base to permalink structure.
       
  1809 	 */
       
  1810 	function add_permastruct($name, $struct, $with_front = true) {
       
  1811 		if ( $with_front )
       
  1812 			$struct = $this->front . $struct;
       
  1813 		$this->extra_permastructs[$name] = $struct;
       
  1814 	}
       
  1815 
       
  1816 	/**
       
  1817 	 * Remove rewrite rules and then recreate rewrite rules.
       
  1818 	 *
       
  1819 	 * Calls {@link WP_Rewrite::wp_rewrite_rules()} after removing the
       
  1820 	 * 'rewrite_rules' option. If the function named 'save_mod_rewrite_rules'
       
  1821 	 * exists, it will be called.
       
  1822 	 *
       
  1823 	 * @since 2.0.1
       
  1824 	 * @access public
       
  1825 	 * @param $hard bool Whether to update .htaccess (hard flush) or just update rewrite_rules transient (soft flush). Default is true (hard).
       
  1826 	 */
       
  1827 	function flush_rules($hard = true) {
       
  1828 		delete_transient('rewrite_rules');
       
  1829 		$this->wp_rewrite_rules();
       
  1830 		if ( $hard && function_exists('save_mod_rewrite_rules') )
       
  1831 			save_mod_rewrite_rules();
       
  1832 		if ( $hard && function_exists('iis7_save_url_rewrite_rules') )
       
  1833 			iis7_save_url_rewrite_rules();
       
  1834 	}
       
  1835 
       
  1836 	/**
       
  1837 	 * Sets up the object's properties.
       
  1838 	 *
       
  1839 	 * The 'use_verbose_page_rules' object property will be set to true if the
       
  1840 	 * permalink structure begins with one of the following: '%postname%', '%category%',
       
  1841 	 * '%tag%', or '%author%'.
       
  1842 	 *
       
  1843 	 * @since 1.5.0
       
  1844 	 * @access public
       
  1845 	 */
       
  1846 	function init() {
       
  1847 		$this->extra_rules = $this->non_wp_rules = $this->endpoints = array();
       
  1848 		$this->permalink_structure = get_option('permalink_structure');
       
  1849 		$this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%'));
       
  1850 		$this->root = '';
       
  1851 		if ($this->using_index_permalinks()) {
       
  1852 			$this->root = $this->index . '/';
       
  1853 		}
       
  1854 		$this->category_base = get_option( 'category_base' );
       
  1855 		$this->tag_base = get_option( 'tag_base' );
       
  1856 		unset($this->category_structure);
       
  1857 		unset($this->author_structure);
       
  1858 		unset($this->date_structure);
       
  1859 		unset($this->page_structure);
       
  1860 		unset($this->search_structure);
       
  1861 		unset($this->feed_structure);
       
  1862 		unset($this->comment_feed_structure);
       
  1863 		$this->use_trailing_slashes = ( substr($this->permalink_structure, -1, 1) == '/' ) ? true : false;
       
  1864 
       
  1865 		// Enable generic rules for pages if permalink structure doesn't begin with a wildcard.
       
  1866 		if ( preg_match("/^[^%]*%(?:postname|category|tag|author)%/", $this->permalink_structure) )
       
  1867 			 $this->use_verbose_page_rules = true;
       
  1868 		else
       
  1869 			$this->use_verbose_page_rules = false;
       
  1870 	}
       
  1871 
       
  1872 	/**
       
  1873 	 * Set the main permalink structure for the blog.
       
  1874 	 *
       
  1875 	 * Will update the 'permalink_structure' option, if there is a difference
       
  1876 	 * between the current permalink structure and the parameter value. Calls
       
  1877 	 * {@link WP_Rewrite::init()} after the option is updated.
       
  1878 	 *
       
  1879 	 * Fires the 'permalink_structure_changed' action once the init call has
       
  1880 	 * processed passing the old and new values
       
  1881 	 *
       
  1882 	 * @since 1.5.0
       
  1883 	 * @access public
       
  1884 	 *
       
  1885 	 * @param string $permalink_structure Permalink structure.
       
  1886 	 */
       
  1887 	function set_permalink_structure($permalink_structure) {
       
  1888 		if ($permalink_structure != $this->permalink_structure) {
       
  1889 			update_option('permalink_structure', $permalink_structure);
       
  1890 			$this->init();
       
  1891 			do_action('permalink_structure_changed', $this->permalink_structure, $permalink_structure);
       
  1892 		}
       
  1893 	}
       
  1894 
       
  1895 	/**
       
  1896 	 * Set the category base for the category permalink.
       
  1897 	 *
       
  1898 	 * Will update the 'category_base' option, if there is a difference between
       
  1899 	 * the current category base and the parameter value. Calls
       
  1900 	 * {@link WP_Rewrite::init()} after the option is updated.
       
  1901 	 *
       
  1902 	 * @since 1.5.0
       
  1903 	 * @access public
       
  1904 	 *
       
  1905 	 * @param string $category_base Category permalink structure base.
       
  1906 	 */
       
  1907 	function set_category_base($category_base) {
       
  1908 		if ($category_base != $this->category_base) {
       
  1909 			update_option('category_base', $category_base);
       
  1910 			$this->init();
       
  1911 		}
       
  1912 	}
       
  1913 
       
  1914 	/**
       
  1915 	 * Set the tag base for the tag permalink.
       
  1916 	 *
       
  1917 	 * Will update the 'tag_base' option, if there is a difference between the
       
  1918 	 * current tag base and the parameter value. Calls
       
  1919 	 * {@link WP_Rewrite::init()} after the option is updated.
       
  1920 	 *
       
  1921 	 * @since 2.3.0
       
  1922 	 * @access public
       
  1923 	 *
       
  1924 	 * @param string $tag_base Tag permalink structure base.
       
  1925 	 */
       
  1926 	function set_tag_base( $tag_base ) {
       
  1927 		if ( $tag_base != $this->tag_base ) {
       
  1928 			update_option( 'tag_base', $tag_base );
       
  1929 			$this->init();
       
  1930 		}
       
  1931 	}
       
  1932 
       
  1933 	/**
       
  1934 	 * PHP4 Constructor - Calls init(), which runs setup.
       
  1935 	 *
       
  1936 	 * @since 1.5.0
       
  1937 	 * @access public
       
  1938 	 *
       
  1939 	 * @return WP_Rewrite
       
  1940 	 */
       
  1941 	function WP_Rewrite() {
       
  1942 		$this->init();
       
  1943 	}
       
  1944 }
       
  1945 
       
  1946 ?>