Passed
Push — master ( ece31d...41b8a6 )
by Paul
10:20 queued 04:17
created

NoticeController::getUserMeta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
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->renderRebusifyNotice($screen->post_type);
21
        $this->renderAddonsNotice($screen->id);
22
    }
23
24
    /**
25
     * @return void
26
     */
27
    public function routerDismissNotice(array $request)
28
    {
29
        $key = glsr_get($request, 'notice');
30
        $method = glsr(Helper::class)->buildMethodName($key, 'dismiss');
31
        if (method_exists($this, $method)) {
32
            call_user_func([$this, $method], $key);
33
        }
34
    }
35
36
    /**
37
     * @param string $key
38
     * @return void
39
     */
40
    protected function dismissRebusify($key)
41
    {
42
        $this->setUserMeta($key, glsr()->version('major'));
43
    }
44
45
    /**
46
     * @param string $key
47
     * @param mixed $fallback
48
     * @return mixed
49
     */
50
    protected function getUserMeta($key, $fallback)
51
    {
52
        $meta = get_user_meta(get_current_user_id(), static::USER_META_KEY, true);
53
        return glsr_get($meta, $key, $fallback);
54
    }
55
56
    /**
57
     * @param string $screenId
58
     * @return void
59
     */
60
    protected function renderAddonsNotice($screenId)
61
    {
62
        if (Application::POST_TYPE.'_page_addons' == $screenId) {
63
            echo glsr()->render('partials/notices/addons');
64
        }
65
    }
66
67
    /**
68
     * @param string $screenPostType
69
     * @return void
70
     */
71
    protected function renderRebusifyNotice($screenPostType)
72
    {
73
        if (Application::POST_TYPE == $screenPostType
74
            && version_compare(glsr()->version('major'), $this->getUserMeta('rebusify', 0), '>')
75
            && !glsr(OptionManager::class)->getBool('settings.general.rebusify')) {
76
            echo glsr()->render('partials/notices/rebusify');
77
        }
78
    }
79
80
    /**
81
     * @param string $key
82
     * @param mixed $fallback
83
     * @return mixed
84
     */
85
    protected function setUserMeta($key, $value)
86
    {
87
        $userId = get_current_user_id();
88
        $meta = (array) get_user_meta($userId, static::USER_META_KEY, true);
89
        $meta = array_filter(wp_parse_args($meta, []));
90
        $meta[$key] = $value;
91
        update_user_meta($userId, static::USER_META_KEY, $meta);
92
    }
93
}
94