AbstractNotice   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 27
eloc 46
c 1
b 0
f 0
dl 0
loc 117
ccs 0
cts 58
cp 0
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A data() 0 3 1
A futureTime() 0 6 2
A isMonitored() 0 3 1
A isNoticeScreen() 0 3 1
A isIntroverted() 0 3 1
A render() 0 5 4
A __construct() 0 15 4
A isInFooter() 0 3 1
A isDismissible() 0 3 1
A hasPermission() 0 3 1
A storedVersion() 0 4 1
A canRender() 0 12 4
A version() 0 3 1
A isDismissed() 0 6 2
A dismiss() 0 8 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Notices;
4
5
use GeminiLabs\SiteReviews\Database\OptionManager;
6
use GeminiLabs\SiteReviews\Helper;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Helpers\Str;
9
use GeminiLabs\SiteReviews\Modules\Migrate;
10
11
abstract class AbstractNotice
12
{
13
    public const USER_META_KEY = '_glsr_notices';
14
15
    public $key;
16
17
    public function __construct()
18
    {
19
        $this->key = Str::dashCase(
20
            Str::removeSuffix((new \ReflectionClass($this))->getShortName(), 'Notice')
21
        );
22
        if (!$this->canRender()) {
23
            return;
24
        }
25
        if ($this->isMonitored()) {
26
            glsr()->append('notices', $this->key);
27
        }
28
        if ($this->isInFooter()) {
29
            add_action('in_admin_footer', [$this, 'render']);
30
        } else {
31
            add_action('in_admin_header', [$this, 'render']);
32
        }
33
    }
34
35
    public function dismiss(): void
36
    {
37
        if ($this->isDismissible()) {
38
            $userId = get_current_user_id();
39
            $meta = Arr::consolidate(get_user_meta($userId, static::USER_META_KEY, true));
40
            $meta = array_filter(wp_parse_args($meta, []));
41
            $meta[$this->key] = $this->version();
42
            update_user_meta($userId, static::USER_META_KEY, $meta);
43
        }
44
    }
45
46
    public function render(): void
47
    {
48
        $notices = glsr()->retrieveAs('array', 'notices');
49
        if (!$this->isIntroverted() || ($this->isIntroverted() && empty($notices))) { // @phpstan-ignore-line
50
            glsr()->render("partials/notices/{$this->key}", $this->data());
51
        }
52
    }
53
54
    protected function canRender(): bool
55
    {
56
        if (!$this->hasPermission()) {
57
            return false;
58
        }
59
        if (!$this->isNoticeScreen()) {
60
            return false;
61
        }
62
        if ($this->isDismissed()) {
63
            return false;
64
        }
65
        return true;
66
    }
67
68
    protected function data(): array
69
    {
70
        return [];
71
    }
72
73
    protected function futureTime(): int
74
    {
75
        $time = glsr(Migrate::class)->isMigrationNeeded()
76
            ? time() // now
77
            : glsr(Migrate::class)->lastRun();
78
        return $time + WEEK_IN_SECONDS;
79
    }
80
81
    protected function hasPermission(): bool
82
    {
83
        return glsr()->hasPermission('notices', $this->key);
84
    }
85
86
    protected function isDismissed(): bool
87
    {
88
        if (!$this->isDismissible()) {
89
            return false;
90
        }
91
        return !Helper::isGreaterThan($this->version(), $this->storedVersion());
92
    }
93
94
    protected function isDismissible(): bool
95
    {
96
        return true;
97
    }
98
99
    protected function isInFooter(): bool
100
    {
101
        return false;
102
    }
103
104
    protected function isIntroverted(): bool
105
    {
106
        return false;
107
    }
108
109
    protected function isMonitored(): bool
110
    {
111
        return true;
112
    }
113
114
    protected function isNoticeScreen(): bool
115
    {
116
        return str_starts_with(glsr_current_screen()->post_type, glsr()->post_type);
117
    }
118
119
    protected function storedVersion(): string
120
    {
121
        $meta = get_user_meta(get_current_user_id(), static::USER_META_KEY, true);
122
        return Arr::getAs('string', $meta, $this->key, '0');
123
    }
124
125
    protected function version(): string
126
    {
127
        return '0';
128
    }
129
}
130