Make WordPress Core

Ticket #12955: 12955.4.diff

File 12955.4.diff, 4.8 KB (added by MikeSchinkel, 9 years ago)

Another approach: 'make_post_instance' and 'get_virtual_post_instance hooks' because some people at 10up were worried about calling the hook after caching (I don't think it would have any significant effect but I'm trying to provide an alternate solution.)

  • wp-admin/includes/post.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    618618                $post->page_template = 'default';
    619619                $post->post_parent = 0;
    620620                $post->menu_order = 0;
    621                 $post = new WP_Post( $post );
     621                $post = WP_Post::make_instance( $post );
    622622        }
    623623
    624624        /**
     
    18441844         */
    18451845        wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) );
    18461846        exit;
    1847 }
    1848  No newline at end of file
     1847}
  • wp-includes/class-wp-post.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    197197        public $filter;
    198198
    199199        /**
     200         * Flag to disallow "new WP_Post()" to be called w/o deprecation warning.
     201         *
     202         * @var bool
     203         */
     204        private static $_allow__construct = false;
     205
     206        /**
     207         * Retrieve virtual WP_Post instance.
     208         *
     209         * @static
     210         * @access public
     211         *
     212         * @param WP_Post|object|array $post Post value.
     213         * @return WP_Post|object Post object.
     214         */
     215        public static function make_instance( $post ) {
     216
     217                if ( is_array( $post ) || 'stdClass' === get_class( $post ) ) {
     218                        self::$_allow__construct = true;
     219                        $post = new WP_Post( (object) $post );
     220                        self::$_allow__construct = false;
     221                }
     222
     223                /**
     224                 * Filter post object to be sanitized, virtualized and/or have properties annotated.
     225                 *
     226                 * @since 4.4.0
     227                 *
     228                 * @param WP_Post $post Post object to sanitize/virtualize/annotate/etc.
     229                 */
     230                return apply_filters( 'make_post_instance', $post );
     231
     232        }
     233
     234        /**
    200235         * Retrieve WP_Post instance.
    201236         *
    202237         * @static
     
    219254                if ( ! $_post ) {
    220255                        $_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) );
    221256
    222                         if ( ! $_post )
    223                                 return false;
     257                        if ( ! $_post ) {
     258
     259                                /**
     260                                 * Retrieve a virtual post instance based on its post_id
     261                                 *
     262                                 * @since 4.4.0
     263                                 *
     264                                 * @param int $post_id Virtual post object to retrieve
     265                                 */
     266                                $_post = apply_filters( 'get_virtual_post_instance', $post_id );
     267
     268                                if ( ! $_post ) {
     269                                        return false;
     270                                }
     271                        }
    224272
    225273                        $_post = sanitize_post( $_post, 'raw' );
    226274                        wp_cache_add( $_post->ID, $_post, 'posts' );
     275
    227276                } elseif ( empty( $_post->filter ) ) {
     277
    228278                        $_post = sanitize_post( $_post, 'raw' );
     279
    229280                }
    230281
    231                 return new WP_Post( $_post );
     282                return self::make_instance( $_post );
     283
    232284        }
    233285
    234286        /**
    235287         * Constructor.
    236288         *
    237          * @param WP_Post|object $post Post object.
     289         * @param WP_Post|object    $post      Post object.
    238290         */
    239291        public function __construct( $post ) {
     292
     293                if ( ! self::$_allow__construct ) {
     294                        _deprecated_function( 'new ' . __CLASS__ . '()', '4.4', 'WP_Post::make_instance()' );
     295                }
     296
    240297                foreach ( get_object_vars( $post ) as $key => $value )
    241298                        $this->$key = $value;
    242299        }
  • wp-includes/post-functions.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    433433        } elseif ( is_object( $post ) ) {
    434434                if ( empty( $post->filter ) ) {
    435435                        $_post = sanitize_post( $post, 'raw' );
    436                         $_post = new WP_Post( $_post );
     436                        $_post = WP_Post::make_instance( $_post );
    437437                } elseif ( 'raw' == $post->filter ) {
    438                         $_post = new WP_Post( $post );
     438                        $_post = WP_Post::make_instance( $post );
    439439                } else {
    440440                        $_post = WP_Post::get_instance( $post->ID );
    441441                }
  • wp-admin/includes/media.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    11871187        if ( is_int($post) )
    11881188                $post = get_post($post);
    11891189        if ( is_array($post) )
    1190                 $post = new WP_Post( (object) $post );
     1190                $post = WP_Post::make_instance( $post );
    11911191
    11921192        $image_url = wp_get_attachment_url($post->ID);
    11931193
  • wp-includes/class-wp-customize-setting.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    13481348
    13491349                $item->ID = $this->post_id;
    13501350                $item->db_id = $this->post_id;
    1351                 $post = new WP_Post( (object) $item );
     1351                $post = WP_Post::make_instance( $item );
    13521352
    13531353                if ( empty( $post->post_author ) ) {
    13541354                        $post->post_author = get_current_user_id();