1 | <?php |
---|
2 | |
---|
3 | class When_Inside_Widgets_And_Sidebars |
---|
4 | { |
---|
5 | public $active_sidebar; |
---|
6 | public $active_widget; |
---|
7 | |
---|
8 | protected $_sidebars; |
---|
9 | protected $_sidebars_orig; |
---|
10 | protected $_widget_callbacks; |
---|
11 | |
---|
12 | public function __construct() |
---|
13 | { |
---|
14 | add_action( 'dynamic_sidebar', array(&$this, 'event_dynamic_sidebar' ) ); |
---|
15 | add_filter( 'sidebars_widgets', array(&$this, 'filter_sidebars_widgets' ) ); |
---|
16 | } |
---|
17 | |
---|
18 | public function event_dynamic_sidebar( $widget ) |
---|
19 | { |
---|
20 | $this->active_widget = $widget['id']; |
---|
21 | foreach( (array) $this->_sidebars as $sidebar_id => $sidebar ) { |
---|
22 | if ( in_array( $widget['id'], $sidebar ) ) { |
---|
23 | $this->active_sidebar = $sidebar_id; |
---|
24 | if ( count( $this->_sidebars[ $sidebar_id ] ) == count( $this->_sidebars_orig[ $sidebar_id ] ) ) { |
---|
25 | var_dump( sprintf( 'Starting sidebar %s!', $sidebar_id ) ); |
---|
26 | } |
---|
27 | unset( $this->_sidebars[ $sidebar_id ][ array_search( $widget['id'], $this->_sidebars[ $sidebar_id ] ) ] ); |
---|
28 | break; |
---|
29 | } |
---|
30 | } |
---|
31 | var_dump( sprintf( 'Starting widget %s!', $widget['id'] ) ); |
---|
32 | } |
---|
33 | |
---|
34 | public function filter_sidebars_widgets( $sidebars ) |
---|
35 | { |
---|
36 | global $wp_registered_widgets; |
---|
37 | |
---|
38 | $this->_sidebars_orig = $this->_sidebars = $sidebars; |
---|
39 | foreach( (array) $wp_registered_widgets as $widget ) { |
---|
40 | if ( isset( $widget['id'] ) && isset( $widget['callback'] ) ) { |
---|
41 | $this->_widget_callbacks[ $widget['id'] ] = $widget['callback']; |
---|
42 | $wp_registered_widgets[ $widget['id'] ]['callback'] = array(&$this, 'callable_wrapper'); |
---|
43 | } |
---|
44 | } |
---|
45 | return $sidebars; |
---|
46 | } |
---|
47 | |
---|
48 | public function callable_wrapper() |
---|
49 | { |
---|
50 | if ( is_callable( $this->_widget_callbacks[ $this->active_widget ] ) ) { |
---|
51 | $params = func_get_args(); |
---|
52 | call_user_func_array( $this->_widget_callbacks[ $this->active_widget ], $params ); |
---|
53 | var_dump( sprintf( 'Ending widget %s!', $this->active_widget ) ); |
---|
54 | |
---|
55 | if ( empty( $this->_sidebars[ $this->active_sidebar ] ) && ! empty( $this->_sidebars_orig[ $this->active_sidebar ] ) ) { |
---|
56 | var_dump( sprintf( 'Ending sidebar %s!', $this->active_sidebar ) ); |
---|
57 | $this->active_sidebar = null; |
---|
58 | } |
---|
59 | $this->active_widget = null; |
---|
60 | } |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | $GLOBALS['inside_widgets_sidebars'] = new When_Inside_Widgets_And_Sidebars; |
---|