Passed
Push — master ( a722cd...843c92 )
by Chris
03:45
created

monsterinsights_frontend_admin_bar_scripts()   C

Complexity

Conditions 12
Paths 33

Size

Total Lines 33
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 26
nc 33
nop 0
dl 0
loc 33
rs 6.9666
c 0
b 0
f 0

How to fix   Complexity   

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
 * Frontend events tracking.
4
 *
5
 * @since 6.0.0
6
 *
7
 * @package MonsterInsights
8
 * @author  Chris Christoff
9
 */
10
11
// Exit if accessed directly
12
if ( ! defined( 'ABSPATH' ) ) {
13
    exit;
14
}
15
16
17
/**
18
 * Get frontend tracking options.
19
 *
20
 * This function is used to return an array of parameters
21
 * for the frontend_output() function to output. These are
22
 * generally dimensions and turned on GA features.
23
 *
24
 * @since 7.0.0
25
 * @access public
26
 *
27
 * @return array Array of the options to use.
28
 */
29
function monsterinsights_tracking_script( ) {
30
    require_once plugin_dir_path( MONSTERINSIGHTS_PLUGIN_FILE ) . 'includes/frontend/class-tracking-abstract.php';
31
32
    $mode = is_preview() ? 'preview' : 'analytics';
33
34
    do_action( 'monsterinsights_tracking_before_' . $mode );
35
    do_action( 'monsterinsights_tracking_before', $mode );
36
    if ( $mode === 'preview' ) {
37
        require_once plugin_dir_path( MONSTERINSIGHTS_PLUGIN_FILE ) . 'includes/frontend/tracking/class-tracking-preview.php';
38
        $tracking = new MonsterInsights_Tracking_Preview();
39
        echo $tracking->frontend_output();
40
    } else {
41
         require_once plugin_dir_path( MONSTERINSIGHTS_PLUGIN_FILE ) . 'includes/frontend/tracking/class-tracking-analytics.php';
42
         $tracking = new MonsterInsights_Tracking_Analytics();
43
         echo $tracking->frontend_output();
44
    }
45
46
    do_action( 'monsterinsights_tracking_after_' . $mode );
47
    do_action( 'monsterinsights_tracking_after', $mode );
48
}
49
add_action( 'wp_head', 'monsterinsights_tracking_script', 6 );
50
//add_action( 'login_head', 'monsterinsights_tracking_script', 6 );
51
52
/**
53
 * Get frontend tracking options.
54
 *
55
 * This function is used to return an array of parameters
56
 * for the frontend_output() function to output. These are
57
 * generally dimensions and turned on GA features.
58
 *
59
 * @since 6.0.0
60
 * @access public
61
 *
62
 * @return array Array of the options to use.
63
 */
64
function monsterinsights_events_tracking( ) {
65
    $track_user    = monsterinsights_track_user();
66
67
    if ( $track_user ) {
68
        require_once plugin_dir_path( MONSTERINSIGHTS_PLUGIN_FILE ) . 'includes/frontend/events/class-analytics-events.php';
69
        new MonsterInsights_Analytics_Events();
70
    } else {
71
        // User is in the disabled group or events mode is off
72
    }
73
}
74
add_action( 'template_redirect', 'monsterinsights_events_tracking', 9 );
75
76
/**
77
 * Add the UTM source parameters in the RSS feeds to track traffic.
78
 *
79
 * @since 6.0.0
80
 * @access public
81
 *
82
 * @param string $guid The link for the RSS feed.
83
 *
84
 * @return string The new link for the RSS feed.
85
 */
86
function monsterinsights_rss_link_tagger( $guid ) {
87
    global $post;
88
89
    if ( monsterinsights_get_option( 'tag_links_in_rss', false ) ){
90
        if ( is_feed() ) {
91
            if ( monsterinsights_get_option( 'allow_anchor', false ) ) {
92
                $delimiter = '#';
93
            } else {
94
                $delimiter = '?';
95
                if ( strpos( $guid, $delimiter ) > 0 ) {
96
                    $delimiter = '&amp;';
97
                }
98
            }
99
            return $guid . $delimiter . 'utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=' . urlencode( $post->post_name );
100
        }
101
    }
102
    return $guid;
103
}
104
add_filter( 'the_permalink_rss', 'monsterinsights_rss_link_tagger', 99 );
105
106
/**
107
 * Add an admin bar menu item on the frontend.
108
 *
109
 * @since 7.5.0
110
 *
111
 * @return void
112
 */
113
function monsterinsights_add_admin_bar_menu() {
114
	if ( is_admin() || monsterinsights_get_option( 'hide_admin_bar_reports' ) ) {
115
		return;
116
	}
117
118
	global $wp_admin_bar;
119
120
	$args = array(
121
		'id'    => 'monsterinsights_frontend_button',
122
		'title' => '<span class="ab-icon dashicons-before dashicons-chart-bar"></span> Insights', // Maybe allow translation?
123
		'href'  => '#',
124
	);
125
126
	if ( method_exists( $wp_admin_bar, 'add_menu' ) ) {
127
		$wp_admin_bar->add_menu( $args );
128
	}
129
}
130
131
//add_action( 'admin_bar_menu', 'monsterinsights_add_admin_bar_menu', 999 );
132
133
/**
134
 * Load the scripts needed for the admin bar.
135
 *
136
 * @since 7.5.0
137
 *
138
 * @return void
139
 */
140
function monsterinsights_frontend_admin_bar_scripts() {
141
	if ( ! is_admin_bar_showing() || monsterinsights_get_option( 'hide_admin_bar_reports' ) ) {
142
		return;
143
	}
144
145
	$version_path    = monsterinsights_is_pro_version() ? 'pro' : 'lite';
146
	$rtl             = is_rtl() ? '.rtl' : '';
147
	$frontend_js_url = defined( 'MONSTERINSIGHTS_LOCAL_FRONTEND_JS_URL' ) && MONSTERINSIGHTS_LOCAL_FRONTEND_JS_URL ? MONSTERINSIGHTS_LOCAL_FRONTEND_JS_URL : plugins_url( $version_path . '/assets/vue/js/frontend.js', MONSTERINSIGHTS_PLUGIN_FILE );
0 ignored issues
show
Bug introduced by
The constant MONSTERINSIGHTS_LOCAL_FRONTEND_JS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
148
149
	if ( ! defined( 'MONSTERINSIGHTS_LOCAL_FRONTEND_JS_URL' ) ) {
150
		wp_enqueue_style( 'monsterinsights-vue-frontend-style', plugins_url( $version_path . '/assets/vue/css/frontend' . $rtl . '.css', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version() );
151
		wp_enqueue_script( 'monsterinsights-vue-vendors', plugins_url( $version_path . '/assets/vue/js/chunk-vendors.js', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version(), true );
152
		wp_enqueue_script( 'monsterinsights-vue-common', plugins_url( $version_path . '/assets/vue/js/chunk-common.js', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version(), true );
153
	}
154
155
	wp_register_script( 'monsterinsights-vue-frontend', $frontend_js_url, array(), monsterinsights_get_asset_version(), true );
156
	wp_enqueue_script( 'monsterinsights-vue-frontend' );
157
158
	wp_localize_script(
159
		'monsterinsights-vue-frontend',
160
		'monsterinsights',
161
		array(
162
			'ajax'           => admin_url( 'admin-ajax.php' ),
163
			'nonce'          => wp_create_nonce( 'mi-admin-nonce' ),
164
			'network'        => is_network_admin(),
165
			'translations'   => wp_get_jed_locale_data( monsterinsights_is_pro_version() ? 'ga-premium' : 'google-analytics-for-wordpress' ),
166
			'assets'         => plugins_url( $version_path . '/assets/vue', MONSTERINSIGHTS_PLUGIN_FILE ),
167
			'addons_url'     => is_multisite() ? network_admin_url( 'admin.php?page=monsterinsights_network#/addons' ) : admin_url( 'admin.php?page=monsterinsights_settings#/addons' ),
168
			'page_id'        => is_singular() ? get_the_ID() : false,
169
			'page_title'     => is_singular() ? get_the_title() : false,
170
			'plugin_version' => MONSTERINSIGHTS_VERSION,
171
			'shareasale_id'  => monsterinsights_get_shareasale_id(),
172
			'shareasale_url' => monsterinsights_get_shareasale_url( monsterinsights_get_shareasale_id(), '' ),
173
		)
174
	);
175
}
176
177
//add_action( 'wp_enqueue_scripts', 'monsterinsights_frontend_admin_bar_scripts' );
178