Make WordPress Core

Ticket #30292: 30292.2.diff

File 30292.2.diff, 2.5 KB (added by MikeSchinkel, 9 years ago)

Adds 'do_get_post' hook to get_post(), revised for 4.4.

  • wp-includes/post-functions.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    425425 *                            When $output is OBJECT, a `WP_Post` instance is returned.
    426426 */
    427427function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) {
    428         if ( empty( $post ) && isset( $GLOBALS['post'] ) )
    429                 $post = $GLOBALS['post'];
     428
     429        /**
     430         * Filter that allows a custom object to be provided for post when appropriate.
     431         *
     432         * Allows a developer to substitute an object that contains a WP_Post or that simulates a WP_Post.
     433         *
     434         * @since 4.4.0
     435         *
     436         * @param object|null   null    Value to return if no custom post it to be provided
     437         * @param string        $output Optional, default is Object. Accepts OBJECT, ARRAY_A, or ARRAY_N.
     438         *                              Default OBJECT.
     439         * @param string        $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
     440         *                              or 'display'. Default 'raw'.
     441         */
     442        $_post = apply_filters( 'do_get_post', null, $output, $filter );
     443
     444        if ( is_null( $_post ) ) {
     445
     446                if ( empty( $post ) && isset( $GLOBALS['post'] ) ) {
     447                        $post = $GLOBALS['post'];
     448                }
    430449
    431         if ( $post instanceof WP_Post ) {
    432                 $_post = $post;
    433         } elseif ( is_object( $post ) ) {
    434                 if ( empty( $post->filter ) ) {
    435                         $_post = sanitize_post( $post, 'raw' );
    436                         $_post = new WP_Post( $_post );
    437                 } elseif ( 'raw' == $post->filter ) {
    438                         $_post = new WP_Post( $post );
    439                 } else {
    440                         $_post = WP_Post::get_instance( $post->ID );
    441                 }
    442         } else {
    443                 $_post = WP_Post::get_instance( $post );
    444         }
     450                if ( $post instanceof WP_Post ) {
     451                        $_post = $post;
     452                } elseif ( is_object( $post ) ) {
     453                        if ( empty( $post->filter ) ) {
     454                                $_post = sanitize_post( $post, 'raw' );
     455                                $_post = new WP_Post( $_post );
     456                        } elseif ( 'raw' == $post->filter ) {
     457                                $_post = new WP_Post( $post );
     458                        } else {
     459                                $_post = WP_Post::get_instance( $post->ID );
     460                        }
     461                } else {
     462                        $_post = WP_Post::get_instance( $post );
     463                }
    445464
    446         if ( ! $_post )
    447                 return null;
     465                if ( ! $_post ) {
     466                        return null;
     467                }
    448468
     469        }
     470
    449471        $_post = $_post->filter( $filter );
    450472
    451         if ( $output == ARRAY_A )
     473        if ( $output == ARRAY_A ) {
    452474                return $_post->to_array();
    453         elseif ( $output == ARRAY_N )
     475        } elseif ( $output == ARRAY_N ) {
    454476                return array_values( $_post->to_array() );
     477        }
    455478
    456479        return $_post;
     480
    457481}
    458482
    459483/**