Guest

azizhp

By: a guest on Sep 23rd, 2008  |  syntax: PHP  |  size: 8.72 KB  |  hits: 1,468  |  expires: Never
download  |  raw  |  embed  |  report abuse
This paste has a previous version, view the difference. Copied
  1. <?php
  2.  
  3. /*
  4. Plugin Name: AHP Sitewide Recent Posts for WordPress MU
  5. Plugin URI: http://www.metablog.us/blogging/ahp-recent-posts-plugin-for-wordpress-mu/
  6. Description: Retrieves a list of the most recent posts sitewide in a WordPress MU installation.. Automatically excludes blog ID 1 (main blog), and post ID 1 (first "Hello World" posts of new blogs).  Flexible display output.
  7. Author: Aziz Poonawalla
  8. Author URI: http://metablog.us
  9.  
  10. Function arguments
  11. $how_many: how many recent posts are being displayed
  12. $how_long: time frame to choose recent posts from (in days)
  13. $display:  control over output format -
  14.         'title' - show post title only
  15.         'name' - show blog name and post title, as: @name | title
  16.         any integer value - show blog name, post title, and post excerpt of specified number of words
  17.         NOTE: function defaults to excerpt mode, 30 words, if argument does not match the above.
  18. $begin_wrap: customise the start html code to adapt to different themes
  19. $end_wrap: customise the end html code to adapt to different themes
  20. $optmask: optional bitmask for gravatar, date, and author display:
  21.         0: hide gravatar, hide date, hide author
  22.         1: show gravatar, hode date, hide author
  23.         2: hide gravatar, show date, hide author
  24.         3: show both gravatar and date, hide author
  25.         4: hide gravatar, hide date, show author
  26.         5: show gravatar, hode date, show author
  27.         6: hide gravatar, show date, show author
  28.         7: show both gravatar and date, show author
  29.         NOTE: if argument left unspecified, will show all three
  30.  
  31. Sample call: to show 5 posts from recent 30 days, with  20 word excerpt, no gravatar, with date
  32.  
  33.         <?php ahp_recent_posts_dev(5, 30, 20 , '<li>', '</li>',2);  ?>
  34.  
  35. Sample CSS for gravatar styling:  img.avatar-24 { float: left; padding: 0px; border: none; margin: 4px; clear: left; }
  36.  
  37. TODO:
  38. - add post comment count as an option
  39. - link gravatar icon to Extended Profile in buddypress, if installed
  40. - widgetize
  41. - show more than one post per blog
  42.  
  43. Version 0.5
  44. Update Author: Aziz Poonawalla
  45. Update Author URI: http://metablog.us
  46. - changed gravatar link to point to all posts by author on main blog (ID = 1).
  47. - added date string, author output
  48. - implemented bitmask to control gravatar, date, author output
  49. - consolidated numwords argument with display argument
  50.  
  51. Version 0.4.1
  52. Update Author: Aziz Poonawalla
  53. Update Author URI: http://metablog.us
  54. - added gravatar support, icon size 24px
  55. - gravatar can be styled by img.avatar-24 in your css file
  56. - gravatar image links to author's blog
  57. - capitalization of first five words of the excerpt
  58.  
  59. Version 0.4.0
  60. Update Author: Aziz Poonawalla
  61. Update Author URI: http://metablog.us
  62. - added exclusions for first blog, first post, enabled post excerpt
  63.  
  64. Version: 0.32
  65. Update Author: G. Morehouse
  66. Update Author URI: http://wiki.evernex.com/index.php?title=Wordpress_MU_sitewide_recent_posts_plugin
  67.  
  68. Version: 0.31
  69. Update Author: Sven Laqua
  70. Update Author URI: http://www.sl-works.de/
  71.  
  72. Version: 0.3
  73. Author: Ron Rennick
  74. Author URI: http://atypicalhomeschool.net/
  75. */
  76.  
  77. function ahp_recent_posts($how_many, $how_long, $display, $begin_wrap, $end_wrap, $optmask = 7) {
  78.         global $wpdb;
  79.         $counter = 0;
  80.        
  81.         // EDIT THESE TO CUSTOMIZE THE OUTPUT
  82.         $blog_prefix = "@";
  83.         $auth_prefix = ' by ';
  84.         $date_format = 'D M jS, Y';
  85.         $grav_size = 24;
  86.         $exc_size = 30;
  87.        
  88.         // optmask values:
  89.                 switch ($optmask) {
  90.                         case 0: $use_date = 0; $use_grav = 0; $use_auth = 0; break; // 0: no gravatar, no date, no author
  91.                         case 1: $use_date = 0; $use_grav = 1; $use_auth = 0; break; // 1: gravatar, no date, no author
  92.                         case 2: $use_date = 1; $use_grav = 0; $use_auth = 0; break; // 2: no gravatar, date, no author
  93.                         case 3: $use_date = 1; $use_grav = 1; $use_auth = 0; break; // 3: gravatar, date, no author
  94.                         case 4: $use_date = 0; $use_grav = 0; $use_auth = 1; break; // 4: no gravatar, no date, author
  95.                         case 5: $use_date = 0; $use_grav = 1; $use_auth = 1; break; // 5 gravatar, no date, author
  96.                         case 6: $use_date = 1; $use_grav = 0; $use_auth = 1; break; // 6: no gravatar, date, author
  97.                         case 7: $use_date = 1; $use_grav = 1; $use_auth = 1; break; // 7: gravatar, date, author
  98.                 }
  99.                
  100.         // debug output to test switch over optmask
  101.         // echo 'grav '.$use_grav.', date '.$use_date.', auth '.$use_auth;
  102.        
  103.         // hard-code $use_date and $use_grav
  104.         //$use_date = 1; $use_grav = 1;
  105.        
  106.         // get a list of blogs in order of most recent update. show only public and nonarchived/spam/mature/deleted
  107.         $blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE
  108.                 public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND blog_id != '1' AND
  109.                 last_updated >= DATE_SUB(CURRENT_DATE(), INTERVAL $how_long DAY)
  110.                 ORDER BY last_updated DESC");
  111.                
  112.         if ($blogs) {
  113.                 foreach ($blogs as $blog) {
  114.                         // we need _posts and _options tables for this to work
  115.                         $blogOptionsTable = "wp_".$blog."_options";
  116.                         $blogPostsTable = "wp_".$blog."_posts";
  117.                                
  118.                         // fetch  blog url
  119.                         $options = $wpdb->get_results("SELECT option_value FROM
  120.                                 $blogOptionsTable WHERE option_name IN ('siteurl','blogname')
  121.                                 ORDER BY option_name DESC");
  122.  
  123.                     //  fetch the ID, post title, post content, post date, and user's email for the latest post
  124.                         $thispost = $wpdb->get_results("SELECT $blogPostsTable.ID, $blogPostsTable.post_title, $blogPostsTable.post_content, $blogPostsTable.post_date, wp_users.display_name, wp_users.user_email, wp_users.user_login
  125.                                 FROM $blogPostsTable, wp_users
  126.                                 WHERE wp_users.ID = $blogPostsTable.post_author
  127.                                 AND post_status = 'publish' AND post_type = 'post'
  128.                                 AND post_date >= DATE_SUB(CURRENT_DATE(), INTERVAL $how_long DAY)
  129.                                 AND $blogPostsTable.id > 1
  130.                                 ORDER BY $blogPostsTable.id DESC limit 0,1");
  131.                                
  132.                         // if it is found put it to the output
  133.                         if($thispost) {
  134.                        
  135.                                 // get permalink by ID.  check wp-includes/wpmu-functions.php
  136.                                 $thispermalink = get_blog_permalink($blog, $thispost[0]->ID);
  137.                                
  138.                                 // get blog name,  URL
  139.                                 $thisbloglink = $options[0]->option_value;
  140.                                 $thisblogname = $options[1]->option_value;
  141.                                
  142.                                 // get author
  143.                                 if ($use_auth) {
  144.                                         $thisauthor = '<small>'.$auth_prefix.$thispost[0]->display_name.'</small><br>';
  145.                                 } else { $thisauthor = ''; }
  146.                                
  147.                                 // get author's posts link
  148.                                 $thisuser = $thispost[0]->user_login;
  149.                                 $thisuser_url = $thisbloglink."author/".$thisuser;
  150.                                
  151.                                 // get gravatar
  152.                                 if ($use_grav) {
  153.                                         $grav_img = get_avatar( $thispost[0]->user_email , $grav_size );
  154.                                         $thisgravatar = '<a href="'.$thisuser_url.'">'.$grav_img.'</a>';
  155.                                 } else { $thisgravatar = ''; }
  156.                                
  157.                                 // get post date (nicely formatted)
  158.                                 if ($use_date) {
  159.                                         $date_str = date($date_format, strtotime( $thispost[0]->post_date )) ;                                 
  160.                                         $thisdate = '<small>'.$date_str.'</small><br>';
  161.                                         } else { $thisdate = ''; }
  162.                                                                                                
  163.                                 if ($display != "title") {
  164.                                
  165.                                         if ($display != "name") {
  166.                                        
  167.                                                 // use integer value of $display for $numwords or default if non-integer
  168.                                                 if (is_numeric($display)) { $numwords = intval($display); }
  169.                                                 else { $numwords = $exc_size; }
  170.                                                                                                
  171.                                                 // debug output for display excerpt size
  172.                                                 //echo 'is_numeric display = '.is_numeric($display).', numwords = '.$numwords.', display = '.$display;
  173.                                        
  174.                                                 // just in case we end up with numwords of size zero, skip processing the content
  175.                                                 // and default to null excerpt
  176.                                                 if ($numwords == 0) {
  177.                                                         $thisexcerpt = '';
  178.                                                 } else {
  179.                                        
  180.                                                         // get post content and truncate to (numwords) words
  181.                                                         $thiscontent = strip_tags( $thispost[0]->post_content );
  182.                                                         preg_match("/([\S]+\s*){0,$numwords}/", $thiscontent, $regs);
  183.                                                         $trunc_content = explode( ' ', trim($regs[0]) , 6 );
  184.                                                
  185.                                                         // build the excerpt html block, capitalize first five words
  186.                                                         $thisexcerpt = '<small><br>'
  187.                                                         .strtoupper($trunc_content[0]).' '
  188.                                                         .strtoupper($trunc_content[1]).' '
  189.                                                         .strtoupper($trunc_content[2]).' '
  190.                                                         .strtoupper($trunc_content[3]).' '
  191.                                                         .strtoupper($trunc_content[4]).' '
  192.                                                         .$trunc_content[5].'... '
  193.                                                         .'<a href="'.$thispermalink.'">'                                       
  194.                                                         .'&raquo;&raquo;MORE'.'</a>'
  195.                                                         .'</small>';
  196.                                                 }
  197.                                         } else {
  198.                                                 $thisexcerpt = '';
  199.                                         }
  200.                                
  201.                                         echo $begin_wrap.$thisgravatar.$thisdate
  202.                                         .$thisauthor
  203.                                         .$blog_prefix.'<a href="'.$thisbloglink.'">'
  204.                                         .$thisblogname.'</a>'.' | '    
  205.                                         .'<a href="'.$thispermalink.'">'                                       
  206.                                         .$thispost[0]->post_title.'</a>'
  207.                                         .$thisexcerpt.$end_wrap;
  208.                                        
  209.                                         $counter++;
  210.                                         } else {
  211.                                                 echo $begin_wrap.$thisgravatar.$thisdate.$thisauthor.'<a href="'.$thispermalink
  212.                                                 .'">'.$thispost[0]->post_title.'</a>'.$end_wrap;
  213.                                                 $counter++;
  214.                                         }
  215.                         }
  216.                         // don't go over the limit
  217.                         if($counter >= $how_many) {
  218.                                 break;
  219.                         }
  220.                 }
  221.         }
  222. }
  223. ?>