Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (7 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-rewrite.php

    r42228 r42343  
    289289        '%author%',
    290290        '%pagename%',
    291         '%search%'
     291        '%search%',
    292292    );
    293293
     
    310310        '([^/]+)',
    311311        '([^/]+?)',
    312         '(.+)'
     312        '(.+)',
    313313    );
    314314
     
    330330        'author_name=',
    331331        'pagename=',
    332         's='
     332        's=',
    333333    );
    334334
     
    351351     */
    352352    public function using_permalinks() {
    353         return ! empty($this->permalink_structure);
     353        return ! empty( $this->permalink_structure );
    354354    }
    355355
     
    400400     * @return string
    401401     */
    402     public function preg_index($number) {
     402    public function preg_index( $number ) {
    403403        $match_prefix = '$';
    404404        $match_suffix = '';
    405405
    406         if ( ! empty($this->matches) ) {
     406        if ( ! empty( $this->matches ) ) {
    407407            $match_prefix = '$' . $this->matches . '[';
    408408            $match_suffix = ']';
     
    428428
    429429        // 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'" );
    431431        $posts = get_page_hierarchy( $pages );
    432432
    433433        // If we have no pages get out quick.
    434         if ( !$posts )
     434        if ( ! $posts ) {
    435435            return array( array(), array() );
     436        }
    436437
    437438        // 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();
    441442        $page_attachment_uris = array();
    442443
    443444        foreach ( $posts as $id => $post ) {
    444445            // 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 ) ) {
    448449                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;
    451452                }
    452453            }
    453454
    454             $page_uris[$uri] = $id;
     455            $page_uris[ $uri ] = $id;
    455456        }
    456457
     
    492493     */
    493494    public function get_date_permastruct() {
    494         if ( isset($this->date_structure) )
     495        if ( isset( $this->date_structure ) ) {
    495496            return $this->date_structure;
    496 
    497         if ( empty($this->permalink_structure) ) {
     497        }
     498
     499        if ( empty( $this->permalink_structure ) ) {
    498500            $this->date_structure = '';
    499501            return false;
     
    501503
    502504        // 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%' );
    504506
    505507        $this->date_structure = '';
    506         $date_endian = '';
     508        $date_endian          = '';
    507509
    508510        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;
    511513                break;
    512514            }
    513515        }
    514516
    515         if ( empty($date_endian) )
     517        if ( empty( $date_endian ) ) {
    516518            $date_endian = '%year%/%monthnum%/%day%';
     519        }
    517520
    518521        /*
     
    521524         */
    522525        $front = $this->front;
    523         preg_match_all('/%.+?%/', $this->permalink_structure, $tokens);
     526        preg_match_all( '/%.+?%/', $this->permalink_structure, $tokens );
    524527        $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 ) ) {
    527530                $front = $front . 'date/';
    528531                break;
     
    549552        $structure = $this->get_date_permastruct();
    550553
    551         if ( empty($structure) )
     554        if ( empty( $structure ) ) {
    552555            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 );
    557561
    558562        return $structure;
     
    572576        $structure = $this->get_date_permastruct();
    573577
    574         if ( empty($structure) )
     578        if ( empty( $structure ) ) {
    575579            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 );
    579584
    580585        return $structure;
     
    607612     */
    608613    public function get_category_permastruct() {
    609         return $this->get_extra_permastruct('category');
     614        return $this->get_extra_permastruct( 'category' );
    610615    }
    611616
     
    623628     */
    624629    public function get_tag_permastruct() {
    625         return $this->get_extra_permastruct('post_tag');
     630        return $this->get_extra_permastruct( 'post_tag' );
    626631    }
    627632
     
    634639     * @return string|false False if not found. Permalink structure string.
    635640     */
    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 ) ) {
    638643            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        }
    642649
    643650        return false;
     
    656663     */
    657664    public function get_author_permastruct() {
    658         if ( isset($this->author_structure) )
     665        if ( isset( $this->author_structure ) ) {
    659666            return $this->author_structure;
    660 
    661         if ( empty($this->permalink_structure) ) {
     667        }
     668
     669        if ( empty( $this->permalink_structure ) ) {
    662670            $this->author_structure = '';
    663671            return false;
     
    681689     */
    682690    public function get_search_permastruct() {
    683         if ( isset($this->search_structure) )
     691        if ( isset( $this->search_structure ) ) {
    684692            return $this->search_structure;
    685 
    686         if ( empty($this->permalink_structure) ) {
     693        }
     694
     695        if ( empty( $this->permalink_structure ) ) {
    687696            $this->search_structure = '';
    688697            return false;
     
    706715     */
    707716    public function get_page_permastruct() {
    708         if ( isset($this->page_structure) )
     717        if ( isset( $this->page_structure ) ) {
    709718            return $this->page_structure;
    710 
    711         if (empty($this->permalink_structure)) {
     719        }
     720
     721        if ( empty( $this->permalink_structure ) ) {
    712722            $this->page_structure = '';
    713723            return false;
     
    731741     */
    732742    public function get_feed_permastruct() {
    733         if ( isset($this->feed_structure) )
     743        if ( isset( $this->feed_structure ) ) {
    734744            return $this->feed_structure;
    735 
    736         if ( empty($this->permalink_structure) ) {
     745        }
     746
     747        if ( empty( $this->permalink_structure ) ) {
    737748            $this->feed_structure = '';
    738749            return false;
     
    756767     */
    757768    public function get_comment_feed_permastruct() {
    758         if ( isset($this->comment_feed_structure) )
     769        if ( isset( $this->comment_feed_structure ) ) {
    759770            return $this->comment_feed_structure;
    760 
    761         if (empty($this->permalink_structure)) {
     771        }
     772
     773        if ( empty( $this->permalink_structure ) ) {
    762774            $this->comment_feed_structure = '';
    763775            return false;
     
    789801        if ( false !== $position && null !== $position ) {
    790802            $this->rewritereplace[ $position ] = $regex;
    791             $this->queryreplace[ $position ] = $query;
     803            $this->queryreplace[ $position ]   = $query;
    792804        } else {
    793             $this->rewritecode[] = $tag;
     805            $this->rewritecode[]    = $tag;
    794806            $this->rewritereplace[] = $regex;
    795             $this->queryreplace[] = $query;
     807            $this->queryreplace[]   = $query;
    796808        }
    797809    }
     
    845857     * @return array Rewrite rule list.
    846858     */
    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 ) {
    848860        // Build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/?
    849861        $feedregex2 = '';
    850         foreach ( (array) $this->feeds as $feed_name)
     862        foreach ( (array) $this->feeds as $feed_name ) {
    851863            $feedregex2 .= $feed_name . '|';
    852         $feedregex2 = '(' . trim($feedregex2, '|') . ')/?$';
     864        }
     865        $feedregex2 = '(' . trim( $feedregex2, '|' ) . ')/?$';
    853866
    854867        /*
     
    860873        // Build a regex to match the trackback and page/xx parts of URLs.
    861874        $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/?$';
    865878
    866879        // Build up an array of endpoint regexes to append => queries to append.
    867880        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 ) {
    870883                // Match everything after the endpoint name, but allow for nothing to appear there.
    871884                $epmatch = $endpoint[1] . '(/(.*))?/?$';
    872885
    873886                // 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 );
    876889            }
    877890        }
    878891
    879892        // 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, '%' ) );
    881894
    882895        // 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;
    889902        $trackbackindex = $index;
    890         $embedindex = $index;
     903        $embedindex     = $index;
    891904
    892905        /*
     
    896909        $queries = array();
    897910        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;
    905919        }
    906920
    907921        // Get the structure, minus any cruft (stuff that isn't tags) at the front.
    908922        $structure = $permalink_structure;
    909         if ( $front != '/' )
    910             $structure = str_replace($front, '', $structure);
     923        if ( $front != '/' ) {
     924            $structure = str_replace( $front, '', $structure );
     925        }
    911926
    912927        /*
     
    915930         * rewrite rules for /%year%/, /%year%/%monthnum%/ and /%year%/%monthnum%/%postname%
    916931         */
    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 );
    920935
    921936        // Strip slashes from the front of $front.
    922         $front = preg_replace('|^/+|', '', $front);
     937        $front = preg_replace( '|^/+|', '', $front );
    923938
    924939        // The main workhorse loop.
    925940        $post_rewrite = array();
    926         $struct = $front;
     941        $struct       = $front;
    927942        for ( $j = 0; $j < $num_dirs; ++$j ) {
    928943            // 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, '/' );
    931946
    932947            // Replace tags with regexes.
    933             $match = str_replace($this->rewritecode, $this->rewritereplace, $struct);
     948            $match = str_replace( $this->rewritecode, $this->rewritereplace, $struct );
    934949
    935950            // 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 );
    937952
    938953            // 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 ] : '';
    940955
    941956            // Set up $ep_mask_specific which is used to match more specific URL types.
    942             switch ( $dirs[$j] ) {
     957            switch ( $dirs[ $j ] ) {
    943958                case '%year%':
    944959                    $ep_mask_specific = EP_YEAR;
     
    956971            // Create query for /page/xx.
    957972            $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 );
    959974
    960975            // Create query for /comment-page-xx.
    961976            $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' ) ) {
    965980                // Create query for Root /comment-page-xx.
    966981                $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 );
    968983            }
    969984
    970985            // Create query for /feed/(feed|atom|rss|rss2|rdf).
    971986            $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 );
    973988
    974989            // Create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex).
    975990            $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 );
    977992
    978993            // Create query and regex for embeds.
     
    982997            // If asked to, turn the feed queries into comment feed ones.
    983998            if ( $forcomments ) {
    984                 $feedquery .= '&withcomments=1';
     999                $feedquery  .= '&withcomments=1';
    9851000                $feedquery2 .= '&withcomments=1';
    9861001            }
     
    9911006            // ...adding on /feed/ regexes => queries
    9921007            if ( $feed ) {
    993                 $rewrite = array( $feedmatch => $feedquery, $feedmatch2 => $feedquery2, $embedmatch => $embedquery );
     1008                $rewrite = array(
     1009                    $feedmatch  => $feedquery,
     1010                    $feedmatch2 => $feedquery2,
     1011                    $embedmatch => $embedquery,
     1012                );
    9941013            }
    9951014
     
    10011020            // Only on pages with comments add ../comment-page-xx/.
    10021021            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 ) );
    10061025            }
    10071026
    10081027            // Do endpoints.
    10091028            if ( $endpoints ) {
    1010                 foreach ( (array) $ep_query_append as $regex => $ep) {
     1029                foreach ( (array) $ep_query_append as $regex => $ep ) {
    10111030                    // 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                    }
    10141034                }
    10151035            }
     
    10261046                 * minute all present). Set these flags now as we need them for the endpoints.
    10271047                 */
    1028                 if ( strpos($struct, '%postname%') !== false
    1029                         || strpos($struct, '%post_id%') !== false
    1030                         || strpos($struct, '%pagename%') !== false
    1031                         || (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 )
    10321052                        ) {
    10331053                    $post = true;
    1034                     if ( strpos($struct, '%pagename%') !== false )
     1054                    if ( strpos( $struct, '%pagename%' ) !== false ) {
    10351055                        $page = true;
     1056                    }
    10361057                }
    10371058
    10381059                if ( ! $post ) {
    10391060                    // 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 ) {
    10421063                            $post = true;
    10431064
     
    10601081
    10611082                    // Trim slashes from the end of the regex for this dir.
    1062                     $match = rtrim($match, '/');
     1083                    $match = rtrim( $match, '/' );
    10631084
    10641085                    // Get rid of brackets.
    1065                     $submatchbase = str_replace( array('(', ')'), '', $match);
     1086                    $submatchbase = str_replace( array( '(', ')' ), '', $match );
    10661087
    10671088                    // Add a rule for at attachments, which take the form of <permalink>/some-text.
     
    11051126
    11061127                    // 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';
    11121133
    11131134                    // Do endpoints for attachments.
    1114                     if ( !empty($endpoints) ) {
     1135                    if ( ! empty( $endpoints ) ) {
    11151136                        foreach ( (array) $ep_query_append as $regex => $ep ) {
    11161137                            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 );
    11191140                            }
    11201141                        }
     
    11351156                     */
    11361157                    $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.
    11401161                } else {
    11411162                    // Close the match and finalise the query.
    11421163                    $match .= '?$';
    1143                     $query = $index . '?' . $query;
     1164                    $query  = $index . '?' . $query;
    11441165                }
    11451166
     
    11491170                 * this dir
    11501171                 */
    1151                 $rewrite = array_merge($rewrite, array($match => $query));
     1172                $rewrite = array_merge( $rewrite, array( $match => $query ) );
    11521173
    11531174                // If we're matching a permalink, add those extras (attachments etc) on.
    11541175                if ( $post ) {
    11551176                    // Add trackback.
    1156                     $rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite);
     1177                    $rewrite = array_merge( array( $trackbackmatch => $trackbackquery ), $rewrite );
    11571178
    11581179                    // Add embed.
     
    11621183                    if ( ! $page ) {
    11631184                        // 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                        );
    11721195                    }
    11731196
    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                    );
    11751207                }
    11761208            }
    11771209            // 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 );
    11791211        }
    11801212
     
    11981230     * @return array
    11991231     */
    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 );
    12021234    }
    12031235
     
    12221254        $rewrite = array();
    12231255
    1224         if ( empty($this->permalink_structure) )
     1256        if ( empty( $this->permalink_structure ) ) {
    12251257            return $rewrite;
     1258        }
    12261259
    12271260        // robots.txt -only if installed at the root
    1228         $home_path = parse_url( home_url() );
     1261        $home_path      = parse_url( home_url() );
    12291262        $robots_rewrite = ( empty( $home_path['path'] ) || '/' == $home_path['path'] ) ? array( 'robots\.txt$' => $this->index . '?robots=1' ) : array();
    12301263
     
    12381271        $registration_pages = array();
    12391272        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';
    12411274            $registration_pages['.*wp-activate.php$'] = $this->index . '?activate=true';
    12421275        }
     
    12581291
    12591292        // 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 );
    12611294
    12621295        /**
     
    12721305
    12731306        // 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 );
    12751308
    12761309        /**
     
    12871320
    12881321        // 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 );
    12901323
    12911324        /**
     
    13021335        // Search rewrite rules.
    13031336        $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 );
    13051338
    13061339        /**
     
    13171350
    13181351        // 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 );
    13201353
    13211354        /**
     
    13461379        foreach ( $this->extra_permastructs as $permastructname => $struct ) {
    13471380            if ( is_array( $struct ) ) {
    1348                 if ( count( $struct ) == 2 )
     1381                if ( count( $struct ) == 2 ) {
    13491382                    $rules = $this->generate_rewrite_rules( $struct[0], $struct[1] );
    1350                 else
     1383                } else {
    13511384                    $rules = $this->generate_rewrite_rules( $struct['struct'], $struct['ep_mask'], $struct['paged'], $struct['feed'], $struct['forcomments'], $struct['walk_dirs'], $struct['endpoints'] );
     1385                }
    13521386            } else {
    13531387                $rules = $this->generate_rewrite_rules( $struct );
     
    13791413            }
    13801414
    1381             $this->extra_rules_top = array_merge($this->extra_rules_top, $rules);
     1415            $this->extra_rules_top = array_merge( $this->extra_rules_top, $rules );
    13821416        }
    13831417
    13841418        // 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        }
    13891424
    13901425        /**
     
    14221457     */
    14231458    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 ) ) {
    14261461            $this->matches = 'matches';
    14271462            $this->rewrite_rules();
     
    14301465                return $this->rules;
    14311466            }
    1432             update_option('rewrite_rules', $this->rules);
     1467            update_option( 'rewrite_rules', $this->rules );
    14331468        }
    14341469
     
    14501485     */
    14511486    public function mod_rewrite_rules() {
    1452         if ( ! $this->using_permalinks() )
     1487        if ( ! $this->using_permalinks() ) {
    14531488            return '';
     1489        }
    14541490
    14551491        $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 {
    14631500            $home_root = '/';
    1464 
    1465         $rules = "<IfModule mod_rewrite.c>\n";
     1501        }
     1502
     1503        $rules  = "<IfModule mod_rewrite.c>\n";
    14661504        $rules .= "RewriteEngine On\n";
    14671505        $rules .= "RewriteBase $home_root\n";
     
    14711509
    14721510        // 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 ) {
    14741512            // Apache 1.3 does not support the reluctant (non-greedy) modifier.
    1475             $match = str_replace('.+?', '.+', $match);
     1513            $match = str_replace( '.+?', '.+', $match );
    14761514
    14771515            $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
     
    14801518        if ( $this->use_verbose_rules ) {
    14811519            $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" .
    14851523                "RewriteCond %{REQUEST_FILENAME} -d\n" .
    14861524                "RewriteRule ^.*$ - [S=$num_rules]\n";
    14871525
    1488             foreach ( (array) $rewrite as $match => $query) {
     1526            foreach ( (array) $rewrite as $match => $query ) {
    14891527                // 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 ) {
    14931531                    $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
    1494                 else
     1532                } else {
    14951533                    $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n";
     1534                }
    14961535            }
    14971536        } else {
     
    15361575     */
    15371576    public function iis7_url_rewrite_rules( $add_parent_tags = false ) {
    1538         if ( ! $this->using_permalinks() )
     1577        if ( ! $this->using_permalinks() ) {
    15391578            return '';
     1579        }
    15401580        $rules = '';
    15411581        if ( $add_parent_tags ) {
     
    15911631        if ( is_array( $query ) ) {
    15921632            $external = false;
    1593             $query = add_query_arg( $query, 'index.php' );
     1633            $query    = add_query_arg( $query, 'index.php' );
    15941634        } else {
    15951635            $index = false === strpos( $query, '?' ) ? strlen( $query ) : strpos( $query, '?' );
     
    16931733    public function add_permastruct( $name, $struct, $args = array() ) {
    16941734        // Back-compat for the old parameters: $with_front and $ep_mask.
    1695         if ( ! is_array( $args ) )
     1735        if ( ! is_array( $args ) ) {
    16961736            $args = array( 'with_front' => $args );
    1697         if ( func_num_args() == 4 )
     1737        }
     1738        if ( func_num_args() == 4 ) {
    16981739            $args['ep_mask'] = func_get_arg( 3 );
     1740        }
    16991741
    17001742        $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,
    17051747            'forcomments' => false,
    1706             'walk_dirs' => true,
    1707             'endpoints' => true,
     1748            'walk_dirs'   => true,
     1749            'endpoints'   => true,
    17081750        );
    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'] ) {
    17131755            $struct = $this->front . $struct;
    1714         else
     1756        } else {
    17151757            $struct = $this->root . $struct;
     1758        }
    17161759        $args['struct'] = $struct;
    17171760
     
    17721815            return;
    17731816        }
    1774         if ( function_exists( 'save_mod_rewrite_rules' ) )
     1817        if ( function_exists( 'save_mod_rewrite_rules' ) ) {
    17751818            save_mod_rewrite_rules();
    1776         if ( function_exists( 'iis7_save_url_rewrite_rules' ) )
     1819        }
     1820        if ( function_exists( 'iis7_save_url_rewrite_rules' ) ) {
    17771821            iis7_save_url_rewrite_rules();
     1822        }
    17781823    }
    17791824
     
    17881833     */
    17891834    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() ) {
    17961841            $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 ) );
    18051851
    18061852        // 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 ) ) {
    18081854            $this->use_verbose_page_rules = true;
    1809         else
     1855        } else {
    18101856            $this->use_verbose_page_rules = false;
     1857        }
    18111858    }
    18121859
     
    18251872     * @param string $permalink_structure Permalink structure.
    18261873     */
    1827     public function set_permalink_structure($permalink_structure) {
     1874    public function set_permalink_structure( $permalink_structure ) {
    18281875        if ( $permalink_structure != $this->permalink_structure ) {
    18291876            $old_permalink_structure = $this->permalink_structure;
    1830             update_option('permalink_structure', $permalink_structure);
     1877            update_option( 'permalink_structure', $permalink_structure );
    18311878
    18321879            $this->init();
     
    18551902     * @param string $category_base Category permalink structure base.
    18561903     */
    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 );
    18601907            $this->init();
    18611908        }
     
    18741921     */
    18751922    public function set_tag_base( $tag_base ) {
    1876         if ( $tag_base != get_option( 'tag_base') ) {
     1923        if ( $tag_base != get_option( 'tag_base' ) ) {
    18771924            update_option( 'tag_base', $tag_base );
    18781925            $this->init();
     
    18841931     *
    18851932     * @since 1.5.0
    1886      *
    18871933     */
    18881934    public function __construct() {
Note: See TracChangeset for help on using the changeset viewer.