Total Complexity | 43 |
Total Lines | 431 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 | add_action( 'network_admin_menu', array( $this, 'add_dashboard_page' ) ); |
||
30 | |||
31 | add_action( 'wp_ajax_monsterinsights_onboarding_wpforms_install', array( |
||
32 | $this, |
||
33 | 'install_and_activate_wpforms', |
||
34 | ) ); |
||
35 | |||
36 | add_action( 'wp_ajax_monsterinsights_onboarding_get_errors', array( |
||
37 | $this, |
||
38 | 'get_install_errors', |
||
39 | ) ); |
||
40 | |||
41 | // This will only be called in the Onboarding Wizard context because of previous checks. |
||
42 | add_filter( 'monsterinsights_maybe_authenticate_siteurl', array( $this, 'change_return_url' ) ); |
||
43 | add_filter( 'monsterinsights_auth_success_redirect_url', array( $this, 'change_success_url' ) ); |
||
44 | add_filter( 'monsterinsights_reauth_success_redirect_url', array( $this, 'change_success_url' ) ); |
||
45 | |||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Checks if the Wizard should be loaded in current context. |
||
50 | */ |
||
51 | public function maybe_load_onboarding_wizard() { |
||
52 | |||
53 | // Check for wizard-specific parameter |
||
54 | // Allow plugins to disable the onboarding wizard |
||
55 | // Check if current user is allowed to save settings. |
||
56 | 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. |
||
57 | return; |
||
58 | } |
||
59 | |||
60 | // Don't load the interface if doing an ajax call. |
||
61 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
||
62 | return; |
||
63 | } |
||
64 | |||
65 | set_current_screen(); |
||
66 | |||
67 | // Remove an action in the Gutenberg plugin ( not core Gutenberg ) which throws an error. |
||
68 | remove_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' ); |
||
69 | |||
70 | $this->load_onboarding_wizard(); |
||
71 | |||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Register page through WordPress's hooks. |
||
76 | */ |
||
77 | public function add_dashboard_page() { |
||
78 | add_dashboard_page( '', '', 'monsterinsights_save_settings', 'monsterinsights-onboarding', '' ); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Load the Onboarding Wizard template. |
||
83 | */ |
||
84 | private function load_onboarding_wizard() { |
||
85 | |||
86 | $this->enqueue_scripts(); |
||
87 | |||
88 | $this->onboarding_wizard_header(); |
||
89 | $this->onboarding_wizard_content(); |
||
90 | $this->onboarding_wizard_footer(); |
||
91 | |||
92 | exit; |
||
|
|||
93 | |||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Load the scripts needed for the Onboarding Wizard. |
||
98 | */ |
||
99 | public function enqueue_scripts() { |
||
157 | ) |
||
158 | ); |
||
159 | |||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Outputs the simplified header used for the Onboarding Wizard. |
||
164 | */ |
||
165 | public function onboarding_wizard_header() { |
||
177 | <body class="monsterinsights-onboarding"> |
||
178 | <?php |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Outputs the content of the current step. |
||
183 | */ |
||
184 | public function onboarding_wizard_content() { |
||
185 | $admin_url = is_network_admin() ? network_admin_url() : admin_url(); |
||
186 | |||
187 | monsterinsights_settings_error_page( 'monsterinsights-vue-onboarding-wizard', '<a href="' . $admin_url . '">' . esc_html__( 'Return to Dashboard', 'google-analytics-for-wordpress' ) . '</a>' ); |
||
188 | monsterinsights_settings_inline_js(); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Outputs the simplified footer used for the Onboarding Wizard. |
||
193 | */ |
||
194 | public function onboarding_wizard_footer() { |
||
195 | ?> |
||
196 | <?php wp_print_scripts( 'monsterinsights-vue-script' ); ?> |
||
197 | </body> |
||
198 | </html> |
||
199 | <?php |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Check if we should suggest the EU Compliance addon by attempting to determine the website's location. |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | public function should_include_eu_addon() { |
||
239 | |||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Install WPForms lite and activate it, prevent initial setup step. |
||
244 | * |
||
245 | * @return null|string |
||
246 | */ |
||
247 | public function install_and_activate_wpforms() { |
||
323 | |||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Update the redirect url so the user returns to the Onboarding Wizard after auth. |
||
328 | * |
||
329 | * @param string $siteurl The url to which the user is redirected for auth. |
||
330 | * |
||
331 | * @return mixed |
||
332 | */ |
||
333 | public function change_return_url( $siteurl ) { |
||
334 | |||
335 | $url = wp_parse_url( $siteurl ); |
||
336 | $admin_url = is_network_admin() ? network_admin_url() : admin_url(); |
||
337 | |||
338 | if ( isset( $url['query'] ) ) { |
||
339 | |||
340 | parse_str( $url['query'], $parameters ); |
||
341 | |||
342 | $parameters['return'] = rawurlencode( add_query_arg( array( |
||
343 | 'page' => 'monsterinsights-onboarding', |
||
344 | ), $admin_url ) ); |
||
345 | |||
346 | $siteurl = str_replace( $url['query'], '', $siteurl ); |
||
347 | |||
348 | $siteurl = add_query_arg( $parameters, $siteurl ); |
||
349 | |||
350 | $siteurl .= '#/authenticate'; |
||
351 | |||
352 | } |
||
353 | |||
354 | return $siteurl; |
||
355 | |||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Update the success redirect URL so if all is well we get to the next step. |
||
360 | * |
||
361 | * @param string $siteurl The url to which the user is redirected after a successful auth. |
||
362 | * |
||
363 | * @return mixed |
||
364 | */ |
||
365 | public function change_success_url( $siteurl ) { |
||
366 | |||
367 | $admin_url = is_network_admin() ? network_admin_url() : admin_url(); |
||
368 | $return_step = is_network_admin() ? 'recommended_addons' : 'recommended_settings'; |
||
369 | |||
370 | $siteurl = add_query_arg( array( |
||
371 | 'page' => 'monsterinsights-onboarding', |
||
372 | ), $admin_url ); |
||
373 | |||
374 | $siteurl .= '#/' . $return_step; |
||
375 | |||
376 | return $siteurl; |
||
377 | |||
378 | } |
||
379 | |||
380 | /** |
||
381 | * Retrieve an array of European countries. |
||
382 | * |
||
383 | * @return array |
||
384 | */ |
||
385 | public static function get_eu_countries() { |
||
386 | return array( |
||
387 | 'AD', |
||
388 | 'AL', |
||
389 | 'AT', |
||
390 | 'AX', |
||
391 | 'BA', |
||
392 | 'BE', |
||
393 | 'BG', |
||
394 | 'BY', |
||
395 | 'CH', |
||
396 | 'CY', |
||
397 | 'CZ', |
||
398 | 'DE', |
||
399 | 'DK', |
||
400 | 'EE', |
||
401 | 'ES', |
||
402 | 'FI', |
||
403 | 'FO', |
||
404 | 'FR', |
||
405 | 'GB', |
||
406 | 'GG', |
||
407 | 'GI', |
||
408 | 'GR', |
||
409 | 'HR', |
||
410 | 'HU', |
||
411 | 'IE', |
||
412 | 'IM', |
||
413 | 'IS', |
||
414 | 'IT', |
||
415 | 'JE', |
||
416 | 'LI', |
||
417 | 'LT', |
||
418 | 'LU', |
||
419 | 'LV', |
||
420 | 'MC', |
||
421 | 'MD', |
||
422 | 'ME', |
||
423 | 'MK', |
||
424 | 'MT', |
||
425 | 'NL', |
||
426 | 'NO', |
||
427 | 'PL', |
||
428 | 'PT', |
||
429 | 'RO', |
||
430 | 'RS', |
||
431 | 'RU', |
||
432 | 'SE', |
||
433 | 'SI', |
||
434 | 'SJ', |
||
435 | 'SK', |
||
436 | 'SM', |
||
437 | 'TR', |
||
438 | 'UA', |
||
439 | 'VA', |
||
440 | ); |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Ajax handler for grabbing the installed code status. |
||
445 | */ |
||
446 | public function get_install_errors() { |
||
449 | |||
450 | } |
||
451 | |||
452 | } |
||
453 | |||
454 | new MonsterInsights_Onboarding_Wizard(); |
||
455 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.