Make WordPress Core

Ticket #21309: 21309.6.diff

File 21309.6.diff, 12.8 KB (added by scribu, 13 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..d323e9e 100644
    function get_extended($post) { 
    375375 * @param int|object $post Post ID or post object.
    376376 * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
    377377 * @param string $filter Optional, default is raw.
    378  * @return mixed Post data
     378 * @return mixed Post data or null on failure
    379379 */
    380380function &get_post(&$post, $output = OBJECT, $filter = 'raw') {
    381         global $wpdb;
    382381        $null = null;
    383382
    384         if ( empty($post) ) {
    385                 if ( isset($GLOBALS['post']) )
    386                         $_post = & $GLOBALS['post'];
    387                 else
    388                         return $null;
    389         } elseif ( is_object($post) && empty($post->filter) ) {
    390                 _get_post_ancestors($post);
    391                 $_post = sanitize_post($post, 'raw');
    392                 wp_cache_add($post->ID, $_post, 'posts');
    393         } elseif ( is_object($post) && 'raw' == $post->filter ) {
     383        if ( empty( $post ) && isset( $GLOBALS['post'] ) ) {
     384                $_post = & $GLOBALS['post'];
     385        } elseif ( is_a( $post, 'WP_Post' ) ) {
    394386                $_post = $post;
     387        } elseif ( is_object( $post ) ) {
     388                if ( empty( $post->filter ) ) {
     389                        $_post = sanitize_post( $post, 'raw' );
     390                        wp_cache_add( $post->ID, $_post, 'posts' );
     391                        $_post = new WP_Post( $_post );
     392                } elseif ( 'raw' == $post->filter ) {
     393                        $_post = new WP_Post( $post );
     394                } else {
     395                        $_post = WP_Post::get_instance( $post->ID );
     396                }
    395397        } else {
    396                 if ( is_object($post) )
    397                         $post_id = $post->ID;
    398                 else
    399                         $post_id = $post;
     398                $_post = WP_Post::get_instance( $post );
     399        }
     400
     401        if ( !$_post )
     402                return $null;
     403
     404        if ( $filter != 'raw' )
     405                $_post = sanitize_post( $_post, $filter );
     406
     407        if ( $output == ARRAY_A ) {
     408                $__post = $_post->to_array();
     409                return $__post;
     410        } elseif ( $output == ARRAY_N ) {
     411                $__post = array_values( $_post->to_array() );
     412                return $__post;
     413        }
     414
     415        return $_post;
     416}
     417
     418/**
     419 * WordPress Post class.
     420 *
     421 * @since 3.5.0
     422 *
     423 * @property $ID;
     424 * @property $post_author;
     425 * @property $post_date;
     426 * @property $post_date_gmt;
     427 * @property $post_content;
     428 * @property $post_title;
     429 * @property $post_excerpt;
     430 * @property $post_status;
     431 * @property $comment_status;
     432 * @property $ping_status;
     433 * @property $post_password;
     434 * @property $post_name;
     435 * @property $to_ping;
     436 * @property $pinged;
     437 * @property $post_modified;
     438 * @property $post_modified_gmt;
     439 * @property $post_content_filtered;
     440 * @property $post_parent;
     441 * @property $guid;
     442 * @property $menu_order;
     443 * @property $post_type;
     444 * @property $post_mime_type;
     445 * @property $comment_count;
     446 * @property $ancestors;
     447 */
     448final class WP_Post {
     449
     450        public $filter;
     451
     452        public static function get_instance( $post_id ) {
     453                global $wpdb;
    400454
    401455                $post_id = (int) $post_id;
    402                 if ( ! $_post = wp_cache_get($post_id, 'posts') ) {
    403                         $_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id));
     456                if ( !$post_id )
     457                        return false;
     458
     459                if ( ! $_post = wp_cache_get( $post_id, 'posts' ) ) {
     460                        $_post = $wpdb->get_row( $wpdb->prepare( "
     461                                SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1
     462                        ", $post_id ) );
     463
    404464                        if ( ! $_post )
    405                                 return $null;
    406                         _get_post_ancestors($_post);
    407                         $_post = sanitize_post($_post, 'raw');
    408                         wp_cache_add($_post->ID, $_post, 'posts');
     465                                return false;
     466
     467                        $_post = sanitize_post( $_post, 'raw' );
     468                        wp_cache_add( $_post->ID, $_post, 'posts' );
    409469                }
     470
     471                return new WP_Post( $_post );
    410472        }
    411473
    412         if ($filter != 'raw')
    413                 $_post = sanitize_post($_post, $filter);
     474        public function __construct( $post ) {
     475                foreach ( get_object_vars( $post ) as $key => $value )
     476                        $this->$key = $value;
     477        }
    414478
    415         if ( $output == OBJECT ) {
    416                 return $_post;
    417         } elseif ( $output == ARRAY_A ) {
    418                 $__post = get_object_vars($_post);
    419                 return $__post;
    420         } elseif ( $output == ARRAY_N ) {
    421                 $__post = array_values(get_object_vars($_post));
    422                 return $__post;
    423         } else {
    424                 return $_post;
     479        public function __isset( $key ) {
     480                if ( 'ancestors' == $key )
     481                        return true;
     482
     483                return metadata_exists( 'post', $this->ID, $key );
     484        }
     485
     486        public function &__get( $key ) {
     487                if ( 'ancestors' == $key ) {
     488                        $value = get_post_ancestors( $this );
     489                } else {
     490                        $value = get_post_meta( $this->ID, $key, true );
     491                }
     492
     493                if ( $this->filter ) {
     494                        $value = sanitize_post_field( $key, $value, $this->ID, $this->filter );
     495                }
     496
     497                return $value;
     498        }
     499
     500        public function to_array() {
     501                $post = get_object_vars( $this );
     502                $post['ancestors'] = array();
     503
     504                return $post;
    425505        }
    426506}
    427507
    function &get_post(&$post, $output = OBJECT, $filter = 'raw') { 
    433513 * @param int|object $post Post ID or post object
    434514 * @return array Ancestor IDs or empty array if none are found.
    435515 */
    436 function get_post_ancestors($post) {
    437         $post = get_post($post);
     516function get_post_ancestors( $post ) {
     517        if ( !$post )
     518                return false;
    438519
    439         if ( ! isset( $post->ancestors ) )
    440                 _get_post_ancestors( $post );
     520        if ( ! $ancestors = wp_cache_get( $post->ID, 'post_ancestors' ) ) {
     521                $ancestors = array();
    441522
    442         if ( ! empty( $post->ancestors ) )
    443                 return $post->ancestors;
     523                if ( !empty( $post->post_parent ) && $post->ID != $post->post_parent ) {
     524                        $id = $ancestors[] = $post->post_parent;
    444525
    445         return array();
     526                        while ( $ancestor = get_post( $id ) ) {
     527                                // Loop detection: If the ancestor has been seen before, break.
     528                                if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) )
     529                                        break;
     530
     531                                $id = $ancestors[] = $ancestor->post_parent;
     532                        }
     533                }
     534
     535                wp_cache_add( $post->ID, $ancestors, 'post_ancestors' );
     536        }
     537
     538        return $ancestors;
    446539}
    447540
    448541/**
    function get_post_ancestors($post) { 
    460553 * @param string $field Post field name
    461554 * @param id $post Post ID
    462555 * @param string $context Optional. How to filter the field. Default is display.
    463  * @return WP_Error|string Value in post field or WP_Error on failure
     556 * @return bool|string False on failure or returns the value in post field
    464557 */
    465558function get_post_field( $field, $post, $context = 'display' ) {
    466         $post = (int) $post;
    467559        $post = get_post( $post );
    468560
    469         if ( is_wp_error($post) )
    470                 return $post;
    471 
    472         if ( !is_object($post) )
     561        if ( !$post )
    473562                return '';
    474563
    475564        if ( !isset($post->$field) )
    function get_page_uri($page) { 
    33483437                $page = get_page($page);
    33493438        $uri = $page->post_name;
    33503439
    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;
     3440        foreach ( $page->ancestors as $parent ) {
     3441                $uri = get_page($parent)->post_name . "/" . $uri;
    33583442        }
    33593443
    33603444        return $uri;
    function &get_pages($args = '') { 
    34063490        $key = md5( serialize( compact(array_keys($defaults)) ) );
    34073491        if ( $cache = wp_cache_get( 'get_pages', 'posts' ) ) {
    34083492                if ( is_array($cache) && isset( $cache[ $key ] ) ) {
    3409                         $pages = apply_filters('get_pages', $cache[ $key ], $r );
    3410                         return $pages;
     3493                        // Convert to WP_Post instances
     3494                        $pages = array_map( 'get_post', $cache[ $key ] );
     3495
     3496                        return apply_filters( 'get_pages', $pages, $r );
    34113497                }
    34123498        }
    34133499
    function &get_pages($args = '') { 
    35813667        $cache[ $key ] = $pages;
    35823668        wp_cache_set( 'get_pages', $cache, 'posts' );
    35833669
     3670        // Convert to WP_Post instances
     3671        $pages = array_map( 'get_post', $pages );
     3672
    35843673        $pages = apply_filters('get_pages', $pages, $r);
    35853674
    35863675        return $pages;
    function _save_post_hook( $post_id, $post ) { 
    46124701}
    46134702
    46144703/**
    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 /**
    46544704 * Determines which fields of posts are to be saved in revisions.
    46554705 *
    46564706 * Does two things. If passed a post *array*, it will return a post array ready
  • wp-includes/query.php

    diff --git wp-includes/query.php wp-includes/query.php
    index e976cdf..e8c5454 100644
    class WP_Query { 
    26432643
    26442644                        if ( $ids ) {
    26452645                                $this->set_found_posts( $q, $limits );
    2646 
    26472646                                _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
    2648 
    2649                                 $this->posts = array_map( 'get_post', $ids );
     2647                                $this->posts = $ids;
    26502648                        } else {
    2651                                 $this->found_posts = $this->max_num_pages = 0;
    26522649                                $this->posts = array();
     2650                                $this->found_posts = $this->max_num_pages = 0;
    26532651                        }
    26542652                } else {
    26552653                        $this->posts = $wpdb->get_results( $this->request );
     2654                        update_post_caches( $this->posts, 'any', $q['update_post_term_cache'], $q['update_post_meta_cache'] );
    26562655                        $this->set_found_posts( $q, $limits );
    26572656                }
    26582657
     2658                // Convert to WP_Post objects
     2659                $this->posts = array_map( 'get_post', $this->posts );
     2660
    26592661                // Raw results filter. Prior to status checks.
    26602662                if ( !$q['suppress_filters'] )
    26612663                        $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
  • 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);