1 | /* |
---|
2 | jQuery Hooks for WordPress |
---|
3 | |
---|
4 | Examples: |
---|
5 | |
---|
6 | // Add three different test actions |
---|
7 | $.wp.addAction( 'test', function() { alert('Foo!'); } ); |
---|
8 | $.wp.addAction( 'test', function() { alert('Bar!'); } ); |
---|
9 | $.wp.addAction( 'test', function() { alert('Baz!'); } ); |
---|
10 | |
---|
11 | // Remove the first one |
---|
12 | $.wp.removeAction( 'test', 'test_1' ); |
---|
13 | |
---|
14 | // Do the remaining test actions |
---|
15 | $.wp.doAction( 'test' ); |
---|
16 | |
---|
17 | |
---|
18 | // Add a filter somewhere |
---|
19 | $.wp.addFilter('filterOptions',function(options) { |
---|
20 | // Do stuff here to modify variable options |
---|
21 | return options; |
---|
22 | } ); |
---|
23 | |
---|
24 | // Use the filter here |
---|
25 | options = $.wp.applyFilters('filterOptions',options); |
---|
26 | |
---|
27 | */ |
---|
28 | jQuery(document).ready(function($) { |
---|
29 | $.wp = { |
---|
30 | /** |
---|
31 | * Implement a WordPress-link Hook System for Javascript |
---|
32 | * TODO: Change 'tag' to 'args', allow number (priority), string (tag), object (priority+tag) |
---|
33 | */ |
---|
34 | hooks: { action: {}, filter: {} }, |
---|
35 | addAction: function( action, callable, tag ) { |
---|
36 | jQuery.wp.addHook( 'action', action, callable, tag ); |
---|
37 | }, |
---|
38 | addFilter: function( action, callable, tag ) { |
---|
39 | jQuery.wp.addHook( 'filter', action, callable, tag ); |
---|
40 | }, |
---|
41 | doAction: function( action, args ) { |
---|
42 | jQuery.wp.doHook( 'action', action, null, args ); |
---|
43 | }, |
---|
44 | applyFilters: function( action, value, args ) { |
---|
45 | return jQuery.wp.doHook( 'filter', action, value, args ); |
---|
46 | }, |
---|
47 | removeAction: function( action, tag ) { |
---|
48 | jQuery.wp.removeHook( 'action', action, tag ); |
---|
49 | }, |
---|
50 | removeFilter: function( action, tag ) { |
---|
51 | jQuery.wp.removeHook( 'filter', action, tag ); |
---|
52 | }, |
---|
53 | addHook: function( hookType, action, callable, tag ) { |
---|
54 | if ( undefined == jQuery.wp.hooks[hookType][action] ) { |
---|
55 | jQuery.wp.hooks[hookType][action] = []; |
---|
56 | } |
---|
57 | var hooks = jQuery.wp.hooks[hookType][action]; |
---|
58 | if ( undefined == tag ) { |
---|
59 | tag = action + '_' + hooks.length; |
---|
60 | } |
---|
61 | jQuery.wp.hooks[hookType][action].push( { tag:tag, callable:callable } ); |
---|
62 | }, |
---|
63 | doHook: function( hookType, action, value, args ) { |
---|
64 | if ( undefined != jQuery.wp.hooks[hookType][action] ) { |
---|
65 | var hooks = jQuery.wp.hooks[hookType][action]; |
---|
66 | for( var i=0; i<hooks.length; i++) { |
---|
67 | if ( 'action'==hookType ) { |
---|
68 | hooks[i].callable(args); |
---|
69 | } else { |
---|
70 | value = hooks[i].callable(value, args); |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | if ( 'filter'==hookType ) { |
---|
75 | return value; |
---|
76 | } |
---|
77 | }, |
---|
78 | removeHook: function( hookType, action, tag ) { |
---|
79 | if ( undefined != jQuery.wp.hooks[hookType][action] ) { |
---|
80 | var hooks = jQuery.wp.hooks[hookType][action]; |
---|
81 | for( var i=hooks.length-1; i>=0; i--) { |
---|
82 | if (undefined==tag||tag==hooks[i].tag) |
---|
83 | hooks.splice(i,1); |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | }); |
---|
89 | |
---|
90 | |
---|
91 | |
---|
92 | |
---|