AdminNoticeManager::addNotice()   B
last analyzed

Complexity

Conditions 6
Paths 17

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 17
nop 2
dl 0
loc 35
rs 8.7377
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Features\AdminNotices;
4
5
# TODO make strings translatable
6
7
class AdminNoticeManager
8
{
9
    protected static $instance = null;
10
    protected static $notices = [];
11
12
    const DEFAULT_OPTIONS = [
13
        'type' => 'info',
14
        'title' => 'Flynt - Oops, something went wrong',
15
        'dismissible' => true,
16
        'filenames' => ''
17
    ];
18
19
    public static function getInstance()
20
    {
21
        if (null === self::$instance) {
22
            self::$instance = new self;
23
        }
24
        return self::$instance;
25
    }
26
27
    /**
28
     * clone
29
     *
30
     * Prevent cloning with 'protected' keyword
31
    **/
32
    protected function __clone()
33
    {
34
    }
35
36
    /**
37
     * constructor
38
     *
39
     * Prevent instantiation with 'protected' keyword
40
    **/
41
    protected function __construct()
42
    {
43
    }
44
45
    public function addNotice($messages = [], $options = [])
46
    {
47
        if (empty($messages)) {
48
            return;
49
        }
50
51
        $options = array_merge(self::DEFAULT_OPTIONS, $options);
52
53
        $cssClasses = 'notice';
54
        $cssClasses .= $options['dismissible'] ? ' is-dismissible' : '';
55
        $cssClasses .= !empty($options['type']) ? " notice-{$options['type']}" : '';
56
57
        $files = !empty($options['filenames']) ? " ({$options['filenames']}) " : ' ';
58
59
        $msg = '';
60
61
        foreach ($messages as $message) {
62
            $msg .= '<p>' . $message . '</p>';
63
        }
64
65
        $msg .= '<p><i>To resolve this issue either follow the steps above'
66
        . " or remove the Feature{$files}requiring this functionality in your theme.</i></p>";
67
        $msg = "<div class=\"{$cssClasses}\">"
68
        . "<p><strong>{$options['title']}</strong></p>"
69
        . $msg . '</div>';
70
71
        add_action('admin_notices', function () use ($msg) {
72
            echo $msg;
73
        });
74
75
        array_push(self::$notices, [
76
            'options' => $options,
77
            'message' => $msg
78
        ]);
79
    }
80
81
    public function getAll()
82
    {
83
        return self::$notices;
84
    }
85
}
86