| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Template for advanced reviews items. |
| 5 |
* |
| 6 |
* $args module settings. |
| 7 |
* |
| 8 |
* @since 1.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
if ( ! isset( $args[ 'product' ] ) ) { |
| 16 |
return; |
| 17 |
} |
| 18 |
|
| 19 |
if ( ! isset( $args['comments_open'] ) || ! $args['comments_open'] ) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
$product = $args[ 'product' ]; |
| 24 |
$product_id = $product->get_id(); |
| 25 |
$review_count = $product->get_review_count(); |
| 26 |
$average = $product->get_average_rating(); |
| 27 |
$average = floor( $average ) === ceil( $average ) ? intval( $average ) : number_format( $average, 1 ); |
| 28 |
|
| 29 |
// Title tag |
| 30 |
$title_tag = $args[ 'title_tag' ] ?? 'h2'; |
| 31 |
|
| 32 |
// Dropdown sort |
| 33 |
$default_sorting = $args[ 'default_sorting' ] ?? 'newest'; |
| 34 |
$sort_orderby = isset( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : $default_sorting; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 35 |
|
| 36 |
// Get Comments args |
| 37 |
$comments_args = array( |
| 38 |
'post_id' => $product_id, |
| 39 |
'number' => get_option( 'page_comments' ) ? get_option( 'comments_per_page' ) : '', |
| 40 |
); |
| 41 |
|
| 42 |
// Pagination |
| 43 |
$comment_pages = 0; |
| 44 |
if ( get_option( 'page_comments' ) ) { |
| 45 |
$cpaged = get_query_var( 'cpage' ); |
| 46 |
|
| 47 |
$comment_pages = count( get_comments( array( |
| 48 |
'post_id' => $product_id, |
| 49 |
'fields' => 'ids', |
| 50 |
'status' => 'approve', |
| 51 |
'hierarchical' => 'threaded', |
| 52 |
) ) ); |
| 53 |
|
| 54 |
$comment_pages = ceil( $comment_pages / get_option( 'comments_per_page' ) ); |
| 55 |
|
| 56 |
$comments_args['product_id'] = $product_id; |
| 57 |
$comments_args['cpage'] = empty( $cpaged ) ? 1 : $cpaged; |
| 58 |
$comments_args['total'] = $comment_pages; |
| 59 |
} |
| 60 |
|
| 61 |
// Orderby |
| 62 |
switch ( $sort_orderby ) { |
| 63 |
case 'newest': |
| 64 |
$comments_args['order'] = 'DESC'; |
| 65 |
$comments_args['orderby'] = 'comment_date_gmt'; |
| 66 |
break; |
| 67 |
|
| 68 |
case 'oldest': |
| 69 |
$comments_args['order'] = 'ASC'; |
| 70 |
$comments_args['orderby'] = 'comment_date_gmt'; |
| 71 |
break; |
| 72 |
|
| 73 |
case 'top-rated': |
| 74 |
$comments_args['order'] = 'DESC'; |
| 75 |
$comments_args['orderby'] = 'meta_value_num'; |
| 76 |
// phpcs:disable |
| 77 |
$comments_args['meta_key'] = 'rating'; |
| 78 |
// phpcs:enable |
| 79 |
break; |
| 80 |
|
| 81 |
case 'low-rated': |
| 82 |
$comments_args['order'] = 'ASC'; |
| 83 |
$comments_args['orderby'] = 'meta_value_num'; |
| 84 |
// phpcs:disable |
| 85 |
$comments_args['meta_key'] = 'rating'; |
| 86 |
// phpcs:enable |
| 87 |
break; |
| 88 |
|
| 89 |
case 'photo-first': |
| 90 |
// phpcs:disable |
| 91 |
$comments_args['meta_query'] = array( |
| 92 |
'relation' => 'OR', |
| 93 |
array( |
| 94 |
'key' => 'review_images', |
| 95 |
'compare' => 'EXISTS', |
| 96 |
), |
| 97 |
array( |
| 98 |
'key' => 'review_images', |
| 99 |
'compare' => 'NOT EXISTS', |
| 100 |
), |
| 101 |
); |
| 102 |
$comments_args['orderby'] = array( |
| 103 |
'meta_value' => 'DESC', |
| 104 |
'comment_date' => 'DESC', |
| 105 |
); |
| 106 |
// phpcs:enable |
| 107 |
break; |
| 108 |
} |
| 109 |
|
| 110 |
// Set hierarchy to threaded. |
| 111 |
$comments_args['hierarchical'] = 'threaded'; |
| 112 |
|
| 113 |
/** |
| 114 |
* Hook 'merchant_wc_reviews_advanced_sorting_args' |
| 115 |
* |
| 116 |
* @since 1.0 |
| 117 |
*/ |
| 118 |
$_comments = isset( $args['comments'] ) ? $args['comments'] : get_comments( apply_filters( 'merchant_wc_reviews_advanced_sorting_args', $comments_args ) ); |
| 119 |
|
| 120 |
$args['comments'] = ! isset( $args['comments'] ) ? $_comments : $args['comments']; |
| 121 |
|
| 122 |
// Reviews bars rating |
| 123 |
$bars_data = $args['bars_data'] ?? array(); |
| 124 |
$ratings = $bars_data['ratings'] ?? array(); |
| 125 |
$total_ratings = $bars_data['total'] ?? 0; |
| 126 |
|
| 127 |
// Carousel images |
| 128 |
$is_carousel_on = (bool) ( $args['review_images_carousel'] ?? false ); |
| 129 |
$carousel_images_data = $args['carousel_images_data'] ?? array(); |
| 130 |
|
| 131 |
if ( $is_carousel_on && is_array( $carousel_images_data ) && ! empty( $carousel_images_data ) ) : ?> |
| 132 |
<?php |
| 133 |
$images_per_page = $args['review_images_carousel_per_page'] ?? 3; |
| 134 |
|
| 135 |
wp_enqueue_style( 'merchant-carousel' ); |
| 136 |
wp_enqueue_script( 'merchant-carousel' ); |
| 137 |
?> |
| 138 |
<section class="merchant-adv-reviews-media-carousel"> |
| 139 |
<?php if ( ! empty( $args['carousel_title'] ) ) : ?> |
| 140 |
<h3 class="section-title"><?php echo esc_html( Merchant_Translator::translate( $args['carousel_title'] ) ); ?></h3> |
| 141 |
<?php endif; ?> |
| 142 |
|
| 143 |
<div class="merchant-carousel <?php echo esc_attr( $images_per_page >= count( $carousel_images_data ) ? ' no-carousel' : '' ); ?>" data-per-page="<?php echo esc_attr( $images_per_page ); ?>"> |
| 144 |
<div class="merchant-carousel-stage"> |
| 145 |
<?php foreach( $carousel_images_data as $data ) : |
| 146 |
?> |
| 147 |
<?php if ( ! empty( $data['image_id'] ) ) : ?> |
| 148 |
<div class="item js-photo-slider-item" role="button" data-comment-id="<?php echo esc_attr( $data['comment_id'] ?? '' ); ?>"> |
| 149 |
<?php echo wp_get_attachment_image( $data['image_id'], 'full' ); ?> |
| 150 |
</div> |
| 151 |
<?php endif; ?> |
| 152 |
<?php endforeach; ?> |
| 153 |
</div> |
| 154 |
</div> |
| 155 |
</section> |
| 156 |
<?php endif; ?> |
| 157 |
|
| 158 |
<section id="reviews" class="merchant-adv-reviews products<?php echo ( $args[ 'hide_title' ] ) ? ' hide-title' : ''; ?>"> |
| 159 |
<?php |
| 160 |
if ( ! $args[ 'hide_title' ] ) : |
| 161 |
echo wp_kses_post( |
| 162 |
sprintf( |
| 163 |
'<%1$s id="reviews-stars" class="merchant-adv-reviews-title">%2$s</%1$s>', |
| 164 |
$title_tag, |
| 165 |
esc_html( Merchant_Translator::translate( $args[ 'title' ] ) ) |
| 166 |
) |
| 167 |
); |
| 168 |
?> |
| 169 |
<p class="merchant-adv-reviews-desc"> |
| 170 |
<?php echo esc_html( Merchant_Translator::translate( $args[ 'description' ] ) ); ?> |
| 171 |
</p> |
| 172 |
<?php endif; ?> |
| 173 |
|
| 174 |
<div class="merchant-adv-reviews-header"> |
| 175 |
<div class="mrc-row mrc-columns-no-gutter"> |
| 176 |
<div class="mrc-col mrc-left-col"> |
| 177 |
<?php if ( $args['ratings_enabled'] && $total_ratings > 0 ) : ?> |
| 178 |
<div class="merchant-adv-reviews-rating-wrapper"> |
| 179 |
<strong class="merchant-adv-reviews-rating"><?php echo esc_html( $average ); ?></strong> |
| 180 |
<div class="star-rating merchant-star-rating-style2" role="img" aria-label="Rated <?php echo esc_attr( $average ); ?> out of 5"> |
| 181 |
<span style="width: <?php echo esc_attr( ( ( $average / 5 ) * 100 ) ); ?>%;"> |
| 182 |
<?php |
| 183 |
/* translators: %s is average rating value */ |
| 184 |
$rating_text = sprintf( __( 'Rated %s out of 5 based on customer ratings.', 'merchant' ), $average ); |
| 185 |
echo esc_html( $rating_text ); |
| 186 |
?> |
| 187 |
</span> |
| 188 |
</div> |
| 189 |
</div> |
| 190 |
<?php endif; ?> |
| 191 |
|
| 192 |
<p class="merchant-adv-reviews-total"> |
| 193 |
<?php |
| 194 |
if ( $review_count > 0 ) { |
| 195 |
/* translators: %s is review count */ |
| 196 |
$review_count_text = sprintf( _nx( '%s Review', '%s Reviews', $review_count, 'review count', 'merchant' ), number_format_i18n( $review_count ) ); |
| 197 |
echo esc_html( $review_count_text ); |
| 198 |
} else { |
| 199 |
echo esc_html__( 'Be the first to leave a review.', 'merchant' ); |
| 200 |
} |
| 201 |
?> |
| 202 |
</p> |
| 203 |
|
| 204 |
<?php if ( $args['ratings_enabled'] && $total_ratings > 0 ) : ?> |
| 205 |
<div class="merchant-star-rating-bars"> |
| 206 |
<?php if ( is_array( $ratings ) && ! empty( $ratings ) ) : ?> |
| 207 |
<?php foreach ( $ratings as $key => $rating ) : ?> |
| 208 |
<div class="merchant-star-rating-bar-item" tabindex="0" role="button" data-rating="<?php echo esc_attr( substr( $key, 0, strpos( $key, '-' ) ) ); ?>"> |
| 209 |
<div class="merchant-star-rating-bar-item-inner"> |
| 210 |
<p class="item-rating"><?php echo esc_html( $rating['label'] ?? '' ); ?></p> |
| 211 |
<div class="item-bar"> |
| 212 |
<div class="item-bar-inner" style="width: <?php echo esc_attr( $rating[ 'percent' ] ); ?>%;"></div> |
| 213 |
</div> |
| 214 |
<p class="item-qty">(<?php echo esc_html( $rating['value'] ?? '0' ); ?>)</p> |
| 215 |
</div> |
| 216 |
</div> |
| 217 |
<?php endforeach; ?> |
| 218 |
<?php endif; ?> |
| 219 |
</div> |
| 220 |
<?php endif; ?> |
| 221 |
</div> |
| 222 |
|
| 223 |
<div class="mrc-col mrc-right-col"> |
| 224 |
<?php |
| 225 |
$btn_text = esc_html__( 'Write a Review', 'merchant' ); |
| 226 |
$btn_link = '#'; |
| 227 |
$btn_attrs = ''; |
| 228 |
$btn_class = 'merchant-adv-review-write-button'; |
| 229 |
|
| 230 |
if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) { |
| 231 |
$btn_text = esc_html__( 'Log in to write a Review', 'merchant' ); |
| 232 |
$btn_link = wp_login_url( get_permalink() ); |
| 233 |
$btn_attrs = 'rel="nofollow"'; |
| 234 |
} else { |
| 235 |
$btn_class .= ' js-merchant-adv-review-write-button'; |
| 236 |
} |
| 237 |
?> |
| 238 |
<a href="<?php echo esc_url( $btn_link ); ?>" class="<?php echo esc_attr( $btn_class ); ?>" <?php echo wp_kses_post( $btn_attrs ); ?>><?php echo esc_html( $btn_text ); ?></a> |
| 239 |
|
| 240 |
<?php if ( $review_count > 0 ) : ?> |
| 241 |
<form class="merchant-reviews-orderby-form" method="get" action="<?php echo esc_url( get_the_permalink( $product_id ) ); ?>#reviews-stars"> |
| 242 |
<label for="orderby"><?php echo esc_html__( 'Sort by:', 'merchant' ); ?></label> |
| 243 |
<select class="merchant-reviews-orderby" name="orderby" onChange="this.parentNode.submit();"> |
| 244 |
<option value="newest"<?php echo selected( $sort_orderby, 'newest' ); ?>><?php echo esc_html__( 'Newest', 'merchant' ); ?></option> |
| 245 |
<option value="oldest"<?php echo selected( $sort_orderby, 'oldest' ); ?>><?php echo esc_html__( 'Oldest', 'merchant' ); ?></option> |
| 246 |
<option value="top-rated"<?php echo selected( $sort_orderby, 'top-rated' ); ?>><?php echo esc_html__( 'Top rated', 'merchant' ); ?></option> |
| 247 |
<option value="low-rated"<?php echo selected( $sort_orderby, 'low-rated' ); ?>><?php echo esc_html__( 'Low rated', 'merchant' ); ?></option> |
| 248 |
<option value="photo-first"<?php echo selected( $sort_orderby, 'photo-first' ); ?>><?php echo esc_html__( 'Photo first', 'merchant' ); ?></option> |
| 249 |
</select> |
| 250 |
</form> |
| 251 |
<?php endif; ?> |
| 252 |
</div> |
| 253 |
</div> |
| 254 |
</div> |
| 255 |
|
| 256 |
<div class="merchant-adv-reviews-body"> |
| 257 |
<?php merchant_get_template_part( Merchant_Advanced_Reviews::MODULE_TEMPLATES_PATH, 'reviews-list', $args ); ?> |
| 258 |
</div> |
| 259 |
|
| 260 |
<div class="merchant-adv-reviews-footer"> |
| 261 |
<?php |
| 262 |
if ( count( $_comments ) > 0 ) : |
| 263 |
/** |
| 264 |
* Hook 'merchant_after_shop_reviews_adv_pagination' |
| 265 |
* |
| 266 |
* @since 1.0 |
| 267 |
*/ |
| 268 |
do_action( 'merchant_after_shop_reviews_adv_pagination', array_merge( $args, $comments_args ) ); |
| 269 |
endif; |
| 270 |
?> |
| 271 |
</section> |
| 272 |
|
| 273 |
<?php |
| 274 |
/** |
| 275 |
* Hook 'merchant_after_adv_reviews_section' |
| 276 |
* |
| 277 |
* @since 1.0 |
| 278 |
*/ |
| 279 |
do_action( 'merchant_after_adv_reviews_section' ); |
| 280 |
|