Content Aware Sidebars – Unlimited Widget Areas - Version 0.1

Version Description

  • First stable release

=

Download this release

Release Info

Developer intoxstudio
Plugin Icon 128x128 Content Aware Sidebars – Unlimited Widget Areas
Version 0.1
Comparing to
See all releases

Version 0.1

content-aware-sidebars.php ADDED
@@ -0,0 +1,429 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package Content Aware Sidebars
4
+ */
5
+ /*
6
+ Plugin Name: Content Aware Sidebars
7
+ Plugin URI: http://www.intox.dk/
8
+ Description: Manage and show sidebars according to the content being viewed.
9
+ Version: 0.1
10
+ Author: Joachim Jensen
11
+ Author URI: http://www.intox.dk/
12
+ License:
13
+
14
+ Copyright 2011 Joachim Jensen (email : jv@intox.dk)
15
+
16
+ This program is free software; you can redistribute it and/or modify
17
+ it under the terms of the GNU General Public License, version 2, as
18
+ published by the Free Software Foundation.
19
+
20
+ This program is distributed in the hope that it will be useful,
21
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
+ GNU General Public License for more details.
24
+
25
+ You should have received a copy of the GNU General Public License
26
+ along with this program; if not, write to the Free Software
27
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28
+
29
+ */
30
+ class ContentAwareSidebars {
31
+
32
+ public $version = 0.1;
33
+ public $settings = array();
34
+
35
+ /**
36
+ *
37
+ * Constructor
38
+ *
39
+ */
40
+ public function __construct() {
41
+
42
+ add_filter('wp', array(&$this,'replace_sidebar'));
43
+ add_action('init', array(&$this,'init_sidebar_type'));
44
+ add_action('widgets_init', array(&$this,'create_sidebars'));
45
+ add_action('admin_init', array(&$this,'create_meta_boxes'));
46
+ add_action('admin_head', array(&$this,'init_settings'));
47
+ add_action('save_post', array(&$this,'save_post'));
48
+
49
+ register_activation_hook(__FILE__, array(&$this,'upon_activation'));
50
+
51
+ }
52
+
53
+ /**
54
+ *
55
+ * Create post meta fields
56
+ * Loaded in admin_head due to $post. Should be loaded even later if possible.
57
+ *
58
+ */
59
+ public function init_settings() {
60
+ global $post, $wp_registered_sidebars;
61
+
62
+ // List of sidebars
63
+ $sidebar_list = array();
64
+ foreach($wp_registered_sidebars as $sidebar) {
65
+ if(isset($post) && $sidebar['id'] != 'ca-sidebar-'.$post->ID)
66
+ $sidebar_list[$sidebar['id']] = $sidebar['name'];
67
+ }
68
+
69
+ // List of public post types
70
+ $post_type_list = array();
71
+ foreach(get_post_types(array('public'=>true),'objects') as $post_type)
72
+ $post_type_list[$post_type->name] = $post_type->label;
73
+
74
+ // Meta fields
75
+ $this->settings = array(
76
+ 'post_types' => array(
77
+ 'name' => 'Post Types',
78
+ 'id' => 'post_types',
79
+ 'desc' => '',
80
+ 'val' => '',
81
+ 'type' => 'select-multi',
82
+ 'list' => $post_type_list
83
+ ),
84
+ 'handle' => array(
85
+ 'name' => 'Handle',
86
+ 'id' => 'handle',
87
+ 'desc' => 'Replace host sidebar, merge with it or add sidebar manually.',
88
+ 'val' => 0,
89
+ 'type' => 'select',
90
+ 'list' => array('Replace','Merge','Manual')
91
+ ),
92
+ 'host' => array(
93
+ 'name' => 'Host Sidebar',
94
+ 'id' => 'host',
95
+ 'desc' => 'The sidebar that should be handled with. Nesting is possible. Manual handling makes this option superfluous.',
96
+ 'val' => 'sidebar-1',
97
+ 'type' => 'select',
98
+ 'list' => $sidebar_list
99
+ ),
100
+ 'merge-pos' => array(
101
+ 'name' => 'Merge position',
102
+ 'id' => 'merge-pos',
103
+ 'desc' => 'Place sidebar on top or bottom of host when merging. Merging can happen with all handlings.',
104
+ 'val' => 1,
105
+ 'type' => 'select',
106
+ 'list' => array('Top','Bottom')
107
+ )
108
+ );
109
+ }
110
+
111
+ /**
112
+ *
113
+ * Custom Post Type: Sidebar
114
+ *
115
+ */
116
+ public function init_sidebar_type() {
117
+ register_post_type('sidebar',array(
118
+ 'labels' => array(
119
+ 'name' => _x('Sidebars', 'post type general name'),
120
+ 'singular_name' => _x('Sidebar', 'post type singular name'),
121
+ 'add_new' => _x('Add New', 'sidebar'),
122
+ 'add_new_item' => __('Add New Sidebar'),
123
+ 'edit_item' => __('Edit Sidebar'),
124
+ 'new_item' => __('New Sidebar'),
125
+ 'all_items' => __('All Sidebars'),
126
+ 'view_item' => __('View Sidebar'),
127
+ 'search_items' => __('Search Sidebars'),
128
+ 'not_found' => __('No sidebars found'),
129
+ 'not_found_in_trash' => __('No sidebars found in Trash'),
130
+ 'parent_item_colon' => '',
131
+ 'menu_name' => 'Sidebars'
132
+ ),
133
+ 'show_ui' => true,
134
+ 'query_var' => false,
135
+ 'rewrite' => false,
136
+ 'menu_position' => null,
137
+ 'supports' => array('title','page-attributes'),
138
+ ));
139
+ }
140
+
141
+ /**
142
+ *
143
+ * Create sidebars from content types
144
+ *
145
+ */
146
+ public function create_sidebars() {
147
+ $posts = get_posts(array( 'numberposts' => 0, 'post_type'=> 'sidebar'));
148
+ foreach($posts as $post)
149
+ register_sidebar( array(
150
+ 'name' => $post->post_title,
151
+ 'id' => 'ca-sidebar-'.$post->ID,
152
+ 'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
153
+ 'after_widget' => '</li>',
154
+ 'before_title' => '<h3 class="widget-title">',
155
+ 'after_title' => '</h3>',
156
+ ));
157
+ }
158
+
159
+ /**
160
+ *
161
+ * Replace a sidebar with content aware sidebars
162
+ * Handles: replace, merge.
163
+ *
164
+ */
165
+ public function replace_sidebar() {
166
+ global $wp_query, $post_type, $_wp_sidebars_widgets;
167
+
168
+ $posts = get_posts(array(
169
+ 'numberposts' => 0,
170
+ 'post_type' => 'sidebar',
171
+ 'orderby' => 'menu_order meta_value',
172
+ 'meta_key' => 'handle',
173
+ 'order' => 'ASC',
174
+ 'meta_query' => array(
175
+ array(
176
+ 'key' => 'handle',
177
+ 'value' => '2',
178
+ 'compare' => '!='
179
+ )
180
+ )
181
+ ));
182
+ $handled_already = array();
183
+
184
+ // Variable and function are not always equal.
185
+ $content_type = $post_type;
186
+ if(!$content_type)
187
+ $content_type = get_post_type();
188
+
189
+ // Only grab meta when needed.
190
+ foreach($posts as $post) {
191
+
192
+ $id = 'ca-sidebar-'.$post->ID;
193
+ $post_types = (array) unserialize(get_post_meta($post->ID, 'post_types', true));
194
+
195
+ // Check if current post type is part of post type rules
196
+ if(!in_array($content_type,$post_types))
197
+ continue;
198
+
199
+ $host = get_post_meta($post->ID, 'host', true);
200
+
201
+ // Check if sidebar or host exists
202
+ if (!isset($_wp_sidebars_widgets[$id]))
203
+ continue;
204
+
205
+ $handle = get_post_meta($post->ID, 'handle', true);
206
+
207
+ // If host has already been replaced, merge with it instead.
208
+ if($handle || isset($handled_already[$host])) {
209
+ $merge_pos = get_post_meta($post->ID, 'merge-pos', true);
210
+ if($merge_pos)
211
+ $_wp_sidebars_widgets[$host] = array_merge($_wp_sidebars_widgets[$host],$_wp_sidebars_widgets[$id]);
212
+ else
213
+ $_wp_sidebars_widgets[$host] = array_merge($_wp_sidebars_widgets[$id],$_wp_sidebars_widgets[$host]);
214
+ } else {
215
+ $_wp_sidebars_widgets[$host] = $_wp_sidebars_widgets[$id];
216
+ $handled_already[$host] = 1;
217
+ }
218
+ }
219
+ }
220
+
221
+ /**
222
+ *
223
+ * Meta boxes for edit post
224
+ *
225
+ */
226
+ public function create_meta_boxes() {
227
+ global $post;
228
+
229
+ add_meta_box(
230
+ 'ca-sidebar',
231
+ 'Options',
232
+ array(&$this,'meta_box_content'),
233
+ 'sidebar',
234
+ 'normal',
235
+ 'high'
236
+ );
237
+
238
+ }
239
+
240
+ public function meta_box_content() {
241
+
242
+ $this->form_fields(array('post_types','handle','merge-pos','host'));
243
+
244
+ }
245
+
246
+ /**
247
+ *
248
+ * Create form fields
249
+ *
250
+ */
251
+ private function form_fields($array) {
252
+ global $post;
253
+
254
+ // Use nonce for verification
255
+ wp_nonce_field(basename(__FILE__),'_ca-sidebar-nonce');
256
+ ?>
257
+ <table class="form-table">
258
+ <?php
259
+ $array = array_intersect_key($this->settings,array_flip($array));
260
+ foreach($array as $setting) :
261
+
262
+ $meta = get_post_meta($post->ID, $setting['id'], true);
263
+ $current = $meta != '' ? $meta : $setting['val'];
264
+ ?>
265
+ <tr valign="top">
266
+ <th scope="row"><?php echo $setting['name'] ?></th>
267
+ <td>
268
+ <?php switch($setting['type']) :
269
+ case 'select' :
270
+ echo '<select style="width:200px;" name="'.$setting['id'].'">'."\n";
271
+ foreach($setting['list'] as $key => $value) {
272
+ echo '<option value="'.$key.'"'.($key == $current ? ' selected="selected"' : '').'>'.$value.'</option>'."\n";
273
+ }
274
+ echo '</select>'."\n";
275
+ break;
276
+ case 'select-multi' :
277
+ echo '<select multiple="multiple" size="5" style="width:200px;height:60px;" name="'.$setting['id'].'[]">'."\n";
278
+ foreach($setting['list'] as $key => $value) {
279
+ echo '<option value="'.$key.'"'.(in_array($key,unserialize($current)) ? ' selected="selected"' : '').'>'.$value.'</option>'."\n";
280
+ }
281
+ echo '</select>'."\n";
282
+ break;
283
+ case 'text' :
284
+ default :
285
+ echo '<input style="width:200px;" type="text" name="'.$setting['id'].'" value="'.implode(",",unserialize($current)).'" />'."\n";
286
+ break;
287
+ endswitch; ?>
288
+ <br /><span class="description"><?php echo $setting['desc'] ?></span>
289
+ </td>
290
+ </tr>
291
+ <?php endforeach; ?>
292
+ </table>
293
+ <?php
294
+ }
295
+
296
+ /**
297
+ *
298
+ * Save meta values for post
299
+ *
300
+ */
301
+ public function save_post($post_id) {
302
+ global $post;
303
+
304
+
305
+
306
+ // Save button pressed
307
+ if(!isset($_POST['original_publish'])) {
308
+ return $post_id;
309
+ }
310
+
311
+ // Verify nonce
312
+ if (!check_admin_referer(basename(__FILE__),'_ca-sidebar-nonce')) {
313
+ return $post_id;
314
+ }
315
+
316
+ // Check permissions
317
+ if (!current_user_can('edit_post', $post_id)) {
318
+ return $post_id;
319
+ }
320
+
321
+ // Check autosave
322
+ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
323
+ return $post_id;
324
+ }
325
+
326
+ // Load settings manually here. This ought to be done with action/filter
327
+ $this->init_settings();
328
+
329
+ // Update values
330
+ foreach ($this->settings as $field) {
331
+ $old = get_post_meta($post_id, $field['id'], true);
332
+ $new = $_POST[$field['id']];
333
+
334
+ switch($field['id']) {
335
+ case 'post_types' :
336
+ $new = serialize($new);
337
+ break;
338
+ default :
339
+ break;
340
+ }
341
+
342
+ if ($new != '' && $new != $old) {
343
+ update_post_meta($post_id, $field['id'], $new);
344
+ }elseif ($new == '' && $old != '') {
345
+ delete_post_meta($post_id, $field['id'], $old);
346
+ }
347
+ }
348
+ }
349
+
350
+ /**
351
+ *
352
+ * Flush rewrite rules on plugin activation
353
+ *
354
+ */
355
+ public function upon_activation() {
356
+ $this->init_sidebar_type();
357
+ flush_rewrite_rules();
358
+ }
359
+
360
+ }
361
+
362
+ // Launch plugin
363
+ global $ca_sidebars;
364
+ $ca_sidebars = new ContentAwareSidebars();
365
+
366
+ // Template function
367
+ function display_ca_sidebar($args = array()) {
368
+ global $wp_query, $post_type, $_wp_sidebars_widgets;
369
+
370
+ if(empty($args)) {
371
+ $args = array(
372
+ 'before' => '<div id="sidebar" class="widget-area"><ul class="xoxo">',
373
+ 'after' => '</ul></div>'
374
+ );
375
+ }
376
+
377
+ $posts = get_posts(array(
378
+ 'numberposts' => 0,
379
+ 'post_type' => 'sidebar',
380
+ 'orderby' => 'menu_order meta_value',
381
+ 'meta_key' => 'handle',
382
+ 'order' => 'ASC',
383
+ 'meta_query' => array(
384
+ array(
385
+ 'key' => 'handle',
386
+ 'value' => '2',
387
+ 'compare' => '=='
388
+ )
389
+ )
390
+ ));
391
+ $content_type = $post_type;
392
+ if(!$content_type)
393
+ $content_type = get_post_type();
394
+
395
+ $i = $host = 0;
396
+ foreach($posts as $post) {
397
+
398
+ $id = 'ca-sidebar-'.$post->ID;
399
+ $post_types = (array) unserialize(get_post_meta($post->ID, 'post_types', true));
400
+
401
+ // Check if current post type is part of rules
402
+ if(!in_array($content_type,$post_types))
403
+ continue;
404
+
405
+ if($i > 0) {
406
+
407
+ // Check if sidebar is active
408
+ if (!isset($_wp_sidebars_widgets[$id]))
409
+ continue;
410
+
411
+ $merge_pos = get_post_meta($post->ID, 'merge-pos', true);
412
+ if($merge_pos)
413
+ $_wp_sidebars_widgets[$host] = array_merge($_wp_sidebars_widgets[$host],$_wp_sidebars_widgets[$id]);
414
+ else
415
+ $_wp_sidebars_widgets[$host] = array_merge($_wp_sidebars_widgets[$id],$_wp_sidebars_widgets[$host]);
416
+ } else {
417
+ $host = $id;
418
+ }
419
+ $i++;
420
+ }
421
+
422
+ if ($host && is_active_sidebar($host)) {
423
+ echo $args['before'];
424
+ dynamic_sidebar($host);
425
+ echo $args['after'];
426
+ }
427
+
428
+ }
429
+
readme.txt ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Plugin Name ===
2
+ Contributors: intoxstudio
3
+ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=KPZHE6A72LEN4&lc=US&item_name=WordPress%20Plugin%3a%20Content%20Aware%20Sidebars&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted
4
+ Tags: sidebar, widget, content aware, post type
5
+ Requires at least: 3.0
6
+ Tested up to: 3.2.1
7
+ Stable tag: 0.1
8
+
9
+ Manage and show sidebars according to the content being viewed.
10
+
11
+ == Description ==
12
+
13
+ Manage an infinite number of sidebars. Each with different rules for which content they should be visible with. Creating flexible sidebars has never been easier, and no code is needed at all as everything is done in a simple GUI.
14
+
15
+ Current features include:
16
+
17
+ * Show sidebars with specific post types only
18
+ * Merge new sidebars with others, replace them or simply add them to your theme manually
19
+ * Create complex content with nested sidebars
20
+ * Standard Custom Post Type features (status, visibility, publish date)
21
+
22
+ Upcoming features:
23
+
24
+ * Show sidebars with specific taxonomies
25
+ * Show sidebars in a time span only
26
+
27
+ If you have any suggestions, please send me a mail at jv@intox.dk.
28
+
29
+ == Installation ==
30
+
31
+ Do as follows
32
+
33
+ 1. Upload the full plugin directory to your '/wp-content/plugins/' directory or install the plugin through 'Plugins' in the administration
34
+ 1. Activate the plugin through 'Plugins' in the administration
35
+ 1. Have fun creating your first sidebar
36
+ 1. Optional: Insert '<?php display_ca_sidebar(); ?>' in a template if you have a special spot for the new, manual handled, sidebars.
37
+
38
+ == Frequently Asked Questions ==
39
+
40
+ = Who's great? =
41
+
42
+ You are.
43
+
44
+ == Screenshots ==
45
+
46
+ 1. Add a new sidebar 'For Pages' visible on all pages, replacing 'Main Sidebar'
47
+ 2. Add widgets to 'For Pages'
48
+ 3. Viewing home page. 'Main Sidebar' is visible
49
+ 4. Viewing a page. 'For Pages' has replaced 'Main Sidebar'
50
+
51
+ == Changelog ==
52
+
53
+ = 0.1 =
54
+
55
+ * First stable release
56
+
57
+ == Upgrade Notice ==
58
+
59
+ = 0.1 =
60
+
61
+ * Hello World
62
+
63
+ == Translations ==
64
+
65
+ None yet. Might come in the future. Do you want to contribute? Feel free to contact me at jv@intox.dk
screenshot-1.png ADDED
Binary file
screenshot-2.png ADDED
Binary file
screenshot-3.png ADDED
Binary file
screenshot-4.png ADDED
Binary file