Skip to content

Instantly share code, notes, and snippets.

@bradyvercher
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradyvercher/8eaefa5f99922ab2fb89 to your computer and use it in GitHub Desktop.
Save bradyvercher/8eaefa5f99922ab2fb89 to your computer and use it in GitHub Desktop.
Rough proof of concept to display blog posts similar to a topic view in a forum. Save the file as a plugin and drop the [discussion] shortcode in a page.
<?php
/**
* Plugin Name: Discussion View
* Version: 0.1.0
* Description: Display blog posts similar to a the topic view in a forum.
* Author: Brady Vercher
*/
class DiscussionView {
public function load() {
add_action( 'init', array( $this, 'init' ) );
add_action( 'wp_head', array( $this, 'styles' ) );
}
public function init() {
add_shortcode( 'discussion', array( $this, 'shortcode_handler' ) );
}
public function shortcode_handler( $attr = array() ) {
global $wpdb;
$sql = "SELECT c.comment_post_ID, c.comment_author, d.max_date as comment_date, p.post_title, p.comment_count, u.display_name
FROM $wpdb->comments c
INNER JOIN ( SELECT comment_post_ID, MAX( comment_date ) max_date FROM $wpdb->comments GROUP BY comment_post_ID ) d
INNER JOIN $wpdb->posts p
INNER JOIN $wpdb->users u
ON c.comment_post_ID=d.comment_post_ID AND c.comment_date=d.max_date AND c.comment_post_ID=p.ID AND p.post_author=u.ID
WHERE c.comment_approved='1' AND comment_type='' AND post_status='publish' AND post_password='' AND post_type='post'
ORDER BY comment_date DESC
LIMIT 30";
$topics = $wpdb->get_results( $sql );
ob_start();
if ( ! empty( $topics ) ) {
?>
<table cellpadding="0" cellspacing="0" border="0" class="discussion-view">
<thead>
<tr>
<th>Post Title</th>
<th style="text-align: center">Comments</th>
<th>Last Commenter</th>
<th>Last Comment (CST)</th>
</tr>
</thead>
<tbody id="the-list">
<?php foreach ( $topics as $topic ) : ?>
<tr class="<?php echo $this->get_row_classes( $topic ); ?>">
<td>
<a href="<?php echo get_permalink( $topic->comment_post_ID ); ?>#comments"><?php echo $topic->post_title; ?></a>
<span class="dv-post-author"><em>by <?php echo $topic->display_name; ?></em></span>
</td>
<td class="dv-comment-count"><?php echo $topic->comment_count; ?></td>
<td><?php echo $topic->comment_author; ?></td>
<td class="dv-datetime"><?php echo $this->format_datetime( $topic->comment_date ); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
} else {
echo '<div class="user_content"><p>No comments yet!</p></div>';
}
return ob_get_clean();
}
public function get_row_classes( $topic ) {
$classes = array();
if ( mysql2date( 'Y-m-j', $topic->comment_date) == date( 'Y-m-j', current_time( 'timestamp', 1 ) ) ) {
$classes[] = 'today';
}
return implode( ' ', $classes );
}
public function format_datetime( $datetime ) {
$date = mysql2date( 'Y-m-j', $datetime );
if ( $date == date( 'Y-m-j', current_time( 'timestamp', 1 ) ) ) {
$display = 'Today ' . mysql2date( 'g:i A', $datetime );
} elseif ( $date == date( 'Y-m-j', current_time( 'timestamp', 1 ) - 60*60*24 ) ) {
$display = 'Yesterday '. mysql2date( 'g:i A', $datetime );
} else {
$display = date( 'M j, Y g:i A', strtotime( $datetime ) );
}
return $display;
}
public function styles() {
?>
<style type="text/css">
.discussion-view { width: 100%; border-collapse: collapse; border: 1px solid #e1dfd6; border-width: 1px 1px 0 1px;}
.discussion-view th { padding: 8px 5px; color: #444; font-weight: bold; background-color: #f3f3ea;}
.discussion-view tbody tr:nth-child(odd) { background-color: #f6f6f6;}
.discussion-view tbody tr.today td a { font-weight: bold;}
.discussion-view tbody tr.today td.dv-datetime { font-weight: bold;}
.discussion-view td { padding: 5px; border: 1px solid #e1dfd6; border-width: 1px 1px 1px 0;}
.discussion-view td.dv-comment-count { text-align: center;}
.discussion-view td .dv-post-author { display: block;}
</style>
<?php
}
}
$discussion_view = new DiscussionView();
add_action( 'plugins_loaded', array( $discussion_view, 'load' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment