AdminNoticeManager::getAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Flynt\Utils;
4
5
// # TODO make strings translatable
6
7
/**
8
 * AdminNoticeManager provides an easy-to-use interface for the display of admin notices.
9
 *
10
 * Example usage:
11
 * use Flynt\Utils\AdminNoticeManager;
12
 *
13
 * // get the singleton instance of the manager
14
 * $manager = AdminNoticeManager::getInstance();
15
 *
16
 * // Prepare the admin notice (each string in array will be a paragraph)
17
 * $message = ['This notice will show up in the admin backend.'];
18
 * $options = [
19
 *   'type' => 'info', // possible values: 'error', 'warning', 'success', 'info'
20
 *   'title' => 'Flynt Notice',
21
 *   'dismissible' => true,
22
 * ];
23
 *
24
 * // Add the admin notice
25
 * $manager->addNotice($message, $options);
26
 */
27
class AdminNoticeManager
28
{
29
    protected static $instance = null;
30
    protected static $notices = [];
31
32
    const DEFAULT_OPTIONS = [
33
        'type' => 'info',
34
        'title' => 'Flynt - Oops, something went wrong',
35
        'dismissible' => true,
36
        'filenames' => ''
37
    ];
38
39
    public static function getInstance()
40
    {
41
        if (null === self::$instance) {
42
            self::$instance = new self();
43
        }
44
        return self::$instance;
45
    }
46
47
    /**
48
     * clone
49
     *
50
     * Prevent cloning with 'protected' keyword
51
     */
52
    protected function __clone()
53
    {
54
    }
55
56
    /**
57
     * constructor
58
     *
59
     * Prevent instantiation with 'protected' keyword
60
     */
61
    protected function __construct()
62
    {
63
    }
64
65
    public function addNotice($messages = [], $options = [])
66
    {
67
        if (empty($messages)) {
68
            return;
69
        }
70
71
        $options = array_merge(self::DEFAULT_OPTIONS, $options);
72
73
        $cssClasses = 'notice';
74
        $cssClasses .= $options['dismissible'] ? ' is-dismissible' : '';
75
        $cssClasses .= !empty($options['type']) ? " notice-{$options['type']}" : '';
76
77
        $msg = '';
78
79
        foreach ($messages as $message) {
80
            $msg .= '<p>' . $message . '</p>';
81
        }
82
83
        $msg .= '<p><i>To resolve this issue either follow the steps above'
84
        . " or remove the code requiring this functionality in your theme.</i></p>";
85
        $msg = "<div class=\"{$cssClasses}\">"
86
        . "<p><strong>{$options['title']}</strong></p>"
87
        . $msg . '</div>';
88
89
        add_action('admin_notices', function () use ($msg) {
90
            echo $msg;
91
        });
92
93
        array_push(self::$notices, [
94
            'options' => $options,
95
            'message' => $msg
96
        ]);
97
    }
98
99
    public function getAll()
100
    {
101
        return self::$notices;
102
    }
103
}
104