Ticket #35983: 35983.diff
File 35983.diff, 11.9 KB (added by , 9 years ago) |
---|
-
wp-includes/class-wp-rewrite.php
class WP_Rewrite { 900 900 901 901 /* 902 902 * $feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom 903 903 * and <permalink>/atom are both possible 904 904 */ 905 905 $feedregex = $this->feed_base . '/' . $feedregex2; 906 906 907 907 // Build a regex to match the trackback and page/xx parts of URLs. 908 908 $trackbackregex = 'trackback/?$'; 909 909 $pageregex = $this->pagination_base . '/?([0-9]{1,})/?$'; 910 910 $commentregex = $this->comments_pagination_base . '-([0-9]{1,})/?$'; 911 911 $embedregex = 'embed/?$'; 912 912 913 913 // Build up an array of endpoint regexes to append => queries to append. 914 914 if ( $endpoints ) { 915 $ep_query_append = array (); 916 foreach ( (array) $this->endpoints as $endpoint) { 917 // Match everything after the endpoint name, but allow for nothing to appear there. 918 $epmatch = $endpoint[1] . '(/(.*))?/?$'; 915 $ep_query_append = array(); 916 foreach ( (array) $this->endpoints as $endpoint ) { 917 if ( $endpoint['with_parameter'] ) { 918 // Match everything after the endpoint name, but allow for nothing to appear there. 919 $endpoint['regex'] = $endpoint['name'] . '(/(.*))?/?$'; 920 // Capture the inner group 921 $endpoint['regex_capture_offset'] = 2; 922 } else { 923 // Match the endpoint itself 924 $endpoint['regex'] = '(' . $endpoint['name'] . ')/?$'; 925 $endpoint['regex_capture_offset'] = 1; 926 } 919 927 920 928 // This will be appended on to the rest of the query for each dir. 921 $epquery = '&' . $endpoint[2] . '='; 922 $ep_query_append[$epmatch] = array ( $endpoint[0], $epquery ); 929 $endpoint['query'] = '&' . $endpoint['query_var'] . '='; 930 931 $ep_query_append[] = $endpoint; 923 932 } 924 933 } 925 934 926 935 // Get everything up to the first rewrite tag. 927 936 $front = substr($permalink_structure, 0, strpos($permalink_structure, '%')); 928 937 929 938 // Build an array of the tags (note that said array ends up being in $tokens[0]). 930 939 preg_match_all('/%.+?%/', $permalink_structure, $tokens); 931 940 932 941 $num_tokens = count($tokens[0]); 933 942 934 943 $index = $this->index; //probably 'index.php' 935 944 $feedindex = $index; 936 945 $trackbackindex = $index; 937 946 $embedindex = $index; … … class WP_Rewrite { 1042 1051 1043 1052 //...and /page/xx ones 1044 1053 if ( $paged ) { 1045 1054 $rewrite = array_merge( $rewrite, array( $pagematch => $pagequery ) ); 1046 1055 } 1047 1056 1048 1057 // Only on pages with comments add ../comment-page-xx/. 1049 1058 if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask ) { 1050 1059 $rewrite = array_merge($rewrite, array($commentmatch => $commentquery)); 1051 1060 } elseif ( EP_ROOT & $ep_mask && get_option('page_on_front') ) { 1052 1061 $rewrite = array_merge($rewrite, array($rootcommentmatch => $rootcommentquery)); 1053 1062 } 1054 1063 1055 1064 // Do endpoints. 1056 1065 if ( $endpoints ) { 1057 foreach ( (array) $ep_query_append as $ regex => $ep) {1066 foreach ( (array) $ep_query_append as $endpoint ) { 1058 1067 // Add the endpoints on if the mask fits. 1059 if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific ) 1060 $rewrite[$match . $regex] = $index . '?' . $query . $ep[1] . $this->preg_index($num_toks + 2); 1068 if ( $endpoint['mask'] & $ep_mask || $endpoint['mask'] & $ep_mask_specific ) {; 1069 $rewrite[ $match . $endpoint['regex'] ] = $index . '?' . $query . $endpoint['query'] . $this->preg_index( $num_toks + $endpoint['regex_capture_offset'] ); 1070 } 1061 1071 } 1062 1072 } 1063 1073 1064 1074 // If we've got some tags in this dir. 1065 1075 if ( $num_toks ) { 1066 1076 $post = false; 1067 1077 $page = false; 1068 1078 1069 1079 /* 1070 1080 * Check to see if this dir is permalink-level: i.e. the structure specifies an 1071 1081 * individual post. Do this by checking it contains at least one of 1) post name, 1072 1082 * 2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and 1073 1083 * minute all present). Set these flags now as we need them for the endpoints. 1074 1084 */ 1075 1085 if ( strpos($struct, '%postname%') !== false … … class WP_Rewrite { 1147 1157 // And <permalink>/comment-page-xx 1148 1158 $sub2comment = $sub2 . $commentregex; 1149 1159 1150 1160 // And <permalink>/embed/... 1151 1161 $sub2embed = $sub2 . $embedregex; 1152 1162 1153 1163 // Create queries for these extra tag-ons we've just dealt with. 1154 1164 $subquery = $index . '?attachment=' . $this->preg_index(1); 1155 1165 $subtbquery = $subquery . '&tb=1'; 1156 1166 $subfeedquery = $subquery . '&feed=' . $this->preg_index(2); 1157 1167 $subcommentquery = $subquery . '&cpage=' . $this->preg_index(2); 1158 1168 $subembedquery = $subquery . '&embed=true'; 1159 1169 1160 1170 // Do endpoints for attachments. 1161 1171 if ( !empty($endpoints) ) { 1162 foreach ( (array) $ep_query_append as $ regex => $ep) {1163 if ( $e p[0] & EP_ATTACHMENT ) {1164 $rewrite[ $sub1 . $regex] = $subquery . $ep[1] . $this->preg_index(3);1165 $rewrite[ $sub2 . $regex] = $subquery . $ep[1] . $this->preg_index(3);1172 foreach ( (array) $ep_query_append as $endpoint ) { 1173 if ( $endpoint['mask'] & EP_ATTACHMENT ) {; 1174 $rewrite[ $sub1 . $endpoint['regex'] ] = $subquery . $endpoint['query'] . $this->preg_index( 1 + $endpoint['regex_capture_offset'] ); 1175 $rewrite[ $sub2 . $endpoint['regex'] ] = $subquery . $endpoint['query'] . $this->preg_index( 1 + $endpoint['regex_capture_offset'] ); 1166 1176 } 1167 1177 } 1168 1178 } 1169 1179 1170 1180 /* 1171 1181 * Now we've finished with endpoints, finish off the $sub1 and $sub2 matches 1172 1182 * add a ? as we don't have to match that last slash, and finally a $ so we 1173 1183 * match to the end of the URL 1174 1184 */ 1175 1185 $sub1 .= '?$'; 1176 1186 $sub2 .= '?$'; 1177 1187 1178 1188 /* 1179 1189 * Post pagination, e.g. <permalink>/2/ 1180 1190 * Previously: '(/[0-9]+)?/?$', which produced '/2' for page. … … class WP_Rewrite { 1665 1675 * @since 2.1.0 1666 1676 * @access public 1667 1677 * 1668 1678 * @param string $regex Regular expression to match request against. 1669 1679 * @param string $query The corresponding query vars for this rewrite rule. 1670 1680 */ 1671 1681 public function add_external_rule( $regex, $query ) { 1672 1682 $this->non_wp_rules[ $regex ] = $query; 1673 1683 } 1674 1684 1675 1685 /** 1676 1686 * Adds an endpoint, like /trackback/. 1677 1687 * 1678 1688 * @since 2.1.0 1679 1689 * @since 3.9.0 $query_var parameter added. 1680 * @since 4.3.0 Added support for skipping query var registration by passing `false` to `$query_var`. 1690 * @since 4.3.0 Added support for skipping query var registration by passing `false` to `$args[query_var]`. 1691 * @since 4.x.0 Added support for registering a self-matching endpoint by passing `false` to `$args[with_parameter]`. 1681 1692 * @access public 1682 1693 * 1683 1694 * @see add_rewrite_endpoint() for full documentation. 1684 1695 * @global WP $wp 1685 1696 * 1686 1697 * @param string $name Name of the endpoint. 1687 1698 * @param int $places Endpoint mask describing the places the endpoint should be added. 1688 * @param string|bool $query_var Optional. Name of the corresponding query variable. Pass `false` to 1689 * skip registering a query_var for this endpoint. Defaults to the 1690 * value of `$name`. 1699 * @param array { 1700 * @type string|bool $query_var Optional. Name of the corresponding query variable. Pass `false` 1701 * to skip registering a query_var for this endpoint. Defaults to the 1702 * value of `$name`. 1703 * @type bool $with_parameter Optional. Specifies if the endpoint is self-matching or matching 1704 * a parameter. Pass false to match the endpoint name. Defaults to true. 1705 * } 1706 * 1691 1707 */ 1692 public function add_endpoint( $name, $places, $ query_var= true ) {1708 public function add_endpoint( $name, $places, $args = true ) { 1693 1709 global $wp; 1694 1710 1695 // For backward compatibility, if null has explicitly been passed as `$query_var`, assume `true`. 1696 if ( true === $query_var || null === func_get_arg( 2 ) ) { 1697 $query_var = $name; 1711 $defaults = array( 1712 'query_var' => $name, 1713 'with_parameter' => true, 1714 ); 1715 1716 if ( ! is_array( $args ) ) { 1717 // For backward compatibility, if null has explicitly been passed as `$query_var`, assume `true`. 1718 if ( true === $args || null === func_get_arg( 2 ) ) { 1719 $args = array( 'query_var' => $name ); 1720 } elseif ( $args ) { 1721 $args = array( 'query_var' => $args ); 1722 } else { 1723 $args = array(); 1724 } 1698 1725 } 1699 $this->endpoints[] = array( $places, $name, $query_var ); 1726 $args = wp_parse_args( $args, $defaults ); 1727 1728 $args['name'] = $name; 1729 $args['mask'] = $places; 1730 1731 $this->endpoints[] = $args; 1700 1732 1701 if ( $ query_var) {1702 $wp->add_query_var( $ query_var);1733 if ( $args['query_var'] ) { 1734 $wp->add_query_var( $args['query_var'] ); 1703 1735 } 1704 1736 } 1705 1737 1706 1738 /** 1707 1739 * Adds a new permalink structure. 1708 1740 * 1709 1741 * A permalink structure (permastruct) is an abstract definition of a set of rewrite rules; 1710 1742 * it is an easy way of expressing a set of regular expressions that rewrite to a set of 1711 1743 * query strings. The new permastruct is added to the WP_Rewrite::$extra_permastructs array. 1712 1744 * 1713 1745 * When the rewrite rules are built by WP_Rewrite::rewrite_rules(), all of these extra 1714 1746 * permastructs are passed to WP_Rewrite::generate_rewrite_rules() which transforms them 1715 1747 * into the regular expressions that many love to hate. 1716 1748 * 1717 1749 * The `$args` parameter gives you control over how WP_Rewrite::generate_rewrite_rules() -
wp-includes/rewrite.php
function flush_rewrite_rules( $hard = tr 284 284 * will add a new rewrite rule ending with "json(/(.*))?/?$" for every permastruct 285 285 * that describes a permalink (post) or page. This is rewritten to "json=$match" 286 286 * where $match is the part of the URL matched by the endpoint regex (e.g. "foo" in 287 287 * "[permalink]/json/foo/"). 288 288 * 289 289 * A new query var with the same name as the endpoint will also be created. 290 290 * 291 291 * When specifying $places ensure that you are using the EP_* constants (or a 292 292 * combination of them using the bitwise OR operator) as their values are not 293 293 * guaranteed to remain static (especially `EP_ALL`). 294 294 * 295 295 * Be sure to flush the rewrite rules - see flush_rewrite_rules() - when your plugin gets 296 296 * activated and deactivated. 297 297 * 298 298 * @since 2.1.0 299 * @since 4.3.0 Added support for skipping query var registration by passing `false` to `$query_var`. 299 * @since 4.3.0 Added support for skipping query var registration by passing `false` to `$args[query_var]`. 300 * @since 4.x.0 Added support for registering a self-matching endpoint by passing `false` to `$args[with_parameter]`. 300 301 * 301 302 * @global WP_Rewrite $wp_rewrite 302 303 * 303 304 * @param string $name Name of the endpoint. 304 305 * @param int $places Endpoint mask describing the places the endpoint should be added. 305 * @param string|bool $query_var Name of the corresponding query variable. Pass `false` to skip registering a query_var 306 * for this endpoint. Defaults to the value of `$name`. 306 * @param array { 307 * @type string|bool $query_var Optional. Name of the corresponding query variable. Pass `false` 308 * to skip registering a query_var for this endpoint. Defaults to the 309 * value of `$name`. 310 * @type bool $with_parameter Optional. Specifies if the endpoint is self-matching or matching 311 * a parameter. Pass false to match the endpoint name. Defaults to true. 312 * } 307 313 */ 308 function add_rewrite_endpoint( $name, $places, $ query_var = true) {314 function add_rewrite_endpoint( $name, $places, $args = array() ) { 309 315 global $wp_rewrite; 310 $wp_rewrite->add_endpoint( $name, $places, $ query_var);316 $wp_rewrite->add_endpoint( $name, $places, $args ); 311 317 } 312 318 313 319 /** 314 320 * Filter the URL base for taxonomies. 315 321 * 316 322 * To remove any manually prepended /index.php/. 317 323 * 318 324 * @access private 319 325 * @since 2.6.0 320 326 * 321 327 * @param string $base The taxonomy base that we're going to filter 322 328 * @return string 323 329 */ 324 330 function _wp_filter_taxonomy_base( $base ) { 325 331 if ( !empty( $base ) ) {