<?php
namespace Wpupgrader\Libraries\WordPress\Plugins\Core;

class Notices
{
    static protected $instance = null;
    static protected $notices = [];
    static protected $errors = [];

    public static function get_instance()
    {
        if (null == self::$instance) {
            self::$instance = new static;
        }

        return self::$instance;
    }

    public function __construct()
    {
        if (\session_status() == PHP_SESSION_NONE) {
            \session_start();
        }

        if (isset($_SESSION['gdprc']['errors'])) {
            foreach ($_SESSION['gdprc']['errors'] as $error) {
                self::$errors[] = $error;
            }

            unset($_SESSION['gdprc']['errors']);
        }

        if (isset($_SESSION['gdprc']['notices'])) {
            foreach ($_SESSION['gdprc']['notices'] as $notice) {
                self::$notices[] = $notice;
            }

            unset($_SESSION['gdprc']['notices']);
        }

        \add_action('admin_notices', ['Wpupgrader\Libraries\WordPress\Plugins\Core\Notices', 'show_notices']);
    }

    static function show_notices()
    {
        $html = null;

        foreach (self::$errors as $error) {
            $html .= '<div class="error"> <p> '.$error.' </p> </div>';
        }

        foreach (self::$notices as $notice) {
            $html .= '<div class="updated"> <p> '.$notice.' </p> </div>';
        }

        echo $html;
    }

    function register_notice($message)
    {
        self::$notices[] = $message;
    }

    function register_redirect_notice($message)
    {
        $_SESSION['gdprc']['notices'][] = $message;
    }

    function register_error($message)
    {
        self::$errors[] = $message;
    }

    function register_redirect_error($message)
    {
        $_SESSION['gdprc']['errors'][] = $message;
    }
}