<?php
    // prevent direct file access
	if( !defined( 'ABSPATH' ) ) exit;

    // extract multilingual strings from page builder data
    function pixe_extract_multilingual_strings($content, &$strings = array()){
        if( !is_array($content) || empty($content) ) return;

        if( !empty($content['_type']) && $content['_type'] == 'element' && !empty($content['_id']) ){
            $elem_id = $content['_id'];

            if( !empty($content['_tab']) ){
                $strings[$elem_id] = array();
                foreach($content['_tab'] as $i => $tab_value){
                    if( !empty($tab_value['title']) ){
                        $strings[$elem_id][$i . '|title'] = $tab_value['title'];
                    }

                    // recursive through tab content
                    if( !empty($tab_value['content']) ){
                        pixe_extract_multilingual_strings($tab_value['content'], $strings);
                    }
                }
            }else if( !empty($content['title']) ){
                $strings[$elem_id] = array('title' => $content['title']);
            }else if( !empty($content['content']) ){
                $strings[$elem_id] = array('content' => $content['content']);
            }else if( !empty($content['text']) ){
                $strings[$elem_id] = array('text' => $content['text']);
            }else if( !empty($content['left-text']) ){
                $strings[$elem_id] = array('left-text' => $content['left-text']);
            }else if( !empty($content['right-text']) ){
                $strings[$elem_id] = array('right-text' => $content['right-text']);
            }else if( !empty($content['scroll']['text']) ){
                $strings[$elem_id] = array('scroll|text' => $content['scroll']['text']);
            }

        // recursive through all data
        }else{
            foreach($content as $key => $value){
                pixe_extract_multilingual_strings($value, $strings);
            }
        }
    }

    // type - template/header/footer/synced_blocks
    // register multilingual string to string translation
    function pixe_register_multilingual_strings($type, $id, $data){
        $ml_strings = get_option('pixe_ml_' . $type, array());

        $strings = array();
        pixe_extract_multilingual_strings($data, $strings);

        if( empty($strings) ){
            unset($ml_strings[$id]);
        }else{
            $ml_strings[$id] = $strings;
        }

        update_option('pixe_ml_' . $type, $ml_strings, false);
    }

    // replace content in the path with value
    // path can separated with | sign
    function pixe_inject_data_value(&$content, $path, $value){
        if( empty($path) || empty($value) ) return;

        $keys = explode('|', $path);    
        foreach( $keys as $key ){
            if( empty($content[$key]) ) return;
            $content =& $content[$key];
        }
        $content = $value;
    }
    
    // recursively injected translated strings
    function pixe_inject_multilingual_strings(&$content, $strings = array()){
        if( empty($content) || !is_array($content) ) return;

        if( !empty($content['_type']) && $content['_type'] == 'element' && !empty($content['_id']) ){
            $elem_id = $content['_id'];

            // translate
            if( !empty($strings[$elem_id]) ){
                foreach($strings[$elem_id] as $path => $value){
                    pixe_inject_data_value($content, $path, $value);
                }
            } 

            // recursive for tabs
            if( !empty($content['_tab']) ){
                foreach($content['_tab'] as $i => $tab_value){

                    // recursive through tab content
                    if( !empty($tab_value['content']) ){
                        pixe_inject_multilingual_strings($content['_tab'][$i]['content'], $strings);
                    }
                }
            }

        // recursive through all data
        }else{
            foreach($content as $key => $value){
                pixe_inject_multilingual_strings($content[$key], $strings);
            }
        }
    }

    // translate the data
    function pixe_translate_multilingual_strings($type, $id, &$data){
        
        // wpml default language
        if( class_exists('SitePress') ) {
            if( defined('ICL_LANGUAGE_CODE') && ICL_LANGUAGE_CODE === apply_filters('wpml_default_language', null) ){ // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Add supports for WPML plugin
                return;
            }

        // polylang default language
        }else if( function_exists('pll_current_language') ){
            if( pll_current_language() === pll_default_language() ){
                return;
            }

        // no ml plugin installed
        }else{
            return;
        }

        $ml_strings = get_option('pixe_ml_' . $type, array());
        if( !empty($ml_strings[$id]) ){
            pixe_inject_multilingual_strings($data, $ml_strings[$id]);
        }
    }
