Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (9 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-walker.php

    r42228 r42343  
    142142                }
    143143
    144                 $cb_args = array_merge( array(&$output, $element, $depth), $args);
    145                 call_user_func_array(array($this, 'start_el'), $cb_args);
     144                $cb_args = array_merge( array( &$output, $element, $depth ), $args );
     145                call_user_func_array( array( $this, 'start_el' ), $cb_args );
    146146
    147147                // descend only when the depth is right and there are childrens for this element
    148                 if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
    149 
    150                         foreach ( $children_elements[ $id ] as $child ){
    151 
    152                                 if ( !isset($newlevel) ) {
     148                if ( ( $max_depth == 0 || $max_depth > $depth + 1 ) && isset( $children_elements[ $id ] ) ) {
     149
     150                        foreach ( $children_elements[ $id ] as $child ) {
     151
     152                                if ( ! isset( $newlevel ) ) {
    153153                                        $newlevel = true;
    154154                                        //start the child delimiter
    155                                         $cb_args = array_merge( array(&$output, $depth), $args);
    156                                         call_user_func_array(array($this, 'start_lvl'), $cb_args);
     155                                        $cb_args = array_merge( array( &$output, $depth ), $args );
     156                                        call_user_func_array( array( $this, 'start_lvl' ), $cb_args );
    157157                                }
    158158                                $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
    … …  
    161161                }
    162162
    163                 if ( isset($newlevel) && $newlevel ){
     163                if ( isset( $newlevel ) && $newlevel ) {
    164164                        //end the child delimiter
    165                         $cb_args = array_merge( array(&$output, $depth), $args);
    166                         call_user_func_array(array($this, 'end_lvl'), $cb_args);
     165                        $cb_args = array_merge( array( &$output, $depth ), $args );
     166                        call_user_func_array( array( $this, 'end_lvl' ), $cb_args );
    167167                }
    168168
    169169                //end this element
    170                 $cb_args = array_merge( array(&$output, $element, $depth), $args);
    171                 call_user_func_array(array($this, 'end_el'), $cb_args);
     170                $cb_args = array_merge( array( &$output, $element, $depth ), $args );
     171                call_user_func_array( array( $this, 'end_el' ), $cb_args );
    172172        }
    173173
    … …  
    188188         */
    189189        public function walk( $elements, $max_depth ) {
    190                 $args = array_slice(func_get_args(), 2);
     190                $args   = array_slice( func_get_args(), 2 );
    191191                $output = '';
    192192
    … …  
    201201                if ( -1 == $max_depth ) {
    202202                        $empty_array = array();
    203                         foreach ( $elements as $e )
     203                        foreach ( $elements as $e ) {
    204204                                $this->display_element( $e, $empty_array, 1, 0, $args, $output );
     205                        }
    205206                        return $output;
    206207                }
    … …  
    214215                $top_level_elements = array();
    215216                $children_elements  = array();
    216                 foreach ( $elements as $e) {
    217                         if ( empty( $e->$parent_field ) )
     217                foreach ( $elements as $e ) {
     218                        if ( empty( $e->$parent_field ) ) {
    218219                                $top_level_elements[] = $e;
    219                         else
     220                        } else {
    220221                                $children_elements[ $e->$parent_field ][] = $e;
     222                        }
    221223                }
    222224
    … …  
    225227                 * Assume the first one must be root of the sub elements.
    226228                 */
    227                 if ( empty($top_level_elements) ) {
     229                if ( empty( $top_level_elements ) ) {
    228230
    229231                        $first = array_slice( $elements, 0, 1 );
    230                         $root = $first[0];
     232                        $root  = $first[0];
    231233
    232234                        $top_level_elements = array();
    233235                        $children_elements  = array();
    234                         foreach ( $elements as $e) {
    235                                 if ( $root->$parent_field == $e->$parent_field )
     236                        foreach ( $elements as $e ) {
     237                                if ( $root->$parent_field == $e->$parent_field ) {
    236238                                        $top_level_elements[] = $e;
    237                                 else
     239                                } else {
    238240                                        $children_elements[ $e->$parent_field ][] = $e;
    239                         }
    240                 }
    241 
    242                 foreach ( $top_level_elements as $e )
     241                                }
     242                        }
     243                }
     244
     245                foreach ( $top_level_elements as $e ) {
    243246                        $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
     247                }
    244248
    245249                /*
    … …  
    249253                if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
    250254                        $empty_array = array();
    251                         foreach ( $children_elements as $orphans )
    252                                 foreach ( $orphans as $op )
     255                        foreach ( $children_elements as $orphans ) {
     256                                foreach ( $orphans as $op ) {
    253257                                        $this->display_element( $op, $empty_array, 1, 0, $args, $output );
     258                                }
     259                        }
    254260                }
    255261
    … …  
    258264
    259265        /**
    260          * paged_walk() - produce a page of nested elements
    261          *
    262          * Given an array of hierarchical elements, the maximum depth, a specific page number,
    263          * and number of elements per page, this function first determines all top level root elements
    264          * belonging to that page, then lists them and all of their children in hierarchical order.
    265          *
     266         * paged_walk() - produce a page of nested elements
     267         *
     268         * Given an array of hierarchical elements, the maximum depth, a specific page number,
     269         * and number of elements per page, this function first determines all top level root elements
     270         * belonging to that page, then lists them and all of their children in hierarchical order.
     271         *
    266272         * $max_depth = 0 means display all levels.
    267273         * $max_depth > 0 specifies the number of display levels.
    268274         *
    269          * @since 2.7.0
     275         * @since 2.7.0
    270276         *
    271277         * @param array $elements
    … …  
    280286                }
    281287
    282                 $args = array_slice( func_get_args(), 4 );
     288                $args   = array_slice( func_get_args(), 4 );
    283289                $output = '';
    284290
    … …  
    286292
    287293                $count = -1;
    288                 if ( -1 == $max_depth )
     294                if ( -1 == $max_depth ) {
    289295                        $total_top = count( $elements );
    290                 if ( $page_num < 1 || $per_page < 0  ) {
     296                }
     297                if ( $page_num < 1 || $per_page < 0 ) {
    291298                        // No paging
    292299                        $paging = false;
    293                         $start = 0;
    294                         if ( -1 == $max_depth )
     300                        $start  = 0;
     301                        if ( -1 == $max_depth ) {
    295302                                $end = $total_top;
     303                        }
    296304                        $this->max_pages = 1;
    297305                } else {
    298306                        $paging = true;
    299                         $start = ( (int)$page_num - 1 ) * (int)$per_page;
    300                         $end   = $start + $per_page;
    301                         if ( -1 == $max_depth )
    302                                 $this->max_pages = ceil($total_top / $per_page);
     307                        $start  = ( (int) $page_num - 1 ) * (int) $per_page;
     308                        $end    = $start + $per_page;
     309                        if ( -1 == $max_depth ) {
     310                                $this->max_pages = ceil( $total_top / $per_page );
     311                        }
    303312                }
    304313
    305314                // flat display
    306315                if ( -1 == $max_depth ) {
    307                         if ( !empty($args[0]['reverse_top_level']) ) {
     316                        if ( ! empty( $args[0]['reverse_top_level'] ) ) {
    308317                                $elements = array_reverse( $elements );
    309318                                $oldstart = $start;
    310                                 $start = $total_top - $end;
    311                                 $end = $total_top - $oldstart;
     319                                $start    = $total_top - $end;
     320                                $end      = $total_top - $oldstart;
    312321                        }
    313322
    … …  
    315324                        foreach ( $elements as $e ) {
    316325                                $count++;
    317                                 if ( $count < $start )
     326                                if ( $count < $start ) {
    318327                                        continue;
    319                                 if ( $count >= $end )
     328                                }
     329                                if ( $count >= $end ) {
    320330                                        break;
     331                                }
    321332                                $this->display_element( $e, $empty_array, 1, 0, $args, $output );
    322333                        }
    … …  
    331342                $top_level_elements = array();
    332343                $children_elements  = array();
    333                 foreach ( $elements as $e) {
    334                         if ( 0 == $e->$parent_field )
     344                foreach ( $elements as $e ) {
     345                        if ( 0 == $e->$parent_field ) {
    335346                                $top_level_elements[] = $e;
    336                         else
     347                        } else {
    337348                                $children_elements[ $e->$parent_field ][] = $e;
     349                        }
    338350                }
    339351
    340352                $total_top = count( $top_level_elements );
    341                 if ( $paging )
    342                         $this->max_pages = ceil($total_top / $per_page);
    343                 else
     353                if ( $paging ) {
     354                        $this->max_pages = ceil( $total_top / $per_page );
     355                } else {
    344356                        $end = $total_top;
    345 
    346                 if ( !empty($args[0]['reverse_top_level']) ) {
     357                }
     358
     359                if ( ! empty( $args[0]['reverse_top_level'] ) ) {
    347360                        $top_level_elements = array_reverse( $top_level_elements );
    348                         $oldstart = $start;
    349                         $start = $total_top - $end;
    350                         $end = $total_top - $oldstart;
    351                 }
    352                 if ( !empty($args[0]['reverse_children']) ) {
    353                         foreach ( $children_elements as $parent => $children )
    354                                 $children_elements[$parent] = array_reverse( $children );
     361                        $oldstart           = $start;
     362                        $start              = $total_top - $end;
     363                        $end                = $total_top - $oldstart;
     364                }
     365                if ( ! empty( $args[0]['reverse_children'] ) ) {
     366                        foreach ( $children_elements as $parent => $children ) {
     367                                $children_elements[ $parent ] = array_reverse( $children );
     368                        }
    355369                }
    356370
    … …  
    359373
    360374                        // For the last page, need to unset earlier children in order to keep track of orphans.
    361                         if ( $end >= $total_top && $count < $start )
     375                        if ( $end >= $total_top && $count < $start ) {
    362376                                        $this->unset_children( $e, $children_elements );
    363 
    364                         if ( $count < $start )
     377                        }
     378
     379                        if ( $count < $start ) {
    365380                                continue;
    366 
    367                         if ( $count >= $end )
     381                        }
     382
     383                        if ( $count >= $end ) {
    368384                                break;
     385                        }
    369386
    370387                        $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
    … …  
    373390                if ( $end >= $total_top && count( $children_elements ) > 0 ) {
    374391                        $empty_array = array();
    375                         foreach ( $children_elements as $orphans )
    376                                 foreach ( $orphans as $op )
     392                        foreach ( $children_elements as $orphans ) {
     393                                foreach ( $orphans as $op ) {
    377394                                        $this->display_element( $op, $empty_array, 1, 0, $args, $output );
     395                                }
     396                        }
    378397                }
    379398
    … …  
    389408         * @return int Number of root elements.
    390409         */
    391         public function get_number_of_root_elements( $elements ){
    392                 $num = 0;
     410        public function get_number_of_root_elements( $elements ) {
     411                $num          = 0;
    393412                $parent_field = $this->db_fields['parent'];
    394413
    395                 foreach ( $elements as $e) {
    396                         if ( 0 == $e->$parent_field )
     414                foreach ( $elements as $e ) {
     415                        if ( 0 == $e->$parent_field ) {
    397416                                $num++;
     417                        }
    398418                }
    399419                return $num;
    … …  
    408428         * @param array $children_elements
    409429         */
    410         public function unset_children( $e, &$children_elements ){
     430        public function unset_children( $e, &$children_elements ) {
    411431                if ( ! $e || ! $children_elements ) {
    412432                        return;
    … …  
    414434
    415435                $id_field = $this->db_fields['id'];
    416                 $id = $e->$id_field;
    417 
    418                 if ( !empty($children_elements[$id]) && is_array($children_elements[$id]) )
    419                         foreach ( (array) $children_elements[$id] as $child )
     436                $id       = $e->$id_field;
     437
     438                if ( ! empty( $children_elements[ $id ] ) && is_array( $children_elements[ $id ] ) ) {
     439                        foreach ( (array) $children_elements[ $id ] as $child ) {
    420440                                $this->unset_children( $child, $children_elements );
     441                        }
     442                }
    421443
    422444                unset( $children_elements[ $id ] );
Note: See TracChangeset for help on using the changeset viewer.