<?php

defined('ABSPATH') or die('No script kiddies please!!');
if (!class_exists('EGPR_Shortcode')) {

    /**
     * Frontend Review Shortcode
     */
    class EGPR_Shortcode extends EGPR_Library {

        function __construct() {
            add_shortcode('egpr_reviews', array($this, 'egpr_shortcode_generator'));
            add_action('wp_footer', array($this, 'egpr_display_floating_review'));
        }

        /*
         * Generating shortcode with post id
         */

        function egpr_shortcode_generator($atts) {
            $egprreviews = array();
            if(isset($atts['id'])){
                $args = array(
                'post_type' => 'egprreviews',
                'post_status' => 'publish',
                'posts_per_page' => 1,
                'p' => $atts['id']
                );
                foreach ($atts as $key => $val) {
                    $$key = $val;
                }
                $egpr_reviews = new WP_Query($args);
                ob_start();
                if ($egpr_reviews->have_posts()) :
                    include( EGPR_PATH . 'inc/frontend/egpr-shortcode.php' );
                    $egprreviews = ob_get_contents();
                    // $egprreviews = preg_replace('<br />', '', $egprreviews);
                    // $egprreviews = wpautop(trim($egprreviews));
                endif;
                wp_reset_query();
                ob_end_clean();


            }else if(isset($atts['place_id'])){
                ob_start();
                 include( EGPR_PATH . 'inc/frontend/egpr-places-shortcode.php' );
                 $egprreviews = ob_get_contents();
                 ob_end_clean();
            }
            
            return $egprreviews;
        }

        /*
         * Display Floating Review
        */
        public function egpr_display_floating_review() {
             include_once( EGPR_PATH . 'inc/frontend/egpr-floating-review.php' );
        }

    }

    new EGPR_Shortcode();
}