Options/Recent Comments after activation of the plugin.
*/
if ( function_exists("is_plugin_page") && is_plugin_page() ) {
kjgrc_options_page();
return;
}
function kjgrc_subpage_misc()
{
?>
get_results("SELECT * FROM $wpdb->categories ORDER BY cat_name");
}
$exclude_cat = kjgrc_get_exclude_cat();
?>
1. What this plugin does
It shows excerpts of the latest comments and/or trackbacks in your sidebar. You
have comprehensive control about their appearance. This ranges from the number
of comments, the length of the excerpts up to the html layout. You can let the
plugin order the comments by the corresponding post, or simply order them by
date. The plugin can (optionally) separate the trackbacks/pingbacks from the
comments. It can ignore comments to certain categories, and it offers support
for gravatars. It only gives extra work to the database, when actually a new
comment arrived. And you can filter out unwanted pingbacks, which originate
from your own blog. And it is a widget.
2. Installation
Since you are reading this text, you already uploaded and activated the plugin.
Now you want to add the plugin to your theme. There are two options to do this:
2.1 Modern Theme with widget support
The plugin is a widget. If
your theme supports widgets, and you have installed the widget plugin, adding
the plugin to the sidebar is easy: Go to the presentation menu and drag
and drop the widget into the sidebar. Don't forget the Get Recent Trackbacks
box. And you might want to change the title. All done.
2.2 Old school theme without widget support
You need to insert the following code snippet into the sidebar template.
The strongest feature of the plugin is that you can change the html layout in the admin interface, by filling in templates. In the templates you make use of macros, that are later replaced by the actual data.
3.1 Using the Macros
%comment_excerpt
The text of the comment. It might get shorted to the number of characters you entered in "Long comments are chopped off at..."
%comment_link
The URL to the cited comment.
%comment_author
The name, the commenter entered in the comment form. If she left the field empty, the name is "Anonymous".
%comment_date
The date, when the comment was posted in the style you configured as default date format.
%comment_time
The time, when the comment was posted
%time_since
Time since the comment was posted. For example: "9 hours 16 minutes".
%comment_type
"Comment", "Trackback" or "Pingback".
%gravatar
This macro becomes a complete image tag. If the comment author registered a gravatar with gravatar.com. Example: <img src="http://www.gravatar.com/avatar.php?gravatar_id=1ebbd34d4e45cac&size=25&rating=X"/>
%gravatar_url
This macro becomes only the URL to the gravatar. Example: http://www.gravatar.com/avatar.php?gravatar_id=1ebbd34d4e45cac&size=25&rating=X
%profile_picture
Becomes to URL of a profile picture. Use it like this: <img src="%profile_picture" width=16 height=16>. This only works after activation of Hannah Gray'sProfile Pics Plugin.
%userid
If the comment author is registered with your wordpress, and was logged in, when she wrote the comment this is replaced with the user id, she has in WordPress. The user id's are listed here: users.php. You can do fancyful things with this macro. For example you may construct an image url, that points to pictures of all the authors of your blog: <img src="/images/user%userid.jpg">
%author_url
The URL, the comment author left in the comment form, or if the comment is a trackback, the URL of the site that issued the trackback.
%author_url_href
The same like %author_url but inside a href statement. If there is no URL, the whole macro is empty. Without a href="" statement the link looks like a link, but is inactive. Use it like this: <a %author_url_href title="%comment_date">comment_author</a>
%post_title
The title of the posting that was commented.
%post_link
The URL of the posting that was commented.
%post_date
The date when the commented posting was published.
%post_counter
The number of comments this post has..
%trackback_title
Only applicable in trackbacks: The title of the trackback. It might get shorted to the number of characters you entered in "Long trackbacks are chopped off at..."
3.2 Group by comments
If you want to order the comments by their posting, you will have not one but
three templates. The middle one is just the same as in the normal order. The
first and the last template are used to generate headers for the grouped
comments. Usually you will show the %post_title in this header. This is the
html markup that is generated by the templates:
<li><h2>Recent Comments:</h2>
<ul>
Start sequence. Automatically provided when the plugin is used as widget. Otherwise you have to enter this by manually into the sidebar.php template.
<li>Post 1</li>
<ul>
Template for the post. This is the header for a list of comments, that all belong to the same posting.
<li>Comment1 to Post1</li>
<li>Comment2 to Post1</li>
Template for the comments It is repeated for every single comment
</ul>
</li>
Template for the closing tags of the post template
<li>Post 2</li>
<ul>
The next posting.
<li>Comment1 to Post2</li>
<li>Comment2 to Post2</li>
The comments to the next posting
</ul>
</li>
Again close the tags
</ul>
</li>
End sequence. Automatically provided when the plugin is used as widget. Otherwise you have to enter this by manually into the sidebar.php template.
4. Miscellaneous
Don't worry if you screwed up the template, reset the template to default and try again.
"Wrap long words at..." means: words, that exceed this length are split into fragments to prevent damage to the layout of your blog.
"Ignore trackbacks originating from this ip address" on the
configuration page for recent trackbacks is useful for filtering out pingbacks
that occur when you have a link to your own site in a post.
If you select to exclude comments made by blog authors (your own comments) from the list, then the comments which you made, when you were logged in, are ignored. If you also want to exclude comments you made, when you were not logged in, you can select to also consider the username and e-mail address.
5. Interoperation with other plugins
If Hannah Gray'sProfile Pics Plugin is present, you may use to macro %profile_picture to obtain an URL to the profile picture of the commentator (read in the macro section for a working example).
If malyfred'sPolyglot Plugin is present, additional filters are applied to comments, titles, dates and times, which select the right language for the user.
",
"misc_exclude_cat" => "",
"misc_exclude_cat_reverse" => 0,
"misc_convert_smileys" => 1,
"gravatar_size" => 20,
"gravatar_alt_url" => "",
"grt_ignore_ip" => "",
"gravatar_rating" => 0,
"grc_sidebar_title" => "Recent Comments",
"grt_sidebar_title" => "Recent Trackbacks"
);
$old_keys = array("grc_max_comments","grc_chars_per_comment","grc_chars_per_word","grc_format","grt_max_comments","grt_chars_per_comment","grt_chars_per_word","grt_format","misc_exclude_cat","gravatar_size","gravatar_alt_url","gravatar_rating","grt_ignore_ip");
// This might be an upgrade from < 1.4 to 1.4 or newer version. If there are old keys, migrate them to the new array:
add_option('gravatar_alt_url','');
if ($kjgrc_options == NULL) {
foreach ($old_keys as $key) {
$sql = "SELECT option_value,option_id FROM $wpdb->options WHERE option_name = 'kjgrc_$key'";
$old_value = $wpdb->get_row($sql,ARRAY_N); // $old_value[0] = old value, $old_value[1] = option id
if ($old_value[1] != NULL) {
$kjgrc_options[$key] = $old_value[0];
delete_option("kjgrc_$key");
}
}
}
// Whether it's a new installation or an introduction of new options: Fill in default values
foreach (array_keys($default_options) as $key) {
if (($kjgrc_options == NULL) || ! array_key_exists($key,$kjgrc_options) ) {
$kjgrc_options[$key] = $default_options[$key];
}
}
// Delete keys that are not in use any more
foreach (array_keys($default_options) as $key) {
$tmp[$key] = $kjgrc_options[$key];
}
$kjgrc_options = $tmp;
$kjgrc_options["version"] = $version;
add_option("kjgrc_options",$options,$description = '', $autoload = 'no');
update_option('kjgrc_options',$kjgrc_options);
}
return $kjgrc_options[$section . "_" . "$option_name"];
}
function kjgrc_use_cache()
{
add_option('kjgrc_use_cache',1);
return get_option("kjgrc_use_cache");
}
function kjgrc_options_page ()
{
?>
%comment_author: %comment_excerpt');
kjgrc_set_option('grc_grouped_by_post_a','
');
}
//delete_option('kjgrc_'.$function.'_format');
} else {
kjgrc_set_option($function.'_max_comments', (int)$_POST['max_comments']);
kjgrc_set_option($function.'_chars_per_comment', (int)$_POST['chars_per_comment']);
kjgrc_set_option($function.'_chars_per_word', (int)$_POST['chars_per_word']);
kjgrc_set_option($function.'_format', $_POST['format']);
kjgrc_set_option($function.'_grouped_by_post_a', $_POST['grouped_by_post_a']);
kjgrc_set_option($function.'_grouped_by_post_b', $_POST['grouped_by_post_b']);
}
}
if ($function == 'grc') {
if ($_POST['grc_exclude_blog_owner_checkbox'] == 'on')
kjgrc_set_option('grc_exclude_blog_owner',1);
else
kjgrc_set_option('grc_exclude_blog_owner',0);
if ($_POST['grc_exclude_blog_owner2_checkbox'] == 'on')
kjgrc_set_option('grc_exclude_blog_owner2',1);
else
kjgrc_set_option('grc_exclude_blog_owner2',0);
if ($_POST['grouped_by_post_checkbox'] == 'on') {
kjgrc_set_option('grc_grouped_by_post',1);
} else {
kjgrc_set_option('grc_grouped_by_post',0);
}
if ($_POST['grc_show_trackbacks_checkbox'] == 'on') {
kjgrc_set_option('grc_show_trackbacks',1);
} else {
kjgrc_set_option('grc_show_trackbacks',0);
}
if (! empty($_POST['grc_comments_per_post'])) {
kjgrc_set_option('grc_comments_per_post',(int)$_POST['grc_comments_per_post']);
if (kjgrc_get_option("grc","comments_per_post") == 0)
kjgrc_set_option("grc_comments_per_post",1);
}
if ($_POST['grc_limit_comments_per_post_checkbox'] == 'on') {
kjgrc_set_option('grc_limit_comments_per_post',1);
} else {
kjgrc_set_option('grc_limit_comments_per_post',0);
}
}
}
if (isset($_GET['updated']) && ($_GET['updated'] == 'true'))
{
if ($function == 'exclude_cat')
{
if (count($_POST['exclude_category']) == 0) {
kjgrc_set_option('misc_exclude_cat','');
} else {
kjgrc_set_option('misc_exclude_cat',implode(" ",$_POST['exclude_category']));
}
if (isset($_POST['exclude_categories_reverse'])) {
if ($_POST['exclude_categories_reverse'] == 0 || $_POST['exclude_categories_reverse'] == 1) {
kjgrc_set_option("misc_exclude_cat_reverse",$_POST['exclude_categories_reverse']);
}
}
}
if ($function == 'gravatar') {
if ($_POST['gravatar_size'] > 0 && $_POST['gravatar_size'] < 81) {
kjgrc_set_option('gravatar_size',$_POST['gravatar_size']);
}
kjgrc_set_option('gravatar_alt_url',$_POST['gravatar_alt_url']);
kjgrc_set_option('gravatar_rating',$_POST['gravatar_rating']);
}
if ($function == 'grt' && isset($_POST['ignore_ip']))
{
kjgrc_set_option('grt_ignore_ip',trim($_POST['ignore_ip']));
}
if ($function == 'misc' && isset($_POST['convert_smileys'])) {
if ($_POST['convert_smileys'] == 0 || $_POST['convert_smileys'] == 1) {
kjgrc_set_option("misc_convert_smileys",$_POST['convert_smileys']);
}
}
if ($function == 'misc') {
if ($_POST['use_cache_checkbox'] == 'on') {
update_option("kjgrc_use_cache",1);
} else {
update_option("kjgrc_use_cache",0);
}
}
}
$kjgrc_subpage = 1;
if (isset($_GET['subpage'])) {
$kjgrc_subpage = $_GET['subpage'];
}
kjgrc_subpage_header($kjgrc_subpage);
if ($kjgrc_subpage == 1) {
kjgrc_subpage_grc();
} elseif ($kjgrc_subpage == 2) {
kjgrc_subpage_grt();
} elseif ($kjgrc_subpage == 3) {
kjgrc_subpage_instructions();
} elseif ($kjgrc_subpage == 4) {
kjgrc_subpage_exclude_cat();
} elseif ($kjgrc_subpage == 5) {
kjgrc_subpage_gravatar();
} elseif ($kjgrc_subpage == 6) {
kjgrc_subpage_misc();
}
}
function kjgrc_add_options_page()
{
add_options_page('Get Recent Comments Plugin', 'Recent Comments', 8, 'get-recent-comments.php','kjgrc_options_page');
}
function kjgrc_get_exclude_cat ()
{
$exclude_cat = kjgrc_get_option('misc','exclude_cat');
if ($exclude_cat == '') {
return FALSE;
}
#echo "cats: '". kjgrc_get_option('misc','exclude_cat') ."' ";
return explode(" ",kjgrc_get_option('misc','exclude_cat'));
}
#function kjgrc_suicide ()
#{
# $plugins = get_settings('active_plugins');
# if (in_array('get-recent-comments.php',$plugins)) {
# unset($plugins['get-recent-comments.php']);
# update_option('active_plugins',$plugins);
# }
# delete_option('kjgrc_options');
# kjgrc_get_option('grc','max_comments');
# delete_option('kjgrc_options');
# delete_option('kjgrc_cache');
# echo 'you are now dead';
#}
function get_recent_comments()
{
echo kjgrc_cache("comments");
}
function get_recent_trackbacks()
{
echo kjgrc_cache("trackbacks");
}
function kjgrc_invalidate_cache() {
delete_option('kjgrc_cache');
}
function kjgrc_handle_new_comment($args) {
if (wp_get_comment_status($args) == 'approved') {
kjgrc_invalidate_cache();
}
}
function kjgrc_prepare_cache($cache)
{
// return $cache;
$last = 0;
$start = strpos($cache,'<%time_since:');
if ($start === false) {
return $cache;
}
while ($start !== false)
{
//kjgrc_log("start: $start");
$end = strpos($cache,'>',$start);
//kjgrc_log("end $end");
//kjgrc_log("cut $last - $start");
$newcache = $newcache . substr($cache,$last,$start-$last);
$tmp = substr($cache,$start+13,$end-$start-13);
$ago = gmdate('U')-$tmp;
//kjgrc_log("--> $tmp <--");
$newcache = $newcache . kjgrc_format_seconds($ago);
#kjgrc_log("$newcache");
$last = $end + 1;
$start = strpos($cache,'<%time_since:',$last);
}
$newcache = $newcache . substr($cache,$last);
return $newcache;
}
function kjgrc_cache($type)
{
global $wpdb,$kjgrc_cache;
$use_cache = FALSE;
if (kjgrc_use_cache()) {
$use_cache = TRUE;
}
if ($use_cache == FALSE) {
// kjgrc_log("return cache without caching");
$kjgrc_cache[comments] = kjgrc_prepare_cache(kjgrc_create_recent_comments());
$kjgrc_cache[trackbacks] = kjgrc_prepare_cache(kjgrc_create_recent_trackbacks());
return $kjgrc_cache[$type];
}
if ($kjgrc_cache == NULL) {
// kjgrc_log("return cache WITH caching");
$kjgrc_cache = get_option('kjgrc_cache');
if ($kjgrc_cache == NULL) {
// kjgrc_log("write new cache");
$kjgrc_cache[comments] = base64_encode(kjgrc_create_recent_comments());
$kjgrc_cache[trackbacks] = base64_encode(kjgrc_create_recent_trackbacks());
update_option('kjgrc_cache',$kjgrc_cache);
}
$kjgrc_cache[comments] = kjgrc_prepare_cache(base64_decode($kjgrc_cache[comments]));
$kjgrc_cache[trackbacks] = kjgrc_prepare_cache(base64_decode($kjgrc_cache[trackbacks]));
}
// search and replace up to date information
return $kjgrc_cache[$type];
}
function kjgrc_create_recent_trackbacks( $caller = '')
{
global $kjgrc_we_are_a_widget,$kjgrc_widget_args;;
// $entries = kjgrc_get_entries($max_comments,$chars_per_comment,$chars_per_word,$format,$query,0,$caller);
$entries = kjgrc_get_comments2('grt',$caller);
// If we are a widget: Also care for the title
if ($kjgrc_we_are_a_widget == TRUE) {
extract($kjgrc_widget_args);
return $before_widget . $before_title . kjgrc_get_option("grt","sidebar_title") . $after_title .
'
' .
$entries .
'
' .
$after_widget;
}
return $entries;
}
function kjgrc_create_recent_comments ($caller='')
{
global $kjgrc_we_are_a_widget,$kjgrc_widget_args;
// $entries = kjgrc_get_entries($max_comments,$chars_per_comment,$chars_per_word,$format,$query,kjgrc_get_option("grc","grouped_by_post"),$caller);
// If we are a widget: Also care for the title
// $entries = kjgrc_get_comments2($max_comments,$chars_per_comment,$chars_per_word,$format,kjgrc_get_option("grc","grouped_by_post"),$caller);
$entries = kjgrc_get_comments2('grc',$caller);
if ($kjgrc_we_are_a_widget == TRUE) {
extract($kjgrc_widget_args);
return $before_widget . $before_title . kjgrc_get_option("grc","sidebar_title") . $after_title .
'
' .
$entries .
'
' .
$after_widget;
}
return $entries;
}
function kjgrc_parse_pingback($pingback_author)
{
$workstring = trim($pingback_author);
/* most common syntax
1. author » title
2. author » category » title
3. title at author
4. title - author (too insignificant)
5. [»] title « author
*/
$first_delimiter = strpos($workstring,'»');
while ($first_delimiter !== false && $first_delimiter == 0) {
$workstring = trim(substr($workstring,7));
$first_delimiter = strpos($workstring,'»');
}
if ($first_delimiter !== false) {
$comment_author = substr($workstring,0,$first_delimiter-1);
$workstring = trim(substr($workstring,$first_delimiter+7));
$first_delimiter = strpos($workstring,'»');
if ($first_delimiter !== false) {
$workstring = trim(substr($workstring,$first_delimiter+7));
}
return array($comment_author,$workstring);
}
foreach (array(' at ','«',' - ',' auf ',' by ',' // ',' | ',' : ',' @ ',' / ') as $delimiter)
{
$first_delimiter = strpos($workstring,$delimiter);
if ($first_delimiter !== false) {
$trackback_title = trim(substr($workstring,0,$first_delimiter));
$comment_author = trim(substr($workstring,$first_delimiter+strlen($delimiter)));
// kjgrc_log("delimiter match [$delimiter]: $workstring -> a: '$comment_author' t: '$trackback_title' ");
return array($comment_author,$trackback_title);
}
}
// $comment_author = 'Unknown';
$comment_author = $pingback_author;
$trackback_title = '';
return array($comment_author,$trackback_title);
}
// original function from wordpress 2.3 for backwards compatibility to wordpress 2.0.11
function kjgrc_get_users_of_blog( $id = '' ) {
global $wpdb, $blog_id;
if ( empty($id) )
$id = (int) $blog_id;
$users = $wpdb->get_results( "SELECT user_id, user_login, display_name, user_email, meta_value FROM $wpdb->users, $wpdb->usermeta WHERE " . $wpdb->users .
".ID = " . $wpdb->usermeta . ".user_id AND meta_key = '" . $wpdb->prefix . "capabilities' ORDER BY {$wpdb->usermeta}.user_id" );
return $users;
}
/* This function is dedicated to Mike */
function kjgrc_is_wordpress_user($comment)
{
global $kjgrc_wordpress_users;
if ($kjgrc_wordpress_users == NULL) {
// be still compatible to wordpress 2.0.11
if (function_exists("get_users_of_blog")) {
$kjgrc_wordpress_users = get_users_of_blog();
} else {
$kjgrc_wordpress_users = kjgrc_get_users_of_blog();
}
}
foreach ($kjgrc_wordpress_users as $user) {
if (strcasecmp($comment->comment_author_email,$user->user_email) == 0 ) {
if ( (strcasecmp($comment->comment_author,$user->user_login) == 0) ||
(strcasecmp($comment->comment_author,$user->display_name) == 0) )
{
return TRUE;
}
}
}
return FALSE;
}
// function kjgrc_get_comments2 ($max_comments,$chars_per_comment,$chars_per_word,$format,$grouped_by_post,$caller)
function kjgrc_get_comments2 ($requested_comment_type,$caller)
{
// kjgrc_log("kjgrc_get_comments2 $caller");
// echo "kjgrc_get_comments2 $requested_comment_type $caller ";
global $wpdb;
$batch_number = 0;
$number_of_comments = 0;
$requested_number_of_comments = kjgrc_get_option($requested_comment_type,"max_comments");
$chars_per_comment = kjgrc_get_option($requested_comment_type,"chars_per_comment");
$chars_per_word = kjgrc_get_option($requested_comment_type,"chars_per_word");
$format = stripslashes(kjgrc_get_option($requested_comment_type,"format"));
$batch_size = $requested_number_of_comments * 3;
// grc
//x// $max_comments = kjgrc_get_option("grc","max_comments");
//x// $chars_per_comment = kjgrc_get_option("grc","chars_per_comment");
//x// $chars_per_word = kjgrc_get_option("grc","chars_per_word");
//x// $format = stripslashes(kjgrc_get_option("grc","format"));
//x// $sql_comment_type = "AND comment_type = '' ";
//x// if (kjgrc_get_option("grc","show_trackbacks") == 1) {
//x// $sql_comment_type = '';
//x// }
// grt
//// $max_comments = kjgrc_get_option("grt","max_comments");
//// $chars_per_comment = kjgrc_get_option("grt","chars_per_comment");
//// $chars_per_word = kjgrc_get_option("grt","chars_per_word");
//// $format = stripslashes(kjgrc_get_option("grt","format"));
//// $query = "SELECT DISTINCT $wpdb->comments.* FROM $wpdb->comments ".
//// "LEFT JOIN $wpdb->posts ON $wpdb->posts.ID=$wpdb->comments.comment_post_ID ".
//// $sql_join_post2cat .
//// "WHERE (post_status = 'publish' OR post_status = 'static') AND comment_approved= '1' AND post_password = '' ".
//// $sql_exlude_cat .
//// "AND ( comment_type = 'trackback' OR comment_type = 'pingback' ) ".
//// $sql_ignore_ip .
//// "ORDER BY comment_date DESC LIMIT $max_comments";
$sql_ignore_ip = '';
$sql_exclude_owner = '';
$grouped_by_post = 0;
if ($requested_comment_type == 'grt')
{
if (kjgrc_get_option("grt","ignore_ip") != '') {
$sql_ignore_ip = "AND comment_author_IP != '". kjgrc_get_option("grt","ignore_ip") ."' ";
}
$sql_comment_type = "( comment_type = 'trackback' OR comment_type = 'pingback' ) ";
}
if ($requested_comment_type == 'grc')
{
$sql_comment_type = "comment_type = '' ";
if (kjgrc_get_option("grc","show_trackbacks") == 1) {
$sql_comment_type = '1 ';
}
if (kjgrc_get_option("grc","exclude_blog_owner") == 1) {
$sql_exclude_owner = "AND user_id = '0' ";
}
if (kjgrc_get_option("grc","grouped_by_post")) {
$grouped_by_post = 1;
}
}
while ($comments_number < $requested_number_of_comments)
{
$query = "SELECT * from $wpdb->comments WHERE comment_approved= '1' AND " .
$sql_comment_type .
$sql_exclude_owner .
$sql_ignore_ip .
"ORDER BY comment_date DESC LIMIT " . ($batch_number*$batch_size) . ",$batch_size";
// echo "$query
";
$comments = $wpdb->get_results($query);
if (!$comments) {
$result .= "none";
break;
}
unset($missing_post);
foreach ($comments as $comment) {
if ($post_cache == NULL || ! array_key_exists($comment->comment_post_ID,$post_cache)) {
$missing_post[$comment->comment_post_ID] = 1;
}
}
if ($missing_post != NULL)
{
unset($comma_separated);
$comma_separated = implode(",", array_keys($missing_post));
if (empty($wpdb->term_relationships)) {
$query = "SELECT * from $wpdb->posts JOIN $wpdb->post2cat ON ID = post_id WHERE ID IN ($comma_separated);";
} else {
$query = "SELECT * from $wpdb->posts JOIN $wpdb->term_relationships ON ($wpdb->term_relationships.object_id=ID) WHERE ID IN ($comma_separated);";
}
// echo "$query ";
$posts = $wpdb->get_results($query);
foreach ($posts as $post) {
// echo "p: $post->ID ";
$post_cache[$post->ID] = $post;
if (empty($wpdb->term_relationships)) {
$cat_cache[$post->ID][$post->category_id] = 1;
} else {
$cat_cache[$post->ID][$post->term_taxonomy_id] = 1;
}
}
}
// echo "------- ";
// drop comments:
// 1. comment_approved = 1 -> in select
// 2. post_status = 'publish' OR post_status = 'static'
// 3. post_password = ''
// 4. AND user_id = '0' -> in select
// 5. AND comment_author_IP != '1.2.3.4' -> in select
// 6. "AND category_id != '$cat'
// 7. limit_comments_per_post
foreach ($comments as $comment)
{
if ($post_cache[$comment->comment_post_ID]->post_status != 'publish' &&
$post_cache[$comment->comment_post_ID]->post_status != 'static' )
{
// echo "drop $comment->comment_ID (". $post_cache[$comment->comment_post_ID]->post_status .") ";
continue;
}
if ($post_cache[$comment->comment_post_ID]->post_password != '') {
// echo "drop $comment->comment_ID (protected) ";
continue;
}
/* Optional additional check for wordpress users who are not logged in */
if ((kjgrc_get_option("grc","exclude_blog_owner") == 1) &&
(kjgrc_get_option("grc","exclude_blog_owner2") == 1)
)
{
if (kjgrc_is_wordpress_user($comment)) {
// echo "drop $comment->comment_ID (is_wordpress_user) ";
continue;
}
}
$exclude_cat = kjgrc_get_exclude_cat();
$is_in_excluded_cat = 0;
if (kjgrc_get_option("misc","exclude_cat_reverse") == 1) {
$is_in_excluded_cat = 1;
}
if ($exclude_cat) {
foreach ($exclude_cat as $cat) {
if ($cat_cache[$comment->comment_post_ID][$cat] == 1) {
$is_in_excluded_cat = 1;
if (kjgrc_get_option("misc","exclude_cat_reverse") == 1) {
$is_in_excluded_cat = 0;
}
// echo "drop $comment->comment_ID (is in excluded cat $cat) ";
}
}
if ($is_in_excluded_cat) {
continue;
}
}
// nur !trackbacks?
if ($grouped_by_post && kjgrc_get_option("grc","limit_comments_per_post") == 1) {
if ($comments_per_post_counter[$comment->comment_post_ID] >= kjgrc_get_option("grc","comments_per_post")) {
// echo "drop $comment->comment_ID (max nr cmt/post reached) ";
continue;
}
}
$comments_per_post_counter[$comment->comment_post_ID]++;
$comments_number++;
// $result .= "$comments_number [$batch_number] $comment->comment_ID ($comment->comment_content) ";
$fetched_comments[] = $comment;
if ($comments_number >= $requested_number_of_comments) {
break;
}
}
$batch_number++;
}
// comments are selected. now format them
if (!(strpos($format,"%gravatar") !== false))
$has_gravatar = 0;
else {
$has_gravatar = 1;
$gravatar_alt_url = kjgrc_get_option('gravatar','alt_url');
$gravatar_size = kjgrc_get_option('gravatar','size');
$gravatar_rating = kjgrc_get_option('gravatar','rating');
$gravatar_mpaa[0] = 'G';
$gravatar_mpaa[1] = 'PG';
$gravatar_mpaa[2] = 'R';
$gravatar_mpaa[3] = 'X';
$gravatar_options .= "&size=$gravatar_size";
$gravatar_options .= "&rating=" . $gravatar_mpaa[$gravatar_rating];
if (kjgrc_get_option('gravatar','alt_url') != '') {
$gravatar_options .= "&default=" . urlencode($gravatar_alt_url);
}
}
if (! $fetched_comments) {
return "";
}
foreach ($fetched_comments as $comment)
{
$trackback_title = '';
if (function_exists("polyglot_init")) {
// This looks like the wrong filter, but the_content deletes smileys when called from here
$comment_excerpt = apply_filters('single_post_title',$comment->comment_content);
} else {
$comment_excerpt = $comment->comment_content;
}
// comment_author,
$comment_type = "Comment";
if ($comment->comment_type == 'pingback')
{
$comment_type = "Pingback";
list($comment_author,$trackback_title) = kjgrc_parse_pingback($comment->comment_author);
if(strpos($comment_excerpt,'[...]') == 0)
$comment_excerpt = trim(substr($comment_excerpt,5));
if(strpos($comment_excerpt,'[...]') == strlen($comment_excerpt)-5)
$comment_excerpt = trim(substr($comment_excerpt,0,strlen($comment_excerpt)-5));
}
elseif ($comment->comment_type == 'trackback')
{
$comment_type = "Trackback";
$trackback_title = preg_replace("/^(.+?)<\/strong>.*/s","$1",$comment->comment_content);
$trackback_title = strip_tags($trackback_title);
$trackback_title = preg_replace("/[\n\t\r]/"," ",$trackback_title);
$trackback_title = preg_replace("/\s{2,}/"," ",$trackback_title);
$trackback_title = wordwrap($trackback_title,$chars_per_word,' ',1);
$comment_excerpt = preg_replace("/^.+?<\/strong>/","",$comment->comment_content,1);
$comment_author = $comment->comment_author;
}
else
{
$comment_author = $comment->comment_author;
if (!$comment_author)
$comment_author = "Anonymous";
}
$comment_excerpt = strip_tags(wptexturize($comment_excerpt));
$comment_excerpt = preg_replace("/[\n\t\r]/"," ",$comment_excerpt); // whitespace into 1 blank
$comment_excerpt = preg_replace("/\s{2,}/"," ",$comment_excerpt); // whitespace into 1 blank
$comment_excerpt = wordwrap($comment_excerpt,$chars_per_word,' ',1);
if ($trackback_title == '')
$trackback_title = $comment_excerpt;
$post_link = get_permalink($comment->comment_post_ID);
$comment_link = $post_link .
"#comment-$comment->comment_ID";
/* Does not work - polyglot uses global variables to access the comment/post data
if (function_exists("polyglot_init")) {
$comment_date = apply_filters('the_date',$comment->comment_date);
} else {
$comment_date = mysql2date(get_settings('date_format'),$comment->comment_date);
}
if (function_exists("polyglot_init")) {
$comment_time = apply_filters('the_time',$comment->comment_date);
} else {
//$comment_time = substr($comment->comment_date,11,5); // 2005-03-09 22:23:53
$comment_time = mysql2date(get_settings('time_format'),$comment->comment_date); // Thanks to Keith
}
*/
$comment_date = mysql2date(get_settings('date_format'),$comment->comment_date);
$comment_time = mysql2date(get_settings('time_format'),$comment->comment_date); // Thanks to Keith
if ($has_gravatar && $comment_author != '')
{
if ($md5_cache && array_key_exists($comment->comment_author,$md5_cache)) {
$gravatar_md5 = $md5_cache[$comment->comment_author];
} else {
$gravatar_md5 = md5($comment->comment_author_email);
$md5_cache[$comment->comment_author_email] = $gravatar_md5;
}
$comment_gravatar_url = "http://www.gravatar.com/avatar.php?" .
"gravatar_id=$gravatar_md5" .
$gravatar_options;
$comment_gravatar = "";
}
#$post = get_postdata($comment->comment_post_ID);
#$post_date = mysql2date(get_settings('date_format'),$post['Date']);
#$post_title = trim(htmlspecialchars(stripslashes($post['Title'])));
#$post = get_postdata($comment->comment_post_ID);
// *** insert cache for post data here
// $post = $wpdb->get_row("SELECT * from $wpdb->posts WHERE ID = $comment->comment_post_ID");
$post = $post_cache[$comment->comment_post_ID];
if (function_exists("polyglot_init")) {
$post_date = apply_filters('the_date',$post->post_date);
} else {
$post_date = mysql2date(get_settings('date_format'),$post->post_date);
}
// $post_title = trim(htmlspecialchars(stripslashes($post->post_title)));
$post_title = strip_tags(wptexturize($post->post_title));
if (function_exists("polyglot_init")) {
$post_title = apply_filters('single_post_title',$post_title);
}
$post_counter = $post->comment_count;
$author_url = $comment->comment_author_url;
if ($author_url == "http://")
$author_url = "";
if (empty($author_url) || $author_url == "http://")
$author_url_href = "";
$output = $format;
// Replace tags by values
$output = str_replace("%comment_link", $comment_link, $output);
$output = str_replace("%author_url_href", $author_url_href, $output);
$output = str_replace("%author_url", $author_url, $output);
$output = str_replace("%userid", $comment->user_id, $output);
$output = str_replace("%gravatar_url", $comment_gravatar_url, $output);
$output = str_replace("%gravatar", $comment_gravatar, $output);
// function author_image_path($authorID, $display = true, $type = 'url')
if (function_exists("author_image_path")) {
$profile_pict = author_image_path($comment->user_id,false,'url');
$output = str_replace("%profile_picture", $profile_pict, $output);
} else {
$output = str_replace("%profile_picture", '', $output);
}
$output = str_replace("%comment_author", $comment_author, $output);
$output = str_replace("%comment_date", $comment_date, $output);
$output = str_replace("%comment_time", $comment_time, $output);
//$output = str_replace("%time_since", 'time_since_' . $comment->unixdate . ' - ' . gmdate('U') .' = '. ($comment->unixdate-gmdate('U')), $output);
$utc_time = kjgrc_utc2unixtime($comment->comment_date_gmt); //2006-12-30 17:05:59
$output = str_replace("%time_since", "<%time_since:$utc_time>", $output);
$output = str_replace("%comment_type", $comment_type, $output);
$output = str_replace("%post_title", $post_title, $output);
$output = str_replace("%post_link", $post_link, $output);
$output = str_replace("%post_date", $post_date, $output);
$output = str_replace("%post_counter", $post_counter, $output);
/*
// Nice idea, but confuses users
//strip title or content?
$visible = strip_tags($output);
if (strpos($visible,'%comment_excerpt') !== false) {
$comment_excerpt = kjgrc_excerpt($comment_excerpt,$chars_per_comment,$chars_per_word,'%comment_excerpt',$output);
}
elseif (strpos($visible,'%trackback_title') !== false) {
$trackback_title = kjgrc_excerpt($trackback_title,$chars_per_comment,$chars_per_word,'%trackback_title',$output);
}
*/
$comment_excerpt = kjgrc_excerpt($comment_excerpt,$chars_per_comment,$chars_per_word,'%comment_excerpt',$output);
if (kjgrc_get_option("misc","convert_smileys")) {
if (function_exists("csm_convert") ) {
$comment_excerpt = csm_convert($comment_excerpt);
}
else {
if (get_settings('use_smilies')) {
$comment_excerpt = convert_smilies($comment_excerpt);
}
}
}
$trackback_title = kjgrc_excerpt($trackback_title,$chars_per_comment,$chars_per_word,'%trackback_title',$output);
$output = str_replace("%comment_excerpt", $comment_excerpt, $output);
$output = str_replace("%trackback_title", $trackback_title, $output);
// Replacement done
//$len = strlen(strip_tags($output));
//$output .= " [$comment_time]";
// . " (" . time_since(strtotime($comment->comment_date_gmt." GMT")) ." ago)";
// *** Das aber nur bei recent comments, nicht bei trackbacks!
// if (kjgrc_get_option("grc","limit_comments_per_post") == 1) {
// if (count($comment_list[$comment->comment_post_ID]) < kjgrc_get_option("grc","comments_per_post")) {
// $comment_list[$comment->comment_post_ID][] = $output;
// }
// } else {
// $comment_list[$comment->comment_post_ID][] = $output;
// }
$comment_list[$comment->comment_post_ID][] = $output;
if (($post_list == NULL) || ! array_key_exists($comment->comment_post_ID,$post_list)) {
$post_output = stripslashes(kjgrc_get_option("grc","grouped_by_post_a"));
$post_output = str_replace("%post_title", $post_title, $post_output);
$post_output = str_replace("%post_link", $post_link, $post_output);
$post_output = str_replace("%post_date", $post_date, $post_output);
$post_output = str_replace("%post_counter", $post_counter, $post_output);
$post_list[$comment->comment_post_ID] = $post_output;
}
$all_entries .= "\t$output\n";
if ($caller == 'grc_sample' || $caller == 'grt_sample')
break;
} // foreach comments
if ($grouped_by_post == 1)
{
$all_entries = '';
foreach (array_keys($post_list) as $post_id) {
$all_entries .= $post_list[$post_id] . "\n";
foreach ($comment_list[$post_id] as $tmp) {
$all_entries .= $tmp ."\n";
}
$all_entries .= kjgrc_get_option("grc","grouped_by_post_b") ."\n";
}
}
return $all_entries;
}
function kjgrc_utc2unixtime($utc_time)
{
$y = substr($utc_time,0,4);
$m = substr($utc_time,5,2);
$d = substr($utc_time,8,2);
$h = substr($utc_time,11,2);
$min = substr($utc_time,14,2);
$s = substr($utc_time,17,2);
//mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]])
//$ago = gmdate('U')-$utc_time;
return gmmktime($h,$min,$s,$m,$d,$y);
}
function kjgrc_format_seconds($seconds)
{
$d_str = "days";
$h_str = "hours";
$m_str = "minutes";
$s_str = "seconds";
$d = floor($seconds / (24 * 3600));
if ($d == 1) $d_str = "day";
$seconds = $seconds - ($d * 24 * 3600);
$h = floor($seconds / 3600);
if ($h == 1) $h_str = "hour";
$seconds = $seconds - ($h * 3600);
$m = floor($seconds / 60);
if ($m == 1) $m_str = "minute";
$seconds = $seconds - ($m * 60);
$s = $seconds;
if ($s == 1) $s_str = "second";
if ($d > 0) return "$d $d_str $h $h_str";
if ($h > 0) return "$h $h_str $m $m_str";
if ($m > 0) return "$m $m_str $s $s_str";
return "$s $s_str";
}
function kjgrc_excerpt ($text,$chars_per_comment,$chars_per_word,$tag,$output)
{
$length = strlen(str_replace($tag,"",strip_tags($output)));
$length = $chars_per_comment - $length;
$length = $length -2; // we will add three dots at the end
if ($length < 0) $length = 0;
if (strlen($text) > $length) {
$text = substr($text,0,$length);
$text = substr($text,0,strrpos($text,' '));
// last word exceeds max word length:
if ((strlen($text) - strrpos($text,' ')) > $chars_per_word) {
$text = substr($text,0,strlen($text)-3);
}
$text = $text . "...";
}
#$text = "[EXCERPT]: '$text'";
return "$text";
}
function widget_kj_get_recent_comments_init() {
if (! function_exists("register_sidebar_widget")) {
return;
}
function widget_get_recent_comments($args) {
global $kjgrc_we_are_a_widget,$kjgrc_widget_args;
$kjgrc_we_are_a_widget = TRUE;
$kjgrc_widget_args = $args;
get_recent_comments();
}
function widget_get_recent_comments_control() {
global $kjgrc_we_are_a_widget;
$kjgrc_we_are_a_widget = TRUE;
if ( $_POST['get_recent_comments-submit'] ) {
kjgrc_set_option("grc_sidebar_title",stripslashes($_POST['get_recent_comments-title']));
kjgrc_invalidate_cache();
}
echo '';
echo '';
echo 'More options are on the plugin page.';
}
register_sidebar_widget('Get Recent Comments', 'widget_get_recent_comments');
register_widget_control('Get Recent Comments', 'widget_get_recent_comments_control', 300, 100);
}
function widget_kj_get_recent_trackbacks_init() {
if (! function_exists("register_sidebar_widget")) {
return;
}
function widget_get_recent_trackbacks($args) {
global $kjgrc_we_are_a_widget,$kjgrc_widget_args;;
$kjgrc_we_are_a_widget = TRUE;
$kjgrc_widget_args = $args;
get_recent_trackbacks();
}
function widget_get_recent_trackbacks_control() {
global $kjgrc_we_are_a_widget;
$kjgrc_we_are_a_widget = TRUE;
if ( $_POST['get_recent_trackbacks-submit'] ) {
kjgrc_set_option("grt_sidebar_title",stripslashes($_POST['get_recent_trackbacks-title']));
kjgrc_invalidate_cache();
}
echo '';
echo '';
echo 'More options are on the plugin page.';
}
register_sidebar_widget('Get Recent Trackbacks', 'widget_get_recent_trackbacks');
register_widget_control('Get Recent Trackbacks', 'widget_get_recent_trackbacks_control', 300, 100);
}
add_action('admin_menu', 'kjgrc_add_options_page');
add_action('edit_comment','kjgrc_invalidate_cache');
add_action('delete_comment','kjgrc_invalidate_cache');
add_action('edit_post','kjgrc_invalidate_cache');
add_action('delete_post','kjgrc_invalidate_cache');
add_action('publish_post','kjgrc_invalidate_cache');
add_action('switch_theme', 'kjgrc_invalidate_cache');
add_action('wp_set_comment_status','kjgrc_invalidate_cache');
add_action('comment_post','kjgrc_handle_new_comment');
add_action('trackback_post','kjgrc_handle_new_comment');
add_action('pingback_post','kjgrc_handle_new_comment');
add_action('plugins_loaded', 'widget_kj_get_recent_comments_init');
add_action('plugins_loaded', 'widget_kj_get_recent_trackbacks_init');
?>