| Total Complexity | 43 |
| Total Lines | 460 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MonsterInsights_Onboarding_Wizard 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_Onboarding_Wizard, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class MonsterInsights_Onboarding_Wizard { |
||
| 19 | |||
| 20 | |||
| 21 | /** |
||
| 22 | * MonsterInsights_Onboarding_Wizard constructor. |
||
| 23 | */ |
||
| 24 | public function __construct() { |
||
| 25 | |||
| 26 | add_action( 'admin_init', array( $this, 'maybe_load_onboarding_wizard' ) ); |
||
| 27 | |||
| 28 | add_action( 'admin_menu', array( $this, 'add_dashboard_page' ) ); |
||
| 29 | |||
| 30 | add_action( 'wp_ajax_monsterinsights_onboarding_wpforms_install', array( |
||
| 31 | $this, |
||
| 32 | 'install_and_activate_wpforms', |
||
| 33 | ) ); |
||
| 34 | |||
| 35 | add_action( 'wp_ajax_monsterinsights_onboarding_get_errors', array( |
||
| 36 | $this, |
||
| 37 | 'get_install_errors', |
||
| 38 | ) ); |
||
| 39 | |||
| 40 | // This will only be called in the Onboarding Wizard context because of previous checks. |
||
| 41 | add_filter( 'monsterinsights_maybe_authenticate_siteurl', array( $this, 'change_return_url' ) ); |
||
| 42 | add_filter( 'monsterinsights_auth_success_redirect_url', array( $this, 'change_success_url' ) ); |
||
| 43 | add_filter( 'monsterinsights_reauth_success_redirect_url', array( $this, 'change_success_url' ) ); |
||
| 44 | |||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Checks if the Wizard should be loaded in current context. |
||
| 49 | */ |
||
| 50 | public function maybe_load_onboarding_wizard() { |
||
| 51 | |||
| 52 | // Check for wizard-specific parameter |
||
| 53 | // Allow plugins to disable the onboarding wizard |
||
| 54 | // Check if current user is allowed to save settings. |
||
| 55 | if ( ! ( isset( $_GET['page'] ) || 'monsterinsights-onboarding' !== $_GET['page'] || apply_filters( 'monsterinsights_enable_onboarding_wizard', true ) || ! current_user_can( 'monsterinsights_save_settings' ) ) ) { // WPCS: CSRF ok, input var ok. |
||
| 56 | return; |
||
| 57 | } |
||
| 58 | |||
| 59 | // Don't load the interface if doing an ajax call. |
||
| 60 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
||
| 61 | return; |
||
| 62 | } |
||
| 63 | |||
| 64 | $this->load_onboarding_wizard(); |
||
| 65 | |||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Register page through WordPress's hooks. |
||
| 70 | */ |
||
| 71 | public function add_dashboard_page() { |
||
| 72 | add_dashboard_page( '', '', 'monsterinsights_save_settings', 'monsterinsights-onboarding', '' ); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Load the Onboarding Wizard template. |
||
| 77 | */ |
||
| 78 | private function load_onboarding_wizard() { |
||
| 79 | |||
| 80 | $this->enqueue_scripts(); |
||
| 81 | |||
| 82 | $this->onboarding_wizard_header(); |
||
| 83 | $this->onboarding_wizard_content(); |
||
| 84 | $this->onboarding_wizard_footer(); |
||
| 85 | |||
| 86 | exit; |
||
|
|
|||
| 87 | |||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Load the scripts needed for the Onboarding Wizard. |
||
| 92 | */ |
||
| 93 | public function enqueue_scripts() { |
||
| 142 | ) |
||
| 143 | ); |
||
| 144 | |||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Outputs the simplified header used for the Onboarding Wizard. |
||
| 149 | */ |
||
| 150 | public function onboarding_wizard_header() { |
||
| 161 | <body class="monsterinsights-onboarding"> |
||
| 162 | <?php |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Outputs the content of the current step. |
||
| 167 | */ |
||
| 168 | public function onboarding_wizard_content() { |
||
| 169 | ?> |
||
| 170 | <div id="monsterinsights-vue-onboarding-wizard"></div> |
||
| 171 | <?php |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Outputs the simplified footer used for the Onboarding Wizard. |
||
| 176 | */ |
||
| 177 | public function onboarding_wizard_footer() { |
||
| 178 | ?> |
||
| 179 | <?php wp_print_scripts( 'monsterinsights-vue-script' ); ?> |
||
| 180 | </body> |
||
| 181 | </html> |
||
| 182 | <?php |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Check if we should suggest the EU Compliance addon by attempting to determine the website's location. |
||
| 187 | * |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | public function should_include_eu_addon() { |
||
| 222 | |||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Install WPForms lite and activate it, prevent initial setup step. |
||
| 227 | * |
||
| 228 | * @return null|string |
||
| 229 | */ |
||
| 230 | public function install_and_activate_wpforms() { |
||
| 311 | |||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Make a request to the front page and check if the tracking code is present. |
||
| 316 | * |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | public function is_code_installed_frontend() { |
||
| 320 | |||
| 321 | // Grab the front page html. |
||
| 322 | $request = wp_remote_request( home_url(), array( |
||
| 323 | 'sslverify' => false, |
||
| 324 | ) ); |
||
| 325 | $errors = array(); |
||
| 326 | |||
| 327 | if ( 200 === wp_remote_retrieve_response_code( $request ) ) { |
||
| 328 | |||
| 329 | $body = wp_remote_retrieve_body( $request ); |
||
| 330 | $current_ua_code = monsterinsights_get_ua_to_output(); |
||
| 331 | // Translators: The placeholders are for making the "We noticed you're using a caching plugin" text bold. |
||
| 332 | $cache_error = sprintf( esc_html__( '%1$sWe noticed you\'re using a caching plugin.%2$s Be sure to clear the cache to ensure the tracking appears on all pages and posts. %3$s(See this guide on how to clear cache)%4$s.', 'google-analytics-for-wordpress' ), '<b>', '</b>', ' <a href="https://www.wpbeginner.com/beginners-guide/how-to-clear-your-cache-in-wordpress/" target="_blank">', '</a>' ); |
||
| 333 | // Translators: The placeholders are for making the "We have detected multiple tracking codes" text bold & adding a link to support. |
||
| 334 | $multiple_ua_error = sprintf( esc_html__( '%1$sWe have detected multiple tracking codes%2$s, you should remove non-MonsterInsights ones, if you need help finding them please %3$sread this article%4$s.', 'ga-premium' ), '<b>', '</b>', '<a href="https://www.monsterinsights.com/docs/how-to-find-duplicate-google-analytics-tracking-codes-in-wordpress/" target="_blank">', '</a>' ); |
||
| 335 | |||
| 336 | // First, check if the tracking frontend code is present. |
||
| 337 | if ( false === strpos( $body, '__gaTracker' ) ) { |
||
| 338 | $errors[] = $cache_error; |
||
| 339 | } else { |
||
| 340 | // Check if the current UA code is actually present. |
||
| 341 | if ( $current_ua_code && false === strpos( $body, $current_ua_code ) ) { |
||
| 342 | // We have the tracking code but using another UA, so it's cached. |
||
| 343 | $errors[] = $cache_error; |
||
| 344 | } |
||
| 345 | // Grab all the UA codes from the page. |
||
| 346 | $pattern = '/UA-[^\s\;\']+/m'; |
||
| 347 | preg_match_all( $pattern, $body, $matches ); |
||
| 348 | // If more than twice ( because MI has a ga-disable-UA also ), let them know to remove the others. |
||
| 349 | if ( ! empty( $matches[0] ) && is_array( $matches[0] ) && count( $matches[0] ) > 2 ) { |
||
| 350 | $errors[] = $multiple_ua_error; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | return $errors; |
||
| 356 | |||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Update the redirect url so the user returns to the Onboarding Wizard after auth. |
||
| 361 | * |
||
| 362 | * @param string $siteurl The url to which the user is redirected for auth. |
||
| 363 | * |
||
| 364 | * @return mixed |
||
| 365 | */ |
||
| 366 | public function change_return_url( $siteurl ) { |
||
| 367 | |||
| 368 | $url = wp_parse_url( $siteurl ); |
||
| 369 | |||
| 370 | if ( isset( $url['query'] ) ) { |
||
| 371 | |||
| 372 | parse_str( $url['query'], $parameters ); |
||
| 373 | |||
| 374 | $parameters['return'] = rawurlencode( add_query_arg( array( |
||
| 375 | 'page' => 'monsterinsights-onboarding', |
||
| 376 | ), admin_url() ) ); |
||
| 377 | |||
| 378 | $siteurl = str_replace( $url['query'], '', $siteurl ); |
||
| 379 | |||
| 380 | $siteurl = add_query_arg( $parameters, $siteurl ); |
||
| 381 | |||
| 382 | $siteurl .= '#/authenticate'; |
||
| 383 | |||
| 384 | } |
||
| 385 | |||
| 386 | return $siteurl; |
||
| 387 | |||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Update the success redirect URL so if all is well we get to the next step. |
||
| 392 | * |
||
| 393 | * @param string $siteurl The url to which the user is redirected after a successful auth. |
||
| 394 | * |
||
| 395 | * @return mixed |
||
| 396 | */ |
||
| 397 | public function change_success_url( $siteurl ) { |
||
| 398 | |||
| 399 | $siteurl = add_query_arg( array( |
||
| 400 | 'page' => 'monsterinsights-onboarding', |
||
| 401 | ), admin_url() ); |
||
| 402 | |||
| 403 | $siteurl .= '#/recommended_settings'; |
||
| 404 | |||
| 405 | return $siteurl; |
||
| 406 | |||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Retrieve an array of European countries. |
||
| 411 | * |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | public static function get_eu_countries() { |
||
| 415 | return array( |
||
| 416 | 'AD', |
||
| 417 | 'AL', |
||
| 418 | 'AT', |
||
| 419 | 'AX', |
||
| 420 | 'BA', |
||
| 421 | 'BE', |
||
| 422 | 'BG', |
||
| 423 | 'BY', |
||
| 424 | 'CH', |
||
| 425 | 'CY', |
||
| 426 | 'CZ', |
||
| 427 | 'DE', |
||
| 428 | 'DK', |
||
| 429 | 'EE', |
||
| 430 | 'ES', |
||
| 431 | 'FI', |
||
| 432 | 'FO', |
||
| 433 | 'FR', |
||
| 434 | 'GB', |
||
| 435 | 'GG', |
||
| 436 | 'GI', |
||
| 437 | 'GR', |
||
| 438 | 'HR', |
||
| 439 | 'HU', |
||
| 440 | 'IE', |
||
| 441 | 'IM', |
||
| 442 | 'IS', |
||
| 443 | 'IT', |
||
| 444 | 'JE', |
||
| 445 | 'LI', |
||
| 446 | 'LT', |
||
| 447 | 'LU', |
||
| 448 | 'LV', |
||
| 449 | 'MC', |
||
| 450 | 'MD', |
||
| 451 | 'ME', |
||
| 452 | 'MK', |
||
| 453 | 'MT', |
||
| 454 | 'NL', |
||
| 455 | 'NO', |
||
| 456 | 'PL', |
||
| 457 | 'PT', |
||
| 458 | 'RO', |
||
| 459 | 'RS', |
||
| 460 | 'RU', |
||
| 461 | 'SE', |
||
| 462 | 'SI', |
||
| 463 | 'SJ', |
||
| 464 | 'SK', |
||
| 465 | 'SM', |
||
| 466 | 'TR', |
||
| 467 | 'UA', |
||
| 468 | 'VA', |
||
| 469 | ); |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Ajax handler for grabbing the installed code status. |
||
| 474 | */ |
||
| 475 | public function get_install_errors() { |
||
| 478 | |||
| 479 | } |
||
| 480 | |||
| 481 | } |
||
| 482 | |||
| 483 | new MonsterInsights_Onboarding_Wizard(); |
||
| 484 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.