Passed
Push — master ( 1f45e8...4e7cf6 )
by Paul
09:12 queued 04:19
created

NoticeController::routerDismissNotice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 6
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Helper;
8
9
class NoticeController extends Controller
10
{
11
    const USER_META_KEY = '_glsr_notices';
12
13
    /**
14
     * @return void
15
     * @action admin_notices
16
     */
17
    public function filterAdminNotices()
18
    {
19
        $screen = glsr_current_screen();
20
        $this->renderWelcomeNotice($screen->post_type);
21
        $this->renderRebusifyNotice($screen->post_type);
22
        $this->renderAddonsNotice($screen->id);
23
    }
24
25
    /**
26
     * @return void
27
     */
28
    public function routerDismissNotice(array $request)
29
    {
30
        if ($key = glsr_get($request, 'notice')) {
31
            $this->dismissNotice($key);
32
        }
33
    }
34
35
    /**
36
     * @param string $key
37
     * @return void
38
     */
39
    protected function dismissNotice($key)
40
    {
41
        $this->setUserMeta($key, glsr()->version('major'));
42
    }
43
44
    /**
45
     * @param string $key
46
     * @param mixed $fallback
47
     * @return mixed
48
     */
49
    protected function getUserMeta($key, $fallback)
50
    {
51
        $meta = get_user_meta(get_current_user_id(), static::USER_META_KEY, true);
52
        return glsr_get($meta, $key, $fallback);
53
    }
54
55
    /**
56
     * @param string $screenId
57
     * @return void
58
     */
59
    protected function renderAddonsNotice($screenId)
60
    {
61
        if (Application::POST_TYPE.'_page_addons' == $screenId) {
62
            echo glsr()->render('partials/notices/addons');
63
        }
64
    }
65
66
    /**
67
     * @param string $screenPostType
68
     * @return void
69
     */
70
    protected function renderRebusifyNotice($screenPostType)
71
    {
72
        if (Application::POST_TYPE == $screenPostType
73
            && version_compare(glsr()->version('major'), $this->getUserMeta('rebusify', 0), '>')
74
            && !glsr(OptionManager::class)->getBool('settings.general.rebusify')) {
75
            echo glsr()->render('partials/notices/rebusify');
76
        }
77
    }
78
79
    /**
80
     * @param string $screenPostType
81
     * @return void
82
     */
83
    protected function renderWelcomeNotice($screenPostType)
84
    {
85
        if (Application::POST_TYPE == $screenPostType
86
            && version_compare(glsr()->version('major'), $this->getUserMeta('welcome', 0), '>')) {
87
            echo glsr()->render('partials/notices/welcome');
88
        }
89
    }
90
91
    /**
92
     * @param string $key
93
     * @param mixed $fallback
94
     * @return mixed
95
     */
96
    protected function setUserMeta($key, $value)
97
    {
98
        $userId = get_current_user_id();
99
        $meta = (array) get_user_meta($userId, static::USER_META_KEY, true);
100
        $meta = array_filter(wp_parse_args($meta, []));
101
        $meta[$key] = $value;
102
        update_user_meta($userId, static::USER_META_KEY, $meta);
103
    }
104
}
105