| | 1333 | /* rewrite_rules |
|---|
| | 1334 | * Construct rewrite matches and queries from permalink structure. |
|---|
| | 1335 | * matches - The name of the match array to use in the query strings. |
|---|
| | 1336 | * If empty, $1, $2, $3, etc. are used. |
|---|
| | 1337 | * Returns an associate array of matches and queries. |
|---|
| | 1338 | */ |
|---|
| | 1339 | function rewrite_rules($matches = '') { |
|---|
| | 1340 | |
|---|
| | 1341 | function preg_index($number, $matches = '') { |
|---|
| | 1342 | $match_prefix = '$'; |
|---|
| | 1343 | $match_suffix = ''; |
|---|
| | 1344 | |
|---|
| | 1345 | if (! empty($matches)) { |
|---|
| | 1346 | $match_prefix = '$' . $matches . '['; |
|---|
| | 1347 | $match_suffix = ']'; |
|---|
| | 1348 | } |
|---|
| | 1349 | |
|---|
| | 1350 | return "$match_prefix$number$match_suffix"; |
|---|
| | 1351 | } |
|---|
| | 1352 | |
|---|
| | 1353 | $rewrite = array(); |
|---|
| | 1354 | |
|---|
| | 1355 | $permalink_structure = get_settings('permalink_structure'); |
|---|
| | 1356 | |
|---|
| | 1357 | if (empty($permalink_structure)) { |
|---|
| | 1358 | return $rewrite; |
|---|
| | 1359 | } |
|---|
| | 1360 | |
|---|
| | 1361 | $rewritecode = array( |
|---|
| | 1362 | '%year%', |
|---|
| | 1363 | '%monthnum%', |
|---|
| | 1364 | '%day%', |
|---|
| | 1365 | '%postname%', |
|---|
| | 1366 | '%post_id%' |
|---|
| | 1367 | ); |
|---|
| | 1368 | |
|---|
| | 1369 | $rewritereplace = array( |
|---|
| | 1370 | '([0-9]{4})?', |
|---|
| | 1371 | '([0-9]{1,2})?', |
|---|
| | 1372 | '([0-9]{1,2})?', |
|---|
| | 1373 | '([0-9a-z-]+)?', |
|---|
| | 1374 | '([0-9]+)?' |
|---|
| | 1375 | ); |
|---|
| | 1376 | |
|---|
| | 1377 | $queryreplace = array ( |
|---|
| | 1378 | 'year=', |
|---|
| | 1379 | 'monthnum=', |
|---|
| | 1380 | 'day=', |
|---|
| | 1381 | 'name=', |
|---|
| | 1382 | 'p=' |
|---|
| | 1383 | ); |
|---|
| | 1384 | |
|---|
| | 1385 | |
|---|
| | 1386 | $match = str_replace('/', '/?', $permalink_structure); |
|---|
| | 1387 | $match = preg_replace('|/[?]|', '', $match, 1); |
|---|
| | 1388 | |
|---|
| | 1389 | $match = str_replace($rewritecode, $rewritereplace, $match); |
|---|
| | 1390 | $match = preg_replace('|[?]|', '', $match, 1); |
|---|
| | 1391 | |
|---|
| | 1392 | $feedmatch = str_replace('?/?', '/', $match); |
|---|
| | 1393 | $trackbackmatch = $feedmatch; |
|---|
| | 1394 | |
|---|
| | 1395 | preg_match_all('/%.+?%/', $permalink_structure, $tokens); |
|---|
| | 1396 | |
|---|
| | 1397 | $query = 'index.php?'; |
|---|
| | 1398 | $feedquery = 'wp-feed.php?'; |
|---|
| | 1399 | $trackbackquery = 'wp-trackback.php?'; |
|---|
| | 1400 | for ($i = 0; $i < count($tokens[0]); ++$i) { |
|---|
| | 1401 | if (0 < $i) { |
|---|
| | 1402 | $query .= '&'; |
|---|
| | 1403 | $feedquery .= '&'; |
|---|
| | 1404 | $trackbackquery .= '&'; |
|---|
| | 1405 | } |
|---|
| | 1406 | |
|---|
| | 1407 | $query_token = str_replace($rewritecode, $queryreplace, $tokens[0][$i]) . preg_index($i+1, $matches); |
|---|
| | 1408 | $query .= $query_token; |
|---|
| | 1409 | $feedquery .= $query_token; |
|---|
| | 1410 | $trackbackquery .= $query_token; |
|---|
| | 1411 | } |
|---|
| | 1412 | ++$i; |
|---|
| | 1413 | |
|---|
| | 1414 | // Add post paged stuff |
|---|
| | 1415 | $match .= '([0-9]+)?/?'; |
|---|
| | 1416 | $query .= '&page=' . preg_index($i, $matches); |
|---|
| | 1417 | |
|---|
| | 1418 | // Add post feed stuff |
|---|
| | 1419 | $feedregex = '(feed|rdf|rss|rss2|atom)/?'; |
|---|
| | 1420 | $feedmatch .= $feedregex; |
|---|
| | 1421 | $feedquery .= '&feed=' . preg_index($i, $matches); |
|---|
| | 1422 | |
|---|
| | 1423 | // Add post trackback stuff |
|---|
| | 1424 | $trackbackregex = 'trackback/?'; |
|---|
| | 1425 | $trackbackmatch .= $trackbackregex; |
|---|
| | 1426 | |
|---|
| | 1427 | // Site feed |
|---|
| | 1428 | $sitefeedmatch = 'feed/?([0-9a-z-]+)?/?$'; |
|---|
| | 1429 | $sitefeedquery = $site_root . 'wp-feed.php?feed=' . preg_index(1, $matches); |
|---|
| | 1430 | |
|---|
| | 1431 | // Site comment feed |
|---|
| | 1432 | $sitecommentfeedmatch = 'comments/feed/?([0-9a-z-]+)?/?$'; |
|---|
| | 1433 | $sitecommentfeedquery = $site_root . 'wp-feed.php?feed=' . preg_index(1, $matches) . '&withcomments=1'; |
|---|
| | 1434 | |
|---|
| | 1435 | // Code for nice categories and authors, currently not very flexible |
|---|
| | 1436 | $front = substr($permalink_structure, 0, strpos($permalink_structure, '%')); |
|---|
| | 1437 | $catmatch = $front . 'category/'; |
|---|
| | 1438 | $catmatch = preg_replace('|^/+|', '', $catmatch); |
|---|
| | 1439 | |
|---|
| | 1440 | $catfeedmatch = $catmatch . '(.*)/' . $feedregex; |
|---|
| | 1441 | $catfeedquery = 'wp-feed.php?category_name=' . preg_index(1, $matches) . '&feed=' . preg_index(2, $matches); |
|---|
| | 1442 | |
|---|
| | 1443 | $catmatch = $catmatch . '?(.*)'; |
|---|
| | 1444 | $catquery = 'index.php?category_name=' . preg_index(1, $matches); |
|---|
| | 1445 | |
|---|
| | 1446 | $authormatch = $front . 'author/'; |
|---|
| | 1447 | $authormatch = preg_replace('|^/+|', '', $authormatch); |
|---|
| | 1448 | |
|---|
| | 1449 | $authorfeedmatch = $authormatch . '(.*)/' . $feedregex; |
|---|
| | 1450 | $authorfeedquery = 'wp-feed.php?author_name=' . preg_index(1, $matches) . '&feed=' . preg_index(2, $matches); |
|---|
| | 1451 | |
|---|
| | 1452 | $authormatch = $authormatch . '?(.*)'; |
|---|
| | 1453 | $authorquery = 'index.php?author_name=' . preg_index(1, $matches); |
|---|
| | 1454 | |
|---|
| | 1455 | $rewrite = array( |
|---|
| | 1456 | $catfeedmatch => $catfeedquery, |
|---|
| | 1457 | $catmatch => $catquery, |
|---|
| | 1458 | $authorfeedmatch => $authorfeedquery, |
|---|
| | 1459 | $authormatch => $authorquery, |
|---|
| | 1460 | $match => $query, |
|---|
| | 1461 | $feedmatch => $feedquery, |
|---|
| | 1462 | $trackbackmatch => $tracbackquery, |
|---|
| | 1463 | $sitefeedmatch => $sitefeedquery, |
|---|
| | 1464 | $sitecommentfeedmatch => $sitecommentfeedquery |
|---|
| | 1465 | ); |
|---|
| | 1466 | |
|---|
| | 1467 | return $rewrite; |
|---|
| | 1468 | } |
|---|
| | 1469 | |
|---|