[wp-trac] [WordPress Trac] #36094: Solving the Globals Problem... Introduce Dependency Injection For Filters and Actions

WordPress Trac noreply at wordpress.org
Fri Mar 4 21:24:33 UTC 2016


#36094: Solving the Globals Problem... Introduce Dependency Injection For Filters
and Actions
-------------------------+-----------------------------
 Reporter:  seancjones   |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Plugins      |    Version:  trunk
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 This has been bugging me for a while. I'm hoping to tease this idea out
 further with the help of all you wonderful people.

 Presently, the only way to pass values in WordPress is via the global
 scope. For large projects, I am sure we have all had that "banging your
 head against the wall" moment where we might wish there was a better way.

 I'm proposing a change to the WordPress core: Adding a 5th argument to
 `add_filter` and `add_action` called ''$dependencies''. Like ''$priority''
 and ''$accepted_args'', this variable would be optional. If passed, it
 will expect an array. Any passed variable should be treated like an array,
 even if it's just a single variable.

 Developers seeking to make use of dependencies can forego globals and pass
 their dependencies via this array, calling them in the filter after
 **all** accepted args of an action are called.

 Example:
 {{{
 $title = $wpdb->get_results( 'SELECT title FROM customtable' );
 add_filter( 'the_title', 'get_custom_title', 10, 1, array( 'title' =>
 $title, 'post' => $post ) );

 //...
 // The Loop
 the_title(); ?>
 Welcome to the page called: <?php echo $title[0];  ?>

 <?php
 // Function
 function get_custom_title($title, $dependencies) {
     $title = $dependencies['title'];
     $post = $dependencies['post'];
     $post->ID = $post->ID%100; //not changing the global object
     return $title . ' (Last 2 digits of post ID: ' . $post->ID . ')';
 }
 }}}
 The ''$dependencies'' variable would allow a plugin developer to pass
 filters along.


 What do you think? Good? Bad? Terrible? Would love some feedback.

 Thanks,

 Sean

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


More information about the wp-trac mailing list