wp/wp-includes/rewrite.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
     9 /**
     9 /**
    10  * Endpoint Mask for default, which is nothing.
    10  * Endpoint Mask for default, which is nothing.
    11  *
    11  *
    12  * @since 2.1.0
    12  * @since 2.1.0
    13  */
    13  */
    14 define('EP_NONE', 0);
    14 define( 'EP_NONE', 0 );
    15 
    15 
    16 /**
    16 /**
    17  * Endpoint Mask for Permalink.
    17  * Endpoint Mask for Permalink.
    18  *
    18  *
    19  * @since 2.1.0
    19  * @since 2.1.0
    20  */
    20  */
    21 define('EP_PERMALINK', 1);
    21 define( 'EP_PERMALINK', 1 );
    22 
    22 
    23 /**
    23 /**
    24  * Endpoint Mask for Attachment.
    24  * Endpoint Mask for Attachment.
    25  *
    25  *
    26  * @since 2.1.0
    26  * @since 2.1.0
    27  */
    27  */
    28 define('EP_ATTACHMENT', 2);
    28 define( 'EP_ATTACHMENT', 2 );
    29 
    29 
    30 /**
    30 /**
    31  * Endpoint Mask for date.
    31  * Endpoint Mask for date.
    32  *
    32  *
    33  * @since 2.1.0
    33  * @since 2.1.0
    34  */
    34  */
    35 define('EP_DATE', 4);
    35 define( 'EP_DATE', 4 );
    36 
    36 
    37 /**
    37 /**
    38  * Endpoint Mask for year
    38  * Endpoint Mask for year
    39  *
    39  *
    40  * @since 2.1.0
    40  * @since 2.1.0
    41  */
    41  */
    42 define('EP_YEAR', 8);
    42 define( 'EP_YEAR', 8 );
    43 
    43 
    44 /**
    44 /**
    45  * Endpoint Mask for month.
    45  * Endpoint Mask for month.
    46  *
    46  *
    47  * @since 2.1.0
    47  * @since 2.1.0
    48  */
    48  */
    49 define('EP_MONTH', 16);
    49 define( 'EP_MONTH', 16 );
    50 
    50 
    51 /**
    51 /**
    52  * Endpoint Mask for day.
    52  * Endpoint Mask for day.
    53  *
    53  *
    54  * @since 2.1.0
    54  * @since 2.1.0
    55  */
    55  */
    56 define('EP_DAY', 32);
    56 define( 'EP_DAY', 32 );
    57 
    57 
    58 /**
    58 /**
    59  * Endpoint Mask for root.
    59  * Endpoint Mask for root.
    60  *
    60  *
    61  * @since 2.1.0
    61  * @since 2.1.0
    62  */
    62  */
    63 define('EP_ROOT', 64);
    63 define( 'EP_ROOT', 64 );
    64 
    64 
    65 /**
    65 /**
    66  * Endpoint Mask for comments.
    66  * Endpoint Mask for comments.
    67  *
    67  *
    68  * @since 2.1.0
    68  * @since 2.1.0
    69  */
    69  */
    70 define('EP_COMMENTS', 128);
    70 define( 'EP_COMMENTS', 128 );
    71 
    71 
    72 /**
    72 /**
    73  * Endpoint Mask for searches.
    73  * Endpoint Mask for searches.
    74  *
    74  *
    75  * @since 2.1.0
    75  * @since 2.1.0
    76  */
    76  */
    77 define('EP_SEARCH', 256);
    77 define( 'EP_SEARCH', 256 );
    78 
    78 
    79 /**
    79 /**
    80  * Endpoint Mask for categories.
    80  * Endpoint Mask for categories.
    81  *
    81  *
    82  * @since 2.1.0
    82  * @since 2.1.0
    83  */
    83  */
    84 define('EP_CATEGORIES', 512);
    84 define( 'EP_CATEGORIES', 512 );
    85 
    85 
    86 /**
    86 /**
    87  * Endpoint Mask for tags.
    87  * Endpoint Mask for tags.
    88  *
    88  *
    89  * @since 2.3.0
    89  * @since 2.3.0
    90  */
    90  */
    91 define('EP_TAGS', 1024);
    91 define( 'EP_TAGS', 1024 );
    92 
    92 
    93 /**
    93 /**
    94  * Endpoint Mask for authors.
    94  * Endpoint Mask for authors.
    95  *
    95  *
    96  * @since 2.1.0
    96  * @since 2.1.0
    97  */
    97  */
    98 define('EP_AUTHORS', 2048);
    98 define( 'EP_AUTHORS', 2048 );
    99 
    99 
   100 /**
   100 /**
   101  * Endpoint Mask for pages.
   101  * Endpoint Mask for pages.
   102  *
   102  *
   103  * @since 2.1.0
   103  * @since 2.1.0
   104  */
   104  */
   105 define('EP_PAGES', 4096);
   105 define( 'EP_PAGES', 4096 );
   106 
   106 
   107 /**
   107 /**
   108  * Endpoint Mask for all archive views.
   108  * Endpoint Mask for all archive views.
   109  *
   109  *
   110  * @since 3.7.0
   110  * @since 3.7.0
   156  * @param string $regex Regular expression to substitute the tag for in rewrite rules.
   156  * @param string $regex Regular expression to substitute the tag for in rewrite rules.
   157  * @param string $query Optional. String to append to the rewritten query. Must end in '='. Default empty.
   157  * @param string $query Optional. String to append to the rewritten query. Must end in '='. Default empty.
   158  */
   158  */
   159 function add_rewrite_tag( $tag, $regex, $query = '' ) {
   159 function add_rewrite_tag( $tag, $regex, $query = '' ) {
   160 	// validate the tag's name
   160 	// validate the tag's name
   161 	if ( strlen( $tag ) < 3 || $tag[0] != '%' || $tag[ strlen($tag) - 1 ] != '%' )
   161 	if ( strlen( $tag ) < 3 || $tag[0] != '%' || $tag[ strlen( $tag ) - 1 ] != '%' ) {
   162 		return;
   162 		return;
       
   163 	}
   163 
   164 
   164 	global $wp_rewrite, $wp;
   165 	global $wp_rewrite, $wp;
   165 
   166 
   166 	if ( empty( $query ) ) {
   167 	if ( empty( $query ) ) {
   167 		$qv = trim( $tag, '%' );
   168 		$qv = trim( $tag, '%' );
   201  */
   202  */
   202 function add_permastruct( $name, $struct, $args = array() ) {
   203 function add_permastruct( $name, $struct, $args = array() ) {
   203 	global $wp_rewrite;
   204 	global $wp_rewrite;
   204 
   205 
   205 	// Back-compat for the old parameters: $with_front and $ep_mask.
   206 	// Back-compat for the old parameters: $with_front and $ep_mask.
   206 	if ( ! is_array( $args ) )
   207 	if ( ! is_array( $args ) ) {
   207 		$args = array( 'with_front' => $args );
   208 		$args = array( 'with_front' => $args );
   208 	if ( func_num_args() == 4 )
   209 	}
       
   210 	if ( func_num_args() == 4 ) {
   209 		$args['ep_mask'] = func_get_arg( 3 );
   211 		$args['ep_mask'] = func_get_arg( 3 );
       
   212 	}
   210 
   213 
   211 	$wp_rewrite->add_permastruct( $name, $struct, $args );
   214 	$wp_rewrite->add_permastruct( $name, $struct, $args );
   212 }
   215 }
   213 
   216 
   214 /**
   217 /**
   264  * @since 3.0.0
   267  * @since 3.0.0
   265  *
   268  *
   266  * @global WP_Rewrite $wp_rewrite
   269  * @global WP_Rewrite $wp_rewrite
   267  *
   270  *
   268  * @param bool $hard Whether to update .htaccess (hard flush) or just update
   271  * @param bool $hard Whether to update .htaccess (hard flush) or just update
   269  * 	                 rewrite_rules transient (soft flush). Default is true (hard).
   272  *                   rewrite_rules transient (soft flush). Default is true (hard).
   270  */
   273  */
   271 function flush_rewrite_rules( $hard = true ) {
   274 function flush_rewrite_rules( $hard = true ) {
   272 	global $wp_rewrite;
   275 	global $wp_rewrite;
   273 	$wp_rewrite->flush_rules( $hard );
   276 	$wp_rewrite->flush_rules( $hard );
   274 }
   277 }
   320  *
   323  *
   321  * @param string $base The taxonomy base that we're going to filter
   324  * @param string $base The taxonomy base that we're going to filter
   322  * @return string
   325  * @return string
   323  */
   326  */
   324 function _wp_filter_taxonomy_base( $base ) {
   327 function _wp_filter_taxonomy_base( $base ) {
   325 	if ( !empty( $base ) ) {
   328 	if ( ! empty( $base ) ) {
   326 		$base = preg_replace( '|^/index\.php/|', '', $base );
   329 		$base = preg_replace( '|^/index\.php/|', '', $base );
   327 		$base = trim( $base, '/' );
   330 		$base = trim( $base, '/' );
   328 	}
   331 	}
   329 	return $base;
   332 	return $base;
   330 }
   333 }
   371 	 * for month-slug clashes when `is_month` *or* `is_day`.
   374 	 * for month-slug clashes when `is_month` *or* `is_day`.
   372 	 */
   375 	 */
   373 	$compare = '';
   376 	$compare = '';
   374 	if ( 0 === $postname_index && ( isset( $query_vars['year'] ) || isset( $query_vars['monthnum'] ) ) ) {
   377 	if ( 0 === $postname_index && ( isset( $query_vars['year'] ) || isset( $query_vars['monthnum'] ) ) ) {
   375 		$compare = 'year';
   378 		$compare = 'year';
   376 	} elseif ( '%year%' === $permastructs[ $postname_index - 1 ] && ( isset( $query_vars['monthnum'] ) || isset( $query_vars['day'] ) ) ) {
   379 	} elseif ( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && ( isset( $query_vars['monthnum'] ) || isset( $query_vars['day'] ) ) ) {
   377 		$compare = 'monthnum';
   380 		$compare = 'monthnum';
   378 	} elseif ( '%monthnum%' === $permastructs[ $postname_index - 1 ] && isset( $query_vars['day'] ) ) {
   381 	} elseif ( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && isset( $query_vars['day'] ) ) {
   379 		$compare = 'day';
   382 		$compare = 'day';
   380 	}
   383 	}
   381 
   384 
   382 	if ( ! $compare ) {
   385 	if ( ! $compare ) {
   383 		return $query_vars;
   386 		return $query_vars;
   478 	if ( $url_host && $url_host !== $home_url_host ) {
   481 	if ( $url_host && $url_host !== $home_url_host ) {
   479 		return 0;
   482 		return 0;
   480 	}
   483 	}
   481 
   484 
   482 	// First, check to see if there is a 'p=N' or 'page_id=N' to match against
   485 	// First, check to see if there is a 'p=N' or 'page_id=N' to match against
   483 	if ( preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values) )	{
   486 	if ( preg_match( '#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values ) ) {
   484 		$id = absint($values[2]);
   487 		$id = absint( $values[2] );
   485 		if ( $id )
   488 		if ( $id ) {
   486 			return $id;
   489 			return $id;
       
   490 		}
   487 	}
   491 	}
   488 
   492 
   489 	// Get rid of the #anchor
   493 	// Get rid of the #anchor
   490 	$url_split = explode('#', $url);
   494 	$url_split = explode( '#', $url );
   491 	$url = $url_split[0];
   495 	$url       = $url_split[0];
   492 
   496 
   493 	// Get rid of URL ?query=string
   497 	// Get rid of URL ?query=string
   494 	$url_split = explode('?', $url);
   498 	$url_split = explode( '?', $url );
   495 	$url = $url_split[0];
   499 	$url       = $url_split[0];
   496 
   500 
   497 	// Set the correct URL scheme.
   501 	// Set the correct URL scheme.
   498 	$scheme = parse_url( home_url(), PHP_URL_SCHEME );
   502 	$scheme = parse_url( home_url(), PHP_URL_SCHEME );
   499 	$url = set_url_scheme( $url, $scheme );
   503 	$url    = set_url_scheme( $url, $scheme );
   500 
   504 
   501 	// Add 'www.' if it is absent and should be there
   505 	// Add 'www.' if it is absent and should be there
   502 	if ( false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.') )
   506 	if ( false !== strpos( home_url(), '://www.' ) && false === strpos( $url, '://www.' ) ) {
   503 		$url = str_replace('://', '://www.', $url);
   507 		$url = str_replace( '://', '://www.', $url );
       
   508 	}
   504 
   509 
   505 	// Strip 'www.' if it is present and shouldn't be
   510 	// Strip 'www.' if it is present and shouldn't be
   506 	if ( false === strpos(home_url(), '://www.') )
   511 	if ( false === strpos( home_url(), '://www.' ) ) {
   507 		$url = str_replace('://www.', '://', $url);
   512 		$url = str_replace( '://www.', '://', $url );
       
   513 	}
   508 
   514 
   509 	if ( trim( $url, '/' ) === home_url() && 'page' == get_option( 'show_on_front' ) ) {
   515 	if ( trim( $url, '/' ) === home_url() && 'page' == get_option( 'show_on_front' ) ) {
   510 		$page_on_front = get_option( 'page_on_front' );
   516 		$page_on_front = get_option( 'page_on_front' );
   511 
   517 
   512 		if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) {
   518 		if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) {
   516 
   522 
   517 	// Check to see if we are using rewrite rules
   523 	// Check to see if we are using rewrite rules
   518 	$rewrite = $wp_rewrite->wp_rewrite_rules();
   524 	$rewrite = $wp_rewrite->wp_rewrite_rules();
   519 
   525 
   520 	// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
   526 	// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
   521 	if ( empty($rewrite) )
   527 	if ( empty( $rewrite ) ) {
   522 		return 0;
   528 		return 0;
       
   529 	}
   523 
   530 
   524 	// Strip 'index.php/' if we're not using path info permalinks
   531 	// Strip 'index.php/' if we're not using path info permalinks
   525 	if ( !$wp_rewrite->using_index_permalinks() )
   532 	if ( ! $wp_rewrite->using_index_permalinks() ) {
   526 		$url = str_replace( $wp_rewrite->index . '/', '', $url );
   533 		$url = str_replace( $wp_rewrite->index . '/', '', $url );
       
   534 	}
   527 
   535 
   528 	if ( false !== strpos( trailingslashit( $url ), home_url( '/' ) ) ) {
   536 	if ( false !== strpos( trailingslashit( $url ), home_url( '/' ) ) ) {
   529 		// Chop off http://domain.com/[path]
   537 		// Chop off http://domain.com/[path]
   530 		$url = str_replace(home_url(), '', $url);
   538 		$url = str_replace( home_url(), '', $url );
   531 	} else {
   539 	} else {
   532 		// Chop off /path/to/blog
   540 		// Chop off /path/to/blog
   533 		$home_path = parse_url( home_url( '/' ) );
   541 		$home_path = parse_url( home_url( '/' ) );
   534 		$home_path = isset( $home_path['path'] ) ? $home_path['path'] : '' ;
   542 		$home_path = isset( $home_path['path'] ) ? $home_path['path'] : '';
   535 		$url = preg_replace( sprintf( '#^%s#', preg_quote( $home_path ) ), '', trailingslashit( $url ) );
   543 		$url       = preg_replace( sprintf( '#^%s#', preg_quote( $home_path ) ), '', trailingslashit( $url ) );
   536 	}
   544 	}
   537 
   545 
   538 	// Trim leading and lagging slashes
   546 	// Trim leading and lagging slashes
   539 	$url = trim($url, '/');
   547 	$url = trim( $url, '/' );
   540 
   548 
   541 	$request = $url;
   549 	$request              = $url;
   542 	$post_type_query_vars = array();
   550 	$post_type_query_vars = array();
   543 
   551 
   544 	foreach ( get_post_types( array() , 'objects' ) as $post_type => $t ) {
   552 	foreach ( get_post_types( array(), 'objects' ) as $post_type => $t ) {
   545 		if ( ! empty( $t->query_var ) )
   553 		if ( ! empty( $t->query_var ) ) {
   546 			$post_type_query_vars[ $t->query_var ] = $post_type;
   554 			$post_type_query_vars[ $t->query_var ] = $post_type;
       
   555 		}
   547 	}
   556 	}
   548 
   557 
   549 	// Look for matches.
   558 	// Look for matches.
   550 	$request_match = $request;
   559 	$request_match = $request;
   551 	foreach ( (array)$rewrite as $match => $query) {
   560 	foreach ( (array) $rewrite as $match => $query ) {
   552 
   561 
   553 		// If the requesting file is the anchor of the match, prepend it
   562 		// If the requesting file is the anchor of the match, prepend it
   554 		// to the path info.
   563 		// to the path info.
   555 		if ( !empty($url) && ($url != $request) && (strpos($match, $url) === 0) )
   564 		if ( ! empty( $url ) && ( $url != $request ) && ( strpos( $match, $url ) === 0 ) ) {
   556 			$request_match = $url . '/' . $request;
   565 			$request_match = $url . '/' . $request;
   557 
   566 		}
   558 		if ( preg_match("#^$match#", $request_match, $matches) ) {
   567 
       
   568 		if ( preg_match( "#^$match#", $request_match, $matches ) ) {
   559 
   569 
   560 			if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
   570 			if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
   561 				// This is a verbose page match, let's check to be sure about it.
   571 				// This is a verbose page match, let's check to be sure about it.
   562 				$page = get_page_by_path( $matches[ $varmatch[1] ] );
   572 				$page = get_page_by_path( $matches[ $varmatch[1] ] );
   563 				if ( ! $page ) {
   573 				if ( ! $page ) {
   571 				}
   581 				}
   572 			}
   582 			}
   573 
   583 
   574 			// Got a match.
   584 			// Got a match.
   575 			// Trim the query of everything up to the '?'.
   585 			// Trim the query of everything up to the '?'.
   576 			$query = preg_replace("!^.+\?!", '', $query);
   586 			$query = preg_replace( '!^.+\?!', '', $query );
   577 
   587 
   578 			// Substitute the substring matches into the query.
   588 			// Substitute the substring matches into the query.
   579 			$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
   589 			$query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) );
   580 
   590 
   581 			// Filter out non-public query vars
   591 			// Filter out non-public query vars
   582 			global $wp;
   592 			global $wp;
   583 			parse_str( $query, $query_vars );
   593 			parse_str( $query, $query_vars );
   584 			$query = array();
   594 			$query = array();
   585 			foreach ( (array) $query_vars as $key => $value ) {
   595 			foreach ( (array) $query_vars as $key => $value ) {
   586 				if ( in_array( $key, $wp->public_query_vars ) ){
   596 				if ( in_array( $key, $wp->public_query_vars ) ) {
   587 					$query[$key] = $value;
   597 					$query[ $key ] = $value;
   588 					if ( isset( $post_type_query_vars[$key] ) ) {
   598 					if ( isset( $post_type_query_vars[ $key ] ) ) {
   589 						$query['post_type'] = $post_type_query_vars[$key];
   599 						$query['post_type'] = $post_type_query_vars[ $key ];
   590 						$query['name'] = $value;
   600 						$query['name']      = $value;
   591 					}
   601 					}
   592 				}
   602 				}
   593 			}
   603 			}
   594 
   604 
   595 			// Resolve conflicts between posts with numeric slugs and date archive queries.
   605 			// Resolve conflicts between posts with numeric slugs and date archive queries.
   596 			$query = wp_resolve_numeric_slug_conflicts( $query );
   606 			$query = wp_resolve_numeric_slug_conflicts( $query );
   597 
   607 
   598 			// Do the query
   608 			// Do the query
   599 			$query = new WP_Query( $query );
   609 			$query = new WP_Query( $query );
   600 			if ( ! empty( $query->posts ) && $query->is_singular )
   610 			if ( ! empty( $query->posts ) && $query->is_singular ) {
   601 				return $query->post->ID;
   611 				return $query->post->ID;
   602 			else
   612 			} else {
   603 				return 0;
   613 				return 0;
       
   614 			}
   604 		}
   615 		}
   605 	}
   616 	}
   606 	return 0;
   617 	return 0;
   607 }
   618 }