
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php

defined( 'PRESSO_CONST_REVIEW_URL' ) || define( 'PRESSO_CONST_REVIEW_URL', get_template_directory_uri().'/inc/review' );
defined( 'PRESSO_CONST_REVIEW_DEFAULT_SCORE_STYLE' ) || define( 'PRESSO_CONST_REVIEW_DEFAULT_SCORE_STYLE', 'star' );
defined( 'PRESSO_CONST_REVIEW_DEFAULT_POSITION' ) || define( 'PRESSO_CONST_REVIEW_DEFAULT_POSITION', 'bottom' );

if ( ! function_exists( 'presso_get_average_score' ) ) {
	function presso_get_average_score() {
		$scores = get_field( 'vw_review_scores', get_the_id() );

		$average_score = 0;
		foreach( $scores as $score ) {
			$average_score += $score['score'];
		}

		if ( $average_score > 0 ) {
			$average_score /= count( $scores );
		}

		return $average_score;
	}
}

if ( ! function_exists( 'presso_get_average_score_text' ) ) {
	function presso_get_average_score_text() {
		$score_style = esc_html( get_post_meta( get_the_id(), 'vw_review_score_style', true ) );

		if ( 'percentage' == $score_style ) {
			return intval( presso_get_average_score() ) . '%';

		} elseif ( 'points' == $score_style ) {
			return number_format( presso_get_average_score() / 10.0, 1 );
			
		} else {
			return number_format( presso_get_average_score() / 20.0, 1 );

		}
	}
}

/* -----------------------------------------------------------------------------
 * Template Tag
 * -------------------------------------------------------------------------- */
if ( ! function_exists( 'presso_the_review' ) ) {
	function presso_the_review() {
		if ( presso_has_review() ) {
			wp_enqueue_script( 'knob' );
			get_template_part( 'templates/single/review' );
		}
	}
}

if ( ! function_exists( 'presso_the_review_stars' ) ) {
	function presso_the_review_stars() {
		if ( ! presso_has_review() ) return;

		$average_score = intval( presso_get_average_score() );

		presso_render_review_as_stars( $average_score, 100 );
	}
}

if ( ! function_exists( 'presso_the_review_summary' ) ) {
	function presso_the_review_summary() {
		if ( ! presso_has_review() ) return;

		$total_score = presso_get_average_score();
		?>
			<span class="vw-review-summary">

				<span class="vw-review-summary__icon vw-star-icon-bg"></span>

				<span class="vw-review-summary__score"><?php echo number_format( $total_score, 0 ) ?><span>%</span></span>

			</span>
		<?php
	}
}


if ( ! function_exists( 'presso_render_review_as_stars' ) ) {
	function presso_render_review_as_stars( $score, $total, $size='' ) {
		$star_percentage = ( $score / $total ) * 10;
		$star_percentage = floor( $star_percentage ) * 10;

		$size_class = '';
		if ( $size ) {
			$size_class = 'vw-stars-icon--'.$size;
		}
		?>
		<span class="vw-post-review-stars">
			<span class="vw-stars-icon <?php echo esc_attr( $size_class ); ?>">
				<span class="hidden"><?php echo esc_html( $score/10 ); ?></span>
				<span class="vw-stars-icon__full-star" style="width: <?php echo esc_attr( $star_percentage ); ?>%;"></span>
			</span>
		</span>
		<?php
	}
}


/* -----------------------------------------------------------------------------
 * Shortcode
 * -------------------------------------------------------------------------- */
if ( ! function_exists( 'presso_rev_review_shortcode' ) ) {
	function presso_rev_review_shortcode( $atts, $content = null ) {
		ob_start();
		presso_the_review();
		return ob_get_clean();
	}
}


/* -----------------------------------------------------------------------------
 * Insert Review Box
 * ------------------------------------------------------------------------- */
add_filter( 'the_content', 'presso_rev_insert_review_box' );
if ( ! function_exists( 'presso_rev_insert_review_box' ) ) {
	function presso_rev_insert_review_box( $content ) {
		global $post;
		$location = get_post_meta( $post->ID, 'vw_review_position', true );

		if ( ! is_single() || empty( $location ) || $location == 'custom' ) {
			return $content;
		}

		ob_start();
		presso_the_review();
		$review = ob_get_clean();

		if ( 'bottom' == $location ) {
			return $content .= $review;

		} elseif ( 'top' == $location || 'top-floating' == $location ) {
			return $review .= $content;

		} else {
			return $content;

		}
	}
}

if ( ! function_exists( 'presso_has_review' ) ) {
	function presso_has_review() {
		$review_scores = get_field( 'vw_review_scores', get_the_id() );
		return
			get_post_meta( get_the_id(), 'vw_enable_review', true )
			&& ! empty( $review_scores );
	}
}

if ( ! function_exists( 'presso_get_stared_score' ) ) {
	function presso_get_stared_score( $score ) {
		return $score * 5 / 100; // 5-starts based
	}
}