|
1 <?php |
|
2 /* |
|
3 Plugin Name: xili-language widget |
|
4 Plugin URI: http://dev.xiligroup.com/xili-language/ |
|
5 Description: This plugin is a part of xili-language plugin. Add a optional widgets to display list of languages in the sidebar or recent comments and recents posts (since 2.8.0) |
|
6 Author: dev.xiligroup.com - MS |
|
7 Version: 1.4.1 |
|
8 Author URI: http://dev.xiligroup.com |
|
9 */ |
|
10 |
|
11 # 100219 - add new widget recent posts if WP >= 2.8.0 |
|
12 # 090606 - xili-language list widget is now multiple and more features |
|
13 # 090518 - new widget for recent comments |
|
14 # 090404 - new registering way. |
|
15 # 090325 - better options record. |
|
16 |
|
17 /* thanks to http://blog.zen-dreams.com/ tutorial |
|
18 |
|
19 Copyright 2009-10 dev.xiligroup.com |
|
20 |
|
21 This program is free software; you can redistribute it and/or modify |
|
22 it under the terms of the GNU General Public License as published by |
|
23 the Free Software Foundation; either version 2 of the License, or |
|
24 (at your option) any later version. |
|
25 |
|
26 This program is distributed in the hope that it will be useful, |
|
27 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
29 GNU General Public License for more details. |
|
30 |
|
31 You should have received a copy of the GNU General Public License |
|
32 along with this program; if not, write to the Free Software |
|
33 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
34 */ |
|
35 |
|
36 /** |
|
37 * Recent_Posts widget class |
|
38 * rewritten from default WP widget to suppress wp_reset_query and add sub-selection by language (current or forced) |
|
39 * @since 1.4.0 |
|
40 */ |
|
41 class xili_Widget_Recent_Posts extends WP_Widget { |
|
42 |
|
43 function xili_Widget_Recent_Posts() { |
|
44 $widget_ops = array('classname' => 'xili_widget_recent_entries', 'description' => __( "The most recent posts on your blog by xili-language",'xili-language-widget') ); |
|
45 $this->WP_Widget('xili-recent-posts', __('List of recent posts','xili-language-widget'), $widget_ops); |
|
46 $this->alt_option_name = 'xili_widget_recent_entries'; |
|
47 |
|
48 add_action( 'save_post', array(&$this, 'flush_widget_cache') ); |
|
49 add_action( 'deleted_post', array(&$this, 'flush_widget_cache') ); |
|
50 add_action( 'switch_theme', array(&$this, 'flush_widget_cache') ); |
|
51 } |
|
52 |
|
53 function widget($args, $instance) { |
|
54 $cache = wp_cache_get('xili_widget_recent_posts', 'widget'); |
|
55 |
|
56 if ( !is_array($cache) ) |
|
57 $cache = array(); |
|
58 |
|
59 if ( isset($cache[$args['widget_id']]) ) { |
|
60 echo $cache[$args['widget_id']]; |
|
61 return; |
|
62 } |
|
63 |
|
64 ob_start(); |
|
65 extract($args); |
|
66 |
|
67 $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title']); |
|
68 if ( !$number = (int) $instance['number'] ) |
|
69 $number = 10; |
|
70 else if ( $number < 1 ) |
|
71 $number = 1; |
|
72 else if ( $number > 15 ) |
|
73 $number = 15; |
|
74 |
|
75 $the_lang = $instance['the_lang']; |
|
76 |
|
77 if (class_exists('xili_language')) { |
|
78 global $wp_query; |
|
79 $tmp_query = $wp_query->query_vars[QUETAG] ; $wp_query->query_vars[QUETAG] = ""; |
|
80 if ($the_lang == '') |
|
81 $thequery = array('showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1); |
|
82 else if ($the_lang == '*') |
|
83 $thequery = array ('showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1, QUETAG => the_curlang()); |
|
84 else |
|
85 $thequery = array ('showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1, QUETAG => $the_lang); |
|
86 |
|
87 add_action('parse_query','xiliml_add_lang_to_parsed_query'); |
|
88 $r = new WP_Query($thequery); |
|
89 remove_filter('parse_query','xiliml_add_lang_to_parsed_query'); |
|
90 $wp_query->query_vars[QUETAG] = $tmp_query; |
|
91 } else { |
|
92 $thequery = array('showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1); |
|
93 $r = new WP_Query($thequery); |
|
94 } |
|
95 |
|
96 if ($r->have_posts()) : |
|
97 ?> |
|
98 <?php echo $before_widget; ?> |
|
99 <?php if ( $title ) echo $before_title . $title . $after_title; ?> |
|
100 <ul> |
|
101 <?php while ($r->have_posts()) : $r->the_post(); ?> |
|
102 <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li> |
|
103 <?php endwhile; ?> |
|
104 </ul> |
|
105 <?php echo $after_widget; ?> |
|
106 <?php |
|
107 //wp_reset_query(); // Restore global post data stomped by the_post(). |
|
108 endif; |
|
109 |
|
110 $cache[$args['widget_id']] = ob_get_flush(); |
|
111 wp_cache_add('xili_widget_recent_posts', $cache, 'widget'); |
|
112 } |
|
113 |
|
114 function update( $new_instance, $old_instance ) { |
|
115 $instance = $old_instance; |
|
116 $instance['title'] = strip_tags($new_instance['title']); |
|
117 $instance['the_lang'] = strtolower($new_instance['the_lang']); |
|
118 $instance['number'] = (int) $new_instance['number']; |
|
119 $this->flush_widget_cache(); |
|
120 |
|
121 $alloptions = wp_cache_get( 'alloptions', 'options' ); |
|
122 if ( isset($alloptions['xili_widget_recent_entries']) ) |
|
123 delete_option('xili_widget_recent_entries'); |
|
124 |
|
125 return $instance; |
|
126 } |
|
127 |
|
128 function flush_widget_cache() { |
|
129 wp_cache_delete('xili_widget_recent_posts', 'widget'); |
|
130 } |
|
131 |
|
132 function form( $instance ) { |
|
133 $title = isset($instance['title']) ? esc_attr($instance['title']) : ''; |
|
134 $the_lang = isset($instance['the_lang']) ? strtolower($instance['the_lang']) : ''; |
|
135 if ( !isset($instance['number']) || !$number = (int) $instance['number'] ) |
|
136 $number = 5; |
|
137 ?> |
|
138 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> |
|
139 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p> |
|
140 |
|
141 <?php if (class_exists('xili_language')) { global $xili_language; ?> |
|
142 <p> |
|
143 <label for="<?php echo $this->get_field_id('the_lang'); ?>"><?php _e('Language:','xili-language-widget'); ?></label> |
|
144 <select name="<?php echo $this->get_field_name('the_lang'); ?>" id="<?php echo $this->get_field_id('the_lang'); ?>" class="widefat"> |
|
145 <option value=""<?php selected( $instance['the_lang'], '' ); ?>><?php _e('All languages','xili-language-widget'); ?></option> |
|
146 <option value="*"<?php selected( $instance['the_lang'], '*' ); ?>><?php _e('Current language','xili-language-widget'); ?></option> |
|
147 <?php $listlanguages = get_terms_of_groups_lite ($xili_language->langs_group_id,TAXOLANGSGROUP,TAXONAME,'ASC'); |
|
148 foreach ($listlanguages as $language) { ?> |
|
149 <option value="<?php echo $language->slug ?>"<?php selected( $instance['the_lang'], $language->slug ); ?>><?php _e($language->description,'xili-language-widget'); ?></option> |
|
150 |
|
151 <?php } /* end */ |
|
152 ?> |
|
153 </select> |
|
154 </p> |
|
155 <?php } ?> |
|
156 |
|
157 <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label> |
|
158 <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /><br /> |
|
159 <small><?php _e('(at most 15)'); ?></small></p> |
|
160 <p><small>© xili-language</small></p> |
|
161 <?php |
|
162 } |
|
163 } |
|
164 |
|
165 |
|
166 /* |
|
167 * Adapted Recent Comments Widget (original in widget.php is not multilingual ready) |
|
168 * |
|
169 *@since 0.9.9.4 |
|
170 * |
|
171 * |
|
172 */ |
|
173 |
|
174 class xili_recent_comments_Widget { |
|
175 |
|
176 function xili_recent_comments_Widget() { |
|
177 load_plugin_textdomain('xili-language-widget',PLUGINDIR.'/'.dirname(plugin_basename(__FILE__)), dirname(plugin_basename(__FILE__))); |
|
178 add_action('widgets_init', array(&$this, 'init_widget')); |
|
179 } |
|
180 |
|
181 function init_widget() { |
|
182 if ( !function_exists('wp_register_sidebar_widget') || !function_exists('wp_register_widget_control') ) |
|
183 return; |
|
184 $widget_ops = array('classname' => 'xili-language_recent_comments_Widget', 'description' => __( "List of recent comments by xili-language plugin",'xili-language-widget') ); |
|
185 wp_register_sidebar_widget('xili-language_recent_comments_Widget',__('List of recent comments','xili-language-widget'),array(&$this, 'widget'),$widget_ops); |
|
186 wp_register_widget_control('xili-language_recent_comments_Widget',__('List of recent comments','xili-language-widget'),array(&$this, 'widget_options'),$widget_ops); |
|
187 |
|
188 } |
|
189 |
|
190 function widget($args) { |
|
191 global $comments, $comment; |
|
192 extract($args, EXTR_SKIP); |
|
193 $options = get_option('xili_language_recent_comments'); |
|
194 $title = empty($options['title']) ? __('Recent Comments',THEME_TEXTDOMAIN) : apply_filters('widget_title', $options['title']); |
|
195 if ( !$number = (int) $options['number'] ) |
|
196 $number = 5; |
|
197 else if ( $number < 1 ) |
|
198 $number = 1; |
|
199 else if ( $number > 15 ) |
|
200 $number = 15; |
|
201 /* if xili-language plugin is activated */ |
|
202 if (function_exists('xiliml_recent_comments')) |
|
203 $comments = xiliml_recent_comments($number); |
|
204 /* */ |
|
205 echo $before_widget; ?> |
|
206 <?php echo $before_title . $title . $after_title; ?> |
|
207 <ul id="recentcomments"><?php |
|
208 if ( $comments ) : foreach ( (array) $comments as $comment) : |
|
209 echo '<li class="recentcomments">' . sprintf(__('%1$s on %2$s',THEME_TEXTDOMAIN), get_comment_author_link(), '<a href="' . clean_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>'; |
|
210 endforeach; endif;?></ul> |
|
211 <?php echo $after_widget; |
|
212 } |
|
213 |
|
214 function widget_options() { |
|
215 $options = $newoptions = get_option('xili_language_recent_comments'); |
|
216 if ( isset($_POST["xl_recent-comments-submit"]) ) { |
|
217 $newoptions['title'] = strip_tags(stripslashes($_POST["xl_recent-comments-title"])); |
|
218 $newoptions['number'] = (int) $_POST["xl_recent-comments-number"]; |
|
219 } |
|
220 if ( $options != $newoptions ) { |
|
221 $options = $newoptions; |
|
222 update_option('xili_language_recent_comments', $options); |
|
223 wp_delete_recent_comments_cache(); |
|
224 } |
|
225 $title = attribute_escape($options['title']); |
|
226 if ( !$number = (int) $options['number'] ) |
|
227 $number = 5; |
|
228 ?> |
|
229 <p><label for="xl_recent-comments-title"><?php _e('Title:'); ?> <input class="widefat" id="xl_recent-comments-title" name="xl_recent-comments-title" type="text" value="<?php echo $title; ?>" /></label></p> |
|
230 <p> |
|
231 <label for="xl_recent-comments-number"><?php _e('Number of comments to show:'); ?> <input style="width: 25px; text-align: center;" id="xl_recent-comments-number" name="xl_recent-comments-number" type="text" value="<?php echo $number; ?>" /></label> |
|
232 <br /> |
|
233 <small><?php _e('(at most 15)'); ?></small> |
|
234 </p> |
|
235 <input type="hidden" id="xl_recent-comments-submit" name="xl_recent-comments-submit" value="1" /> |
|
236 <?php |
|
237 } |
|
238 } |
|
239 |
|
240 /* |
|
241 * class for multiple xili-language widgets |
|
242 * @since 0.9.9.6 |
|
243 * |
|
244 */ |
|
245 class xili_language_Widgets { |
|
246 |
|
247 function xili_language_Widgets () { |
|
248 load_plugin_textdomain('xili-language-widget',PLUGINDIR.'/'.dirname(plugin_basename(__FILE__)), dirname(plugin_basename(__FILE__))); |
|
249 add_action('widgets_init', array(&$this, 'init_widget')); |
|
250 } |
|
251 |
|
252 function init_widget() { |
|
253 if ( !function_exists('wp_register_sidebar_widget') || !function_exists('wp_register_widget_control') ) |
|
254 return; |
|
255 if (!$options = get_option('xili_language_widgets_options')) |
|
256 $options = array(); |
|
257 $widget_ops = array('classname' => 'xili-language_Widgets', 'description' => __( "List of available languages by xili-language plugin",'xili-language-widget') ); |
|
258 $control_ops = array('id_base' => 'xili_language_widgets'); |
|
259 $name = __('Languages list','xili-language-widget'); |
|
260 |
|
261 $id = false; |
|
262 foreach ( (array) array_keys($options) as $o ) { |
|
263 $id = "xili_language_widgets-$o"; // Never never never translate an id |
|
264 wp_register_sidebar_widget($id, $name, array(&$this, 'widget'), $widget_ops, array( 'number' => $o )); |
|
265 wp_register_widget_control($id, $name, array(&$this, 'widget_options'), $control_ops, array( 'number' => $o )); |
|
266 } |
|
267 |
|
268 // If there are none, we register the widget's existance with a generic template |
|
269 if ( !$id ) { |
|
270 wp_register_sidebar_widget( 'xili_language_widgets-1', $name, array(&$this, 'widget'), $widget_ops, array( 'number' => -1 ) ); |
|
271 wp_register_widget_control( 'xili_language_widgets-1', $name, array(&$this, 'widget_options'), $control_ops, array( 'number' => -1 ) ); |
|
272 |
|
273 } |
|
274 |
|
275 } |
|
276 |
|
277 function widget($args, $widget_args = 1) { |
|
278 global $wpdb; |
|
279 |
|
280 $options = get_option('xili_language_widgets_options'); |
|
281 extract($args); |
|
282 |
|
283 if ( is_numeric($widget_args) ) |
|
284 $widget_args = array( 'number' => $widget_args ); |
|
285 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) ); |
|
286 extract( $widget_args, EXTR_SKIP ); |
|
287 |
|
288 if ( !isset($options[$number]) ) |
|
289 return; |
|
290 if ("" != $options[$number]['title']) { |
|
291 echo $before_widget.$before_title.__($options[$number]['title'],THEME_TEXTDOMAIN).$after_title; |
|
292 } else { |
|
293 echo $before_widget.$before_title.$aftertitle; |
|
294 } |
|
295 |
|
296 if (isset($options[$number]['beforelist'])) { |
|
297 $beforelist = stripslashes($options[$number]['beforelist']); |
|
298 $afterlist = stripslashes($options[$number]['afterlist']); |
|
299 } else { |
|
300 $beforelist = "<ul class='xililanguagelist'>"; |
|
301 $afterlist = '</ul>'; |
|
302 } |
|
303 if (isset($options[$number]['beforeline'])) { |
|
304 $beforeline = stripslashes($options[$number]['beforeline']); |
|
305 $afterline = stripslashes($options[$number]['afterline']); |
|
306 } else { |
|
307 $beforeline = '<li>'; |
|
308 $afterline = '</li>'; |
|
309 } |
|
310 $theoption = $options[$number]['theoption']; |
|
311 |
|
312 if (function_exists('xili_language_list')) { |
|
313 echo $beforelist; |
|
314 xili_language_list($beforeline, $afterline, $theoption); |
|
315 echo $afterlist; |
|
316 } |
|
317 echo $after_widget; |
|
318 } |
|
319 |
|
320 function widget_options($widget_args) { |
|
321 if ( is_numeric($widget_args) ) |
|
322 $widget_args = array( 'number' => $widget_args ); |
|
323 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) ); |
|
324 extract( $widget_args, EXTR_SKIP ); |
|
325 |
|
326 $options = get_option('xili_language_widgets_options'); |
|
327 if (!is_array($options)) |
|
328 $options = array(); |
|
329 |
|
330 if ( !$updated && !empty($_POST['sidebar']) ) { |
|
331 $sidebar = (string) $_POST['sidebar']; |
|
332 |
|
333 $sidebars_widgets = wp_get_sidebars_widgets(); |
|
334 if ( isset($sidebars_widgets[$sidebar]) ) |
|
335 $this_sidebar =& $sidebars_widgets[$sidebar]; |
|
336 else |
|
337 $this_sidebar = array(); |
|
338 |
|
339 foreach ( (array) $this_sidebar as $_widget_id ) { |
|
340 |
|
341 if ( 'xili-language_widgets' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) { |
|
342 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number']; |
|
343 if ( !in_array( "xili-language_widgets-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed. |
|
344 unset($options[$widget_number]); |
|
345 } |
|
346 } |
|
347 |
|
348 foreach ( (array) $_POST['xili-language_Widgets'] as $widget_number => $widget_text ) { |
|
349 if (isset($widget_text['submit'])) { |
|
350 $options[$widget_number]['title'] = strip_tags(stripslashes($widget_text['title'])); |
|
351 $options[$widget_number]['beforelist'] = $widget_text['beforelist'] ; |
|
352 $options[$widget_number]['beforeline'] = $widget_text['beforeline'] ; |
|
353 $options[$widget_number]['afterline'] = $widget_text['afterline'] ; |
|
354 $options[$widget_number]['afterlist'] = $widget_text['afterlist'] ; |
|
355 $options[$widget_number]['theoption'] = strip_tags(stripslashes($widget_text['theoption'])) ; |
|
356 |
|
357 } |
|
358 } |
|
359 update_option('xili_language_widgets_options',$options); |
|
360 $updated = true; |
|
361 } |
|
362 |
|
363 |
|
364 $options = get_option('xili_language_widgets_options'); |
|
365 |
|
366 if ( -1 == $number ) { |
|
367 $title = ''; |
|
368 $number = '%i%'; |
|
369 |
|
370 $beforelist = "<ul class='xililanguagelist'>"; |
|
371 $afterlist = '</ul>'; |
|
372 $beforeline = '<li>'; |
|
373 $afterline = '</li>'; |
|
374 $theoption = ''; |
|
375 |
|
376 } else { |
|
377 $title = attribute_escape($options[$number]['title']); |
|
378 $beforelist = htmlentities(stripslashes($options[$number]['beforelist'])); |
|
379 $beforeline = htmlentities(stripslashes($options[$number]['beforeline'])); |
|
380 $afterline = htmlentities(stripslashes($options[$number]['afterline'])); |
|
381 $afterlist = htmlentities(stripslashes($options[$number]['afterlist'])); |
|
382 $theoption = stripslashes($options[$number]['theoption']); |
|
383 } |
|
384 |
|
385 echo '<label for="xili_l_widget_title-'.$number.'">'.__('Title').': <input id="xili_l_widget_title-'.$number.'" name="xili-language_Widgets['.$number.'][title]" type="text" value="'.$title.'" /></label>'; |
|
386 // other option and list html tags |
|
387 echo '<br /><label for="xili_l_widget_theoption-'.$number.'">'.__('Type','xili-language-widget').': <input id="xili_l_widget_theoption-'.$number.'" name="xili-language_Widgets['.$number.'][theoption]" type="text" value="'.$theoption.'" /></label>'; |
|
388 |
|
389 echo '<fieldset style="margin:2px; padding:3px; border:1px solid #ccc;"><legend>'.__('HTML tags of list','xili-language-widget').'</legend>'; |
|
390 |
|
391 echo "<label for=\"xili_l_widget_beforelist-".$number."\">".__('before list','xili-language-widget').": <input id=\"xili_l_widget_beforelist-".$number."\" name=\"xili-language_Widgets[".$number."][beforelist]\" type=\"text\" value=\"".$beforelist."\" style=\"width:100%\" /></label><br />"; |
|
392 echo '<label for="xili_l_widget_beforeline-'.$number.'">'.__('before line','xili-language-widget').': <input id="xili_l_widget_beforeline-'.$number.'" name="xili-language_Widgets['.$number.'][beforeline]" type="text" value="'.$beforeline.'" style="width:100%" /></label><br />'; |
|
393 echo '<label for="xili_l_widget_afterline-'.$number.'">'.__('after line','xili-language-widget').': <input id="xili_l_widget_afterline-'.$number.'" name="xili-language_Widgets['.$number.'][afterline]" type="text" value="'.$afterline.'" style="width:100%" /></label><br />'; |
|
394 echo '<label for="xili_l_widget_afterlist-'.$number.'">'.__('after list','xili-language-widget').': <input id="xili_l_widget_afterlist-'.$number.'" name="xili-language_Widgets['.$number.'][afterlist]" type="text" value="'.$afterlist.'" style="width:100%" /></label>'; |
|
395 |
|
396 echo '</fieldset>'; |
|
397 |
|
398 |
|
399 // |
|
400 echo '<input type="hidden" id="xili_l_widget_submit-'.$number.'" name="xili-language_Widgets['.$number.'][submit]" value="1" />'; |
|
401 |
|
402 } // end options (control) |
|
403 |
|
404 } // end class |
|
405 |
|
406 |
|
407 |
|
408 $xili_recent_comments_widget =& new xili_recent_comments_Widget (); |
|
409 |
|
410 /* since 0.9.9.6 - multiple widgets available */ |
|
411 |
|
412 $xili_language_widgets =& new xili_language_Widgets (); |
|
413 |
|
414 /* since 1.3.2 - multiple recent posts widgets available */ |
|
415 |
|
416 if ( $wp_version >= '2.8.0') { |
|
417 function add_new_widgets() { |
|
418 register_widget('xili_Widget_Recent_Posts'); |
|
419 } |
|
420 add_action('widgets_init','add_new_widgets'); |
|
421 } |
|
422 ?> |