WordPress.org

Make WordPress Core


Ignore:
Timestamp:
03/11/15 20:45:17 (3 years ago)
Author:
johnbillion
Message:

Introduce a new algorithm for displaying a hierarchical list of post objects in the WP_Posts_List_Table. This reduces processing time, reduces database queries, and substantially reduces memory use on sites with a high number of Pages.

Props nofearinc, rodrigosprimo, nacin, johnbillion.
Fixes #15459

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-posts-list-table.php

    r31513 r31730  
    8787    } 
    8888 
     89    /** 
     90     * Sets whether the table layout should be hierarchical or not. 
     91     * 
     92     * @since 4.2.0 
     93     * 
     94     * @param bool $display Whether the table layout should be hierarchical. 
     95     */ 
     96    public function set_hierarchical_display( $display ) { 
     97        $this->hierarchical_display = $display; 
     98    } 
     99 
    89100    public function ajax_user_can() { 
    90101        return current_user_can( get_post_type_object( $this->screen->post_type )->cap->edit_posts ); 
     
    96107        $avail_post_stati = wp_edit_posts_query(); 
    97108 
    98         $this->hierarchical_display = ( is_post_type_hierarchical( $this->screen->post_type ) && 'menu_order title' == $wp_query->query['orderby'] ); 
     109        $this->set_hierarchical_display( is_post_type_hierarchical( $this->screen->post_type ) && 'menu_order title' == $wp_query->query['orderby'] ); 
    99110 
    100111        $total_items = $this->hierarchical_display ? $wp_query->post_count : $wp_query->found_posts; 
     
    479490        $start = ( $pagenum - 1 ) * $per_page; 
    480491        $end = $start + $per_page; 
     492        $to_display = array(); 
    481493 
    482494        foreach ( $pages as $page ) { 
     
    485497 
    486498            if ( $count >= $start ) { 
    487                 echo "\t"; 
    488                 $this->single_row( $page, $level ); 
     499                $to_display[$page->ID] = $level; 
    489500            } 
    490501 
     
    492503 
    493504            if ( isset( $children_pages ) ) 
    494                 $this->_page_rows( $children_pages, $count, $page->ID, $level + 1, $pagenum, $per_page ); 
     505                $this->_page_rows( $children_pages, $count, $page->ID, $level + 1, $pagenum, $per_page, $to_display ); 
    495506        } 
    496507 
     
    503514 
    504515                    if ( $count >= $start ) { 
    505                         echo "\t"; 
    506                         $this->single_row( $op, 0 ); 
     516                        $to_display[$op->ID] = 0; 
    507517                    } 
    508518 
     
    510520                } 
    511521            } 
     522        } 
     523 
     524        $ids = array_keys( $to_display ); 
     525        _prime_post_caches( $ids ); 
     526 
     527        if ( ! isset( $GLOBALS['post'] ) ) { 
     528            $GLOBALS['post'] = array_shift( $ids ); 
     529        } 
     530 
     531        foreach ( $to_display as $page_id => $level ) { 
     532            echo "\t"; 
     533            $this->single_row( $page_id, $level ); 
    512534        } 
    513535    } 
     
    518540     * 
    519541     * @since 3.1.0 (Standalone function exists since 2.6.0) 
     542     * @since 4.2.0 Added the `$to_display` parameter. 
    520543     * 
    521544     * @param array $children_pages 
     
    525548     * @param int $pagenum 
    526549     * @param int $per_page 
     550     * @param array $to_display list of pages to be displayed 
    527551     */ 
    528     private function _page_rows( &$children_pages, &$count, $parent, $level, $pagenum, $per_page ) { 
     552    private function _page_rows( &$children_pages, &$count, $parent, $level, $pagenum, $per_page, &$to_display ) { 
    529553 
    530554        if ( ! isset( $children_pages[$parent] ) ) 
     
    544568                $my_parent = $page->post_parent; 
    545569                while ( $my_parent ) { 
    546                     $my_parent = get_post( $my_parent ); 
     570                    // Get the ID from the list or the attribute if my_parent is an object 
     571                    $parent_id = $my_parent; 
     572                    if ( is_object( $my_parent ) ) { 
     573                        $parent_id = $my_parent->ID; 
     574                    } 
     575 
     576                    $my_parent = get_post( $parent_id ); 
    547577                    $my_parents[] = $my_parent; 
    548578                    if ( !$my_parent->post_parent ) 
     
    552582                $num_parents = count( $my_parents ); 
    553583                while ( $my_parent = array_pop( $my_parents ) ) { 
    554                     echo "\t"; 
    555                     $this->single_row( $my_parent, $level - $num_parents ); 
     584                    $to_display[$my_parent->ID] = $level - $num_parents; 
    556585                    $num_parents--; 
    557586                } 
     
    559588 
    560589            if ( $count >= $start ) { 
    561                 echo "\t"; 
    562                 $this->single_row( $page, $level ); 
     590                $to_display[$page->ID] = $level; 
    563591            } 
    564592 
    565593            $count++; 
    566594 
    567             $this->_page_rows( $children_pages, $count, $page->ID, $level + 1, $pagenum, $per_page ); 
     595            $this->_page_rows( $children_pages, $count, $page->ID, $level + 1, $pagenum, $per_page, $to_display ); 
    568596        } 
    569597 
     
    580608 
    581609        $global_post = get_post(); 
     610 
     611        $post = get_post( $post ); 
     612 
    582613        $GLOBALS['post'] = $post; 
    583614        setup_postdata( $post ); 
Note: See TracChangeset for help on using the changeset viewer.