Content Aware Sidebars – Unlimited Widget Areas - Version 0.6.2

Version Description

  • Fixed: array_flip triggered type mismatch errors in some cases
Download this release

Release Info

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

Code changes from version 0.5 to 0.6.2

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.5
10
  Author: Joachim Jensen
11
  Author URI: http://www.intox.dk/
12
  Text Domain: content-aware-sidebars
@@ -31,10 +31,11 @@ License: GPL2
31
  */
32
  class ContentAwareSidebars {
33
 
34
- protected $version = 0.5;
35
- protected $settings = array();
36
- protected $post_types = array();
37
- protected $taxonomies = array();
 
38
 
39
  /**
40
  *
@@ -43,32 +44,46 @@ class ContentAwareSidebars {
43
  */
44
  public function __construct() {
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
  // Public post types
66
- foreach(get_post_types(array('public'=>true),'objects') as $post_type)
67
  $this->post_types[$post_type->name] = $post_type->label;
 
 
68
 
69
  // Public taxonomies
70
- foreach(get_taxonomies(array('public'=>true),'objects') as $tax)
71
  $this->taxonomies[$tax->name] = $tax->label;
 
 
72
  }
73
 
74
  /**
@@ -113,8 +128,8 @@ class ContentAwareSidebars {
113
  'type' => 'checkbox',
114
  'list' => array(
115
  'front-page' => __('Front Page', 'content-aware-sidebars'),
116
- 'search' => __('Search', 'content-aware-sidebars'),
117
- '404' => __('404', 'content-aware-sidebars')
118
  )
119
  ),
120
  'exposure' => array(
@@ -144,7 +159,7 @@ class ContentAwareSidebars {
144
  'host' => array(
145
  'name' => __('Host Sidebar', 'content-aware-sidebars'),
146
  'id' => 'host',
147
- 'desc' => __('The sidebar that should be handled with. Nesting is possible. Manual handling makes this option superfluous.', 'content-aware-sidebars'),
148
  'val' => 'sidebar-1',
149
  'type' => 'select',
150
  'list' => $sidebar_list
@@ -186,27 +201,54 @@ class ContentAwareSidebars {
186
  'view_item' => __('View Sidebar', 'content-aware-sidebars'),
187
  'search_items' => __('Search Sidebars', 'content-aware-sidebars'),
188
  'not_found' => __('No sidebars found', 'content-aware-sidebars'),
189
- 'not_found_in_trash' => __('No sidebars found in Trash', 'content-aware-sidebars'),
190
- 'parent_item_colon' => '',
191
- 'menu_name' => __('Sidebars', 'content-aware-sidebars')
192
  ),
193
  'show_ui' => true,
194
  'query_var' => false,
195
  'rewrite' => false,
196
  'menu_position' => null,
197
  'supports' => array('title','excerpt','page-attributes'),
198
- 'taxonomies' => array_flip($this->taxonomies)
 
199
  ));
200
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
 
202
  /**
203
  *
204
- * Remove taxonomy shortcuts from menu. Gets too cluttered.
205
  *
206
  */
207
  function clear_admin_menu() {
208
- foreach($this->taxonomies as $key => $value)
209
  remove_submenu_page('edit.php?post_type=sidebar','edit-tags.php?taxonomy='.$key.'&post_type=sidebar');
 
 
 
210
  }
211
 
212
  /**
@@ -231,6 +273,86 @@ class ContentAwareSidebars {
231
  'after_title' => '</h3>',
232
  ));
233
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
 
235
  /**
236
  *
@@ -294,7 +416,8 @@ class ContentAwareSidebars {
294
  } elseif(is_singular()) {
295
 
296
  $joins .= "LEFT JOIN $wpdb->postmeta post_types ON post_types.post_id = posts.ID AND post_types.meta_key = 'post_types' ";
297
- $where .= "(post_types.meta_value LIKE '%".serialize(get_post_type())."%'";
 
298
 
299
  $post_taxonomies = get_object_taxonomies(get_post_type());
300
 
@@ -422,18 +545,11 @@ class ContentAwareSidebars {
422
 
423
  /**
424
  *
425
- * Meta boxes for edit post
426
  *
427
  */
428
- public function create_meta_boxes() {
429
- add_meta_box(
430
- 'ca-sidebar',
431
- __('Options', 'content-aware-sidebars'),
432
- array(&$this,'meta_box_content'),
433
- 'sidebar',
434
- 'normal',
435
- 'high'
436
- );
437
  add_meta_box(
438
  'ca-sidebar-author-words',
439
  __('Words from the author', 'content-aware-sidebars'),
@@ -442,7 +558,39 @@ class ContentAwareSidebars {
442
  'side',
443
  'high'
444
  );
445
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446
  }
447
 
448
  /**
@@ -473,6 +621,112 @@ class ContentAwareSidebars {
473
  <?php
474
  }
475
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
476
  /**
477
  *
478
  * Create form fields
@@ -484,10 +738,14 @@ class ContentAwareSidebars {
484
  ?>
485
  <table class="form-table">
486
  <?php
487
- if(!empty($array))
488
- $array = array_intersect_key($this->settings,array_flip($array));
489
- else
490
  $array = $this->settings;
 
 
 
 
491
  foreach($array as $setting) :
492
 
493
  $meta = get_post_meta($post->ID, $setting['id'], true);
@@ -539,37 +797,37 @@ class ContentAwareSidebars {
539
  */
540
  public function save_post($post_id) {
541
 
 
 
 
 
542
  // Only sidebar type
543
  if(get_post_type($post_id) != 'sidebar')
544
- return $post_id;
545
-
546
- // Save button pressed
547
- if(!isset($_POST['original_publish']))
548
- return $post_id;
549
 
550
  // Verify nonce
551
  if (!check_admin_referer(basename(__FILE__),'_ca-sidebar-nonce'))
552
- return $post_id;
553
 
554
  // Check permissions
555
  if (!current_user_can('edit_post', $post_id))
556
- return $post_id;
557
 
558
  // Check autosave
559
  if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
560
- return $post_id;
561
 
562
- // Load settings manually here. This ought to be done with action/filter
563
  $this->init_metadata();
564
 
565
- // Update values
566
  foreach ($this->settings as $field) {
567
  $old = get_post_meta($post_id, $field['id'], true);
568
  $new = isset($_POST[$field['id']]) ? $_POST[$field['id']] : '';
569
 
570
  if ($new != '' && $new != $old) {
571
  update_post_meta($post_id, $field['id'], $new);
572
- }elseif ($new == '' && $old != '') {
573
  delete_post_meta($post_id, $field['id'], $old);
574
  }
575
  }
@@ -585,6 +843,12 @@ class ContentAwareSidebars {
585
  flush_rewrite_rules();
586
  }
587
 
 
 
 
 
 
 
588
  }
589
 
590
  // Launch plugin
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.6.2
10
  Author: Joachim Jensen
11
  Author URI: http://www.intox.dk/
12
  Text Domain: content-aware-sidebars
31
  */
32
  class ContentAwareSidebars {
33
 
34
+ protected $settings = array();
35
+ protected $post_types = array();
36
+ protected $post_type_objects = array();
37
+ protected $taxonomies = array();
38
+ protected $taxonomy_objects = array();
39
 
40
  /**
41
  *
44
  */
45
  public function __construct() {
46
 
47
+ $this->load_dependencies();
 
 
 
 
 
 
48
 
49
+ add_filter('wp', array(&$this,'replace_sidebar'));
50
+ add_filter('request', array(&$this,'admin_column_orderby'));
51
+ add_filter('default_hidden_meta_boxes', array(&$this,'change_default_hidden'),10,2);
52
+ add_filter('manage_edit-sidebar_columns', array(&$this,'admin_column_headers'));
53
+ add_filter('manage_edit-sidebar_sortable_columns', array(&$this,'admin_column_headers'));
54
+ add_filter('manage_posts_custom_column', array(&$this,'admin_column_rows'),10,3);
55
+ add_filter('post_row_actions', array(&$this,'sidebar_row_actions'),10,2);
56
+ add_filter('post_updated_messages', array(&$this,'sidebar_updated_messages'));
57
+
58
+ add_action('init', array(&$this,'init_sidebar_type'),50);
59
+ add_action('widgets_init', array(&$this,'create_sidebars'));
60
+ add_action('admin_init', array(&$this,'create_meta_boxes'));
61
+ add_action('admin_head', array(&$this,'init_metadata'));
62
+ add_action('admin_menu', array(&$this,'clear_admin_menu'));
63
+ add_action('save_post', array(&$this,'save_post'));
64
+
65
+ register_activation_hook(__FILE__, array(&$this,'upon_activation'));
66
 
67
  }
68
+
69
 
70
  /**
71
  *
72
  * Initiate lists
73
  *
74
  */
75
+ private function init_settings() {
 
76
  // Public post types
77
+ foreach(get_post_types(array('public'=>true),'objects') as $post_type) {
78
  $this->post_types[$post_type->name] = $post_type->label;
79
+ $this->post_type_objects[$post_type->name] = $post_type;
80
+ }
81
 
82
  // Public taxonomies
83
+ foreach(get_taxonomies(array('public'=>true),'objects') as $tax) {
84
  $this->taxonomies[$tax->name] = $tax->label;
85
+ $this->taxonomy_objects[$tax->name] = $tax;
86
+ }
87
  }
88
 
89
  /**
128
  'type' => 'checkbox',
129
  'list' => array(
130
  'front-page' => __('Front Page', 'content-aware-sidebars'),
131
+ 'search' => __('Search Results', 'content-aware-sidebars'),
132
+ '404' => __('404 Page', 'content-aware-sidebars')
133
  )
134
  ),
135
  'exposure' => array(
159
  'host' => array(
160
  'name' => __('Host Sidebar', 'content-aware-sidebars'),
161
  'id' => 'host',
162
+ 'desc' => __('The sidebar that should be handled. Nesting is possible. Manual handling makes this option superfluous.', 'content-aware-sidebars'),
163
  'val' => 'sidebar-1',
164
  'type' => 'select',
165
  'list' => $sidebar_list
201
  'view_item' => __('View Sidebar', 'content-aware-sidebars'),
202
  'search_items' => __('Search Sidebars', 'content-aware-sidebars'),
203
  'not_found' => __('No sidebars found', 'content-aware-sidebars'),
204
+ 'not_found_in_trash' => __('No sidebars found in Trash', 'content-aware-sidebars')
 
 
205
  ),
206
  'show_ui' => true,
207
  'query_var' => false,
208
  'rewrite' => false,
209
  'menu_position' => null,
210
  'supports' => array('title','excerpt','page-attributes'),
211
+ 'taxonomies' => array_keys($this->taxonomies),
212
+ 'menu_icon' => WP_PLUGIN_URL.'/'.plugin_basename(dirname(__FILE__)).'/icon-16.png'
213
  ));
214
  }
215
+
216
+ /**
217
+ *
218
+ * Create update messages
219
+ *
220
+ */
221
+ function sidebar_updated_messages( $messages ) {
222
+ global $post;
223
+ $messages['sidebar'] = array(
224
+ 0 => '',
225
+ 1 => sprintf(__('Sidebar updated. <a href="%s">Manage widgets</a>','content-aware-sidebars'),'widgets.php'),
226
+ 2 => '',
227
+ 3 => '',
228
+ 4 => __('Sidebar updated.','content-aware-sidebars'),
229
+ 5 => '',
230
+ 6 => sprintf(__('Sidebar published. <a href="%s">Manage widgets</a>','content-aware-sidebars'), 'widgets.php'),
231
+ 7 => __('Sidebar saved.','content-aware-sidebars'),
232
+ 8 => sprintf(__('Sidebar submitted. <a href="%s">Manage widgets</a>','content-aware-sidebars'),'widgets.php'),
233
+ 9 => sprintf(__('Sidebar scheduled for: <strong>%1$s</strong>. <a href="%2$s">Manage widgets</a>','content-aware-sidebars'),
234
+ // translators: Publish box date format, see http://php.net/date
235
+ date_i18n(__('M j, Y @ G:i'),strtotime($post->post_date)),'widgets.php'),
236
+ 10 => sprintf(__('Sidebar draft updated. <a href="%s">Manage widgets</a>','content-aware-sidebars'),'widgets.php'),
237
+ );
238
+ return $messages;
239
+ }
240
 
241
  /**
242
  *
243
+ * Remove taxonomy shortcuts from menu and standard meta boxes.
244
  *
245
  */
246
  function clear_admin_menu() {
247
+ foreach($this->taxonomies as $key => $value) {
248
  remove_submenu_page('edit.php?post_type=sidebar','edit-tags.php?taxonomy='.$key.'&amp;post_type=sidebar');
249
+ remove_meta_box('tagsdiv-'.$key, 'sidebar', 'side');
250
+ remove_meta_box($key.'div', 'sidebar', 'side');
251
+ }
252
  }
253
 
254
  /**
273
  'after_title' => '</h3>',
274
  ));
275
  }
276
+
277
+ /**
278
+ *
279
+ * Add (sortable) admin column headers
280
+ *
281
+ */
282
+ public function admin_column_headers($columns) {
283
+ unset($columns['categories'],$columns['tags']);
284
+ return array_merge(
285
+ array_slice($columns, 0, 2, true),
286
+ array(
287
+ 'exposure' => __('Exposure', 'content-aware-sidebars'),
288
+ 'handle' => _x('Handle','option', 'content-aware-sidebars'),
289
+ 'merge-pos' => __('Merge position', 'content-aware-sidebars')
290
+ ),
291
+ $columns
292
+ );
293
+ }
294
+
295
+ /**
296
+ * Manage custom column sorting
297
+ */
298
+ public function admin_column_orderby($vars) {
299
+ if (isset($vars['orderby']) && in_array($vars['orderby'],array('exposure','handle','merge-pos'))) {
300
+ $vars = array_merge( $vars, array(
301
+ 'meta_key' => $vars['orderby'],
302
+ 'orderby' => 'meta_value'
303
+ ) );
304
+ }
305
+ return $vars;
306
+ }
307
+
308
+ /**
309
+ *
310
+ * Add admin column rows
311
+ *
312
+ */
313
+ public function admin_column_rows($column_name,$post_id) {
314
+
315
+ // Fix for quick edit
316
+ if(!$this->settings) $this->init_metadata();
317
+
318
+ $current = get_post_meta($post_id,$column_name,true);
319
+ $current_from_list = $this->settings[$column_name]['list'][$current];
320
+
321
+ switch($column_name) {
322
+ case 'handle':
323
+ $host = $this->settings['host']['list'][get_post_meta($post_id,'host',true)];
324
+ if($current == 0) {
325
+ printf(__("Replace %s",'content-aware-sidebars'),$host);
326
+ } elseif($current == 1) {
327
+ printf(__("Merge with %s",'content-aware-sidebars'),$host);
328
+ } else {
329
+ echo $current_from_list;
330
+ }
331
+ break;
332
+ case 'exposure':
333
+ case 'merge-pos':
334
+ echo $current_from_list;
335
+ break;
336
+ }
337
+ }
338
+
339
+ /**
340
+ *
341
+ * Add admin rows actions
342
+ *
343
+ */
344
+ public function sidebar_row_actions($actions, $post) {
345
+ if($post->post_type == 'sidebar') {
346
+ return array_merge(
347
+ array_slice($actions, 0, 2, true),
348
+ array(
349
+ 'mng_widgets' => '<a href="widgets.php" title="'.esc_html(__( 'Manage Widgets','content-aware-sidebars')).'">'.__( 'Manage Widgets','content-aware-sidebars').'</a>'
350
+ ),
351
+ $actions
352
+ );
353
+ }
354
+ return $actions;
355
+ }
356
 
357
  /**
358
  *
416
  } elseif(is_singular()) {
417
 
418
  $joins .= "LEFT JOIN $wpdb->postmeta post_types ON post_types.post_id = posts.ID AND post_types.meta_key = 'post_types' ";
419
+ $where .= "(post_types.meta_value LIKE '%".serialize(get_post_type())."%'";
420
+ $where .= " OR post_types.meta_value LIKE '%".serialize((string)get_the_ID())."%'";
421
 
422
  $post_taxonomies = get_object_taxonomies(get_post_type());
423
 
545
 
546
  /**
547
  *
548
+ * Meta boxes for sidebar edit
549
  *
550
  */
551
+ public function create_meta_boxes() {
552
+ // Author Words
 
 
 
 
 
 
 
553
  add_meta_box(
554
  'ca-sidebar-author-words',
555
  __('Words from the author', 'content-aware-sidebars'),
558
  'side',
559
  'high'
560
  );
561
+ // Post Types
562
+ foreach($this->post_type_objects as $post_type) {
563
+ add_meta_box(
564
+ 'ca-sidebar-post-type-'.$post_type->name,
565
+ $post_type->label,
566
+ array(&$this,'meta_box_post_type'),
567
+ 'sidebar',
568
+ 'normal',
569
+ 'high',
570
+ $post_type
571
+ );
572
+ }
573
+ // Taxonomies
574
+ foreach($this->taxonomy_objects as $tax) {
575
+ add_meta_box(
576
+ 'ca-sidebar-tax-'.$tax->name,
577
+ $tax->label,
578
+ array(&$this,'meta_box_taxonomy'),
579
+ 'sidebar',
580
+ 'side',
581
+ 'default',
582
+ $tax
583
+ );
584
+ }
585
+ // Options
586
+ add_meta_box(
587
+ 'ca-sidebar',
588
+ __('Options', 'content-aware-sidebars'),
589
+ array(&$this,'meta_box_content'),
590
+ 'sidebar',
591
+ 'normal',
592
+ 'high'
593
+ );
594
  }
595
 
596
  /**
621
  <?php
622
  }
623
 
624
+ public function meta_box_taxonomy($post, $tax) {
625
+ $meta = get_post_meta($post->ID, 'taxonomies', true);
626
+ $current = $meta != '' ? $meta : array();
627
+
628
+ $taxonomy = $tax['args']->name;
629
+
630
+ $terms = get_terms( $taxonomy);
631
+
632
+ if ( ! $terms || is_wp_error($terms) ) {
633
+ echo '<p>' . __( 'No items.' ) . '</p>';
634
+
635
+ } else {
636
+
637
+ ?>
638
+ <div id="taxonomy-<?php echo $taxonomy; ?>" class="categorydiv">
639
+ <ul id="<?php echo $taxonomy; ?>-tabs" class="category-tabs">
640
+ <li class="hide-if-no-js"><a href="#<?php echo $taxonomy; ?>-pop" tabindex="3"><?php _e( 'Most Used' ); ?></a></li>
641
+ <li class="tabs"><a href="#<?php echo $taxonomy; ?>-all" tabindex="3"><?php _e('View All');; ?></a></li>
642
+ </ul>
643
+
644
+ <div id="<?php echo $taxonomy; ?>-pop" class="tabs-panel" style="display: none;height:inherit;max-height:200px;">
645
+ <ul id="<?php echo $taxonomy; ?>checklist-pop" class="categorychecklist form-no-clear" >
646
+ <?php $popular_ids = cas_popular_terms_checklist($tax['args']); ?>
647
+ </ul>
648
+ </div>
649
+
650
+ <div id="<?php echo $taxonomy; ?>-all" class="tabs-panel" style="height:inherit;max-height:200px;">
651
+ <?php
652
+ $name = ( $taxonomy == 'category' ) ? 'post_category[]' : 'tax_input[' . $taxonomy . ']';
653
+ echo "<input type='hidden' name='{$name}' value='0' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
654
+ ?>
655
+ <ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy?> categorychecklist form-no-clear">
656
+ <?php cas_terms_checklist($post->ID, array( 'taxonomy' => $taxonomy, 'popular_cats' => $popular_ids ) ) ?>
657
+ </ul>
658
+ </div>
659
+ </div>
660
+ <?php
661
+ }
662
+
663
+ echo '<p style="padding:6px 0 4px;">'."\n";
664
+ echo '<label><input type="checkbox" name="taxonomies[]" value="'.$tax['args']->name.'"'.(in_array($tax['args']->name,$current) ? ' checked="checked"' : '').' /> '.sprintf(__('Show with %s'),$tax['args']->labels->all_items).'</label>'."\n";
665
+ echo '</p>'."\n";
666
+
667
+ }
668
+
669
+ public function meta_box_post_type($post, $box) {
670
+ $meta = get_post_meta($post->ID, 'post_types', true);
671
+ $current = $meta != '' ? $meta : array();
672
+ $post_type = $box['args'];
673
+
674
+ $exclude = array();
675
+ if($post_type->name == 'page' && 'page' == get_option( 'show_on_front')) {
676
+ $exclude[] = get_option('page_on_front');
677
+ $exclude[] = get_option('page_for_posts');
678
+ }
679
+
680
+ $posts = get_posts(array('post_type'=>$post_type->name,'exclude'=>$exclude));
681
+
682
+ if ( ! $posts || is_wp_error($posts) ) {
683
+ echo '<p>' . __( 'No items.' ) . '</p>';
684
+
685
+ } else {
686
+
687
+ ?>
688
+
689
+ <div id="posttype-<?php echo $post_type->name; ?>" class="categorydiv">
690
+ <ul id="posttype-<?php echo $post_type->name; ?>-tabs" class="category-tabs">
691
+ <li class="tabs"><a href="#<?php echo $post_type->name; ?>-all" tabindex="3"><?php _e('View All');; ?></a></li>
692
+ </ul>
693
+
694
+ <div id="<?php echo $post_type->name; ?>-all" class="tabs-panel" style="height:inherit;max-height:200px;">
695
+ <ul id="<?php echo $post_type->name; ?>checklist" class="list:<?php echo $post_type->name?> categorychecklist form-no-clear">
696
+ <?php cas_posts_checklist($post->ID, array( 'post_type' => $post_type->name) ) ?>
697
+ </ul>
698
+ </div>
699
+ </div>
700
+
701
+ <?php
702
+
703
+ }
704
+
705
+ echo '<p style="padding:6px 0 4px;">'."\n";
706
+ echo '<label><input type="checkbox" name="post_types[]" value="'.$post_type->name.'"'.(in_array($post_type->name,$current) ? ' checked="checked"' : '').' /> '.sprintf(__('Show with %s'),$post_type->labels->all_items).'</label>'."\n";
707
+ echo '</p>'."\n";
708
+
709
+ }
710
+
711
+ /**
712
+ *
713
+ * Hide some meta boxes from start
714
+ *
715
+ */
716
+ function change_default_hidden( $hidden, $screen ) {
717
+
718
+ if ($screen->base == 'sidebar' && get_user_option( 'metaboxhidden_sidebar' ) === false) {
719
+
720
+ $hidden_meta_boxes = array('pageparentdiv','ca-sidebar-tax-post_format','ca-sidebar-post-type-attachment');
721
+ $hidden = array_merge($hidden,$hidden_meta_boxes);
722
+
723
+ $user = wp_get_current_user();
724
+ update_user_option( $user->ID, 'metaboxhidden_sidebar', $hidden, true );
725
+
726
+ }
727
+ return $hidden;
728
+ }
729
+
730
  /**
731
  *
732
  * Create form fields
738
  ?>
739
  <table class="form-table">
740
  <?php
741
+ if(!empty($array)) {
742
+ $array = array_intersect_key($this->settings,array_keys($array));
743
+ } else {
744
  $array = $this->settings;
745
+ unset($array['taxonomies']);
746
+ unset($array['post_types']);
747
+ }
748
+
749
  foreach($array as $setting) :
750
 
751
  $meta = get_post_meta($post->ID, $setting['id'], true);
797
  */
798
  public function save_post($post_id) {
799
 
800
+ // Save button pressed
801
+ if(!isset($_POST['original_publish']) && !isset($_POST['save_post']))
802
+ return;
803
+
804
  // Only sidebar type
805
  if(get_post_type($post_id) != 'sidebar')
806
+ return;
 
 
 
 
807
 
808
  // Verify nonce
809
  if (!check_admin_referer(basename(__FILE__),'_ca-sidebar-nonce'))
810
+ return;
811
 
812
  // Check permissions
813
  if (!current_user_can('edit_post', $post_id))
814
+ return;
815
 
816
  // Check autosave
817
  if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
818
+ return;
819
 
820
+ // Load metadata
821
  $this->init_metadata();
822
 
823
+ // Update metadata
824
  foreach ($this->settings as $field) {
825
  $old = get_post_meta($post_id, $field['id'], true);
826
  $new = isset($_POST[$field['id']]) ? $_POST[$field['id']] : '';
827
 
828
  if ($new != '' && $new != $old) {
829
  update_post_meta($post_id, $field['id'], $new);
830
+ } elseif ($new == '' && $old != '') {
831
  delete_post_meta($post_id, $field['id'], $old);
832
  }
833
  }
843
  flush_rewrite_rules();
844
  }
845
 
846
+ public function load_dependencies() {
847
+
848
+ require_once('walker.php');
849
+
850
+ }
851
+
852
  }
853
 
854
  // Launch plugin
icon-16.png ADDED
Binary file
icon-32.png ADDED
Binary file
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, taxonomy, term, archive
5
  Requires at least: 3.1
6
  Tested up to: 3.2.1
7
- Stable tag: 0.5
8
 
9
  Manage and show sidebars according to the content being viewed.
10
 
@@ -15,10 +15,12 @@ No extra database tables or table columns will be added.
15
 
16
  = Features =
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
- * Show sidebars with search, 404 page and front page
 
 
22
  * Merge new sidebars with others, replace them or simply add them to your theme manually
23
  * Create complex content with nested sidebars
24
  * Private sidebars only for members
@@ -26,7 +28,7 @@ No extra database tables or table columns will be added.
26
 
27
  = Translations =
28
 
29
- Content Aware Sidebars can now be translated. If you want to help, please send your translation to jv[at]intox.dk!
30
 
31
  = Contact =
32
 
@@ -49,13 +51,29 @@ You are.
49
 
50
  == Screenshots ==
51
 
52
- 1. Add a new sidebar `For Pages` visible on all pages, replacing `Main Sidebar`
53
- 2. Add widgets to `For Pages`
54
- 3. Viewing home page. `Main Sidebar` is visible
55
- 4. Viewing a page. `For Pages` has replaced `Main Sidebar`
 
56
 
57
  == Changelog ==
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  = 0.5 =
60
 
61
  * Added: search, 404, front page rules now supported
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, singular
5
  Requires at least: 3.1
6
  Tested up to: 3.2.1
7
+ Stable tag: 0.6.2
8
 
9
  Manage and show sidebars according to the content being viewed.
10
 
15
 
16
  = Features =
17
 
18
+ * Show sidebars with:
19
+ * Specific singulars
20
+ * Specific post types
21
+ * Singulars containing specific taxonomies or taxonomy terms
22
+ * Specific post type archives, taxonomy archives or taxonomy term archives
23
+ * Search results, 404 page and front page
24
  * Merge new sidebars with others, replace them or simply add them to your theme manually
25
  * Create complex content with nested sidebars
26
  * Private sidebars only for members
28
 
29
  = Translations =
30
 
31
+ Content Aware Sidebars can now be translated. If you want to help, please contact jv[at]intox.dk.
32
 
33
  = Contact =
34
 
51
 
52
  == Screenshots ==
53
 
54
+ 1. Add a new content aware sidebar visible with all pages, search result and a specific category. It replaces `Main Sidebar`
55
+ 2. Simple overview of current content aware sidebars
56
+ 3. Add widgets to our new sidebar
57
+ 4. Viewing a static front page. `Main Sidebar` is visible
58
+ 5. Viewing a page. The content aware sidebar has replaced `Main Sidebar`
59
 
60
  == Changelog ==
61
 
62
+ = 0.6.2 =
63
+
64
+ * Fixed: array_flip triggered type mismatch errors in some cases
65
+
66
+ = 0.6.1 =
67
+
68
+ * Fixed: an image caused headers already sent error
69
+
70
+ = 0.6 =
71
+
72
+ * Added: sidebars can be set with specific singulars
73
+ * Added: sidebars can be set with specific post formats
74
+ * Added: updated gui
75
+ * Fixed: draft sidebars save meta
76
+
77
  = 0.5 =
78
 
79
  * Added: search, 404, front page rules now supported
screenshot-1.png CHANGED
Binary file
screenshot-2.png CHANGED
Binary file
screenshot-3.png CHANGED
Binary file
screenshot-4.png CHANGED
Binary file
screenshot-5.png ADDED
Binary file
walker.php ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * TODO: Clean this mess...
5
+ */
6
+
7
+ /**
8
+ *
9
+ * Custom Walker Class
10
+ *
11
+ */
12
+ class CAS_Walker_Tax_Checklist extends Walker {
13
+ var $tree_type = 'category';
14
+ var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
15
+
16
+ public function start_lvl(&$output, $depth, $args) {
17
+ $indent = str_repeat("\t", $depth);
18
+ $output .= "$indent<ul class='children'>\n";
19
+ }
20
+
21
+ public function end_lvl(&$output, $depth, $args) {
22
+ $indent = str_repeat("\t", $depth);
23
+ $output .= "$indent</ul>\n";
24
+ }
25
+
26
+ public function start_el(&$output, $term, $depth, $args) {
27
+ extract($args);
28
+
29
+ if ( empty($taxonomy) ) {
30
+ $output .= "\n<li>";
31
+ return;
32
+ }
33
+
34
+ $name = $taxonomy->name == 'category' ? 'post_category' : 'tax_input['.$taxonomy->name.']';
35
+ $value = $taxonomy->hierarchical ? 'term_id' : 'slug';
36
+ $class = in_array( $term->term_id, $popular_cats ) ? ' class="popular-category"' : '';
37
+
38
+ $output .= "\n".'<li id="'.$taxonomy->name.'-'.$term->term_id.'"$class><label class="selectit"><input value="'.$term->$value.'" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy->name.'-'.$term->term_id.'"'.checked(in_array($term->term_id,$selected_cats),true,false).disabled(empty($disabled),false,false).'/>'.esc_html( apply_filters('the_category', $term->name )) . '</label>';
39
+ }
40
+
41
+ public function end_el(&$output, $term, $depth, $args) {
42
+ $output .= "</li>\n";
43
+ }
44
+ }
45
+
46
+ /**
47
+ *
48
+ * Custom Walker Class
49
+ *
50
+ */
51
+ class CAS_Walker_Post_Checklist extends Walker {
52
+ var $tree_type = 'post';
53
+ var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');
54
+
55
+ public function start_lvl(&$output, $depth, $args) {
56
+ $indent = str_repeat("\t", $depth);
57
+ $output .= "$indent<ul class='children'>\n";
58
+ }
59
+
60
+ public function end_lvl(&$output, $depth, $args) {
61
+ $indent = str_repeat("\t", $depth);
62
+ $output .= "$indent</ul>\n";
63
+ }
64
+
65
+ public function start_el(&$output, $term, $depth, $args) {
66
+ extract($args);
67
+
68
+ if ( empty($post_type) ) {
69
+ $output .= "\n<li>";
70
+ return;
71
+ }
72
+
73
+ $output .= "\n".'<li id="'.$post_type->name.'-'.$term->ID.'"><label class="selectit"><input value="'.$term->ID.'" type="checkbox" name="post_types[]" id="in-'.$post_type->name.'-'.$term->ID.'"'.checked( in_array($term->ID,$selected_cats),true,false).disabled(empty($disabled),false,false).'/>'.esc_html( $term->post_title ) . '</label>';
74
+ }
75
+
76
+ public function end_el(&$output, $term, $depth, $args) {
77
+ $output .= "</li>\n";
78
+ }
79
+ }
80
+
81
+ /**
82
+ *
83
+ * Show terms checklist
84
+ *
85
+ */
86
+ function cas_terms_checklist($post_id = 0, $args = array()) {
87
+ $defaults = array(
88
+ 'descendants_and_self' => 0,
89
+ 'selected_cats' => false,
90
+ 'popular_cats' => false,
91
+ 'walker' => null,
92
+ 'taxonomy' => 'category',
93
+ 'checked_ontop' => true
94
+ );
95
+ extract( wp_parse_args($args, $defaults), EXTR_SKIP );
96
+
97
+ if ( empty($walker) || !is_a($walker, 'Walker') )
98
+ $walker = new CAS_Walker_Tax_Checklist();
99
+
100
+ $descendants_and_self = (int) $descendants_and_self;
101
+
102
+ $tax = get_taxonomy($taxonomy);
103
+
104
+ $args = array('taxonomy' => $tax);
105
+ $args['disabled'] = !current_user_can($tax->cap->assign_terms);
106
+
107
+ if ( is_array( $selected_cats ) )
108
+ $args['selected_cats'] = $selected_cats;
109
+ elseif ( $post_id )
110
+ $args['selected_cats'] = wp_get_object_terms($post_id, $taxonomy, array_merge($args, array('fields' => 'ids')));
111
+ else
112
+ $args['selected_cats'] = array();
113
+
114
+ if ( is_array( $popular_cats ) )
115
+ $args['popular_cats'] = $popular_cats;
116
+ else
117
+ $args['popular_cats'] = get_terms( $taxonomy, array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
118
+
119
+ if ( $descendants_and_self ) {
120
+ $categories = (array) get_terms($taxonomy, array( 'child_of' => $descendants_and_self, 'hierarchical' => 0, 'hide_empty' => 0 ) );
121
+ $self = get_term( $descendants_and_self, $taxonomy );
122
+ array_unshift( $categories, $self );
123
+ } else {
124
+ $categories = (array) get_terms($taxonomy, array('get' => 'all'));
125
+ }
126
+
127
+ if ( $checked_ontop ) {
128
+ // Post process $categories rather than adding an exclude to the get_terms() query to keep the query the same across all posts (for any query cache)
129
+ $checked_categories = array();
130
+ $keys = array_keys( $categories );
131
+
132
+ foreach( $keys as $k ) {
133
+ if ( in_array( $categories[$k]->term_id, $args['selected_cats'] ) ) {
134
+ $checked_categories[] = $categories[$k];
135
+ unset( $categories[$k] );
136
+ }
137
+ }
138
+
139
+ // Put checked cats on top
140
+ echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args));
141
+ }
142
+ // Then the rest of them
143
+ echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args));
144
+ }
145
+
146
+ /**
147
+ *
148
+ * Show checklist for popular terms
149
+ *
150
+ */
151
+ function cas_popular_terms_checklist( $taxonomy, $default = 0, $number = 10, $echo = true ) {
152
+ global $post_ID;
153
+
154
+ if ( $post_ID )
155
+ $checked_terms = wp_get_object_terms($post_ID, $taxonomy->name, array('fields'=>'ids'));
156
+ else
157
+ $checked_terms = array();
158
+
159
+ $terms = get_terms( $taxonomy->name, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => $number, 'hierarchical' => false ) );
160
+
161
+ $disabled = current_user_can($taxonomy->cap->assign_terms) ? '' : ' disabled="disabled"';
162
+
163
+ $popular_ids = array();
164
+ foreach ( (array) $terms as $term ) {
165
+ $popular_ids[] = $term->term_id;
166
+ if ( !$echo ) // hack for AJAX use
167
+ continue;
168
+ $id = "popular-$taxonomy->name-$term->term_id";
169
+ ?>
170
+
171
+ <li id="<?php echo $id; ?>" class="popular-category">
172
+ <label class="selectit">
173
+ <input id="in-<?php echo $id; ?>" type="checkbox"<?php echo in_array( $term->term_id, $checked_terms ) ? ' checked="checked"' : ''; ?> value="<?php echo $term->term_id; ?>"<?php echo $disabled ?>/>
174
+ <?php echo esc_html( apply_filters( 'the_category', $term->name ) ); ?>
175
+ </label>
176
+ </li>
177
+
178
+ <?php
179
+ }
180
+ return $popular_ids;
181
+ }
182
+
183
+ /**
184
+ *
185
+ * Show terms checklist
186
+ *
187
+ */
188
+ function cas_posts_checklist($post_id = 0, $args = array()) {
189
+ $defaults = array(
190
+ 'descendants_and_self' => 0,
191
+ 'selected_cats' => false,
192
+ 'walker' => null,
193
+ 'post_type' => 'post',
194
+ 'checked_ontop' => true
195
+ );
196
+ extract( wp_parse_args($args, $defaults), EXTR_SKIP );
197
+
198
+ if ( empty($walker) || !is_a($walker, 'Walker') )
199
+ $walker = new CAS_Walker_Post_Checklist();
200
+
201
+ $descendants_and_self = (int) $descendants_and_self;
202
+
203
+ $ptype = get_post_type_object($post_type);
204
+
205
+ $args = array('post_type' => $ptype);
206
+ $args['disabled'] = !current_user_can($ptype->cap->edit_post);
207
+
208
+ if ( is_array( $selected_cats ) )
209
+ $args['selected_cats'] = $selected_cats;
210
+ elseif ( $post_id )
211
+ $args['selected_cats'] = (array)get_post_meta($post_id, 'post_types', true);
212
+ else
213
+ $args['selected_cats'] = array();
214
+
215
+ if ( $descendants_and_self ) {
216
+ $categories = (array) get_posts(array('post_type'=>$post_type,'post_parent' => $descendants_and_self, 'numberposts'=>-1) );
217
+ $self = get_post($descendants_and_self);
218
+ array_unshift($categories, $self);
219
+ } else {
220
+ $categories = (array) get_posts(array('post_type'=>$post_type, 'numberposts'=>-1));
221
+ }
222
+
223
+ if ( $checked_ontop ) {
224
+ //Post process $categories rather than adding an exclude to the get_terms() query to keep the query the same across all posts (for any query cache)
225
+ $checked_categories = array();
226
+ $keys = array_keys( $categories );
227
+
228
+ foreach( $keys as $k ) {
229
+ if ( in_array( $categories[$k]->ID, $args['selected_cats'] ) ) {
230
+ $checked_categories[] = $categories[$k];
231
+ unset( $categories[$k] );
232
+ }
233
+ elseif($post_type == 'page' && 'page' == get_option( 'show_on_front') && (get_option( 'page_on_front' ) == $categories[$k]->ID || get_option('page_for_posts') == $categories[$k]->ID))
234
+ unset( $categories[$k] );
235
+ }
236
+
237
+ //Put checked cats on top
238
+ echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args));
239
+ }
240
+ // Then the rest of them
241
+ echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args));
242
+ }
243
+
244
+ ?>