<?php


function delete_csv(){
    $nonce = $_REQUEST['postNonce'];



    if ( ! wp_verify_nonce( $nonce, 'import-ajax-post-nonce' ) ){
        die ( 'Busted!');
    }
    $current_user = wp_get_current_user();

    $playlists= get_user_meta($current_user->ID, 'ms_playlists', true);



    if(!empty($playlists)){
        $pdf_url=get_user_meta($current_user->ID, 'ms_playlist_pdf_url', true);
        $result =update_user_meta($current_user->ID, 'ms_playlist_pdf_url', '');
        $url=str_replace(rtrim(get_site_url(),'/').'/', ABSPATH, $pdf_url);
        @unlink($url);
        update_user_meta($current_user->ID, 'ms_playlists', '');
    }
    set_time_limit(0);
    $count = 0;
    // while($total_song_count != 'done') {
    $my_query = new WP_Query(array( 'post_type' => 'songs', 'posts_per_page' => 100));
    //  if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post();
        $current_ID = get_the_ID();
        wp_delete_post($current_ID , true);
    endwhile;
    wp_reset_query();
    ?>

    <?php stats_update();
    $args = array('post_type' => 'songs','post_status' => 'publish',  'fields' => 'ids', 'posts_per_page' => -1 );//, 'offset' => $x * 1000);
    $query = new WP_Query($args);
    if( $query->have_posts() ) {
        $total_songs = $query->post_count;
    }else{
        $total_songs =0;
    }

    //}


    if( isset($total_songs)){
        echo ceil($total_songs/100);exit;
    }



}






function import_ajax_request() {
    $nonce = $_REQUEST['postNonce'];
    if ( ! wp_verify_nonce( $nonce, 'import-ajax-post-nonce' ) ){
        die ( 'Busted!');
    }
    // The $_REQUEST contains all the data sent via ajax
    if ( isset($_REQUEST) ) {

        $args = array('post_type' => 'songs','post_status' => 'publish',  'fields' => 'ids', 'posts_per_page' => -1 );//, 'offset' => $x * 1000);
        $query = new WP_Query($args);
        if( $query->have_posts() ) {
            $total_songs = $query->post_count;
        }
        if(isset($_REQUEST['CsvPart'])){
            $csv_part = explode(',@S@',$_REQUEST['CsvPart']);
        }
        set_time_limit(0);
        $uploadfiles = $_FILES['formData'];
        if (is_array($uploadfiles)) {
            if ($uploadfiles['error'] == 0 && isset($csv_part) && !empty($csv_part)) {
                $filetmp = $uploadfiles['tmp_name'];
                $num=  count(file($filetmp));
                if (($handle = fopen($filetmp, "r")) !== FALSE) {
                    foreach($csv_part as $csv_line){
                        if(!empty($csv_line)){
                            $data =explode(',',$csv_line);
                            $currently_processed = process_custom_post($data);
                        }
                    }
                    $args = array($uploadfiles['name'], date('r'), $num);
                    $updated = stats_update($args);
                    fclose($handle);
                }

                unlink($filetmp); // delete the temp csv file
            }
        }
    }

    // Always die in functions echoing ajax content
    die();
}








?>