[wp-trac] [WordPress Trac] #30292: Stop get_post() overwriting non-WP_Post objects passed as first parameter

WordPress Trac noreply at wordpress.org
Sat Nov 8 21:38:22 UTC 2014


#30292: Stop get_post() overwriting non-WP_Post objects passed as first parameter
-------------------------------+-----------------------------
 Reporter:  MikeSchinkel       |      Owner:
     Type:  enhancement        |     Status:  new
 Priority:  normal             |  Milestone:  Awaiting Review
Component:  Posts, Post Types  |    Version:  trunk
 Severity:  normal             |   Keywords:
  Focuses:                     |
-------------------------------+-----------------------------
 In his 2014 New York WordCamp @nacin answered a question about `WP_Post`
 being declared `final` and @nacin suggested using composition as a
 workaround.  Our team has been using composition for a while now but it
 creates a problem when trying to create a modular custom theme for complex
 client applications, the types of applications that benefit so much from
 encapsulating the business logic into a class rather than having the
 business logic littered through theme templates.

 The problem is that the `get_post()` function checks `is_a( $post,
 'WP_Post' )` and also does `new WP_Post( $post )` and
 `WP_Post::get_instance( $post )` for different values of `$post` resulting
 in it destroying our object that contains an instance of `WP_Post`. And
 you can't get around this by just calling your own function instead of
 `get_post()` because so many other WordPress API functions call
 `get_post()` internally such as `get_the_title()` and `WP_Screen::get()`,
 for example.

 **I'd like to propose is a `'do_get_post'` filter.**

 This filter would be similar to
 [https://github.com/WordPress/WordPress/blob/master/wp-includes/class-
 wp.php#L132 `'do_parse_request'`] in that it would call a filter passing
 it a false value along with other values and if the filter returns a value
 that does not evaluate to false it will cause `get_post()` to simply
 return that value.  This code would be included as the first 3 lines
 within `get_post()`, additionally passing along all the parameters that
 might have been passed into the `get_post()` function:

 {{{
 if ( $_post = apply_filters( 'do_get_post', false, $post, $output, $filter
 ) ) {
    return $_post;
 }
 }}}

 This filter would be responsible for ensuring that a proper WP_Post object
 is returned and also responsible to respect the `$output` and `$filter`
 parameters.

 This filter would allow us to build complex custom WordPress applications
 that make the use of objects that contain and emulate a `WP_Post` object
 ''(which we are already doing)'' without forcing us to replace all of the
 functionality of WordPress that assumes a global WP_Post object ''(which
 we cannot currently do, at least not without hacking core.)''

--
Ticket URL: <https://core.trac.wordpress.org/ticket/30292>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list