Content Aware Sidebars – Unlimited Widget Areas - Version 0.2

Version Description

  • Added: taxonomy terms rules
  • Added: optional description for sidebars
  • Added: display_ca_sidebar also accepts URL-style string as parameter
  • Fixed: saving meta now only kicks in with sidebar types
  • Fixed: archives are not singulars and will not be treated like them
Download this release

Release Info

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

Code changes from version 0.1 to 0.2

Files changed (2) hide show
  1. content-aware-sidebars.php +82 -33
  2. readme.txt +19 -10
content-aware-sidebars.php CHANGED
@@ -6,7 +6,7 @@
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:
@@ -29,7 +29,7 @@ License:
29
  */
30
  class ContentAwareSidebars {
31
 
32
- public $version = 0.1;
33
  public $settings = array();
34
 
35
  /**
@@ -44,6 +44,8 @@ class ContentAwareSidebars {
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'));
@@ -100,7 +102,7 @@ class ContentAwareSidebars {
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')
@@ -114,6 +116,7 @@ class ContentAwareSidebars {
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'),
@@ -134,8 +137,25 @@ class ContentAwareSidebars {
134
  'query_var' => false,
135
  'rewrite' => false,
136
  'menu_position' => null,
137
- 'supports' => array('title','page-attributes'),
 
138
  ));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  }
140
 
141
  /**
@@ -144,10 +164,15 @@ class ContentAwareSidebars {
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>',
@@ -165,6 +190,14 @@ class ContentAwareSidebars {
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',
@@ -179,32 +212,48 @@ class ContentAwareSidebars {
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)
@@ -299,9 +348,9 @@ class ContentAwareSidebars {
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'])) {
@@ -364,15 +413,15 @@ 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,
@@ -420,9 +469,9 @@ function display_ca_sidebar($args = array()) {
420
  }
421
 
422
  if ($host && is_active_sidebar($host)) {
423
- echo $args['before'];
424
  dynamic_sidebar($host);
425
- echo $args['after'];
426
  }
427
 
428
  }
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.2
10
  Author: Joachim Jensen
11
  Author URI: http://www.intox.dk/
12
  License:
29
  */
30
  class ContentAwareSidebars {
31
 
32
+ public $version = 0.2;
33
  public $settings = array();
34
 
35
  /**
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( 'admin_menu', array(&$this,'clear_admin_menu') );
48
+
49
  add_action('save_post', array(&$this,'save_post'));
50
 
51
  register_activation_hook(__FILE__, array(&$this,'upon_activation'));
102
  'merge-pos' => array(
103
  'name' => 'Merge position',
104
  'id' => 'merge-pos',
105
+ 'desc' => 'Place sidebar on top or bottom of host when merging.',
106
  'val' => 1,
107
  'type' => 'select',
108
  'list' => array('Top','Bottom')
116
  *
117
  */
118
  public function init_sidebar_type() {
119
+ global $submenu;
120
  register_post_type('sidebar',array(
121
  'labels' => array(
122
  'name' => _x('Sidebars', 'post type general name'),
137
  'query_var' => false,
138
  'rewrite' => false,
139
  'menu_position' => null,
140
+ 'supports' => array('title','excerpt','page-attributes'),
141
+ 'taxonomies' => get_taxonomies(array('public'=>true))
142
  ));
143
+
144
+
145
+
146
+ }
147
+
148
+ /**
149
+ *
150
+ * Remove taxonomy shortcuts from menu. Gets too cluttered.
151
+ *
152
+ */
153
+ function clear_admin_menu() {
154
+ $taxonomies = get_taxonomies(array('public'=>true));
155
+ foreach($taxonomies as $tax)
156
+ remove_submenu_page('edit.php?post_type=sidebar','edit-tags.php?taxonomy='.$tax.'&amp;post_type=sidebar');
157
+
158
+
159
  }
160
 
161
  /**
164
  *
165
  */
166
  public function create_sidebars() {
167
+ $posts = get_posts(array(
168
+ 'numberposts' => 0,
169
+ 'post_type' => 'sidebar',
170
+ 'post_status' => array('publish','future')
171
+ ));
172
  foreach($posts as $post)
173
  register_sidebar( array(
174
  'name' => $post->post_title,
175
+ 'description' => $post->post_excerpt,
176
  'id' => 'ca-sidebar-'.$post->ID,
177
  'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
178
  'after_widget' => '</li>',
190
  public function replace_sidebar() {
191
  global $wp_query, $post_type, $_wp_sidebars_widgets;
192
 
193
+ // Archives are not supported yet.
194
+ if(!is_singular())
195
+ return;
196
+
197
+ $handled_already = array();
198
+ $content_type = get_post_type();
199
+ $post_terms = get_object_taxonomies($content_type);
200
+
201
  $posts = get_posts(array(
202
  'numberposts' => 0,
203
  'post_type' => 'sidebar',
212
  )
213
  )
214
  ));
 
 
 
 
 
 
215
 
 
216
  foreach($posts as $post) {
217
+
218
+ $continue = 1;
219
  $id = 'ca-sidebar-'.$post->ID;
 
220
 
221
+ // Check if sidebar exists
 
 
 
 
 
 
222
  if (!isset($_wp_sidebars_widgets[$id]))
223
  continue;
224
 
225
+ $post_types = (array) unserialize(get_post_meta($post->ID, 'post_types', true));
226
+
227
+ // Check if current post type is part of rules
228
+ if(in_array($content_type,$post_types)) {
229
+ $continue--;
230
+ // Check if post has any taxonomies at all
231
+ } elseif($post_terms) {
232
+
233
+ $sorted_terms = array();
234
+ $post_terms = wp_get_object_terms(get_the_ID(),$post_terms);
235
+
236
+ //Grab posts terms and split them in taxonomies
237
+ foreach($post_terms as $term)
238
+ $sorted_terms[$term->taxonomy][] = $term->slug;
239
+
240
+ //Check if any of current terms is part of rules
241
+ foreach($sorted_terms as $taxonomy => $terms) {
242
+ if(has_term($terms,$taxonomy,$post->ID)) {
243
+ $continue--;
244
+ break;
245
+ }
246
+ }
247
+ }
248
+
249
+ // Final check
250
+ if($continue)
251
+ continue;
252
+
253
+ $host = get_post_meta($post->ID, 'host', true);
254
  $handle = get_post_meta($post->ID, 'handle', true);
255
 
256
+ // If host has already been replaced, merge with it instead. Might change in future.
257
  if($handle || isset($handled_already[$host])) {
258
  $merge_pos = get_post_meta($post->ID, 'merge-pos', true);
259
  if($merge_pos)
348
  *
349
  */
350
  public function save_post($post_id) {
 
 
351
 
352
+ if(get_post_type($post_id) != 'sidebar')
353
+ return $post_id;
354
 
355
  // Save button pressed
356
  if(!isset($_POST['original_publish'])) {
413
  $ca_sidebars = new ContentAwareSidebars();
414
 
415
  // Template function
416
+ function display_ca_sidebar($args) {
417
  global $wp_query, $post_type, $_wp_sidebars_widgets;
418
 
419
+ $defaults = array (
420
+ 'before' => '<div id="sidebar" class="widget-area"><ul class="xoxo">',
421
+ 'after' => '</ul></div>'
422
+ );
423
+ $args = wp_parse_args($args,$defaults);
424
+ extract( $args, EXTR_SKIP );
425
 
426
  $posts = get_posts(array(
427
  'numberposts' => 0,
469
  }
470
 
471
  if ($host && is_active_sidebar($host)) {
472
+ echo $before;
473
  dynamic_sidebar($host);
474
+ echo $after;
475
  }
476
 
477
  }
readme.txt CHANGED
@@ -1,10 +1,10 @@
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
 
@@ -15,14 +15,15 @@ Manage an infinite number of sidebars. Each with different rules for which conte
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
 
@@ -30,10 +31,10 @@ If you have any suggestions, please send me a mail at jv@intox.dk.
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
 
@@ -43,13 +44,21 @@ 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
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, taxonomy, term
5
  Requires at least: 3.0
6
  Tested up to: 3.2.1
7
+ Stable tag: 0.2
8
 
9
  Manage and show sidebars according to the content being viewed.
10
 
15
  Current features include:
16
 
17
  * Show sidebars with specific post types only
18
+ * Show sidebars with posts containing specific taxonomy terms only
19
  * Merge new sidebars with others, replace them or simply add them to your theme manually
20
  * Create complex content with nested sidebars
21
  * Standard Custom Post Type features (status, visibility, publish date)
22
 
23
  Upcoming features:
24
 
 
25
  * Show sidebars in a time span only
26
+ * Show sidebars with (taxonomy term and post type) archives
27
 
28
  If you have any suggestions, please send me a mail at jv@intox.dk.
29
 
31
 
32
  Do as follows
33
 
34
+ 1. Upload the full plugin directory to your `/wp-content/plugins/` directory or install the plugin through `Plugins` in the administration
35
+ 1. Activate the plugin through `Plugins` in the administration
36
  1. Have fun creating your first sidebar
37
+ 1. Optional: Insert `<?php display_ca_sidebar(); ?>` in a template if you have a special spot for the new, manual handled, sidebars.
38
 
39
  == Frequently Asked Questions ==
40
 
44
 
45
  == Screenshots ==
46
 
47
+ 1. Add a new sidebar `For Pages` visible on all pages, replacing `Main Sidebar`
48
+ 2. Add widgets to `For Pages`
49
+ 3. Viewing home page. `Main Sidebar` is visible
50
+ 4. Viewing a page. `For Pages` has replaced `Main Sidebar`
51
 
52
  == Changelog ==
53
 
54
+ = 0.2 =
55
+
56
+ * Added: taxonomy terms rules
57
+ * Added: optional description for sidebars
58
+ * Added: display_ca_sidebar also accepts URL-style string as parameter
59
+ * Fixed: saving meta now only kicks in with sidebar types
60
+ * Fixed: archives are not singulars and will not be treated like them
61
+
62
  = 0.1 =
63
 
64
  * First stable release