Changeset 42343 for trunk/src/wp-includes/class-wp-rewrite.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-rewrite.php
r42228 r42343 289 289 '%author%', 290 290 '%pagename%', 291 '%search%' 291 '%search%', 292 292 ); 293 293 … … 310 310 '([^/]+)', 311 311 '([^/]+?)', 312 '(.+)' 312 '(.+)', 313 313 ); 314 314 … … 330 330 'author_name=', 331 331 'pagename=', 332 's=' 332 's=', 333 333 ); 334 334 … … 351 351 */ 352 352 public function using_permalinks() { 353 return ! empty( $this->permalink_structure);353 return ! empty( $this->permalink_structure ); 354 354 } 355 355 … … 400 400 * @return string 401 401 */ 402 public function preg_index( $number) {402 public function preg_index( $number ) { 403 403 $match_prefix = '$'; 404 404 $match_suffix = ''; 405 405 406 if ( ! empty( $this->matches) ) {406 if ( ! empty( $this->matches ) ) { 407 407 $match_prefix = '$' . $this->matches . '['; 408 408 $match_suffix = ']'; … … 428 428 429 429 // Get pages in order of hierarchy, i.e. children after parents. 430 $pages = $wpdb->get_results( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page' AND post_status != 'auto-draft'");430 $pages = $wpdb->get_results( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page' AND post_status != 'auto-draft'" ); 431 431 $posts = get_page_hierarchy( $pages ); 432 432 433 433 // If we have no pages get out quick. 434 if ( ! $posts )434 if ( ! $posts ) { 435 435 return array( array(), array() ); 436 } 436 437 437 438 // Now reverse it, because we need parents after children for rewrite rules to work properly. 438 $posts = array_reverse( $posts, true);439 440 $page_uris = array();439 $posts = array_reverse( $posts, true ); 440 441 $page_uris = array(); 441 442 $page_attachment_uris = array(); 442 443 443 444 foreach ( $posts as $id => $post ) { 444 445 // URL => page name 445 $uri = get_page_uri($id);446 $attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id ) );447 if ( ! empty($attachments) ) {446 $uri = get_page_uri( $id ); 447 $attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id ) ); 448 if ( ! empty( $attachments ) ) { 448 449 foreach ( $attachments as $attachment ) { 449 $attach_uri = get_page_uri($attachment->ID);450 $page_attachment_uris[ $attach_uri] = $attachment->ID;450 $attach_uri = get_page_uri( $attachment->ID ); 451 $page_attachment_uris[ $attach_uri ] = $attachment->ID; 451 452 } 452 453 } 453 454 454 $page_uris[ $uri] = $id;455 $page_uris[ $uri ] = $id; 455 456 } 456 457 … … 492 493 */ 493 494 public function get_date_permastruct() { 494 if ( isset( $this->date_structure) )495 if ( isset( $this->date_structure ) ) { 495 496 return $this->date_structure; 496 497 if ( empty($this->permalink_structure) ) { 497 } 498 499 if ( empty( $this->permalink_structure ) ) { 498 500 $this->date_structure = ''; 499 501 return false; … … 501 503 502 504 // The date permalink must have year, month, and day separated by slashes. 503 $endians = array( '%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%');505 $endians = array( '%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%' ); 504 506 505 507 $this->date_structure = ''; 506 $date_endian = '';508 $date_endian = ''; 507 509 508 510 foreach ( $endians as $endian ) { 509 if ( false !== strpos( $this->permalink_structure, $endian) ) {510 $date_endian = $endian;511 if ( false !== strpos( $this->permalink_structure, $endian ) ) { 512 $date_endian = $endian; 511 513 break; 512 514 } 513 515 } 514 516 515 if ( empty( $date_endian) )517 if ( empty( $date_endian ) ) { 516 518 $date_endian = '%year%/%monthnum%/%day%'; 519 } 517 520 518 521 /* … … 521 524 */ 522 525 $front = $this->front; 523 preg_match_all( '/%.+?%/', $this->permalink_structure, $tokens);526 preg_match_all( '/%.+?%/', $this->permalink_structure, $tokens ); 524 527 $tok_index = 1; 525 foreach ( (array) $tokens[0] as $token ) {526 if ( '%post_id%' == $token && ( $tok_index <= 3) ) {528 foreach ( (array) $tokens[0] as $token ) { 529 if ( '%post_id%' == $token && ( $tok_index <= 3 ) ) { 527 530 $front = $front . 'date/'; 528 531 break; … … 549 552 $structure = $this->get_date_permastruct(); 550 553 551 if ( empty( $structure) )554 if ( empty( $structure ) ) { 552 555 return false; 553 554 $structure = str_replace('%monthnum%', '', $structure); 555 $structure = str_replace('%day%', '', $structure); 556 $structure = preg_replace('#/+#', '/', $structure); 556 } 557 558 $structure = str_replace( '%monthnum%', '', $structure ); 559 $structure = str_replace( '%day%', '', $structure ); 560 $structure = preg_replace( '#/+#', '/', $structure ); 557 561 558 562 return $structure; … … 572 576 $structure = $this->get_date_permastruct(); 573 577 574 if ( empty( $structure) )578 if ( empty( $structure ) ) { 575 579 return false; 576 577 $structure = str_replace('%day%', '', $structure); 578 $structure = preg_replace('#/+#', '/', $structure); 580 } 581 582 $structure = str_replace( '%day%', '', $structure ); 583 $structure = preg_replace( '#/+#', '/', $structure ); 579 584 580 585 return $structure; … … 607 612 */ 608 613 public function get_category_permastruct() { 609 return $this->get_extra_permastruct( 'category');614 return $this->get_extra_permastruct( 'category' ); 610 615 } 611 616 … … 623 628 */ 624 629 public function get_tag_permastruct() { 625 return $this->get_extra_permastruct( 'post_tag');630 return $this->get_extra_permastruct( 'post_tag' ); 626 631 } 627 632 … … 634 639 * @return string|false False if not found. Permalink structure string. 635 640 */ 636 public function get_extra_permastruct( $name) {637 if ( empty( $this->permalink_structure) )641 public function get_extra_permastruct( $name ) { 642 if ( empty( $this->permalink_structure ) ) { 638 643 return false; 639 640 if ( isset($this->extra_permastructs[$name]) ) 641 return $this->extra_permastructs[$name]['struct']; 644 } 645 646 if ( isset( $this->extra_permastructs[ $name ] ) ) { 647 return $this->extra_permastructs[ $name ]['struct']; 648 } 642 649 643 650 return false; … … 656 663 */ 657 664 public function get_author_permastruct() { 658 if ( isset( $this->author_structure) )665 if ( isset( $this->author_structure ) ) { 659 666 return $this->author_structure; 660 661 if ( empty($this->permalink_structure) ) { 667 } 668 669 if ( empty( $this->permalink_structure ) ) { 662 670 $this->author_structure = ''; 663 671 return false; … … 681 689 */ 682 690 public function get_search_permastruct() { 683 if ( isset( $this->search_structure) )691 if ( isset( $this->search_structure ) ) { 684 692 return $this->search_structure; 685 686 if ( empty($this->permalink_structure) ) { 693 } 694 695 if ( empty( $this->permalink_structure ) ) { 687 696 $this->search_structure = ''; 688 697 return false; … … 706 715 */ 707 716 public function get_page_permastruct() { 708 if ( isset( $this->page_structure) )717 if ( isset( $this->page_structure ) ) { 709 718 return $this->page_structure; 710 711 if (empty($this->permalink_structure)) { 719 } 720 721 if ( empty( $this->permalink_structure ) ) { 712 722 $this->page_structure = ''; 713 723 return false; … … 731 741 */ 732 742 public function get_feed_permastruct() { 733 if ( isset( $this->feed_structure) )743 if ( isset( $this->feed_structure ) ) { 734 744 return $this->feed_structure; 735 736 if ( empty($this->permalink_structure) ) { 745 } 746 747 if ( empty( $this->permalink_structure ) ) { 737 748 $this->feed_structure = ''; 738 749 return false; … … 756 767 */ 757 768 public function get_comment_feed_permastruct() { 758 if ( isset( $this->comment_feed_structure) )769 if ( isset( $this->comment_feed_structure ) ) { 759 770 return $this->comment_feed_structure; 760 761 if (empty($this->permalink_structure)) { 771 } 772 773 if ( empty( $this->permalink_structure ) ) { 762 774 $this->comment_feed_structure = ''; 763 775 return false; … … 789 801 if ( false !== $position && null !== $position ) { 790 802 $this->rewritereplace[ $position ] = $regex; 791 $this->queryreplace[ $position ] = $query;803 $this->queryreplace[ $position ] = $query; 792 804 } else { 793 $this->rewritecode[] = $tag;805 $this->rewritecode[] = $tag; 794 806 $this->rewritereplace[] = $regex; 795 $this->queryreplace[] = $query;807 $this->queryreplace[] = $query; 796 808 } 797 809 } … … 845 857 * @return array Rewrite rule list. 846 858 */ 847 public function generate_rewrite_rules( $permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true) {859 public function generate_rewrite_rules( $permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true ) { 848 860 // Build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/? 849 861 $feedregex2 = ''; 850 foreach ( (array) $this->feeds as $feed_name )862 foreach ( (array) $this->feeds as $feed_name ) { 851 863 $feedregex2 .= $feed_name . '|'; 852 $feedregex2 = '(' . trim($feedregex2, '|') . ')/?$'; 864 } 865 $feedregex2 = '(' . trim( $feedregex2, '|' ) . ')/?$'; 853 866 854 867 /* … … 860 873 // Build a regex to match the trackback and page/xx parts of URLs. 861 874 $trackbackregex = 'trackback/?$'; 862 $pageregex = $this->pagination_base . '/?([0-9]{1,})/?$';863 $commentregex = $this->comments_pagination_base . '-([0-9]{1,})/?$';864 $embedregex = 'embed/?$';875 $pageregex = $this->pagination_base . '/?([0-9]{1,})/?$'; 876 $commentregex = $this->comments_pagination_base . '-([0-9]{1,})/?$'; 877 $embedregex = 'embed/?$'; 865 878 866 879 // Build up an array of endpoint regexes to append => queries to append. 867 880 if ( $endpoints ) { 868 $ep_query_append = array 869 foreach ( (array) $this->endpoints as $endpoint ) {881 $ep_query_append = array(); 882 foreach ( (array) $this->endpoints as $endpoint ) { 870 883 // Match everything after the endpoint name, but allow for nothing to appear there. 871 884 $epmatch = $endpoint[1] . '(/(.*))?/?$'; 872 885 873 886 // This will be appended on to the rest of the query for each dir. 874 $epquery = '&' . $endpoint[2] . '=';875 $ep_query_append[ $epmatch] = array( $endpoint[0], $epquery );887 $epquery = '&' . $endpoint[2] . '='; 888 $ep_query_append[ $epmatch ] = array( $endpoint[0], $epquery ); 876 889 } 877 890 } 878 891 879 892 // Get everything up to the first rewrite tag. 880 $front = substr( $permalink_structure, 0, strpos($permalink_structure, '%'));893 $front = substr( $permalink_structure, 0, strpos( $permalink_structure, '%' ) ); 881 894 882 895 // Build an array of the tags (note that said array ends up being in $tokens[0]). 883 preg_match_all( '/%.+?%/', $permalink_structure, $tokens);884 885 $num_tokens = count( $tokens[0]);886 887 $index = $this->index; //probably 'index.php'888 $feedindex = $index;896 preg_match_all( '/%.+?%/', $permalink_structure, $tokens ); 897 898 $num_tokens = count( $tokens[0] ); 899 900 $index = $this->index; //probably 'index.php' 901 $feedindex = $index; 889 902 $trackbackindex = $index; 890 $embedindex = $index;903 $embedindex = $index; 891 904 892 905 /* … … 896 909 $queries = array(); 897 910 for ( $i = 0; $i < $num_tokens; ++$i ) { 898 if ( 0 < $i ) 899 $queries[$i] = $queries[$i - 1] . '&'; 900 else 901 $queries[$i] = ''; 902 903 $query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1); 904 $queries[$i] .= $query_token; 911 if ( 0 < $i ) { 912 $queries[ $i ] = $queries[ $i - 1 ] . '&'; 913 } else { 914 $queries[ $i ] = ''; 915 } 916 917 $query_token = str_replace( $this->rewritecode, $this->queryreplace, $tokens[0][ $i ] ) . $this->preg_index( $i + 1 ); 918 $queries[ $i ] .= $query_token; 905 919 } 906 920 907 921 // Get the structure, minus any cruft (stuff that isn't tags) at the front. 908 922 $structure = $permalink_structure; 909 if ( $front != '/' ) 910 $structure = str_replace($front, '', $structure); 923 if ( $front != '/' ) { 924 $structure = str_replace( $front, '', $structure ); 925 } 911 926 912 927 /* … … 915 930 * rewrite rules for /%year%/, /%year%/%monthnum%/ and /%year%/%monthnum%/%postname% 916 931 */ 917 $structure = trim( $structure, '/');918 $dirs = $walk_dirs ? explode('/', $structure) : array( $structure );919 $num_dirs = count($dirs);932 $structure = trim( $structure, '/' ); 933 $dirs = $walk_dirs ? explode( '/', $structure ) : array( $structure ); 934 $num_dirs = count( $dirs ); 920 935 921 936 // Strip slashes from the front of $front. 922 $front = preg_replace( '|^/+|', '', $front);937 $front = preg_replace( '|^/+|', '', $front ); 923 938 924 939 // The main workhorse loop. 925 940 $post_rewrite = array(); 926 $struct = $front;941 $struct = $front; 927 942 for ( $j = 0; $j < $num_dirs; ++$j ) { 928 943 // Get the struct for this dir, and trim slashes off the front. 929 $struct .= $dirs[ $j] . '/'; // Accumulate. see comment near explode('/', $structure) above.930 $struct = ltrim($struct, '/');944 $struct .= $dirs[ $j ] . '/'; // Accumulate. see comment near explode('/', $structure) above. 945 $struct = ltrim( $struct, '/' ); 931 946 932 947 // Replace tags with regexes. 933 $match = str_replace( $this->rewritecode, $this->rewritereplace, $struct);948 $match = str_replace( $this->rewritecode, $this->rewritereplace, $struct ); 934 949 935 950 // Make a list of tags, and store how many there are in $num_toks. 936 $num_toks = preg_match_all( '/%.+?%/', $struct, $toks);951 $num_toks = preg_match_all( '/%.+?%/', $struct, $toks ); 937 952 938 953 // Get the 'tagname=$matches[i]'. 939 $query = ( ! empty( $num_toks ) && isset( $queries[ $num_toks - 1] ) ) ? $queries[$num_toks - 1] : '';954 $query = ( ! empty( $num_toks ) && isset( $queries[ $num_toks - 1 ] ) ) ? $queries[ $num_toks - 1 ] : ''; 940 955 941 956 // Set up $ep_mask_specific which is used to match more specific URL types. 942 switch ( $dirs[ $j] ) {957 switch ( $dirs[ $j ] ) { 943 958 case '%year%': 944 959 $ep_mask_specific = EP_YEAR; … … 956 971 // Create query for /page/xx. 957 972 $pagematch = $match . $pageregex; 958 $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index( $num_toks + 1);973 $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index( $num_toks + 1 ); 959 974 960 975 // Create query for /comment-page-xx. 961 976 $commentmatch = $match . $commentregex; 962 $commentquery = $index . '?' . $query . '&cpage=' . $this->preg_index( $num_toks + 1);963 964 if ( get_option( 'page_on_front') ) {977 $commentquery = $index . '?' . $query . '&cpage=' . $this->preg_index( $num_toks + 1 ); 978 979 if ( get_option( 'page_on_front' ) ) { 965 980 // Create query for Root /comment-page-xx. 966 981 $rootcommentmatch = $match . $commentregex; 967 $rootcommentquery = $index . '?' . $query . '&page_id=' . get_option( 'page_on_front') . '&cpage=' . $this->preg_index($num_toks + 1);982 $rootcommentquery = $index . '?' . $query . '&page_id=' . get_option( 'page_on_front' ) . '&cpage=' . $this->preg_index( $num_toks + 1 ); 968 983 } 969 984 970 985 // Create query for /feed/(feed|atom|rss|rss2|rdf). 971 986 $feedmatch = $match . $feedregex; 972 $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index( $num_toks + 1);987 $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index( $num_toks + 1 ); 973 988 974 989 // Create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex). 975 990 $feedmatch2 = $match . $feedregex2; 976 $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index( $num_toks + 1);991 $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index( $num_toks + 1 ); 977 992 978 993 // Create query and regex for embeds. … … 982 997 // If asked to, turn the feed queries into comment feed ones. 983 998 if ( $forcomments ) { 984 $feedquery .= '&withcomments=1';999 $feedquery .= '&withcomments=1'; 985 1000 $feedquery2 .= '&withcomments=1'; 986 1001 } … … 991 1006 // ...adding on /feed/ regexes => queries 992 1007 if ( $feed ) { 993 $rewrite = array( $feedmatch => $feedquery, $feedmatch2 => $feedquery2, $embedmatch => $embedquery ); 1008 $rewrite = array( 1009 $feedmatch => $feedquery, 1010 $feedmatch2 => $feedquery2, 1011 $embedmatch => $embedquery, 1012 ); 994 1013 } 995 1014 … … 1001 1020 // Only on pages with comments add ../comment-page-xx/. 1002 1021 if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask ) { 1003 $rewrite = array_merge( $rewrite, array($commentmatch => $commentquery));1004 } elseif ( EP_ROOT & $ep_mask && get_option( 'page_on_front') ) {1005 $rewrite = array_merge( $rewrite, array($rootcommentmatch => $rootcommentquery));1022 $rewrite = array_merge( $rewrite, array( $commentmatch => $commentquery ) ); 1023 } elseif ( EP_ROOT & $ep_mask && get_option( 'page_on_front' ) ) { 1024 $rewrite = array_merge( $rewrite, array( $rootcommentmatch => $rootcommentquery ) ); 1006 1025 } 1007 1026 1008 1027 // Do endpoints. 1009 1028 if ( $endpoints ) { 1010 foreach ( (array) $ep_query_append as $regex => $ep ) {1029 foreach ( (array) $ep_query_append as $regex => $ep ) { 1011 1030 // Add the endpoints on if the mask fits. 1012 if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific ) 1013 $rewrite[$match . $regex] = $index . '?' . $query . $ep[1] . $this->preg_index($num_toks + 2); 1031 if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific ) { 1032 $rewrite[ $match . $regex ] = $index . '?' . $query . $ep[1] . $this->preg_index( $num_toks + 2 ); 1033 } 1014 1034 } 1015 1035 } … … 1026 1046 * minute all present). Set these flags now as we need them for the endpoints. 1027 1047 */ 1028 if ( strpos( $struct, '%postname%') !== false1029 || strpos( $struct, '%post_id%') !== false1030 || strpos( $struct, '%pagename%') !== false1031 || ( strpos($struct, '%year%') !== false && strpos($struct, '%monthnum%') !== false && strpos($struct, '%day%') !== false && strpos($struct, '%hour%') !== false && strpos($struct, '%minute%') !== false && strpos($struct, '%second%') !== false)1048 if ( strpos( $struct, '%postname%' ) !== false 1049 || strpos( $struct, '%post_id%' ) !== false 1050 || strpos( $struct, '%pagename%' ) !== false 1051 || ( strpos( $struct, '%year%' ) !== false && strpos( $struct, '%monthnum%' ) !== false && strpos( $struct, '%day%' ) !== false && strpos( $struct, '%hour%' ) !== false && strpos( $struct, '%minute%' ) !== false && strpos( $struct, '%second%' ) !== false ) 1032 1052 ) { 1033 1053 $post = true; 1034 if ( strpos( $struct, '%pagename%') !== false )1054 if ( strpos( $struct, '%pagename%' ) !== false ) { 1035 1055 $page = true; 1056 } 1036 1057 } 1037 1058 1038 1059 if ( ! $post ) { 1039 1060 // For custom post types, we need to add on endpoints as well. 1040 foreach ( get_post_types( array( '_builtin' => false ) ) as $ptype ) {1041 if ( strpos( $struct, "%$ptype%") !== false ) {1061 foreach ( get_post_types( array( '_builtin' => false ) ) as $ptype ) { 1062 if ( strpos( $struct, "%$ptype%" ) !== false ) { 1042 1063 $post = true; 1043 1064 … … 1060 1081 1061 1082 // Trim slashes from the end of the regex for this dir. 1062 $match = rtrim( $match, '/');1083 $match = rtrim( $match, '/' ); 1063 1084 1064 1085 // Get rid of brackets. 1065 $submatchbase = str_replace( array( '(', ')'), '', $match);1086 $submatchbase = str_replace( array( '(', ')' ), '', $match ); 1066 1087 1067 1088 // Add a rule for at attachments, which take the form of <permalink>/some-text. … … 1105 1126 1106 1127 // Create queries for these extra tag-ons we've just dealt with. 1107 $subquery = $index . '?attachment=' . $this->preg_index(1);1108 $subtbquery = $subquery . '&tb=1';1109 $subfeedquery = $subquery . '&feed=' . $this->preg_index(2);1110 $subcommentquery = $subquery . '&cpage=' . $this->preg_index( 2);1111 $subembedquery = $subquery . '&embed=true';1128 $subquery = $index . '?attachment=' . $this->preg_index( 1 ); 1129 $subtbquery = $subquery . '&tb=1'; 1130 $subfeedquery = $subquery . '&feed=' . $this->preg_index( 2 ); 1131 $subcommentquery = $subquery . '&cpage=' . $this->preg_index( 2 ); 1132 $subembedquery = $subquery . '&embed=true'; 1112 1133 1113 1134 // Do endpoints for attachments. 1114 if ( ! empty($endpoints) ) {1135 if ( ! empty( $endpoints ) ) { 1115 1136 foreach ( (array) $ep_query_append as $regex => $ep ) { 1116 1137 if ( $ep[0] & EP_ATTACHMENT ) { 1117 $rewrite[ $sub1 . $regex] = $subquery . $ep[1] . $this->preg_index(3);1118 $rewrite[ $sub2 . $regex] = $subquery . $ep[1] . $this->preg_index(3);1138 $rewrite[ $sub1 . $regex ] = $subquery . $ep[1] . $this->preg_index( 3 ); 1139 $rewrite[ $sub2 . $regex ] = $subquery . $ep[1] . $this->preg_index( 3 ); 1119 1140 } 1120 1141 } … … 1135 1156 */ 1136 1157 $match = $match . '(?:/([0-9]+))?/?$'; 1137 $query = $index . '?' . $query . '&page=' . $this->preg_index( $num_toks + 1);1138 1139 // Not matching a permalink so this is a lot simpler.1158 $query = $index . '?' . $query . '&page=' . $this->preg_index( $num_toks + 1 ); 1159 1160 // Not matching a permalink so this is a lot simpler. 1140 1161 } else { 1141 1162 // Close the match and finalise the query. 1142 1163 $match .= '?$'; 1143 $query = $index . '?' . $query;1164 $query = $index . '?' . $query; 1144 1165 } 1145 1166 … … 1149 1170 * this dir 1150 1171 */ 1151 $rewrite = array_merge( $rewrite, array($match => $query));1172 $rewrite = array_merge( $rewrite, array( $match => $query ) ); 1152 1173 1153 1174 // If we're matching a permalink, add those extras (attachments etc) on. 1154 1175 if ( $post ) { 1155 1176 // Add trackback. 1156 $rewrite = array_merge( array($trackbackmatch => $trackbackquery), $rewrite);1177 $rewrite = array_merge( array( $trackbackmatch => $trackbackquery ), $rewrite ); 1157 1178 1158 1179 // Add embed. … … 1162 1183 if ( ! $page ) { 1163 1184 // Require <permalink>/attachment/stuff form for pages because of confusion with subpages. 1164 $rewrite = array_merge( $rewrite, array( 1165 $sub1 => $subquery, 1166 $sub1tb => $subtbquery, 1167 $sub1feed => $subfeedquery, 1168 $sub1feed2 => $subfeedquery, 1169 $sub1comment => $subcommentquery, 1170 $sub1embed => $subembedquery 1171 ) ); 1185 $rewrite = array_merge( 1186 $rewrite, array( 1187 $sub1 => $subquery, 1188 $sub1tb => $subtbquery, 1189 $sub1feed => $subfeedquery, 1190 $sub1feed2 => $subfeedquery, 1191 $sub1comment => $subcommentquery, 1192 $sub1embed => $subembedquery, 1193 ) 1194 ); 1172 1195 } 1173 1196 1174 $rewrite = array_merge( array( $sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery, $sub2comment => $subcommentquery, $sub2embed => $subembedquery ), $rewrite ); 1197 $rewrite = array_merge( 1198 array( 1199 $sub2 => $subquery, 1200 $sub2tb => $subtbquery, 1201 $sub2feed => $subfeedquery, 1202 $sub2feed2 => $subfeedquery, 1203 $sub2comment => $subcommentquery, 1204 $sub2embed => $subembedquery, 1205 ), $rewrite 1206 ); 1175 1207 } 1176 1208 } 1177 1209 // Add the rules for this dir to the accumulating $post_rewrite. 1178 $post_rewrite = array_merge( $rewrite, $post_rewrite);1210 $post_rewrite = array_merge( $rewrite, $post_rewrite ); 1179 1211 } 1180 1212 … … 1198 1230 * @return array 1199 1231 */ 1200 public function generate_rewrite_rule( $permalink_structure, $walk_dirs = false) {1201 return $this->generate_rewrite_rules( $permalink_structure, EP_NONE, false, false, false, $walk_dirs);1232 public function generate_rewrite_rule( $permalink_structure, $walk_dirs = false ) { 1233 return $this->generate_rewrite_rules( $permalink_structure, EP_NONE, false, false, false, $walk_dirs ); 1202 1234 } 1203 1235 … … 1222 1254 $rewrite = array(); 1223 1255 1224 if ( empty( $this->permalink_structure) )1256 if ( empty( $this->permalink_structure ) ) { 1225 1257 return $rewrite; 1258 } 1226 1259 1227 1260 // robots.txt -only if installed at the root 1228 $home_path = parse_url( home_url() );1261 $home_path = parse_url( home_url() ); 1229 1262 $robots_rewrite = ( empty( $home_path['path'] ) || '/' == $home_path['path'] ) ? array( 'robots\.txt$' => $this->index . '?robots=1' ) : array(); 1230 1263 … … 1238 1271 $registration_pages = array(); 1239 1272 if ( is_multisite() && is_main_site() ) { 1240 $registration_pages['.*wp-signup.php$'] = $this->index . '?signup=true';1273 $registration_pages['.*wp-signup.php$'] = $this->index . '?signup=true'; 1241 1274 $registration_pages['.*wp-activate.php$'] = $this->index . '?activate=true'; 1242 1275 } … … 1258 1291 1259 1292 // Date rewrite rules. 1260 $date_rewrite = $this->generate_rewrite_rules( $this->get_date_permastruct(), EP_DATE);1293 $date_rewrite = $this->generate_rewrite_rules( $this->get_date_permastruct(), EP_DATE ); 1261 1294 1262 1295 /** … … 1272 1305 1273 1306 // Root-level rewrite rules. 1274 $root_rewrite = $this->generate_rewrite_rules( $this->root . '/', EP_ROOT);1307 $root_rewrite = $this->generate_rewrite_rules( $this->root . '/', EP_ROOT ); 1275 1308 1276 1309 /** … … 1287 1320 1288 1321 // Comments rewrite rules. 1289 $comments_rewrite = $this->generate_rewrite_rules( $this->root . $this->comments_base, EP_COMMENTS, false, true, true, false);1322 $comments_rewrite = $this->generate_rewrite_rules( $this->root . $this->comments_base, EP_COMMENTS, false, true, true, false ); 1290 1323 1291 1324 /** … … 1302 1335 // Search rewrite rules. 1303 1336 $search_structure = $this->get_search_permastruct(); 1304 $search_rewrite = $this->generate_rewrite_rules($search_structure, EP_SEARCH);1337 $search_rewrite = $this->generate_rewrite_rules( $search_structure, EP_SEARCH ); 1305 1338 1306 1339 /** … … 1317 1350 1318 1351 // Author rewrite rules. 1319 $author_rewrite = $this->generate_rewrite_rules( $this->get_author_permastruct(), EP_AUTHORS);1352 $author_rewrite = $this->generate_rewrite_rules( $this->get_author_permastruct(), EP_AUTHORS ); 1320 1353 1321 1354 /** … … 1346 1379 foreach ( $this->extra_permastructs as $permastructname => $struct ) { 1347 1380 if ( is_array( $struct ) ) { 1348 if ( count( $struct ) == 2 ) 1381 if ( count( $struct ) == 2 ) { 1349 1382 $rules = $this->generate_rewrite_rules( $struct[0], $struct[1] ); 1350 else1383 } else { 1351 1384 $rules = $this->generate_rewrite_rules( $struct['struct'], $struct['ep_mask'], $struct['paged'], $struct['feed'], $struct['forcomments'], $struct['walk_dirs'], $struct['endpoints'] ); 1385 } 1352 1386 } else { 1353 1387 $rules = $this->generate_rewrite_rules( $struct ); … … 1379 1413 } 1380 1414 1381 $this->extra_rules_top = array_merge( $this->extra_rules_top, $rules);1415 $this->extra_rules_top = array_merge( $this->extra_rules_top, $rules ); 1382 1416 } 1383 1417 1384 1418 // Put them together. 1385 if ( $this->use_verbose_page_rules ) 1386 $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules); 1387 else 1388 $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules); 1419 if ( $this->use_verbose_page_rules ) { 1420 $this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules ); 1421 } else { 1422 $this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules ); 1423 } 1389 1424 1390 1425 /** … … 1422 1457 */ 1423 1458 public function wp_rewrite_rules() { 1424 $this->rules = get_option( 'rewrite_rules');1425 if ( empty( $this->rules) ) {1459 $this->rules = get_option( 'rewrite_rules' ); 1460 if ( empty( $this->rules ) ) { 1426 1461 $this->matches = 'matches'; 1427 1462 $this->rewrite_rules(); … … 1430 1465 return $this->rules; 1431 1466 } 1432 update_option( 'rewrite_rules', $this->rules);1467 update_option( 'rewrite_rules', $this->rules ); 1433 1468 } 1434 1469 … … 1450 1485 */ 1451 1486 public function mod_rewrite_rules() { 1452 if ( ! $this->using_permalinks() ) 1487 if ( ! $this->using_permalinks() ) { 1453 1488 return ''; 1489 } 1454 1490 1455 1491 $site_root = parse_url( site_url() ); 1456 if ( isset( $site_root['path'] ) ) 1457 $site_root = trailingslashit($site_root['path']); 1458 1459 $home_root = parse_url(home_url()); 1460 if ( isset( $home_root['path'] ) ) 1461 $home_root = trailingslashit($home_root['path']); 1462 else 1492 if ( isset( $site_root['path'] ) ) { 1493 $site_root = trailingslashit( $site_root['path'] ); 1494 } 1495 1496 $home_root = parse_url( home_url() ); 1497 if ( isset( $home_root['path'] ) ) { 1498 $home_root = trailingslashit( $home_root['path'] ); 1499 } else { 1463 1500 $home_root = '/'; 1464 1465 $rules = "<IfModule mod_rewrite.c>\n"; 1501 } 1502 1503 $rules = "<IfModule mod_rewrite.c>\n"; 1466 1504 $rules .= "RewriteEngine On\n"; 1467 1505 $rules .= "RewriteBase $home_root\n"; … … 1471 1509 1472 1510 // Add in the rules that don't redirect to WP's index.php (and thus shouldn't be handled by WP at all). 1473 foreach ( (array) $this->non_wp_rules as $match => $query ) {1511 foreach ( (array) $this->non_wp_rules as $match => $query ) { 1474 1512 // Apache 1.3 does not support the reluctant (non-greedy) modifier. 1475 $match = str_replace( '.+?', '.+', $match);1513 $match = str_replace( '.+?', '.+', $match ); 1476 1514 1477 1515 $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n"; … … 1480 1518 if ( $this->use_verbose_rules ) { 1481 1519 $this->matches = ''; 1482 $rewrite = $this->rewrite_rules();1483 $num_rules = count($rewrite);1484 $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" .1520 $rewrite = $this->rewrite_rules(); 1521 $num_rules = count( $rewrite ); 1522 $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" . 1485 1523 "RewriteCond %{REQUEST_FILENAME} -d\n" . 1486 1524 "RewriteRule ^.*$ - [S=$num_rules]\n"; 1487 1525 1488 foreach ( (array) $rewrite as $match => $query ) {1526 foreach ( (array) $rewrite as $match => $query ) { 1489 1527 // Apache 1.3 does not support the reluctant (non-greedy) modifier. 1490 $match = str_replace( '.+?', '.+', $match);1491 1492 if ( strpos( $query, $this->index) !== false )1528 $match = str_replace( '.+?', '.+', $match ); 1529 1530 if ( strpos( $query, $this->index ) !== false ) { 1493 1531 $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n"; 1494 else1532 } else { 1495 1533 $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n"; 1534 } 1496 1535 } 1497 1536 } else { … … 1536 1575 */ 1537 1576 public function iis7_url_rewrite_rules( $add_parent_tags = false ) { 1538 if ( ! $this->using_permalinks() ) 1577 if ( ! $this->using_permalinks() ) { 1539 1578 return ''; 1579 } 1540 1580 $rules = ''; 1541 1581 if ( $add_parent_tags ) { … … 1591 1631 if ( is_array( $query ) ) { 1592 1632 $external = false; 1593 $query = add_query_arg( $query, 'index.php' );1633 $query = add_query_arg( $query, 'index.php' ); 1594 1634 } else { 1595 1635 $index = false === strpos( $query, '?' ) ? strlen( $query ) : strpos( $query, '?' ); … … 1693 1733 public function add_permastruct( $name, $struct, $args = array() ) { 1694 1734 // Back-compat for the old parameters: $with_front and $ep_mask. 1695 if ( ! is_array( $args ) ) 1735 if ( ! is_array( $args ) ) { 1696 1736 $args = array( 'with_front' => $args ); 1697 if ( func_num_args() == 4 ) 1737 } 1738 if ( func_num_args() == 4 ) { 1698 1739 $args['ep_mask'] = func_get_arg( 3 ); 1740 } 1699 1741 1700 1742 $defaults = array( 1701 'with_front' => true,1702 'ep_mask' => EP_NONE,1703 'paged' => true,1704 'feed' => true,1743 'with_front' => true, 1744 'ep_mask' => EP_NONE, 1745 'paged' => true, 1746 'feed' => true, 1705 1747 'forcomments' => false, 1706 'walk_dirs' => true,1707 'endpoints' => true,1748 'walk_dirs' => true, 1749 'endpoints' => true, 1708 1750 ); 1709 $args = array_intersect_key( $args, $defaults );1710 $args = wp_parse_args( $args, $defaults );1711 1712 if ( $args['with_front'] ) 1751 $args = array_intersect_key( $args, $defaults ); 1752 $args = wp_parse_args( $args, $defaults ); 1753 1754 if ( $args['with_front'] ) { 1713 1755 $struct = $this->front . $struct; 1714 else1756 } else { 1715 1757 $struct = $this->root . $struct; 1758 } 1716 1759 $args['struct'] = $struct; 1717 1760 … … 1772 1815 return; 1773 1816 } 1774 if ( function_exists( 'save_mod_rewrite_rules' ) ) 1817 if ( function_exists( 'save_mod_rewrite_rules' ) ) { 1775 1818 save_mod_rewrite_rules(); 1776 if ( function_exists( 'iis7_save_url_rewrite_rules' ) ) 1819 } 1820 if ( function_exists( 'iis7_save_url_rewrite_rules' ) ) { 1777 1821 iis7_save_url_rewrite_rules(); 1822 } 1778 1823 } 1779 1824 … … 1788 1833 */ 1789 1834 public function init() { 1790 $this->extra_rules = $this->non_wp_rules = $this->endpoints = array();1791 $this->permalink_structure = get_option( 'permalink_structure');1792 $this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%'));1793 $this->root = '';1794 1795 if ( $this->using_index_permalinks() ) 1835 $this->extra_rules = $this->non_wp_rules = $this->endpoints = array(); 1836 $this->permalink_structure = get_option( 'permalink_structure' ); 1837 $this->front = substr( $this->permalink_structure, 0, strpos( $this->permalink_structure, '%' ) ); 1838 $this->root = ''; 1839 1840 if ( $this->using_index_permalinks() ) { 1796 1841 $this->root = $this->index . '/'; 1797 1798 unset($this->author_structure); 1799 unset($this->date_structure); 1800 unset($this->page_structure); 1801 unset($this->search_structure); 1802 unset($this->feed_structure); 1803 unset($this->comment_feed_structure); 1804 $this->use_trailing_slashes = ( '/' == substr($this->permalink_structure, -1, 1) ); 1842 } 1843 1844 unset( $this->author_structure ); 1845 unset( $this->date_structure ); 1846 unset( $this->page_structure ); 1847 unset( $this->search_structure ); 1848 unset( $this->feed_structure ); 1849 unset( $this->comment_feed_structure ); 1850 $this->use_trailing_slashes = ( '/' == substr( $this->permalink_structure, -1, 1 ) ); 1805 1851 1806 1852 // Enable generic rules for pages if permalink structure doesn't begin with a wildcard. 1807 if ( preg_match( "/^[^%]*%(?:postname|category|tag|author)%/", $this->permalink_structure) )1853 if ( preg_match( '/^[^%]*%(?:postname|category|tag|author)%/', $this->permalink_structure ) ) { 1808 1854 $this->use_verbose_page_rules = true; 1809 else1855 } else { 1810 1856 $this->use_verbose_page_rules = false; 1857 } 1811 1858 } 1812 1859 … … 1825 1872 * @param string $permalink_structure Permalink structure. 1826 1873 */ 1827 public function set_permalink_structure( $permalink_structure) {1874 public function set_permalink_structure( $permalink_structure ) { 1828 1875 if ( $permalink_structure != $this->permalink_structure ) { 1829 1876 $old_permalink_structure = $this->permalink_structure; 1830 update_option( 'permalink_structure', $permalink_structure);1877 update_option( 'permalink_structure', $permalink_structure ); 1831 1878 1832 1879 $this->init(); … … 1855 1902 * @param string $category_base Category permalink structure base. 1856 1903 */ 1857 public function set_category_base( $category_base) {1858 if ( $category_base != get_option( 'category_base') ) {1859 update_option( 'category_base', $category_base);1904 public function set_category_base( $category_base ) { 1905 if ( $category_base != get_option( 'category_base' ) ) { 1906 update_option( 'category_base', $category_base ); 1860 1907 $this->init(); 1861 1908 } … … 1874 1921 */ 1875 1922 public function set_tag_base( $tag_base ) { 1876 if ( $tag_base != get_option( 'tag_base' ) ) {1923 if ( $tag_base != get_option( 'tag_base' ) ) { 1877 1924 update_option( 'tag_base', $tag_base ); 1878 1925 $this->init(); … … 1884 1931 * 1885 1932 * @since 1.5.0 1886 *1887 1933 */ 1888 1934 public function __construct() {
Note: See TracChangeset
for help on using the changeset viewer.