<?php
/*
* QuantumCloud Promo + Support Page
* Revised On: 18-10-2023
*/

if ( ! defined( 'qcld_seohelp_support_path' ) ) {
    define('qcld_seohelp_support_path', plugin_dir_path(__FILE__));
}

if ( ! defined( 'qcld_seohelp_support_url' ) )
    define('qcld_seohelp_support_url', plugin_dir_url( __FILE__ ) );

if ( ! defined( 'qcld_seohelp_support_img_url' ) )
    define('qcld_seohelp_support_img_url', qcld_seohelp_support_url . "/images" );




/*******************************
 * Main Class to Display Support
 * form and the promo pages
 *******************************/

if ( ! function_exists( 'qcld_seohelp_include_promo_page_scripts' ) ) {	
	function qcld_seohelp_include_promo_page_scripts( ) {   


        if( isset($_GET["page"]) && !empty($_GET["page"]) && (   $_GET["page"] == "qcld-seo-help-supports"  ) ){

            wp_enqueue_style( 'qcld-support-fontawesome-css', qcld_seohelp_support_url . "css/font-awesome.min.css");                              
            wp_enqueue_style( 'qcld-seo-support-style-css', qcld_seohelp_support_url . "css/style.css");

            wp_enqueue_script( 'jquery' );
            wp_enqueue_script( 'jquery-ui-core');
            wp_enqueue_script( 'jquery-ui-tabs' );
            wp_enqueue_script( 'jquery-qcld_seo-form-processor', qcld_seohelp_support_url . 'js/support-form-script.js',  array('jquery', 'jquery-ui-core','jquery-ui-tabs') );

            wp_add_inline_script( 'jquery-qcld_seo-form-processor', 
                                    'var qcld_seohelp_recommend_ajaxurl    = "' . admin_url('admin-ajax.php') . '";
                                    var qcld_seohelp_recommend_ajax_nonce  = "'. wp_create_nonce( 'seo-help' ).'";   
                                ', 'before');
            
        }
	   
	}
	add_action('admin_enqueue_scripts', 'qcld_seohelp_include_promo_page_scripts');
	
}
		
/*******************************
 * Callback function to show the HTML
 *******************************/

include_once qcld_seohelp_support_path . '/qc-clr-recommendbot-support-plugin.php';

if ( ! function_exists( 'qcld_seohelp_promo_support_page_callback_func' ) ) {

	function qcld_seohelp_promo_support_page_callback_func() {
		
?>


        <div class="qcld-seo-support qcld-support-new-page">
            <div class="support-btn-main justify-content-center">
                <div class="col text-center">
                    <h2 class="py-3"><?php esc_html_e('Check Out Some of Our Other Works that Might Make Your Website Better', 'seo-help'); ?></h2>
                    <h5><?php esc_html_e('All our Pro Version users get Premium, Guaranteed Quick, One on One Priority Support.', 'seo-help'); ?></h5>
                    <div class="support-btn">
                        <a class="premium-support" href="<?php echo esc_url('https://qc.ticksy.com/'); ?>" target="_blank"><?php esc_html_e('Get Priority Support ', 'seo-help'); ?></a>
                        <a style="width:282px" class="premium-support" href="<?php echo esc_url('https://www.quantumcloud.com/resources/kb-sections/comment-tools/'); ?>" target="_blank"><?php esc_html_e('Online KnowledgeBase', 'seo-help'); ?></a>
                    </div>
                </div>
            
                <div class="qc-column-12" >
                    <div class="support-btn">
                        
                        <a class="premium-support premium-support-free" href="<?php echo esc_url('https://www.quantumcloud.com/resources/free-support/','seo-help') ?>" target="_blank"><?php esc_html_e('Get Support for Free Version','seo-help') ?></a>
                    </div>
                </div>
            </div>
            
            <div class="qcld-plugins-lists">
                <div class="qcld-plugins-loading">
                    <img src="<?php echo esc_url(qcld_seohelp_support_img_url); ?>/loading.gif" alt="loading">
                </div>
            </div>
        </div>
			
		
<?php
            
       
    }
}


/*******************************
 * Handle Ajex Request for Form Processing
 *******************************/
add_action( 'wp_ajax_qcld_seohelp_process_qc_promo_form', 'qcld_seohelp_process_qc_promo_form' );

if( !function_exists('qcld_seohelp_process_qc_promo_form') ){

    function qcld_seohelp_process_qc_promo_form(){

        check_ajax_referer( 'seo-help', 'security');

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( esc_html( 'Unauthorized', 'seo-help' ) );
            wp_die();
        }
        
        $data['status']   = 'failed';
        $data['message']  = esc_html('Problem in processing your form submission request! Apologies for the inconveniences.<br> 
Please email to <span style="color:#22A0C9;font-weight:bold !important;font-size:14px "> quantumcloud@gmail.com </span> with any feedback. We will get back to you right away!', 'seo-help');

        $name         = isset($_POST['post_name']) ? trim(sanitize_text_field(wp_unslash($_POST['post_name']))) : '';
        $email        = isset($_POST['post_email']) ? trim(sanitize_email(wp_unslash($_POST['post_email']))) : '';
        $subject      = isset($_POST['post_subject']) ? trim(sanitize_text_field(wp_unslash($_POST['post_subject']))) : '';
        $message      = isset($_POST['post_message']) ? trim(sanitize_text_field(wp_unslash($_POST['post_message']))) : '';
        $plugin_name  = isset($_POST['post_plugin_name']) ? trim(sanitize_text_field(wp_unslash($_POST['post_plugin_name']))) : '';

        if( $name == "" || $email == "" || $subject == "" || $message == "" )
        {
            $data['message'] = esc_html('Please fill up all the requried form fields.', 'seo-help');
        }
        else if ( filter_var($email, FILTER_VALIDATE_EMAIL) === false ) 
        {
            $data['message'] = esc_html('Invalid email address.', 'seo-help');
        }
        else
        {

            //build email body

            $bodyContent = "";
                
            $bodyContent .= "<p><strong>".esc_html('Support Request Details:', 'seo-help')."</strong></p><hr>";

            $bodyContent .= "<p>".esc_html('Name', 'seo-help')." : ".$name."</p>";
            $bodyContent .= "<p>".esc_html('Email', 'seo-help')." : ".$email."</p>";
            $bodyContent .= "<p>".esc_html('Subject', 'seo-help')." : ".$subject."</p>";
            $bodyContent .= "<p>".esc_html('Message', 'seo-help')." : ".$message."</p>";

            $bodyContent .= "<p>".esc_html('Sent Via the Plugin', 'seo-help')." : ".$plugin_name."</p>";

            $bodyContent .="<p></p><p>".esc_html('Mail sent from:', 'seo-help')." <strong>".get_bloginfo('name')."</strong>, ".esc_html('URL:', 'seo-help')." [".get_bloginfo('url')."].</p>";
            $bodyContent .="<p>".esc_html('Mail Generated on:', 'seo-help')." " . gmdate("F j, Y, g:i a") . "</p>";           
            
            $toEmail = "quantumcloud@gmail.com"; //Receivers email address
            //$toEmail = "qc.kadir@gmail.com"; //Receivers email address

            //Extract Domain
            $url = get_site_url();
            $url = wp_parse_url($url);
            $domain = $url['host'];
            

            $fakeFromEmailAddress = "wordpress@" . $domain;
            
            $to = $toEmail;
            $body = $bodyContent;
            $headers = array();
            $headers[] = 'Content-Type: text/html; charset=UTF-8';
            $headers[] = 'From: '.esc_attr($name, 'seo-help').' <'.esc_attr($fakeFromEmailAddress, 'seo-help').'>';
            $headers[] = 'Reply-To: '.esc_attr($name, 'seo-help').' <'.esc_attr($email, 'seo-help').'>';

            $finalSubject = esc_html('From Plugin Support Page:', 'seo-help')." " . esc_attr($subject, 'seo-help');
            
            $result = wp_mail( $to, $finalSubject, $body, $headers );

            if( $result )
            {
                $data['status'] = 'success';
                $data['message'] = esc_html('Your email was sent successfully. Thanks!', 'seo-help');
            }

        }

        ob_clean();

        
        echo wp_json_encode($data);
    
        die();
    }
}



