<?php defined('ABSPATH') or die("No script kiddies please!");
global $egpr_wpdb, $post, $wpdb;
$review_posts = $egpr_wpdb->get_all_options_data('egp_reviews');
$enable_floating_review = (isset($review_posts['floating_review']['enable_floating_review']) && $review_posts['floating_review']['enable_floating_review'] == 1) ? 1 : 0;
if ($enable_floating_review == 1) {
    $default_review = (isset($review_posts['floating_review']['default_review']) && $review_posts['floating_review']['default_review'] != '') ? esc_attr($review_posts['floating_review']['default_review']) : '';
    $freview_layout = (isset($review_posts['floating_review']['layout']) && $review_posts['floating_review']['layout'] != '') ? esc_attr($review_posts['floating_review']['layout']) : 'template1';
    $specific_freview_layouts = get_post_meta( $post->ID, 'egpr-floating-review-templates', true);
    if($specific_freview_layouts == "default"){
           $layout = $freview_layout;
    }else if($specific_freview_layouts == ''){
           $layout = $freview_layout;
    }else{
         $layout = $specific_freview_layouts;
    }

    $placement = (isset($review_posts['floating_review']['placement']) && $review_posts['floating_review']['placement'] != '') ? esc_attr($review_posts['floating_review']['placement']) : '';
    $show_freview_on = (isset($review_posts['floating_review']['show_floating_review_on']) && $review_posts['floating_review']['show_floating_review_on'] != '') ? esc_attr($review_posts['floating_review']['show_floating_review_on']) : 'all_pages';
    $page_specific = (isset($review_posts['floating_review']['page_specific_display']) && !empty($review_posts['floating_review']['page_specific_display'])) ? $review_posts['floating_review']['page_specific_display'] : array();
    //show reviews on all pages or specific pages or only home page.
    $spage_reviewid = $egpr_wpdb->get_page_base_floating_review_id($post->ID);
    if($default_review != ''){
         if (intval($spage_reviewid)) {
            $pagereview = "show_review"; 
            $reviewid = (int) $spage_reviewid;
         }else if($spage_reviewid == "default" || $spage_reviewid == ''){
            $pagereview = "show_review"; 
             $reviewid = (int) $default_review;
         }else{
            $pagereview = "disable";  
            $reviewid = '';
         }
    }else{
        if (intval($spage_reviewid)) {
            $pagereview = "show_review"; 
            $reviewid = (int) $spage_reviewid;
         }else if($spage_reviewid == "default" || $spage_reviewid == ''){
             $pagereview = "disable"; 
             $reviewid = '';
         }else{
            $pagereview = "disable";  
            $reviewid = '';
         }
    }
    
if ($pagereview != 'disable') {
        if (!is_feed()) { 
          if ($show_freview_on == "all_pages") {
            if(is_page()){
                if ($reviewid != '' && intval($reviewid)) {
                        $scode = '[egpr_reviews id="'.$reviewid.'" position="hide"]';
                        $display_settings = "egpr-show-freviews";
                        $fshow = '1';
                    } else{
                        $fshow = '0';
                        $display_settings = "egpr-hide-freviews";
                        $fshortcodes = '';
                    } 
                }else{
                        $fshow = '0';
                        $display_settings = "egpr-hide-freviews";
                        $fshortcodes = '';
                } 
            }else if ($show_freview_on == "all_posts") {
                if(is_single()){
                 if ($reviewid != '' && intval($reviewid)) {
                        $scode = '[egpr_reviews id="'.$reviewid.'" position="hide"]';
                        $display_settings = "egpr-show-freviews";
                        $fshow = '1';
                    } else{
                        $fshow = '0';
                        $display_settings = "egpr-hide-freviews";
                        $fshortcodes = '';
                    } 
                 }else{
                        $fshow = '0';
                        $display_settings = "egpr-hide-freviews";
                        $fshortcodes = '';
                } 
            } else if ($show_freview_on == "only_on_home_page") {
                if (is_front_page()) {
                    if ($reviewid != '' && intval($reviewid)) {
                        $scode = '[egpr_reviews id="'.$reviewid.'" position="hide"]';
                        $fshow = '1';
                        $display_settings = "egpr-show-freviews";
                    } else{
                        $fshow = '0';
                        $display_settings = "egpr-hide-freviews";
                        $fshortcodes = '';
                    } 
                } else {
                    $fshow = '0';
                    $display_settings = "egpr-hide-freviews";
                    $fshortcodes = '';
                }
            } else {
                //specific pages only
                if(isset($page_specific) && is_array($page_specific)){
                    $showpages = $page_specific;
                }else{
                    $showpages = array();
                }
                 if(in_array($post->ID, $showpages)){
                     if ($reviewid != '' && intval($reviewid)) {
                        $scode = '[egpr_reviews id="'.$reviewid.'" position="hide"]';
                        $fshow = '1';
                        $display_settings = "egpr-show-freviews";
                    } else{
                        $fshow = '0';
                        $display_settings = "egpr-hide-freviews";
                        $fshortcodes = '';
                    } 
                 }
                 if(is_category()){
                    $cat_ID = get_query_var('cat');
                    if(isset($showpages['category']) && !empty($showpages['category'])){
                     if(in_array($cat_ID, $showpages['category'])){
                      if ($reviewid != '' && intval($reviewid)) {
                        $scode = '[egpr_reviews id="'.$reviewid.'" position="hide"]';
                        $fshow = '1';
                        $display_settings = "egpr-show-freviews";
                      }else{
                        $fshow = '0';
                        $display_settings = "egpr-hide-freviews";
                        $fshortcodes = '';
                     } 
                    }
                   }
                 }
                 
                 
                
            }        
        }// feed check 
    }
}

if(isset($fshow) && $fshow == 1){
    include(EGPR_PATH . 'inc/frontend/floating-review-page/egpr-freviews.php');
}