WordPress.org

Make WordPress Core

Ticket #21309: 21309.diff

File 21309.diff, 7.9 KB (added by scribu, 8 years ago)
  • wp-includes/load.php

    diff --git wp-includes/load.php wp-includes/load.php
    index 94e6339..09e6067 100644
    function wp_start_object_cache() { 
    410410
    411411        if ( function_exists( 'wp_cache_add_global_groups' ) ) {
    412412                wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
    413                 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
     413                wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins', 'post_ancestors' ) );
    414414        }
    415415}
    416416
  • wp-includes/nav-menu-template.php

    diff --git wp-includes/nav-menu-template.php wp-includes/nav-menu-template.php
    index 51321f5..2173087 100644
    function _wp_menu_item_classes_by_context( &$menu_items ) { 
    286286                                }
    287287                        }
    288288                }
    289         } elseif ( ! empty( $queried_object->post_type ) && is_post_type_hierarchical( $queried_object->post_type ) ) {
    290                 _get_post_ancestors( $queried_object );
    291289        } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
    292290                $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy );
    293291                $term_to_ancestor = array();
  • wp-includes/post-template.php

    diff --git wp-includes/post-template.php wp-includes/post-template.php
    index 0581a10..89ad4c4 100644
    class Walker_Page extends Walker { 
    10161016                $css_class = array('page_item', 'page-item-'.$page->ID);
    10171017                if ( !empty($current_page) ) {
    10181018                        $_current_page = get_page( $current_page );
    1019                         _get_post_ancestors($_current_page);
    1020                         if ( isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors) )
     1019                        if ( in_array( $page->ID, $_current_page->ancestors ) )
    10211020                                $css_class[] = 'current_page_ancestor';
    10221021                        if ( $page->ID == $current_page )
    10231022                                $css_class[] = 'current_page_item';
  • wp-includes/post.php

    diff --git wp-includes/post.php wp-includes/post.php
    index ea4f3d6..32a2ca1 100644
    function &get_post(&$post, $output = OBJECT, $filter = 'raw') { 
    387387                else
    388388                        return $null;
    389389        } elseif ( is_object($post) && empty($post->filter) ) {
    390                 _get_post_ancestors($post);
    391390                $_post = sanitize_post($post, 'raw');
    392391                wp_cache_add($post->ID, $_post, 'posts');
    393392        } elseif ( is_object($post) && 'raw' == $post->filter ) {
    function &get_post(&$post, $output = OBJECT, $filter = 'raw') { 
    403402                        $_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id));
    404403                        if ( ! $_post )
    405404                                return $null;
    406                         _get_post_ancestors($_post);
    407405                        $_post = sanitize_post($_post, 'raw');
    408406                        wp_cache_add($_post->ID, $_post, 'posts');
    409407                }
    function &get_post(&$post, $output = OBJECT, $filter = 'raw') { 
    413411                $_post = sanitize_post($_post, $filter);
    414412
    415413        if ( $output == OBJECT ) {
     414                if ( is_a( $_post, 'stdClass' ) )
     415                        $_post = new WP_Post( $_post );
    416416                return $_post;
    417417        } elseif ( $output == ARRAY_A ) {
    418418                $__post = get_object_vars($_post);
    function &get_post(&$post, $output = OBJECT, $filter = 'raw') { 
    426426}
    427427
    428428/**
     429 * Wrapper class to preserve back-compat for $post->ancestors
     430 *
     431 * @since 3.4.0
     432 */
     433class WP_Post {
     434
     435        private $post;
     436
     437        function __construct( $post ) {
     438                $this->post = $post;
     439        }
     440
     441        function __isset( $key ) {
     442                if ( 'ancestors' == $key )
     443                        return true;
     444
     445                return isset( $this->post->$key );
     446        }
     447
     448        function &__get( $key ) {
     449                if ( 'ancestors' == $key )
     450                        $ref = get_post_ancestors( $this );
     451                else
     452                        $ref = &$this->post->$key;
     453
     454                return $ref;
     455        }
     456
     457        function __set( $key, $value ) {
     458                if ( 'ancestors' == $key )
     459                        return;
     460
     461                $this->post->$key = $value;
     462        }
     463}
     464
     465/**
    429466 * Retrieve ancestors of a post.
    430467 *
    431468 * @since 2.5.0
    function &get_post(&$post, $output = OBJECT, $filter = 'raw') { 
    433470 * @param int|object $post Post ID or post object
    434471 * @return array Ancestor IDs or empty array if none are found.
    435472 */
    436 function get_post_ancestors($post) {
    437         $post = get_post($post);
     473function get_post_ancestors( $post ) {
     474        if ( !$post )
     475                return false;
    438476
    439         if ( ! isset( $post->ancestors ) )
    440                 _get_post_ancestors( $post );
     477        if ( ! $ancestors = wp_cache_get( $post->ID, 'post_ancestors' ) ) {
     478                $ancestors = array();
    441479
    442         if ( ! empty( $post->ancestors ) )
    443                 return $post->ancestors;
     480                if ( !empty( $post->post_parent ) && $post->ID != $post->post_parent ) {
     481                        $id = $ancestors[] = $post->post_parent;
    444482
    445         return array();
     483                        while ( $ancestor = get_post( $id ) ) {
     484                                // Loop detection: If the ancestor has been seen before, break.
     485                                if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) )
     486                                        break;
     487
     488                                $id = $ancestors[] = $ancestor->post_parent;
     489                        }
     490                }
     491
     492                wp_cache_add( $post->ID, $ancestors, 'post_ancestors' );
     493        }
     494
     495        return $ancestors;
    446496}
    447497
    448498/**
    function get_page_uri($page) { 
    33483398                $page = get_page($page);
    33493399        $uri = $page->post_name;
    33503400
    3351         // A page cannot be it's own parent.
    3352         if ( $page->post_parent == $page->ID )
    3353                 return $uri;
    3354 
    3355         while ($page->post_parent != 0) {
    3356                 $page = get_page($page->post_parent);
    3357                 $uri = $page->post_name . "/" . $uri;
     3401        foreach ( $page->ancestors as $parent ) {
     3402                $uri = get_page($parent)->post_name . "/" . $uri;
    33583403        }
    33593404
    33603405        return $uri;
    function _save_post_hook( $post_id, $post ) { 
    46124657}
    46134658
    46144659/**
    4615  * Retrieve post ancestors and append to post ancestors property.
    4616  *
    4617  * Will only retrieve ancestors once, if property is already set, then nothing
    4618  * will be done. If there is not a parent post, or post ID and post parent ID
    4619  * are the same then nothing will be done.
    4620  *
    4621  * The parameter is passed by reference, so nothing needs to be returned. The
    4622  * property will be updated and can be referenced after the function is
    4623  * complete. The post parent will be an ancestor and the parent of the post
    4624  * parent will be an ancestor. There will only be two ancestors at the most.
    4625  *
    4626  * @since 2.5.0
    4627  * @access private
    4628  * @uses $wpdb
    4629  *
    4630  * @param object $_post Post data.
    4631  * @return null When nothing needs to be done.
    4632  */
    4633 function _get_post_ancestors(&$_post) {
    4634         global $wpdb;
    4635 
    4636         if ( isset($_post->ancestors) )
    4637                 return;
    4638 
    4639         $_post->ancestors = array();
    4640 
    4641         if ( empty($_post->post_parent) || $_post->ID == $_post->post_parent )
    4642                 return;
    4643 
    4644         $id = $_post->ancestors[] = (int) $_post->post_parent;
    4645         while ( $ancestor = $wpdb->get_var( $wpdb->prepare("SELECT `post_parent` FROM $wpdb->posts WHERE ID = %d LIMIT 1", $id) ) ) {
    4646                 // Loop detection: If the ancestor has been seen before, break.
    4647                 if ( ( $ancestor == $_post->ID ) || in_array($ancestor,  $_post->ancestors) )
    4648                         break;
    4649                 $id = $_post->ancestors[] = (int) $ancestor;
    4650         }
    4651 }
    4652 
    4653 /**
    46544660 * Determines which fields of posts are to be saved in revisions.
    46554661 *
    46564662 * Does two things. If passed a post *array*, it will return a post array ready
  • wp-includes/taxonomy.php

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