<?php

class rm_get_date_custom{
    public $lang;
    public $current_lang;
    public $info;
    public $lang_func_exists;
    public $pages;
    public $metas;
    
    function __construct() {
        $this->lang_func_exists = function_exists("pll_get_post");
        $this->current_lang = get_locale();
        $this->pages = [];
        $this->metas = [];
      }
    function get_page_id_from_option($key, $lang){
        if( ! isset($this->info[$lang])) {
            $this->info[$lang] = [];
        }
        if( ! isset($this->info[$lang][$key]) ) {
            $this->info[$lang][$key] = get_option($key, '');
        }
        
        return $this->info[$lang][$key];
    }
    
    function get_page_id_from_meta($key, $lang, $more_info_key=''){
        if( ! isset($this->info[$lang])) {
            $this->info[$lang] = [];
        }
        if( ! isset($this->info[$lang][$key]) ) {
            $meta_val_id = get_post_meta($key, $more_info_key, true);
            if( function_exists("pll_get_post") ) {
               $meta_val_id = pll_get_post( $meta_val_id, $lang );
            }
            $this->info[$lang][$key] = $meta_val_id;
        }
        
        return $this->info[$lang][$key];
    }
    
    
    // $get_type == id, option, meta
    // $get_key == 22, contact_us
    // $get_info_key == contact_us_page_id
    // $more_info_key:string == for option, meta 
    function get($get_type ='id', $get_key=0, $get_info_key=0, $return='id', $more_info_key='', $lang=false){
        $this->lang_func_exists = function_exists("pll_get_post");
        
        
        // If META && called before
        if( $return == 'meta' && isset($this->metas[$get_key ."_". $more_info_key]) ) {
            return $this->metas[$get_key ."_". $more_info_key];
        }
        
        
        $lang=($lang) ? : get_locale();
        $page_id = 0;


        if($get_type == 'home') {
            if($this->lang_func_exists) {
                return pll_home_url( );
            }
            else {
                return get_home_url();
            }
        }
        
        $get_key=($get_key)? : get_the_id();
        
        // check id or key
        if( $get_type == 'option' ) {
            $page_id = $this->get_page_id_from_option($get_key, $lang);
        }
        elseif( $get_type == 'meta' ) {
            $page_id = $this->get_page_id_from_meta($get_key, $lang, $get_info_key);
        }
        else {
            $page_id = $get_key;

            // If called before
            if( isset($this->pages[$get_key]) ) {
                $page_id = $this->pages[$get_key];
            }
            elseif( $this->lang_func_exists ) {
                $page_id = pll_get_post( $page_id, $lang );
                $this->pages[$get_key] = $page_id;
            }

        }

        if($return == 'link') {
            return get_permalink($page_id);
        }
        else if($return == 'meta') {
            $this->metas[$get_key ."_". $more_info_key] = get_post_meta($page_id, $more_info_key, true);
            return $this->metas[$get_key ."_". $more_info_key];
        }
        else if($return == 'acf') {
            return get_field($more_info_key, $page_id);
        }
        else if($return == 'id') {
            return $page_id;
        }
    }
    
    function get_terms($get_key=0, $return='id', $lang=false) {
        $lang=($lang) ? : get_locale();
        
        $t_id = pll_get_term( $get_key, $lang);
        
        if($return == 'link') {
            return get_term_link($t_id);
        }
        else if($return == 'id') {
            return $t_id;
        }
    }
}

$rm_get_date_custom = new rm_get_date_custom();
function rm_get_date_custom_bl($get_type ='id', $get_key=0, $get_info_key=0, $return='id', $more_info_key='', $lang=false){
    global $rm_get_date_custom;
    return $rm_get_date_custom->get($get_type, $get_key, $get_info_key, $return, $more_info_key, $lang);
}

function rm_get_date_custom_term_bl($get_key=0, $return='id', $lang=false){
    global $rm_get_date_custom;
    return  $rm_get_date_custom->get_terms($get_key, $return, $lang);
}




class rm_get_date_current_x_data{
    public $lang;
    public $current_lang;
    public $info;
    public $lang_func_exists;
    
}


function get_option_locale($key, $def='') {
    $key = $key . "_" . get_locale();
    return get_option($key, $def);
}