Advertisement
Guest User

sfire38

a guest
Feb 4th, 2009
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. <?
  2. /*
  3. Plugin Name: List blogs widget
  4. Plugin URI: http://wordpress.org/#
  5. Description: Show off all your blogs (public ones or not) in the sidebar
  6. Version: 0.1
  7. Author: Anton Lindqvist
  8. Author URI: http://qvister.se
  9. */
  10. define(LIST_BLOGS_WIDGET_ID, 'widget_list_blogs');
  11. define(LIST_BLOGS_WIDGET_CACHE, 'widget_list_blogs_cache');
  12. define(LIST_BLOGS_WIDGET_CACHETIME, 'widget_list_blogs_cachetime');
  13.  
  14. function order($x,$y) {
  15. if($x['blogname'] == $y['blogname']) return 0;
  16. return ($x['blogname'] < $y['blogname']) ? -1 : 1;
  17. }
  18. function list_blogs($private=false,$title=null,$force=false) {
  19. global $wpdb;
  20.  
  21. if(get_option(LIST_BLOGS_WIDGET_CACHETIME) < time() - 3600 || $force) {
  22. $title = (!$title) ? 'Blogs' : $title;
  23. $return = '<div class=\"pd\">'.'<h2>'.$title.'</h2><ul>';
  24. $query = 'select blog_id from '.$wpdb->blogs.' where archived=\'0\' and spam=\'0\' and deleted=\'0\' and blog_id > 1';
  25. $query .= (!$private) ? ' and public=\'1\'' : ' and public >= -1';
  26. $results = $wpdb->get_results($query);
  27.  
  28. foreach($results as $row) {
  29. $blogname = get_blog_option($row->blog_id,'blogname');
  30. $siteurl = get_blog_option($row->blog_id,'siteurl');
  31. $blogs[] = array(
  32. 'id' => $row->blog_id,
  33. 'blogname' => $blogname,
  34. 'siteurl' => $siteurl
  35. );
  36. }
  37.  
  38. usort($blogs,'order');
  39.  
  40. foreach($blogs as $blog) {
  41. $return .= '<li id="id_'.$blog['id'].'"><a href="'.$blog['siteurl'].'">'.$blog['blogname'].'</a></li>';
  42. }
  43.  
  44. $return .= '</ul>';
  45. update_option(LIST_BLOGS_WIDGET_CACHE,$return);
  46. update_option(LIST_BLOGS_WIDGET_CACHETIME,time());
  47.  
  48. if(!$force) {
  49. echo $return;
  50. } else {
  51. return;
  52. }
  53. } else {
  54. echo get_option(LIST_BLOGS_WIDGET_CACHE);
  55. }
  56. }
  57. function widget_list_blogs($args) {
  58. extract($args, EXTR_SKIP);
  59. $options = get_option(LIST_BLOGS_WIDGET_ID);
  60. $private = $options['private'];
  61. $title = $options['title'];
  62. echo $before_widget;
  63. list_blogs($private,$title);
  64. echo $after_widget;
  65. }
  66. function widget_list_blogs_init() {
  67. wp_register_sidebar_widget(LIST_BLOGS_WIDGET_ID,
  68. __('List blogs'),'widget_list_blogs');
  69. wp_register_widget_control(LIST_BLOGS_WIDGET_ID,
  70. __('List blogs'),'widget_list_blogs_control');
  71. }
  72. function widget_list_blogs_control() {
  73. $options = get_option(LIST_BLOGS_WIDGET_ID);
  74.  
  75. if(!is_array($options)) {
  76. $options = array();
  77. }
  78.  
  79. $widget_data = $_POST[LIST_BLOGS_WIDGET_ID];
  80.  
  81. if($widget_data['submit']) {
  82. $options['private'] = $widget_data['private'];
  83. $options['title'] = $widget_data['title'];
  84. update_option(LIST_BLOGS_WIDGET_ID, $options);
  85. list_blogs($options['private'],$options['title'],true);
  86. }
  87.  
  88. $title = $options['title'];
  89. $private = $options['private'];
  90. ?>
  91. <p>
  92. <label for="<?=LIST_BLOGS_WIDGET_ID;?>-title">Custom title:</label>
  93. <input class="widefat" type="text" name="<?=LIST_BLOGS_WIDGET_ID?>[title]" id="<?=LIST_BLOGS_WIDGET_ID?>-num-posts" value="<?=$title?>" />
  94. </p>
  95. <p>
  96. <label for="<?php echo LIST_BLOGS_WIDGET_ID;?>-private">Show private blogs:</label>
  97. <select class="widefat" name="<?=LIST_BLOGS_WIDGET_ID?>[private]" id="<?=LIST_BLOGS_WIDGET_ID?>-private">
  98. <option value="1" <?=($private == '1') ? 'selected' : ''?>>Yes</option>
  99. <option value="0" <?=($private == '1') ? '' : 'selected'?>>No</option>
  100. </select>
  101. </p>
  102. <input type="hidden" name="<?=LIST_BLOGS_WIDGET_ID?>[submit]" value="1" />
  103. <?
  104. }
  105. add_action('plugins_loaded','widget_list_blogs_init');
  106. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement