Passed
Push — master ( 29aa47...a9ceae )
by Chris
09:18
created

MonsterInsights_Welcome::welcome_scripts()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 51
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 42
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 51
rs 8.9368

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Class MonsterInsights_Welcome
5
 */
6
class MonsterInsights_Welcome {
7
8
	/**
9
	 * MonsterInsights_Welcome constructor.
10
	 */
11
	public function __construct() {
12
13
		add_action( 'admin_init', array( $this, 'maybe_redirect' ), 9999 );
14
		add_action( 'admin_menu', array( $this, 'register' ) );
15
		add_action( 'admin_head', array( $this, 'hide_menu' ) );
16
17
		add_action( 'admin_enqueue_scripts', array( $this, 'welcome_scripts' ) );
18
19
	}
20
21
	/**
22
	 * Register the pages to be used for the Welcome screen.
23
	 *
24
	 * These pages will be removed from the Dashboard menu, so they will
25
	 * not actually show. Sneaky, sneaky.
26
	 *
27
	 * @since 1.0.0
28
	 */
29
	public function register() {
30
31
		// Getting started - shows after installation.
32
		add_dashboard_page(
33
			esc_html__( 'Welcome to MonsterInsights', 'google-analytics-for-wordpress' ),
34
			esc_html__( 'Welcome to MonsterInsights', 'google-analytics-for-wordpress' ),
35
			apply_filters( 'monsterinsights_welcome_cap', 'manage_options' ),
36
			'monsterinsights-getting-started',
37
			array( $this, 'welcome_screen' )
38
		);
39
	}
40
41
	/**
42
	 * Removed the dashboard pages from the admin menu.
43
	 *
44
	 * This means the pages are still available to us, but hidden.
45
	 *
46
	 * @since 1.0.0
47
	 */
48
	public function hide_menu() {
49
		remove_submenu_page( 'index.php', 'monsterinsights-getting-started' );
50
	}
51
52
53
	/**
54
	 * Check if we should do any redirect.
55
	 */
56
	public function maybe_redirect() {
57
58
		// Bail if no activation redirect.
59
		if ( ! get_transient( '_monsterinsights_activation_redirect' ) ) {
60
			return;
61
		}
62
63
		// Delete the redirect transient.
64
		delete_transient( '_monsterinsights_activation_redirect' );
65
66
		// Bail if activating from network, or bulk.
67
		if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) { // WPCS: CSRF ok, input var ok.
68
			return;
69
		}
70
71
		$upgrade = get_option( 'monsterinsights_version_upgraded_from', false );
72
		if ( apply_filters( 'monsterinsights_enable_onboarding_wizard', false === $upgrade ) ) {
73
			$redirect = admin_url( 'index.php?page=monsterinsights-getting-started' );
74
			wp_safe_redirect( $redirect );
75
			exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
76
		}
77
	}
78
79
	/**
80
	 * Scripts for loading the welcome screen Vue instance.
81
	 */
82
	public function welcome_scripts() {
83
84
		$current_screen = get_current_screen();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $current_screen is correct as get_current_screen() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
85
86
		if ( empty( $current_screen->id ) || 'dashboard_page_monsterinsights-getting-started' !== $current_screen->id ) {
87
			return;
88
		}
89
90
		global $wp_version;
91
		$version_path = monsterinsights_is_pro_version() ? 'pro' : 'lite';
92
		if ( ! defined( 'MONSTERINSIGHTS_LOCAL_WIZARD_JS_URL' ) ) {
93
			wp_enqueue_style( 'monsterinsights-vue-welcome-style-vendors', plugins_url( $version_path . '/assets/vue/css/chunk-vendors.css', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version() );
94
			wp_enqueue_style( 'monsterinsights-vue-welcome-style-common', plugins_url( $version_path . '/assets/vue/css/chunk-common.css', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version() );
95
			wp_enqueue_style( 'monsterinsights-vue-welcome-style', plugins_url( $version_path . '/assets/vue/css/wizard.css', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version() );
96
			wp_enqueue_script( 'monsterinsights-vue-welcome-vendors', plugins_url( $version_path . '/assets/vue/js/chunk-vendors.js', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version(), true );
97
			wp_enqueue_script( 'monsterinsights-vue-welcome-common', plugins_url( $version_path . '/assets/vue/js/chunk-common.js', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version(), true );
98
			wp_register_script( 'monsterinsights-vue-welcome-script', plugins_url( $version_path . '/assets/vue/js/wizard.js', MONSTERINSIGHTS_PLUGIN_FILE ), array(
99
				'monsterinsights-vue-welcome-vendors',
100
				'monsterinsights-vue-welcome-common',
101
			), monsterinsights_get_asset_version(), true );
102
		} else {
103
			wp_register_script( 'monsterinsights-vue-welcome-script', MONSTERINSIGHTS_LOCAL_WIZARD_JS_URL, array(), monsterinsights_get_asset_version(), true );
0 ignored issues
show
Bug introduced by
The constant MONSTERINSIGHTS_LOCAL_WIZARD_JS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
104
		}
105
		wp_enqueue_script( 'monsterinsights-vue-welcome-script' );
106
107
		wp_localize_script(
108
			'monsterinsights-vue-welcome-script',
109
			'monsterinsights',
110
			array(
111
				'ajax'                 => add_query_arg( 'page', 'monsterinsights-onboarding', admin_url( 'admin-ajax.php' ) ),
112
				'nonce'                => wp_create_nonce( 'mi-admin-nonce' ),
113
				'network'              => is_network_admin(),
114
				'translations'         => wp_get_jed_locale_data( 'mi-vue-app' ),
115
				'assets'               => plugins_url( $version_path . '/assets/vue', MONSTERINSIGHTS_PLUGIN_FILE ),
116
				'roles'                => monsterinsights_get_roles(),
117
				'roles_manage_options' => monsterinsights_get_manage_options_roles(),
118
				'wizard_url'           => admin_url( 'index.php?page=monsterinsights-onboarding' ),
119
				'shareasale_id'        => monsterinsights_get_shareasale_id(),
120
				'shareasale_url'       => monsterinsights_get_shareasale_url( monsterinsights_get_shareasale_id(), '' ),
121
				// Used to add notices for future deprecations.
122
				'versions'             => array(
123
					'php_version'          => phpversion(),
124
					'php_version_below_54' => apply_filters( 'monsterinsights_temporarily_hide_php_52_and_53_upgrade_warnings', version_compare( phpversion(), '5.4', '<' ) ),
125
					'php_version_below_56' => apply_filters( 'monsterinsights_temporarily_hide_php_54_and_55_upgrade_warnings', version_compare( phpversion(), '5.6', '<' ) ),
126
					'php_update_link'      => monsterinsights_get_url( 'settings-notice', 'settings-page', 'https://www.monsterinsights.com/docs/update-php/' ),
127
					'wp_version'           => $wp_version,
128
					'wp_version_below_46'  => version_compare( $wp_version, '4.6', '<' ),
129
					'wp_version_below_49'  => version_compare( $wp_version, '4.9', '<' ),
130
					'wp_update_link'       => monsterinsights_get_url( 'settings-notice', 'settings-page', 'https://www.monsterinsights.com/docs/update-wordpress/' ),
131
				),
132
				'plugin_version'       => MONSTERINSIGHTS_VERSION,
133
			)
134
		);
135
	}
136
137
	/**
138
	 * Load the welcome screen content.
139
	 */
140
	public function welcome_screen() {
141
		do_action( 'monsterinsights_head' );
142
143
		monsterinsights_settings_error_page( 'monsterinsights-welcome' );
144
		monsterinsights_settings_inline_js();
145
	}
146
}
147
148
new MonsterInsights_Welcome();
149