<?php

add_action('admin_enqueue_scripts', 'bdp_update_dashboard_style');
add_action('login_enqueue_scripts', 'bdp_update_login_style');

function bdp_update_login_style() {
	if (false === (bool)get_option( 'use_mareike_theme', false )) {
		$css = file_get_contents( BDP_LV_PLUGIN_DIR . 'assets/dashboard.style.css.tpl' );
		echo str_replace( '%%BDP_LV_PLUGIN_URL%%', BDP_LV_PLUGIN_URL, $css );
	}
}

function bdp_update_dashboard_style() {
	if (true === (bool)get_option( 'use_mareike_theme', false )) {
		#wp_enqueue_style( 'custom-dashboard-styles', BDP_LV_PLUGIN_URL . '/assets/mareike.css' );
		wp_enqueue_style( 'custom-dashboard-styles', BDP_LV_PLUGIN_URL . '/assets/wordpress-bdp.css' );
	} else {
		wp_enqueue_style( 'custom-dashboard-styles', BDP_LV_PLUGIN_URL . '/assets/wordpress-bdp.css' );
	}
    wp_enqueue_style('custom-calendar-styles', BDP_LV_PLUGIN_URL . '/assets/calendar.css');
    wp_enqueue_style('custom-security-styles', BDP_LV_PLUGIN_URL . '/assets/security.css');
}

function kompass_install_plugin($url, $slug) {
    // Überprüfen, ob die URL gültig ist
    if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
        echo 'Ungültige URL';
        return;
    }

    // Dateinamen und Pfad festlegen
    $tmp_file = download_url($url);

    // Überprüfen, ob der Download erfolgreich war
    if (is_wp_error($tmp_file)) {
        echo 'Download-Fehler: ' . $tmp_file->get_error_message();
        return;
    }

    // Pfad des Plugins
    $plugin_folder = WP_PLUGIN_DIR;

    // Plugin Upgrader Klassen einbinden
    require_once ABSPATH . 'wp-admin/includes/file.php';
    require_once ABSPATH . 'wp-admin/includes/plugin.php';
    require_once ABSPATH . 'wp-admin/includes/misc.php';
    require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
    require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
    require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';

    // Plugin upgrader initialisieren
    $upgrader = new Plugin_Upgrader(new WP_Ajax_Upgrader_Skin());

    // Plugin installieren
    $result = $upgrader->install($tmp_file);

    // Temp Datei löschen
    unlink($tmp_file);
    activate_plugin($slug . '/' . $slug . '.php' );


    // Überprüfen, ob die Installation erfolgreich war
    if (is_wp_error($result)) {
        echo 'Installations-Fehler: ' . $result->get_error_message();
    } else {
        echo 'Plugin erfolgreich installiert';
    }

}

