1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database; |
6
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
7
|
|
|
use GeminiLabs\SiteReviews\Helper; |
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Migrate; |
11
|
|
|
use GeminiLabs\SiteReviews\Request; |
12
|
|
|
|
13
|
|
|
class NoticeController extends Controller |
14
|
|
|
{ |
15
|
|
|
const USER_META_KEY = '_glsr_notices'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $dismissValuesMap; |
21
|
|
|
|
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->dismissValuesMap = [ |
25
|
|
|
'addons' => glsr()->version('major'), |
26
|
|
|
'welcome' => glsr()->version('minor'), |
27
|
|
|
]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return void |
32
|
|
|
* @filter admin_notices |
33
|
|
|
*/ |
34
|
|
|
public function adminNotices() |
35
|
|
|
{ |
36
|
|
|
$screen = glsr_current_screen(); |
37
|
|
|
// order is intentional! |
38
|
|
|
$this->renderAddonsNotice(); |
39
|
|
|
$this->renderWelcomeNotice($screen->post_type); |
40
|
|
|
$this->renderMigrationNotice($screen->post_type); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return void |
45
|
|
|
* @action site-reviews/route/admin/dismiss-notice |
46
|
|
|
*/ |
47
|
|
|
public function dismissNotice(Request $request) |
48
|
|
|
{ |
49
|
|
|
if ($request->notice) { |
50
|
|
|
$this->setUserMeta($request->notice, $this->getVersionFor($request->notice)); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return void |
56
|
|
|
* @action site-reviews/route/ajax/dismiss-notice |
57
|
|
|
*/ |
58
|
|
|
public function dismissNoticeAjax(Request $request) |
59
|
|
|
{ |
60
|
|
|
$this->dismissNotice($request); |
61
|
|
|
wp_send_json_success(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $key |
66
|
|
|
* @param mixed $fallback |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
protected function getUserMeta($key, $fallback) |
70
|
|
|
{ |
71
|
|
|
$meta = get_user_meta(get_current_user_id(), static::USER_META_KEY, true); |
72
|
|
|
return Arr::get($meta, $key, $fallback); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $noticeKey |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function getVersionFor($noticeKey) |
80
|
|
|
{ |
81
|
|
|
return Arr::get($this->dismissValuesMap, $noticeKey, glsr()->version('major')); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
protected function renderAddonsNotice() |
88
|
|
|
{ |
89
|
|
|
if ('site-review_page_addons' !== glsr_current_screen()->id |
90
|
|
|
&& Helper::isGreaterThan($this->getVersionFor('addons'), $this->getUserMeta('addons', 0)) |
91
|
|
|
&& glsr()->can('edit_others_posts')) { |
92
|
|
|
glsr()->render('partials/notices/addons'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $screenPostType |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
protected function renderMigrationNotice($screenPostType) |
101
|
|
|
{ |
102
|
|
|
if (glsr()->post_type == $screenPostType |
103
|
|
|
&& glsr()->hasPermission('tools', 'general') |
104
|
|
|
&& (glsr(Migrate::class)->isMigrationNeeded() || glsr(Database::class)->isMigrationNeeded())) { |
105
|
|
|
glsr()->render('partials/notices/migrate', [ |
106
|
|
|
'action' => glsr(Builder::class)->a([ |
107
|
|
|
'href' => admin_url('edit.php?post_type='.glsr()->post_type.'&page=tools#tab-general'), |
108
|
|
|
'text' => _x('Import Third Party Reviews', 'admin-text', 'site-reviews'), |
109
|
|
|
]), |
110
|
|
|
]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $screenPostType |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
protected function renderWelcomeNotice($screenPostType) |
119
|
|
|
{ |
120
|
|
|
if (glsr()->post_type == $screenPostType |
121
|
|
|
&& Helper::isGreaterThan($this->getVersionFor('welcome'), $this->getUserMeta('welcome', 0)) |
122
|
|
|
&& glsr()->can('edit_others_posts')) { |
123
|
|
|
$welcomeText = '0.0.0' == glsr(OptionManager::class)->get('version_upgraded_from') |
124
|
|
|
? _x('Thanks for installing Site Reviews v%s, we hope you love it!', 'admin-text', 'site-reviews') |
125
|
|
|
: _x('Thanks for updating to Site Reviews v%s, we hope you love the changes!', 'admin-text', 'site-reviews'); |
126
|
|
|
glsr()->render('partials/notices/welcome', [ |
127
|
|
|
'text' => sprintf($welcomeText, glsr()->version), |
128
|
|
|
]); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $key |
134
|
|
|
* @param mixed $value |
135
|
|
|
* @return mixed |
136
|
|
|
*/ |
137
|
|
|
protected function setUserMeta($key, $value) |
138
|
|
|
{ |
139
|
|
|
$userId = get_current_user_id(); |
140
|
|
|
$meta = (array) get_user_meta($userId, static::USER_META_KEY, true); |
141
|
|
|
$meta = array_filter(wp_parse_args($meta, [])); |
142
|
|
|
$meta[$key] = $value; |
143
|
|
|
update_user_meta($userId, static::USER_META_KEY, $meta); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|