1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
This program is free software; you can redistribute it and/or |
4
|
|
|
modify it under the terms of the GNU General Public License |
5
|
|
|
as published by the Free Software Foundation; either version 2 |
6
|
|
|
of the License, or (at your option) any later version. |
7
|
|
|
|
8
|
|
|
This program is distributed in the hope that it will be useful, |
9
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
10
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11
|
|
|
GNU General Public License for more details. |
12
|
|
|
|
13
|
|
|
You should have received a copy of the GNU General Public License |
14
|
|
|
along with this program; if not, write to the Free Software |
15
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Custom_Sidebars |
20
|
|
|
*/ |
21
|
|
|
class Custom_Sidebars { |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Custom_Sidebars_Details |
25
|
|
|
*/ |
26
|
|
|
private $custom_sidebar_details; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The default sidebar id |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
* @access private |
33
|
|
|
* @static |
34
|
|
|
*/ |
35
|
|
|
private static $default_sidebar_id; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The class constructor |
39
|
|
|
*/ |
40
|
|
|
public function __construct() { |
41
|
|
|
|
42
|
|
|
add_action( 'init', array( $this, 'init' ) ); |
43
|
|
|
add_action( 'admin_init', array( $this, 'admin_init' ) ); |
44
|
|
|
add_filter( 'sidebars_widgets', array( $this, 'sidebars_widgets' ) ); |
45
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Instantiate the custom sidebars metabox class |
50
|
|
|
* |
51
|
|
|
* This method is hooked to the WP admin_init action |
52
|
|
|
*/ |
53
|
|
|
public function admin_init() { |
54
|
|
|
|
55
|
|
|
$this->custom_sidebar_details = new Custom_Sidebars_Details(); |
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The WP admin_init action callback |
61
|
|
|
* |
62
|
|
|
* Get all pages and posts and add sidebars for each individual post |
63
|
|
|
*/ |
64
|
|
|
public function init() { |
65
|
|
|
|
66
|
|
|
$posts = array(); |
|
|
|
|
67
|
|
|
|
68
|
|
|
if ( ! $posts = wp_cache_get( 'sidebars', 'custom_sidebars' ) ) { |
69
|
|
|
$args = array( |
70
|
|
|
'post_type' => Custom_Sidebars::get_post_types(), |
71
|
|
|
'meta_query' => array( |
72
|
|
|
array( |
73
|
|
|
'key' => '_custom_sidebar', |
74
|
|
|
'value' => true, |
75
|
|
|
) |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$query = new WP_Query( $args ); |
80
|
|
|
|
81
|
|
|
if ( $query->have_posts() ) { |
82
|
|
|
$posts = $query->posts; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
wp_cache_set( 'sidebars', 'custom_sidebars' ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
foreach ( $posts as $post ) { |
89
|
|
|
$args = apply_filters( 'custom_sidebar_args', array( |
90
|
|
|
'name' => sprintf( __( 'Sidebar: %s', 'custom_sidebars' ), $post->post_title ), |
91
|
|
|
'id' => $this->get_sidebar_id( $post->ID ), |
92
|
|
|
'description' => sprintf( __( 'This sidebar will appear on the %s single page.', 'cd-sidebar' ), $post->post_title ), |
93
|
|
|
'class' => '', |
94
|
|
|
'before_widget' => '<aside id="%1$s" class="widget %2$s">', |
95
|
|
|
'after_widget' => '</aside>', |
96
|
|
|
'before_title' => '<h2 class="widget-title">', |
97
|
|
|
'after_title' => '</h2>', |
98
|
|
|
) ); |
99
|
|
|
|
100
|
|
|
register_sidebar( $args ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the sidebar id for a specific post |
107
|
|
|
* |
108
|
|
|
* @param int|null The WP post id |
109
|
|
|
* @return string The sidebar id |
110
|
|
|
*/ |
111
|
|
|
public function get_sidebar_id( $post_id = null ) { |
112
|
|
|
|
113
|
|
|
$post = get_post( $post_id ); |
114
|
|
|
|
115
|
|
|
return "custom-sidebar-{$post->ID}"; |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the post types for which custom sidebars should be registered |
121
|
|
|
*/ |
122
|
|
|
public static function get_post_types() { |
123
|
|
|
|
124
|
|
|
return apply_filters( 'custom_sidebar_post_types', array_values( get_post_types() ) ); |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the sidebar_id assigned to a specific post |
130
|
|
|
*/ |
131
|
|
|
public static function get_sidebar( $post_id = null ) { |
132
|
|
|
|
133
|
|
|
if ( empty( $post_id ) ) { |
134
|
|
|
$post_id = get_post()->ID; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$sidebar = get_post_meta( $post_id, '_custom_sidebar_id', true ); |
138
|
|
|
|
139
|
|
|
if ( $sidebar == false && isset( self::$default_sidebar_id ) ) { |
140
|
|
|
$sidebar = self::$default_sidebar_id; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $sidebar; |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Register a default sidebar to use when a post has no specific sidebar assigned |
149
|
|
|
*/ |
150
|
|
|
public static function register_default_sidebar( $sidebar_id ) { |
151
|
|
|
|
152
|
|
|
self::$default_sidebar_id = $sidebar_id; |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Filter the sidebar widgets |
158
|
|
|
* |
159
|
|
|
* This method is hooked to the WP filter sidebars_widgets. |
160
|
|
|
* |
161
|
|
|
* Since we have no way to know what sidebar is currently being called, this method |
162
|
|
|
* replaces the array of widgets for ALL registered sidebars with the widgets |
163
|
|
|
* assigned to the sidebar specified for use on this particular post/page. This is a |
164
|
|
|
* carpet-bomb approach that should be more specific, but current limitiations in the |
165
|
|
|
* WP infrastructure require it to be done this way. |
166
|
|
|
* |
167
|
|
|
* @param array $sidebar_widgets |
168
|
|
|
* @return array $sidebar_widgets The filtered widget array |
169
|
|
|
* @since 0.3 |
170
|
|
|
*/ |
171
|
|
|
public function sidebars_widgets( $sidebar_widgets ) { |
172
|
|
|
|
173
|
|
|
if ( ! is_admin() && ! empty( $this->get_sidebar() ) ) { |
174
|
|
|
foreach ( $sidebar_widgets as $key => $widgets ) { |
175
|
|
|
$sidebar_widgets[ $key ] = $sidebar_widgets[ $this->get_sidebar() ]; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $sidebar_widgets; |
180
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.