wp/wp-includes/rewrite.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
     5  * @package WordPress
     5  * @package WordPress
     6  * @subpackage Rewrite
     6  * @subpackage Rewrite
     7  */
     7  */
     8 
     8 
     9 /**
     9 /**
    10  * Endpoint Mask for default, which is nothing.
    10  * Endpoint mask that matches 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 that matches post permalinks.
    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 that matches attachment permalinks.
    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 that matches any date archives.
    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 that matches yearly archives.
    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 that matches monthly archives.
    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 that matches daily archives.
    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 that matches the site 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 that matches comment feeds.
    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 that matches searches.
       
    74  *
       
    75  * Note that this only matches a search at a "pretty" URL such as
       
    76  * `/search/my-search-term`, not `?s=my-search-term`.
    74  *
    77  *
    75  * @since 2.1.0
    78  * @since 2.1.0
    76  */
    79  */
    77 define( 'EP_SEARCH', 256 );
    80 define( 'EP_SEARCH', 256 );
    78 
    81 
    79 /**
    82 /**
    80  * Endpoint Mask for categories.
    83  * Endpoint mask that matches category archives.
    81  *
    84  *
    82  * @since 2.1.0
    85  * @since 2.1.0
    83  */
    86  */
    84 define( 'EP_CATEGORIES', 512 );
    87 define( 'EP_CATEGORIES', 512 );
    85 
    88 
    86 /**
    89 /**
    87  * Endpoint Mask for tags.
    90  * Endpoint mask that matches tag archives.
    88  *
    91  *
    89  * @since 2.3.0
    92  * @since 2.3.0
    90  */
    93  */
    91 define( 'EP_TAGS', 1024 );
    94 define( 'EP_TAGS', 1024 );
    92 
    95 
    93 /**
    96 /**
    94  * Endpoint Mask for authors.
    97  * Endpoint mask that matches author archives.
    95  *
    98  *
    96  * @since 2.1.0
    99  * @since 2.1.0
    97  */
   100  */
    98 define( 'EP_AUTHORS', 2048 );
   101 define( 'EP_AUTHORS', 2048 );
    99 
   102 
   100 /**
   103 /**
   101  * Endpoint Mask for pages.
   104  * Endpoint mask that matches pages.
   102  *
   105  *
   103  * @since 2.1.0
   106  * @since 2.1.0
   104  */
   107  */
   105 define( 'EP_PAGES', 4096 );
   108 define( 'EP_PAGES', 4096 );
   106 
   109 
   107 /**
   110 /**
   108  * Endpoint Mask for all archive views.
   111  * Endpoint mask that matches all archive views.
   109  *
   112  *
   110  * @since 3.7.0
   113  * @since 3.7.0
   111  */
   114  */
   112 define( 'EP_ALL_ARCHIVES', EP_DATE | EP_YEAR | EP_MONTH | EP_DAY | EP_CATEGORIES | EP_TAGS | EP_AUTHORS );
   115 define( 'EP_ALL_ARCHIVES', EP_DATE | EP_YEAR | EP_MONTH | EP_DAY | EP_CATEGORIES | EP_TAGS | EP_AUTHORS );
   113 
   116 
   114 /**
   117 /**
   115  * Endpoint Mask for everything.
   118  * Endpoint mask that matches everything.
   116  *
   119  *
   117  * @since 2.1.0
   120  * @since 2.1.0
   118  */
   121  */
   119 define( 'EP_ALL', EP_PERMALINK | EP_ATTACHMENT | EP_ROOT | EP_COMMENTS | EP_SEARCH | EP_PAGES | EP_ALL_ARCHIVES );
   122 define( 'EP_ALL', EP_PERMALINK | EP_ATTACHMENT | EP_ROOT | EP_COMMENTS | EP_SEARCH | EP_PAGES | EP_ALL_ARCHIVES );
   120 
   123 
   141 }
   144 }
   142 
   145 
   143 /**
   146 /**
   144  * Add a new rewrite tag (like %postname%).
   147  * Add a new rewrite tag (like %postname%).
   145  *
   148  *
   146  * The $query parameter is optional. If it is omitted you must ensure that
   149  * The `$query` parameter is optional. If it is omitted you must ensure that you call
   147  * you call this on, or before, the {@see 'init'} hook. This is because $query defaults
   150  * this on, or before, the {@see 'init'} hook. This is because `$query` defaults to
   148  * to "$tag=", and for this to work a new query var has to be added.
   151  * `$tag=`, and for this to work a new query var has to be added.
   149  *
   152  *
   150  * @since 2.1.0
   153  * @since 2.1.0
   151  *
   154  *
   152  * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
   155  * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
   153  * @global WP         $wp         Current WordPress environment instance.
   156  * @global WP         $wp         Current WordPress environment instance.
   306  *
   309  *
   307  * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
   310  * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
   308  *
   311  *
   309  * @param string      $name      Name of the endpoint.
   312  * @param string      $name      Name of the endpoint.
   310  * @param int         $places    Endpoint mask describing the places the endpoint should be added.
   313  * @param int         $places    Endpoint mask describing the places the endpoint should be added.
       
   314  *                               Accepts a mask of:
       
   315  *                               - `EP_ALL`
       
   316  *                               - `EP_NONE`
       
   317  *                               - `EP_ALL_ARCHIVES`
       
   318  *                               - `EP_ATTACHMENT`
       
   319  *                               - `EP_AUTHORS`
       
   320  *                               - `EP_CATEGORIES`
       
   321  *                               - `EP_COMMENTS`
       
   322  *                               - `EP_DATE`
       
   323  *                               - `EP_DAY`
       
   324  *                               - `EP_MONTH`
       
   325  *                               - `EP_PAGES`
       
   326  *                               - `EP_PERMALINK`
       
   327  *                               - `EP_ROOT`
       
   328  *                               - `EP_SEARCH`
       
   329  *                               - `EP_TAGS`
       
   330  *                               - `EP_YEAR`
   311  * @param string|bool $query_var Name of the corresponding query variable. Pass `false` to skip registering a query_var
   331  * @param string|bool $query_var Name of the corresponding query variable. Pass `false` to skip registering a query_var
   312  *                               for this endpoint. Defaults to the value of `$name`.
   332  *                               for this endpoint. Defaults to the value of `$name`.
   313  */
   333  */
   314 function add_rewrite_endpoint( $name, $places, $query_var = true ) {
   334 function add_rewrite_endpoint( $name, $places, $query_var = true ) {
   315 	global $wp_rewrite;
   335 	global $wp_rewrite;
   398 	}
   418 	}
   399 
   419 
   400 	// If the date of the post doesn't match the date specified in the URL, resolve to the date archive.
   420 	// If the date of the post doesn't match the date specified in the URL, resolve to the date archive.
   401 	if ( preg_match( '/^([0-9]{4})\-([0-9]{2})/', $post->post_date, $matches ) && isset( $query_vars['year'] ) && ( 'monthnum' === $compare || 'day' === $compare ) ) {
   421 	if ( preg_match( '/^([0-9]{4})\-([0-9]{2})/', $post->post_date, $matches ) && isset( $query_vars['year'] ) && ( 'monthnum' === $compare || 'day' === $compare ) ) {
   402 		// $matches[1] is the year the post was published.
   422 		// $matches[1] is the year the post was published.
   403 		if ( intval( $query_vars['year'] ) !== intval( $matches[1] ) ) {
   423 		if ( (int) $query_vars['year'] !== (int) $matches[1] ) {
   404 			return $query_vars;
   424 			return $query_vars;
   405 		}
   425 		}
   406 
   426 
   407 		// $matches[2] is the month the post was published.
   427 		// $matches[2] is the month the post was published.
   408 		if ( 'day' === $compare && isset( $query_vars['monthnum'] ) && intval( $query_vars['monthnum'] ) !== intval( $matches[2] ) ) {
   428 		if ( 'day' === $compare && isset( $query_vars['monthnum'] ) && (int) $query_vars['monthnum'] !== (int) $matches[2] ) {
   409 			return $query_vars;
   429 			return $query_vars;
   410 		}
   430 		}
   411 	}
   431 	}
   412 
   432 
   413 	/*
   433 	/*
   435 		return $query_vars;
   455 		return $query_vars;
   436 	}
   456 	}
   437 
   457 
   438 	// If we've gotten to this point, we have a slug/date clash. First, adjust for nextpage.
   458 	// If we've gotten to this point, we have a slug/date clash. First, adjust for nextpage.
   439 	if ( '' !== $maybe_page ) {
   459 	if ( '' !== $maybe_page ) {
   440 		$query_vars['page'] = intval( $maybe_page );
   460 		$query_vars['page'] = (int) $maybe_page;
   441 	}
   461 	}
   442 
   462 
   443 	// Next, unset autodetected date-related query vars.
   463 	// Next, unset autodetected date-related query vars.
   444 	unset( $query_vars['year'] );
   464 	unset( $query_vars['year'] );
   445 	unset( $query_vars['monthnum'] );
   465 	unset( $query_vars['monthnum'] );