/** * Filter comments (contributions) on frontend * * Filters contributions by specified type and/or rating in query args * * @since 1.0.0 * @param array $comments * @return array */ public function filter_comments($comments) { global $post; if ('product' == $post->post_type) { $filters = wc_product_reviews_pro_get_current_comment_filters(); if ($filters && !empty($filters)) { foreach ($filters as $filter => $value) { switch ($filter) { # Filter by comment type case 'comment_type': $_comments = array(); foreach ($comments as $comment) { switch ($comment->comment_type) { case $value: $_comments[] = $comment; break; case 'contribution_comment': foreach ($comments as $parent) { if ($parent->comment_ID == $comment->comment_parent && $value == $parent->comment_type) { $_comments[] = $comment; } } break; } } $comments = $_comments; break; # Filter by review rating # Filter by review rating case 'rating': $_comments = array(); foreach ($comments as $comment) { switch ($comment->comment_type) { // Include reviews with matching rating case 'review': $rating = get_comment_meta($comment->comment_ID, 'rating', true); if ($rating == $value) { $_comments[] = $comment; } break; // Include comments that have a parent with matching rating // Include comments that have a parent with matching rating case 'contribution_comment': foreach ($_comments as $parent) { if ($parent->comment_ID == $comment->comment_parent) { $_comments[] = $comment; } } break; } } $comments = $_comments; break; # Filter by review qualifier # Filter by review qualifier case 'review_qualifier': $_comments = array(); $parts = explode(':', $value); // Make sure we actually have a qualifier value if (!isset($parts[1])) { break; } $filter_qualifier_value = $parts[1]; foreach ($comments as $comment) { $qualifier_value = get_comment_meta($comment->comment_ID, 'wc_product_reviews_pro_review_qualifier_' . $parts[0], true); if ($qualifier_value == $filter_qualifier_value) { $_comments[] = $comment; } } $comments = $_comments; break; # Filter by unanswered # Filter by unanswered case 'unanswered': $_comments = array(); global $wpdb; foreach ($comments as $comment) { if (!$comment->comment_parent) { $answers_count = $wpdb->get_var($wpdb->prepare("\n\t\t\t\t\t\t\t\t\t\tSELECT COUNT(comment_ID) FROM {$wpdb->comments}\n\t\t\t\t\t\t\t\t\t\tWHERE comment_parent = %d\n\t\t\t\t\t\t\t\t\t", $comment->comment_ID)); if (!$answers_count) { $_comments[] = $comment; } } } $comments = $_comments; break; # Filter by classification (positive/negative) # Filter by classification (positive/negative) case 'classification': $_comments = array(); global $wpdb; foreach ($comments as $comment) { $rating = get_comment_meta($comment->comment_ID, 'rating', true); if ($value == 'positive' && $rating >= 3) { $_comments[] = $comment; } if ($value == 'negative' && $rating < 3) { $_comments[] = $comment; } } $comments = $_comments; break; # Filter by helpfulness # Filter by helpfulness case 'helpful': $_comments = array(); foreach ($comments as $comment) { $contribution = wc_product_reviews_pro_get_contribution($comment); $ratio = $contribution->get_helpfulness_ratio(); if ($ratio >= 0.66) { $_comments[] = $comment; } } $comments = $_comments; break; # Apply filters if this is an unknown filter # Apply filters if this is an unknown filter default: /** * Allow plugins to filter comments using a custom filter * * @since 1.0.0 * @param array $comments The comments array. * @param array $args Associative array of arguments including the filter and value. */ $comments = apply_filters('wc_product_reviews_pro_filter_comments', $comments, array('filter' => $filter, 'value' => $value)); break; } } } } return $comments; }
/** * Get the no results text for the contribution type * * @return string */ public function get_no_results_text() { $text = __('There are no reviews yet', WC_Product_Reviews_Pro::TEXT_DOMAIN); $filters = wc_product_reviews_pro_get_current_comment_filters(); switch ($this->type) { case 'review': if (!empty($filters) && isset($filters['helpful']) && $filters['helpful']) { $text = __('There are no helpful reviews yet', WC_Product_Reviews_Pro::TEXT_DOMAIN); if (isset($filters['classification']) && 'positive' == $filters['classification']) { $text = __('There are no helpful positive reviews yet', WC_Product_Reviews_Pro::TEXT_DOMAIN); } else { if (isset($filters['classification']) && 'negative' == $filters['classification']) { $text = __('There are no helpful negative reviews yet', WC_Product_Reviews_Pro::TEXT_DOMAIN); } } } else { if (!empty($filters) && isset($filters['rating']) && $filters['rating']) { $text = sprintf(__(' There are no reviews with a %d-star rating yet', WC_Product_Reviews_Pro::TEXT_DOMAIN), $filters['rating']); } else { $text = __('There are no reviews yet', WC_Product_Reviews_Pro::TEXT_DOMAIN); } } break; case 'question': if (!empty($filters) && isset($filters['unanswered']) && $filters['unanswered']) { $text = __('There are no unanswered questions', WC_Product_Reviews_Pro::TEXT_DOMAIN); } else { $text = __('There are no questions yet', WC_Product_Reviews_Pro::TEXT_DOMAIN); } break; case 'photo': $text = __('There are no photos yet', WC_Product_Reviews_Pro::TEXT_DOMAIN); break; case 'video': $text = __('There are no videos yet', WC_Product_Reviews_Pro::TEXT_DOMAIN); break; } /** * Filter contribution type no results text * * @since 1.0.0 * @param string $text The text * @param string $type The contribution type */ return apply_filters('wc_product_reviews_pro_contribution_type_no_results_text', $text, $this->type); }
* DISCLAIMER * * Do not edit or add to this file if you wish to upgrade WooCommerce Product Reviews Pro to newer * versions in the future. If you wish to customize WooCommerce Product Reviews Pro for your * needs please refer to http://docs.woothemes.com/document/woocommerce-product-reviews-pro/ for more information. * * @package WC-Product-Reviews-Pro/Templates * @author SkyVerge * @copyright Copyright (c) 2015, SkyVerge, Inc. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 */ if (!defined('ABSPATH')) { exit; } // Exit if accessed directly $filters = wc_product_reviews_pro_get_current_comment_filters(); $current_type = isset($filters['comment_type']) ? $filters['comment_type'] : null; $current_rating = isset($filters['rating']) ? $filters['rating'] : null; ?> <h2 id="contributions-list-title"> <?php wc_product_reviews_pro_contributions_list_title($current_type, wc_product_reviews_pro_get_comment_count($comments, $current_type), $current_rating); ?> </h2> <div class="contributions-container"> <?php if (have_comments()) { ?>
/** * Render contributions list HTML * * @since 1.0.0 */ public function contributions_list() { // Bail out if product ID is not provided if (!isset($_REQUEST['product_id'])) { return; } global $wp_query; query_posts(array('p' => $_REQUEST['product_id'], 'post_type' => 'product', 'withcomments' => 1, 'feed' => 1)); if (have_posts()) { while (have_posts()) { the_post(); ob_start(); comments_template('', true); ob_end_clean(); $filters = wc_product_reviews_pro_get_current_comment_filters(); $current_type = isset($filters['comment_type']) ? $filters['comment_type'] : null; $current_rating = isset($filters['rating']) ? $filters['rating'] : null; wc_get_template('single-product/contributions-list.php', array('comments' => $wp_query->comments, 'current_type' => $current_type, 'current_rating' => $current_rating)); } } exit; }