[wp-trac] [WordPress Trac] #26877: Allow get_post() to accept a custom post object instead of only WP_Post

WordPress Trac noreply at wordpress.org
Mon Jan 20 04:27:06 UTC 2014


#26877: Allow get_post() to accept a custom post object instead of only WP_Post
--------------------------+------------------------------------
 Reporter:  MikeSchinkel  |      Owner:
     Type:  enhancement   |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Post Types    |    Version:
 Severity:  normal        |   Keywords:  has-patch dev-feedback
--------------------------+------------------------------------
 I'm refactoring a poorly coded theme that I want to use for it's design
 and I am finding it very helpful to be able to create a theme-specific
 Post class that encapsulates theme-specific behaviors related to Posts
 that the theme currently has ''"littered"'' over many different templates.
 ''(I usually build plugins and (almost?) never build themes but it seems
 this would be useful when ''building'' themes too.)''

 So I created a class that looks like this for my refactoring efforts
 ''(the following is only a small segment of the class but I've only
 included the relevant portions for this ticket):''

 {{{
 /**
  * Class MyTheme_Post
  */
 class MyTheme_Post {

   private $_post;
   private $_post_stack = array();

   /**
    * @param int|WP_Post $post
    */
   function __construct( $post ) {
     if ( is_numeric( $post ) ) {
       $post = get_post( $post );
     }
     $this->_post = $post;
   }

   /**
    * Get the title by using the_title() function.
    *
    * @param string $before
    * @param string $after
    *
    * @return string
    */
   function title( $before = '', $after = '' ) {
     $this->_push_post();
     $title = the_title( $before, $after, false );
     $this->_pop_post();
     return $title;
   }

   /**
    * Set global $post to contain the inner WP_Post and push the prior onto
 an internal stack.
    * We need this so that get_post() will work correctly.
    */
   function _push_post() {
     global $post;
     array_push( $this->_post_stack, $post );
     $post = $this->_post;
   }

   /**
    * Pop the prior post off the internal stack.
    * We need this so that get_post() will work correctly.
    */
   function _pop_post() {
     global $post;
     $post = array_pop( $this->_post_stack );
   }

   /**
    * Delegate any property requests to the contained post.
    *
    * @param string $property
    *
    * @return array|mixed
    */
   function __get( $property ) {
     return $this->_post->$property;
   }

 }
 }}}
 And then at the top of my theme template I do the following:
 {{{
 $post = new MyTheme_Post( $post );
 }}}
 This works '''brilliantly''' ''except'' **unfortunately** whenever
 `get_post()` is called by any template tag or other function, and lots of
 functions call it; `get_post()` throws away the custom instance and
 creates a new instance of `WP_Post` for use instead.  You see how I get
 around this in `$post->title()` above, but I can't always do that.

 I can envision two (2) fixes for this:

 1. Remove `final` from the `WP_Post` class so I can really subclass it, or
 2. Have `get_post()` inspect a special property ''(maybe
 `$post->is_custom`)'' and if it's set to `true` then assume it's an object
 the developer wants to keep.

 I know that subclassing
 [https://core.trac.wordpress.org/ticket/21309#comment:14 was not wanted
 for WP_Post when it was being implemented] so if that's still the case I'm
 hoping we could at least do option 2.

 Patch attached.

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


More information about the wp-trac mailing list