<?php

if (! defined('ABSPATH') ) { exit;
}
if (!class_exists('legalPages') ) :
class legalPages
{
    var $plugin_url;
    var $plugin_image_url;
    var $tablename;
    var $popuptable; 

    function __construct()
    {
        global $table_prefix;

        $this->plugin_url = trailingslashit(WP_PLUGIN_URL.'/'.dirname(plugin_basename(__FILE__)));
        $this->plugin_image_url = trailingslashit(WP_PLUGIN_URL.'/'.dirname(plugin_basename(__FILE__))).'images/';
        $this->tablename = $table_prefix . "legal_pages";
        $this->popuptable = $table_prefix . "lp_popups";
        add_action('admin_menu', array($this, 'admin_menu'));
        //add_action('admin_head', array($this, 'wpgattack_enqueue_editor'));
        add_shortcode('wp-legalpage', array($this, 'shortCode'));
        add_shortcode('wp-legalpopup', array($this, 'popupshortCode'));
        add_filter('the_content', array($this,'lpShortcode'));
        add_filter('the_excerpt', array($this,'lpShortcode'));
        add_action('wp_head', array($this,'lp_css'));
        add_action('wp_enqueue_scripts', array($this,'enqueue_frontend_script'));
        add_action('wp_footer', array($this,'wp_legalpages_show_eu_cookie_message'));
        add_action('wp_head', array($this, 'disable_legal_pages_comments'));
       // add_action('wp_head', array($this, 'disable_legal_pages_adults'));

$general = get_option('lp_general');

        if(isset($general['is_adult']) && $general['is_adult'] == 1 && !isset($_COOKIE['is_user_adult'])) {
            add_action('wp_enqueue_scripts', array($this , 'lp_is_adult_scripts'));
            add_action('wp_footer', array($this , 'print_is_adult_popup' ));
        }
    }

    function disable_legal_pages_adults()
    {
       $post_id = get_the_ID();


       // exit;

        // echo $post_id;
        // exit;
        $general = get_option('lp_general');

        //add_action('add_meta_boxes', array($this, 'lp_add_custom_box'));
        //add_action('save_post', array($this, 'lp_save_postdata'));

        add_action('admin_notices', array($this,'legal_pages_admin_notices'));

        add_action('wp_dashboard_setup', array($this, 'lp_dashboard_setup'));

        $general = get_option('lp_general');


        $adult_value = get_option("lp_eu_adult_page_enable_".$post_id);
       // exit;
        if(   trim($adult_value) != "" && !isset($_COOKIE['is_user_adult']) ) {
            //echo "if";
            if($adult_value == 'ON' && !isset($_COOKIE['is_user_adult']) )
            {
                //echo "im in on";
                add_action('wp_enqueue_scripts', array($this , 'lp_is_adult_scripts'));
                add_action('wp_footer', array($this , 'print_is_adult_popup' ));
            }

        } 
        else
        {
            //echo "else";
            $general = get_option('lp_general');             
            //echo "im in on else";
            if(isset($general['is_adult']) && $general['is_adult'] == 1 && !isset($_COOKIE['is_user_adult']) ) {
                //echo "aaya";

                add_action('wp_enqueue_scripts', array($this , 'lp_is_adult_scripts'));
                add_action('wp_footer', array($this , 'print_is_adult_popup' ));
            }
        }



     
    }

    //Disable comments for legalpages
    function disable_legal_pages_comments()
    {
        $post_id=get_the_ID();
        $meta=get_post_meta($post_id, 'is_legal', true);

        $general = get_option('lp_general');
        if(isset($general['disable_comments']) && $general['disable_comments'] == 1 && $meta=='yes') {
            add_filter('comments_open', array($this ,'legal_disable_comments'));
        }
    }

    function legal_pages_admin_notices()
    {
        if (!get_option('legal_pages_notice_shown') && !is_plugin_active(LP_PLUGIN_DIR."/legal-pages-pro.php") ) {
            echo "<div class='updated'><p>If you have recently updated to Version 4.5 or greater , please de-activate and activate WPLegalPages once, in order to refresh your legal page templates.</p></div>";
            update_option('legal_pages_notice_shown', 'true');
        }
    }

    function legal_disable_comments( $open )
    {

            $post = get_post_meta(get_the_ID(), 'is_legal');

            if ($post[0] == 'yes' ) {
                $open = false;
            }

            return $open;
     }

        function lp_dashboard()
        {
            echo '<span style="line-height:18px;">WP Legal Eagle plugin is the easiest method to create WP legal Eagle for your website.</span>';
        }

        function lp_dashboard_setup()
        {
            wp_add_dashboard_widget('lp_dashboard_widget', 'WP Legal Eagle', array($this, 'lp_dashboard'));
        }

        function install()
        {
            global $wpdb;

            //echo dirname(__FILE__) . '/templates/Terms.php';exit;
            $terms_forced = file_get_contents(dirname(__FILE__) . '/templates/Terms-latest.html');
            $terms = file_get_contents(dirname(__FILE__) . '/templates/Terms.html');
            $privacyCalifornia = file_get_contents(dirname(__FILE__) . '/templates/privacyCalifornia.html');
            $privacy = file_get_contents(dirname(__FILE__) . '/templates/privacy.html');
            $earnings = '<div style="text-align: center; font-weight: bold;">Earnings Disclaimer<br></div><br><br>EVERY EFFORT HAS BEEN MADE TO ACCURATELY REPRESENT THIS PRODUCT AND IT\'S POTENTIAL. EVEN THOUGH THIS INDUSTRY IS ONE OF THE FEW WHERE ONE CAN WRITE THEIR OWN CHECK IN TERMS OF EARNINGS, THERE IS NO GUARANTEE THAT YOU WILL EARN ANY MONEY USING THE TECHNIQUES AND IDEAS IN THESE MATERIALS. EXAMPLES IN THESE MATERIALS ARE NOT TO BE INTERPRETED AS A PROMISE OR GUARANTEE OF EARNINGS. EARNING POTENTIAL IS ENTIRELY DEPENDENT ON THE PERSON USING OUR PRODUCT, IDEAS AND TECHNIQUES. WE DO NOT PURPORT THIS AS A "GET RICH SCHEME."<br> <br> ANY CLAIMS MADE OF ACTUAL EARNINGS OR EXAMPLES OF ACTUAL RESULTS CAN BE VERIFIED UPON REQUEST. YOUR LEVEL OF SUCCESS IN ATTAINING THE RESULTS CLAIMED IN OUR MATERIALS DEPENDS ON THE TIME YOU DEVOTE TO THE PROGRAM, IDEAS AND TECHNIQUES MENTIONED, YOUR FINANCES, KNOWLEDGE AND VARIOUS SKILLS. SINCE THESE FACTORS DIFFER ACCORDING TO INDIVIDUALS, WE CANNOT GUARANTEE YOUR SUCCESS OR INCOME LEVEL. NOR ARE WE RESPONSIBLE FOR ANY OF YOUR ACTIONS.<br> <br> MATERIALS IN OUR PRODUCT AND OUR WEBSITE MAY CONTAIN INFORMATION THAT INCLUDES OR IS BASED UPON FORWARD-LOOKING STATEMENTS WITHIN THE MEANING OF THE SECURITIES LITIGATION REFORM ACT OF 1995. FORWARD-LOOKING STATEMENTS GIVE OUR EXPECTATIONS OR FORECASTS OF FUTURE EVENTS. YOU CAN IDENTIFY THESE STATEMENTS BY THE FACT THAT THEY DO NOT RELATE STRICTLY TO HISTORICAL OR CURRENT FACTS. THEY USE WORDS SUCH AS "ANTICIPATE," "ESTIMATE," "EXPECT," "PROJECT," "INTEND," "PLAN," "BELIEVE," AND OTHER WORDS AND TERMS OF SIMILAR MEANING IN CONNECTION WITH A DESCRIPTION OF POTENTIAL EARNINGS OR FINANCIAL PERFORMANCE.<br> <br> ANY AND ALL FORWARD LOOKING STATEMENTS HERE OR ON ANY OF OUR SALES MATERIAL ARE INTENDED TO EXPRESS OUR OPINION OF EARNINGS POTENTIAL. MANY FACTORS WILL BE IMPORTANT IN DETERMINING YOUR ACTUAL RESULTS AND NO GUARANTEES ARE MADE THAT YOU WILL ACHIEVE RESULTS SIMILAR TO OURS OR ANYBODY ELSES, IN FACT NO GUARANTEES ARE MADE THAT YOU WILL ACHIEVE ANY RESULTS FROM OUR IDEAS AND TECHNIQUES IN OUR MATERIAL.<br>  <br> The author and publisher disclaim any warranties (express or implied), merchantability, or fitness for any particular purpose. The author and publisher shall in no event be held liable to any party for any direct, indirect, punitive, special, incidental or other consequential damages arising directly or indirectly from any use of this material, which is provided "as is", and without warranties.<br> <br> As always, the advice of a competent legal, tax, accounting or other  professional should be sought.<br> <br> [Domain] does not warrant the performance, effectiveness or applicability of any sites listed or linked to on [Domain]<br> <br> All links are for information purposes only and are not warranted for content, accuracy or any other implied or explicit purpose.<br> <br>';

            $disclaimer = file_get_contents(dirname(__FILE__) . '/templates/disclaimer.html');
            $testimonials = file_get_contents(dirname(__FILE__) . '/templates/testimonial-disclosure.html');
            $linking = file_get_contents(dirname(__FILE__) . '/templates/linking-policy.html');
            $refund = file_get_contents(dirname(__FILE__) . '/templates/refund-policy.html');
            $return_refund = file_get_contents(dirname(__FILE__) . '/templates/return-refund-policy.html');
            $affiliate = file_get_contents(dirname(__FILE__) . '/templates/affiliate-agreement.html');
            $disclosure = file_get_contents(dirname(__FILE__) . '/templates/affiliate-disclosure.html');
            $antispam = file_get_contents(dirname(__FILE__) . '/templates/antispam.html');
            $ftc = file_get_contents(dirname(__FILE__) . '/templates/ftcstatement.html');
            $medical = file_get_contents(dirname(__FILE__) . '/templates/medical-disclaimer.html');
            $amazon = file_get_contents(dirname(__FILE__) . '/templates/amazon-affiliate.html');
            $dart = file_get_contents(dirname(__FILE__) . '/templates/double-dart-cookie.html');
            $external = file_get_contents(dirname(__FILE__) . '/templates/external-links.html');
            $fbpolicy = file_get_contents(dirname(__FILE__) . '/templates/fbpolicy.html');
            $about_us = file_get_contents(dirname(__FILE__) . '/templates/about-us.html');
            $dmca = file_get_contents(dirname(__FILE__) . '/templates/dmca.html');
            $gigitalGoods = file_get_contents(dirname(__FILE__) . '/templates/digital-goods-refund-policy.html');
            $coppa = file_get_contents(dirname(__FILE__) . '/templates/COPPA.html');
        $blogPolicy = file_get_contents(dirname(__FILE__) . '/templates/blog-comments-policy.html');
              $newsletter = file_get_contents(dirname(__FILE__) . '/templates/Newsletter-Subscription-and-Disclaimer.html');

            add_option('lp_excludePage', 'true');
            add_option('lp_general', '');
            add_option('lp_accept_terms', '0');
            add_option('lp_eu_cookie_title', __('A note to our visitors','wpekalegalpage'));
            $message_body="This website has updated its privacy policy in compliance with EU Cookie legislation. Please read this to review the updates about which cookies we use and what information we collect on our site. By continuing to use this site, you are agreeing to our updated privacy policy.";
            add_option('lp_eu_cookie_message', htmlentities($message_body));
            add_option('lp_eu_cookie_enable', 'OFF');
            add_option('lp_eu_box_color', '#000000');
            add_option('lp_eu_button_color', '#e3e3e3');
            add_option('lp_eu_button_text_color', '#333333');
            add_option('lp_eu_text_color', '#FFFFFF');
            add_option('lp_eu_link_color', '#8f0410');
            add_option('lp_eu_text_size', '12');


            $sql = "CREATE TABLE IF NOT EXISTS `$this->tablename` (
                      `id` int(11) NOT NULL AUTO_INCREMENT,
                      `title` text NOT NULL,
                      `content` longtext NOT NULL,
                      `notes` text NOT NULL,
                      `contentfor` varchar(200) NOT NULL,
                      PRIMARY KEY (`id`)
                    ) ENGINE=MyISAM;";
            $sqlpopup = "CREATE TABLE IF NOT EXISTS `$this->popuptable` (
                      `id` int(11) NOT NULL AUTO_INCREMENT,
                      `popupName` text NOT NULL,
                      `content` longtext NOT NULL,
                      PRIMARY KEY (`id`)
                    ) ENGINE=MyISAM;";
            $wpdb->query($sql);
            $wpdb->query($sqlpopup);
            $wpdb->insert($this->tablename, array('title'=>'Terms(forced agreement)','content'=>$terms,'contentfor'=>'1a2b3c4d5e6f7g8h9i'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Terms of Use','content'=>$terms_forced,'contentfor'=>'1a2b3c4d5e6f7g8h9i'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'California Privacy Rights','content'=>$privacyCalifornia,'contentfor'=>'1a2b3c4d5e6f7g8h9i10j'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Privacy Policy','content'=>$privacy,'contentfor'=>'1a2b3c4d5e6f7g8h9i'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Earnings Disclaimer','content'=>$earnings,'contentfor'=>'4d5e6f7g8h'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Disclaimer','content'=>$disclaimer,'contentfor'=>'1a2b3c4d5e6f7g8h9i'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Testimonials Disclosure','content'=>$testimonials,'contentfor'=>'1a2b3c4d5e6f7g8h9i'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Linking Policy','content'=>$linking,'contentfor'=>'1a2b3c9i'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Refund-Policy','content'=>$refund,'contentfor'=>'2b3c9i'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Return and Refund Policy','content'=>$return_refund,'contentfor'=>'2b3c9i'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Affiliate Agreement','content'=>$affiliate,'contentfor'=>'3c'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Antispam','content'=>$antispam,'contentfor'=>'3c'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'FTC Statement','content'=>$ftc,'contentfor'=>'4d5e7g'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Medical Disclaimer','content'=>$medical,'contentfor'=>'6f'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Amazon Affiliate','content'=>$amazon,'contentfor'=>'7g'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Double Dart Cookie','content'=>$dart,'contentfor'=>'8h'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'External Links Policy','content'=>$external,'contentfor'=>'4d5e6f7g8h9i'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Affiliate Disclosure','content'=>$disclosure,'contentfor'=>'4d'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'FB Policy','content'=>$fbpolicy,'contentfor'=>'4d'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'About Us','content'=>$about_us,'contentfor'=>'1a2b3c4d5e6f7g8h9i'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'DMCA','content'=>$dmca,'contentfor'=>'10j'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Digital Goods Refund Policy','content'=>$gigitalGoods,'contentfor'=>'2b3c9i'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'COPPA - Children’s Online Privacy Policy','content'=>$coppa,'contentfor'=>'1a2b3c4d5e'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Blog Comments Policy','content'=>$blogPolicy,'contentfor'=>'1a2b3c4d'), array('%s','%s','%s'));
            $wpdb->insert($this->tablename, array('title'=>'Newsletter : Subscription and Disclaimer','content'=>$newsletter,'contentfor'=>'1a2b3c4d5e6f7g8h9i10j'), array('%s','%s','%s'));
}



        function admin_menu()
        {
            add_menu_page('WP Legal Eagle', 'WP Legal Eagle', 'manage_options', 'legal-pages', array($this, 'adminSetting'));
            $terms = get_option('lp_accept_terms');
            if($terms == 1) {
                add_submenu_page('legal-pages', 'Settings', __('Settings','wplegalpages'), 'manage_options', 'legal-pages', array($this, 'adminSetting'));
                add_submenu_page('legal-pages', 'WP Legal Eagle', __('WP Legal Eagle','wplegalpages'), 'manage_options', 'showpages', array($this, 'showpages'));
                add_submenu_page('legal-pages', 'Create Page', __('Create Page','wplegalpages'), 'manage_options', 'lp-create', array($this, 'createPage'));
                add_submenu_page('legal-pages', 'Create/Edit Templates', __('Create/Edit Templates','wplegalpages'), 'manage_options', 'lp-templates', array($this, 'createTemplates'));
                add_submenu_page('legal-pages', 'Create Popups', __('Create Popups','wplegalpages'), 'manage_options', 'lp-popups', array($this, 'createPopups'));
                add_submenu_page('legal-pages', 'EU Cookies', __('EU Cookies','wplegalpages'), 'manage_options', 'lp-eu-cookies', array($this, 'update_eu_cookies'));
            }


        }

        function createPage()
        {
            include_once 'createPage.php';
        }

        function showpages()
        {
            include_once 'showpages.php';
        }
        function createPage1()
        {
            include_once 'createPage1.php';
        }
        function createTemplates()
        {
            include_once 'createTemplates.php';
        }
        function createPopups()
        {
            include_once 'createPopups.php';
        }
        function adminSetting()
        {
            include_once "adminSetting.php";
        }

        function adminTerms()
        {
            include_once "adminTerms.php";
        }

        function adminPrivacy()
        {
            include_once "adminPrivacy.php";
        }

        function adminDisclaimer()
        {
            include_once "adminDisclaimer.php";
        }

        function update_eu_cookies()
        {
            include_once "updateEUCookies.php";
        }

        function deactivate()
        {
            global $wpdb;

            delete_option('lp_excludePage');
            delete_option('lp_accept_terms');
            delete_option('lp_eu_cookie_title');
            delete_option('lp_eu_cookie_message');
            delete_option('lp_eu_cookie_enable');
            delete_option('lp_eu_box_color');
            delete_option('lp_eu_button_color');
            delete_option('lp_eu_button_text_color');
            delete_option('lp_eu_text_color');
            remove_meta_box('lp_dashboard_widget', 'dashboard', 'side');
            remove_meta_box('lp_dashboard_widget', 'dashboard', 'normal');
            $wpdb->query("Drop table $this->tablename");
            $wpdb->query("Drop table $this->popuptable");
        }

        function lpShortcode($content)
        {
            //$lp_find = array("[Domain]","[Business Name]","[Phone]","[Street]","[City, State, Zip code]","[Country]","[Email]","[Address]","[Niche]","[facebook-url]","[google-url]","[twitter-url]","[linkedin-url]");
            $lp_find = array("[Domain]","[Business Name]","[Phone]","[Street]","[City, State, Zip code]","[Country]","[Email]","[Address]","[Niche]");

            $lp_general = get_option('lp_general');

            $cont = str_replace($lp_find, $lp_general, stripslashes($content));

            /*
            *   Get social icons shortcode content and maps Latitude Longitude from page-content
            *   Added by Kaustubh
            */

            $latitude = '';
            $longitude = '';
            $facebook = '';
            $google = '';
            $twitter = '';
            $linkedin = '';

            $pos = strpos($cont, "[latitude]");
            if($pos !== false) {

                include_once 'map.php';
                $output=add_map();

                if(!empty($output->results)) {
                     $latitude = $output->results[0]->geometry->location->lat;
                     $longitude = $output->results[0]->geometry->location->lng;
                }
                $lp_map_find=array("[latitude]","[longitude]");
                $map_dimension=array("latitude"=>$latitude,"longitude"=>$longitude);
                $cont = str_replace($lp_map_find, $map_dimension, stripslashes($cont));
            }

            $url=(plugins_url()."/wplegalpages/image");

            $facebook_url = isset($lp_general['facebook-url']) ? $lp_general['facebook-url'] : '';

            if($facebook_url) {

                $facebook=array("facebook"=>'<a style="width: 30px; height: 30px; display:inline-block" href="'. $facebook_url.'"><img src="'.$url.'/facebook.png"/></a>');
            }
            $lp_find_facebook=array("[facebook]");
            $cont = str_replace($lp_find_facebook, $facebook, stripslashes($cont));

            $google_url = isset($lp_general['google-url']) ? $lp_general['google-url'] : '';

            if($google_url) {

                $google=array("google"=> '<a style="width: 30px; height: 30px; display:inline-block" href="'. $google_url.'"><img src="'.$url.'/google.png"/></a>');
            }
            $lp_find_google=array("[google]");
            $cont = str_replace($lp_find_google, $google, stripslashes($cont));

            $twitter_url = isset($lp_general['twitter-url']) ? $lp_general['twitter-url'] : '';
            if($twitter_url) {

                $twitter=array("twitter"=>'<a style="width: 30px; height: 30px; display:inline-block" href="'.$twitter_url.'"><img src="'.$url.'/twitter.png"/></a>');
            }
            $lp_find_twitter=array("[twitter]");
            $cont = str_replace($lp_find_twitter, $twitter, stripslashes($cont));

            $linkedin_url = isset($lp_general['linkedin-url']) ? $lp_general['linkedin-url'] : '';
            if($linkedin_url) {
                $linkedin=array("linkedin"=>'<a style="width: 30px; height: 30px; display:inline-block" href="'. $linkedin_url.'"><img src="'.$url.'/linkedin.png"/></a>');
            }
            $lp_find_linkedin=array("[linkedin]");
            $cont = str_replace($lp_find_linkedin, $linkedin, stripslashes($cont));

            return $cont;
        }


        function lp_enqueue_editor()
        {
            wp_enqueue_script('common');
            wp_enqueue_script('jquery-affect');
            wp_admin_css('thickbox');
            wp_print_scripts('post');
            wp_print_scripts('media-upload');
            wp_print_scripts('jquery');
            wp_print_scripts('jquery-ui-core');
            wp_print_scripts('jquery-ui-tabs');

            wp_print_scripts('editor');

            //self bootstrap style sheet
            wp_enqueue_style('bootstrap-min', $this->plugin_url. 'css/bootstrap.min.css');

            //self style sheet
            wp_enqueue_style('style', $this->plugin_url. 'css/style.css');

            wp_enqueue_script('jscolor', $this->plugin_url. 'js/jscolor/jscolor.js');
            wp_enqueue_script('jscolor');

             wp_enqueue_script('jquery-min', $this->plugin_url. 'js/jquery.min.js');
             wp_enqueue_script('bootstrap-min-js', $this->plugin_url. 'js/bootstrap.min.js');

            wp_enqueue_script('tooltip-js', $this->plugin_url. 'js/tooltip.js');

            /* Include the link dialog functions */
            //include ABSPATH . 'wp-admin/includes/internal-linking.php';
            wp_print_scripts('wplink');
            wp_print_styles('wplink');
            add_thickbox();
            wp_admin_css();
            wp_enqueue_script('utils');
            do_action("admin_print_styles-post-php");
            do_action('admin_print_styles');


            //remove_all_filters('mce_external_plugins');
        }

        function shortCode($atts)
        {
            global $wpdb;
            global $post;
            global $wp_query;

            extract(
                shortcode_atts(
                    array(
                    'tid' => 1
                    ), $atts
                )
            );

             $res = $wpdb->get_row($wpdb->prepare('SELECT * from '.$this->tablename.' where id= %d', $tid));
             $content = do_shortcode( $this->lpShortcode($res->content) );
             //$content = "select * from $this->tablename where id='$tid'";


            if(is_single() || is_page()) {
               return html_entity_decode($content);
            }
        }

        function popupshortCode($atts)
        {
            global $wpdb;
            global $post;
            global $wp_query;

            extract(
                shortcode_atts(
                    array(
                    'pid' => 1
                    ), $atts
                )
            );

            $res = $wpdb->get_row($wpdb->prepare('SELECT * from '. $this->popuptable.' where id= %d' , $pid));

            //$terms = apply_filters('the_content', $res->content);
            // print_r($res);
            $terms = $res->content;
            $terms = $this->lpShortcode($terms);

            $content = '<div>
                        '.$terms.'
                        <p ><input type="checkbox" name="lp_accept" id="lp_accept" value="1" onclick="jQuery(\'.accept\').toggle();" /> I agree to the terms and conditions.
                        <input type="submit" name="lp_submit" id="lp_submit" value="Accept" class="accept" style="padding: 15px; display:none;"/></p>
                        <br/>
                    </div>';

            add_thickbox();
           ?>
           <div id="thick-box" style="display:none;" >
                 <p> <?php echo $content; ?> </p>
            </div>

         <script type="text/javascript">

            jQuery(document).ready(function(){

                //set page's last character for cookies name
                full_url = window.location.href;
                full_url_last_char =  full_url.substring(full_url.length-1, full_url.length-4);

                if (jQuery.cookie('lp_eu_terms_condition__popup_'+full_url_last_char) == null) {
                      window.onload=function(){tb_show("", "#TB_inline?width=600&height=550&inlineId=thick-box&modal=true", false);}
                }

                jQuery("#lp_submit").click(function() {
                    jQuery.cookie('lp_eu_terms_condition__popup_'+full_url_last_char, 'NO', { expires: 7, path: '/' });
                    self.parent.tb_remove();
                });

                jQuery(window).load(function() {
                    if (jQuery.cookie('lp_eu_terms_condition__popup_'+full_url_last_char) == null) {
                        if( jQuery(window).width() == 640 ){
                            window_width = 629;
                        }else{
                            window_width = jQuery(window).width();
                        }

                        if( TB_WIDTH > window_width ){
                            jQuery("#TB_window").css({marginTop: 0, marginLeft: 0, width: '90%', left: '5%',  top:'10%', height:'80%'});
                            jQuery("#TB_ajaxContent, #TB_iframeContent").css({width: 'auto', height:'80%'});
                            jQuery("#TB_closeWindowButton").css({fontSize: '1.2em', marginRight: '5px'});
                        }
                        else{
                            jQuery("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: 'auto'});
                        }

                    }
                });

            });
            </script>
       <?php
        }

        function footerPages()
        {
            $general = get_option('lp_general');

            remove_filter('get_pages', 'lp_exclude_pages');
            echo "<ul class='legalfooter'>";
            wp_list_pages("title_li=&include=$general[pagefooter]");
            echo "</ul>";
            add_filter('get_pages', 'lp_exclude_pages');
        }

        function lp_css()
        {
            ?>
            <style type="text/css">
       ul.legalfooter li{
        list-style:none;
        float:left;
        padding-right:20px;
       }
       .accept{
        display:none;
        border: 1px solid #000;
        background:#000;
        color:#fff;
        border-radius:3px;
        -moz-border-radius:3px;
        -webkit-border-radius:3px;
        padding:5px;
       }
            </style>
            <?php

        }

        function enqueue_frontend_script()
        {
            wp_enqueue_script('jquery-cookie',  $this->plugin_url. 'js/jquery.cookie.js', array('jquery'));

        }




        function wp_legalpages_show_eu_cookie_message()
        {
            $lp_eu_get_visibility= get_option('lp_eu_cookie_enable');

            if($lp_eu_get_visibility=='ON') {

                $post_idd = get_the_ID();
                $lp_eu_get_visibility_post_id   = get_option('lp_eu_cookie_enable_' . $post_idd );

                if ($lp_eu_get_visibility_post_id == "OFF") {
                    return false;
                }



                $lp_eu_theme_css        = get_option('lp_eu_theme_css');
                $lp_eu_title            = get_option('lp_eu_cookie_title');
                $lp_eu_message          = get_option('lp_eu_cookie_message');
                $lp_eu_box_color        = get_option('lp_eu_box_color');
                $lp_eu_button_color     = get_option('lp_eu_button_color');
                $lp_eu_button_text_color= get_option('lp_eu_button_text_color');
                $lp_eu_text_color       = get_option('lp_eu_text_color');
                $lp_eu_button_text      = get_option('lp_eu_button_text');
                $lp_eu_link_text        = get_option('lp_eu_link_text');
                $lp_eu_link_url         = get_option('lp_eu_link_url');
                $lp_eu_text_size        = get_option('lp_eu_text_size');
                $lp_eu_link_color       = get_option('lp_eu_link_color');
                $lp_eu_head_text_size   = $lp_eu_text_size + 4;

                $lp_eu_html='<div id="lp_eu_container">';

                $lp_eu_html.='<table id="lp_eu_table" class="lp_eu_table" style="border:none;"><tr><td width="90%">';

                if(!empty($lp_eu_title)){
                    $lp_eu_html.='<b id="lp_eu_title">'.$lp_eu_title.' </b>';
                 
                }

                $lp_eu_html.='<p id="lp_eu_body">'.stripslashes(html_entity_decode($lp_eu_message));
                $lp_eu_html.=' <a id="lp_eu_link" target="_blank" href="'.$lp_eu_link_url.'">'.$lp_eu_link_text.'</a></p></td>';
                $lp_eu_html.='<td width="10%"><p id="lp_eu_btnContainer"><button type="button" id="lp_eu_btn_agree">'.$lp_eu_button_text.'</button></p></td></tr></table>';
                $lp_eu_html.='</div>';

                echo '<style>
                    .lp_eu_table td{
                        border:none;
                    }
                    #lp_eu_table{
                        border-color:rgba(255,255,255,0.9);
                        margin-bottom : 0em;
                        margin-top : 0em;
                        width: 100%;
                    }
                    #lp_eu_table td {
                        vertical-align: middle;
                    }
                    #lp_eu_table th, td {
                        padding: inherit;
                    }
                    #lp_eu_container{
                        display: none;
                        margin: 1%;
                        padding: 5px 10px;
                        width: 98%;
                        z-index: 9999;
                        position: fixed;
                        bottom: 0px;
                        border-radius: 10px;
                        box-shadow: 2px 2px 5px #888 inset;
                        box-sizing : border-box;
                        opacity: 0.8;
                    }
                    #lp_eu_btnContainer{
                    text-align: center;
                    }

                    #lp_eu_title{
                        margin: inherit;
                    }
                    #lp_eu_body,#lp_eu_btnContainer{
                        margin: 0px;
                    }
                    a#lp_eu_link{
                      border-bottom: 1px dotted;
                      text-decoration: none;
                    }
                    @media only screen and (max-width: 360px) {
                        #lp_eu_table td {
                            border-width: 0 1px 1px 0;
                            box-sizing: border-box;
                            display: block;
                            width: 100%;
                        }
                    }
                </style>';

                ?>
                <script type="text/javascript">
                jQuery(document).ready(function(){

                    if (jQuery.cookie('lp_eu_agree') == null) {
                  jQuery.cookie('lp_eu_agree', 'NO', { expires: 7, path: '/' });
                  lp_eu_show_cookie_bar();
                 }
                 else if (jQuery.cookie('lp_eu_agree') == 'NO') {
                  lp_eu_show_cookie_bar();
                 }
                 jQuery('#lp_eu_btn_agree').click(function (){
                  jQuery.cookie('lp_eu_agree', 'YES', { expires: 7, path: '/' });
                  jQuery('#lp_eu_container').hide(500);
                 });
                });

                function lp_eu_show_cookie_bar(){
                    jQuery('body').prepend('<?php echo $lp_eu_html; ?>');
                    <?php if($lp_eu_theme_css == 0){ ?>

                             // container deisgn
                             jQuery('#lp_eu_container').css( { 'background-color' : '<?php echo $lp_eu_box_color; ?>',
                                                               'border-color'     : '<?php echo $lp_eu_text_color; ?>',
                                                               'color'            : '<?php echo $lp_eu_text_color; ?>' });

                             //Text font
                             jQuery('p#lp_eu_body').css('font-size', '<?php echo $lp_eu_text_size."px"; ?>');

                             // Title design
                             jQuery('#lp_eu_title').css('font-size','<?php echo $lp_eu_head_text_size."px"; ?>');

                             // agree button design
                             jQuery('#lp_eu_btn_agree').css( { 'background-color' : '<?php echo $lp_eu_button_color; ?>',
                                                               'color'            : '<?php echo $lp_eu_button_text_color; ?>',
                                                               'border-style'     : 'none',
                                                               'border'           : '1px solid #bbb',
                                                               'border-radius'    : '5px',
                                                               'box-shadow'       : 'inset 0 0 1px 1px #f6f6f6',
                                                               'line-height'      : 1,
                                                               'padding'          : '3px 5px',
                                                               'text-align'       : 'center',
                                                               'text-shadow'      : '0 1px 0 #fff',
                                                               'cursor'           : 'pointer',
                                                               'font-size'        : '<?php echo $lp_eu_text_size."px"; ?>'
                                                            });

                            // link color
                            jQuery('#lp_eu_link').css({ 'color' : '<?php echo $lp_eu_link_color; ?>' });

                    <?php }
                          else {
                            // container deisgn
                            ?>
                            jQuery('#lp_eu_container').css({ 'background-color' : '<?php echo "inherit"; ?>',
                                                             'color'            : '<?php echo "inherit"; ?>' });

                   <?php  }
                    ?>
                    jQuery('#lp_eu_container').show(500);
                   }

                </script>
                <?php
            }
        }

        function print_is_adult_popup()
        {
            $general = get_option('lp_general');
            
            ?>
            <a id="inline" style="display:none" href="#data">This shows content of element who has id="data"</a>

            <div id="is_adult_thickbox" style="display:none">
                 <div id="data">
                   <p>This website contains content suitable for adults only . Please proceed only if you are above your country's legal age limit.</p>
                   <a href="#" id="enter_site" style="text-decoration:none;">Yes,I am above my country's legal age limit (Enter)</a>
                   <a href="<?php echo $general['leave-url']; ?>" style="text-decoration:none;" id="leave_site">Leave</a>
                </div>
            </div>

           <script type="text/javascript">
           jQuery(document).ready(function($) {

            window.onload=function(){tb_show("", "#TB_inline?width=570&height=250&inlineId=is_adult_thickbox&modal=true", false);}

            jQuery(window).load(function() {
                if( jQuery(window).width() == 640 ){
                    window_width = 629;
                }else
                    window_width = jQuery(window).width();

                if( TB_WIDTH > window_width ){
                    jQuery("#TB_window").css({marginTop: 0, marginLeft: 0, width: '90%', left: '5%',  top:'10%', height:'80%'});
                    jQuery("#TB_ajaxContent, #TB_iframeContent").css({width: 'auto', height:'80%'});
                    jQuery("#TB_closeWindowButton").css({fontSize: '1.2em', marginRight: '5px'});
                }
                else{
                    jQuery("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: 'auto'});
                }
            });

            //set design to box
            jQuery('#data').css( { 'font-size'   : '0.9em',
                                    'margin'     : '5% 1% 10%',
                                    'font-weight':  'normal',
                                    'line-height':  '2em'
                                 } );

            jQuery('#leave_site').css( { 'background-color' : '#e84c3d',
                                         'color'            : '#fff',
                                         'font-size'        : '1em',
                                         'margin'           : '10px 6px 0 0',
                                         'padding'          : '6px 10px',
                                        'border-radius'     : '4px',
                                        'float'             : 'left'
                                    });

            jQuery('#enter_site').css( { 'background-color' : '#000',
                                         'color'            : '#fff',
                                         'font-size'        : '1em',
                                         'margin'           : '10px 6px 0 0',
                                         'padding'          : '6px 10px',
                                         'border-radius'    : '4px',
                                         'float'            : 'left'
                                    });

            $('#enter_site').click(function(){
             $.cookie("is_user_adult", 1);
                self.parent.tb_remove();
            });

           });
           </script>
            <?php
        }

        function lp_is_adult_scripts()
        {
             wp_enqueue_script('media-upload');
            add_thickbox();
        }

    }
else :
    exit("Class legalPages already declared!");
endif;

/************* widget code - start ***********/
class WP_Legal_Pages extends WP_Widget
{


    public function __construct()
    {
        $widget_ops = array(
          'classname' => 'wp_legal_pages',
          'description' => 'WP Legal Eagle Widget is awesome',
        );
        parent::__construct('wp_legal_pages', 'WP Legal Eagle', $widget_ops);

    }


    public function widget( $args, $instance )
    {
        global $wpdb;
        $resultcontent = $wpdb->get_results("select title,content from wp_legal_pages where id=".$instance['lpSelect']);

        echo $args['before_widget'];
        foreach($resultcontent as $ras){
            _e('<h5>'.$ras->title.'</h5><hr>');
            _e($ras->content);
        }
        echo $args['after_widget'];

    }


    public function form( $instance )
    {
        global $wpdb;
   ?>
    <h3> <?php _e('Choose Template','wplegalpages'); ?> </h3><br/>


        <div>
         <select id="lpSelect" name="lpSelect">
            <?php
            $result = $wpdb->get_results("select id,title from wp_legal_pages");


            foreach($result as $ras){

                $lpvalue = isset($instance['lpSelect']) ? $instance['lpSelect'] : '';

                if($lpvalue==$ras->id) {
                    ?><option value="<?php echo $ras->id;?>" selected><?php echo $ras->title;?></option>
                <?php }else{ ?>
                   <option value="<?php echo $ras->id;?>"><?php echo $ras->title;?></option>
                <?php }
            }?>
            </select>
           </div><br><br>
        <?php
        return $instance;
    }

    public function update( $new_instance, $old_instance )
    {
        $new_instance['lpSelect']=$_POST['lpSelect'];
        return $new_instance;

    }

}

function WP_Legal_Pages_register_widgets()
{
    register_widget('WP_Legal_Pages');
}
add_action('widgets_init', 'WP_Legal_Pages_register_widgets');


function cookies_gdprs()
{

$lp_general = get_option('lp_general');
$gdpresval = $lp_general['gdprs'];
$gdpr_btn_val = $lp_general['gdpr_button_val'];
$article_content = $lp_general['article_content'];
 
$gdprscookie_val =  $_COOKIE["gdprs"]; 

/*if($article_content!="") { 

     $article_content = $article_content;
          }
    else{
      $article_content = "Our site uses cookies. By continuing to use our site you are agreeing to our Cookie Policy";
       }*/

if($gdpresval==1 && $gdprscookie_val!= 'true' ){

echo '<style> #cookiebanner {
    font-family: "Roboto",Arial,sans-serif;
    box-sizing: border-box;
    background-color: #333;
    width: 100%;
    padding: 25px 40px;
    position: fixed;
    z-index: 2147483645;
    bottom: -800px;
    font-size: 14px;
    line-height: 1.4;
    color: #fff;
    letter-spacing: .5px;
    text-align:center;
}.gdprs-value{color:#1e9cd3;}</style>
<div id="cookiebanner" lang="en" dir="ltr" ng-non-bindable="" name="cookiebanner" style="display: block; bottom: 0px;"><p class="c-message">'.do_shortcode($article_content).'</br></br><a href="javascript:void(0)" onclick="Cookiebot.dialog.submitConsent()" class="c-button gdprs-value">'.$gdpr_btn_val.'</a></p></div>';

    }  

}

add_action( 'wp_enqueue_scripts', 'cookies_gdprs' );

?>
<?php 

add_action( 'wp_footer', 'my_custom_script_load' );


add_action( 'admin_enqueue_scripts', 'my_custom_script_load' );


function my_custom_script_load(){

  wp_enqueue_script( 'my-custom-script',   plugins_url () . '/wplegaleagle/js/cookie.js', array( 'jquery' ) );
  wp_enqueue_script( 'my-custom-newscript',   plugins_url () . '/wplegaleagle/js/script.js', array( 'jquery' ) );


   }
   

   ?>
