"SELECT ID AS id, post_title, comment_count FROM `" . $wpdb->posts . "` WHERE post_type = 'post' AND post_status = 'publish' AND comment_count > 0 ORDER BY comment_count DESC LIMIT 5",
// When do your posts generate the most comments?
'comments_per_day' => "SELECT COUNT(comment_ID) AS count, DATE_FORMAT(comment_date, '%w') AS day FROM `" . $wpdb->comments . "` WHERE comment_approved = 1 AND DATE_FORMAT(comment_date, '%w') IS NOT NULL GROUP BY DATE_FORMAT(comment_date, '%w') ORDER BY day ASC",
// Total comments approved
'comments_total' => "SELECT COUNT(comment_ID) AS count FROM `" . $wpdb->comments . "` WHERE comment_approved = '1'",
// Total words per comments approved
'comments_word' => "SELECT SUM(LENGTH(comment_content) - LENGTH(REPLACE(comment_content, ' ', ''))+1) AS count FROM `" . $wpdb->comments . "` WHERE comment_approved = '1'",
// 5 authors who comment the most
'comments_per_author' => "SELECT u.ID, u.user_login, u.display_name, COUNT(c.comment_ID) AS comment_count FROM `" . $wpdb->comments . "` c LEFT JOIN `" . $wpdb->users . "` u ON c.user_id = u.ID WHERE c.user_id != 0 GROUP BY c.user_id ORDER BY comment_count DESC LIMIT 5",
// Total posts published
'posts_total' => "SELECT COUNT(ID) FROM `" . $wpdb->posts . "` WHERE post_type = 'post' AND post_status = 'publish'",
// Total words per posts approved
'posts_word' => "SELECT SUM(LENGTH(post_content) - LENGTH(REPLACE(post_content, ' ', ''))+1) AS count FROM `" . $wpdb->posts . "` WHERE post_status = 'publish'",
// Date of first post
'first_post' => "SELECT post_date FROM `" . $wpdb->posts . "` WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date ASC LIMIT 1"
);
// First post
$firstpost = $wpdb->get_row( $queries['first_post'] );
$dayselapsed = date_diff_days($firstpost->post_date, date('Y-m-d H:i:s'));
// Comment Registration required
$commentregistration = (bool)get_option('comment_registration');
add_action('admin_init', 'abm_admin_init');
add_action('wp_dashboard_setup', 'abm_dashboard_init');
function abm_admin_init() {
wp_register_style( 'advanced-blog-metrics', plugins_url( 'style.css', __FILE__ ) );
wp_enqueue_style( 'advanced-blog-metrics' );
}
function abm_dashboard_init() {
wp_add_dashboard_widget( 'dashboard_comments_per_post', '5 posts which generate the most comments', 'dashboard_comments_per_post' );
wp_add_dashboard_widget( 'dashboard_comments_per_day', 'When do your posts generate the most comments?', 'dashboard_comments_per_day' );
wp_add_dashboard_widget( 'dashboard_comments', 'Comments', 'dashboard_comments' );
wp_add_dashboard_widget( 'dashboard_comments_per_author', '5 authors who comment the most', 'dashboard_comments_per_author' );
wp_add_dashboard_widget( 'dashboard_posts', 'Posts', 'dashboard_posts' );
}
// Posts which generate the most comments
function dashboard_comments_per_post() {
global $queries, $wpdb;
$posts = $wpdb->get_results( $queries['comments_per_post'] );
$html = '
';
$html.= '| Post | Comments |
';
$html.= '';
foreach ( $posts as $post ) {
$html.= '';
$html.= '| ' . $post->post_title . ' | ';
$html.= '';
$html.= '
';
}
$html.= '';
$html.= '
';
echo $html;
}
// When do your posts generate the most comments?
function dashboard_comments_per_day() {
global $queries, $wpdb, $days;
$posts = $wpdb->get_col( $queries['comments_per_day'], 0 );
$max = max($posts);
$html.= '';
$html.= '';
for ($num_day = 0; $num_day <= 6; $num_day++) {
$html.= '| ';
$html.= '' . $posts[$num_day] . '';
$html.= '';
$html.= ' | ';
}
$html.= '
';
for ($num_day = 0; $num_day <= 6; $num_day++) {
$html.= '| ' . strtoupper( substr( $days[$num_day], 0, 3 ) ) . ' | ';
}
$html.= '
';
$html.= '
';
echo $html;
}
// Comments
function dashboard_comments() {
global $queries, $wpdb, $dayselapsed;
$total = current( $wpdb->get_col( $queries['comments_total'], 0 ) );
$posts = current( $wpdb->get_col( $queries['posts_total'], 0 ) );
$words = current( $wpdb->get_col( $queries['comments_word'], 0 ) );
$html.= '';
$html.= '';
$html.= '';
$html .= '| Approved comments | ';
$html .= 'Comments per Day | ';
$html .= 'Comments per Post | ';
$html .= 'Words per Comment | ';
$html.= '
';
$html.= '';
$html.= '';
$html.= '';
$html.= '| ' . $total . ' | ';
$html.= '' . round( $total / $dayselapsed, 2 ) . ' | ';
$html.= '' . round( $total / $posts, 1 ) . ' | ';
$html.= '' . round( $words / $total ) . ' | ';
$html.= '
';
$html.= '';
$html.= '
';
echo $html;
}
// Authors who comment the most
function dashboard_comments_per_author() {
global $queries, $wpdb, $commentregistration;
if ($commentregistration) {
$authors = $wpdb->get_results($queries['comments_per_author']);
}
$html = '';
$html.= '| Author | Comments |
';
if ($commentregistration) {
foreach ($authors as $author) {
$html.= '';
$html.= '| ' . $author->display_name . ' | ';
$html.= '';
$html.= '
';
}
} else {
$html.= 'Note that you need to check "Users must be registered and logged in to comment" in the Wordpress Settings->Discussion to see data in the "5 authors who comments the most" widget. View discussion options |
';
}
$html.= '
';
echo $html;
}
// Posts
function dashboard_posts() {
global $queries, $wpdb, $dayselapsed;
$total = current( $wpdb->get_col( $queries['posts_total'], 0 ) );
$comments = current( $wpdb->get_col( $queries['comments_total'], 0 ) );
$words = current( $wpdb->get_col( $queries['posts_word'], 0 ) );
$html.= '';
$html.= '';
$html .= '| Posts | ';
$html .= 'Posts per Day | ';
$html .= 'Comments per Post | ';
$html .= 'Words per Post | ';
$html.= '
';
$html.= '| ' . $total . ' | ';
$html.= '' . round( $total / $dayselapsed, 2 ) . ' | ';
$html.= '' . round( $comments / $total, 1 ) . ' | ';
$html.= '' . round( $words / $total ) . ' | ';
$html.= '
';
$html.= '
';
echo $html;
}
function date_diff_days($date1, $date2) {
$s = strtotime( $date2 ) - strtotime( $date1 );
$d = intval( $s / 86400 ) + 1;
return "$d";
}