<?php 

class AHA_POST_TYPE_USER_EMAIL {

    public function __construct(){

    }

  

    public function CheckUserExit($args=false){

        $default = array(

            'post_type' => AHACHAT_LIST_EMAIL_POST_TYPE,

            'post_status'=>array('publish','pending','future'),

            'meta_query'=>array(

                    'relation' => 'OR',

                    array(

                      'key'=>'ahachat_email',

                      'value'=>$args['email'],

                      'compare'=>'='

                    ),



                ),

            );

         if($args!=false){

               // $args = wp_parse_args($args, $default );

                $posts = new WP_Query($default);

                if($posts->have_posts())

                {



                    return true ;

                }

         }

         return false;

    }

    

    public function CheckCookieBrowserExit($args=false){

        $default = array(

            'post_type' => AHACHAT_LIST_EMAIL_POST_TYPE,

            'post_status'=>array('publish','pending','future'),

            'meta_query'=>array(

                    'relation' => 'OR',

                    array(

                      'key'=>'cookie_popup_br',

                      'value'=>$args['cookie_popup'],

                      'compare'=>'=' 

                    ),

                    

                ),

        );

         if($args!=false){

               // $args = wp_parse_args($args, $default );

                $posts = new WP_Query($default);

                if($posts->have_posts())

                {   

                    

                    return true ;

                }

         }

         return false;

    }



    public function InsertEmail($args=false){

        $arr = array(

            'post_content' => $args['mess'],

            'post_date' => $args['post_date'],

            'post_date_gmt' => $args['post_date'],

            'post_type' => $args['post_type'],

            'post_title' => wp_trim_words($args['mess'], 200),

            'post_status' => 'publish',

        );

        // print_r($args);

        if($args!=false && is_array($args) ){

            $insert_id = wp_insert_post($arr);

            if($args['post_type']==AHACHAT_LIST_EMAIL_POST_TYPE){

                update_post_meta($insert_id, 'cookie_popup_br', $args['cookie_popup_br']);

                update_post_meta($insert_id, 'ahachat_email', $args['ahachat_email']);

                update_post_meta($insert_id, 'ahachat_email_product', $args['ahachat_email_product']);

                update_post_meta($insert_id, 'ahachat_email_reminder', $args['ahachat_email_reminder']);



            }

            

            return $insert_id;

        }

        return false;

    }



    public function CheckMailPostID($args){



        $default = array(

            'post_type' => AHACHAT_LIST_EMAIL_POST_TYPE,

            'post_status'=>array('publish','pending','future'),

            'meta_query'=>array(

                    'relation' => 'OR',

                    array(

                      'key'=>'ahachat_email',

                      'value'=>$args['email'],

                      'compare'=>'=' 

                    ),

                  

                 

                ),

            );



         if($args!=false){

               // $args = wp_parse_args($args, $default );

                $posts = new WP_Query($default);

                if($posts->have_posts())

                {   

                    

                    return $posts->post->ID;

                }

         }

         return false;

    }



    public function CheckCookiePostID($args){



        $default = array(

            'post_type' => AHACHAT_LIST_EMAIL_POST_TYPE,

            'post_status'=>array('publish','pending','future'),

            'meta_query'=>array(

                    'relation' => 'OR',

                    array(

                      'key'=>'cookie_popup_br',

                      'value'=>$args['cookie_popup'],

                      'compare'=>'=' 

                    ),

                  

                 

                ),

            );



         if($args!=false){

               // $args = wp_parse_args($args, $default );

                $posts = new WP_Query($default);

                if($posts->have_posts())

                {   

                    

                    return $posts->post->ID;

                }

         }

         return false;

    }

}

?>