Changeset 1374
 Timestamp:
 05/31/2004 09:34:00 PM (20 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/wpincludes/functions.php
r1373 r1374 1163 1163 } 1164 1164 1165 /* rewrite_rules 1166 * Construct rewrite matches and queries from permalink structure. 1167 * matches  The name of the match array to use in the query strings. 1168 * If empty, $1, $2, $3, etc. are used. 1169 * Returns an associate array of matches and queries. 1170 */ 1171 function rewrite_rules($matches = '', $permalink_structure = '') { 1172 1173 function preg_index($number, $matches = '') { 1174 $match_prefix = '$'; 1175 $match_suffix = ''; 1176 1177 if (! empty($matches)) { 1178 $match_prefix = '$' . $matches . '['; 1179 $match_suffix = ']'; 1180 } 1181 1182 return "$match_prefix$number$match_suffix"; 1183 } 1165 function preg_index($number, $matches = '') { 1166 $match_prefix = '$'; 1167 $match_suffix = ''; 1184 1168 1185 $rewrite = array(); 1186 1187 if (empty($permalink_structure)) { 1188 $permalink_structure = get_settings('permalink_structure'); 1189 1190 if (empty($permalink_structure)) { 1191 return $rewrite; 1192 } 1193 } 1194 1169 if (! empty($matches)) { 1170 $match_prefix = '$' . $matches . '['; 1171 $match_suffix = ']'; 1172 } 1173 1174 return "$match_prefix$number$match_suffix"; 1175 } 1176 1177 function generate_rewrite_rules($permalink_structure = '') { 1195 1178 $rewritecode = 1196 1179 array( … … 1202 1185 '%second%', 1203 1186 '%postname%', 1204 '%post_id%' 1187 '%post_id%', 1188 '%category%', 1189 '%author%' 1205 1190 ); 1206 1191 … … 1214 1199 '([09]{1,2})', 1215 1200 '([_09az]+)', 1216 '([09]+)' 1201 '([09]+)', 1202 '(.*)', 1203 '([_09az]+)' 1217 1204 ); 1218 1205 … … 1226 1213 'second=', 1227 1214 'name=', 1228 'p=' 1215 'p=', 1216 'category_name=', 1217 'author_name=' 1229 1218 ); 1230 1219 … … 1290 1279 } 1291 1280 1281 return $post_rewrite; 1282 } 1283 1284 /* rewrite_rules 1285 * Construct rewrite matches and queries from permalink structure. 1286 * matches  The name of the match array to use in the query strings. 1287 * If empty, $1, $2, $3, etc. are used. 1288 * Returns an associate array of matches and queries. 1289 */ 1290 function rewrite_rules($matches = '', $permalink_structure = '') { 1291 $rewrite = array(); 1292 1293 if (empty($permalink_structure)) { 1294 $permalink_structure = get_settings('permalink_structure'); 1295 1296 if (empty($permalink_structure)) { 1297 return $rewrite; 1298 } 1299 } 1300 1301 $post_rewrite = generate_rewrite_rules($permalink_structure); 1302 1292 1303 // If the permalink does not have year, month, and day, we need to create a 1293 1304 // separate archive rule. 1294 // TODO: Need to write separate rules for each component of the permalink.1295 1305 $doarchive = false; 1296 1306 if (! (strstr($permalink_structure, '%year') && strstr($permalink_structure, '%monthnum') && strstr($permalink_structure, '%day')) ) { 1297 1307 $doarchive = true; 1298 $archivematch = $front . '([09]{4})/?([09]{1,2})?/?([09]{1,2})?/?$'; 1299 $archivequery = 'index.php?year=' . preg_index(1, $matches) . '&monthnum=' . preg_index(2, $matches) . '&day=' . preg_index(3, $matches) ; 1300 } 1308 $archive_structure = $front . '%year%/%monthnum%/%day%/'; 1309 $archive_rewrite = generate_rewrite_rules($archive_structure); 1310 } 1311 1312 $feedregex = '(feedrdfrssrss2atom)/?$'; 1313 $pageregex = 'page/?([09]{1,})/?$'; 1314 $front = substr($permalink_structure, 0, strpos($permalink_structure, '%')); 1301 1315 1302 1316 // Site feed … … 1304 1318 $sitefeedquery = 'wpfeed.php?feed=' . preg_index(1, $matches); 1305 1319 1306 $sitepagematch = $pageregex;1307 $sitepagequery = 'index.php?paged=' . preg_index(1, $matches);1308 1309 1320 // Site comment feed 1310 1321 $sitecommentfeedmatch = 'comments/feed/?([_09az]+)?/?$'; 1311 1322 $sitecommentfeedquery = 'wpfeed.php?feed=' . preg_index(1, $matches) . '&withcomments=1'; 1312 1323 1313 // Code for nice categories and authors. 1314 if ( '' == get_settings('category_base') ) 1315 $catmatch = $front . 'category/'; 1316 else 1317 $catmatch = get_settings('category_base') . '/'; 1318 $catmatch = preg_replace('^/+', '', $catmatch); 1319 1320 $catfeedmatch = $catmatch . '(.*)/' . $feedregex; 1321 $catfeedquery = 'wpfeed.php?category_name=' . preg_index(1, $matches) . '&feed=' . preg_index(2, $matches); 1322 1323 $catpagematch = $catmatch . '(.*)/' . $pageregex; 1324 $catpagequery = 'index.php?category_name=' . preg_index(1, $matches) . '&paged=' . preg_index(2, $matches); 1325 1326 $catmatch = $catmatch . '?(.*)'; 1327 $catquery = 'index.php?category_name=' . preg_index(1, $matches); 1328 1329 $authormatch = $front . 'author/'; 1330 $authormatch = preg_replace('^/+', '', $authormatch); 1331 1332 $authorfeedmatch = $authormatch . '(.*)/' . $feedregex; 1333 $authorfeedquery = 'wpfeed.php?author_name=' . preg_index(1, $matches) . '&feed=' . preg_index(2, $matches); 1334 1335 $authorpagematch = $authormatch . '(.*)/' . $pageregex; 1336 $authorpagequery = 'index.php?author_name=' . preg_index(1, $matches) . '&paged=' . preg_index(2, $matches); 1337 1338 $authormatch = $authormatch . '?(.*)'; 1339 $authorquery = 'index.php?author_name=' . preg_index(1, $matches); 1340 1341 $rewrite = array( 1324 // Site page 1325 $sitepagematch = $pageregex; 1326 $sitepagequery = 'index.php?paged=' . preg_index(1, $matches); 1327 1328 $site_rewrite = array( 1342 1329 $sitefeedmatch => $sitefeedquery, 1343 1330 $sitecommentfeedmatch => $sitecommentfeedquery, 1344 1331 $sitepagematch => $sitepagequery, 1345 $catfeedmatch => $catfeedquery,1346 $catpagematch => $catpagequery,1347 $catmatch => $catquery,1348 $authorfeedmatch => $authorfeedquery,1349 $authorpagematch => $authorpagequery,1350 $authormatch => $authorquery1351 1332 ); 1352 1333 1353 $rewrite = $rewrite + $post_rewrite; 1354 1334 // Categories 1335 if ( '' == get_settings('category_base') ) 1336 $category_structure = $front . 'category/'; 1337 else 1338 $category_structure = get_settings('category_base') . '/'; 1339 1340 $category_structure = $category_structure . '%category%'; 1341 $category_rewrite = generate_rewrite_rules($category_structure); 1342 1343 // Authors 1344 $author_structure = $front . 'author/%author%'; 1345 $author_rewrite = generate_rewrite_rules($author_structure); 1346 1347 1348 // Put them together. 1349 $rewrite = $site_rewrite + $category_rewrite + $author_rewrite + $post_rewrite; 1350 1351 // Add on archive rewrite rules if needed. 1355 1352 if ($doarchive) { 1356 $rewrite = $rewrite + array($archivematch => $archivequery);1353 $rewrite = $rewrite + $archive_rewrite; 1357 1354 } 1358 1355
Note: See TracChangeset
for help on using the changeset viewer.