[wp-trac] [WordPress Trac] #21170: JavaScript actions and filters

WordPress Trac wp-trac at lists.automattic.com
Fri Jul 6 02:08:42 UTC 2012


#21170: JavaScript actions and filters
----------------------------+--------------------------
 Reporter:  koopersmith     |       Owner:  koopersmith
     Type:  task (blessed)  |      Status:  new
 Priority:  normal          |   Milestone:  3.5
Component:  General         |     Version:  3.4
 Severity:  normal          |  Resolution:
 Keywords:                  |
----------------------------+--------------------------
Changes (by mikeschinkel):

 * cc: mikeschinkel@… (added)


Comment:

 Replying to [ticket:21170 koopersmith]:
 > The concept of adding JavaScript actions and filters has been tossed
 around for some time. We've experimented with various configurations of
 actions in both the fullscreen and customizer APIs, and they've proven
 their utility enough to graduate them to a core feature in their own
 right.

 This is an excellent ticket, really glad to see it.   We've been using
 something very much like this in our client's systems for the past year.
 I've copied the code from plugin below changing the name from our plugin's
 name to `'wp'`; I've also added as an attachment.

 Basically what we did was added a "hooks" object as a `wp` property of the
 jQuery object, i.e. `$.wp`:

 {{{
 /*
   jQuery Hooks for WordPress

   Examples:

   // Add three different test actions
   $.wp.addAction( 'test', function() { alert('Foo!'); } );
   $.wp.addAction( 'test', function() { alert('Bar!'); } );
   $.wp.addAction( 'test', function() { alert('Baz!'); } );

   // Remove the first one
   $.wp.removeAction( 'test', 'test_1' );

   // Do the remaining test actions
   $.wp.doAction( 'test' );


   // Add a filter somewhere
   $.wp.addFilter('filterOptions',function(options) {
     // Do stuff here to modify variable options
     return options;
   } );

   // Use the filter here
   options = $.wp.applyFilters('filterOptions',options);

  */
 jQuery(document).ready(function($) {
   $.wp = {
     /**
      * Implement a WordPress-link Hook System for Javascript
      * TODO: Change 'tag' to 'args', allow number (priority), string
 (tag), object (priority+tag)
      */
     hooks: { action: {}, filter: {} },
     addAction: function( action, callable, tag ) {
       jQuery.wp.addHook( 'action', action, callable, tag );
     },
     addFilter: function( action, callable, tag ) {
       jQuery.wp.addHook( 'filter', action, callable, tag );
     },
     doAction: function( action, args ) {
       jQuery.wp.doHook( 'action', action, null, args );
     },
     applyFilters: function( action, value, args ) {
       return jQuery.wp.doHook( 'filter', action, value, args );
     },
     removeAction: function( action, tag ) {
       jQuery.wp.removeHook( 'action', action, tag );
     },
     removeFilter: function( action, tag ) {
       jQuery.wp.removeHook( 'filter', action, tag );
     },
     addHook: function( hookType, action, callable, tag ) {
       if ( undefined == jQuery.wp.hooks[hookType][action] ) {
         jQuery.wp.hooks[hookType][action] = [];
       }
       var hooks = jQuery.wp.hooks[hookType][action];
       if ( undefined == tag ) {
         tag = action + '_' + hooks.length;
       }
       jQuery.wp.hooks[hookType][action].push( { tag:tag, callable:callable
 } );
     },
     doHook: function( hookType, action, value, args ) {
       if ( undefined != jQuery.wp.hooks[hookType][action] ) {
         var hooks = jQuery.wp.hooks[hookType][action];
         for( var i=0; i<hooks.length; i++) {
           if ( 'action'==hookType ) {
             hooks[i].callable(args);
           } else {
             value = hooks[i].callable(value, args);
           }
         }
       }
       if ( 'filter'==hookType ) {
         return value;
       }
     },
     removeHook: function( hookType, action, tag ) {
       if ( undefined != jQuery.wp.hooks[hookType][action] ) {
         var hooks = jQuery.wp.hooks[hookType][action];
         for( var i=hooks.length-1; i>=0; i--) {
           if (undefined==tag||tag==hooks[i].tag)
             hooks.splice(i,1);
           }
         }
       }
   }
 });
 }}}
 This code has worked really well for us and stood the test of time, though
 I do have a TODO comment in the code to enable priorities.  Maybe it could
 be a base for something in 3.5?

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/21170#comment:11>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list