<?php
namespace com\cminds\listmanager\plugin\notices;

use com\cminds\listmanager\App;
use com\cminds\listmanager\plugin\notices\AdminNotice;

class AdminNoticeManager {

    private static $sessionKey = 'session_key';

    public static function init() {
        add_action('init', array(get_called_class(), 'actionInit'));
        add_action('admin_notices', array(get_called_class(), 'actionAdminNotices'));
    }

    public static function actionInit() {
        //if (!session_id()) {
        //    session_start();
        //}
        self::$sessionKey = sprintf('%s_admin_notice_manager', App::PREFIX);
        if (!isset($_SESSION[self::$sessionKey]) || !is_array($_SESSION[self::$sessionKey])) {
            $_SESSION[self::$sessionKey] = array();
        }
    }

    public static function actionAdminNotices() {
        $items = (array) $_SESSION[self::$sessionKey];
        foreach ($items as $item) {
            echo $item;
        }
        unset($_SESSION[self::$sessionKey]);
        session_write_close();
    }

    public static function enqueue(AdminNotice $item) {
        $items = (array) $_SESSION[self::$sessionKey];
        $items[] = $item;
        $_SESSION[self::$sessionKey] = $items;
    }

}