<?php
/*
  File: custom login  - deprecated
*/
if( ! defined( 'ABSPATH' ) ) exit;

new login();
class login {

    public $loginscreen = array();
    /* 
        Constructor 
    */
    function __construct() {

        $this->customizer();
        $this->customlogin();
    }

    /*
        customizer settings extensions
    */
    public function customizer() {
        add_action( 'get_header', array($this, 'add_process_defaults') );
        add_action( 'customize_register', array($this, 'add_customizer_settings') );
    }
    public function add_process_defaults() {
        
        if (!array_key_exists('OPTseparator' ,processor::$root)) {
            // common prio to bundle sets
            $prio = 10;
            $section = 'section_modules';
            processor::$root = array_merge(processor::$root, array(

                /* section 'section_modules' */
                'OPTseparator'      => array( 'section' => 'section_modules', 'type' => 'separator',  'label' => 'Login Seite', 'mod' => 'sep_options', 'priority' => $prio ),
                'OPTclogin'         => array( 'section' => 'section_modules', 'type' => 'checkbox',   'label' => 'Individuell gestalten', 'mod' => 'custom_login_style', 'priority' => $prio, 'default' => 0 ),
                'OPTcinfo'          => array( 'section' => 'section_modules', 'type' => 'info',       'description' => 'Bei dem Feature handelt es sich um ein Beta-Feature. Nach dem Speichern deiner Seite kann es nicht mehr aktiviert werden. In zukünftigen Updates wird das Feature nicht mehr unterstützt.', 'mod' => 'custom_login_style_info', 'priority' => $prio, 'input_attrs' => 'warning', 'dependency' => array( array( 'element' => 'custom_login_style', 'value' => false ))),
                'OPTclogingroup'    => array( 'section' => 'section_modules', 'type' => 'group',      'label' => 'Einstellungen', 'mod' => 'group_custom_login', 'priority' => $prio, 'dependency' => 'custom_login_style', 'input_attrs' => array( 'children' => 8 ) ),
                'OPTcloginfile'     => array( 'section' => 'section_modules', 'type' => 'text',       'label' => 'Stylesheet Dateiname', 'description' => 'Die Datei muss im Ordner "assets/css/login/" hinterlegt sein.', 'mod' => 'custom_login_style_file', 'priority' => $prio, 'dependency' => 'custom_login_style', 'default' => '' ),
                'OPTcloginlogo'     => array( 'section' => 'section_modules', 'type' => 'image',      'label' => 'Logo', 'mod' => 'custom_login_style_logo', 'priority' => $prio, 'dependency' => 'custom_login_style', 'default' => '' ),
                'OPTcloginlogosze'  => array( 'section' => 'section_modules', 'type' => 'text',       'description' => 'Logo Größe', 'mod' => 'custom_login_style_logo_width', 'priority' => $prio, 'dependency' => 'custom_login_style', 'default' => '' ),
                'OPTcloginbg'       => array( 'section' => 'section_modules', 'type' => 'image',      'label' => 'Hintergrundbild', 'mod' => 'custom_login_style_background', 'priority' => $prio, 'dependency' => 'custom_login_style', 'default' => '' ),
                'OPTclogintitle'    => array( 'section' => 'section_modules', 'type' => 'text',       'label' => 'Texte', 'description' => 'Überschrift', 'mod' => 'custom_login_style_title', 'priority' => $prio, 'dependency' => 'custom_login_style', 'default' => '' ),
                'OPTcloginsub'      => array( 'section' => 'section_modules', 'type' => 'text',       'description' => 'Unterüberschrift', 'mod' => 'custom_login_style_sub', 'priority' => $prio, 'dependency' => 'custom_login_style', 'default' => '' ),
                'OPTcloginfooter'   => array( 'section' => 'section_modules', 'type' => 'text',       'label' => 'Footer Link', 'description' => 'Text', 'mod' => 'custom_login_style_footer', 'priority' => $prio, 'dependency' => 'custom_login_style', 'default' => '' ),
                'OPTcloginftlink'   => array( 'section' => 'section_modules', 'type' => 'text',       'description' => 'URL', 'mod' => 'custom_login_style_link', 'priority' => $prio, 'dependency' => 'custom_login_style', 'default' => '' ),

            ));
        }

    }
    public function add_customizer_settings($wp_customize) {

        $this->add_process_defaults();

    }

    /*
        custom loginscreen
    */
    public function customlogin() {

        $this->loginscreen['enable'] = get_theme_mod('custom_login_style', false);
        if ($this->loginscreen['enable']) {
            $this->loginscreen['file'] = get_theme_mod('custom_login_style_file', '');
            $this->loginscreen['title'] = get_theme_mod('custom_login_style_title', '');
            $this->loginscreen['sub'] = get_theme_mod('custom_login_style_sub', '');
            $this->loginscreen['footer_title'] = get_theme_mod('custom_login_style_footer', '');
            $this->loginscreen['footer_link'] = get_theme_mod('custom_login_style_link', esc_url( home_url( '/' ) ));

            $this->loginscreen['logo'] = get_theme_mod('custom_login_style_logo', '');
            $this->loginscreen['logo_width'] = get_theme_mod('custom_login_style_logo_width', 'unset');
            $this->loginscreen['background'] = get_theme_mod('custom_login_style_background', '');
            // debug($this->loginscreen['logo']);
            // debug($this->loginscreen['background']);

            // https://codex.wordpress.org/Customizing_the_Login_Form
            add_action( 'login_enqueue_scripts', array($this, 'custom_login_stylesheet') );
            add_filter( 'login_headertitle', array($this, 'custom_login_title') );
            add_filter( 'login_footer', array($this, 'custom_login_footer') );

            add_action( 'activate_wp_head', array($this, 'custom_activate_inline_style') );
            add_action( 'activate_header', array($this, 'custom_activate_stylesheet') );
        }
    }
    public function custom_login_stylesheet() {
        // $custom_login_style = get_theme_mod('custom_login_style', false);
        // $custom_login_style_file = get_theme_mod('custom_login_style_file', '');
        // debug($custom_login_style);
        // debug($custom_login_style_file);
        if ($this->loginscreen['enable']) {
            if ($this->loginscreen['file'] != "" && strpos($this->loginscreen['file'], ".css") > -1) {
                wp_enqueue_style( 'custom-login', get_template_directory_uri().'/assets/css/login/'.$this->loginscreen['file'] );
            }
            if ($this->loginscreen['background'] != "") {
                ?> <style type="text/css">
                    body {
                        background-image: url(<?=$this->loginscreen['background']?>) !important;
                        background-size: cover !important;
                        background-repeat: no-repeat !important;
                        background-attachment: fixed !important;
                        background-position: center center !important;
                    }
                </style> <?php
            }
            if ($this->loginscreen['logo'] != "") {
                ?> <style type="text/css">
                    #login h1 a, .login h1 a {
                        background-image: url(<?=$this->loginscreen['logo']?>);
                        background-size: <?=$this->loginscreen['logo_width']?>;
                        background-repeat: no-repeat;
                        background-position: 0px 0px;
                    }
                </style> <?php
            }
        }
    }  
    public function custom_activate_stylesheet() {
        if ($this->loginscreen['enable']) {
            if ($this->loginscreen['file'] != "" && strpos($this->loginscreen['file'], ".css") > -1) {
                wp_enqueue_style( 'custom-login', get_template_directory_uri().'/assets/css/login/'.$this->loginscreen['file'], array( 'default_style' ) );
            }
        }
    }  
    public function custom_activate_inline_style() {
        if ($this->loginscreen['enable']) {
            ?>
                <style type="text/css">
                    nav, .cookiebar { display: none; }
                    .main { margin: 0px; }
                </style>
            <?php if ($this->loginscreen['background'] != "") { ?>
                <style type="text/css">
                    body {
                        background-image: url(<?=$this->loginscreen['background']?>) !important;
                        background-size: cover !important;
                        background-repeat: no-repeat !important;
                        background-attachment: fixed !important;
                        background-position: center center !important;
                    }
                </style>
            <?php }
        }
    }  
    public function custom_login_title($title) {
        // $custom_login_style = get_theme_mod('custom_login_style', false);
        // $custom_login_style_file = get_theme_mod('custom_login_style_file', '');
        // $custom_login_style_title = get_theme_mod('custom_login_style_title', '');
        // $custom_login_style_sub = get_theme_mod('custom_login_style_sub', '');
        if ($this->loginscreen['enable'] && $this->loginscreen['file'] != "" && strpos($this->loginscreen['file'], ".css") > -1 && $this->loginscreen['title'] != "") {
            $sub = ($this->loginscreen['sub'] != "") ? "<br><i>".$this->loginscreen['sub']."</i>" : "";
            return $this->loginscreen['title'].$sub;
        }
        else
            return $title;
    }
    public function custom_login_footer() {
        // $custom_login_style = get_theme_mod('custom_login_style', false);
        // $custom_login_style_file = get_theme_mod('custom_login_style_file', '');
        // $custom_login_style_footer = get_theme_mod('custom_login_style_footer', '');
        // $custom_login_style_link = get_theme_mod('custom_login_style_link', esc_url( home_url( '/' ) ));
        if ($this->loginscreen['enable'] && $this->loginscreen['file'] != "" && strpos($this->loginscreen['file'], ".css") > -1 && $this->loginscreen['footer_title'] != "")
            echo "<div id='login_more'><p id='becomepartner'><a href='".$this->loginscreen['footer_link']."'>
                    ".$this->loginscreen['footer_title']."
                </a></p></div>";
    }

    
}

