Content Aware Sidebars – Unlimited Widget Areas - Version 0.3

Version Description

  • Added: Sidebars can now be private
  • Fixed: taxonomy terms are now supported by template function
  • Fixed: faster rule recognition and handling
  • Fixed: custom taxonomies are now supported properly
  • Fixed: error if several sidebars had taxonomy terms rules
Download this release

Release Info

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

Code changes from version 0.2 to 0.3

Files changed (2) hide show
  1. content-aware-sidebars.php +124 -135
  2. readme.txt +14 -5
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.2
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.2;
33
  public $settings = array();
34
 
35
  /**
@@ -40,12 +40,11 @@ class ContentAwareSidebars {
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( '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'));
@@ -154,8 +153,6 @@ class ContentAwareSidebars {
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.'&post_type=sidebar');
157
-
158
-
159
  }
160
 
161
  /**
@@ -167,7 +164,7 @@ class ContentAwareSidebars {
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(
@@ -180,7 +177,7 @@ class ContentAwareSidebars {
180
  'after_title' => '</h3>',
181
  ));
182
  }
183
-
184
  /**
185
  *
186
  * Replace a sidebar with content aware sidebars
@@ -188,83 +185,100 @@ class ContentAwareSidebars {
188
  *
189
  */
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',
204
- 'orderby' => 'menu_order meta_value',
205
- 'meta_key' => 'handle',
206
- 'order' => 'ASC',
207
- 'meta_query' => array(
208
- array(
209
- 'key' => 'handle',
210
- 'value' => '2',
211
- 'compare' => '!='
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)
260
- $_wp_sidebars_widgets[$host] = array_merge($_wp_sidebars_widgets[$host],$_wp_sidebars_widgets[$id]);
261
- else
262
- $_wp_sidebars_widgets[$host] = array_merge($_wp_sidebars_widgets[$id],$_wp_sidebars_widgets[$host]);
263
- } else {
264
- $_wp_sidebars_widgets[$host] = $_wp_sidebars_widgets[$id];
265
- $handled_already[$host] = 1;
266
- }
267
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
  }
269
 
270
  /**
@@ -272,9 +286,7 @@ class ContentAwareSidebars {
272
  * Meta boxes for edit post
273
  *
274
  */
275
- public function create_meta_boxes() {
276
- global $post;
277
-
278
  add_meta_box(
279
  'ca-sidebar',
280
  'Options',
@@ -282,14 +294,16 @@ class ContentAwareSidebars {
282
  'sidebar',
283
  'normal',
284
  'high'
285
- );
286
-
287
  }
288
 
 
 
 
 
 
289
  public function meta_box_content() {
290
-
291
  $this->form_fields(array('post_types','handle','merge-pos','host'));
292
-
293
  }
294
 
295
  /**
@@ -325,7 +339,7 @@ class ContentAwareSidebars {
325
  case 'select-multi' :
326
  echo '<select multiple="multiple" size="5" style="width:200px;height:60px;" name="'.$setting['id'].'[]">'."\n";
327
  foreach($setting['list'] as $key => $value) {
328
- echo '<option value="'.$key.'"'.(in_array($key,unserialize($current)) ? ' selected="selected"' : '').'>'.$value.'</option>'."\n";
329
  }
330
  echo '</select>'."\n";
331
  break;
@@ -349,28 +363,25 @@ class ContentAwareSidebars {
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'])) {
357
  return $post_id;
358
- }
359
 
360
  // Verify nonce
361
- if (!check_admin_referer(basename(__FILE__),'_ca-sidebar-nonce')) {
362
  return $post_id;
363
- }
364
 
365
  // Check permissions
366
- if (!current_user_can('edit_post', $post_id)) {
367
  return $post_id;
368
- }
369
 
370
  // Check autosave
371
- if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
372
  return $post_id;
373
- }
374
 
375
  // Load settings manually here. This ought to be done with action/filter
376
  $this->init_settings();
@@ -380,13 +391,13 @@ class ContentAwareSidebars {
380
  $old = get_post_meta($post_id, $field['id'], true);
381
  $new = $_POST[$field['id']];
382
 
383
- switch($field['id']) {
384
- case 'post_types' :
385
- $new = serialize($new);
386
- break;
387
- default :
388
- break;
389
- }
390
 
391
  if ($new != '' && $new != $old) {
392
  update_post_meta($post_id, $field['id'], $new);
@@ -413,66 +424,44 @@ global $ca_sidebars;
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,
428
- 'post_type' => 'sidebar',
429
- 'orderby' => 'menu_order meta_value',
430
- 'meta_key' => 'handle',
431
- 'order' => 'ASC',
432
- 'meta_query' => array(
433
- array(
434
- 'key' => 'handle',
435
- 'value' => '2',
436
- 'compare' => '=='
437
- )
438
- )
439
- ));
440
- $content_type = $post_type;
441
- if(!$content_type)
442
- $content_type = get_post_type();
443
 
444
- $i = $host = 0;
 
 
 
 
445
  foreach($posts as $post) {
446
-
447
- $id = 'ca-sidebar-'.$post->ID;
448
- $post_types = (array) unserialize(get_post_meta($post->ID, 'post_types', true));
449
 
450
- // Check if current post type is part of rules
451
- if(!in_array($content_type,$post_types))
452
- continue;
 
 
453
 
 
454
  if($i > 0) {
455
-
456
- // Check if sidebar is active
457
- if (!isset($_wp_sidebars_widgets[$id]))
458
- continue;
459
-
460
- $merge_pos = get_post_meta($post->ID, 'merge-pos', true);
461
- if($merge_pos)
462
  $_wp_sidebars_widgets[$host] = array_merge($_wp_sidebars_widgets[$host],$_wp_sidebars_widgets[$id]);
463
  else
464
- $_wp_sidebars_widgets[$host] = array_merge($_wp_sidebars_widgets[$id],$_wp_sidebars_widgets[$host]);
465
  } else {
466
  $host = $id;
467
  }
468
  $i++;
469
  }
470
 
471
- if ($host && is_active_sidebar($host)) {
472
  echo $before;
473
  dynamic_sidebar($host);
474
  echo $after;
475
  }
476
-
477
- }
478
-
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.3
10
  Author: Joachim Jensen
11
  Author URI: http://www.intox.dk/
12
  License:
29
  */
30
  class ContentAwareSidebars {
31
 
32
+ public $version = 0.3;
33
  public $settings = array();
34
 
35
  /**
40
  public function __construct() {
41
 
42
  add_filter('wp', array(&$this,'replace_sidebar'));
43
+ add_action('init', array(&$this,'init_sidebar_type'),20);
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
  add_action('save_post', array(&$this,'save_post'));
49
 
50
  register_activation_hook(__FILE__, array(&$this,'upon_activation'));
153
  $taxonomies = get_taxonomies(array('public'=>true));
154
  foreach($taxonomies as $tax)
155
  remove_submenu_page('edit.php?post_type=sidebar','edit-tags.php?taxonomy='.$tax.'&amp;post_type=sidebar');
 
 
156
  }
157
 
158
  /**
164
  $posts = get_posts(array(
165
  'numberposts' => 0,
166
  'post_type' => 'sidebar',
167
+ 'post_status' => array('publish','private','future')
168
  ));
169
  foreach($posts as $post)
170
  register_sidebar( array(
177
  'after_title' => '</h3>',
178
  ));
179
  }
180
+
181
  /**
182
  *
183
  * Replace a sidebar with content aware sidebars
185
  *
186
  */
187
  public function replace_sidebar() {
188
+ global $_wp_sidebars_widgets;
189
 
190
+ $posts = $this->get_sidebars();
191
+ if(!$posts)
192
  return;
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  foreach($posts as $post) {
195
+
 
196
  $id = 'ca-sidebar-'.$post->ID;
197
 
198
  // Check if sidebar exists
199
  if (!isset($_wp_sidebars_widgets[$id]))
200
  continue;
201
 
202
+ // If host has already been replaced, merge with it instead. Might change in future.
203
+ if($post->handle || isset($handled_already[$post->host])) {
204
+ if($post->merge_pos)
205
+ $_wp_sidebars_widgets[$post->host] = array_merge($_wp_sidebars_widgets[$post->host],$_wp_sidebars_widgets[$id]);
206
+ else
207
+ $_wp_sidebars_widgets[$post->host] = array_merge($_wp_sidebars_widgets[$id],$_wp_sidebars_widgets[$post->host]);
208
+ } else {
209
+ $_wp_sidebars_widgets[$post->host] = $_wp_sidebars_widgets[$id];
210
+ $handled_already[$post->host] = 1;
211
+ }
212
+ }
213
+ }
214
+
215
+ public function get_sidebars($handle = "!= '2'") {
216
+ global $wpdb;
217
+
218
+ $errors = 1;
219
+
220
+ $joins = "";
221
+ $where = "";
222
+
223
+ if(is_singular()) {
224
+
225
+ $joins .= "LEFT JOIN $wpdb->postmeta post_types ON post_types.post_id = posts.ID AND post_types.meta_key = 'post_types' ";
226
+ $where .= "(post_types.meta_value LIKE '%".serialize(get_post_type())."%'";
227
 
228
+ if(has_term() || has_category() || has_tag()) {
 
 
 
 
229
 
230
+ $post_terms = wp_get_object_terms(get_the_ID(),get_object_taxonomies(get_post_type()));
231
+ $terms = array();
232
 
233
+ //Grab posts terms by slugs.
234
+ foreach($post_terms as $term)
235
+ $terms[] = $term->slug;
236
 
237
+ $joins .= "LEFT JOIN $wpdb->term_relationships term ON term.object_id = posts.ID ";
238
+ $joins .= "LEFT JOIN $wpdb->term_taxonomy taxonomy ON taxonomy.term_taxonomy_id = term.term_taxonomy_id ";
239
+ $joins .= "LEFT JOIN $wpdb->terms terms ON terms.term_id = taxonomy.term_id ";
240
+ $where .= " OR terms.slug IN('".implode("','",$terms)."')";
241
+
 
 
242
  }
243
+ $where .= ") AND ";
244
+ $errors--;
245
+ }
246
+
247
+ if($errors)
248
+ return false;
249
+
250
+ // Show private sidebars or not
251
+ if(current_user_can('read_private_posts'))
252
+ $post_status = "IN('publish','private')";
253
+ else
254
+ $post_status = "= 'publish'";
255
+ $where .= "posts.post_status ".$post_status." AND ";
256
+
257
+ // Get proper sidebars
258
+ return $wpdb->get_results("
259
+ SELECT
260
+ posts.ID,
261
+ handle.meta_value handle,
262
+ host.meta_value host,
263
+ merge_pos.meta_value merge_pos
264
+ FROM $wpdb->posts posts
265
+ LEFT JOIN $wpdb->postmeta handle
266
+ ON handle.post_id = posts.ID
267
+ AND handle.meta_key = 'handle'
268
+ LEFT JOIN $wpdb->postmeta host
269
+ ON host.post_id = posts.ID
270
+ AND host.meta_key = 'host'
271
+ LEFT JOIN $wpdb->postmeta merge_pos
272
+ ON merge_pos.post_id = posts.ID
273
+ AND merge_pos.meta_key = 'merge-pos'
274
+ $joins
275
+ WHERE
276
+ posts.post_type = 'sidebar' AND
277
+ $where
278
+ handle.meta_value $handle
279
+ GROUP BY posts.ID
280
+ ORDER BY posts.menu_order ASC, handle.meta_value DESC, posts.post_date DESC
281
+ ");
282
  }
283
 
284
  /**
286
  * Meta boxes for edit post
287
  *
288
  */
289
+ public function create_meta_boxes() {
 
 
290
  add_meta_box(
291
  'ca-sidebar',
292
  'Options',
294
  'sidebar',
295
  'normal',
296
  'high'
297
+ );
 
298
  }
299
 
300
+ /**
301
+ *
302
+ * Options content
303
+ *
304
+ */
305
  public function meta_box_content() {
 
306
  $this->form_fields(array('post_types','handle','merge-pos','host'));
 
307
  }
308
 
309
  /**
339
  case 'select-multi' :
340
  echo '<select multiple="multiple" size="5" style="width:200px;height:60px;" name="'.$setting['id'].'[]">'."\n";
341
  foreach($setting['list'] as $key => $value) {
342
+ echo '<option value="'.$key.'"'.(in_array($key,$current) ? ' selected="selected"' : '').'>'.$value.'</option>'."\n";
343
  }
344
  echo '</select>'."\n";
345
  break;
363
  */
364
  public function save_post($post_id) {
365
 
366
+ // Only sidebar type
367
  if(get_post_type($post_id) != 'sidebar')
368
  return $post_id;
369
 
370
  // Save button pressed
371
+ if(!isset($_POST['original_publish']))
372
  return $post_id;
 
373
 
374
  // Verify nonce
375
+ if (!check_admin_referer(basename(__FILE__),'_ca-sidebar-nonce'))
376
  return $post_id;
 
377
 
378
  // Check permissions
379
+ if (!current_user_can('edit_post', $post_id))
380
  return $post_id;
 
381
 
382
  // Check autosave
383
+ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
384
  return $post_id;
 
385
 
386
  // Load settings manually here. This ought to be done with action/filter
387
  $this->init_settings();
391
  $old = get_post_meta($post_id, $field['id'], true);
392
  $new = $_POST[$field['id']];
393
 
394
+ //switch($field['id']) {
395
+ // case 'post_types' :
396
+ // $new = serialize($new);
397
+ // break;
398
+ // default :
399
+ // break;
400
+ //}
401
 
402
  if ($new != '' && $new != $old) {
403
  update_post_meta($post_id, $field['id'], $new);
424
  $ca_sidebars = new ContentAwareSidebars();
425
 
426
  // Template function
427
+ function display_ca_sidebar($args = array()) {
428
+ global $ca_sidebars, $_wp_sidebars_widgets;
429
+
430
  $defaults = array (
431
  'before' => '<div id="sidebar" class="widget-area"><ul class="xoxo">',
432
  'after' => '</ul></div>'
433
  );
434
  $args = wp_parse_args($args,$defaults);
435
+ extract($args,EXTR_SKIP);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436
 
437
+ $posts = $ca_sidebars->get_sidebars("='2'");
438
+ if(!$posts)
439
+ return;
440
+
441
+ $i = $host = 0;
442
  foreach($posts as $post) {
 
 
 
443
 
444
+ $id = 'ca-sidebar-'.$post->ID;
445
+
446
+ // Check if sidebar exists
447
+ if (!isset($_wp_sidebars_widgets[$id]))
448
+ continue;
449
 
450
+ // Merge if more than one. First one is host.
451
  if($i > 0) {
452
+ if($post->merge_pos)
 
 
 
 
 
 
453
  $_wp_sidebars_widgets[$host] = array_merge($_wp_sidebars_widgets[$host],$_wp_sidebars_widgets[$id]);
454
  else
455
+ $_wp_sidebars_widgets[$host] = array_merge($_wp_sidebars_widgets[$id],$_wp_sidebars_widgets[$host]);
456
  } else {
457
  $host = $id;
458
  }
459
  $i++;
460
  }
461
 
462
+ if ($host) {
463
  echo $before;
464
  dynamic_sidebar($host);
465
  echo $after;
466
  }
467
+ }
 
 
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=KPZHE
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
 
@@ -18,14 +18,15 @@ Current features include:
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
 
30
  == Installation ==
31
 
@@ -51,6 +52,14 @@ You are.
51
 
52
  == Changelog ==
53
 
 
 
 
 
 
 
 
 
54
  = 0.2 =
55
 
56
  * Added: taxonomy terms rules
@@ -71,4 +80,4 @@ You are.
71
 
72
  == Translations ==
73
 
74
- None yet. Might come in the future. Do you want to contribute? Feel free to contact me at jv@intox.dk
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.3
8
 
9
  Manage and show sidebars according to the content being viewed.
10
 
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
+ * Private sidebars only for members
22
+ * Schedule sidebars for later publishing
23
 
24
+ Some upcoming features:
25
 
26
  * Show sidebars in a time span only
27
  * Show sidebars with (taxonomy term and post type) archives
28
 
29
+ If you have any suggestions, please send a mail to jv[at]intox.dk or contact me at www.intox.dk.
30
 
31
  == Installation ==
32
 
52
 
53
  == Changelog ==
54
 
55
+ = 0.3 =
56
+
57
+ * Added: Sidebars can now be private
58
+ * Fixed: taxonomy terms are now supported by template function
59
+ * Fixed: faster rule recognition and handling
60
+ * Fixed: custom taxonomies are now supported properly
61
+ * Fixed: error if several sidebars had taxonomy terms rules
62
+
63
  = 0.2 =
64
 
65
  * Added: taxonomy terms rules
80
 
81
  == Translations ==
82
 
83
+ None yet. Might come in the future. Do you want to contribute? Send a mail to jv[at]intox.dk