<?php

$term_arnelio = term_exists('Tarot Free deck', 'tarokkina_free-cat');

// Si no existe Tarokina free, instalar 
if ( $term_arnelio == 0 || $term_arnelio == null ) {

    global $wpdb;
    $textos = $wpdb->get_row($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key LIKE '%s'",'%tkna_free_text_88888888%'));

    $json =  TAROKINA_LIB_PATH_FREE . 'demo-deck/data.json';
    $json2 = file_get_contents($json);
    $array = json_decode($json2,true);


    $id_free = 88888888;
    $nameFree = 'Tarot free';
    $nameFreeDeck = 'Tarot Free deck';

    // Crear categoria Free deck
			wp_insert_term(
				'Tarot Free deck',   
				'tarokkina_free-cat'
				);		

    foreach ($array as $subarray) {
        $post_name = $subarray['post_name'];
        $titulo = $subarray['title'];
        $texto = $subarray['texto'];
          
            // Register Post Data
            $post                   = array();
            $post['post_name']       = $post_name;
            $post['post_status']    = 'publish';
            $post['post_type']      = 'tarokkina_free'; // can be a CPT too
            $post['tax_input']     = array('tarokina');
            $post['post_title']     = $titulo;
            

            // Create Post
            $post_id = wp_insert_post( $post );
            // Vincular taxonomia
            wp_set_object_terms( $post_id, $nameFreeDeck, 'tarokkina_free-cat', true );



            //verificar si existen textos
            if ( $textos == null) {
                $textosFree = update_post_meta($post_id,'_tkna_free_text_'.$id_free.'basic0',$texto);
            }else{
                $textosFree == '';
            }


           // Vinvular los textos con el tarot
            if ( $textos == null) {
                update_post_meta($post_id,'tkna_free_text_'.$id_free.'basic0',$post_id.$id_free.'basic0');
            }


    }// foreach

        $tarot_id_cat = get_term_by('name', $nameFreeDeck, 'tarokkina_free-cat')->term_id;



                 // Crear transient con las imagenes de la categoría
                 $args = array(
                    'post_type' => 'tarokkina_free',
                    'fields'=>'ids',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'tarokkina_free-cat',
                            'field' => 'term_id',
                            'terms' => $tarot_id_cat,
                            'include_children' => true,
                            )
                        ),
                    'post_status' => 'publish',
                    'posts_per_page' => -1
                    );
        
                $cardsids = new WP_Query( $args );
                
        
                update_option( $tarot_id_cat. '_tarokki_cardsids_free', $cardsids->posts );


    
        update_option( '_tarokki_free_complex|tkta_free_barajas|0|0|value',$tarot_id_cat,'no');

        update_option( '_tarokki_free_complex|||0|value','tarokina-free','no' );
        update_option( '_tarokki_free_complex|tkta_free_id|0|0|value', $id_free, 'no');
        update_option( '_tarokki_free_complex|tkta_free_name|0|0|value',$nameFree, 'no');

        update_option( '_tarokki_free_complex|tkta_free_mode|0|0|value','basic','no' );
        update_option( '_tarokki_free_complex|tkta_free_image_backface|0|0|value',null,'no' );
        update_option( '_tarokki_free_complex|tkta_free_spread|0|0|value','3cards','no' );
        update_option( '_tarokki_free_complex|tkta_free_1st|0|0|value',__('Present','tarokina-free'),'no' );
        update_option( '_tarokki_free_complex|tkta_free_2st|0|0|value',__('Past','tarokina-free'),'no' );
        update_option( '_tarokki_free_complex|tkta_free_3st|0|0|value',__('Future','tarokina-free'),'no' );
        update_option( '_tarokki_free_complex|tkta_free_4st|0|0|value',null, 'no' );
        update_option( '_tarokki_free_complex|tkta_free_5st|0|0|value',null, 'no');
        update_option( '_tarokki_free_complex|tkta_free_6st|0|0|value',null, 'no' );
        update_option( '_tarokki_free_complex|tkta_free_7st|0|0|value',null, 'no' );
        update_option( '_tarokki_free_complex|tkta_free_8st|0|0|value',null, 'no' );
        update_option( '_tarokki_free_complex|tkta_free_9st|0|0|value',null, 'no' );
        update_option( '_tarokki_free_complex|tkta_free_10st|0|0|value',null,'no' );
        update_option( '_tarokki_free_complex|tkta_free_title_spread|0|0|value',__('Tarot title','tarokina-free'),'no' );
        update_option( '_tarokki_free_complex|tkta_free_subtitle_spread|0|0|value',__('Subtitle','tarokina-free'),'no' );
        update_option( '_tarokki_free_complex|tkta_free_text_top_deck2|0|0|value',__('Final card','tarokina-free'),'no' );
        update_option( '_tarokki_free_complex|tkta_free_text_button|0|0|value',__('Click to start your reading','tarokina-free'),'no' );
        update_option( '_tarokki_free_complex|tkta_free_text_button_volver|0|0|value',__('New reading','tarokina-free'),'no' );
        update_option( '_tarokki_free_complex|tkta_free_title_spread_color|0|0|value',null,'no' );
        update_option( '_tarokki_free_complex|tkta_free_texto_spread_color|0|0|value',null,'no' );
        update_option( '_tarokki_free_complex|tkta_free_background_color|0|0|value',null,'no' );
        update_option( '_tarokki_free_complex|tkta_free_image_background|0|0|value',null,'no' );
        update_option( '_tarokki_free_complex|tkta_free_image_transparent|0|0|value','0','no');


    wp_reset_postdata();
}