Make WordPress Core


Ignore:
Timestamp:
08/28/2012 07:08:28 PM (14 years ago)
Author:
ryan
Message:

Add tags_input, page_template, and post_category get magic to WP_Post.
Deprecate get_post_to_edit() and wp_get_single_post().
Props scribu
see #21309

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r21628 r21651  
    476476                        return true;
    477477
     478                if ( 'page_template' == $key )
     479                        return ( 'page' == $this->post_type );
     480
     481                if ( 'post_category' == $key )
     482                   return true;
     483
     484                if ( 'tags_input' == $key )
     485                   return true;
     486
    478487                return metadata_exists( 'post', $this->ID, $key );
    479488        }
    480489
    481490        public function &__get( $key ) {
     491                if ( 'page_template' == $key && $this->__isset( $key ) ) {
     492                        $_ref = get_post_meta( $this->ID, '_wp_page_template', true );
     493                }
     494
     495                if ( 'post_category' == $key ) {
     496                        if ( is_object_in_taxonomy( $this->post_type, 'category' ) ) {
     497                                $_ref = wp_get_post_categories( $this->ID );
     498                        } else {
     499                                $_ref = array();
     500                        }
     501                }
     502
     503                if ( 'tags_input' == $key ) {
     504                        if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) ) {
     505                                $_ref = wp_get_post_tags( $this->ID, array( 'fields' => 'names' ) );
     506                        } else {
     507                                $_ref = array();
     508                        }
     509                }
     510
     511                if ( isset( $_ref ) )
     512                        return $_ref;
     513
     514                // Rest of the values need filtering
     515
    482516                if ( 'ancestors' == $key ) {
    483517                        $value = get_post_ancestors( $this );
    … …  
    505539        public function to_array() {
    506540                $post = get_object_vars( $this );
    507                 $post['ancestors'] = array();
     541
     542                foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) {
     543                        if ( $this->__isset( $key ) )
     544                                $post[ $key ] = $this->__get( $key );
     545                }
    508546
    509547                return $post;
    … …  
    22272265                return wp_delete_post($post_id, true);
    22282266
    2229         if ( !$post = wp_get_single_post($post_id, ARRAY_A) )
     2267        if ( !$post = get_post($post_id, ARRAY_A) )
    22302268                return $post;
    22312269
    … …  
    22592297 */
    22602298function wp_untrash_post($post_id = 0) {
    2261         if ( !$post = wp_get_single_post($post_id, ARRAY_A) )
     2299        if ( !$post = get_post($post_id, ARRAY_A) )
    22622300                return $post;
    22632301
    … …  
    24872525        return $results ? $results : false;
    24882526
    2489 }
    2490 
    2491 /**
    2492  * Retrieve a single post, based on post ID.
    2493  *
    2494  * Has categories in 'post_category' property or key. Has tags in 'tags_input'
    2495  * property or key.
    2496  *
    2497  * @since 1.0.0
    2498  *
    2499  * @param int $postid Post ID.
    2500  * @param string $mode How to return result, either OBJECT, ARRAY_N, or ARRAY_A.
    2501  * @return WP_Post|array WP_Post object or array holding post contents and information
    2502  */
    2503 function wp_get_single_post($postid = 0, $mode = OBJECT) {
    2504         $postid = (int) $postid;
    2505 
    2506         $post = get_post($postid, $mode);
    2507 
    2508         if (
    2509                 ( OBJECT == $mode && empty( $post->ID ) ) ||
    2510                 ( OBJECT != $mode && empty( $post['ID'] ) )
    2511         )
    2512                 return ( OBJECT == $mode ? null : array() );
    2513 
    2514         // Set categories and tags
    2515         if ( $mode == OBJECT ) {
    2516                 $post->post_category = array();
    2517                 if ( is_object_in_taxonomy($post->post_type, 'category') )
    2518                         $post->post_category = wp_get_post_categories($postid);
    2519                 $post->tags_input = array();
    2520                 if ( is_object_in_taxonomy($post->post_type, 'post_tag') )
    2521                         $post->tags_input = wp_get_post_tags($postid, array('fields' => 'names'));
    2522         } else {
    2523                 $post['post_category'] = array();
    2524                 if ( is_object_in_taxonomy($post['post_type'], 'category') )
    2525                         $post['post_category'] = wp_get_post_categories($postid);
    2526                 $post['tags_input'] = array();
    2527                 if ( is_object_in_taxonomy($post['post_type'], 'post_tag') )
    2528                         $post['tags_input'] = wp_get_post_tags($postid, array('fields' => 'names'));
    2529         }
    2530 
    2531         return $post;
    25322527}
    25332528
    … …  
    28332828
    28342829        // First, get all of the original fields
    2835         $post = wp_get_single_post($postarr['ID'], ARRAY_A);
     2830        $post = get_post($postarr['ID'], ARRAY_A);
    28362831
    28372832        // Escape data pulled from DB.
    … …  
    32423237        if ( ! empty( $tb_list ) ) {
    32433238                // get post data
    3244                 $postdata = wp_get_single_post($post_id, ARRAY_A);
     3239                $postdata = get_post($post_id, ARRAY_A);
    32453240
    32463241                // import postdata as variables
Note: See TracChangeset for help on using the changeset viewer.