Last active
February 1, 2017 07:38
-
-
Save phlbnks/605c512f78c998b5e908 to your computer and use it in GitHub Desktop.
Show WordPress "authors" only the comments on their posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Clone of wp_count_comments from WP4.2. | |
*/ | |
function myblogs_count_comments( $post_id = 0 ) { | |
global $wpdb; | |
$post_id = (int) $post_id; | |
/** | |
* Filter the comments count for a given post. | |
* | |
* @since 2.7.0 | |
* | |
* @param array $count An empty array. | |
* @param int $post_id The post ID. | |
*/ | |
if ( !empty($stats) ) | |
return $stats; | |
$count = wp_cache_get("comments-{$post_id}", 'counts'); | |
if ( false !== $count ) | |
return $count; | |
$where = ''; | |
if ( $post_id > 0 ) | |
$where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id ); | |
$count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A ); | |
$total = 0; | |
$approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed'); | |
foreach ( (array) $count as $row ) { | |
// Don't count post-trashed toward totals | |
if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) | |
$total += $row['num_comments']; | |
if ( isset( $approved[$row['comment_approved']] ) ) | |
$stats[$approved[$row['comment_approved']]] = $row['num_comments']; | |
} | |
$stats['total_comments'] = $total; | |
foreach ( $approved as $key ) { | |
if ( empty($stats[$key]) ) | |
$stats[$key] = 0; | |
} | |
$stats = (object) $stats; | |
wp_cache_set("comments-{$post_id}", $stats, 'counts'); | |
return $stats; | |
} | |
/** | |
* In WP Admin filter wp_count_coments so it shows current users comments only | |
* Runs only for the Author role. | |
*/ | |
add_filter('wp_count_comments','myblogs_comments_count'); | |
function myblogs_comments_count() { | |
global $pagenow; | |
if(is_admin() && !current_user_can( 'edit_others_posts' )){ | |
global $user_ID; | |
get_currentuserinfo(); | |
if ( false === ( $stats = get_transient( 'myblogs_author'.$user_ID.'_comments' ) ) ) { | |
$stats = array('moderated'=>0,'approved'=>0,'post-trashed'=>0,'trash'=>0,'total_comments'=>0,'spam'=>0); | |
$the_query = new WP_Query( array('author' => $user_ID,'posts_per_page' => -1) ); | |
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); | |
$comments = myblogs_count_comments(get_the_id()); | |
$stats['moderated'] = $stats['moderated'] + $comments->moderated; | |
$stats['approved'] = $stats['approved'] + $comments->approved; | |
$stats['post-trashed'] = $stats['post-trashed'] + $comments->{'post-trashed'}; | |
$stats['trash'] = $stats['trash'] + $comments->trash; | |
$stats['total_comments'] = $stats['total_comments'] + $comments->total_comments; | |
$stats['spam'] = $stats['spam'] + $comments->spam; | |
endwhile; | |
endif; | |
wp_reset_postdata(); | |
set_transient( 'myblogs_author'.$user_ID.'_comments', $stats, 60 * 30 ); | |
} | |
return (object) $stats; | |
} | |
} | |
/** | |
* In WP Admin filter Edit-Comments.php so it shows current users comments only | |
* Runs only for the Author role. | |
*/ | |
add_filter('pre_get_comments','myblogs_user_comments_only'); | |
function myblogs_user_comments_only($query){ | |
global $pagenow; | |
if('edit-comments.php' != $pagenow || $query->is_admin) | |
return $query; | |
if( !current_user_can( 'edit_others_posts' ) ) { | |
global $user_ID; | |
$query->query_vars['post_author'] = $user_ID; | |
} | |
return $query; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment