| 858 | | class WP_Rewrite { |
|---|
| 859 | | var $permalink_structure; |
|---|
| 860 | | var $category_base; |
|---|
| 861 | | var $category_structure; |
|---|
| 862 | | var $author_base = 'author'; |
|---|
| 863 | | var $author_structure; |
|---|
| 864 | | var $date_structure; |
|---|
| 865 | | var $page_structure; |
|---|
| 866 | | var $search_base = 'search'; |
|---|
| 867 | | var $search_structure; |
|---|
| 868 | | var $comments_base = 'comments'; |
|---|
| 869 | | var $feed_base = 'feed'; |
|---|
| 870 | | var $comments_feed_structure; |
|---|
| 871 | | var $feed_structure; |
|---|
| 872 | | var $front; |
|---|
| 873 | | var $root = ''; |
|---|
| 874 | | var $index = 'index.php'; |
|---|
| 875 | | var $matches = ''; |
|---|
| 876 | | var $rules; |
|---|
| 877 | | var $use_verbose_rules = false; |
|---|
| 878 | | var $rewritecode = |
|---|
| 879 | | array( |
|---|
| 880 | | '%year%', |
|---|
| 881 | | '%monthnum%', |
|---|
| 882 | | '%day%', |
|---|
| 883 | | '%hour%', |
|---|
| 884 | | '%minute%', |
|---|
| 885 | | '%second%', |
|---|
| 886 | | '%postname%', |
|---|
| 887 | | '%post_id%', |
|---|
| 888 | | '%category%', |
|---|
| 889 | | '%author%', |
|---|
| 890 | | '%pagename%', |
|---|
| 891 | | '%search%' |
|---|
| 892 | | ); |
|---|
| 893 | | |
|---|
| 894 | | var $rewritereplace = |
|---|
| 895 | | array( |
|---|
| 896 | | '([0-9]{4})', |
|---|
| 897 | | '([0-9]{1,2})', |
|---|
| 898 | | '([0-9]{1,2})', |
|---|
| 899 | | '([0-9]{1,2})', |
|---|
| 900 | | '([0-9]{1,2})', |
|---|
| 901 | | '([0-9]{1,2})', |
|---|
| 902 | | '([^/]+)', |
|---|
| 903 | | '([0-9]+)', |
|---|
| 904 | | '(.+?)', |
|---|
| 905 | | '([^/]+)', |
|---|
| 906 | | '([^/]+)', |
|---|
| 907 | | '(.+)' |
|---|
| 908 | | ); |
|---|
| 909 | | |
|---|
| 910 | | var $queryreplace = |
|---|
| 911 | | array ( |
|---|
| 912 | | 'year=', |
|---|
| 913 | | 'monthnum=', |
|---|
| 914 | | 'day=', |
|---|
| 915 | | 'hour=', |
|---|
| 916 | | 'minute=', |
|---|
| 917 | | 'second=', |
|---|
| 918 | | 'name=', |
|---|
| 919 | | 'p=', |
|---|
| 920 | | 'category_name=', |
|---|
| 921 | | 'author_name=', |
|---|
| 922 | | 'pagename=', |
|---|
| 923 | | 's=' |
|---|
| 924 | | ); |
|---|
| 925 | | |
|---|
| 926 | | var $feeds = array ('feed', 'rdf', 'rss', 'rss2', 'atom'); |
|---|
| 927 | | |
|---|
| 928 | | function using_permalinks() { |
|---|
| 929 | | if (empty($this->permalink_structure)) |
|---|
| 930 | | return false; |
|---|
| 931 | | else |
|---|
| 932 | | return true; |
|---|
| 933 | | } |
|---|
| 934 | | |
|---|
| 935 | | function using_index_permalinks() { |
|---|
| 936 | | if (empty($this->permalink_structure)) { |
|---|
| 937 | | return false; |
|---|
| 938 | | } |
|---|
| 939 | | |
|---|
| 940 | | // If the index is not in the permalink, we're using mod_rewrite. |
|---|
| 941 | | if (preg_match('#^/*' . $this->index . '#', $this->permalink_structure)) { |
|---|
| 942 | | return true; |
|---|
| 943 | | } |
|---|
| 944 | | |
|---|
| 945 | | return false; |
|---|
| 946 | | } |
|---|
| 947 | | |
|---|
| 948 | | function using_mod_rewrite_permalinks() { |
|---|
| 949 | | if ( $this->using_permalinks() && ! $this->using_index_permalinks()) |
|---|
| 950 | | return true; |
|---|
| 951 | | else |
|---|
| 952 | | return false; |
|---|
| 953 | | } |
|---|
| 954 | | |
|---|
| 955 | | function preg_index($number) { |
|---|
| 956 | | $match_prefix = '$'; |
|---|
| 957 | | $match_suffix = ''; |
|---|
| 958 | | |
|---|
| 959 | | if (! empty($this->matches)) { |
|---|
| 960 | | $match_prefix = '$' . $this->matches . '['; |
|---|
| 961 | | $match_suffix = ']'; |
|---|
| 962 | | } |
|---|
| 963 | | |
|---|
| 964 | | return "$match_prefix$number$match_suffix"; |
|---|
| 965 | | } |
|---|
| 966 | | |
|---|
| 967 | | function page_rewrite_rules() { |
|---|
| 968 | | $uris = get_settings('page_uris'); |
|---|
| 969 | | $attachment_uris = get_settings('page_attachment_uris'); |
|---|
| 970 | | |
|---|
| 971 | | $rewrite_rules = array(); |
|---|
| 972 | | $page_structure = $this->get_page_permastruct(); |
|---|
| 973 | | if( is_array( $attachment_uris ) ) { |
|---|
| 974 | | foreach ($attachment_uris as $uri => $pagename) { |
|---|
| 975 | | $this->add_rewrite_tag('%pagename%', "($uri)", 'attachment='); |
|---|
| 976 | | $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure)); |
|---|
| 977 | | } |
|---|
| 978 | | } |
|---|
| 979 | | if( is_array( $uris ) ) { |
|---|
| 980 | | foreach ($uris as $uri => $pagename) { |
|---|
| 981 | | $this->add_rewrite_tag('%pagename%', "($uri)", 'pagename='); |
|---|
| 982 | | $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure)); |
|---|
| 983 | | } |
|---|
| 984 | | } |
|---|
| 985 | | |
|---|
| 986 | | return $rewrite_rules; |
|---|
| 987 | | } |
|---|
| 988 | | |
|---|
| 989 | | function get_date_permastruct() { |
|---|
| 990 | | if (isset($this->date_structure)) { |
|---|
| 991 | | return $this->date_structure; |
|---|
| 992 | | } |
|---|
| 993 | | |
|---|
| 994 | | if (empty($this->permalink_structure)) { |
|---|
| 995 | | $this->date_structure = ''; |
|---|
| 996 | | return false; |
|---|
| 997 | | } |
|---|
| 998 | | |
|---|
| 999 | | // The date permalink must have year, month, and day separated by slashes. |
|---|
| 1000 | | $endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%'); |
|---|
| 1001 | | |
|---|
| 1002 | | $this->date_structure = ''; |
|---|
| 1003 | | $date_endian = ''; |
|---|
| 1004 | | |
|---|
| 1005 | | foreach ($endians as $endian) { |
|---|
| 1006 | | if (false !== strpos($this->permalink_structure, $endian)) { |
|---|
| 1007 | | $date_endian= $endian; |
|---|
| 1008 | | break; |
|---|
| 1009 | | } |
|---|
| 1010 | | } |
|---|
| 1011 | | |
|---|
| 1012 | | if ( empty($date_endian) ) |
|---|
| 1013 | | $date_endian = '%year%/%monthnum%/%day%'; |
|---|
| 1014 | | |
|---|
| 1015 | | // Do not allow the date tags and %post_id% to overlap in the permalink |
|---|
| 1016 | | // structure. If they do, move the date tags to $front/date/. |
|---|
| 1017 | | $front = $this->front; |
|---|
| 1018 | | preg_match_all('/%.+?%/', $this->permalink_structure, $tokens); |
|---|
| 1019 | | $tok_index = 1; |
|---|
| 1020 | | foreach ($tokens[0] as $token) { |
|---|
| 1021 | | if ( ($token == '%post_id%') && ($tok_index <= 3) ) { |
|---|
| 1022 | | $front = $front . 'date/'; |
|---|
| 1023 | | break; |
|---|
| 1024 | | } |
|---|
| 1025 | | } |
|---|
| 1026 | | |
|---|
| 1027 | | $this->date_structure = $front . $date_endian; |
|---|
| 1028 | | |
|---|
| 1029 | | return $this->date_structure; |
|---|
| 1030 | | } |
|---|
| 1031 | | |
|---|
| 1032 | | function get_year_permastruct() { |
|---|
| 1033 | | $structure = $this->get_date_permastruct($this->permalink_structure); |
|---|
| 1034 | | |
|---|
| 1035 | | if (empty($structure)) { |
|---|
| 1036 | | return false; |
|---|
| 1037 | | } |
|---|
| 1038 | | |
|---|
| 1039 | | $structure = str_replace('%monthnum%', '', $structure); |
|---|
| 1040 | | $structure = str_replace('%day%', '', $structure); |
|---|
| 1041 | | |
|---|
| 1042 | | $structure = preg_replace('#/+#', '/', $structure); |
|---|
| 1043 | | |
|---|
| 1044 | | return $structure; |
|---|
| 1045 | | } |
|---|
| 1046 | | |
|---|
| 1047 | | function get_month_permastruct() { |
|---|
| 1048 | | $structure = $this->get_date_permastruct($this->permalink_structure); |
|---|
| 1049 | | |
|---|
| 1050 | | if (empty($structure)) { |
|---|
| 1051 | | return false; |
|---|
| 1052 | | } |
|---|
| 1053 | | |
|---|
| 1054 | | $structure = str_replace('%day%', '', $structure); |
|---|
| 1055 | | |
|---|
| 1056 | | $structure = preg_replace('#/+#', '/', $structure); |
|---|
| 1057 | | |
|---|
| 1058 | | return $structure; |
|---|
| 1059 | | } |
|---|
| 1060 | | |
|---|
| 1061 | | function get_day_permastruct() { |
|---|
| 1062 | | return $this->get_date_permastruct($this->permalink_structure); |
|---|
| 1063 | | } |
|---|
| 1064 | | |
|---|
| 1065 | | function get_category_permastruct() { |
|---|
| 1066 | | if (isset($this->category_structure)) { |
|---|
| 1067 | | return $this->category_structure; |
|---|
| 1068 | | } |
|---|
| 1069 | | |
|---|
| 1070 | | if (empty($this->permalink_structure)) { |
|---|
| 1071 | | $this->category_structure = ''; |
|---|
| 1072 | | return false; |
|---|
| 1073 | | } |
|---|
| 1074 | | |
|---|
| 1075 | | if (empty($this->category_base)) |
|---|
| 1076 | | $this->category_structure = $this->front . 'category/'; |
|---|
| 1077 | | else |
|---|
| 1078 | | $this->category_structure = $this->category_base . '/'; |
|---|
| 1079 | | |
|---|
| 1080 | | $this->category_structure .= '%category%'; |
|---|
| 1081 | | |
|---|
| 1082 | | return $this->category_structure; |
|---|
| 1083 | | } |
|---|
| 1084 | | |
|---|
| 1085 | | function get_author_permastruct() { |
|---|
| 1086 | | if (isset($this->author_structure)) { |
|---|
| 1087 | | return $this->author_structure; |
|---|
| 1088 | | } |
|---|
| 1089 | | |
|---|
| 1090 | | if (empty($this->permalink_structure)) { |
|---|
| 1091 | | $this->author_structure = ''; |
|---|
| 1092 | | return false; |
|---|
| 1093 | | } |
|---|
| 1094 | | |
|---|
| 1095 | | $this->author_structure = $this->front . $this->author_base . '/%author%'; |
|---|
| 1096 | | |
|---|
| 1097 | | return $this->author_structure; |
|---|
| 1098 | | } |
|---|
| 1099 | | |
|---|
| 1100 | | function get_search_permastruct() { |
|---|
| 1101 | | if (isset($this->search_structure)) { |
|---|
| 1102 | | return $this->search_structure; |
|---|
| 1103 | | } |
|---|
| 1104 | | |
|---|
| 1105 | | if (empty($this->permalink_structure)) { |
|---|
| 1106 | | $this->search_structure = ''; |
|---|
| 1107 | | return false; |
|---|
| 1108 | | } |
|---|
| 1109 | | |
|---|
| 1110 | | $this->search_structure = $this->root . $this->search_base . '/%search%'; |
|---|
| 1111 | | |
|---|
| 1112 | | return $this->search_structure; |
|---|
| 1113 | | } |
|---|
| 1114 | | |
|---|
| 1115 | | function get_page_permastruct() { |
|---|
| 1116 | | if (isset($this->page_structure)) { |
|---|
| 1117 | | return $this->page_structure; |
|---|
| 1118 | | } |
|---|
| 1119 | | |
|---|
| 1120 | | if (empty($this->permalink_structure)) { |
|---|
| 1121 | | $this->page_structure = ''; |
|---|
| 1122 | | return false; |
|---|
| 1123 | | } |
|---|
| 1124 | | |
|---|
| 1125 | | $this->page_structure = $this->root . '%pagename%'; |
|---|
| 1126 | | |
|---|
| 1127 | | return $this->page_structure; |
|---|
| 1128 | | } |
|---|
| 1129 | | |
|---|
| 1130 | | function get_feed_permastruct() { |
|---|
| 1131 | | if (isset($this->feed_structure)) { |
|---|
| 1132 | | return $this->feed_structure; |
|---|
| 1133 | | } |
|---|
| 1134 | | |
|---|
| 1135 | | if (empty($this->permalink_structure)) { |
|---|
| 1136 | | $this->feed_structure = ''; |
|---|
| 1137 | | return false; |
|---|
| 1138 | | } |
|---|
| 1139 | | |
|---|
| 1140 | | $this->feed_structure = $this->root . $this->feed_base . '/%feed%'; |
|---|
| 1141 | | |
|---|
| 1142 | | return $this->feed_structure; |
|---|
| 1143 | | } |
|---|
| 1144 | | |
|---|
| 1145 | | function get_comment_feed_permastruct() { |
|---|
| 1146 | | if (isset($this->comment_feed_structure)) { |
|---|
| 1147 | | return $this->comment_feed_structure; |
|---|
| 1148 | | } |
|---|
| 1149 | | |
|---|
| 1150 | | if (empty($this->permalink_structure)) { |
|---|
| 1151 | | $this->comment_feed_structure = ''; |
|---|
| 1152 | | return false; |
|---|
| 1153 | | } |
|---|
| 1154 | | |
|---|
| 1155 | | $this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%'; |
|---|
| 1156 | | |
|---|
| 1157 | | return $this->comment_feed_structure; |
|---|
| 1158 | | } |
|---|
| 1159 | | |
|---|
| 1160 | | function add_rewrite_tag($tag, $pattern, $query) { |
|---|
| 1161 | | // If the tag already exists, replace the existing pattern and query for |
|---|
| 1162 | | // that tag, otherwise add the new tag, pattern, and query to the end of |
|---|
| 1163 | | // the arrays. |
|---|
| 1164 | | $position = array_search($tag, $this->rewritecode); |
|---|
| 1165 | | if (FALSE !== $position && NULL !== $position) { |
|---|
| 1166 | | $this->rewritereplace[$position] = $pattern; |
|---|
| 1167 | | $this->queryreplace[$position] = $query; |
|---|
| 1168 | | } else { |
|---|
| 1169 | | $this->rewritecode[] = $tag; |
|---|
| 1170 | | $this->rewritereplace[] = $pattern; |
|---|
| 1171 | | $this->queryreplace[] = $query; |
|---|
| 1172 | | } |
|---|
| 1173 | | } |
|---|
| 1174 | | |
|---|
| 1175 | | function generate_rewrite_rules($permalink_structure, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true) { |
|---|
| 1176 | | $feedregex2 = ''; |
|---|
| 1177 | | foreach ($this->feeds as $feed_name) { |
|---|
| 1178 | | $feedregex2 .= $feed_name . '|'; |
|---|
| 1179 | | } |
|---|
| 1180 | | $feedregex2 = '(' . trim($feedregex2, '|') . ')/?$'; |
|---|
| 1181 | | $feedregex = $this->feed_base . '/' . $feedregex2; |
|---|
| 1182 | | |
|---|
| 1183 | | $trackbackregex = 'trackback/?$'; |
|---|
| 1184 | | $pageregex = 'page/?([0-9]{1,})/?$'; |
|---|
| 1185 | | |
|---|
| 1186 | | $front = substr($permalink_structure, 0, strpos($permalink_structure, '%')); |
|---|
| 1187 | | preg_match_all('/%.+?%/', $permalink_structure, $tokens); |
|---|
| 1188 | | |
|---|
| 1189 | | $num_tokens = count($tokens[0]); |
|---|
| 1190 | | |
|---|
| 1191 | | $index = $this->index; |
|---|
| 1192 | | $feedindex = $index; |
|---|
| 1193 | | $trackbackindex = $index; |
|---|
| 1194 | | for ($i = 0; $i < $num_tokens; ++$i) { |
|---|
| 1195 | | if (0 < $i) { |
|---|
| 1196 | | $queries[$i] = $queries[$i - 1] . '&'; |
|---|
| 1197 | | } |
|---|
| 1198 | | |
|---|
| 1199 | | $query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1); |
|---|
| 1200 | | $queries[$i] .= $query_token; |
|---|
| 1201 | | } |
|---|
| 1202 | | |
|---|
| 1203 | | $structure = $permalink_structure; |
|---|
| 1204 | | if ($front != '/') { |
|---|
| 1205 | | $structure = str_replace($front, '', $structure); |
|---|
| 1206 | | } |
|---|
| 1207 | | $structure = trim($structure, '/'); |
|---|
| 1208 | | if ($walk_dirs) { |
|---|
| 1209 | | $dirs = explode('/', $structure); |
|---|
| 1210 | | } else { |
|---|
| 1211 | | $dirs[] = $structure; |
|---|
| 1212 | | } |
|---|
| 1213 | | $num_dirs = count($dirs); |
|---|
| 1214 | | |
|---|
| 1215 | | $front = preg_replace('|^/+|', '', $front); |
|---|
| 1216 | | |
|---|
| 1217 | | $post_rewrite = array(); |
|---|
| 1218 | | $struct = $front; |
|---|
| 1219 | | for ($j = 0; $j < $num_dirs; ++$j) { |
|---|
| 1220 | | $struct .= $dirs[$j] . '/'; |
|---|
| 1221 | | $struct = ltrim($struct, '/'); |
|---|
| 1222 | | $match = str_replace($this->rewritecode, $this->rewritereplace, $struct); |
|---|
| 1223 | | $num_toks = preg_match_all('/%.+?%/', $struct, $toks); |
|---|
| 1224 | | $query = $queries[$num_toks - 1]; |
|---|
| 1225 | | |
|---|
| 1226 | | $pagematch = $match . $pageregex; |
|---|
| 1227 | | $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1); |
|---|
| 1228 | | |
|---|
| 1229 | | $feedmatch = $match . $feedregex; |
|---|
| 1230 | | $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1); |
|---|
| 1231 | | |
|---|
| 1232 | | $feedmatch2 = $match . $feedregex2; |
|---|
| 1233 | | $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1); |
|---|
| 1234 | | |
|---|
| 1235 | | if ($forcomments) { |
|---|
| 1236 | | $feedquery .= '&withcomments=1'; |
|---|
| 1237 | | $feedquery2 .= '&withcomments=1'; |
|---|
| 1238 | | } |
|---|
| 1239 | | |
|---|
| 1240 | | $rewrite = array(); |
|---|
| 1241 | | if ($feed) |
|---|
| 1242 | | $rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2); |
|---|
| 1243 | | if ($paged) |
|---|
| 1244 | | $rewrite = array_merge($rewrite, array($pagematch => $pagequery)); |
|---|
| 1245 | | |
|---|
| 1246 | | if ($num_toks) { |
|---|
| 1247 | | $post = false; |
|---|
| 1248 | | $page = false; |
|---|
| 1249 | | if (strstr($struct, '%postname%') || strstr($struct, '%post_id%') |
|---|
| 1250 | | || strstr($struct, '%pagename%') |
|---|
| 1251 | | || (strstr($struct, '%year%') && strstr($struct, '%monthnum%') && strstr($struct, '%day%') && strstr($struct, '%hour%') && strstr($struct, '%minute') && strstr($struct, '%second%'))) { |
|---|
| 1252 | | $post = true; |
|---|
| 1253 | | if ( strstr($struct, '%pagename%') ) |
|---|
| 1254 | | $page = true; |
|---|
| 1255 | | $trackbackmatch = $match . $trackbackregex; |
|---|
| 1256 | | $trackbackquery = $trackbackindex . '?' . $query . '&tb=1'; |
|---|
| 1257 | | $match = rtrim($match, '/'); |
|---|
| 1258 | | $submatchbase = str_replace(array('(',')'),'',$match); |
|---|
| 1259 | | $sub1 = $submatchbase . '/([^/]+)/'; |
|---|
| 1260 | | $sub1tb = $sub1 . $trackbackregex; |
|---|
| 1261 | | $sub1feed = $sub1 . $feedregex; |
|---|
| 1262 | | $sub1feed2 = $sub1 . $feedregex2; |
|---|
| 1263 | | $sub1 .= '?$'; |
|---|
| 1264 | | $sub2 = $submatchbase . '/attachment/([^/]+)/'; |
|---|
| 1265 | | $sub2tb = $sub2 . $trackbackregex; |
|---|
| 1266 | | $sub2feed = $sub2 . $feedregex; |
|---|
| 1267 | | $sub2feed2 = $sub2 . $feedregex2; |
|---|
| 1268 | | $sub2 .= '?$'; |
|---|
| 1269 | | $subquery = $index . '?attachment=' . $this->preg_index(1); |
|---|
| 1270 | | $subtbquery = $subquery . '&tb=1'; |
|---|
| 1271 | | $subfeedquery = $subquery . '&feed=' . $this->preg_index(2); |
|---|
| 1272 | | $match = $match . '(/[0-9]+)?/?$'; |
|---|
| 1273 | | $query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1); |
|---|
| 1274 | | } else { |
|---|
| 1275 | | $match .= '?$'; |
|---|
| 1276 | | $query = $index . '?' . $query; |
|---|
| 1277 | | } |
|---|
| 1278 | | |
|---|
| 1279 | | $rewrite = array_merge($rewrite, array($match => $query)); |
|---|
| 1280 | | |
|---|
| 1281 | | if ($post) { |
|---|
| 1282 | | $rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite); |
|---|
| 1283 | | if ( ! $page ) |
|---|
| 1284 | | $rewrite = array_merge($rewrite, array($sub1 => $subquery, $sub1tb => $subtbquery, $sub1feed => $subfeedquery, $sub1feed2 => $subfeedquery)); |
|---|
| 1285 | | $rewrite = array_merge($rewrite, array($sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery)); |
|---|
| 1286 | | } |
|---|
| 1287 | | } |
|---|
| 1288 | | $post_rewrite = array_merge($rewrite, $post_rewrite); |
|---|
| 1289 | | } |
|---|
| 1290 | | return $post_rewrite; |
|---|
| 1291 | | } |
|---|
| 1292 | | |
|---|
| 1293 | | function generate_rewrite_rule($permalink_structure, $walk_dirs = false) { |
|---|
| 1294 | | return $this->generate_rewrite_rules($permalink_structure, false, false, false, $walk_dirs); |
|---|
| 1295 | | } |
|---|
| 1296 | | |
|---|
| 1297 | | /* rewrite_rules |
|---|
| 1298 | | * Construct rewrite matches and queries from permalink structure. |
|---|
| 1299 | | * Returns an associate array of matches and queries. |
|---|
| 1300 | | */ |
|---|
| 1301 | | function rewrite_rules() { |
|---|
| 1302 | | $rewrite = array(); |
|---|
| 1303 | | |
|---|
| 1304 | | if (empty($this->permalink_structure)) { |
|---|
| 1305 | | return $rewrite; |
|---|
| 1306 | | } |
|---|
| 1307 | | |
|---|
| 1308 | | // Post |
|---|
| 1309 | | $post_rewrite = $this->generate_rewrite_rules($this->permalink_structure); |
|---|
| 1310 | | $post_rewrite = apply_filters('post_rewrite_rules', $post_rewrite); |
|---|
| 1311 | | |
|---|
| 1312 | | // Date |
|---|
| 1313 | | $date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct()); |
|---|
| 1314 | | $date_rewrite = apply_filters('date_rewrite_rules', $date_rewrite); |
|---|
| 1315 | | |
|---|
| 1316 | | // Root |
|---|
| 1317 | | $root_rewrite = $this->generate_rewrite_rules($this->root . '/'); |
|---|
| 1318 | | $root_rewrite = apply_filters('root_rewrite_rules', $root_rewrite); |
|---|
| 1319 | | |
|---|
| 1320 | | // Comments |
|---|
| 1321 | | $comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, true, true, true, false); |
|---|
| 1322 | | $comments_rewrite = apply_filters('comments_rewrite_rules', $comments_rewrite); |
|---|
| 1323 | | |
|---|
| 1324 | | // Search |
|---|
| 1325 | | $search_structure = $this->get_search_permastruct(); |
|---|
| 1326 | | $search_rewrite = $this->generate_rewrite_rules($search_structure); |
|---|
| 1327 | | $search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite); |
|---|
| 1328 | | |
|---|
| 1329 | | // Categories |
|---|
| 1330 | | $category_rewrite = $this->generate_rewrite_rules($this->get_category_permastruct()); |
|---|
| 1331 | | $category_rewrite = apply_filters('category_rewrite_rules', $category_rewrite); |
|---|
| 1332 | | |
|---|
| 1333 | | // Authors |
|---|
| 1334 | | $author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct()); |
|---|
| 1335 | | $author_rewrite = apply_filters('author_rewrite_rules', $author_rewrite); |
|---|
| 1336 | | |
|---|
| 1337 | | // Pages |
|---|
| 1338 | | $page_rewrite = $this->page_rewrite_rules(); |
|---|
| 1339 | | $page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite); |
|---|
| 1340 | | |
|---|
| 1341 | | // Put them together. |
|---|
| 1342 | | $this->rules = array_merge($page_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $author_rewrite, $date_rewrite, $post_rewrite); |
|---|
| 1343 | | |
|---|
| 1344 | | do_action('generate_rewrite_rules', array(&$this)); |
|---|
| 1345 | | $this->rules = apply_filters('rewrite_rules_array', $this->rules); |
|---|
| 1346 | | |
|---|
| 1347 | | return $this->rules; |
|---|
| 1348 | | } |
|---|
| 1349 | | |
|---|
| 1350 | | function wp_rewrite_rules() { |
|---|
| 1351 | | $this->rules = get_option('rewrite_rules'); |
|---|
| 1352 | | if ( empty($this->rules) ) { |
|---|
| 1353 | | $this->matches = 'matches'; |
|---|
| 1354 | | $this->rewrite_rules(); |
|---|
| 1355 | | update_option('rewrite_rules', $this->rules); |
|---|
| 1356 | | } |
|---|
| 1357 | | |
|---|
| 1358 | | return $this->rules; |
|---|
| 1359 | | } |
|---|
| 1360 | | |
|---|
| 1361 | | function mod_rewrite_rules() { |
|---|
| 1362 | | if ( ! $this->using_permalinks()) { |
|---|
| 1363 | | return ''; |
|---|
| 1364 | | } |
|---|
| 1365 | | |
|---|
| 1366 | | $site_root = parse_url(get_settings('siteurl')); |
|---|
| 1367 | | $site_root = trailingslashit($site_root['path']); |
|---|
| 1368 | | |
|---|
| 1369 | | $home_root = parse_url(get_settings('home')); |
|---|
| 1370 | | $home_root = trailingslashit($home_root['path']); |
|---|
| 1371 | | |
|---|
| 1372 | | $rules = "<IfModule mod_rewrite.c>\n"; |
|---|
| 1373 | | $rules .= "RewriteEngine On\n"; |
|---|
| 1374 | | $rules .= "RewriteBase $home_root\n"; |
|---|
| 1375 | | |
|---|
| 1376 | | if ($this->use_verbose_rules) { |
|---|
| 1377 | | $this->matches = ''; |
|---|
| 1378 | | $rewrite = $this->rewrite_rules(); |
|---|
| 1379 | | $num_rules = count($rewrite); |
|---|
| 1380 | | $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" . |
|---|
| 1381 | | "RewriteCond %{REQUEST_FILENAME} -d\n" . |
|---|
| 1382 | | "RewriteRule ^.*$ - [S=$num_rules]\n"; |
|---|
| 1383 | | |
|---|
| 1384 | | foreach ($rewrite as $match => $query) { |
|---|
| 1385 | | // Apache 1.3 does not support the reluctant (non-greedy) modifier. |
|---|
| 1386 | | $match = str_replace('.+?', '.+', $match); |
|---|
| 1387 | | |
|---|
| 1388 | | // If the match is unanchored and greedy, prepend rewrite conditions |
|---|
| 1389 | | // to avoid infinite redirects and eclipsing of real files. |
|---|
| 1390 | | if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) { |
|---|
| 1391 | | //nada. |
|---|
| 1392 | | } |
|---|
| 1393 | | |
|---|
| 1394 | | if (strstr($query, $this->index)) { |
|---|
| 1395 | | $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n"; |
|---|
| 1396 | | } else { |
|---|
| 1397 | | $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n"; |
|---|
| 1398 | | } |
|---|
| 1399 | | } |
|---|
| 1400 | | } else { |
|---|
| 1401 | | $rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" . |
|---|
| 1402 | | "RewriteCond %{REQUEST_FILENAME} !-d\n" . |
|---|
| 1403 | | "RewriteRule . {$home_root}{$this->index} [L]\n"; |
|---|
| 1404 | | } |
|---|
| 1405 | | |
|---|
| 1406 | | $rules .= "</IfModule>\n"; |
|---|
| 1407 | | |
|---|
| 1408 | | $rules = apply_filters('mod_rewrite_rules', $rules); |
|---|
| 1409 | | $rules = apply_filters('rewrite_rules', $rules); // Deprecated |
|---|
| 1410 | | |
|---|
| 1411 | | return $rules; |
|---|
| 1412 | | } |
|---|
| 1413 | | |
|---|
| 1414 | | function flush_rules() { |
|---|
| 1415 | | generate_page_uri_index(); |
|---|
| 1416 | | delete_option('rewrite_rules'); |
|---|
| 1417 | | $this->wp_rewrite_rules(); |
|---|
| 1418 | | if ( function_exists('save_mod_rewrite_rules') ) |
|---|
| 1419 | | save_mod_rewrite_rules(); |
|---|
| 1420 | | } |
|---|
| 1421 | | |
|---|
| 1422 | | function init() { |
|---|
| 1423 | | $this->permalink_structure = get_settings('permalink_structure'); |
|---|
| 1424 | | $this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%')); |
|---|
| 1425 | | $this->root = ''; |
|---|
| 1426 | | if ($this->using_index_permalinks()) { |
|---|
| 1427 | | $this->root = $this->index . '/'; |
|---|
| 1428 | | } |
|---|
| 1429 | | $this->category_base = get_settings('category_base'); |
|---|
| 1430 | | unset($this->category_structure); |
|---|
| 1431 | | unset($this->author_structure); |
|---|
| 1432 | | unset($this->date_structure); |
|---|
| 1433 | | unset($this->page_structure); |
|---|
| 1434 | | unset($this->search_structure); |
|---|
| 1435 | | unset($this->feed_structure); |
|---|
| 1436 | | unset($this->comment_feed_structure); |
|---|
| 1437 | | } |
|---|
| 1438 | | |
|---|
| 1439 | | function set_permalink_structure($permalink_structure) { |
|---|
| 1440 | | if ($permalink_structure != $this->permalink_structure) { |
|---|
| 1441 | | update_option('permalink_structure', $permalink_structure); |
|---|
| 1442 | | $this->init(); |
|---|
| 1443 | | } |
|---|
| 1444 | | } |
|---|
| 1445 | | |
|---|
| 1446 | | function set_category_base($category_base) { |
|---|
| 1447 | | if ($category_base != $this->category_base) { |
|---|
| 1448 | | update_option('category_base', $category_base); |
|---|
| 1449 | | $this->init(); |
|---|
| 1450 | | } |
|---|
| 1451 | | } |
|---|
| 1452 | | |
|---|
| 1453 | | function WP_Rewrite() { |
|---|
| 1454 | | $this->init(); |
|---|
| 1455 | | } |
|---|
| 1456 | | } |
|---|
| 1457 | | |
|---|