Changeset 34957
- Timestamp:
- 10/08/2015 09:17:14 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-rewrite.php
r34955 r34957 457 457 global $wpdb; 458 458 459 // get pages in order of hierarchy, i.e. children after parents459 // Get pages in order of hierarchy, i.e. children after parents. 460 460 $pages = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page' AND post_status != 'auto-draft'"); 461 461 $posts = get_page_hierarchy( $pages ); 462 462 463 // If we have no pages get out quick 463 // If we have no pages get out quick. 464 464 if ( !$posts ) 465 465 return array( array(), array() ); 466 466 467 // now reverse it, because we need parents after children for rewrite rules to work properly467 // Now reverse it, because we need parents after children for rewrite rules to work properly. 468 468 $posts = array_reverse($posts, true); 469 469 … … 497 497 */ 498 498 public function page_rewrite_rules() { 499 // the extra .? at the beginning prevents clashes with other regular expressions in the rules array499 // The extra .? at the beginning prevents clashes with other regular expressions in the rules array. 500 500 $this->add_rewrite_tag( '%pagename%', '(.?.+?)', 'pagename=' ); 501 501 … … 548 548 $date_endian = '%year%/%monthnum%/%day%'; 549 549 550 // Do not allow the date tags and %post_id% to overlap in the permalink 551 // structure. If they do, move the date tags to $front/date/. 550 /* 551 * Do not allow the date tags and %post_id% to overlap in the permalink 552 * structure. If they do, move the date tags to $front/date/. 553 */ 552 554 $front = $this->front; 553 555 preg_match_all('/%.+?%/', $this->permalink_structure, $tokens); … … 860 862 */ 861 863 public function generate_rewrite_rules($permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true) { 862 // build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/?864 // Build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/? 863 865 $feedregex2 = ''; 864 866 foreach ( (array) $this->feeds as $feed_name) … … 866 868 $feedregex2 = '(' . trim($feedregex2, '|') . ')/?$'; 867 869 868 //$feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom 869 //and <permalink>/atom are both possible 870 /* 871 * $feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom 872 * and <permalink>/atom are both possible 873 */ 870 874 $feedregex = $this->feed_base . '/' . $feedregex2; 871 875 872 // build a regex to match the trackback and page/xx parts of URLs876 // Build a regex to match the trackback and page/xx parts of URLs. 873 877 $trackbackregex = 'trackback/?$'; 874 878 $pageregex = $this->pagination_base . '/?([0-9]{1,})/?$'; … … 876 880 $embedregex = 'embed/?$'; 877 881 878 // build up an array of endpoint regexes to append => queries to append882 // Build up an array of endpoint regexes to append => queries to append. 879 883 if ( $endpoints ) { 880 884 $ep_query_append = array (); 881 885 foreach ( (array) $this->endpoints as $endpoint) { 882 // match everything after the endpoint name, but allow for nothing to appear there886 // Match everything after the endpoint name, but allow for nothing to appear there. 883 887 $epmatch = $endpoint[1] . '(/(.*))?/?$'; 884 //this will be appended on to the rest of the query for each dir 888 889 // This will be appended on to the rest of the query for each dir. 885 890 $epquery = '&' . $endpoint[2] . '='; 886 891 $ep_query_append[$epmatch] = array ( $endpoint[0], $epquery ); … … 888 893 } 889 894 890 // get everything up to the first rewrite tag895 // Get everything up to the first rewrite tag. 891 896 $front = substr($permalink_structure, 0, strpos($permalink_structure, '%')); 892 //build an array of the tags (note that said array ends up being in $tokens[0]) 897 898 // Build an array of the tags (note that said array ends up being in $tokens[0]). 893 899 preg_match_all('/%.+?%/', $permalink_structure, $tokens); 894 900 … … 900 906 $embedindex = $index; 901 907 902 //build a list from the rewritecode and queryreplace arrays, that will look something like 903 //tagname=$matches[i] where i is the current $i 908 /* 909 * Build a list from the rewritecode and queryreplace arrays, that will look something 910 * like tagname=$matches[i] where i is the current $i. 911 */ 904 912 $queries = array(); 905 913 for ( $i = 0; $i < $num_tokens; ++$i ) { … … 913 921 } 914 922 915 // get the structure, minus any cruft (stuff that isn't tags) at the front923 // Get the structure, minus any cruft (stuff that isn't tags) at the front. 916 924 $structure = $permalink_structure; 917 925 if ( $front != '/' ) 918 926 $structure = str_replace($front, '', $structure); 919 927 920 //create a list of dirs to walk over, making rewrite rules for each level 921 //so for example, a $structure of /%year%/%monthnum%/%postname% would create 922 //rewrite rules for /%year%/, /%year%/%monthnum%/ and /%year%/%monthnum%/%postname% 928 /* 929 * Create a list of dirs to walk over, making rewrite rules for each level 930 * so for example, a $structure of /%year%/%monthnum%/%postname% would create 931 * rewrite rules for /%year%/, /%year%/%monthnum%/ and /%year%/%monthnum%/%postname% 932 */ 923 933 $structure = trim($structure, '/'); 924 934 $dirs = $walk_dirs ? explode('/', $structure) : array( $structure ); 925 935 $num_dirs = count($dirs); 926 936 927 // strip slashes from the front of $front937 // Strip slashes from the front of $front. 928 938 $front = preg_replace('|^/+|', '', $front); 929 939 930 // the main workhorse loop940 // The main workhorse loop. 931 941 $post_rewrite = array(); 932 942 $struct = $front; 933 943 for ( $j = 0; $j < $num_dirs; ++$j ) { 934 // get the struct for this dir, and trim slashes off the front935 $struct .= $dirs[$j] . '/'; // accumulate. see comment near explode('/', $structure) above944 // Get the struct for this dir, and trim slashes off the front. 945 $struct .= $dirs[$j] . '/'; // Accumulate. see comment near explode('/', $structure) above. 936 946 $struct = ltrim($struct, '/'); 937 947 938 // replace tags with regexes948 // Replace tags with regexes. 939 949 $match = str_replace($this->rewritecode, $this->rewritereplace, $struct); 940 950 941 // make a list of tags, and store how many there are in $num_toks951 // Make a list of tags, and store how many there are in $num_toks. 942 952 $num_toks = preg_match_all('/%.+?%/', $struct, $toks); 943 953 944 // get the 'tagname=$matches[i]'954 // Get the 'tagname=$matches[i]'. 945 955 $query = ( ! empty( $num_toks ) && isset( $queries[$num_toks - 1] ) ) ? $queries[$num_toks - 1] : ''; 946 956 947 // set up $ep_mask_specific which is used to match more specific URL types957 // Set up $ep_mask_specific which is used to match more specific URL types. 948 958 switch ( $dirs[$j] ) { 949 959 case '%year%': … … 960 970 } 961 971 962 // create query for /page/xx972 // Create query for /page/xx. 963 973 $pagematch = $match . $pageregex; 964 974 $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1); 965 975 966 // create query for /comment-page-xx976 // Create query for /comment-page-xx. 967 977 $commentmatch = $match . $commentregex; 968 978 $commentquery = $index . '?' . $query . '&cpage=' . $this->preg_index($num_toks + 1); 969 979 970 980 if ( get_option('page_on_front') ) { 971 // create query for Root /comment-page-xx981 // Create query for Root /comment-page-xx. 972 982 $rootcommentmatch = $match . $commentregex; 973 983 $rootcommentquery = $index . '?' . $query . '&page_id=' . get_option('page_on_front') . '&cpage=' . $this->preg_index($num_toks + 1); 974 984 } 975 985 976 // create query for /feed/(feed|atom|rss|rss2|rdf)986 // Create query for /feed/(feed|atom|rss|rss2|rdf). 977 987 $feedmatch = $match . $feedregex; 978 988 $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1); 979 989 980 // create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex)990 // Create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex). 981 991 $feedmatch2 = $match . $feedregex2; 982 992 $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1); 983 993 984 // if asked to, turn the feed queries into comment feed ones994 // If asked to, turn the feed queries into comment feed ones. 985 995 if ( $forcomments ) { 986 996 $feedquery .= '&withcomments=1'; … … 988 998 } 989 999 990 // start creating the array of rewrites for this dir1000 // Start creating the array of rewrites for this dir. 991 1001 $rewrite = array(); 992 if ( $feed ) //...adding on /feed/ regexes => queries 993 $rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2); 994 if ( $paged ) //...and /page/xx ones 995 $rewrite = array_merge($rewrite, array($pagematch => $pagequery)); 996 997 //only on pages with comments add ../comment-page-xx/ 1002 1003 // ...adding on /feed/ regexes => queries 1004 if ( $feed ) { 1005 $rewrite = array( $feedmatch => $feedquery, $feedmatch2 => $feedquery2 ); 1006 } 1007 1008 //...and /page/xx ones 1009 if ( $paged ) { 1010 $rewrite = array_merge( $rewrite, array( $pagematch => $pagequery ) ); 1011 } 1012 1013 // Only on pages with comments add ../comment-page-xx/. 998 1014 if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask ) { 999 1015 $rewrite = array_merge($rewrite, array($commentmatch => $commentquery)); … … 1001 1017 $rewrite = array_merge($rewrite, array($rootcommentmatch => $rootcommentquery)); 1002 1018 } 1003 //do endpoints 1019 1020 // Do endpoints. 1004 1021 if ( $endpoints ) { 1005 1022 foreach ( (array) $ep_query_append as $regex => $ep) { 1006 // add the endpoints on if the mask fits1023 // Add the endpoints on if the mask fits. 1007 1024 if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific ) 1008 1025 $rewrite[$match . $regex] = $index . '?' . $query . $ep[1] . $this->preg_index($num_toks + 2); … … 1010 1027 } 1011 1028 1012 // if we've got some tags in this dir1029 // If we've got some tags in this dir. 1013 1030 if ( $num_toks ) { 1014 1031 $post = false; 1015 1032 $page = false; 1016 1033 1017 //check to see if this dir is permalink-level: i.e. the structure specifies an 1018 //individual post. Do this by checking it contains at least one of 1) post name, 1019 //2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and 1020 //minute all present). Set these flags now as we need them for the endpoints. 1034 /* 1035 * Check to see if this dir is permalink-level: i.e. the structure specifies an 1036 * individual post. Do this by checking it contains at least one of 1) post name, 1037 * 2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and 1038 * minute all present). Set these flags now as we need them for the endpoints. 1039 */ 1021 1040 if ( strpos($struct, '%postname%') !== false 1022 1041 || strpos($struct, '%post_id%') !== false … … 1034 1053 if ( strpos($struct, "%$ptype%") !== false ) { 1035 1054 $post = true; 1036 $page = is_post_type_hierarchical( $ptype ); // This is for page style attachment url's 1055 1056 // This is for page style attachment URLs. 1057 $page = is_post_type_hierarchical( $ptype ); 1037 1058 break; 1038 1059 } … … 1040 1061 } 1041 1062 1042 // if we're creating rules for a permalink, do all the endpoints like attachments etc1063 // If creating rules for a permalink, do all the endpoints like attachments etc. 1043 1064 if ( $post ) { 1044 // create query and regex for trackback1065 // Create query and regex for trackback. 1045 1066 $trackbackmatch = $match . $trackbackregex; 1046 1067 $trackbackquery = $trackbackindex . '?' . $query . '&tb=1'; … … 1050 1071 $embedquery = $embedindex . '?' . $query . '&embed=true'; 1051 1072 1052 // trim slashes from the end of the regex for this dir1073 // Trim slashes from the end of the regex for this dir. 1053 1074 $match = rtrim($match, '/'); 1054 1075 1055 // get rid of brackets1076 // Get rid of brackets. 1056 1077 $submatchbase = str_replace( array('(', ')'), '', $match); 1057 1078 1058 // add a rule for at attachments, which take the form of <permalink>/some-text1079 // Add a rule for at attachments, which take the form of <permalink>/some-text. 1059 1080 $sub1 = $submatchbase . '/([^/]+)/'; 1060 $sub1tb = $sub1 . $trackbackregex; //add trackback regex <permalink>/trackback/... 1061 $sub1feed = $sub1 . $feedregex; //and <permalink>/feed/(atom|...) 1062 $sub1feed2 = $sub1 . $feedregex2; //and <permalink>/(feed|atom...) 1063 $sub1comment = $sub1 . $commentregex; //and <permalink>/comment-page-xx 1064 $sub1embed = $sub1 . $embedregex; //and <permalink>/embed/... 1065 1066 //add another rule to match attachments in the explicit form: 1067 //<permalink>/attachment/some-text 1081 1082 // Add trackback regex <permalink>/trackback/... 1083 $sub1tb = $sub1 . $trackbackregex; 1084 1085 // And <permalink>/feed/(atom|...) 1086 $sub1feed = $sub1 . $feedregex; 1087 1088 // And <permalink>/(feed|atom...) 1089 $sub1feed2 = $sub1 . $feedregex2; 1090 1091 // And <permalink>/comment-page-xx 1092 $sub1comment = $sub1 . $commentregex; 1093 1094 // And <permalink>/embed/... 1095 $sub1embed = $sub1 . $embedregex; 1096 1097 /* 1098 * Add another rule to match attachments in the explicit form: 1099 * <permalink>/attachment/some-text 1100 */ 1068 1101 $sub2 = $submatchbase . '/attachment/([^/]+)/'; 1069 $sub2tb = $sub2 . $trackbackregex; //and add trackbacks <permalink>/attachment/trackback 1070 $sub2feed = $sub2 . $feedregex; //feeds, <permalink>/attachment/feed/(atom|...) 1071 $sub2feed2 = $sub2 . $feedregex2; //and feeds again on to this <permalink>/attachment/(feed|atom...) 1072 $sub2comment = $sub2 . $commentregex; //and <permalink>/comment-page-xx 1073 $sub2embed = $sub2 . $embedregex; //and <permalink>/embed/... 1074 1075 //create queries for these extra tag-ons we've just dealt with 1102 1103 // And add trackbacks <permalink>/attachment/trackback. 1104 $sub2tb = $sub2 . $trackbackregex; 1105 1106 // Feeds, <permalink>/attachment/feed/(atom|...) 1107 $sub2feed = $sub2 . $feedregex; 1108 1109 // And feeds again on to this <permalink>/attachment/(feed|atom...) 1110 $sub2feed2 = $sub2 . $feedregex2; 1111 1112 // And <permalink>/comment-page-xx 1113 $sub2comment = $sub2 . $commentregex; 1114 1115 // And <permalink>/embed/... 1116 $sub2embed = $sub2 . $embedregex; 1117 1118 // Create queries for these extra tag-ons we've just dealt with. 1076 1119 $subquery = $index . '?attachment=' . $this->preg_index(1); 1077 1120 $subtbquery = $subquery . '&tb=1'; … … 1080 1123 $subembedquery = $subquery . '&embed=true'; 1081 1124 1082 // do endpoints for attachments1125 // Do endpoints for attachments. 1083 1126 if ( !empty($endpoints) ) { 1084 1127 foreach ( (array) $ep_query_append as $regex => $ep ) { … … 1090 1133 } 1091 1134 1092 //now we've finished with endpoints, finish off the $sub1 and $sub2 matches 1093 //add a ? as we don't have to match that last slash, and finally a $ so we 1094 //match to the end of the URL 1135 /* 1136 * Now we've finished with endpoints, finish off the $sub1 and $sub2 matches 1137 * add a ? as we don't have to match that last slash, and finally a $ so we 1138 * match to the end of the URL 1139 */ 1095 1140 $sub1 .= '?$'; 1096 1141 $sub2 .= '?$'; 1097 1142 1098 // Post pagination, e.g. <permalink>/2/ 1099 // Previously: '(/[0-9]+)?/?$', which produced '/2' for page. 1100 // When cast to int, returned 0. 1143 /* 1144 * Post pagination, e.g. <permalink>/2/ 1145 * Previously: '(/[0-9]+)?/?$', which produced '/2' for page. 1146 * When cast to int, returned 0. 1147 */ 1101 1148 $match = $match . '(?:/([0-9]+))?/?$'; 1102 1149 $query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1); 1103 } else { //not matching a permalink so this is a lot simpler 1104 //close the match and finalise the query 1150 1151 // Not matching a permalink so this is a lot simpler. 1152 } else { 1153 // Close the match and finalise the query. 1105 1154 $match .= '?$'; 1106 1155 $query = $index . '?' . $query; 1107 1156 } 1108 1157 1109 //create the final array for this dir by joining the $rewrite array (which currently 1110 //only contains rules/queries for trackback, pages etc) to the main regex/query for 1111 //this dir 1158 /* 1159 * Create the final array for this dir by joining the $rewrite array (which currently 1160 * only contains rules/queries for trackback, pages etc) to the main regex/query for 1161 * this dir 1162 */ 1112 1163 $rewrite = array_merge($rewrite, array($match => $query)); 1113 1164 1114 // if we're matching a permalink, add those extras (attachments etc) on1165 // If we're matching a permalink, add those extras (attachments etc) on. 1115 1166 if ( $post ) { 1116 // add trackback1167 // Add trackback. 1117 1168 $rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite); 1118 1169 1119 // add embed1170 // Add embed. 1120 1171 $rewrite = array_merge( array( $embedmatch => $embedquery ), $rewrite ); 1121 1172 1122 // add regexes/queries for attachments, attachment trackbacks and so on1173 // Add regexes/queries for attachments, attachment trackbacks and so on. 1123 1174 if ( ! $page ) { 1124 // require <permalink>/attachment/stuff form for pages because of confusion with subpages1175 // Require <permalink>/attachment/stuff form for pages because of confusion with subpages. 1125 1176 $rewrite = array_merge( $rewrite, array( 1126 1177 $sub1 => $subquery, … … 1135 1186 $rewrite = array_merge( array( $sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery, $sub2comment => $subcommentquery, $sub2embed => $subembedquery ), $rewrite ); 1136 1187 } 1137 } //if($num_toks)1138 // add the rules for this dir to the accumulating $post_rewrite1188 } 1189 // Add the rules for this dir to the accumulating $post_rewrite. 1139 1190 $post_rewrite = array_merge($rewrite, $post_rewrite); 1140 } //foreach ($dir) 1141 return $post_rewrite; //the finished rules. phew! 1191 } 1192 1193 // The finished rules. phew! 1194 return $post_rewrite; 1142 1195 } 1143 1196
Note: See TracChangeset
for help on using the changeset viewer.