Passed
Push — master ( 2009cc...a406f9 )
by Paul
04:50 queued 21s
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\Helpers\Arr;
8
9
class NoticeController extends Controller
10
{
11
    const USER_META_KEY = '_glsr_notices';
12
13
    /**
14
     * @var array
15
     */
16
    protected $dismissValuesMap;
17
18
    public function __construct()
19
    {
20
        $this->dismissValuesMap = [
21
            'rebusify' => glsr()->version('major'),
22
            'welcome' => glsr()->version('minor'),
23
        ];
24
    }
25
26
    /**
27
     * @return void
28
     * @action admin_notices
29
     */
30
    public function filterAdminNotices()
31
    {
32
        $screen = glsr_current_screen();
33
        $this->renderWelcomeNotice($screen->post_type);
34
        $this->renderRebusifyNotice($screen->post_type);
35
        $this->renderAddonsNotice($screen->id);
36
    }
37
38
    /**
39
     * @return void
40
     */
41
    public function routerDismissNotice(array $request)
42
    {
43
        if ($key = Arr::get($request, 'notice')) {
44
            $this->dismissNotice($key);
45
        }
46
    }
47
48
    /**
49
     * @param string $key
50
     * @return void
51
     */
52
    protected function dismissNotice($key)
53
    {
54
        $this->setUserMeta($key, $this->getVersionFor($key));
55
    }
56
57
    /**
58
     * @param string $key
59
     * @param mixed $fallback
60
     * @return mixed
61
     */
62
    protected function getUserMeta($key, $fallback)
63
    {
64
        $meta = get_user_meta(get_current_user_id(), static::USER_META_KEY, true);
65
        return Arr::get($meta, $key, $fallback);
66
    }
67
68
    /**
69
     * @param string $noticeKey
70
     * @return string
71
     */
72
    protected function getVersionFor($noticeKey)
73
    {
74
        return Arr::get($this->dismissValuesMap, $noticeKey, glsr()->version('major'));
75
    }
76
77
    /**
78
     * @param string $screenId
79
     * @return void
80
     */
81
    protected function renderAddonsNotice($screenId)
82
    {
83
        if (Application::POST_TYPE.'_page_addons' == $screenId) {
84
            echo glsr()->render('partials/notices/addons');
85
        }
86
    }
87
88
    /**
89
     * @param string $screenPostType
90
     * @return void
91
     */
92
    protected function renderRebusifyNotice($screenPostType)
93
    {
94
        if (Application::POST_TYPE == $screenPostType
95
            && version_compare($this->getVersionFor('rebusify'), $this->getUserMeta('rebusify', 0), '>')
96
            && !glsr(OptionManager::class)->getBool('settings.general.rebusify')) {
97
            echo glsr()->render('partials/notices/rebusify');
98
        }
99
    }
100
101
    /**
102
     * @param string $screenPostType
103
     * @return void
104
     */
105
    protected function renderWelcomeNotice($screenPostType)
106
    {
107
        if (Application::POST_TYPE == $screenPostType
108
            && version_compare($this->getVersionFor('welcome'), $this->getUserMeta('welcome', 0), '>')) {
109
            $welcomeText = '0.0.0' == glsr(OptionManager::class)->get('version_upgraded_from')
110
                ? __('Thanks for installing Site Reviews %s, we hope you love it!', 'site-reviews')
111
                : __('Thanks for updating to Site Reviews %s, we hope you love the changes!', 'site-reviews');
112
            echo glsr()->render('partials/notices/welcome', [
113
                'text' => sprintf($welcomeText, glsr()->version),
114
            ]);
115
        }
116
    }
117
118
    /**
119
     * @param string $key
120
     * @param mixed $fallback
121
     * @return mixed
122
     */
123
    protected function setUserMeta($key, $value)
124
    {
125
        $userId = get_current_user_id();
126
        $meta = (array) get_user_meta($userId, static::USER_META_KEY, true);
127
        $meta = array_filter(wp_parse_args($meta, []));
128
        $meta[$key] = $value;
129
        update_user_meta($userId, static::USER_META_KEY, $meta);
130
    }
131
}
132