[wp-trac] [WordPress Trac] #46635: Improve identifying of non–trivial callbacks in hooks

WordPress Trac noreply at wordpress.org
Wed Jul 22 15:59:14 UTC 2020


#46635: Improve identifying of non–trivial callbacks in hooks
-------------------------+-------------------------------
 Reporter:  Rarst        |       Owner:  (none)
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  General      |     Version:
 Severity:  normal       |  Resolution:
 Keywords:               |     Focuses:  coding-standards
-------------------------+-------------------------------

Comment (by jason_the_adams):

 Just wanted to chime in here from our perspective (GiveWP). We're building
 a Service Container into our next version for automatic dependency
 injection. We wanted to have a way where we weren't having to instantiate
 every hookable class ''just in case'' the hook fires. This is an
 unnecessary memory expenditure.

 The way we came up with to get around this issue was to use a Closure in
 the hook and instantiate the class ''if and when'' it's fired. This works
 nicely, but we ended up needing to include an ugly workaround to provide a
 way for folks to effectively "remove" the hook:

 {{{#!php
 <?php
         public static function addAction( $tag, $class, $method =
 '__invoke', $priority = 10, $acceptedArgs = 1 ) {
                 if ( ! method_exists( $class, $method ) ) {
                         throw new InvalidArgumentException( "The method
 $method does not exist on $class" );
                 }

                 add_action(
                         $tag,
                         static function () use ( $tag, $class, $method ) {
                                 // Provide a way of disabling the hook
                                 if ( apply_filters(
 "give_disable_hook-{$tag}", false ) || apply_filters(
 "give_disable_hook-{$tag}:{$class}@{$method}", false ) ) {
                                         return;
                                 }

                                 $instance = give( $class );

                                 call_user_func_array( [ $instance, $method
 ], func_get_args() );
                         },
                         $priority,
                         $acceptedArgs
                 );
         }
 }}}

 Looking at @schlessera's solution, I believe that would work great for us.
 At first I wished that WP had a way of instantiating as needed itself, but
 I now realize that would be more than necessary. Simply having a way of
 registering a removable Closure would work fine.

 I agree that the suggestion in
 [https://core.trac.wordpress.org/ticket/46635#comment:17 comment 17] is
 the best and will work for all callables (which includes Closures and
 class methods).

 Thanks for all the great discussion. Hope to see this move forward!

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/46635#comment:26>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list