PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 3.0.2
WP STAGING – WordPress Backups, Restore, Migration & Clone v3.0.2
4.11.2 4.11.1 4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Analytics / AnalyticsConsent.php
wp-staging / Framework / Analytics Last commit date
Actions 3 years ago AnalyticsCleanup.php 4 years ago AnalyticsConsent.php 3 years ago AnalyticsEventDto.php 3 years ago AnalyticsSender.php 4 years ago WithAnalyticsAPI.php 4 years ago WithAnalyticsSiteInfo.php 3 years ago
AnalyticsConsent.php
213 lines
1 <?php
2
3 namespace WPStaging\Framework\Analytics;
4
5 use WPStaging\Framework\Notices\Notices;
6 use WPStaging\Core\WPStaging;
7
8 class AnalyticsConsent
9 {
10 use WithAnalyticsAPI;
11
12 /**
13 * Shows the admin notice asking the user whether he consents to sending usage information to WP STAGING.
14 * @action admin_notices
15 */
16 public function maybeShowConsentNotice()
17 {
18 // Early bail: Not a WP STAGING page
19 if (!WPStaging::make(Notices::class)->isWPStagingAdminPage()) {
20 return;
21 }
22
23 // Early bail: User does not have enough access to consent, or has consented already.
24 if (!current_user_can('manage_options') || get_option('wpstg_analytics_notice_dismissed')) {
25 return;
26 }
27
28 $remindMe = get_option('wpstg_analytics_consent_remind_me');
29
30 if (!empty($remindMe) && time() < $remindMe) {
31 return;
32 }
33
34 $notice = WPSTG_PLUGIN_DIR . '/Backend/views/notices/analytics-consent.php';
35
36 if (!file_exists($notice)) {
37 return;
38 }
39
40 include_once $notice;
41 }
42
43 /**
44 * If the request that sends the consent fails, we show a notice to let the user know.
45 * @action admin_notices
46 */
47 public function maybeShowConsentFailureNotice()
48 {
49 // Early bail: Not a WP STAGING page
50 if (!WPStaging::make(Notices::class)->isWPStagingAdminPage()) {
51 return;
52 }
53
54 // Early bail
55 if (!isset($_GET['wpstgConsentFailed'])) {
56 return;
57 }
58
59 $notice = WPSTG_PLUGIN_DIR . '/Backend/views/notices/analytics-consent-failed.php';
60
61 if (!file_exists($notice)) {
62 return;
63 }
64
65 include_once $notice;
66 }
67
68 /**
69 * Listens for whether the user has given or denied consent to send usage information.
70 * @action admin_init
71 */
72 public function listenForConsent()
73 {
74 // Early bail: Not a consent request
75 if (!isset($_GET['wpstgConsent'])) {
76 return;
77 }
78
79 // Early bail: Not enough permissions
80 if (!current_user_can('manage_options')) {
81 return;
82 }
83
84 // Early bail: Invalid nonce
85 check_ajax_referer('wpstg_consent_nonce', 'wpstgConsentNonce');
86
87 if ($_GET['wpstgConsent'] == 'later') {
88 update_option('wpstg_analytics_consent_remind_me', strtotime('+7 days'), false);
89
90 return;
91 }
92
93 // Early bail: User has not consented
94 if ($_GET['wpstgConsent'] == 'no') {
95 update_option('wpstg_analytics_notice_dismissed', '1', false);
96 update_option('wpstg_analytics_has_consent', '0', false);
97 delete_option('wpstg_analytics_consent_remind_me');
98
99 add_action('admin_notices', [$this, 'showNoticeConsentRefused']);
100
101 return;
102 }
103
104 if ($_GET['wpstgConsent'] == 'yes') {
105 try {
106 $this->giveConsent();
107 } catch (\Exception $e) {
108 // Show notice informing the user
109 wp_redirect(add_query_arg([
110 'wpstgConsentFailed' => true,
111 ], $this->getReturnUrl()));
112 exit;
113 }
114
115 update_option('wpstg_analytics_notice_dismissed', '1', false);
116 update_option('wpstg_analytics_has_consent', '1', false);
117 delete_option('wpstg_analytics_consent_remind_me');
118 }
119 }
120
121 public function showNoticeConsentRefused()
122 {
123 $notice = WPSTG_PLUGIN_DIR . '/Backend/views/notices/analytics-consent-refused.php';
124
125 if (!file_exists($notice)) {
126 return;
127 }
128
129 include_once $notice;
130 }
131
132 /**
133 * Registers the consent on the Analytics database
134 *
135 * @throws \Exception
136 */
137 public function giveConsent()
138 {
139 $url = $this->getApiUrl('consent');
140
141 $response = wp_remote_post($url, [
142 'method' => 'POST',
143 'headers' => ['Content-Type' => 'application/json; charset=utf-8'],
144 'body' => json_encode([
145 'site_hash' => $this->getSiteHash(),
146 'site_url' => get_home_url(),
147 ]),
148 'data_format' => 'body',
149 'timeout' => 10,
150 'redirection' => 5,
151 'httpversion' => '1.0',
152 'blocking' => true,
153 'sslverify' => false,
154 ]);
155
156 // Early bail: Something went wrong with the consent request.
157 if (is_wp_error($response) || !in_array(wp_remote_retrieve_response_code($response), [201, 409])) {
158 $errorMessage = is_wp_error($response) ? $response->get_error_message() : wp_remote_retrieve_body($response);
159 \WPStaging\functions\debug_log('WP STAGING Analytics Send Error: ' . $errorMessage, 'debug');
160
161 // Dismiss the consent notice so that it doesn't appear anymore
162 update_option('wpstg_analytics_notice_dismissed', '1', false);
163
164 throw new \Exception();
165 }
166 }
167
168 /**
169 * @return bool|null Whether the user has consented to the Analytics. Null if didn't answer.
170 */
171 public function hasUserConsent()
172 {
173 return get_option('wpstg_analytics_has_consent', null);
174 }
175
176 /**
177 * Invalidate the fact that the user has consented.
178 */
179 public function invalidateConsent()
180 {
181 delete_option('wpstg_analytics_notice_dismissed');
182 delete_option('wpstg_analytics_has_consent');
183 }
184
185 protected function getReturnUrl()
186 {
187 global $pagenow, $plugin_page;
188
189 return add_query_arg('page', $plugin_page, admin_url($pagenow));
190 }
191
192 /**
193 * @param bool $agreeOrDecline True to generate a link that agrees to send usage information. False to generate a link that disagrees.
194 *
195 * @return string The link to either agree or decline analytics.
196 */
197 public function getConsentLink($agreeOrDecline)
198 {
199 return add_query_arg([
200 'wpstgConsent' => $agreeOrDecline ? 'yes' : 'no',
201 'wpstgConsentNonce' => wp_create_nonce('wpstg_consent_nonce'),
202 ], $this->getReturnUrl());
203 }
204
205 public function getRemindMeLaterConsentLink()
206 {
207 return add_query_arg([
208 'wpstgConsent' => 'later',
209 'wpstgConsentNonce' => wp_create_nonce('wpstg_consent_nonce'),
210 ], $this->getReturnUrl());
211 }
212 }
213