/**
 * Review manager get rating by post ID.
 *
 * @since 1.0.0
 * @package GeoDirectory_Review_Rating_Manager
 *
 * @param int $post_id The post ID.
 * @return array|bool
 */
function geodir_reviewrating_get_post_rating($post_id)
{
    global $wpdb, $plugin_prefix;
    $post_type = get_post_type($post_id);
    $detail_table = $plugin_prefix . $post_type . '_detail';
    $ratings = array();
    if ($wpdb->get_var("SHOW TABLES LIKE '" . $detail_table . "'") == $detail_table) {
        $sql = $wpdb->prepare("SELECT ratings, rating_count, overall_rating FROM " . $detail_table . " WHERE post_id = %d", array($post_id));
        $post_ratings = $wpdb->get_row($sql);
        if (!empty($post_ratings) && $post_ratings->rating_count > 0) {
            $old_rating = @unserialize($post_ratings->ratings);
            if (function_exists('geodir_get_commentoverall_number')) {
                $overall = geodir_get_commentoverall_number($post_id);
            } else {
                $overall = $post_ratings->overall_rating / $post_ratings->rating_count;
            }
        }
    } else {
        $old_rating_val = get_post_meta($post_id, 'ratings');
        if (isset($post_id) && is_array($old_rating_val)) {
            $old_rating = end($old_rating_val);
        } else {
            $old_rating = array();
        }
        $overall_val = get_post_meta($post_id, 'overall_rating');
        if (isset($post_id) && is_array($overall_val)) {
            $overall = end($overall_val);
        } else {
            $overall = array();
        }
    }
    if (!empty($old_rating)) {
        foreach ($old_rating as $key => $value) {
            $ratings[$key] = $value;
        }
    }
    if (isset($overall) && $overall != '') {
        $ratings['overall'] = $overall;
    }
    if (!empty($ratings)) {
        return $ratings;
    } else {
        return false;
    }
}
/**
 * Adds google rich snippets.
 *
 * @since 1.0.0
 * @package GeoDirectory_Review_Rating_Manager
 */
function geodir_review_rating_reviews_rich_snippets()
{
    if (geodir_is_geodir_page() && geodir_is_page('detail')) {
        $post_id = get_the_ID();
        $geodir_post_info = geodir_get_post_info($post_id);
        if (!empty($geodir_post_info)) {
            $post_title = $geodir_post_info->post_title;
            $post_thumbnail = '';
            $post_thumbnail_id = get_post_thumbnail_id($post_id);
            $max_rating = get_option('geodir_reviewrating_overall_count');
            $average_rating = geodir_get_commentoverall_number($post_id);
            $total_reviews = geodir_get_review_count_total($post_id);
            if ($total_reviews > 0) {
                if ($post_thumbnail_id > 0) {
                    $attachment_image = wp_get_attachment_image_src($post_thumbnail_id, 'post-thumbnail');
                    $post_thumbnail = !empty($attachment_image) && isset($attachment_image[0]) && $attachment_image[0] != '' ? $attachment_image[0] : '';
                }
                $content = '';
                $content .= '<div style="height:0;width:0;margin:0;padding:0" itemscope itemtype="http://data-vocabulary.org/Review-aggregate">';
                $content .= '<meta itemprop="itemreviewed" content="' . esc_attr($post_title) . '" />';
                if ($post_thumbnail != '') {
                    $content .= '<meta itemprop="photo" content="' . $post_thumbnail . '" />';
                }
                $content .= '<div itemprop="rating" itemscope itemtype="http://data-vocabulary.org/Rating">';
                $content .= '<meta itemprop="average" content="' . $average_rating . '" />';
                $content .= '<meta itemprop="best" content="' . $max_rating . '" />';
                $content .= '</div>';
                $content .= '<meta itemprop="count" content="' . $total_reviews . '" />';
                $content .= '</div>';
                echo $content;
            }
        }
    }
}