<?php
/*
Plugin Name: OgreAlert
Plugin URI: https://plugins.cleverogre.com/plugin/ogrealert/
Description: OgreAlert is a plugin developed by CleverOgre in Pensacola, Florida.
Version: 0.1.9
Author: CleverOgre
Author URI: https://cleverogre.com/
Icon1x: https://plugins.cleverogre.com/plugin/ogrealert/?asset=icon-sm
Icon2x: https://plugins.cleverogre.com/plugin/ogrealert/?asset=icon
BannerHigh: https://plugins.cleverogre.com/plugin/ogrealert/?asset=banner
BannerLow: https://plugins.cleverogre.com/plugin/ogrealert/?asset=banner-sm
Text Domain: ogrealert
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Copyright: © 2020 CleverOgre, Inc. All rights reserved.

    .-'''-.
   '   _    \                                                 .---.
 /   /` '.   \                        __.....__               |   |      __.....__
.   |     \  '  .--./)            .-''         '.             |   |  .-''         '.
|   '      |  '/.''\\   .-,.--.  /     .-''"'-.  `.           |   | /     .-''"'-.  `. .-,.--.      .|
\    \     / /| |  | |  |  .-. |/     /________\   \    __    |   |/     /________\   \|  .-. |   .' |_
 `.   ` ..' /  \`-' /   | |  | ||                  | .:--.'.  |   ||                  || |  | | .'     |
    '-...-'`   /("'`    | |  | |\    .-------------'/ |   \ | |   |\    .-------------'| |  | |'--.  .-'
               \ '---.  | |  '-  \    '-.____...---.`" __ | | |   | \    '-.____...---.| |  '-    |  |
                /'""'.\ | |       `.             .'  .'.''| | |   |  `.             .' | |        |  |
               ||     ||| |         `''-...... -'   / /   | |_'---'    `''-...... -'   | |        |  '.'
               \'. __// |_|                         \ \._,\ '/                         |_|        |   /
                `'---'                               `--'  `"                                     `'-'
*/

namespace OgreAlert;

if (!defined('ABSPATH')) exit;

class Plugin {

    public static function init() {
        $path = self::get_path(__FILE__);

        // Initialize Settings
        include_once($path . 'inc/settings.php');
        \OgreAlert\Settings::init();

        // Default Settings
        \OgreAlert\Settings::set('name', 'OgreAlert');
        \OgreAlert\Settings::set('version', '0.1.9');
        \OgreAlert\Settings::set('capability', 'edit_posts');
        \OgreAlert\Settings::set('path', $path);
        \OgreAlert\Settings::set('dir', self::get_dir(__FILE__));
        \OgreAlert\Settings::set('hook', self::get_hook(__FILE__));

        // Setup Plugin Updates
        include_once(\OgreAlert\Settings::get('path') . 'lib/wp-package-updater/class-wp-package-updater.php'); // Include private plugin updating library
        $package_updater = new \WP_Package_Updater(
            'https://plugins.cleverogre.com',
            wp_normalize_path(__FILE__),
            wp_normalize_path(plugin_dir_path(__FILE__)),
            false // License key not necessary
        );

        // Set Text Domain
        load_plugin_textdomain('ogrealert', false, plugin_basename(dirname(__FILE__)) . '/lang');

        // Plugin Filters
        add_filter('plugin_action_links_' . plugin_basename(__FILE__), array(__CLASS__, 'action_links'));

        // Delay loading
        add_action('plugins_loaded', array(__CLASS__, 'load'), 10, 0);
    }

    static function load() {
        // Load Core Classes
        include_once(\OgreAlert\Settings::get('path') . 'inc/alert.php');
        include_once(\OgreAlert\Settings::get('path') . 'inc/expiration.php');
        include_once(\OgreAlert\Settings::get('path') . 'inc/priority.php');
        include_once(\OgreAlert\Settings::get('path') . 'inc/display.php');
    }

    // Plugin Action Links

    static function action_links($links) {
        array_unshift($links, '<a href="' . esc_url(admin_url('edit.php?post_type=alert&page=ogrealert_settings')) . '">' . __('Settings', 'ogrealert') . '</a>');
        return $links;
    }

    // Plugin File Path Calculations

    private static function get_path($file) {
        return trailingslashit(dirname($file));
    }
    private static function get_dir($file) {
        $dir = trailingslashit(dirname($file));
        $count = 0;

        // Sanitize for Win32 installs
        $dir = str_replace('\\', '/', $dir);

        // If file is in plugins folder
        $wp_plugin_dir = str_replace('\\', '/', WP_PLUGIN_DIR);
        $dir = str_replace($wp_plugin_dir, plugins_url(), $dir, $count);

        if ($count < 1) {
            // If file is in wp-content folder
            $wp_content_dir = str_replace('\\', '/', WP_CONTENT_DIR);
            $dir = str_replace($wp_content_dir, content_url(), $dir, $count);
        }

        if ($count < 1) {
            // If file is in ??? folder
            $wp_dir = str_replace('\\', '/', ABSPATH);
            $dir = str_replace($wp_dir, site_url('/'), $dir);
        }

        return $dir;
    }
    private static function get_hook($file) {
        return basename(dirname($file)) . '/' . basename($file);
    }

}

\OgreAlert\Plugin::init();
