Skip to content

Instantly share code, notes, and snippets.

@Pross
Last active August 29, 2015 14:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pross/fe0b28b42ce2d83ab68e to your computer and use it in GitHub Desktop.
Save Pross/fe0b28b42ce2d83ab68e to your computer and use it in GitHub Desktop.
<?php
/*
Seperate Woocommerce reviews from comments and make a reviews moderation panel.
*/
class PL_Woo_Review_Mods {
function __construct() {
add_action( 'current_screen', array( $this, 'check_current_page' ), 10, 2 );
add_action( 'admin_menu', array( $this, 'add_product_reviews' ) );
}
function check_current_page( $screen ) {
if ( $screen->id == 'edit-comments' ) {
add_filter( 'comments_clauses', array( $this, 'separate_comments_and_review' ), 10, 2 );
add_filter( 'comment_status_links', array( $this, 'change_comment_status_link' ) );
if( isset( $_GET['post_type'] ) && $_GET['post_type'] == 'product' ) {
add_filter( 'manage_edit-comments_columns', array( $this, 'comment_columns' ) );
add_filter( 'manage_comments_custom_column', array( $this, 'comment_column' ), 10, 2 );
}
}
}
function comment_column( $column, $comment_ID ) {
if ( 'review_custom_column' == $column ) {
$rating = get_comment_meta( $comment_ID, 'rating', true );
echo $rating;
}
}
function comment_columns( $columns ) {
$columns['review_custom_column'] = __( 'Rating' );
return $columns;
}
function change_comment_status_link( $status_links ) {
if( isset( $_GET['post_type'] ) ) {
$status_links['all'] = '<a href="edit-comments.php?post_type=product&comment_status=all">All</a>';
$status_links['moderated'] = '<a href="edit-comments.php?post_type=product&comment_status=moderated">Pending</a>';
$status_links['approved'] = '<a href="edit-comments.php?post_type=product&comment_status=approved">Approved</a>';
$status_links['spam'] = '<a href="edit-comments.php?post_type=product&comment_status=spam">Spam</a>';
$status_links['trash'] = '<a href="edit-comments.php?post_type=product&comment_status=trash">Trash</a>';
}
return $status_links;
}
function separate_comments_and_review( $clauses, $wp_comment_query ) {
global $wpdb;
if ( ! $clauses['join'] ) {
$clauses['join'] = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
}
if( ! empty( $_GET['post_type'] ) && $_GET['post_type'] == 'product' ) {
if ( ! $wp_comment_query->query_vars['post_type'] ) {}
$clauses['where'] .= $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", 'product' );
} else {
if ( ! $wp_comment_query->query_vars['post_type' ] )
$clauses['where'] .= $wpdb->prepare( " AND {$wpdb->posts}.post_type != %s", 'product' );
}
return $clauses;
}
function add_product_reviews() {
global $wpdb;
$query = "SELECT *
FROM wp_comments
INNER JOIN wp_posts ON ( wp_posts.ID = wp_comments.comment_post_ID )
WHERE wp_posts.post_type = 'product'
AND wp_comments.comment_approved = '0'";
$result = $wpdb->get_row($query);
$count = $wpdb->num_rows;
if( $count > 0 ) {
$alert = sprintf( ' <span class="awaiting-mod count-%s"><span class="pending-count">%s</span></span>',
$count,
$count
);
} else {
$alert = '';
}
$post_type = 'product';
$text = sprintf( 'Reviews%s', $alert );
add_menu_page( $text,$text,'manage_options',"edit-comments.php?post_type={$post_type}",'','',59);
}
}
if( is_admin() )
new PL_Woo_Review_Mods;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment