Passed
Push — master ( aa437c...805644 )
by Chris
08:50
created

monsterinsights_dismiss_tracking_notice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
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
/**
108
 * Checks used for loading the frontend scripts/admin bar button.
109
 */
110
function monsterinsights_prevent_loading_frontend_reports() {
111
	return ! current_user_can( 'monsterinsights_view_dashboard' ) || monsterinsights_get_option( 'hide_admin_bar_reports' ) || function_exists( 'monsterinsights_is_reports_page' ) && monsterinsights_is_reports_page();
112
}
113
114
/**
115
 * Add an admin bar menu item on the frontend.
116
 *
117
 * @since 7.5.0
118
 *
119
 * @return void
120
 */
121
function monsterinsights_add_admin_bar_menu() {
122
	if ( monsterinsights_prevent_loading_frontend_reports() ) {
123
		return;
124
	}
125
126
	global $wp_admin_bar;
127
128
	$args = array(
129
		'id'    => 'monsterinsights_frontend_button',
130
		'title' => '<span class="ab-icon dashicons-before dashicons-chart-bar"></span> Insights', // Maybe allow translation?
131
		'href'  => '#',
132
	);
133
134
	if ( method_exists( $wp_admin_bar, 'add_menu' ) ) {
135
		$wp_admin_bar->add_menu( $args );
136
	}
137
}
138
139
add_action( 'admin_bar_menu', 'monsterinsights_add_admin_bar_menu', 999 );
140
141
/**
142
 * Load the scripts needed for the admin bar.
143
 *
144
 * @since 7.5.0
145
 *
146
 * @return void
147
 */
148
function monsterinsights_frontend_admin_bar_scripts() {
149
	if ( monsterinsights_prevent_loading_frontend_reports() ) {
150
		return;
151
	}
152
153
	$version_path    = monsterinsights_is_pro_version() ? 'pro' : 'lite';
154
	$rtl             = is_rtl() ? '.rtl' : '';
155
	$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...
156
157
	if ( ! defined( 'MONSTERINSIGHTS_LOCAL_FRONTEND_JS_URL' ) ) {
158
		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() );
159
		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 );
160
		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 );
161
	}
162
163
	wp_register_script( 'monsterinsights-vue-frontend', $frontend_js_url, array(), monsterinsights_get_asset_version(), true );
164
	wp_enqueue_script( 'monsterinsights-vue-frontend' );
165
166
	$page_title = is_singular() ? get_the_title() : monsterinsights_get_page_title();
167
	// We do not have a current auth.
168
	$site_auth = MonsterInsights()->auth->get_viewname();
0 ignored issues
show
Bug Best Practice introduced by
The property $auth is declared protected in MonsterInsights_Lite. Since you implement __get, consider adding a @property or @property-read.
Loading history...
169
	$ms_auth   = is_multisite() && MonsterInsights()->auth->get_network_viewname();
170
171
	// Check if any of the other admin scripts are enqueued, if so, use their object.
172
	if ( ! wp_script_is( 'monsterinsights-vue-script' ) && ! wp_script_is( 'monsterinsights-vue-reports' ) && ! wp_script_is( 'monsterinsights-vue-widget' ) ) {
173
		wp_localize_script(
174
			'monsterinsights-vue-frontend',
175
			'monsterinsights',
176
			array(
177
				'ajax'                => admin_url( 'admin-ajax.php' ),
178
				'nonce'               => wp_create_nonce( 'mi-admin-nonce' ),
179
				'network'             => is_network_admin(),
180
				'translations'        => wp_get_jed_locale_data( monsterinsights_is_pro_version() ? 'ga-premium' : 'google-analytics-for-wordpress' ),
181
				'assets'              => plugins_url( $version_path . '/assets/vue', MONSTERINSIGHTS_PLUGIN_FILE ),
182
				'addons_url'          => is_multisite() ? network_admin_url( 'admin.php?page=monsterinsights_network#/addons' ) : admin_url( 'admin.php?page=monsterinsights_settings#/addons' ),
183
				'page_id'             => is_singular() ? get_the_ID() : false,
184
				'page_title'          => $page_title,
185
				'plugin_version'      => MONSTERINSIGHTS_VERSION,
186
				'shareasale_id'       => monsterinsights_get_shareasale_id(),
187
				'shareasale_url'      => monsterinsights_get_shareasale_url( monsterinsights_get_shareasale_id(), '' ),
188
				'is_admin'            => is_admin(),
189
				'reports_url'         => add_query_arg( 'page', 'monsterinsights_reports', admin_url( 'admin.php' ) ),
190
				'authed'              => $site_auth || $ms_auth,
191
				'getting_started_url' => is_multisite() ? network_admin_url( 'admin.php?page=monsterinsights_network#/about/getting-started' ) : admin_url( 'admin.php?page=monsterinsights_settings#/about/getting-started' ),
192
				'wizard_url'          => admin_url( 'index.php?page=monsterinsights-onboarding' ),
193
			)
194
		);
195
	}
196
}
197
198
add_action( 'wp_enqueue_scripts', 'monsterinsights_frontend_admin_bar_scripts' );
199
add_action( 'admin_enqueue_scripts', 'monsterinsights_frontend_admin_bar_scripts', 1005 );
200
201
202
/**
203
 * Load the tracking notice for logged in users.
204
 */
205
function monsterinsights_administrator_tracking_notice() {
206
	// Don't do anything for guests.
207
	if ( ! is_user_logged_in() ) {
208
		return;
209
	}
210
211
	// Only show this for users who can see the settings panel.
212
	if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
213
		return;
214
	}
215
216
	// Only show when tracking.
217
	$ua = monsterinsights_get_ua();
218
	if ( empty( $ua ) ) {
219
		return;
220
	}
221
222
	// Don't show if already dismissed.
223
	if ( get_option( 'monsterinsights_frontend_tracking_notice_viewed', false ) ) {
224
		return;
225
	}
226
227
	?>
228
	<div class="monsterinsights-tracking-notice monsterinsights-tracking-notice-hide">
229
		<div class="monsterinsights-tracking-notice-icon">
230
			<img src="<?php echo esc_url( plugins_url( 'assets/images/mascot.png', MONSTERINSIGHTS_PLUGIN_FILE ) ); ?>" width="40" alt="MonsterInsights Mascot" />
231
		</div>
232
		<div class="monsterinsights-tracking-notice-text">
233
			<h3><?php esc_html_e( 'Tracking is Disabled for Administrators', 'ga-premium' ); ?></h3>
234
			<p>
235
				<?php
236
				$doc_url = 'https://monsterinsights.com/docs/tracking-disabled-administrators-editors';
237
				$doc_url = add_query_arg( array(
238
					'utm_source'   => monsterinsights_is_pro_version() ? 'proplugin' : 'liteplugin',
239
					'utm_medium'   => 'frontend-notice',
240
					'utm_campaign' => 'admin-tracking-doc',
241
				), $doc_url );
242
				// Translators: %s is the link to the article where more details about tracking are listed.
243
				printf( esc_html__( 'To keep stats accurate, we do not load Google Analytics scripts for admin users. %1$sLearn More &raquo;%2$s', 'ga-premium' ), '<a href="' . esc_url( $doc_url ) . '" target="_blank">', '</a>' );
244
				?>
245
			</p>
246
		</div>
247
		<div class="monsterinsights-tracking-notice-close">&times;</div>
248
	</div>
249
	<style type="text/css">
250
		.monsterinsights-tracking-notice {
251
			position: fixed;
252
			bottom: 20px;
253
			right: 15px;
254
			font-family: Arial, Helvetica, "Trebuchet MS", sans-serif;
255
			background: #fff;
256
			box-shadow: 0 0 10px 0 #dedede;
257
			padding: 6px 5px;
258
			display: flex;
259
			align-items: center;
260
			justify-content: center;
261
			width: 380px;
262
			max-width: calc( 100% - 30px );
263
			border-radius: 6px;
264
			transition: bottom 700ms ease;
265
			z-index: 10000;
266
		}
267
268
		.monsterinsights-tracking-notice h3 {
269
			font-size: 13px;
270
			color: #222;
271
			font-weight: 700;
272
			margin: 0 0 8px;
273
			padding: 0;
274
			line-height: 1;
275
			border: none;
276
		}
277
278
		.monsterinsights-tracking-notice p {
279
			font-size: 13px;
280
			color: #7f7f7f;
281
			font-weight: 400;
282
			margin: 0;
283
			padding: 0;
284
			line-height: 1.2;
285
			border: none;
286
		}
287
288
		.monsterinsights-tracking-notice p a {
289
			color: #7f7f7f;
290
			font-size: 13px;
291
			line-height: 1.2;
292
			margin: 0;
293
			padding: 0;
294
			text-decoration: underline;
295
			font-weight: 400;
296
		}
297
298
		.monsterinsights-tracking-notice p a:hover {
299
			color: #7f7f7f;
300
			text-decoration: none;
301
		}
302
303
		.monsterinsights-tracking-notice-icon img {
304
			height: auto;
305
			display: block;
306
			margin: 0;
307
		}
308
309
		.monsterinsights-tracking-notice-icon {
310
			padding: 14px;
311
			background-color: #f2f6ff;
312
			border-radius: 6px;
313
			flex-grow: 0;
314
			flex-shrink: 0;
315
			margin-right: 12px;
316
		}
317
318
		.monsterinsights-tracking-notice-close {
319
			padding: 0;
320
			margin: 0 3px 0 0;
321
			border: none;
322
			box-shadow: none;
323
			border-radius: 0;
324
			color: #7f7f7f;
325
			background: transparent;
326
			line-height: 1;
327
			align-self: flex-start;
328
			cursor: pointer;
329
			font-weight: 400;
330
		}
331
332
		.monsterinsights-tracking-notice.monsterinsights-tracking-notice-hide {
333
			bottom: -200px;
334
		}
335
	</style>
336
	<?php
337
338
	if ( ! wp_script_is( 'jquery', 'queue' ) ) {
339
		wp_enqueue_script( 'jquery' );
340
	}
341
	?>
342
	<script>
343
		if ( 'undefined' !== typeof jQuery ) {
344
			jQuery( document ).ready( function ( $ ) {
345
				/* Don't show the notice if we don't have a way to hide it (no js, no jQuery). */
346
				$( document.querySelector( '.monsterinsights-tracking-notice' ) ).removeClass( 'monsterinsights-tracking-notice-hide' );
347
				$( document.querySelector( '.monsterinsights-tracking-notice-close' ) ).on( 'click', function ( e ) {
348
					e.preventDefault();
349
					$( this ).closest( '.monsterinsights-tracking-notice' ).addClass( 'monsterinsights-tracking-notice-hide' );
350
					$.ajax( {
351
						url: '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>',
352
						method: 'POST',
353
						data: {
354
							action: 'monsterinsights_dismiss_tracking_notice',
355
							nonce: '<?php echo esc_js( wp_create_nonce( 'monsterinsights-tracking-notice' ) ); ?>',
356
						}
357
					} );
358
				} );
359
			} );
360
		}
361
	</script>
362
	<?php
363
}
364
365
add_action( 'wp_footer', 'monsterinsights_administrator_tracking_notice', 300 );
366
367
/**
368
 * Ajax handler to hide the tracking notice.
369
 */
370
function monsterinsights_dismiss_tracking_notice() {
371
372
	check_ajax_referer( 'monsterinsights-tracking-notice', 'nonce' );
373
374
	update_option( 'monsterinsights_frontend_tracking_notice_viewed', 1 );
375
376
	wp_die();
377
378
}
379
380
add_action( 'wp_ajax_monsterinsights_dismiss_tracking_notice', 'monsterinsights_dismiss_tracking_notice' );
381