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 = "Add Reply"; return $link; } /** * Prints link to edit reply section in admin * * @param int $reply_ID */ function cr_edit_reply_link($reply_ID) { echo cr_get_edit_reply_link($reply_ID); } /** * Prints link to delete a reply * * @param int $reply_ID */ function cr_delete_reply_link($reply_ID) { echo(ADMIN_URL."edit-comments.php?page=comment-replies/comment-replies.php&action=delete&reply_ID=$reply_ID") ; } /** * Returns link to edit reply in admin * * @param unknown_type $reply_ID * @return unknown */ function cr_get_edit_reply_link($reply_ID) { return (ADMIN_URL. "edit-comments.php?page=comment-replies/comment-replies.php&action=edit&reply_ID=$reply_ID"); } /** * Adds admin menu options * */ function cr_menu() { add_submenu_page('edit-comments.php', 'Author Comment Replies', 'Author Comment Replies', 1, __FILE__, 'cr_comment_replies_admin'); add_options_page('Author Comment Replies Options', 'Author Comment Replies', 5, __FILE__, 'cr_options_page'); } /** * Renders Admin Page * */ function cr_comment_replies_admin() { global $wpdb; $showlisting = true; $action = $_REQUEST['action']; if($action == 'edit' || $action == 'update' || $action == 'delete') { cr_reply_edit_page($showlisting); } if($showlisting) { cr_reply_admin_listing(); } } /** * Inserts reply into database * @param int $comment_ID * @param object $userInfo * @param string $reply */ function cr_add_reply($comment_ID, $userInfo, $reply) { global $wpdb; $table_name = $wpdb->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
Comment reply was added
Comment reply has been deleted
$r
"; } if ($comments) { $offset = $offset + 1; $start = " start='$offset'"; echo "$itemsPerPage ) { $total_pages = ceil( $total / $itemsPerPage ); $r = ''; if ( 1 < $page ) { $args['apage'] = ( 1 == $page - 1 ) ? FALSE : $page - 1; $r .= '« '. __('Previous Page') .'' . "\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 .= ''. __('Next Page') .' »' . "\n"; } echo "
$r
"; ?>'.$text.'
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) )?>
comment_post_ID)) { ?> [Edit Reply] [Delete Reply]reply_content)?>