Passed
Push — master ( c2a16e...9283b2 )
by Paul
17:15 queued 07:52
created

Notice::normalizeArgs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 16
cp 0
rs 9.8666
cc 2
nc 2
nop 1
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
     * @param string[] $details
30
     * @return static
31
     */
32
    public function add($type, $message, array $details = [])
33
    {
34
        if (is_wp_error($message)) {
35
            $message = $message->get_error_message();
36
        }
37
        $this->notices[] = [
38
            'details' => (array) $details,
39
            'messages' => (array) $message,
40
            'type' => Str::restrictTo(['error', 'warning', 'info', 'success'], $type, 'info'),
41
        ];
42
        return $this;
43
    }
44
45
    /**
46
     * @param string|array|\WP_Error $message
47
     * @param string[] $details
48
     * @return static
49
     */
50
    public function addError($message, array $details = [])
51
    {
52
        return $this->add('error', $message, $details);
53
    }
54
55
    /**
56
     * @param string|array|\WP_Error $message
57
     * @param string[] $details
58
     * @return static
59
     */
60
    public function addInfo($message, array $details = [])
61
    {
62
        return $this->add('info', $message, $details);
63
    }
64
65
    /**
66
     * @param string|array|\WP_Error $message
67
     * @param string[] $details
68
     * @return static
69
     */
70
    public function addSuccess($message, array $details = [])
71
    {
72
        return $this->add('success', $message, $details);
73
    }
74
75
    /**
76
     * @param string|array|\WP_Error $message
77
     * @param string[] $details
78
     * @return static
79
     */
80
    public function addWarning($message, array $details = [])
81
    {
82
        return $this->add('warning', $message, $details);
83
    }
84
85
    /**
86
     * @return static
87
     */
88
    public function clear()
89
    {
90
        $this->notices = [];
91
        return $this;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function get()
98
    {
99
        $this->sort();
100
        $notices = glsr()->filterArray('notices', $this->notices);
101
        return array_reduce($notices, function ($carry, $args) {
102
            return $carry.glsr(Builder::class)->div($this->normalizeArgs($args));
103
        });
104
    }
105
106
    /**
107
     * @return static
108
     */
109
    public function sort()
110
    {
111
        $notices = array_map('unserialize', array_unique(array_map('serialize', $this->notices)));
112
        usort($notices, function ($a, $b) {
113
            $order = ['error', 'warning', 'info', 'success'];
114
            return array_search($a['type'], $order) - array_search($b['type'], $order);
115
        });
116
        $this->notices = $notices;
117
        return $this;
118
    }
119
120
    /**
121
     * @return static
122
     */
123
    public function store()
124
    {
125
        if (!empty($this->notices)) {
126
            set_transient(glsr()->prefix.'notices', $this->notices, 30);
127
        }
128
        return $this;
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    protected function normalizeArgs(array $args)
135
    {
136
        $class = sprintf('notice notice-%s inline is-dismissible', $args['type']);
137
        if (!empty($args['details'])) {
138
            $class = 'bulk-action-notice '.$class;
139
            $lastIndex = count($args['messages']) - 1;
140
            $args['messages'][$lastIndex] .= sprintf(' <button class="button-link bulk-action-errors-collapsed" aria-expanded="false">%s <span class="toggle-indicator" aria-hidden="true"></span></button>',
141
                _x('Show more details', 'admin-text', 'site-reviews')
142
            );
143
            $li = array_reduce($args['details'], function ($carry, $text) {
144
                return sprintf('%s<li>%s</li>', $carry, $text);
145
            });
146
            $args['messages'][] = sprintf('<ul class="bulk-action-errors hidden">%s</ul>', $li);
147
        }
148
        $text = array_reduce($args['messages'], function ($carry, $message) {
149
            return $carry.wpautop($message);
150
        });
151
        return compact('class', 'text');
152
    }
153
}
154