PlayStation.Blog to visually associate reader comments and author responses. Version: 1.04 Author: Michael Pretty (cnp_studio) Author URI: http://cnpstudio.com */ /* Copyright 2008 Michael Pretty (email : mike@cnpstudio.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ //Set the URL to the admin section IF(!defined('ADMIN_DIR')) { define('ADMIN_DIR', "/wp-admin/"); } function cr_init() { IF(!defined('ADMIN_URL')) { define('ADMIN_URL', get_settings("siteurl").ADMIN_DIR); } define("DEFAULT_AUTH_MODE", "default"); define("AUTHOR_AUTH_MODE", "authors"); //Set the Authentication Mode here //Default allows only users who can modify the post to edit/add replies //Author mode allows all authors to edit/add replies define("CURRENT_AUTH_MODE", DEFAULT_AUTH_MODE); define('CR_BASE_NAME', plugin_basename('comment-replies/comment-replies.php')); define('CR_OPTION_PAGE', 'options-general.php?page='.$cr_base_name); define('CR_BASE_PAGE', 'edit-comments.php?page='.$cr_base_name); add_action('admin_menu', 'cr_menu'); add_action('wp_print_scripts', 'cr_includescript'); IF(CURRENT_AUTH_MODE == DEFAULT_AUTH_MODE) { add_filter('edit_comment_link', 'cr_add_reply_comment_link', 10, 2); } } add_action('plugins_loaded', 'cr_init'); /** * Includes javascript file in post header. * */ function cr_includescript() { global $post; if($post != null && cr_user_can_edit_reply($post->ID)) { echo ''; } } /** * Function used to determine whether current user can add/edit replies. * @param int $post_ID * @return boolean */ function cr_user_can_edit_reply($post_ID) { if(CURRENT_AUTH_MODE == AUTHOR_AUTH_MODE) { static $canEditReply = null; if($canEditReply === null) { $userInfo = wp_get_current_user(); if($userInfo->wp_capabilities['author']|| $userInfo->wp_capabilities['administrator']) { $canEditReply = true; } else { $canEditReply = false; } } return $canEditReply; } else //DEFAULT_AUTH_MODE { return current_user_can('edit_post', $post_ID); } } /** * Enter description here... * * @param unknown_type $link * @param unknown_type $comment_ID * @return unknown */ function cr_add_reply_comment_link($link, $comment_ID) { return $link . "   ". cr_get_add_reply_link($comment_ID, true); } /** * Returns link for adding replies * * @param int $comment_ID * @param bool $add_javascript add javascript to bypass admin page. * @return string */ function cr_get_add_reply_link($comment_ID, $add_javascript = false) { $link = "prefix . 'comment_replies'; $sql = $wpdb->prepare("INSERT INTO $table_name (comment_ID, reply_author, reply_author_email, reply_author_url, reply_date, reply_date_gmt, reply_content, user_id) VALUES (%d, %s, %s, %s, %s, %s, %s, %d)", $comment_ID, $userInfo->nickname, $userInfo->user_email, $userInfo->user_url, current_time('mysql'), current_time('mysql', 1), $reply, $userInfo->ID); if($wpdb->query($sql)) { return true; } else { return false; } } /** * Renders edit page under admin section * * @param ref bool $showlisting */ function cr_reply_edit_page(&$showlisting) { global $wpdb; $table_name = $wpdb->prefix . 'comment_replies'; $comment_ID = (int)$_REQUEST['comment_ID']; $reply_ID = (int)$_REQUEST['reply_ID']; $error = false; if($reply_ID > 0) { $sql = "SELECT C.*, R.* FROM $wpdb->comments C JOIN $table_name R ON R.comment_ID = C.comment_ID WHERE R.reply_ID = $reply_ID"; $toprow_title = 'Editing Reply #'.$reply_ID; } elseif($comment_ID > 0) { $sql = "SELECT C.* FROM $wpdb->comments C WHERE comment_ID = $comment_ID"; $toprow_title = 'Replying to Comment #'.$comment_ID; } else { $error = true; } if(!$error) { global $comment; $comment = $wpdb->get_row($sql); if($_REQUEST['action'] == 'update') { $userInfo = wp_get_current_user(); if($reply_ID > 0) { $sql = $wpdb->prepare("UPDATE $table_name SET reply_content = %s WHERE reply_ID = %d", $_REQUEST['content'], $reply_ID); if($wpdb->query($sql)) { echo '

Comment reply was updated

'; } } else { //$sql = $wpdb->prepare("INSERT INTO $table_name (comment_ID, reply_author, reply_author_email, reply_author_url, reply_date, reply_date_gmt, reply_content, user_id) // VALUES (%d, %s, %s, %s, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, %s, %d)", //$comment_ID, $userInfo->nickname, $userInfo->user_email, $userInfo->user_url, $_REQUEST['content'], $userInfo->ID); if(cr_add_reply($comment_ID, $userInfo, $_REQUEST['content'])) { echo '

Comment reply was added

'; } } return ; } elseif($_REQUEST['action'] == 'delete') { $reply_ID = (int)$_REQUEST['reply_ID']; $sql = $wpdb->prepare("DELETE FROM $table_name WHERE reply_ID = %d", $reply_ID); if($wpdb->query($sql)) { echo '

Comment reply has been deleted

'; } return ; } else { $showlisting = false; ?>

comment_post_ID, OBJECT, 'display'); $post_title = wp_specialchars( $post->post_title, 'double' ); $post_title = ('' == $post_title) ? "# $comment->comment_post_ID" : $post_title; ?>

comment_author_email) { ?>| comment_author_url && 'http://' != $comment->comment_author_url) { ?> | | | Comment Date: |

reply_content, 'content', 'newcomment_author_url'); ?>

prefix . 'comment_replies'; ?>
comments C JOIN $table_name R ON R.comment_ID = C.comment_ID WHERE C.comment_approved = '1' ORDER BY reply_date_gmt DESC LIMIT $start, $itemsPerPage"; $comments = $wpdb->get_results($sql); $total = $wpdb->get_var("SELECT Count(*) FROM $wpdb->comments C JOIN $table_name R ON R.comment_ID = C.comment_ID WHERE C.comment_approved = '1'"); if($total > $itemsPerPage) { $total_pages = ceil( $total / $itemsPerPage ); $r = ''; if ( 1 < $page ) { $args['apage'] = ( 1 == $page - 1 ) ? FALSE : $page - 1; $r .= '' . "\n"; } if ( ( $total_pages = ceil( $total / $itemsPerPage ) ) > 1 ) { for ( $page_num = 1; $page_num <= $total_pages; $page_num++ ) { if ( $page == $page_num ) { $r .= "$page_num\n"; } else { $p = false; if ( $page_num < 3 || ( $page_num >= $page - 3 && $page_num <= $page + 3 ) || $page_num > $total_pages - 3 ) { $args['apage'] = ( 1 == $page_num ) ? FALSE : $page_num; $r .= '' . ( $page_num ) . "\n"; $in = true; } elseif ( $in == true ) { $r .= "...\n"; $in = false; } } } } if ( ( $page ) * $itemsPerPage < $total || -1 == $total ) { $args['apage'] = $page + 1; $r .= '' . "\n"; } echo ""; } if ($comments) { $offset = $offset + 1; $start = " start='$offset'"; echo "
    "; $i = 0; global $comment; foreach ($comments as $comment) { ++$i; $class = ''; if ($i % 2) $class = ' alternate'; echo "
  1. "; ?>

    comment_post_ID, OBJECT, 'display'); $post_title = wp_specialchars( $post->post_title, 'double' ); $post_title = ('' == $post_title) ? "# $comment->comment_post_ID" : $post_title; ?>

    comment_author_email) { ?>| comment_author_url && 'http://' != $comment->comment_author_url) { ?> | | | Comment Date: |

    reply_author . ' | Reply Date: '. mysql2date( get_option('time_format'), $comment->reply_date_gmt) )?>

    reply_content)?>

    comment_post_ID)) { ?> [Edit Reply] [Delete Reply]

$itemsPerPage ) { $total_pages = ceil( $total / $itemsPerPage ); $r = ''; if ( 1 < $page ) { $args['apage'] = ( 1 == $page - 1 ) ? FALSE : $page - 1; $r .= '' . "\n"; } if ( ( $total_pages = ceil( $total / $itemsPerPage ) ) > 1 ) { for ( $page_num = 1; $page_num <= $total_pages; $page_num++ ) : if ( $page == $page_num ) : $r .= "$page_num\n"; else : $p = false; if ( $page_num < 3 || ( $page_num >= $page - 3 && $page_num <= $page + 3 ) || $page_num > $total_pages - 3 ) : $args['apage'] = ( 1 == $page_num ) ? FALSE : $page_num; $r .= '' . ( $page_num ) . "\n"; $in = true; elseif ( $in == true ) : $r .= "...\n"; $in = false; endif; endif; endfor; } if ( ( $page ) * $itemsPerPage < $total || -1 == $total ) { $args['apage'] = $page + 1; $r .= '' . "\n"; } echo ""; ?>
Comment Template Updated
'; } if(empty($text)) { $text = ''.__('No Comment Reply Options Updated', 'comment_replies').''; } } ?>

Author Comment Replies

'.$text.'

'; } ?>
Reply Template:

Allowed Variables:
- %REPLY_USERID%
- %REPLY_AUTHOR%
- %REPLY_DATE%
- %REPLY_TIME%
- %REPLY_TEXT%
prefix . 'comment_replies'; $sql = "SELECT DISTINCT R.reply_ID, R.comment_ID, R.reply_author, R.reply_author_email, R.reply_author_url, R.reply_date, R.reply_content, R.user_id FROM $reply_table R JOIN $wpdb->comments C ON C.comment_ID = R.comment_ID WHERE C.comment_post_ID = $comment->comment_post_ID AND C.comment_approved = '1' AND comment_type = '' ORDER BY reply_date_gmt"; $reply_results = $wpdb->get_results($sql); $replies = array(); foreach ($reply_results as $reply) { if($replies[$reply->comment_ID] === null) { $replies[$reply->comment_ID] = array(); } $replies[$reply->comment_ID][] = $reply; } } $replyTemplate = stripslashes(get_option('cr_reply_template')); $responseText = ""; //var_dump($replies); if(CURRENT_AUTH_MODE == AUTHOR_AUTH_MODE && cr_user_can_edit_reply($comment->comment_post_ID)) { $responseText.= "".cr_get_add_reply_link($comment->comment_ID, true)."
"; } if(count($replies) > 0 && count($replies[$comment->comment_ID]) > 0) { foreach($replies[$comment->comment_ID] as $reply) { $replyText = $replyTemplate; $replyText = str_replace("%REPLY_USERID%",$reply->user_id, $replyText); $replyText = str_replace("%REPLY_DATE%", mysql2date( get_option('date_format'), $reply->reply_date), $replyText); $replyText = str_replace("%REPLY_TIME%", mysql2date( get_option('time_format'), $reply->reply_date), $replyText); $author = $reply->reply_author; if(strlen($author) > 30) { $author = substr($author, 0, 30); } $replyText = str_replace("%REPLY_AUTHOR%", $author , $replyText); $replyText = str_replace("%REPLY_TEXT%", apply_filters('comment_text', stripslashes($reply->reply_content)), $replyText); $responseText.= $replyText; } } echo($responseText); } /** * Comment Replies Installation Method * */ function cr_comment_replies_install() { global $wpdb; $installed_ver = get_option('comment_replies_version'); $comment_replies_version = '1.04'; if($installed_ver != $comment_replies_version) { update_option("comment_replies_version", $comment_replies_version); } $table_name = $wpdb->prefix . 'comment_replies'; add_option("cr_reply_template", cr_getDefaultTemplate('reply')); $sql = "CREATE TABLE " . $table_name . " ( reply_ID bigint(20) UNSIGNED NOT NULL auto_increment, comment_ID bigint(20) UNSIGNED NOT NULL, reply_author tinytext collate latin1_general_ci NOT NULL, reply_author_email varchar(100) collate latin1_general_ci NOT NULL default '', reply_author_url varchar(200) collate latin1_general_ci NOT NULL default '', reply_date datetime NOT NULL default '0000-00-00 00:00:00', reply_date_gmt datetime NOT NULL default '0000-00-00 00:00:00', reply_content text collate latin1_general_ci NOT NULL, user_id bigint(20) NOT NULL default '0', PRIMARY KEY reply_ID (reply_ID), KEY comment_ID (comment_ID) );"; if(!function_exists("dbDelta")) { require_once(ABSPATH.ADMIN_DIR.'/upgrade-functions.php'); } dbDelta($sql); } add_action('activate_comment-replies/comment-replies.php', 'cr_comment_replies_install'); /** * Returns default comment template * * @return string */ function cr_getDefaultTemplate($template) { switch ($template) { case 'reply': $tmp = '%REPLY_AUTHOR% says
%REPLY_DATE% at %REPLY_TIME%'."\n". '%REPLY_TEXT%'."\n"; break; } return $tmp; } ?>