Opened 7 years ago
Last modified 5 years ago
#43740 new enhancement
Filter WP_Post methods (vs. removing final/'get_post' filter)
Reported by: | MikeSchinkel | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | Posts, Post Types | Keywords: | has-patch dev-feedback needs-testing |
Focuses: | Cc: |
Description
There has been a lot of demand to remove final
from WP_Post
and to add a get_post
filter, for good reason. There as also be a lot of pushback on those actions, also for very good reason. Both sets of reasons can be found in ticket #24672 and #12955, respectively.
In a nutshell for, there are many valid use-cases where being able to extend WP_Post
would make for more robust and maintainable code. In the against case, removing 'final'
could easily resolve in an explosion of incompatible child classes making plugin interoperability challenging, and backward compatibility almost impossible if code decided to evolve WP_Post in any way. And the 'get_post'
filter would be almost as bad as removing final
, and I am one who advocated for both for years.
However, we actually already have an extensibility mechanism that has shown the test of time and that is the use of filter hooks. Given a judicious use of filter hooks we could actually allow developers to extend WP_Post
in a compossible manner thus minimizing plugin integration problems and also safeguarding core's ability to add enhancements in the future.
The proposal is basically to add filter hooks in the methods of WP_Post
, and to add a __set()
and __call()
magic method as a companion to the existing __get()
magic method. Here are the filters I am proposing:
Filter Hook | Method |
---|---|
'wp_post_supports_instance' | __construct()
|
'pre_wp_post__isset' | __isset()
|
'wp_post__isset' | __isset()
|
'wp_post__call' | __call()
|
'wp_post__set' | __set()
|
'pre_wp_post__get' | __get()
|
'wp_post_filter' | filter()
|
'wp_post_to_array' | to_array()
|
And then this action hook:
'wp_post__construct'
in__construct()
I have attached a patch for class-wp-post.php
that would implement these hooks.
Patch that would all method filters to WP_Post