Passed
Push — master ( 5feb54...24c7e5 )
by Paul
12:51 queued 05:53
created

Notice::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
6
use GeminiLabs\SiteReviews\Helpers\Str;
7
use GeminiLabs\SiteReviews\Modules\Html\Builder;
8
9
class Notice
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $notices;
15
16
    public function __construct()
17
    {
18
        $this->notices = [];
19
        $notices = get_transient(glsr()->prefix.'notices');
20
        if (is_array($notices)) {
21
            $this->notices = $notices;
22
            delete_transient(glsr()->prefix.'notices');
23
        }
24
    }
25
26
    /**
27
     * @param string $type
28
     * @param string|array|\WP_Error $message
29
     * @return static
30
     */
31
    public function add($type, $message)
32
    {
33
        if (is_wp_error($message)) {
34
            $message = $message->get_error_message();
35
        }
36
        $this->notices[] = [
37
            'messages' => (array) $message,
38
            'type' => Str::restrictTo(['error', 'warning', 'info', 'success'], $type, 'info'),
39
        ];
40
        return $this;
41
    }
42
43
    /**
44
     * @param string|array|\WP_Error $message
45
     * @return static
46
     */
47
    public function addError($message)
48
    {
49
        return $this->add('error', $message);
50
    }
51
52
    /**
53
     * @param string|array|\WP_Error $message
54
     * @return static
55
     */
56
    public function addSuccess($message)
57
    {
58
        return $this->add('success', $message);
59
    }
60
61
    /**
62
     * @param string|array|\WP_Error $message
63
     * @return static
64
     */
65
    public function addWarning($message)
66
    {
67
        return $this->add('warning', $message);
68
    }
69
70
    /**
71
     * @return static
72
     */
73
    public function clear()
74
    {
75
        $this->notices = [];
76
        return $this;
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function get()
83
    {
84
        $this->sort();
85
        $notices = glsr()->filterArray('notices', $this->notices);
86
        return array_reduce($notices, function ($carry, $args) {
87
            $text = array_reduce($args['messages'], function ($carry, $message) {
88
                return $carry.wpautop($message);
89
            });
90
            return $carry.glsr(Builder::class)->div([
91
                'class' => sprintf('notice notice-%s inline is-dismissible', $args['type']),
92
                'text' => $text,
93
            ]);
94
        });
95
    }
96
97
    /**
98
     * @return static
99
     */
100
    public function sort()
101
    {
102
        $notices = array_map('unserialize', array_unique(array_map('serialize', $this->notices)));
103
        usort($notices, function ($a, $b) {
104
            $order = ['error', 'warning', 'info', 'success'];
105
            return array_search($a['type'], $order) - array_search($b['type'], $order);
106
        });
107
        $this->notices = $notices;
108
        return $this;
109
    }
110
111
    /**
112
     * @return static
113
     */
114
    public function store()
115
    {
116
        if (!empty($this->notices)) {
117
            set_transient(glsr()->prefix.'notices', $this->notices, 30);
118
        }
119
        return $this;
120
    }
121
}
122