<?php
/*
Plugin Name: AHP Sitewide Recent Posts for WordPress MU
Plugin URI: http://www.metablog.us/blogging/ahp-recent-posts-plugin-for-wordpress-mu/
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.
Author: Aziz Poonawalla
Author URI: http://metablog.us
Function arguments
$how_many: how many recent posts are being displayed
$how_long: time frame to choose recent posts from (in days)
$display: control over output format -
'title' - show post title only
'name' - show blog name and post title, as: @name | title
any integer value - show blog name, post title, and post excerpt of specified number of words
NOTE: function defaults to excerpt mode, 30 words, if argument does not match the above.
$begin_wrap: customise the start html code to adapt to different themes
$end_wrap: customise the end html code to adapt to different themes
$optmask: optional bitmask for gravatar, date, and author display:
0: hide gravatar, hide date, hide author
1: show gravatar, hode date, hide author
2: hide gravatar, show date, hide author
3: show both gravatar and date, hide author
4: hide gravatar, hide date, show author
5: show gravatar, hode date, show author
6: hide gravatar, show date, show author
7: show both gravatar and date, show author
NOTE: if argument left unspecified, will show all three
Sample call: to show 5 posts from recent 30 days, with 20 word excerpt, no gravatar, with date
<?php ahp_recent_posts_dev(5, 30, 20 , '<li>', '</li>',2); ?>
Sample CSS for gravatar styling: img.avatar-24 { float: left; padding: 0px; border: none; margin: 4px; clear: left; }
TODO:
- add post comment count as an option
- link gravatar icon to Extended Profile in buddypress, if installed
- widgetize
- show more than one post per blog
Version 0.5
Update Author: Aziz Poonawalla
Update Author URI: http://metablog.us
- changed gravatar link to point to all posts by author on main blog (ID = 1).
- added date string, author output
- implemented bitmask to control gravatar, date, author output
- consolidated numwords argument with display argument
Version 0.4.1
Update Author: Aziz Poonawalla
Update Author URI: http://metablog.us
- added gravatar support, icon size 24px
- gravatar can be styled by img.avatar-24 in your css file
- gravatar image links to author's blog
- capitalization of first five words of the excerpt
Version 0.4.0
Update Author: Aziz Poonawalla
Update Author URI: http://metablog.us
- added exclusions for first blog, first post, enabled post excerpt
Version: 0.32
Update Author: G. Morehouse
Update Author URI: http://wiki.evernex.com/index.php?title=Wordpress_MU_sitewide_recent_posts_plugin
Version: 0.31
Update Author: Sven Laqua
Update Author URI: http://www.sl-works.de/
Version: 0.3
Author: Ron Rennick
Author URI: http://atypicalhomeschool.net/
*/
function ahp_recent_posts($how_many, $how_long, $display, $begin_wrap, $end_wrap, $optmask = 7) {
global $wpdb;
$counter = 0;
// EDIT THESE TO CUSTOMIZE THE OUTPUT
$blog_prefix = "@";
$auth_prefix = ' by ';
$date_format = 'D M jS, Y';
$grav_size = 24;
$exc_size = 30;
// optmask values:
switch ($optmask) {
case 0: $use_date = 0; $use_grav = 0; $use_auth = 0; break; // 0: no gravatar, no date, no author
case 1: $use_date = 0; $use_grav = 1; $use_auth = 0; break; // 1: gravatar, no date, no author
case 2: $use_date = 1; $use_grav = 0; $use_auth = 0; break; // 2: no gravatar, date, no author
case 3: $use_date = 1; $use_grav = 1; $use_auth = 0; break; // 3: gravatar, date, no author
case 4: $use_date = 0; $use_grav = 0; $use_auth = 1; break; // 4: no gravatar, no date, author
case 5: $use_date = 0; $use_grav = 1; $use_auth = 1; break; // 5 gravatar, no date, author
case 6: $use_date = 1; $use_grav = 0; $use_auth = 1; break; // 6: no gravatar, date, author
case 7: $use_date = 1; $use_grav = 1; $use_auth = 1; break; // 7: gravatar, date, author
}
// debug output to test switch over optmask
// echo 'grav '.$use_grav.', date '.$use_date.', auth '.$use_auth;
// hard-code $use_date and $use_grav
//$use_date = 1; $use_grav = 1;
// get a list of blogs in order of most recent update. show only public and nonarchived/spam/mature/deleted
$blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE
public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND blog_id != '1' AND
last_updated >= DATE_SUB(CURRENT_DATE(), INTERVAL $how_long DAY)
ORDER BY last_updated DESC");
if ($blogs) {
foreach ($blogs as $blog) {
// we need _posts and _options tables for this to work
$blogOptionsTable = "wp_".$blog."_options";
$blogPostsTable = "wp_".$blog."_posts";
// fetch blog url
$options = $wpdb->get_results("SELECT option_value FROM
$blogOptionsTable WHERE option_name IN ('siteurl','blogname')
ORDER BY option_name DESC");
// fetch the ID, post title, post content, post date, and user's email for the latest post
$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
FROM $blogPostsTable, wp_users
WHERE wp_users.ID = $blogPostsTable.post_author
AND post_status = 'publish' AND post_type = 'post'
AND post_date >= DATE_SUB(CURRENT_DATE(), INTERVAL $how_long DAY)
AND $blogPostsTable.id > 1
ORDER BY $blogPostsTable.id DESC limit 0,1");
// if it is found put it to the output
if($thispost) {
// get permalink by ID. check wp-includes/wpmu-functions.php
$thispermalink = get_blog_permalink($blog, $thispost[0]->ID);
// get blog name, URL
$thisbloglink = $options[0]->option_value;
$thisblogname = $options[1]->option_value;
// get author
if ($use_auth) {
$thisauthor = '<small>'.$auth_prefix.$thispost[0]->display_name.'</small><br>';
} else { $thisauthor = ''; }
// get author's posts link
$thisuser = $thispost[0]->user_login;
$thisuser_url = $thisbloglink."author/".$thisuser;
// get gravatar
if ($use_grav) {
$grav_img = get_avatar( $thispost[0]->user_email , $grav_size );
$thisgravatar = '<a href="'.$thisuser_url.'">'.$grav_img.'</a>';
} else { $thisgravatar = ''; }
// get post date (nicely formatted)
if ($use_date) {
$date_str = date($date_format, strtotime( $thispost[0]->post_date )) ;
$thisdate = '<small>'.$date_str.'</small><br>';
} else { $thisdate = ''; }
if ($display != "title") {
if ($display != "name") {
// use integer value of $display for $numwords or default if non-integer
if (is_numeric($display)) { $numwords = intval($display); }
else { $numwords = $exc_size; }
// debug output for display excerpt size
//echo 'is_numeric display = '.is_numeric($display).', numwords = '.$numwords.', display = '.$display;
// just in case we end up with numwords of size zero, skip processing the content
// and default to null excerpt
if ($numwords == 0) {
$thisexcerpt = '';
} else {
// get post content and truncate to (numwords) words
$thiscontent = strip_tags( $thispost[0]->post_content );
preg_match("/([\S]+\s*){0,$numwords}/", $thiscontent, $regs);
$trunc_content = explode( ' ', trim($regs[0]) , 6 );
// build the excerpt html block, capitalize first five words
$thisexcerpt = '<small><br>'
.strtoupper($trunc_content[0]).' '
.strtoupper($trunc_content[1]).' '
.strtoupper($trunc_content[2]).' '
.strtoupper($trunc_content[3]).' '
.strtoupper($trunc_content[4]).' '
.$trunc_content[5].'... '
.'<a href="'.$thispermalink.'">'
.'»»MORE'.'</a>'
.'</small>';
}
} else {
$thisexcerpt = '';
}
echo $begin_wrap.$thisgravatar.$thisdate
.$thisauthor
.$blog_prefix.'<a href="'.$thisbloglink.'">'
.$thisblogname.'</a>'.' | '
.'<a href="'.$thispermalink.'">'
.$thispost[0]->post_title.'</a>'
.$thisexcerpt.$end_wrap;
$counter++;
} else {
echo $begin_wrap.$thisgravatar.$thisdate.$thisauthor.'<a href="'.$thispermalink
.'">'.$thispost[0]->post_title.'</a>'.$end_wrap;
$counter++;
}
}
// don't go over the limit
if($counter >= $how_many) {
break;
}
}
}
}
?>