whyisjake

Jake Spurlock

Jan 26th, 2010
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.89 KB | None | 0 0
  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 highly customizable list of the most recent sitewide posts in a WordPress MU installation. Automatically excludes blog ID 1 (main blog), and post ID 1 (first "Hello World" posts of new blogs).
  7. Author: Aziz Poonawalla
  8. Author URI: http://metablog.us
  9.  
  10. FUNCTION ARGUMENTS
  11.  
  12. $how_many: how many recent posts are being displayed
  13. $how_long: time frame to choose recent posts from (in days)
  14. $optmask: bitmask for various display options (default: 255)
  15.     DISPLAY OPTIONS BITMASK
  16.     1;  // gravatar
  17.     2;  // date
  18.     4;  // author name
  19.     8;  // comment count
  20.     16; // blog name   
  21.     32; // post name   
  22.     64; // post excerpt
  23.     128; // excerpt capitalization
  24. $exc_size: size of excerpt in words (default: 30)
  25. $begin_wrap: start html code (default: <li class="ahp_recent-posts">)
  26. $end_wrap: end html code to adapt to different themes (default: </li>)
  27.  
  28. SAMPLE FUNCTION CALL
  29.  
  30. to show 5 posts from recent 30 days: <?php ahp_recent_posts(5, 30);  ?>
  31.  
  32. SAMPLE CSS
  33.  
  34. gravatar styling:  img.avatar-24 { float: left; padding: 0px; border: none; margin: 4px; clear: left; }
  35. LI styling: li.ahp-recent-posts { list-style-type: none ;}
  36. excerpt styling: .ahp-excerpt { margin-top: 2px }
  37.  
  38. TODO:
  39.  
  40. - link gravatar icon to Extended Profile in buddypress, if installed
  41. - widgetize
  42. - show more than one post per blog
  43.  
  44. CHANGELOG
  45.  
  46. Version 0.6
  47. Update Author: Aziz Poonawalla
  48. Update Author URI: http://metablog.us
  49. - added comment count display option
  50. - added enable/disable excerpt capitalization
  51. - consolidated title/name of post display options into bitmask
  52. - reduced number of required arguments
  53. - added class name ahp-recent-posts to default start html LI tags
  54. - added class ahp-excerpt to excerpt block
  55.  
  56. Version 0.5
  57. Update Author: Aziz Poonawalla
  58. Update Author URI: http://metablog.us
  59. - changed gravatar link to point to all posts by author on main blog (ID = 1).
  60. - added date string, author output
  61. - implemented bitmask to control gravatar, date, author output
  62. - consolidated numwords argument with display argument
  63.  
  64. Version 0.4.1
  65. Update Author: Aziz Poonawalla
  66. Update Author URI: http://metablog.us
  67. - added gravatar support, icon size 24px
  68. - gravatar can be styled by img.avatar-24 in your css file
  69. - gravatar image links to author's blog
  70. - capitalization of first five words of the excerpt
  71.  
  72. Version 0.4.0
  73. Update Author: Aziz Poonawalla
  74. Update Author URI: http://metablog.us
  75. - added exclusions for first blog, first post, enabled post excerpt
  76.  
  77. Version: 0.32
  78. Update Author: G. Morehouse
  79. Update Author URI: http://wiki.evernex.com/index.php?title=Wordpress_MU_sitewide_recent_posts_plugin
  80.  
  81. Version: 0.31
  82. Update Author: Sven Laqua
  83. Update Author URI: http://www.sl-works.de/
  84.  
  85. Version: 0.3
  86. Author: Ron Rennick
  87. Author URI: http://atypicalhomeschool.net/
  88. */
  89.  
  90. function ahp_recent_posts($how_many, $how_long, $optmask = 255, $exc_size = 30, $begin_wrap = '<li class="ahp_recent-posts">', $end_wrap = '</li>') {
  91.     global $wpdb;
  92.     $counter = 0;
  93.    
  94.     // EDIT THESE TO CUSTOMIZE THE OUTPUT
  95.     $debug = 0;
  96.     $blog_prefix = "@";
  97.     $post_prefix = " | ";
  98.     $auth_prefix = ' By ';
  99.     $com_prefix = ' | ';
  100.     $date_format = 'D M jS, Y';
  101.     $grav_size = 90;
  102.    
  103.     // DISPLAY OPTIONS BITMASK
  104.     $grav_flag = 1;  // gravatar
  105.     $date_flag = 2;  // date
  106.     $auth_flag = 4;  // author name
  107.     $com_flag  = 8;  // comment count
  108.     $name_flag = 16; // blog name  
  109.     $post_flag = 32; // post name  
  110.     $exc_flag  = 64; // post excerpt
  111.     $cap_flag  = 128; // excerpt capitalization
  112.    
  113.     // set the various option flags
  114.     if ($optmask & $grav_flag) { $use_grav = 1; } else { $use_grav = 0; }
  115.     if ($optmask & $date_flag) { $use_date = 1; } else { $use_date = 0; }
  116.     if ($optmask & $auth_flag) { $use_auth = 1; } else { $use_auth = 0; }
  117.     if ($optmask & $com_flag)  { $use_com  = 1; } else { $use_com = 0;  }
  118.     if ($optmask & $name_flag) { $use_name = 1; } else { $use_name = 0; }
  119.     if ($optmask & $post_flag) { $use_post = 1; } else { $use_post = 0; }
  120.     if ($optmask & $exc_flag)  { $use_exc  = 1; } else { $use_exc = 0;  }
  121.     if ($optmask & $cap_flag)  { $use_cap  = 1; } else { $use_cap = 0;  }
  122.    
  123.     // debug block
  124.     if ($debug) {
  125.         echo '<hr>'.'opt = '.$optmask.': grav = '.$use_grav.', date = '.$use_date
  126.         .', auth = '.$use_auth.', use_com = '.$use_com.', use_name = '.$use_name
  127.         .', use_post = '.$use_post.', use_exc = '.$use_exc.', use_cap = '.$use_cap;
  128.     }
  129.    
  130.     // get a list of blogs in order of most recent update. show only public and nonarchived/spam/mature/deleted
  131.     $blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE
  132.         public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND blog_id != '1' AND
  133.         last_updated >= DATE_SUB(CURRENT_DATE(), INTERVAL $how_long DAY)
  134.         ORDER BY last_updated DESC");
  135.        
  136.     if ($blogs) {
  137.        
  138.         foreach ($blogs as $blog) {
  139.  
  140.             // we need _posts, _comments, and _options tables for this to work
  141.             $blogOptionsTable = "wp_".$blog."_options";
  142.             $blogPostsTable = "wp_".$blog."_posts";
  143.             $blogCommentsTable = "wp_".$blog."_comments";
  144.                            
  145.             // debug block
  146.             if ($debug) {
  147.                 echo '<hr>processing blog '.$blog.' = <a href="'.
  148.                 $options[0]->option_value.'">'.$options[1]->option_value.'</a> <br>';
  149.             }
  150.  
  151.             // fetch the ID, post title, post content, post date, and user's email for the latest post
  152.             $thispost = $wpdb->get_results("SELECT $blogPostsTable.ID, $blogPostsTable.post_title,
  153.                 $blogPostsTable.post_content, $blogPostsTable.post_date, wp_users.display_name,
  154.                 wp_users.user_email, wp_users.user_login
  155.                 FROM $blogPostsTable, wp_users
  156.                 WHERE wp_users.ID = $blogPostsTable.post_author
  157.                 AND post_status = 'publish' AND post_type = 'post'
  158.                 AND post_date >= DATE_SUB(CURRENT_DATE(), INTERVAL $how_long DAY)
  159.                 AND $blogPostsTable.id > 1
  160.                 ORDER BY $blogPostsTable.id DESC limit 0,1");
  161.  
  162.             // if it is found put it to the output
  163.             if($thispost) {
  164.            
  165.                 // debug block
  166.                 if ($debugflag) {
  167.                     echo 'processing thispost ID = '.$thispost[0]->ID.'<br>';
  168.                     echo 'post_title = '.$thispost[0]->post_title.'<br>';
  169.                     //echo 'post_content = '.$thispost[0]->post_content.'<br>';
  170.                     echo 'post_date = '.$thispost[0]->post_date.'<br>';
  171.                     echo 'display_name = '.$thispost[0]->display_name.'<br>';
  172.                     echo 'user_email = '.$thispost[0]->user_email.'<br>';
  173.                     echo 'user_login = '.$thispost[0]->user_login.'<br>';
  174.                 }
  175.            
  176.                 // get post ID
  177.                 $thispostID = $thispost[0]->ID;
  178.                
  179.                 // get permalink by ID.  check wp-includes/wpmu-functions.php
  180.                 $thispermalink = get_blog_permalink($blog, $thispostID);
  181.                
  182.                 // get blog name,  URL
  183.                 if ($use_name) {
  184.  
  185.                     $options = $wpdb->get_results("SELECT option_value FROM
  186.                     $blogOptionsTable WHERE option_name IN ('siteurl','blogname')
  187.                     ORDER BY option_name DESC");
  188.  
  189.                     $blog_link = $options[0]->option_value;
  190.                     $blog_name = $options[1]->option_value;
  191.                     $this_blogname = $blog_prefix.'<a href="'.$blog_link.'">'.$blog_name.'</a>';
  192.  
  193.                 } else { $this_blogname = ''; }
  194.  
  195.                 // get comments
  196.                 if ($use_com) {
  197.                    
  198.                     // sql query for all comments on the current post
  199.                     $thesecomments = $wpdb->get_results("SELECT comment_ID
  200.                     FROM $blogCommentsTable
  201.                     WHERE comment_post_ID = $thispostID");
  202.                    
  203.                     // count total number of comments
  204.                     $num_comments = sizeof($thesecomments);
  205.                    
  206.                     // pretty text
  207.                     if ($num_comments == 0) { $thiscomment = $com_prefix.'no comments'; }
  208.                     elseif ($num_comments == 1) { $thiscomment = $com_prefix.'one comment'; }
  209.                     else { $thiscomment = $com_prefix.$num_comments.' comments'; }
  210.                    
  211.                 } else { $thiscomment = ''; }
  212.                                    
  213.                 // get author
  214.                 if ($use_auth) {
  215.                     $thisauthor = $auth_prefix.$thispost[0]->display_name;
  216.                 } else { $thisauthor = ''; }
  217.                
  218.                 // get author's posts link
  219.                 $thisuser = $thispost[0]->user_login;
  220.                 $thisuser_url = $thisbloglink."author/".$thisuser;
  221.                
  222.                 // get gravatar
  223.                 if ($use_grav) {
  224.                     $grav_img = get_avatar( $thispost[0]->user_email , $grav_size );
  225.                     $thisgravatar = '<a href="'.$thisuser_url.'">'.$grav_img.'</a>';
  226.                 } else { $thisgravatar = ''; }
  227.                
  228.                 // get post date (nicely formatted)
  229.                 if ($use_date) {
  230.                     $thisdate = date($date_format, strtotime( $thispost[0]->post_date )) ;                 
  231.                 } else { $thisdate = ''; }
  232.                
  233.                 // get post name
  234.                 if ($use_post) {
  235.                     $this_postname = $post_prefix.'<a href="'.$thispermalink.'">'.$thispost[0]->post_title.'</a><br>';
  236.                 } else { $this_postname = ''; }
  237.                
  238.                 if ($use_exc) {
  239.  
  240.                     if ($exc_size == 0) { $thisexcerpt = ''; }
  241.                     else {
  242.                         // get post content and truncate to (numwords) words
  243.                         $thiscontent = strip_tags( $thispost[0]->post_content );
  244.                         preg_match("/([\S]+\s*){0,$exc_size}/", $thiscontent, $regs);
  245.                        
  246.                         if ($use_cap) {
  247.                             // build the excerpt html block, capitalize first five words
  248.                             $trunc_content = explode( ' ', trim($regs[0]) , 6 );
  249.                             $exc_str = strtoupper($trunc_content[0]).' '
  250.                             .strtoupper($trunc_content[1]).' '
  251.                             .strtoupper($trunc_content[2]).' '
  252.                             .strtoupper($trunc_content[3]).' '
  253.                             .strtoupper($trunc_content[4]).' '
  254.                             .$trunc_content[5].'... ';
  255.                         } else { $exc_str = trim($regs[0]); }
  256.                            
  257.                         $thisexcerpt = '<span style="ahp-excerpt">'.$exc_str
  258.                         .'<a href="'.$thispermalink.'">'                   
  259.                         .'&raquo;&raquo;MORE'.'</a></span>';
  260.                     }
  261.                 } else { $thisexcerpt = ''; }
  262.                                                
  263.                     echo $begin_wrap
  264.                     .$thisgravatar
  265.                     .'<small>'.$thisdate.$thisauthor.$thiscomment.'</small><br>'
  266.                     .$this_blogname
  267.                     .$this_postname
  268.                     .'<small>'.$thisexcerpt.'</small>'
  269.                     .$end_wrap;
  270.                    
  271.                     $counter++;
  272.             }
  273.                        
  274.             // don't go over the limit
  275.             if($counter >= $how_many) {
  276.                 break;
  277.             }
  278.         }
  279.     }
  280.     else {
  281.         //echo "no recent posts meet the criteria...<br>";
  282.     }
  283. }
  284. ?>
Add Comment
Please, Sign In to add comment