Make WordPress Core

Ticket #16574: 16574.4.diff

File 16574.4.diff, 6.4 KB (added by scribu, 12 years ago)

Introduce 'post_ancestors' cache group

  • wp-includes/nav-menu-template.php

    diff --git wp-includes/nav-menu-template.php wp-includes/nav-menu-template.php
    index 1dfc661..4789fc4 100644
    function _wp_menu_item_classes_by_context( &$menu_items ) { 
    287287                                }
    288288                        }
    289289                }
    290         } elseif ( ! empty( $queried_object->post_type ) && is_post_type_hierarchical( $queried_object->post_type ) ) {
    291                 _get_post_ancestors( $queried_object );
    292290        } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
    293291                $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy );
    294292                $term_to_ancestor = array();
    function _wp_menu_item_classes_by_context( &$menu_items ) { 
    419417                                        'post_type' == $parent_item->type &&
    420418                                        ! empty( $queried_object->post_type ) &&
    421419                                        is_post_type_hierarchical( $queried_object->post_type ) &&
    422                                         in_array( $parent_item->object_id, $queried_object->ancestors ) &&
     420                                        in_array( $parent_item->object_id, get_post_ancestors( $queried_object ) ) &&
    423421                                        $parent_item->object != $queried_object->ID
    424422                                ) ||
    425423
  • wp-includes/post-template.php

    diff --git wp-includes/post-template.php wp-includes/post-template.php
    index 7a60672..b2a63fa 100644
    class Walker_Page extends Walker { 
    10351035                $css_class = array('page_item', 'page-item-'.$page->ID);
    10361036                if ( !empty($current_page) ) {
    10371037                        $_current_page = get_page( $current_page );
    1038                         _get_post_ancestors($_current_page);
    1039                         if ( isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors) )
     1038                        if ( in_array( $page->ID, get_post_ancestors( $current_page ) ) )
    10401039                                $css_class[] = 'current_page_ancestor';
    10411040                        if ( $page->ID == $current_page )
    10421041                                $css_class[] = 'current_page_item';
  • wp-includes/post.php

    diff --git wp-includes/post.php wp-includes/post.php
    index 3fcaa46..675041f 100644
    function &get_post(&$post, $output = OBJECT, $filter = 'raw') { 
    378378                else
    379379                        return $null;
    380380        } elseif ( is_object($post) && empty($post->filter) ) {
    381                 _get_post_ancestors($post);
    382381                $_post = sanitize_post($post, 'raw');
    383382                wp_cache_add($post->ID, $_post, 'posts');
    384383        } elseif ( is_object($post) && 'raw' == $post->filter ) {
    function &get_post(&$post, $output = OBJECT, $filter = 'raw') { 
    394393                        $_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id));
    395394                        if ( ! $_post )
    396395                                return $null;
    397                         _get_post_ancestors($_post);
    398396                        $_post = sanitize_post($_post, 'raw');
    399397                        wp_cache_add($_post->ID, $_post, 'posts');
    400398                }
    function &get_post(&$post, $output = OBJECT, $filter = 'raw') { 
    424422 * @param int|object $post Post ID or post object
    425423 * @return array Ancestor IDs or empty array if none are found.
    426424 */
    427 function get_post_ancestors($post) {
    428         $post = get_post($post);
     425function get_post_ancestors( $post ) {
     426        $post = get_post( $post );
     427
     428        if ( !$post )
     429                return false;
     430
     431        if ( ! $ancestors = wp_cache_get( $post->ID, 'post_ancestors' ) ) {
     432                $ancestors = array();
     433
     434                if ( !empty( $post->post_parent ) && $post->ID != $post->post_parent ) {
     435                        $id = $ancestors[] = $post->post_parent;
     436
     437                        while ( $ancestor = get_post( $id ) ) {
     438                                // Loop detection: If the ancestor has been seen before, break.
     439                                if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) )
     440                                        break;
    429441
    430         if ( !empty($post->ancestors) )
    431                 return $post->ancestors;
     442                                $id = $ancestors[] = $ancestor->post_parent;
     443                        }
     444                }
     445
     446                wp_cache_add( $post->ID, $ancestors, 'post_ancestors' );
     447        }
    432448
    433         return array();
     449        return $ancestors;
    434450}
    435451
    436452/**
    function _save_post_hook($post_id, $post) { 
    46134629}
    46144630
    46154631/**
    4616  * Retrieve post ancestors and append to post ancestors property.
    4617  *
    4618  * Will only retrieve ancestors once, if property is already set, then nothing
    4619  * will be done. If there is not a parent post, or post ID and post parent ID
    4620  * are the same then nothing will be done.
    4621  *
    4622  * The parameter is passed by reference, so nothing needs to be returned. The
    4623  * property will be updated and can be referenced after the function is
    4624  * complete. The post parent will be an ancestor and the parent of the post
    4625  * parent will be an ancestor. There will only be two ancestors at the most.
    4626  *
    4627  * @since 2.5.0
    4628  * @access private
    4629  * @uses $wpdb
    4630  *
    4631  * @param object $_post Post data.
    4632  * @return null When nothing needs to be done.
    4633  */
    4634 function _get_post_ancestors(&$_post) {
    4635         global $wpdb;
    4636 
    4637         if ( isset($_post->ancestors) )
    4638                 return;
    4639 
    4640         $_post->ancestors = array();
    4641 
    4642         if ( empty($_post->post_parent) || $_post->ID == $_post->post_parent )
    4643                 return;
    4644 
    4645         $id = $_post->ancestors[] = $_post->post_parent;
    4646         while ( $ancestor = $wpdb->get_var( $wpdb->prepare("SELECT `post_parent` FROM $wpdb->posts WHERE ID = %d LIMIT 1", $id) ) ) {
    4647                 // Loop detection: If the ancestor has been seen before, break.
    4648                 if ( ( $ancestor == $_post->ID ) || in_array($ancestor,  $_post->ancestors) )
    4649                         break;
    4650                 $id = $_post->ancestors[] = $ancestor;
    4651         }
    4652 }
    4653 
    4654 /**
    46554632 * Determines which fields of posts are to be saved in revisions.
    46564633 *
    46574634 * Does two things. If passed a post *array*, it will return a post array ready
    function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache 
    53385315
    53395316                update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
    53405317        }
    5341 }
    5342  No newline at end of file
     5318}
  • wp-includes/taxonomy.php

    diff --git wp-includes/taxonomy.php wp-includes/taxonomy.php
    index 6334ee7..3da27dd 100644
    function get_ancestors($object_id = 0, $object_type = '') { 
    32063206                        $ancestors[] = (int) $term->parent;
    32073207                        $term = get_term($term->parent, $object_type);
    32083208                }
    3209         } elseif ( null !== get_post_type_object( $object_type ) ) {
    3210                 $object = get_post($object_id);
    3211                 if ( ! is_wp_error( $object ) && isset( $object->ancestors ) && is_array( $object->ancestors ) )
    3212                         $ancestors = $object->ancestors;
    3213                 else {
    3214                         while ( ! is_wp_error($object) && ! empty( $object->post_parent ) && ! in_array( $object->post_parent, $ancestors ) ) {
    3215                                 $ancestors[] = (int) $object->post_parent;
    3216                                 $object = get_post($object->post_parent);
    3217                         }
    3218                 }
     3209        } elseif ( post_type_exists( $object_type ) ) {
     3210                $ancestors = get_post_ancestors($object_id);
    32193211        }
    32203212
    32213213        return apply_filters('get_ancestors', $ancestors, $object_id, $object_type);