<?php
/*
    compatibility module
    integrated in core theme
*/
if( ! defined( 'ABSPATH' ) ) exit;

new compatibility();
class compatibility {

    public $defaults = array(
        "compatibility_notice"          => "Sorry, your browser does not support all the features of this website. Please update it to a newer version or get a modern browser.",
        "compatibility_ff_link"         => "https://www.mozilla.org/de/firefox/",
        "compatibility_ff_label"        => "download Firefox",
        "compatibility_ch_link"         => "https://www.google.com/chrome/",
        "compatibility_ch_label"        => "download Google Chrome",
        "compatibility_op_link"         => "https://www.opera.com/de/download",
        "compatibility_op_label"        => "download Opera",
        "compatibility_continue_label"  => "ignore",
        "compatibility_choices"         => array( "link" => "primary link", "link sec" => "secondary link", "button" => "primary button", "button sec" => "secondary button", "button trd" => "alternate button" ),
    );

    /* 
        Constructor 
    */
    public function __construct() {

        $this->enqueue();
        $this->customizer();
        $this->render();
    }

    /*
        enqueue scripts and styles
    */
    public function enqueue() {
        if (!is_admin()) {
            add_action('init', array($this, 'get_strings'));
            add_action('wp_enqueue_scripts', array($this, 'load_scripts'));
        }
    }
    // load Strings
    public function get_strings() {
        $this->defaults = array(
            'compatibility_notice' => __("Dein Browser unterstützt leider nicht alle Funktionen dieser Website. Bitte aktualisiere ihn auf eine neuere Version oder hol’ dir einen modernen Browser.", 'core' ),
            'compatibility_ff_link' => 'https://www.mozilla.org/de/firefox/',
            'compatibility_ff_label' => __('zum Download von Firefox', 'core' ),
            'compatibility_ch_link' => 'https://www.google.com/chrome/',
            'compatibility_ch_label' => __('zum Download von Google Chrome', 'core' ),
            'compatibility_op_link' => 'https://www.opera.com/de/download',
            'compatibility_op_label' => __('zum Download von Opera', 'core' ),
            'compatibility_continue_label' => __('Ignorieren', 'core' ),
            'compatibility_choices' => array( 'link' => 'primärer Link', 'link sec' => 'sekundärer Link', 'button' => 'primärer Button', 'button sec' => 'sekundärer Button', 'button trd' => 'alternativer Button' ),
        );
    }
    // load Scripts and Style
    public function load_scripts() {
        global $config;     
        wp_register_script("compatibility_js", get_template_directory_uri().'/assets/js/compatibility.js', array('jquery'), $config->theme_version, true);
        if ($this->check_compatibility()) {
            wp_enqueue_script("compatibility_js");
            wp_add_inline_style('default_style', processor::process_styles(get_template_directory_uri().'/assets/css/compatibility.css'));
        }
    }


    /*
        customizer settings extensions
    */
    public function customizer() {
        add_action( 'get_header', array($this, 'add_process_defaults') );
        add_action( 'customize_register', array($this, 'add_customizer_settings') );
        add_action( 'customize_controls_enqueue_scripts', array($this, 'add_customizer_scripts'), 99 );
    }
    /* add inline script extension */
	public function add_customizer_scripts() {
		
		$js = "jQuery(function($) {
                    sessionStorage.removeItem('compatibility_preview');
                    $('#_customize-input-show_compatibility').on('click', function () {
                        if (!sessionStorage.getItem('compatibility_preview')) {
                            sessionStorage.setItem('compatibility_preview', JSON.stringify( { 'set': new Date().getTime() } ));
                            wp.customize.previewer.refresh();
                        }
                    } );
                    $('#_customize-input-hide_compatibility').on('click', function () {
                        if (sessionStorage.getItem('compatibility_preview')) {
                            sessionStorage.removeItem('compatibility_preview');
                            wp.customize.previewer.refresh();
                        }
                    } );        
                    wp.customize.section('section_modules', function(section) {
                        section.expanded.bind(function(isExpanded) {
                            if (!isExpanded) {
                                if (sessionStorage.getItem('compatibility_preview')) {
                                    sessionStorage.removeItem('compatibility_preview');
                                    wp.customize.previewer.refresh();
                                }
                            }
                        } );
                    } );              
                } );";
        wp_add_inline_script('customizer_admin', $js, 'after');
    }
    public function add_process_defaults() {
        
        if (!array_key_exists('COMseparator' ,processor::$root)) {
            // common prio to bundle sets
            $prio = 20;
            $section = 'section_modules';
            processor::$root = array_merge(processor::$root, array(
                'COMseparator'      => array( 'section' => $section, 'type' => 'separator', 'label' => 'Kompatibilität', 'mod' => 'sep_compatibility', 'priority' => $prio ),
                'COMenable'         => array( 'section' => $section, 'type' => 'checkbox',  'label' => 'Kompatibilitätsmeldung aktivieren', 'mod' => 'enable_compatibility', 'priority' => $prio, 'default' => 1, 'transport' => 'refresh' ),

                'COMgroup'          => array( 'section' => $section, 'type' => 'group',     'label' => 'Einstellungen', 'mod' => 'group_compatibility', 'priority' => $prio, 'input_attrs' => array( 'children' => 19 ), 
                                                                                                'dependency' => array( 'enable_compatibility' ) ),

                // 'COMPreseparator'   => array( 'section' => $section, 'type' => 'sub_separator', 'label' => 'Allgemein', 'mod' => 'sep_compatibility_preview', 'priority' => $prio, 
                //                                                                                 'dependency' => array( 'enable_compatibility' ) ),
                'COMshow'           => array( 'section' => $section, 'type' => 'button',    'label' => 'Vorschau anzeigen', 'mod' => 'show_compatibility', 'priority' => $prio, 'class' => 'secondary', 
                                                                                                'dependency' => array( 'enable_compatibility' ) ),
                'COMhide'           => array( 'section' => $section, 'type' => 'button',    'label' => 'Vorschau verstecken', 'mod' => 'hide_compatibility', 'priority' => $prio, 'class' => 'secondary', 
                                                                                                'dependency' => array( 'enable_compatibility' ) ),

                'COMcontent'        => array( 'section' => $section, 'type' => 'radio',     'label' => 'Inhalt',  'mod' => 'compatibility_content', 'default' => '', 'priority' => $prio, 'transport' => 'refresh', 
                                                                                                'dependency' => array( 'enable_compatibility' ), 
                                                                                                'choices' => array( '' => 'Nachfolgenden Inhalt anzeigen', 'template' => 'Template anzeigen' ) ),
                'COMtemplateedit'   => array( 'section' => $section, 'type' => 'button',    'label' => 'Template bearbeiten', 'description' => 'Template „Kompatibilitätsmeldung“ wird verwendet.', 'mod' => 'compatibility_template_edit', 'priority' => $prio, 'class' => 'secondary', 'input_attrs' => array( 'template' => 'compatibility', 'mode' => 'edit_template' ),
                                                                                                'dependency' => array( 'enable_compatibility', array( 'element' => 'compatibility_content', 'value' => 'template') ) ),
                'COMtemplatecreate' => array( 'section' => $section, 'type' => 'button',    'label' => 'Template erstellen', 'description' => '<small>Es wurde noch kein Template „Kompatibilitätsmeldung“ erstellt.</small>', 'mod' => 'compatibility_template_create', 'priority' => $prio, 'class' => 'secondary', 'input_attrs' => array( 'template' => 'compatibility', 'mode' => 'create_template' ),
                                                                                                'dependency' => array( 'enable_compatibility', array( 'element' => 'compatibility_content', 'value' => 'template') ) ),

                'COMnotice'         => array( 'section' => $section, 'type' => 'textarea',  'label' => 'Inhalt der Meldung', 'description' => 'Wenn aktiviert, wird dieser Text als Pop-up angezeigt, sobald der benutzte Browser nicht unterstützt wird.', 'mod' => 'compatibility_notice', 'priority' => $prio, 'default' => $this->defaults['compatibility_notice'], 'transport' => 'refresh', 
                                                                                                'dependency' => array( 'enable_compatibility', array( 'element' => 'compatibility_content', 'value' => '') ) ),

                'COMffLabel'        => array( 'section' => $section, 'type' => 'text',      'description' => 'Firefox Link', 'mod' => 'compatibility_ff_label', 'priority' => $prio, 'default' => $this->defaults['compatibility_ff_label'], 'transport' => 'refresh', 
                                                                                                'dependency' => array( 'enable_compatibility', array( 'element' => 'compatibility_content', 'value' => '') ) ),
                'COMffLink'         => array( 'section' => $section, 'type' => 'text',        'mod' => 'compatibility_ff_link', 'priority' => $prio, 'default' => $this->defaults['compatibility_ff_link'], 'transport' => 'refresh', 
                                                                                                'dependency' => array( 'enable_compatibility', array( 'element' => 'compatibility_content', 'value' => '') ) ),
                'COMffStyle'        => array( 'section' => $section, 'type' => 'select',      'mod' => 'compatibility_ff_style', 'priority' => $prio, 'default' => 'link', 'choices' => $this->defaults['compatibility_choices'], 'transport' => 'refresh', 
                                                                                                'dependency' => array( 'enable_compatibility', array( 'element' => 'compatibility_content', 'value' => '') ) ),

                'COMchLabel'        => array( 'section' => $section, 'type' => 'text',      'description' => 'Chrome Link', 'mod' => 'compatibility_ch_label', 'priority' => $prio, 'default' => $this->defaults['compatibility_ch_label'], 'transport' => 'refresh', 
                                                                                                'dependency' => array( 'enable_compatibility', array( 'element' => 'compatibility_content', 'value' => '') ) ),
                'COMchLink'         => array( 'section' => $section, 'type' => 'text',        'mod' => 'compatibility_ch_link', 'priority' => $prio, 'default' => $this->defaults['compatibility_ch_link'], 'transport' => 'refresh', 
                                                                                                'dependency' => array( 'enable_compatibility', array( 'element' => 'compatibility_content', 'value' => '') ) ),
                'COMchStyle'        => array( 'section' => $section, 'type' => 'select',      'mod' => 'compatibility_ch_style', 'priority' => $prio, 'default' => 'link', 'choices' => $this->defaults['compatibility_choices'], 'transport' => 'refresh', 
                                                                                                'dependency' => array( 'enable_compatibility', array( 'element' => 'compatibility_content', 'value' => '') ) ),

                'COMopLabel'        => array( 'section' => $section, 'type' => 'text',      'description' => 'Opera Link', 'mod' => 'compatibility_op_label', 'priority' => $prio, 'default' => $this->defaults['compatibility_op_label'], 'transport' => 'refresh', 
                                                                                                'dependency' => array( 'enable_compatibility', array( 'element' => 'compatibility_content', 'value' => '') ) ),
                'COMopLink'         => array( 'section' => $section, 'type' => 'text',        'mod' => 'compatibility_op_link', 'priority' => $prio, 'default' => $this->defaults['compatibility_op_link'], 'transport' => 'refresh', 
                                                                                                'dependency' => array( 'enable_compatibility', array( 'element' => 'compatibility_content', 'value' => '') ) ),
                'COMopStyle'        => array( 'section' => $section, 'type' => 'select',      'mod' => 'compatibility_op_style', 'priority' => $prio, 'default' => 'link', 'choices' => $this->defaults['compatibility_choices'], 'transport' => 'refresh', 
                                                                                                'dependency' => array( 'enable_compatibility', array( 'element' => 'compatibility_content', 'value' => '') ) ),

                'COMbtnLabel'       => array( 'section' => $section, 'type' => 'text',      'description' => 'Schließen Button', 'mod' => 'compatibility_continue_label', 'priority' => $prio, 'default' => $this->defaults['compatibility_continue_label'], 'transport' => 'refresh', 
                                                                                                'dependency' => array( 'enable_compatibility', array( 'element' => 'compatibility_content', 'value' => '') ) ),
                'COMbtnStyle'       => array( 'section' => $section, 'type' => 'select',      'mod' => 'compatibility_continue_style', 'priority' => $prio, 'default' => 'button', 'choices' => $this->defaults['compatibility_choices'], 'transport' => 'refresh', 
                                                                                                'dependency' => array( 'enable_compatibility', array( 'element' => 'compatibility_content', 'value' => '') ) ),
                                                                                                
                '--CMbgcolor'       => array( 'section' => $section, 'type' => 'colselect', 'label' => 'Allgemein', 'description' => 'Hintergrundfarbe', 'mod' => 'compatibility_background_color2', 'default' => 'color23', 'priority' => $prio, 'transport' => 'refresh', 
                                                                                                'dependency' => array( 'enable_compatibility' ) ),
                '--CMtxtcolor'      => array( 'section' => $section, 'type' => 'colselect',   'description' => 'Schriftfarbe', 'mod' => 'compatibility_text_color2', 'default' => 'color21', 'priority' => $prio, 'transport' => 'refresh', 
                                                                                                'dependency' => array( 'enable_compatibility' ) ),

            ));
        }

    }
    public function add_customizer_settings($wp_customize) {

        $this->add_process_defaults();

    }


    /*
        render compatibility
    */
    public function render() {
        add_action('compatibility_hook', array($this, 'add_compatibility'));
    }
    // render compatibility
    public function add_compatibility() {
        if ($this->check_compatibility()) {   
            
            $template_name = get_theme_mod('compatibility_content', '');
            if ($template_name == 'template') {
                $template_name = 'compatibility';
                $c = "<div class='compatibility_content animate_fast'>".do_shortcode("[dynamic template='".$template_name."']")."</div>";
            }
            else {
                // get theme mods
                $content = get_theme_mod('compatibility_notice') == "" ? $this->defaults['compatibility_notice'] : get_theme_mod('compatibility_notice');
                $ff_style = get_theme_mod('compatibility_ff_style') == "" ? 'link' : get_theme_mod('compatibility_ff_style');
                $ff_link = get_theme_mod('compatibility_ff_link') == "" ? $this->defaults['compatibility_ff_link'] : get_theme_mod('compatibility_ff_link');
                $ff_label = get_theme_mod('compatibility_ff_label') == "" ? $this->defaults['compatibility_ff_label'] : get_theme_mod('compatibility_ff_label');
                $ch_style = get_theme_mod('compatibility_ch_style') == "" ? 'link' : get_theme_mod('compatibility_ch_style');
                $ch_link = get_theme_mod('compatibility_ch_link') == "" ? $this->defaults['compatibility_ch_link'] : get_theme_mod('compatibility_ch_link');
                $ch_label = get_theme_mod('compatibility_ch_label') == "" ? $this->defaults['compatibility_ch_label'] : get_theme_mod('compatibility_ch_label');
                $op_style = get_theme_mod('compatibility_op_style') == "" ? 'link' : get_theme_mod('compatibility_op_style');
                $op_link = get_theme_mod('compatibility_op_link') == "" ? $this->defaults['compatibility_op_link'] : get_theme_mod('compatibility_op_link');
                $op_label = get_theme_mod('compatibility_op_label') == "" ? $this->defaults['compatibility_op_label'] : get_theme_mod('compatibility_op_label');
                $continue_style = get_theme_mod('compatibility_continue_style') == "" ? 'button' : get_theme_mod('compatibility_continue_style');
                $continue_label = get_theme_mod('compatibility_continue_label') == "" ? $this->defaults['compatibility_continue_label'] : get_theme_mod('compatibility_continue_label');
                $c = "<div class='compatibility_content mod animate_fast'>
                        <div class='compatibility_c animate_fast' >$content</div>
                        <a class='compatibility_link $ff_style animate_fast' href='$ff_link' target='_blank'>$ff_label</a>
                        <a class='compatibility_link $ch_style animate_fast' href='$ch_link' target='_blank'>$ch_label</a>
                        <a class='compatibility_link $op_style animate_fast' href='$op_link' target='_blank'>$op_label</a>
                        <div class='compatibility_button $continue_style animate_fast'>$continue_label</div>
                    </div>";
            }
            // render
            ob_start();
                echo "<div class='compatibility'>$c</div></div>";
            return ob_get_contents();
        }
    }
    // check if compatibility is enabled
    public function check_compatibility() {
        return (get_theme_mod('enable_compatibility')) ? true : false;
    }

}

