<?php

/**
 * Plugin Name: Schaden Mail
 */

defined( 'ABSPATH' ) || exit;

// Define LPL_PLUGIN_FILE.
if ( ! defined( 'SA_PLUGIN_FILE' ) ) {
    define( 'SA_PLUGIN_FILE', __FILE__ );
}

/**
 * Main instance of teamleader_api_cs.
 *
 * @since  1.0
 * @return schaden_mail
 */
function SA() {
    return schaden_mail::instance();
}

global $schadenmail;
$schadenmail = SA();


class schaden_mail{

    /**
     * The single instance of the class.
     *
     * @var schaden_mail
     * @since 1.0
     */
    protected static $_instance = null;

    /**
     * @return schaden_mail
     */
    public static function instance()
    {
        if ( is_null( self::$_instance ) ) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    /*
	 * PA Constructor
	 */
    public function __construct()
    {
        do_action( 'SA_pre_load');

        register_activation_hook( SA_PLUGIN_FILE, [ $this, 'activation' ] );

        $this->define_constants();
        $this->include();
        $this->load();

        do_action( 'SA_is_load');
    }

    public function define_constants()
    {
        do_action( 'SA_pre_define');

        $this->define( 'SA_PATH',     trailingslashit( plugin_dir_path( SA_PLUGIN_FILE ) ) );
        $this->define( 'SA_INC_PATH', trailingslashit( SA_PATH . 'includes' ) );
        $this->define( 'SA_VEN_PATH', trailingslashit( SA_PATH . 'vendor' ) );
        $this->define( 'SA_DIR',      trailingslashit( plugin_dir_url( SA_PLUGIN_FILE ) ) );

        do_action( 'SA_after_define');
    }

    /**
     * Define constant if not already set.
     *
     * @param string      $name  Constant name.
     * @param string|bool $value Constant value.
     */
    private function define( $name, $value )
    {
        if ( ! defined( $name ) ) {
            define( $name, $value );
        }
    }

    public function include()
    {
        include_once SA_INC_PATH . 'class-schaden_helper_functions.php';
        include_once SA_INC_PATH . 'class-schaden_admin_backend.php';
        include_once SA_INC_PATH . 'class-schaden_mail_abruf.php';
    }

    public function activation()
    {
        include_once SA_INC_PATH . 'class-schaden_activation.php';
        new schaden_activation();
    }

    public function load()
    {

        if( is_admin() ) {
            new schaden_admin_backend();
        }

        add_filter('cron_schedules',[ $this, 'add_cronrunner' ]);
        new schaden_mail_abruf();

    }

    public function add_cronrunner( $schedules )
    {
        if(!isset($schedules["1min"])){
            $schedules["1min"] = array(
                'interval' => 60,
                'display' => __('every 1 minutes'));
        }
        return $schedules;
    }

}

