<?php 
defined('ABSPATH') or die("You can't access this file directly.");

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

?>
<div class="wrap fs-section wrap-img-Generator Single-content-wrap">
    <?php $seo_help_url = esc_url(admin_url('admin.php?page=qcld-seo-other-features')); ?>
    <div style="margin: 15px 0 25px 0;">
        <a href="<?php echo esc_url($seo_help_url); ?>" class="button button-secondary">
            <span class="dashicons dashicons-arrow-left-alt"></span>
            <?php esc_html_e('Back to SEO Help Page', 'seo-help'); ?>
        </a>
    </div>
    <h2><?php esc_html_e('YouTube Subtitle to Article Generator', "seo-help" ); ?></h2>
    <div>
        <div class="qcld-seohelp-outer">
            <div class="qcld-seohelp qcld_seohelp_ai_single_content">
                <div class="qcld-seohelp-input">
                    <div class="qcld-seohelp-input-field qcld-seohelp-input-video-wrap">
                       
                        <?php if ( ! file_exists( qcld_seohelp_PLUGIN_DIRNAME . '/autoload.php' ) || ( file_exists( qcld_seohelp_PLUGIN_DIRNAME . '/autoload.php' ) && !version_compare( PHP_VERSION, '8.2.0', '>=' ) ) ) : ?>
                        <div class="form-group"> <br>
                        <h4 class="qc-opt-title" style="color:red"><?php echo esc_html( 'For Interacting with Youtube Subtitle the Google Client Package is Required!', 'seo-help' ); ?></h4>
                        <div class="qcld-wpbot-gcdownload-area">
                            <?php if ( !version_compare( PHP_VERSION, '8.2.0', '>=' )  ) { ?>
                                <br>
                                <p><b><?php esc_html_e( 'Upgrade your PHP to 8.2+ and check again here. (if version is less than 8.2)', 'seo-help' ); ?></b></p>
                                <p><b><?php esc_html_e( 'Your PHP Version is', 'seo-help' ); ?> <?php echo PHP_VERSION ?></b></p>
                                <br>
                            <?php } else { ?>

                                <br>
                                <p style="color:indianred"><b><?php esc_html_e( 'PHP Version 8.2+ is required for Google Calender to work.', 'seo-help' ); ?></b></p>
                                <p style="color:indianred"><b><?php esc_html_e( 'Your PHP Version is', 'seo-help' ); ?> <?php echo PHP_VERSION ?></b></p>
                                <br>
                                <button class="btn btn-primary" id="qcld_seohelp_gc_download"><?php esc_html_e( 'Download and Install the Google Client', 'seo-help' ); ?></button>
                                <div class="qcld_seohelp_download_statuses"> </div>
                                
                            <?php } ?>
                           
                        </div>
                        <br>
                        </div>
                        <?php

                        else: 

                            wp_safe_redirect( esc_url( site_url( $_SERVER['REQUEST_URI'] ) ) );
                            exit();

                        endif; 
                        ?>
                   
                    </div>
                </div>

            
            </div>
           
        </div>

      
    </div>
</div>

<?php 

    }
}



function qcld_seohelp_create_folder( $gcdirectory ) {
    return @mkdir( $gcdirectory, 0777, true );
}

function qcld_seohelp_create_file( $filename ) {
    if ( ! @file_exists( $filename ) ) {
        if ( ! @is_writable( dirname( $filename ) ) ) {
            return false;
        }

        if ( ! @touch( $filename ) ) {
            return false;
        }
    } elseif ( ! @is_writable( $filename ) ) {
        return false;
    }

    $is_written = false;
    if ( ( $handle = @fopen( $filename, 'w' ) ) !== false ) {
        if ( @fwrite( $handle, '<?php //silence is golden' ) !== false ) {
            $is_written = true;
        }

        @fclose( $handle );
    }

    return $is_written;
}

function qcld_seohelp_extractgc() {
    $gcdirectory = qcld_seohelp_PLUGIN_DIRNAME;
    $filename    = 'seo-help-client.zip';
    $gcfilename  = qcld_seohelp_PLUGIN_DIRNAME . '/' . $filename;
    /* Open the Zip file */
    $zip         = new ZipArchive();
    $extractPath = 'path_to_extract';
    if ( $zip->open( $gcfilename ) != 'true' ) {
    }
    /* Extract Zip File */
    $zip->extractTo( $gcdirectory );
    $zip->close();
    @unlink( $gcfilename );

    // wp_safe_redirect( esc_url( site_url( $_SERVER['REQUEST_URI'] ) ) );
    //exit();

}


function qcld_seohelp_downloadgc() {

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

    if ( ! current_user_can( 'manage_options' ) ) {
        wp_send_json_error( esc_html( 'Unauthorized', 'seo-help' ) );
        wp_die();
    }

    $gcdirectory = qcld_seohelp_PLUGIN_DIRNAME;
    $download_url = 'https://github.com/qcld/seo-help-client/raw/main/seo-help-client.zip';
    $filename     = 'seo-help-client.zip';

    if ( ! is_dir( $gcdirectory ) ) {
        qcld_seohelp_create_folder( $gcdirectory );
    }
    if ( ! file_exists( $gcdirectory . '/index.php' ) ) {
        qcld_seohelp_create_file( $gcdirectory . '/index.php' );
    }

    if ( is_dir( $gcdirectory ) ) {

        $zipFile     = $gcdirectory . '/' . $filename; // Local Zip File Path
        $zipResource = fopen( $zipFile, 'w' );
        // Get The Zip File From Server
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $download_url );
        curl_setopt( $ch, CURLOPT_FAILONERROR, true );
        curl_setopt( $ch, CURLOPT_HEADER, 0 );
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
        curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true );
        curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
        curl_setopt( $ch, CURLOPT_FILE, $zipResource );
        $page = curl_exec( $ch );
        curl_close( $ch );

        qcld_seohelp_extractgc();

        $response = array(
            'status'  => 'success',
            'content' => 'Files Extracted successfully!',
        );
        echo wp_send_json( $response );
        wp_die();
    } 

}

add_action( 'wp_ajax_qcld_wp_seo_gc_client_download',  'qcld_seohelp_downloadgc' );
add_action( 'wp_ajax_nopriv_qcld_wp_seo_gc_client_download',  'qcld_seohelp_downloadgc' );

//qcld_seohelp_downloadgc();