Skip to content

Instantly share code, notes, and snippets.

@wbcomdev
Created August 9, 2023 11:24
Show Gist options
  • Select an option

  • Save wbcomdev/df8cf32e3fbc8b97ee0cf0839fbe809e to your computer and use it in GitHub Desktop.

Select an option

Save wbcomdev/df8cf32e3fbc8b97ee0cf0839fbe809e to your computer and use it in GitHub Desktop.
<?php
add_shortcode( 'bupr_shortcode_display_review_with_user_id', 'bupr_display_members' );
function bupr_display_members() {
global $wpdb , $bupr;
// give the user ids
$user_ids = array(24,2); // enter the comma saperated User ids
$bupr_type = 'integer';
$bupr_avg_rating = 0;
foreach( $user_ids as $user_id ){
$args = array(
'post_type' => 'review',
'posts_per_page' => -1,
'post_status' => 'publish',
'category' => 'bp-member',
'meta_query' => array(
array(
'key' => 'linked_bp_member',
'value' => $user_id,
'compare' => '=',
),
),
);
$reviews = get_posts( $args );
$bupr_admin_settings = get_option( 'bupr_admin_settings' );
if ( ! empty( $bupr_admin_settings ) ) {
$bupr_review_rating_fields = $bupr_admin_settings['profile_rating_fields'];
}
$bupr_total_rating = $rate_counter = 0;
$bupr_reviews_count = count( $reviews );
$bupr_total_review_count = '';
if ( 0 !== $bupr_reviews_count ) {
foreach ( $reviews as $review ) {
$rate = 0;
$reviews_field_count = 0;
$review_ratings = get_post_meta( $review->ID, 'profile_star_rating', false );
if ( ! empty( $review_ratings[0] ) ) {
if ( ! empty( $bupr_review_rating_fields ) && ! empty( $review_ratings[0] ) ) :
foreach ( $review_ratings[0] as $field => $value ) {
if ( array_key_exists( $field, $bupr_review_rating_fields ) ) {
$rate += $value;
$reviews_field_count++;
}
}
if ( 0 !== $reviews_field_count ) {
$bupr_total_rating += (int) $rate / $reviews_field_count;
$bupr_total_review_count ++;
$rate_counter++;
}
endif;
}
}
if ( 0 !== $bupr_total_review_count && 0 !== $bupr_total_rating ) {
$bupr_avg_rating = $bupr_total_rating / $bupr_total_review_count;
$bupr_type = gettype( $bupr_avg_rating );
}
$bupr_stars_on = $stars_off = $stars_half = '';
if ( 0 !== $bupr_total_review_count && 0 !== $bupr_total_rating ) {
$bupr_avg_rating = $bupr_total_rating / $bupr_total_review_count;
$bupr_type = gettype( $bupr_avg_rating );
}
$bupr_max_review[ $user_id ] = array(
'user_id' => $user_id,
'max_review' => $bupr_reviews_count,
'avg_rating' => $bupr_avg_rating,
'avr_type' => $bupr_type,
'rate_counter' => $rate_counter,
);
$bupr_star_rating[ $user_id ] = array(
'user_id' => $user_id,
'max_review' => $bupr_reviews_count,
'avg_rating' => $bupr_avg_rating,
'avr_type' => $bupr_type,
'rate_counter' => $rate_counter,
);
}
$bupr_members_ratings_data = array();
if ( ! empty( $reviews ) ) {
$bupr_members_ratings_data = $bupr_star_rating;
$bupr_members_ratings_data = $bupr_max_review;
}
$output = '';
$output .= '<div class="bupr-shortcode-top-members-contents bupr_members_review_setting ">';
$output .= ' <input type="hidden" value="' . esc_attr( $bupr['rating_color'] ) . '" class="bupr-display-rating-color"> ';
$bupr_user_count = 0;
$output .= '<ul class="bupr-member-main">';
if ( $reviews ) {
foreach ( $bupr_members_ratings_data as $buprKey => $buprValue ) {
$output .= '<li class="bupr-members"><div class="bupr-img-widget">';
$output .= get_avatar( $buprValue['user_id'], 50 );
$output .= '</div>';
$output .= '<div class="bupr-content-widget">';
$members_profile = bp_core_get_userlink( $buprValue['user_id'] );
$output .= '<div class="bupr-member-title">';
$output .= wp_kses_post( $members_profile );
$output .= '</div>';
$bupr_avg_rating = $buprValue['avg_rating'];
$bupr_reviews_count = $buprValue['max_review'];
$stars_on = $stars_off = $stars_half = '';
$remaining = $bupr_avg_rating - (int) $bupr_avg_rating;
if ( $remaining > 0 ) {
$stars_on = intval( $bupr_avg_rating );
$stars_half = 1;
$bupr_half_squar = 1;
$stars_off = 5 - ( $stars_on + $stars_half );
} else {
$stars_on = $bupr_avg_rating;
$stars_off = 5 - $bupr_avg_rating;
$stars_half = 0;
}
$output .= '<div class="bupr-member-rating">';
if ( $bupr_avg_rating > 0 ) {
$output .= '<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';
$output .= '<span itemprop="ratingValue" content=" ' . esc_attr( $bupr_avg_rating ) . '"></span>';
$output .= '<span itemprop="bestRating" content="5"></span>';
$output .= '<span itemprop="ratingCount" content="' . esc_attr( $buprValue['rate_counter'] ) . '"></span>';
$output .= '<span itemprop="reviewCount" content="' . esc_attr( $bupr_reviews_count ) . '"></span>';
$output .= '<span itemprop="itemReviewed" content="Person"></span>';
$output .= '<span itemprop="name" content="' . esc_attr( bp_core_get_username( $buprValue['user_id'] ) ) . '"></span>';
$output .= '<span itemprop="url" content="' . esc_attr( bp_core_get_userlink( $buprValue['user_id'], false, true ) ) . '"></span>';
$output .= '</div>';
}
for ( $i = 1; $i <= $stars_on; $i++ ) {
$output .= '<span class="fas fa-star bupr-star-rate"></span>';
}
for ( $i = 1; $i <= $stars_half; $i++ ) {
$output .= ' <span class="fas fa-star-half-alt bupr-star-rate"></span>';
}
for ( $i = 1; $i <= $stars_off; $i++ ) {
$output .= '<span class="far fa-star bupr-star-rate"></span>';
}
$output .= '</div>';
$bupr_avg_rating = round( $bupr_avg_rating, 2 );
$output .= '<span class="bupr-meta">';
$output .= sprintf( esc_html__( 'Rating : ( %1$s )', 'bp-member-reviews' ), esc_html( $bupr_avg_rating ) );
$output .= '</span><span class="bupr-meta">';
$output .= sprintf( esc_html__( 'Total %1$s : %2$s', 'bp-member-reviews' ), esc_html( $bupr['review_label'] ), esc_attr( $bupr_reviews_count ) );
$output .= '</span></div></li>';
$bupr_user_count++;
}
}
}
$output .= '</div>';
return $output;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment