<?php

if ( ! function_exists( 'gp_ratings' ) ) {

	function gp_ratings( $gp_post_id = '' ) {
		
		global $gp, $post;

		// Get site rating
		if ( get_post_meta( $gp_post_id, '_wp_page_template', true ) == 'hub-template.php' ) {
			$gp_pages = get_pages( 'child_of=' . $gp_post_id );	
			if ( $gp_pages ) {
				foreach ( $gp_pages as $gp_page ) {
					if ( get_post_meta( $gp_page->ID, '_wp_page_template', true ) == 'review-template.php' ) {
						$review_site_rating = get_post_meta( $gp_page->ID, 'review_site_rating', true );
						if ( get_post_meta( $gp_page->ID, '_gp_old_site_rating', true ) && ( ! isset( $review_site_rating[0] ) OR empty( $review_site_rating[0] ) ) ) {
							$GLOBALS['gp_site_rating'] = get_post_meta( $gp_page->ID, '_gp_old_site_rating', true );
						} else {
							$GLOBALS['gp_site_rating'] = $review_site_rating;
						}						
						break;
					} else {
						$GLOBALS['gp_site_rating'] = '';
					}
				}
			}	
		} elseif ( get_post_meta( $gp_post_id, '_wp_page_template', true ) == 'review-template.php' ) {
			$review_site_rating = get_post_meta( $gp_post_id, 'review_site_rating', true );
			if ( get_post_meta( $gp_post_id, '_gp_old_site_rating', true ) && ( ! isset( $review_site_rating[0] ) OR empty( $review_site_rating[0] ) ) ) {
				$GLOBALS['gp_site_rating'] = get_post_meta( $gp_post_id, '_gp_old_site_rating', true );
			} else {
				$GLOBALS['gp_site_rating'] = $review_site_rating;
			}	
		} elseif ( $post->post_type == 'gp_user_review' ) {
			$GLOBALS['gp_site_rating'] = get_post_meta( $gp_post_id, '_user_review_rating', true );
		}
		if ( ! isset( $GLOBALS['gp_site_rating'][0] ) OR empty( $GLOBALS['gp_site_rating'][0] ) ) {
			$GLOBALS['gp_site_rating'] = '';
		}

		// Get user rating
		if ( get_post_meta( $gp_post_id, '_gp_user_rating', true ) ) { 
			$GLOBALS['gp_user_rating'] = number_format( get_post_meta( $gp_post_id, '_gp_user_rating', true ), 1 ) + 0;
		} else {
			$GLOBALS['gp_user_rating'] = '';
		}
			
		// Detect if site rating enabled
		if ( ( isset( $GLOBALS['gp_display_site_rating'] ) && $GLOBALS['gp_display_site_rating'] == '1' ) && $GLOBALS['gp_site_rating'] != '' ) {
			$GLOBALS['gp_site_rating_enabled'] = true;
		} else {
			$GLOBALS['gp_site_rating_enabled'] = false;	
		}
		
		// Detect if user rating enabled
		if ( ( isset( $GLOBALS['gp_display_user_rating'] ) && $GLOBALS['gp_display_user_rating'] == '1' ) && $GLOBALS['gp_user_rating'] != '' ) {
			$GLOBALS['gp_user_rating_enabled'] = true;
		} else {
			$GLOBALS['gp_user_rating_enabled'] = false;	
		}
				
		// Ratings class
		if ( $GLOBALS['gp_site_rating_enabled'] == true && $GLOBALS['gp_user_rating_enabled'] == true ) {
			$GLOBALS['gp_rated_item_class'] = ' both-rated-item';
		} elseif ( $GLOBALS['gp_site_rating_enabled'] == true && $GLOBALS['gp_user_rating_enabled'] != true ) {
			$GLOBALS['gp_rated_item_class'] = ' site-rated-item';
		} elseif ( $GLOBALS['gp_user_rating_enabled'] == true && $GLOBALS['gp_site_rating_enabled'] != true ) {
			$GLOBALS['gp_rated_item_class'] = ' user-rated-item';
		} else {
			$GLOBALS['gp_rated_item_class'] = ' unrated-item';
		}
				
		// Review score
		if ( $GLOBALS['gp_site_rating_enabled'] == true ) {

			// Detect multiple site ratings
			if ( is_array( $GLOBALS['gp_site_rating'] ) ) {
				if ( count( $GLOBALS['gp_site_rating'] ) === 1 ) {
					$GLOBALS['gp_total_score'] = number_format( $GLOBALS['gp_site_rating'][0], 1 ) + 0;
				} else {
					$GLOBALS['gp_site_rating_sum'] = array_sum( $GLOBALS['gp_site_rating'] );
					$GLOBALS['gp_total_score'] = number_format( $GLOBALS['gp_site_rating_sum'] / count( $GLOBALS['gp_site_rating'] ), 1 ) + 0;
				}
			} else {
				$GLOBALS['gp_total_score'] = number_format( $GLOBALS['gp_site_rating'], 1 ) + 0;
			}
			
			// Get rating degrees
			$GLOBALS['gp_site_deg'] = ( 360 * ( $GLOBALS['gp_total_score'] / $gp['review_rating_number'] ) );
						
			// Site rating text
			if ( $GLOBALS['gp_total_score'] < 1 ) {
				$GLOBALS['gp_site_rating_text'] = '';
			} elseif ( $GLOBALS['gp_total_score'] < 2 ) {
				$GLOBALS['gp_site_rating_text'] = $gp['review_site_rating_text_1'];
			} elseif ( $GLOBALS['gp_total_score'] < 3 ) {
				$GLOBALS['gp_site_rating_text'] = $gp['review_site_rating_text_2'];
			} elseif ( $GLOBALS['gp_total_score'] < 4 ) {
				$GLOBALS['gp_site_rating_text'] = $gp['review_site_rating_text_3'];
			} elseif ( $GLOBALS['gp_total_score'] < 5 ) {
				$GLOBALS['gp_site_rating_text'] = $gp['review_site_rating_text_4'];
			} elseif ( $GLOBALS['gp_total_score'] < 6 ) {
				$GLOBALS['gp_site_rating_text'] = $gp['review_site_rating_text_5'];				
			} elseif ( $GLOBALS['gp_total_score'] < 7 ) {
				$GLOBALS['gp_site_rating_text'] = $gp['review_site_rating_text_6'];
			} elseif ( $GLOBALS['gp_total_score'] < 8 ) {
				$GLOBALS['gp_site_rating_text'] = $gp['review_site_rating_text_7'];
			} elseif ( $GLOBALS['gp_total_score'] < 9 ) {
				$GLOBALS['gp_site_rating_text'] = $gp['review_site_rating_text_8'];
			} elseif ( $GLOBALS['gp_total_score'] < 10 ) {
				$GLOBALS['gp_site_rating_text'] = $gp['review_site_rating_text_9'];
			} elseif ( $GLOBALS['gp_total_score'] < 11 ) {
				$GLOBALS['gp_site_rating_text'] = $gp['review_site_rating_text_10'];
			}

			// Clip class		
			if ( $GLOBALS['gp_site_deg'] < 180 ) {
				$GLOBALS['gp_site_clip_class_1'] = ' score-clip-1';
				$GLOBALS['gp_site_clip_class_2'] = ' score-clip-2';
			} else {
				$GLOBALS['gp_site_clip_class_1'] = ' no-score-clip-1';
				$GLOBALS['gp_site_clip_class_2'] = ' no-score-clip-2';	
			}
		
		}

		// User score
		if ( $GLOBALS['gp_user_rating_enabled'] == true ) {
			
			// Get rating degrees
			$GLOBALS['gp_user_deg'] = ( 360 * ( $GLOBALS['gp_user_rating'] / $gp['review_rating_number'] ) );
						
			// Site rating text
			if ( $GLOBALS['gp_user_rating'] < 1 ) {
				$GLOBALS['gp_user_rating_text'] = '';
			} elseif ( $GLOBALS['gp_user_rating'] < 2 ) {
				$GLOBALS['gp_user_rating_text'] = $gp['review_site_rating_text_1'];
			} elseif ( $GLOBALS['gp_user_rating'] < 3 ) {
				$GLOBALS['gp_user_rating_text'] = $gp['review_site_rating_text_2'];
			} elseif ( $GLOBALS['gp_user_rating'] < 4 ) {
				$GLOBALS['gp_user_rating_text'] = $gp['review_site_rating_text_3'];
			} elseif ( $GLOBALS['gp_user_rating'] < 5 ) {
				$GLOBALS['gp_user_rating_text'] = $gp['review_site_rating_text_4'];
			} elseif ( $GLOBALS['gp_user_rating'] < 6 ) {
				$GLOBALS['gp_user_rating_text'] = $gp['review_site_rating_text_5'];				
			} elseif ( $GLOBALS['gp_user_rating'] < 7 ) {
				$GLOBALS['gp_user_rating_text'] = $gp['review_site_rating_text_6'];
			} elseif ( $GLOBALS['gp_user_rating'] < 8 ) {
				$GLOBALS['gp_user_rating_text'] = $gp['review_site_rating_text_7'];
			} elseif ( $GLOBALS['gp_user_rating'] < 9 ) {
				$GLOBALS['gp_user_rating_text'] = $gp['review_site_rating_text_8'];
			} elseif ( $GLOBALS['gp_user_rating'] < 10 ) {
				$GLOBALS['gp_user_rating_text'] = $gp['review_site_rating_text_9'];
			} elseif ( $GLOBALS['gp_user_rating'] < 11 ) {
				$GLOBALS['gp_user_rating_text'] = $gp['review_site_rating_text_10'];
			}

			// Clip class		
			if ( $GLOBALS['gp_user_deg'] < 180 ) {
				$GLOBALS['gp_user_clip_class_1'] = ' score-clip-1';
				$GLOBALS['gp_user_clip_class_2'] = ' score-clip-2';
			} else {
				$GLOBALS['gp_user_clip_class_1'] = ' no-score-clip-1';
				$GLOBALS['gp_user_clip_class_2'] = ' no-score-clip-2';	
			}
		
		}
				
	}
}


/////////////////////////////////////// Store total score as meta key on hub and review pages ///////////////////////////////////////	

if ( is_admin() ) {
	if ( ! function_exists( 'gp_total_score_meta' ) ) {
		function gp_total_score_meta() {
			if ( get_post_meta( get_the_ID(), '_wp_page_template', true ) == 'review-template.php' && get_post_meta( get_the_ID(), 'review_site_rating', true ) ) {
				global $wpdb, $post;
			
				$GLOBALS['gp_site_rating'] = get_post_meta( get_the_ID(), 'review_site_rating', true );
				$GLOBALS['gp_site_rating_sum'] = array_sum( $GLOBALS['gp_site_rating'] );
				$GLOBALS['gp_total_score'] = number_format( $GLOBALS['gp_site_rating_sum'] / count( $GLOBALS['gp_site_rating'] ), 1 ) + 0;
				
				if ( ! wp_is_post_revision( get_the_ID() ) ) {
					if ( ! update_post_meta( get_the_ID(), '_gp_site_rating', $GLOBALS['gp_total_score'] ) ) {
						add_post_meta( get_the_ID(), '_gp_site_rating', $GLOBALS['gp_total_score'], true );
						add_post_meta( $post->post_parent, '_gp_site_rating', $GLOBALS['gp_total_score'], true );
					} else {
						update_post_meta( get_the_ID(), '_gp_site_rating', $GLOBALS['gp_total_score'] );
						update_post_meta( $post->post_parent, '_gp_site_rating', $GLOBALS['gp_total_score'] );
					}
				}	
			}
		}
	}
	add_action( 'publish_page', 'gp_total_score_meta' );
	add_action( 'save_post', 'gp_total_score_meta' );
}


/////////////////////////////////////// Reset user ratings and votes ///////////////////////////////////////	

if ( is_admin() ) {
	if ( ! function_exists( 'gp_reset_user_ratings' ) ) {
		function gp_reset_user_ratings() {
			global $wpdb, $post;
			if ( ! wp_is_post_revision( get_the_ID() ) ) {
				if ( get_post_meta( get_the_ID(), 'hub_reset_user_ratings', true ) == 1 ) {
					delete_post_meta( get_the_ID(), '_gp_user_rating' );
					delete_post_meta( get_the_ID(), '_gp_user_votes' );
					delete_post_meta( get_the_ID(), '_gp_current_position' );
					delete_user_meta( get_current_user_id(), '_gp_rated_' . get_the_ID() );
					delete_user_meta( get_current_user_id(), '_gp_user_rating_' . get_the_ID() );
					delete_user_meta( get_current_user_id(), '_gp_current_position_' . get_the_ID() );
					update_post_meta( get_the_ID(), 'hub_reset_user_ratings', 0 );
				}
			}	

		}
	}	
	add_action( 'publish_page', 'gp_reset_user_ratings' );
	add_action( 'save_post', 'gp_reset_user_ratings' );
}

?>