WordPress.org

Make WordPress Core

Ticket #16574: 16574.7.diff

File 16574.7.diff, 7.9 KB (added by scribu, 6 years ago)

avoid Inception

  • 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..2abd39c 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') { 
    404402                $_post = sanitize_post($_post, $filter);
    405403
    406404        if ( $output == OBJECT ) {
     405                if ( is_a( $_post, 'stdClass' ) )
     406                        $_post = new _WP_Post_Wrapper( $_post );
    407407                return $_post;
    408408        } elseif ( $output == ARRAY_A ) {
    409409                $__post = get_object_vars($_post);
    function &get_post(&$post, $output = OBJECT, $filter = 'raw') { 
    417417}
    418418
    419419/**
     420 * Wrapper class to preserve back-compat for $post->ancestors
     421 *
     422 * @since 3.4.0
     423 */
     424class _WP_Post_Wrapper {
     425
     426        private $post;
     427
     428        function __construct( $post ) {
     429                $this->post = $post;
     430        }
     431
     432        function __isset( $key ) {
     433                if ( 'ancestors' == $key )
     434                        return true;
     435
     436                return isset( $this->post->$key );
     437        }
     438
     439        function __get( $key ) {
     440                if ( 'ancestors' == $key )
     441                        return get_post_ancestors( $this->post );
     442
     443                return $this->post->$key;
     444        }
     445
     446        function __set( $key, $value ) {
     447                if ( 'ancestors' == $key )
     448                        return;
     449
     450                $this->post->$key = $value;
     451        }
     452}
     453
     454/**
    420455 * Retrieve ancestors of a post.
    421456 *
    422457 * @since 2.5.0
    function &get_post(&$post, $output = OBJECT, $filter = 'raw') { 
    424459 * @param int|object $post Post ID or post object
    425460 * @return array Ancestor IDs or empty array if none are found.
    426461 */
    427 function get_post_ancestors($post) {
    428         $post = get_post($post);
     462function get_post_ancestors( $post ) {
     463        $post = get_post( $post );
     464
     465        if ( !$post )
     466                return false;
     467
     468        if ( ! $ancestors = wp_cache_get( $post->ID, 'post_ancestors' ) ) {
     469                $ancestors = array();
     470
     471                if ( !empty( $post->post_parent ) && $post->ID != $post->post_parent ) {
     472                        $id = $ancestors[] = $post->post_parent;
     473
     474                        while ( $ancestor = get_post( $id ) ) {
     475                                // Loop detection: If the ancestor has been seen before, break.
     476                                if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) )
     477                                        break;
    429478
    430         if ( !empty($post->ancestors) )
    431                 return $post->ancestors;
     479                                $id = $ancestors[] = $ancestor->post_parent;
     480                        }
     481                }
    432482
    433         return array();
     483                wp_cache_add( $post->ID, $ancestors, 'post_ancestors' );
     484        }
     485
     486        return $ancestors;
    434487}
    435488
    436489/**
    function get_page_uri($page) { 
    33003353                $page = get_page($page);
    33013354        $uri = $page->post_name;
    33023355
    3303         // A page cannot be it's own parent.
    3304         if ( $page->post_parent == $page->ID )
    3305                 return $uri;
    3306 
    3307         while ($page->post_parent != 0) {
    3308                 $page = get_page($page->post_parent);
    3309                 $uri = $page->post_name . "/" . $uri;
     3356        foreach ( $page->ancestors as $parent ) {
     3357                $uri = get_page($parent)->post_name . "/" . $uri;
    33103358        }
    33113359
    33123360        return $uri;
    function _save_post_hook($post_id, $post) { 
    46134661}
    46144662
    46154663/**
    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 /**
    46554664 * Determines which fields of posts are to be saved in revisions.
    46564665 *
    46574666 * 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 
    53385347
    53395348                update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
    53405349        }
    5341 }
    5342  No newline at end of file
     5350}
  • 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);