Passed
Branch master (29aa47)
by Chris
05:48
created

MonsterInsights_WP_Site_Health_Lite   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 382
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 173
dl 0
loc 382
rs 9.52
c 0
b 0
f 0
wmc 36

13 Methods

Rating   Name   Duplication   Size   Complexity  
A uses_fbia() 0 3 3
A __construct() 0 5 1
A uses_amp() 0 3 2
A test_check_ecommerce() 0 19 1
A is_ecommerce() 0 17 6
A is_licensed() 0 7 3
A test_check_authentication() 0 45 4
A test_check_license() 0 19 1
A test_check_autoupdates() 0 31 3
A add_tests() 0 46 5
A test_check_fbia() 0 19 1
A test_check_amp() 0 19 1
A test_check_connection() 0 34 5
1
<?php
2
/**
3
 * Add MonsterInsights tests to the WP Site Health area.
4
 */
5
6
/**
7
 * Class MonsterInsights_WP_Site_Health_Lite
8
 */
9
class MonsterInsights_WP_Site_Health_Lite {
10
11
	/**
12
	 * Is the site licensed?
13
	 *
14
	 * @var bool
15
	 */
16
	private $is_licensed;
17
	/**
18
	 * Is the site autherd?
19
	 *
20
	 * @var bool
21
	 */
22
	private $is_authed;
23
	/**
24
	 * Which eCommerce type, if any.
25
	 *
26
	 * @var bool|string
27
	 */
28
	private $ecommerce;
29
30
	/**
31
	 * MonsterInsights_WP_Site_Health_Lite constructor.
32
	 */
33
	public function __construct() {
34
35
		add_filter( 'site_status_tests', array( $this, 'add_tests' ) );
36
37
		add_action( 'wp_ajax_health-check-monsterinsights-test_connection', array( $this, 'test_check_connection' ) );
38
39
	}
40
41
	/**
42
	 * Add MonsterInsights WP Site Health tests.
43
	 *
44
	 * @param array $tests The current filters array.
45
	 *
46
	 * @return array
47
	 */
48
	public function add_tests( $tests ) {
49
50
		if ( $this->is_licensed() ) {
51
			$tests['direct']['monsterinsights_license'] = array(
52
				'label' => __( 'MonsterInsights License', 'google-analytics-for-wordpress' ),
53
				'test'  => array( $this, 'test_check_license' ),
54
			);
55
		}
56
57
		$tests['direct']['monsterinsights_auth'] = array(
58
			'label' => __( 'MonsterInsights Authentication', 'google-analytics-for-wordpress' ),
59
			'test'  => array( $this, 'test_check_authentication' ),
60
		);
61
62
		$tests['direct']['monsterinsights_automatic_updates'] = array(
63
			'label' => __( 'MonsterInsights Automatic Updates', 'google-analytics-for-wordpress' ),
64
			'test'  => array( $this, 'test_check_autoupdates' ),
65
		);
66
67
		if ( $this->is_ecommerce() ) {
68
			$tests['direct']['monsterinsights_ecommerce'] = array(
69
				'label' => __( 'MonsterInsights eCommerce', 'google-analytics-for-wordpress' ),
70
				'test'  => array( $this, 'test_check_ecommerce' ),
71
			);
72
		}
73
74
		if ( $this->uses_amp() ) {
75
			$tests['direct']['monsterinsights_amp'] = array(
76
				'label' => __( 'MonsterInsights AMP', 'google-analytics-for-wordpress' ),
77
				'test'  => array( $this, 'test_check_amp' ),
78
			);
79
		}
80
81
		if ( $this->uses_fbia() ) {
82
			$tests['direct']['monsterinsights_fbia'] = array(
83
				'label' => __( 'MonsterInsights FBIA', 'google-analytics-for-wordpress' ),
84
				'test'  => array( $this, 'test_check_fbia' ),
85
			);
86
		}
87
88
		$tests['async']['monsterinsights_connection'] = array(
89
			'label' => __( 'MonsterInsights Connection', 'google-analytics-for-wordpress' ),
90
			'test'  => 'monsterinsights_test_connection',
91
		);
92
93
		return $tests;
94
	}
95
96
	/**
97
	 * Checke if the website is licensed.
98
	 *
99
	 * @return bool
100
	 */
101
	public function is_licensed() {
102
103
		if ( ! isset( $this->is_licensed ) ) {
104
			$this->is_licensed = is_network_admin() ? MonsterInsights()->license->is_network_licensed() : MonsterInsights()->license->is_site_licensed();
0 ignored issues
show
Bug Best Practice introduced by
The property $license is declared protected in MonsterInsights_Lite. Since you implement __get, consider adding a @property or @property-read.
Loading history...
105
		}
106
107
		return $this->is_licensed;
108
109
	}
110
111
	/**
112
	 * Check if any of the supported eCommerce integrations are available.
113
	 *
114
	 * @return bool
115
	 */
116
	public function is_ecommerce() {
117
118
		if ( isset( $this->ecommerce ) ) {
119
			return $this->ecommerce;
120
		}
121
122
		$this->ecommerce = false;
123
124
		if ( class_exists( 'WooCommerce' ) ) {
125
			$this->ecommerce = 'WooCommerce';
126
		} else if ( class_exists( 'Easy_Digital_Downloads' ) ) {
127
			$this->ecommerce = 'Easy Digital Downloads';
128
		} else if ( defined( 'MEPR_VERSION' ) && version_compare( MEPR_VERSION, '1.3.43', '>' ) ) {
0 ignored issues
show
Bug introduced by
The constant MEPR_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
129
			$this->ecommerce = 'MemberPress';
130
		}
131
132
		return $this->ecommerce;
133
	}
134
135
	/**
136
	 * Is the site using AMP or has the AMP addon installed?
137
	 *
138
	 * @return bool
139
	 */
140
	public function uses_amp() {
141
142
		return class_exists( 'MonsterInsights_AMP' ) || defined( 'AMP__FILE__' );
143
144
	}
145
146
	/**
147
	 * Is the site using FB Instant Articles or has the FBIA addon installed?
148
	 *
149
	 * @return bool
150
	 */
151
	public function uses_fbia() {
152
153
		return class_exists( 'MonsterInsights_FB_Instant_Articles' ) || defined( 'IA_PLUGIN_VERSION' ) && version_compare( IA_PLUGIN_VERSION, '3.3.4', '>' );
0 ignored issues
show
Bug introduced by
The constant IA_PLUGIN_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
154
155
	}
156
157
	/**
158
	 * Check if MonsterInsights is authenticated and display a specific message.
159
	 *
160
	 * @return array
161
	 */
162
	public function test_check_authentication() {
163
		$result = array(
164
			'label'       => __( 'Your website is authenticated with MonsterInsights', 'google-analytics-for-wordpress' ),
165
			'status'      => 'good',
166
			'badge'       => array(
167
				'label' => __( 'MonsterInsights', 'google-analytics-for-wordpress' ),
168
				'color' => 'blue',
169
			),
170
			'description' => __( 'MonsterInsights integrates your WordPress website with Google Analytics.', 'google-analytics-for-wordpress' ),
171
			'actions'     => sprintf(
172
				'<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>',
173
				add_query_arg( 'page', 'monsterinsights_reports', admin_url( 'admin.php' ) ),
174
				__( 'View Reports', 'google-analytics-for-wordpress' )
175
			),
176
			'test'        => 'monsterinsights_auth',
177
		);
178
179
		$this->is_authed = MonsterInsights()->auth->is_authed() || MonsterInsights()->auth->is_network_authed();
0 ignored issues
show
Bug Best Practice introduced by
The property $auth is declared protected in MonsterInsights_Lite. Since you implement __get, consider adding a @property or @property-read.
Loading history...
180
181
		if ( ! $this->is_authed ) {
182
			if ( '' !== monsterinsights_get_ua() ) {
183
				// Using Manual UA.
184
				$result['status']      = 'recommended';
185
				$result['label']       = __( 'You are using Manual UA code output', 'google-analytics-for-wordpress' );
186
				$result['description'] = __( 'We highly recommend authenticating with MonsterInsights so that you can access our new reporting area and take advantage of new MonsterInsights features.', 'google-analytics-for-wordpress' );
187
				$result['actions']     = sprintf(
188
					'<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>',
189
					add_query_arg( 'page', 'monsterinsights_settings', admin_url( 'admin.php' ) ),
190
					__( 'Authenticate now', 'google-analytics-for-wordpress' )
191
				);
192
193
			} else {
194
				// Not authed at all.
195
				$result['status']      = 'critical';
196
				$result['label']       = __( 'Please configure your Google Analytics settings', 'google-analytics-for-wordpress' );
197
				$result['description'] = __( 'Your traffic is not being tracked by MonsterInsights at the moment and you are losing data. Authenticate and get access to the reporting area and advanced tracking features.', 'google-analytics-for-wordpress' );
198
				$result['actions']     = sprintf(
199
					'<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>',
200
					add_query_arg( 'page', 'monsterinsights_settings', admin_url( 'admin.php' ) ),
201
					__( 'Authenticate now', 'google-analytics-for-wordpress' )
202
				);
203
			}
204
		}
205
206
		return $result;
207
	}
208
209
	/**
210
	 * Check if the license is properly set up.
211
	 *
212
	 * @return array
213
	 */
214
	public function test_check_license() {
215
216
		$result = array(
217
			'status'      => 'critical',
218
			'badge'       => array(
219
				'label' => __( 'MonsterInsights', 'google-analytics-for-wordpress' ),
220
				'color' => 'blue',
221
			),
222
			'test'        => 'monsterinsights_license',
223
			'label'       => __( 'MonsterInsights Upgrade not applied', 'google-analytics-for-wordpress' ),
224
			'description' => __( 'A valid license has been added to MonsterInsights but you are still using the Lite version.', 'google-analytics-for-wordpress' ),
225
			'actions'     => sprintf(
226
				'<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>',
227
				add_query_arg( 'page', 'monsterinsights_settings', admin_url( 'admin.php' ) ),
228
				__( 'Go to License Settings', 'google-analytics-for-wordpress' )
229
			),
230
		);
231
232
		return $result;
233
	}
234
235
	/**
236
	 * Tests that run to check if autoupdates are enabled.
237
	 *
238
	 * @return array
239
	 */
240
	public function test_check_autoupdates() {
241
242
		$result = array(
243
			'label'       => __( 'Your website is receiving automatic updates', 'google-analytics-for-wordpress' ),
244
			'status'      => 'good',
245
			'badge'       => array(
246
				'label' => __( 'MonsterInsights', 'google-analytics-for-wordpress' ),
247
				'color' => 'blue',
248
			),
249
			'description' => __( 'MonsterInsights automatic updates are enabled and you are getting the latest features, bugfixes, and security updates as they are released.', 'google-analytics-for-wordpress' ),
250
			'test'        => 'monsterinsights_automatic_updates',
251
		);
252
253
		$updates_option = monsterinsights_get_option( 'automatic_updates', false );
254
255
		if ( 'minor' === $updates_option ) {
256
			$result['label']       = __( 'Your website is receiving minor updates', 'google-analytics-for-wordpress' );
257
			$result['description'] = __( 'MonsterInsights minor updates are enabled and you are getting the latest bugfixes and security updates, but not major features.', 'google-analytics-for-wordpress' );
258
		}
259
		if ( 'none' === $updates_option ) {
260
			$result['status']      = 'recommended';
261
			$result['label']       = __( 'Automatic updates are disabled', 'google-analytics-for-wordpress' );
262
			$result['description'] = __( 'MonsterInsights automatic updates are disabled. We recommend enabling automatic updates so you can get access to the latest features, bugfixes, and security updates as they are released.', 'google-analytics-for-wordpress' );
263
			$result['actions']     = sprintf(
264
				'<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>',
265
				add_query_arg( 'page', 'monsterinsights_settings#/advanced', admin_url( 'admin.php' ) ),
266
				__( 'Update Settings', 'google-analytics-for-wordpress' )
267
			);
268
		}
269
270
		return $result;
271
272
	}
273
274
	/**
275
	 * Tests that run to check if eCommerce is present.
276
	 *
277
	 * @return array
278
	 */
279
	public function test_check_ecommerce() {
280
		$result = array(
281
			'label'       => __( 'eCommerce data is not being tracked', 'google-analytics-for-wordpress' ),
282
			'status'      => 'recommended',
283
			'badge'       => array(
284
				'label' => __( 'MonsterInsights', 'google-analytics-for-wordpress' ),
285
				'color' => 'blue',
286
			),
287
			// Translators: The eCommerce store currently active.
288
			'description' => sprintf( __( 'You are using %s but the MonsterInsights eCommerce addon is not active, please Install & Activate it to start tracking eCommerce data.', 'google-analytics-for-wordpress' ), $this->ecommerce ),
289
			'test'        => 'monsterinsights_ecommerce',
290
			'actions'     => sprintf(
291
				'<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>',
292
				add_query_arg( 'page', 'monsterinsights_settings#/addons', admin_url( 'admin.php' ) ),
293
				__( 'View Addons', 'google-analytics-for-wordpress' )
294
			),
295
		);
296
297
		return $result;
298
	}
299
300
	/**
301
	 * Tests for the AMP cases.
302
	 *
303
	 * @return array
304
	 */
305
	public function test_check_amp() {
306
307
		$result = array(
308
			'label'       => __( 'AMP pages are not being tracked', 'google-analytics-for-wordpress' ),
309
			'status'      => 'recommended',
310
			'badge'       => array(
311
				'label' => __( 'MonsterInsights', 'google-analytics-for-wordpress' ),
312
				'color' => 'blue',
313
			),
314
			'description' => __( 'Your website has Google AMP-enabled pages set up but they are not tracked by Google Analytics at the moment. You need to Install & Activate the MonsterInsights AMP Addon.', 'google-analytics-for-wordpress' ),
315
			'test'        => 'monsterinsights_amp',
316
			'actions'     => sprintf(
317
				'<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>',
318
				add_query_arg( 'page', 'monsterinsights_settings#/addons', admin_url( 'admin.php' ) ),
319
				__( 'View Addons', 'google-analytics-for-wordpress' )
320
			),
321
		);
322
323
		return $result;
324
325
	}
326
327
	/**
328
	 * Tests for the FBIA cases.
329
	 *
330
	 * @return array
331
	 */
332
	public function test_check_fbia() {
333
334
		$result = array(
335
			'label'       => __( 'Facebook Instant Articles pages are not being tracked', 'google-analytics-for-wordpress' ),
336
			'status'      => 'recommended',
337
			'badge'       => array(
338
				'label' => __( 'MonsterInsights', 'google-analytics-for-wordpress' ),
339
				'color' => 'blue',
340
			),
341
			'description' => __( 'Your website has Facebook Instant Articles pages set up but they are not tracked by Google Analytics at the moment. You need to Install & Activate the MonsterInsights Facebook Instant Articles Addon.', 'google-analytics-for-wordpress' ),
342
			'test'        => 'monsterinsights_fbia',
343
			'actions'     => sprintf(
344
				'<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>',
345
				add_query_arg( 'page', 'monsterinsights_settings#/addons', admin_url( 'admin.php' ) ),
346
				__( 'View Addons', 'google-analytics-for-wordpress' )
347
			),
348
		);
349
350
		return $result;
351
352
	}
353
354
	/**
355
	 * Checks if there are errors communicating with Monsterinsights.com.
356
	 */
357
	public function test_check_connection() {
358
359
		$result = array(
360
			'label'       => __( 'Can connect to MonsterInsights.com correctly', 'google-analytics-for-wordpress' ),
361
			'status'      => 'good',
362
			'badge'       => array(
363
				'label' => __( 'MonsterInsights', 'google-analytics-for-wordpress' ),
364
				'color' => 'blue',
365
			),
366
			'description' => __( 'The MonsterInsights API is reachable and no connection issues have been detected.', 'google-analytics-for-wordpress' ),
367
			'test'        => 'monsterinsights_connection',
368
		);
369
370
		$url      = 'https://api.monsterinsights.com/v2/test/';
371
		$params   = array(
372
			'sslverify'  => false,
373
			'timeout'    => 2,
374
			'user-agent' => 'MonsterInsights/' . MONSTERINSIGHTS_VERSION,
375
			'body'       => '',
376
		);
377
		$response = wp_remote_get( $url, $params );
378
379
		if ( is_wp_error( $response ) || $response['response']['code'] < 200 || $response['response']['code'] > 300 ) {
380
			$result['status']      = 'critical';
381
			$result['label']       = __( 'The MonsterInsights server is not reachable.', 'google-analytics-for-wordpress' );
382
			$result['description'] = __( 'Your server is blocking external requests to monsterinsights.com, please check your firewall settings or contact your host for more details.', 'google-analytics-for-wordpress' );
383
384
			if ( is_wp_error( $response ) ) {
385
				// Translators: The error message received.
386
				$result['description'] .= ' ' . sprintf( __( 'Error message: %s', 'google-analytics-for-wordpress' ), $response->get_error_message() );
387
			}
388
		}
389
390
		wp_send_json_success( $result );
391
	}
392
}
393
394
new MonsterInsights_WP_Site_Health_Lite();
395
396