Passed
Push — master ( a9546d...6aabd8 )
by Paul
05:58
created

NoticeController::renderTrustalyzeNotice()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 5
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 30
rs 9.6111
c 0
b 0
f 0
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
use GeminiLabs\SiteReviews\Helpers\Arr;
9
use GeminiLabs\SiteReviews\Modules\Migrate;
10
11
class NoticeController extends Controller
12
{
13
    const USER_META_KEY = '_glsr_notices';
14
15
    /**
16
     * @var array
17
     */
18
    protected $dismissValuesMap;
19
20
    public function __construct()
21
    {
22
        $this->dismissValuesMap = [
23
            'trustalyze' => glsr()->version('major'),
24
            'welcome' => glsr()->version('minor'),
25
        ];
26
    }
27
28
    /**
29
     * @return void
30
     * @action admin_notices
31
     */
32
    public function filterAdminNotices()
33
    {
34
        $screen = glsr_current_screen();
35
        $this->renderWelcomeNotice($screen->post_type);
36
        $this->renderTrustalyzeNotice($screen->post_type);
37
    }
38
39
    /**
40
     * @return void
41
     */
42
    public function routerDismissNotice(array $request)
43
    {
44
        if ($key = Arr::get($request, 'notice')) {
45
            $this->dismissNotice($key);
46
        }
47
    }
48
49
    /**
50
     * @param string $key
51
     * @return void
52
     */
53
    protected function dismissNotice($key)
54
    {
55
        $this->setUserMeta($key, $this->getVersionFor($key));
56
    }
57
58
    /**
59
     * @param string $key
60
     * @param mixed $fallback
61
     * @return mixed
62
     */
63
    protected function getUserMeta($key, $fallback)
64
    {
65
        $meta = get_user_meta(get_current_user_id(), static::USER_META_KEY, true);
66
        return Arr::get($meta, $key, $fallback);
67
    }
68
69
    /**
70
     * @param string $noticeKey
71
     * @return string
72
     */
73
    protected function getVersionFor($noticeKey)
74
    {
75
        return Arr::get($this->dismissValuesMap, $noticeKey, glsr()->version('major'));
76
    }
77
78
    /**
79
     * @param string $screenPostType
80
     * @return void
81
     */
82
    protected function renderTrustalyzeNotice($screenPostType)
83
    {
84
        if (Application::POST_TYPE == $screenPostType
85
            && Helper::isGreaterThan($this->getVersionFor('trustalyze'), $this->getUserMeta('trustalyze', 0))
86
            && !glsr(OptionManager::class)->getBool('settings.general.trustalyze')
87
            && glsr()->can('manage_options')) {
88
            glsr()->render('partials/notices/trustalyze');
89
        }
90
    }
91
92
    /**
93
     * @param string $screenPostType
94
     * @return void
95
     */
96
    protected function renderWelcomeNotice($screenPostType)
97
    {
98
        if (Application::POST_TYPE == $screenPostType
99
            && Helper::isGreaterThan($this->getVersionFor('welcome'), $this->getUserMeta('welcome', 0))
100
            && glsr()->can('edit_others_posts')) {
101
            $welcomeText = '0.0.0' == glsr(OptionManager::class)->get('version_upgraded_from')
102
                ? __('Thanks for installing Site Reviews v%s, we hope you love it!', 'site-reviews')
103
                : __('Thanks for updating to Site Reviews v%s, we hope you love the changes!', 'site-reviews');
104
            glsr()->render('partials/notices/welcome', [
105
                'text' => sprintf($welcomeText, glsr()->version),
106
            ]);
107
        }
108
    }
109
110
    /**
111
     * @param string $key
112
     * @param mixed $fallback
113
     * @return mixed
114
     */
115
    protected function setUserMeta($key, $value)
116
    {
117
        $userId = get_current_user_id();
118
        $meta = (array) get_user_meta($userId, static::USER_META_KEY, true);
119
        $meta = array_filter(wp_parse_args($meta, []));
120
        $meta[$key] = $value;
121
        update_user_meta($userId, static::USER_META_KEY, $meta);
122
    }
123
}
124