Content Aware Sidebars – Unlimited Widget Areas - Version 0.4

Version Description

  • Added: post type archives, taxonomy archives and taxonomy terms archives now supported
  • Added: taxonomy rules
  • Added: removable donation button
  • Fixed: faster!
Download this release

Release Info

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

Code changes from version 0.2 to 0.4

Files changed (2) hide show
  1. content-aware-sidebars.php +265 -161
  2. readme.txt +33 -14
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,8 +29,10 @@ License:
29
  */
30
  class ContentAwareSidebars {
31
 
32
- public $version = 0.2;
33
- public $settings = array();
 
 
34
 
35
  /**
36
  *
@@ -38,27 +40,44 @@ class ContentAwareSidebars {
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( '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'));
52
 
53
  }
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  /**
56
  *
57
  * Create post meta fields
58
  * Loaded in admin_head due to $post. Should be loaded even later if possible.
59
  *
60
  */
61
- public function init_settings() {
62
  global $post, $wp_registered_sidebars;
63
 
64
  // List of sidebars
@@ -68,20 +87,31 @@ class ContentAwareSidebars {
68
  $sidebar_list[$sidebar['id']] = $sidebar['name'];
69
  }
70
 
71
- // List of public post types
72
- $post_type_list = array();
73
- foreach(get_post_types(array('public'=>true),'objects') as $post_type)
74
- $post_type_list[$post_type->name] = $post_type->label;
75
-
76
  // Meta fields
77
  $this->settings = array(
78
  'post_types' => array(
79
  'name' => 'Post Types',
80
  'id' => 'post_types',
81
  'desc' => '',
82
- 'val' => '',
83
- 'type' => 'select-multi',
84
- 'list' => $post_type_list
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  ),
86
  'handle' => array(
87
  'name' => 'Handle',
@@ -138,11 +168,8 @@ class ContentAwareSidebars {
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
  /**
@@ -151,11 +178,8 @@ class ContentAwareSidebars {
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.'&post_type=sidebar');
157
-
158
-
159
  }
160
 
161
  /**
@@ -167,7 +191,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 +204,7 @@ class ContentAwareSidebars {
180
  'after_title' => '</h3>',
181
  ));
182
  }
183
-
184
  /**
185
  *
186
  * Replace a sidebar with content aware sidebars
@@ -188,83 +212,158 @@ 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 +371,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',
@@ -283,36 +380,68 @@ class ContentAwareSidebars {
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
  /**
296
  *
297
- * Create form fields
298
  *
299
  */
300
- private function form_fields($array) {
301
- global $post;
302
-
303
  // Use nonce for verification
304
  wp_nonce_field(basename(__FILE__),'_ca-sidebar-nonce');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
  ?>
306
  <table class="form-table">
307
  <?php
308
- $array = array_intersect_key($this->settings,array_flip($array));
 
 
 
309
  foreach($array as $setting) :
310
 
311
- $meta = get_post_meta($post->ID, $setting['id'], true);
312
- $current = $meta != '' ? $meta : $setting['val'];
313
- ?>
 
314
  <tr valign="top">
315
- <th scope="row"><?php echo $setting['name'] ?></th>
316
  <td>
317
  <?php switch($setting['type']) :
318
  case 'select' :
@@ -325,10 +454,17 @@ 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;
 
 
 
 
 
 
 
332
  case 'text' :
333
  default :
334
  echo '<input style="width:200px;" type="text" name="'.$setting['id'].'" value="'.implode(",",unserialize($current)).'" />'."\n";
@@ -349,45 +485,34 @@ 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();
377
 
378
  // Update values
379
  foreach ($this->settings as $field) {
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);
393
  }elseif ($new == '' && $old != '') {
@@ -413,66 +538,45 @@ 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.4
10
  Author: Joachim Jensen
11
  Author URI: http://www.intox.dk/
12
  License:
29
  */
30
  class ContentAwareSidebars {
31
 
32
+ protected $version = 0.4;
33
+ protected $settings = array();
34
+ protected $post_types = array();
35
+ protected $taxonomies = array();
36
 
37
  /**
38
  *
40
  *
41
  */
42
  public function __construct() {
43
+
44
+ $this->init_settings();
45
 
46
  add_filter('wp', array(&$this,'replace_sidebar'));
47
+ add_action('init', array(&$this,'init_sidebar_type'),20);
48
  add_action('widgets_init', array(&$this,'create_sidebars'));
49
  add_action('admin_init', array(&$this,'create_meta_boxes'));
50
+ add_action('admin_head', array(&$this,'init_metadata'));
51
+ add_action('admin_menu', array(&$this,'clear_admin_menu'));
 
52
  add_action('save_post', array(&$this,'save_post'));
53
 
54
  register_activation_hook(__FILE__, array(&$this,'upon_activation'));
55
 
56
  }
57
 
58
+ /**
59
+ *
60
+ * Initiate lists
61
+ *
62
+ */
63
+ private function init_settings() {
64
+
65
+
66
+ // Public post types
67
+ foreach(get_post_types(array('public'=>true),'objects') as $post_type)
68
+ $this->post_types[$post_type->name] = $post_type->label;
69
+
70
+ foreach(get_taxonomies(array('public'=>true),'objects') as $tax)
71
+ $this->taxonomies[$tax->name] = $tax->label;
72
+ }
73
+
74
  /**
75
  *
76
  * Create post meta fields
77
  * Loaded in admin_head due to $post. Should be loaded even later if possible.
78
  *
79
  */
80
+ public function init_metadata() {
81
  global $post, $wp_registered_sidebars;
82
 
83
  // List of sidebars
87
  $sidebar_list[$sidebar['id']] = $sidebar['name'];
88
  }
89
 
 
 
 
 
 
90
  // Meta fields
91
  $this->settings = array(
92
  'post_types' => array(
93
  'name' => 'Post Types',
94
  'id' => 'post_types',
95
  'desc' => '',
96
+ 'val' => array(),
97
+ 'type' => 'checkbox',
98
+ 'list' => $this->post_types
99
+ ),
100
+ 'taxonomies' => array(
101
+ 'name' => 'Taxonomies',
102
+ 'id' => 'taxonomies',
103
+ 'desc' => '',
104
+ 'val' => array(),
105
+ 'type' => 'checkbox',
106
+ 'list' => $this->taxonomies
107
+ ),
108
+ 'exposure' => array(
109
+ 'name' => 'Exposure',
110
+ 'id' => 'exposure',
111
+ 'desc' => 'Affects post types, taxonomies and taxonomy terms.',
112
+ 'val' => 1,
113
+ 'type' => 'select',
114
+ 'list' => array('Singular','Singular & Archive','Archive')
115
  ),
116
  'handle' => array(
117
  'name' => 'Handle',
168
  'rewrite' => false,
169
  'menu_position' => null,
170
  'supports' => array('title','excerpt','page-attributes'),
171
+ 'taxonomies' => array_flip($this->taxonomies)
172
+ ));
 
 
 
173
  }
174
 
175
  /**
178
  *
179
  */
180
  function clear_admin_menu() {
181
+ foreach($this->taxonomies as $key => $value)
182
+ remove_submenu_page('edit.php?post_type=sidebar','edit-tags.php?taxonomy='.$key.'&amp;post_type=sidebar');
 
 
 
183
  }
184
 
185
  /**
191
  $posts = get_posts(array(
192
  'numberposts' => 0,
193
  'post_type' => 'sidebar',
194
+ 'post_status' => array('publish','private','future')
195
  ));
196
  foreach($posts as $post)
197
  register_sidebar( array(
204
  'after_title' => '</h3>',
205
  ));
206
  }
207
+
208
  /**
209
  *
210
  * Replace a sidebar with content aware sidebars
212
  *
213
  */
214
  public function replace_sidebar() {
215
+ global $_wp_sidebars_widgets;
216
 
217
+ $posts = $this->get_sidebars();
218
+ if(!$posts)
219
  return;
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  foreach($posts as $post) {
222
+
 
223
  $id = 'ca-sidebar-'.$post->ID;
224
 
225
  // Check if sidebar exists
226
  if (!isset($_wp_sidebars_widgets[$id]))
227
  continue;
228
 
229
+ // If host has already been replaced, merge with it instead. Might change in future.
230
+ if($post->handle || isset($handled_already[$post->host])) {
231
+ if($post->merge_pos)
232
+ $_wp_sidebars_widgets[$post->host] = array_merge($_wp_sidebars_widgets[$post->host],$_wp_sidebars_widgets[$id]);
233
+ else
234
+ $_wp_sidebars_widgets[$post->host] = array_merge($_wp_sidebars_widgets[$id],$_wp_sidebars_widgets[$post->host]);
235
+ } else {
236
+ $_wp_sidebars_widgets[$post->host] = $_wp_sidebars_widgets[$id];
237
+ $handled_already[$post->host] = 1;
238
+ }
239
+ }
240
+ }
241
+
242
+ /**
243
+ *
244
+ * Query sidebars according to content
245
+ * @return array|bool
246
+ *
247
+ */
248
+ public function get_sidebars($handle = "!= '2'") {
249
+ global $wpdb, $post_type;
250
+
251
+ $errors = 1;
252
+
253
+ $joins = "";
254
+ $where = "";
255
+
256
+ // Single content
257
+ if(is_singular()) {
258
+
259
+ $joins .= "LEFT JOIN $wpdb->postmeta post_types ON post_types.post_id = posts.ID AND post_types.meta_key = 'post_types' ";
260
+ $where .= "(post_types.meta_value LIKE '%".serialize(get_post_type())."%'";
261
+
262
+ $post_taxonomies = get_object_taxonomies(get_post_type());
263
 
264
+ // Check if content has any taxonomies supported
265
+ if($post_taxonomies) {
266
+ $post_terms = wp_get_object_terms(get_the_ID(),$post_taxonomies);
267
+ // Check if content has any actual taxonomy terms
268
+ if($post_terms) {
269
+ $terms = array();
270
+ $taxonomies = array();
271
 
272
+ //Grab posts terms and make where rules for taxonomies.
273
+ foreach($post_terms as $term) {
274
+ $terms[] = $term->slug;
275
+ if(!isset($taxonomies[$term->taxonomy])) {
276
+ $where .= " OR post_tax.meta_value LIKE '%".$taxonomies[$term->taxonomy] = $term->taxonomy."%'";
277
+ }
 
 
 
 
 
 
278
  }
279
+
280
+ $joins .= "LEFT JOIN $wpdb->term_relationships term ON term.object_id = posts.ID ";
281
+ $joins .= "LEFT JOIN $wpdb->term_taxonomy taxonomy ON taxonomy.term_taxonomy_id = term.term_taxonomy_id ";
282
+ $joins .= "LEFT JOIN $wpdb->terms terms ON terms.term_id = taxonomy.term_id ";
283
+ $joins .= "LEFT JOIN $wpdb->postmeta post_tax ON post_tax.post_id = posts.ID AND post_tax.meta_key = 'taxonomies'";
284
+
285
+ $where .= " OR terms.slug IN('".implode("','",$terms)."')";
286
  }
287
  }
288
+
289
+ $where .= ") AND ";
290
+ $where .= "exposure.meta_value <= '1' AND ";
291
 
292
+ $errors--;
 
 
293
 
294
+ // Taxonomy archives
295
+ } elseif(is_tax() || is_category() || is_tag()) {
296
 
297
+ $term = get_queried_object();
298
+
299
+ $joins .= "LEFT JOIN $wpdb->term_relationships term ON term.object_id = posts.ID ";
300
+ $joins .= "LEFT JOIN $wpdb->term_taxonomy taxonomy ON taxonomy.term_taxonomy_id = term.term_taxonomy_id ";
301
+ $joins .= "LEFT JOIN $wpdb->terms terms ON terms.term_id = taxonomy.term_id ";
302
+ $joins .= "LEFT JOIN $wpdb->postmeta post_tax ON post_tax.post_id = posts.ID AND post_tax.meta_key = 'taxonomies'";
303
+
304
+ $where .= "(terms.slug = '$term->slug'";
305
+ $where .= " OR post_tax.meta_value LIKE '%$term->taxonomy%'";
306
+ $where .= ") AND ";
307
+ $where .= "exposure.meta_value >= '1' AND ";
308
+
309
+ $errors--;
310
+
311
+ // TODO: Front page
312
+ } elseif(is_front_page() && !is_home()) {
313
+ // Front page laters...
314
+ // Post Type archives
315
+ } elseif(is_post_type_archive() || is_home()) {
316
+
317
+ // Home has post as default post type
318
+ if(!$post_type) $post_type = 'post';
319
+
320
+ $joins .= "LEFT JOIN $wpdb->postmeta post_types ON post_types.post_id = posts.ID AND post_types.meta_key = 'post_types' ";
321
+
322
+ $where .= "(post_types.meta_value LIKE '%$post_type%') AND ";
323
+ $where .= "exposure.meta_value >= '1' AND ";
324
+
325
+ $errors--;
326
+ }
327
+
328
+ // Check if any errors are left
329
+ if($errors)
330
+ return false;
331
+
332
+ // Show private sidebars or not
333
+ if(current_user_can('read_private_posts'))
334
+ $post_status = "IN('publish','private')";
335
+ else
336
+ $post_status = "= 'publish'";
337
+ $where .= "posts.post_status ".$post_status." AND ";
338
+
339
+ // Return proper sidebars
340
+ return $wpdb->get_results("
341
+ SELECT
342
+ posts.ID,
343
+ handle.meta_value handle,
344
+ host.meta_value host,
345
+ merge_pos.meta_value merge_pos
346
+ FROM $wpdb->posts posts
347
+ LEFT JOIN $wpdb->postmeta handle
348
+ ON handle.post_id = posts.ID
349
+ AND handle.meta_key = 'handle'
350
+ LEFT JOIN $wpdb->postmeta host
351
+ ON host.post_id = posts.ID
352
+ AND host.meta_key = 'host'
353
+ LEFT JOIN $wpdb->postmeta merge_pos
354
+ ON merge_pos.post_id = posts.ID
355
+ AND merge_pos.meta_key = 'merge-pos'
356
+ LEFT JOIN $wpdb->postmeta exposure
357
+ ON exposure.post_id = posts.ID
358
+ AND exposure.meta_key = 'exposure'
359
+ $joins
360
+ WHERE
361
+ posts.post_type = 'sidebar' AND
362
+ $where
363
+ handle.meta_value $handle
364
+ GROUP BY posts.ID
365
+ ORDER BY posts.menu_order ASC, handle.meta_value DESC, posts.post_date DESC
366
+ ");
367
  }
368
 
369
  /**
371
  * Meta boxes for edit post
372
  *
373
  */
374
+ public function create_meta_boxes() {
 
 
375
  add_meta_box(
376
  'ca-sidebar',
377
  'Options',
380
  'normal',
381
  'high'
382
  );
383
+ add_meta_box(
384
+ 'ca-sidebar-author-words',
385
+ 'Words from the author',
386
+ array(&$this,'meta_box_author_words'),
387
+ 'sidebar',
388
+ 'side',
389
+ 'high'
390
+ );
391
 
392
  }
393
 
394
+ /**
395
+ *
396
+ * Options content
397
+ *
398
+ */
399
  public function meta_box_content() {
400
+ $this->form_fields();
 
 
401
  }
402
 
403
  /**
404
  *
405
+ * Author words content
406
  *
407
  */
408
+ public function meta_box_author_words() {
 
 
409
  // Use nonce for verification
410
  wp_nonce_field(basename(__FILE__),'_ca-sidebar-nonce');
411
+ ?>
412
+ <div style="text-align:center;">
413
+ <div><p>If you love this plugin, please consider donating.</p></div>
414
+ <a href="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"
415
+ target="_blank" title="PayPal - The safer, easier way to pay online!">
416
+ <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" width="147" height="47" alt="PayPal - The safer, easier way to pay online!">
417
+ </a>
418
+ </div>
419
+ <?php
420
+ }
421
+
422
+ /**
423
+ *
424
+ * Create form fields
425
+ *
426
+ */
427
+ private function form_fields($array = array(), $show_name = 1) {
428
+ global $post;
429
+
430
  ?>
431
  <table class="form-table">
432
  <?php
433
+ if(!empty($array))
434
+ $array = array_intersect_key($this->settings,array_flip($array));
435
+ else
436
+ $array = $this->settings;
437
  foreach($array as $setting) :
438
 
439
+ $meta = get_post_meta($post->ID, $setting['id'], true);
440
+ $current = $meta != '' ? $meta : $setting['val'];
441
+ ?>
442
+
443
  <tr valign="top">
444
+ <?php if($show_name) echo '<th scope="row">'.$setting['name'].'</th>'; ?>
445
  <td>
446
  <?php switch($setting['type']) :
447
  case 'select' :
454
  case 'select-multi' :
455
  echo '<select multiple="multiple" size="5" style="width:200px;height:60px;" name="'.$setting['id'].'[]">'."\n";
456
  foreach($setting['list'] as $key => $value) {
457
+ echo '<option value="'.$key.'"'.(in_array($key,$current) ? ' selected="selected"' : '').'>'.$value.'</option>'."\n";
458
  }
459
  echo '</select>'."\n";
460
  break;
461
+ case 'checkbox' :
462
+ echo '<ul>'."\n";
463
+ foreach($setting['list'] as $key => $value) {
464
+ echo '<li><label><input type="checkbox" name="'.$setting['id'].'[]" value="'.$key.'"'.(in_array($key,$current) ? ' checked="checked"' : '').' /> '.$value.'</label></li>'."\n";
465
+ }
466
+ echo '</ul>'."\n";
467
+ break;
468
  case 'text' :
469
  default :
470
  echo '<input style="width:200px;" type="text" name="'.$setting['id'].'" value="'.implode(",",unserialize($current)).'" />'."\n";
485
  */
486
  public function save_post($post_id) {
487
 
488
+ // Only sidebar type
489
  if(get_post_type($post_id) != 'sidebar')
490
  return $post_id;
491
 
492
  // Save button pressed
493
+ if(!isset($_POST['original_publish']))
494
  return $post_id;
 
495
 
496
  // Verify nonce
497
+ if (!check_admin_referer(basename(__FILE__),'_ca-sidebar-nonce'))
498
  return $post_id;
 
499
 
500
  // Check permissions
501
+ if (!current_user_can('edit_post', $post_id))
502
  return $post_id;
 
503
 
504
  // Check autosave
505
+ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
506
  return $post_id;
 
507
 
508
  // Load settings manually here. This ought to be done with action/filter
509
+ $this->init_metadata();
510
 
511
  // Update values
512
  foreach ($this->settings as $field) {
513
  $old = get_post_meta($post_id, $field['id'], true);
514
+ $new = isset($_POST[$field['id']]) ? $_POST[$field['id']] : '';
515
+
 
 
 
 
 
 
 
 
516
  if ($new != '' && $new != $old) {
517
  update_post_meta($post_id, $field['id'], $new);
518
  }elseif ($new == '' && $old != '') {
538
  $ca_sidebars = new ContentAwareSidebars();
539
 
540
  // Template function
541
+ function display_ca_sidebar($args = array()) {
542
+ global $ca_sidebars, $_wp_sidebars_widgets;
543
 
544
+ // Grab args or defaults
545
  $defaults = array (
546
  'before' => '<div id="sidebar" class="widget-area"><ul class="xoxo">',
547
  'after' => '</ul></div>'
548
  );
549
  $args = wp_parse_args($args,$defaults);
550
+ extract($args,EXTR_SKIP);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
551
 
552
+ $posts = $ca_sidebars->get_sidebars("='2'");
553
+ if(!$posts)
554
+ return;
555
+
556
+ $i = $host = 0;
557
  foreach($posts as $post) {
 
 
 
558
 
559
+ $id = 'ca-sidebar-'.$post->ID;
560
+
561
+ // Check if sidebar exists
562
+ if (!isset($_wp_sidebars_widgets[$id]))
563
+ continue;
564
 
565
+ // Merge if more than one. First one is host.
566
  if($i > 0) {
567
+ if($post->merge_pos)
 
 
 
 
 
 
568
  $_wp_sidebars_widgets[$host] = array_merge($_wp_sidebars_widgets[$host],$_wp_sidebars_widgets[$id]);
569
  else
570
+ $_wp_sidebars_widgets[$host] = array_merge($_wp_sidebars_widgets[$id],$_wp_sidebars_widgets[$host]);
571
  } else {
572
  $host = $id;
573
  }
574
  $i++;
575
  }
576
 
577
+ if ($host) {
578
  echo $before;
579
  dynamic_sidebar($host);
580
  echo $after;
581
  }
 
582
  }
 
readme.txt CHANGED
@@ -1,35 +1,35 @@
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
 
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
- * 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
 
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
@@ -51,6 +51,21 @@ You are.
51
 
52
  == Changelog ==
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  = 0.2 =
55
 
56
  * Added: taxonomy terms rules
@@ -65,10 +80,14 @@ You are.
65
 
66
  == Upgrade Notice ==
67
 
 
 
 
 
68
  = 0.1 =
69
 
70
  * Hello World
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
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, archive
5
  Requires at least: 3.0
6
  Tested up to: 3.2.1
7
+ Stable tag: 0.4
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 the administration.
14
+ No extra database tables or table columns will be added.
15
 
16
+ <b>Features</b>
17
 
18
+ * Show sidebars with specific post types
19
+ * Show sidebars with posts containing specific taxonomies or taxonomy terms
20
+ * Show sidebars with specific post type archives, taxonomy archives or taxonomy term archives
21
  * Merge new sidebars with others, replace them or simply add them to your theme manually
22
  * Create complex content with nested sidebars
23
+ * Private sidebars only for members
24
+ * Schedule sidebars for later publishing
25
 
26
+ <b>Contact</b>
27
 
28
+ If you have any suggestions, please send a mail to jv[at]intox.dk or contact me at www.intox.dk.
 
 
 
29
 
30
  == Installation ==
31
 
32
+ Easy install:
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
51
 
52
  == Changelog ==
53
 
54
+ = 0.4 =
55
+
56
+ * Added: post type archives, taxonomy archives and taxonomy terms archives now supported
57
+ * Added: taxonomy rules
58
+ * Added: removable donation button
59
+ * Fixed: faster!
60
+
61
+ = 0.3 =
62
+
63
+ * Added: sidebars can now be private
64
+ * Fixed: taxonomy terms are now supported by template function
65
+ * Fixed: faster rule recognition and handling
66
+ * Fixed: custom taxonomies are now supported properly
67
+ * Fixed: error if several sidebars had taxonomy terms rules
68
+
69
  = 0.2 =
70
 
71
  * Added: taxonomy terms rules
80
 
81
  == Upgrade Notice ==
82
 
83
+ = 0.4 =
84
+
85
+ * All current custom sidebars have to be updated after plugin upgrade due to the new archive rules
86
+
87
  = 0.1 =
88
 
89
  * Hello World
90
 
91
  == Translations ==
92
 
93
+ None yet. Might come in the future. Do you want to contribute? Send a mail to jv[at]intox.dk