PluginProbe
Metricool – Social media and site statistics / 2.0.2
Metricool – Social media and site statistics v2.0.2
2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / app / Features / AdminNotices / Notices / ReviewNotice.php

ReviewNotice.php in Metricool – Social media and site statistics 2.0.2, at app/Features/AdminNotices/Notices/ReviewNotice.php

162 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Metricool\Features\AdminNotices\Notices;
6
7 use Carbon\Carbon;
8 use Metricool\Features\AdminNotices\AbstractAdminNotice;
9 use Metricool\Http\Metricool\MetricoolApi;
10 use Metricool\Services\DashboardService;
11 use Metricool\Support\Helpers\Storages\EnvironmentConfig;
12 use Metricool\Traits\HasAllowlistControl;
13
14 final class ReviewNotice extends AbstractAdminNotice
15 {
16 use HasAllowlistControl;
17
18 public const IDENTIFIER = 'review';
19 private const MIN_SESSIONS_COUNT = 20;
20
21 private MetricoolApi $metricoolApi;
22 private DashboardService $dashboard;
23
24 public function __construct(EnvironmentConfig $env, MetricoolApi $metricoolApi, DashboardService $dashboard)
25 {
26 parent::__construct($env);
27
28 $this->metricoolApi = $metricoolApi;
29 $this->dashboard = $dashboard;
30 }
31
32 /**
33 * @inheritDoc
34 */
35 protected function canDisplay(): bool
36 {
37 return !$this->dashboard->isUserOnDashboard() &&
38 $this->onboardedThirtyDaysAgo() && $this->userCanManage();
39 }
40
41 /**
42 * @inheritDoc
43 */
44 public function isDismissable(): bool
45 {
46 return true;
47 }
48
49 /**
50 * @inheritDoc
51 */
52 public function isSnoozable(): bool
53 {
54 return true;
55 }
56
57 /**
58 * @inheritDoc
59 */
60 public function getSnoozeDays(): int
61 {
62 return 30;
63 }
64
65 /**
66 * @inheritDoc
67 */
68 public function getCtaUrl(): string
69 {
70 return $this->env->getUrl('plugin.review_url');
71 }
72
73 /**
74 * @inheritDoc
75 */
76 public function getCtaLabel(): string
77 {
78 return __('Leave a review', 'metricool');
79 }
80
81 /**
82 * @inheritDoc
83 */
84 protected function getContentView(): string
85 {
86 return 'admin/notices/review-notice';
87 }
88
89 /**
90 * @inheritDoc
91 */
92 protected function getContentVariables(): array
93 {
94 return [
95 'reviewMessage' => $this->getReviewNoticeMessage(),
96 ];
97 }
98
99 /**
100 * Check if the onboarding completion time is more than 30 days ago.
101 */
102 private function onboardedThirtyDaysAgo(): bool
103 {
104 $onboardingCompletedTimestamp = get_option('metricool_onboarding_completed_at');
105 if (empty($onboardingCompletedTimestamp)) {
106 return false;
107 }
108
109 return $this->timestampIsThirtyDaysAgo($onboardingCompletedTimestamp);
110 }
111
112 /**
113 * Check if the timestamp is more than 30 days ago.
114 * @param float|int|string $timestamp
115 */
116 private function timestampIsThirtyDaysAgo($timestamp): bool
117 {
118 $timestamp = Carbon::createFromTimestamp($timestamp);
119 $thirtyDaysAgo = Carbon::now()->subDays(30);
120
121 return $timestamp->isBefore($thirtyDaysAgo);
122 }
123
124 /**
125 * Returns the message we render in the notice. It only includes the amount
126 * of tracked sessions in the last 30 days if this exceeds
127 * {@see MIN_SESSIONS_COUNT}. Otherwise, it will render a general message.
128 */
129 private function getReviewNoticeMessage(): string
130 {
131 $mentionedStatistic = esc_html__('statistics', 'metricool');
132 $sessionCountLast30Days = $this->getSessionCountLast30Days();
133
134 if ($sessionCountLast30Days > self::MIN_SESSIONS_COUNT) {
135 $mentionedStatistic = ($sessionCountLast30Days . ' ' . esc_html__('sessions', 'metricool'));
136 }
137
138 return sprintf(
139 // translators: %s is replaced by either "x sessions" or "statistics", %2$ and %3$ are replaced with opening and closing a tag containing hyperlink
140 __('Hi, Metricool has tracked %1$s on your site for the last 30 days. If you have a moment, please consider leaving a review on wordpress.org to spread the word. We greatly appreciate it! If you have any questions or feedback, leave us a %2$smessage%3$s.', 'metricool'),
141 $mentionedStatistic,
142 '<a href="' . $this->env->getUrl('plugin.support_url') . '" rel="noopener noreferrer" target="_blank">',
143 '</a>'
144 );
145 }
146
147 /**
148 * Return amount of tracked sessions for the last 30 days (default filter)
149 * or return zero when the request fails.
150 */
151 private function getSessionCountLast30Days(): int
152 {
153 try {
154 return $this->metricoolApi->statistics()->visits()->filter([
155 'period' => 'last30days',
156 ])->get()->sum('amount');
157 } catch (\Throwable $e) {
158 return 0;
159 }
160 }
161 }
162