public
Description: A wordpress sidebar widget to show latest post from a specific category
Homepage: http://veilleperso.com
Clone URL: git://github.com/veilleperso/or_latests_posts.git
Search Repo:
veilleperso (author)
Wed May 07 05:45:38 -0700 2008
commit  b06ce555979706259b56d753ef8b490094690154
tree    6dbae8f3294ddf0ee5d5bbf87200303982541367
parent  cb9fbdbc396dfb9ef7cec5cbf7414137615533e0
or_latests_posts / latest_posts_new.php
100644 170 lines (139 sloc) 7.367 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
function or_flush_latest_posts() {
  $options = get_option('or_widget_latest_posts');
  for ($i=1; $i<= $options['widgets_count']; $i++) wp_cache_delete("or_latest_posts_$i");
}
add_action('save_post', 'or_flush_latest_posts');
add_action('deleted_post', 'or_flush_latest_posts');
 
function or_latest_posts_widget($args, $widget_args = 1) {
  if (!(is_home() || is_page() || is_single())) return '';
 
  extract( $args, EXTR_SKIP );
  if ( is_numeric($widget_args) ) $widget_args = array( 'number' => $widget_args );
  $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  extract( $widget_args, EXTR_SKIP );
  
  if ( $output = wp_cache_get("or_latest_posts_$number") ) return print($output);
 
  ob_start();
  $options = get_option('or_widget_latest_posts');
  if ( !isset($options[$number]) ) return;
  
  $catID = empty($options[$number]['cat']) ? 1 : $options[$number]['cat'];
  if ( empty($options[$number]['title']) ) {
    $categoryInfo = get_category($catID);
    $title = $categoryInfo->name;
  } else {
    $title = $options[$number]['title'];
  }
  
  $cat_link = get_category_link($catID);
 
  if ($options[$number]['show_rss']) {
   $feed_url = "<a href=\"$cat_link/feed\" title=\"Flux de $title\"><img src=\"". get_bloginfo('template_url'). "/images/feed.gif\" alt=\"RSS\"/></a>";
    $title .= " $feed_url";
  }
  
  $more_title = $options[$number]['more'];
  if ($more_title!='') $more_link = "<li><br/><a href=\"$cat_link\">$more_title</a></li>";
  else $more_link = "";
  
  if ( !$nb = (int) $options[$number]['number'] ) $nb = 10;
  else if ( $nb < 1 ) $nb = 1;
  else if ( $nb > 15 ) $nb = 15;
 
  $r = new WP_Query("showposts=$nb&what_to_show=posts&nopaging=0&post_status=publish&cat=$catID");
  $i = 0;
  if ($r->have_posts()) :
?>
    <?php echo $before_widget; ?>
      <?php echo $before_title . $title . $after_title; ?>
      <ul>
      <?php while ($r->have_posts()) : $r->the_post(); ?>
      <li><a href="<?php the_permalink() ?>"><?php the_title();?></a></li>
    <?php
     // security because sometime bugs occurs (why?)
     $i = $i + 1;
     if ($i>$nb or $i>15) break;
    ?>
      <?php endwhile; ?>
      <?php echo $more_link; ?>
      </ul>
    <?php echo $after_widget; ?>
<?php
  endif;
  wp_cache_add("or_latest_posts_$number", ob_get_flush());
}
 
function or_latest_posts_widget_control($widget_args = 1 ) {
  global $wp_registered_widgets;
  static $updated = false; // Whether or not we have already updated the data after a POST submit
 
  if ( is_numeric($widget_args) ) $widget_args = array( 'number' => $widget_args );
  $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  extract( $widget_args, EXTR_SKIP );
 
  // Data should be stored as array: array( number => data for that instance of the widget, ... )
  $options = get_option('or_widget_latest_posts');
  if ( !is_array($options) ) $options = array();
 
  // We need to update the data
  if ( !$updated && !empty($_POST['sidebar']) ) {
    // Tells us what sidebar to put the data in
    $sidebar = (string) $_POST['sidebar'];
 
    $sidebars_widgets = wp_get_sidebars_widgets();
    if ( isset($sidebars_widgets[$sidebar]) ) $this_sidebar =& $sidebars_widgets[$sidebar];
    else $this_sidebar = array();
 
    foreach ( $this_sidebar as $_widget_id ) {
      // Remove all widgets of this type from the sidebar.
      // We'll add the new data in a second. This makes sure we don't get any duplicate data
      // since widget ids aren't necessarily persistent across multiple updates
      if ( 'or-latest-posts' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
        $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
        if ( !in_array( "or-latest-posts-$widget_number", $_POST['widget-id'] ) ) unset($options[$widget_number]);
      }
    }
 
    foreach ( (array) $_POST['or-latest-posts'] as $widget_number => $widget_instance ) {
      $data = array();
      $data['title'] = strip_tags(stripslashes($widget_instance["title"]));
      $data['cat'] = (int) $widget_instance["cat"];
      $data['number'] = (int) $widget_instance["number"];
      $data['show_rss'] = (int) $widget_instance["rss"];
      $data['more'] = strip_tags(stripslashes($widget_instance["more"]));
      $options[$widget_number] = $data;
    }
 
    update_option('or_widget_latest_posts', $options);
    or_flush_latest_posts();
    $updated = true; // So that we don't go through this more than once
  }
 
  if ( -1 == $number ) {
   // We echo out a template for a form which can be converted to a specific form later via JS
    $number = '%i%';
  }
  if ( !$nb = (int) $options[$number]['number'] ) $nb = 5;
  
  
  $id = "or-latest-posts-title-$number";
  $value = attribute_escape($options[$number]['title']);
  echo '<p><label for="'. $id. '">'. __('Title'). ': <input style="width: 200px;" id="'. $id. '" name="or-latest-posts['. $number. '][title]" type="text" value="'. $value. '" /></label></p>';
 
  echo '<p><label>Posts in ';
  wp_dropdown_categories(array('name'=> "or-latest-posts[$number][cat]", 'selected'=>$options[$number]['cat'], 'hide_empty'=>0));
  echo '</label></p>';
 
  $id = "or-latest-posts-number-$number";
  echo '<p><label for="'. $id. '">'. __('Number of posts to show'). ': <input size = 4 id="'. $id. '" name="or-latest-posts['. $number. '][number]" type="text" value="'. $nb. '" /></label> '. __('(max 15)'). '</p>';
 
  $id = "or-latest-posts-rss-$number";
  echo '<p><label for="'. $id. '">'. __('Show RSS link'). ': <input type="checkbox" id="'. $id. '" name="or-latest-posts['. $number. '][rss]" value="1"'.($options[$number]['show_rss'] ? ' checked' : '').' /></label></p>';
 
  $id = "or-latest-posts-more-$number";
  $value = attribute_escape($options[$number]['more']);
  echo '<p><label for="'. $id. '">'. __('More link title'). ': <input style="width: 200px;" id="'. $id. '" name="or-latest-posts['. $number. '][more]" type="text" value="'. $value. '" /></label></p>';
 
  echo '<input type="hidden" id="or-latest-posts-submit-'. $number .'" name="or-latest-posts['. $number. '][submit]" value="1" />';
}
 
// Registers the widget(s).
function or_latest_posts_widget_register() {
  if ( !$options = get_option('or_widget_latest_posts') ) $options = array();
  $widget_ops = array('classname' => 'widget_or_latest_posts', 'description' => __('Show latest post from a specific category'));
  $control_ops = array('width' => 300, 'height' => 250, 'id_base' => 'or_latest_posts');
  $name = __('Latest Posts');
  
  $registered = false;
  foreach ( array_keys($options) as $o ) {
    // Old widgets can have null values for some reason
    if ( !isset($options[$o]['title']) ) continue;
 
    // $id should look like {$id_base}-{$o}
    $id = "or_latest_posts-$o"; // Never never never translate an id
    $registered = true;
    wp_register_sidebar_widget( $id, $name, 'or_latest_posts_widget', $widget_ops, array( 'number' => $o ) );
    wp_register_widget_control( $id, $name, 'or_latest_posts_widget_control', $control_ops, array( 'number' => $o ) );
  }
 
  // If there are none, we register the widget's existance with a generic template
  if ( !$registered ) {
    wp_register_sidebar_widget( 'many-1', $name, 'or_latest_posts_widget', $widget_ops, array( 'number' => -1 ) );
    wp_register_widget_control( 'many-1', $name, 'or_latest_posts_widget_control', $control_ops, array( 'number' => -1 ) );
  }
}
 
add_action('widgets_init', 'or_latest_posts_widget_register');
?>