| Total Complexity | 56 |
| Total Lines | 494 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MonsterInsights_WP_Site_Health_Lite often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MonsterInsights_WP_Site_Health_Lite, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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() { |
||
| 40 | |||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Add MonsterInsights WP Site Health tests. |
||
| 45 | * |
||
| 46 | * @param array $tests The current filters array. |
||
| 47 | * |
||
| 48 | * @return array |
||
| 49 | */ |
||
| 50 | public function add_tests( $tests ) { |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Checks if the website is being tracked. |
||
| 100 | * |
||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | public function is_tracking() { |
||
| 104 | |||
| 105 | if ( ! isset( $this->is_tracking ) ) { |
||
| 106 | $ua = monsterinsights_get_ua(); |
||
| 107 | $this->is_tracking = ! empty( $ua ); |
||
| 108 | } |
||
| 109 | |||
| 110 | return $this->is_tracking; |
||
| 111 | |||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Check if any of the supported eCommerce integrations are available. |
||
| 116 | * |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | public function is_ecommerce() { |
||
| 120 | |||
| 121 | if ( isset( $this->ecommerce ) ) { |
||
| 122 | return $this->ecommerce; |
||
| 123 | } |
||
| 124 | |||
| 125 | $this->ecommerce = false; |
||
| 126 | |||
| 127 | if ( class_exists( 'WooCommerce' ) ) { |
||
| 128 | $this->ecommerce = 'WooCommerce'; |
||
| 129 | } else if ( class_exists( 'Easy_Digital_Downloads' ) ) { |
||
| 130 | $this->ecommerce = 'Easy Digital Downloads'; |
||
| 131 | } else if ( defined( 'MEPR_VERSION' ) && version_compare( MEPR_VERSION, '1.3.43', '>' ) ) { |
||
| 132 | $this->ecommerce = 'MemberPress'; |
||
| 133 | } else if ( function_exists( 'LLMS' ) && version_compare( LLMS()->version, '3.32.0', '>=' ) ) { |
||
| 134 | $this->ecommerce = 'LifterLMS'; |
||
| 135 | } |
||
| 136 | |||
| 137 | return $this->ecommerce; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Is the site using AMP or has the AMP addon installed? |
||
| 142 | * |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | public function uses_amp() { |
||
| 146 | |||
| 147 | return class_exists( 'MonsterInsights_AMP' ) || defined( 'AMP__FILE__' ); |
||
| 148 | |||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Is the site using FB Instant Articles or has the FBIA addon installed? |
||
| 153 | * |
||
| 154 | * @return bool |
||
| 155 | */ |
||
| 156 | public function uses_fbia() { |
||
| 159 | |||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Is Coming Soon / Maintenance / Under Construction mode being activated by another plugin? |
||
| 164 | * |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | private function is_coming_soon_active() { |
||
| 168 | if ( defined( 'SEED_CSP4_SHORTNAME' ) ) { |
||
| 169 | // SeedProd |
||
| 170 | // http://www.seedprod.com |
||
| 171 | |||
| 172 | $settings = get_option( 'seed_csp4_settings_content' ); |
||
| 173 | |||
| 174 | // 0: Disabled |
||
| 175 | // 1: Coming soon mode |
||
| 176 | // 2: Maintenance mode |
||
| 177 | return ! empty( $settings['status'] ); |
||
| 178 | } elseif ( defined( 'WPMM_PATH' ) ) { |
||
| 179 | // WP Maintenance Mode |
||
| 180 | // https://designmodo.com/ |
||
| 181 | |||
| 182 | $settings = get_option( 'wpmm_settings', array() ); |
||
| 183 | |||
| 184 | return isset( $settings['general']['status'] ) && 1 === $settings['general']['status']; |
||
| 185 | } elseif ( function_exists( 'csmm_get_options' ) ) { |
||
| 186 | // Minimal Coming Soon & Maintenance Mode |
||
| 187 | // https://comingsoonwp.com/ |
||
| 188 | |||
| 189 | $settings = csmm_get_options(); |
||
| 190 | |||
| 191 | return isset( $settings['status'] ) && 1 === $settings['status']; |
||
| 192 | } elseif ( defined( 'WPM_DIR' ) ) { |
||
| 193 | // WP Maintenance |
||
| 194 | // https://fr.wordpress.org/plugins/wp-maintenance/ |
||
| 195 | |||
| 196 | return '1' === get_option( 'wp_maintenance_active' ); |
||
| 197 | } elseif ( defined( 'ACX_CSMA_CURRENT_VERSION' ) ) { |
||
| 198 | // Under Construction / Maintenance Mode From Acurax |
||
| 199 | // http://www.acurax.com/products/under-construction-maintenance-mode-wordpress-plugin |
||
| 200 | |||
| 201 | return '1' === get_option( 'acx_csma_activation_status' ); |
||
| 202 | } elseif ( defined( 'SAHU_SO_PLUGIN_URL' ) ) { |
||
| 203 | // Site Offline |
||
| 204 | // http://www.freehtmldesigns.com |
||
| 205 | |||
| 206 | $settings = maybe_unserialize( get_option( 'sahu_so_dashboard' ) ); |
||
| 207 | |||
| 208 | return isset( $settings['sahu_so_status'] ) && '1' === $settings['sahu_so_status']; |
||
| 209 | } elseif ( defined( 'CSCS_GENEROPTION_PREFIX' ) ) { |
||
| 210 | // IgniteUp |
||
| 211 | // http://getigniteup.com |
||
| 212 | |||
| 213 | return '1' === get_option( CSCS_GENEROPTION_PREFIX . 'enable', '' ); |
||
| 214 | } elseif ( method_exists( 'UCP', 'is_construction_mode_enabled' ) ) { |
||
| 215 | // Under Construction by WebFactory Ltd |
||
| 216 | // https://underconstructionpage.com/ |
||
| 217 | |||
| 218 | return UCP::is_construction_mode_enabled( true ); |
||
| 219 | } elseif ( function_exists( 'mtnc_get_plugin_options' ) ) { |
||
| 220 | // Maintenance by WP Maintenance |
||
| 221 | // http://wordpress.org/plugins/maintenance/ |
||
| 222 | |||
| 223 | $settings = mtnc_get_plugin_options( true ); |
||
| 224 | |||
| 225 | return 1 === $settings['state']; |
||
| 226 | } elseif ( class_exists( 'CMP_Coming_Soon_and_Maintenance' ) ) { |
||
| 227 | // CMP Coming Soon & Maintenance |
||
| 228 | // https://wordpress.org/plugins/cmp-coming-soon-maintenance/ |
||
| 229 | |||
| 230 | return get_option( 'niteoCS_status' ); |
||
| 231 | } |
||
| 232 | |||
| 233 | return false; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Check if MonsterInsights is authenticated and display a specific message. |
||
| 238 | * |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | public function test_check_authentication() { |
||
| 242 | $result = array( |
||
| 243 | 'label' => __( 'Your website is authenticated with MonsterInsights', 'google-analytics-for-wordpress' ), |
||
| 244 | 'status' => 'good', |
||
| 245 | 'badge' => array( |
||
| 246 | 'label' => __( 'MonsterInsights', 'google-analytics-for-wordpress' ), |
||
| 247 | 'color' => 'blue', |
||
| 248 | ), |
||
| 249 | 'description' => __( 'MonsterInsights integrates your WordPress website with Google Analytics.', 'google-analytics-for-wordpress' ), |
||
| 250 | 'actions' => sprintf( |
||
| 251 | '<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>', |
||
| 252 | add_query_arg( 'page', 'monsterinsights_reports', admin_url( 'admin.php' ) ), |
||
| 253 | __( 'View Reports', 'google-analytics-for-wordpress' ) |
||
| 254 | ), |
||
| 255 | 'test' => 'monsterinsights_auth', |
||
| 256 | ); |
||
| 257 | |||
| 258 | $this->is_authed = MonsterInsights()->auth->is_authed() || MonsterInsights()->auth->is_network_authed(); |
||
| 259 | |||
| 260 | if ( ! $this->is_authed ) { |
||
| 261 | if ( '' !== monsterinsights_get_ua() ) { |
||
| 262 | // Using Manual UA. |
||
| 263 | $result['status'] = 'recommended'; |
||
| 264 | $result['label'] = __( 'You are using Manual UA code output', 'google-analytics-for-wordpress' ); |
||
| 265 | $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' ); |
||
| 266 | $result['actions'] = sprintf( |
||
| 267 | '<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>', |
||
| 268 | add_query_arg( 'page', 'monsterinsights_settings', admin_url( 'admin.php' ) ), |
||
| 269 | __( 'Authenticate now', 'google-analytics-for-wordpress' ) |
||
| 270 | ); |
||
| 271 | |||
| 272 | } else { |
||
| 273 | // Not authed at all. |
||
| 274 | $result['status'] = 'critical'; |
||
| 275 | $result['label'] = __( 'Please configure your Google Analytics settings', 'google-analytics-for-wordpress' ); |
||
| 276 | $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' ); |
||
| 277 | $result['actions'] = sprintf( |
||
| 278 | '<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>', |
||
| 279 | add_query_arg( 'page', 'monsterinsights_settings', admin_url( 'admin.php' ) ), |
||
| 280 | __( 'Authenticate now', 'google-analytics-for-wordpress' ) |
||
| 281 | ); |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | return $result; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Check if the license is properly set up. |
||
| 290 | * |
||
| 291 | * @return array |
||
| 292 | */ |
||
| 293 | public function test_check_license() { |
||
| 294 | |||
| 295 | $result = array( |
||
| 296 | 'status' => 'critical', |
||
| 297 | 'badge' => array( |
||
| 298 | 'label' => __( 'MonsterInsights', 'google-analytics-for-wordpress' ), |
||
| 299 | 'color' => 'blue', |
||
| 300 | ), |
||
| 301 | 'test' => 'monsterinsights_license', |
||
| 302 | 'label' => __( 'MonsterInsights Upgrade not applied', 'google-analytics-for-wordpress' ), |
||
| 303 | 'description' => __( 'A valid license has been added to MonsterInsights but you are still using the Lite version.', 'google-analytics-for-wordpress' ), |
||
| 304 | 'actions' => sprintf( |
||
| 305 | '<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>', |
||
| 306 | add_query_arg( 'page', 'monsterinsights_settings', admin_url( 'admin.php' ) ), |
||
| 307 | __( 'Go to License Settings', 'google-analytics-for-wordpress' ) |
||
| 308 | ), |
||
| 309 | ); |
||
| 310 | |||
| 311 | return $result; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Tests that run to check if autoupdates are enabled. |
||
| 316 | * |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | public function test_check_autoupdates() { |
||
| 320 | |||
| 321 | $result = array( |
||
| 322 | 'label' => __( 'Your website is receiving automatic updates', 'google-analytics-for-wordpress' ), |
||
| 323 | 'status' => 'good', |
||
| 324 | 'badge' => array( |
||
| 325 | 'label' => __( 'MonsterInsights', 'google-analytics-for-wordpress' ), |
||
| 326 | 'color' => 'blue', |
||
| 327 | ), |
||
| 328 | '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' ), |
||
| 329 | 'test' => 'monsterinsights_automatic_updates', |
||
| 330 | ); |
||
| 331 | |||
| 332 | $updates_option = monsterinsights_get_option( 'automatic_updates', false ); |
||
| 333 | |||
| 334 | if ( 'minor' === $updates_option ) { |
||
| 335 | $result['label'] = __( 'Your website is receiving minor updates', 'google-analytics-for-wordpress' ); |
||
| 336 | $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' ); |
||
| 337 | } |
||
| 338 | if ( 'none' === $updates_option ) { |
||
| 339 | $result['status'] = 'recommended'; |
||
| 340 | $result['label'] = __( 'Automatic updates are disabled', 'google-analytics-for-wordpress' ); |
||
| 341 | $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' ); |
||
| 342 | $result['actions'] = sprintf( |
||
| 343 | '<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>', |
||
| 344 | add_query_arg( 'page', 'monsterinsights_settings#/advanced', admin_url( 'admin.php' ) ), |
||
| 345 | __( 'Update Settings', 'google-analytics-for-wordpress' ) |
||
| 346 | ); |
||
| 347 | } |
||
| 348 | |||
| 349 | return $result; |
||
| 350 | |||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Tests that run to check if eCommerce is present. |
||
| 355 | * |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | public function test_check_ecommerce() { |
||
| 359 | $result = array( |
||
| 360 | 'label' => __( 'eCommerce data is not being tracked', 'google-analytics-for-wordpress' ), |
||
| 361 | 'status' => 'recommended', |
||
| 362 | 'badge' => array( |
||
| 363 | 'label' => __( 'MonsterInsights', 'google-analytics-for-wordpress' ), |
||
| 364 | 'color' => 'blue', |
||
| 365 | ), |
||
| 366 | // Translators: The eCommerce store currently active. |
||
| 367 | '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 ), |
||
| 368 | 'test' => 'monsterinsights_ecommerce', |
||
| 369 | 'actions' => sprintf( |
||
| 370 | '<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>', |
||
| 371 | add_query_arg( 'page', 'monsterinsights_settings#/addons', admin_url( 'admin.php' ) ), |
||
| 372 | __( 'View Addons', 'google-analytics-for-wordpress' ) |
||
| 373 | ), |
||
| 374 | ); |
||
| 375 | |||
| 376 | return $result; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Tests for the AMP cases. |
||
| 381 | * |
||
| 382 | * @return array |
||
| 383 | */ |
||
| 384 | public function test_check_amp() { |
||
| 385 | |||
| 386 | $result = array( |
||
| 387 | 'label' => __( 'AMP pages are not being tracked', 'google-analytics-for-wordpress' ), |
||
| 388 | 'status' => 'recommended', |
||
| 389 | 'badge' => array( |
||
| 390 | 'label' => __( 'MonsterInsights', 'google-analytics-for-wordpress' ), |
||
| 391 | 'color' => 'blue', |
||
| 392 | ), |
||
| 393 | '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' ), |
||
| 394 | 'test' => 'monsterinsights_amp', |
||
| 395 | 'actions' => sprintf( |
||
| 396 | '<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>', |
||
| 397 | add_query_arg( 'page', 'monsterinsights_settings#/addons', admin_url( 'admin.php' ) ), |
||
| 398 | __( 'View Addons', 'google-analytics-for-wordpress' ) |
||
| 399 | ), |
||
| 400 | ); |
||
| 401 | |||
| 402 | return $result; |
||
| 403 | |||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Tests for the FBIA cases. |
||
| 408 | * |
||
| 409 | * @return array |
||
| 410 | */ |
||
| 411 | public function test_check_fbia() { |
||
| 412 | |||
| 413 | $result = array( |
||
| 414 | 'label' => __( 'Facebook Instant Articles pages are not being tracked', 'google-analytics-for-wordpress' ), |
||
| 415 | 'status' => 'recommended', |
||
| 416 | 'badge' => array( |
||
| 417 | 'label' => __( 'MonsterInsights', 'google-analytics-for-wordpress' ), |
||
| 418 | 'color' => 'blue', |
||
| 419 | ), |
||
| 420 | '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' ), |
||
| 421 | 'test' => 'monsterinsights_fbia', |
||
| 422 | 'actions' => sprintf( |
||
| 423 | '<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>', |
||
| 424 | add_query_arg( 'page', 'monsterinsights_settings#/addons', admin_url( 'admin.php' ) ), |
||
| 425 | __( 'View Addons', 'google-analytics-for-wordpress' ) |
||
| 426 | ), |
||
| 427 | ); |
||
| 428 | |||
| 429 | return $result; |
||
| 430 | |||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Checks if there are errors communicating with Monsterinsights.com. |
||
| 435 | */ |
||
| 436 | public function test_check_connection() { |
||
| 437 | |||
| 438 | $result = array( |
||
| 439 | 'label' => __( 'Can connect to MonsterInsights.com correctly', 'google-analytics-for-wordpress' ), |
||
| 440 | 'status' => 'good', |
||
| 441 | 'badge' => array( |
||
| 442 | 'label' => __( 'MonsterInsights', 'google-analytics-for-wordpress' ), |
||
| 443 | 'color' => 'blue', |
||
| 444 | ), |
||
| 445 | 'description' => __( 'The MonsterInsights API is reachable and no connection issues have been detected.', 'google-analytics-for-wordpress' ), |
||
| 446 | 'test' => 'monsterinsights_connection', |
||
| 447 | ); |
||
| 448 | |||
| 449 | $url = 'https://api.monsterinsights.com/v2/test/'; |
||
| 450 | $params = array( |
||
| 451 | 'sslverify' => false, |
||
| 452 | 'timeout' => 2, |
||
| 453 | 'user-agent' => 'MonsterInsights/' . MONSTERINSIGHTS_VERSION, |
||
| 454 | 'body' => '', |
||
| 455 | ); |
||
| 456 | $response = wp_remote_get( $url, $params ); |
||
| 457 | |||
| 458 | if ( is_wp_error( $response ) || $response['response']['code'] < 200 || $response['response']['code'] > 300 ) { |
||
| 459 | $result['status'] = 'critical'; |
||
| 460 | $result['label'] = __( 'The MonsterInsights server is not reachable.', 'google-analytics-for-wordpress' ); |
||
| 461 | $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' ); |
||
| 462 | |||
| 463 | if ( is_wp_error( $response ) ) { |
||
| 464 | // Translators: The error message received. |
||
| 465 | $result['description'] .= ' ' . sprintf( __( 'Error message: %s', 'google-analytics-for-wordpress' ), $response->get_error_message() ); |
||
| 466 | } |
||
| 467 | } |
||
| 468 | |||
| 469 | wp_send_json_success( $result ); |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Checks if there is a duplicate tracker. |
||
| 474 | */ |
||
| 475 | public function test_check_tracking_code() { |
||
| 503 | } |
||
| 504 | } |
||
| 505 | |||
| 506 | new MonsterInsights_WP_Site_Health_Lite(); |
||
| 507 | |||
| 508 |