Skip to content

Commit 7c788e5

Browse files
committed
Use wp_enqueue_script, init, and global $is_running var
1 parent 9c8b7e8 commit 7c788e5

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

simple-analytics.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/*
3-
* Plugin Name: Simple Analytics Official Plugin
4-
* Version: 1.2
3+
* Plugin Name: Simple Analytics Official
4+
* Version: 1.3
55
* Plugin URI: https://docs.simpleanalytics.com/install-simple-analytics-on-wordpress
66
* Description: Embed Simple Analytics script in your WordPress website
77
* Author: Simple Analytics
@@ -17,24 +17,31 @@
1717
* @since 1.0.0
1818
*/
1919

20-
if ( ! defined( 'ABSPATH' ) ) exit;
20+
if (!defined('ABSPATH')) exit;
2121

22-
function inject_header_simple_analytics() {
23-
if (current_user_can('editor') || current_user_can('administrator')) {
24-
echo '<script>console.warn(\'Simple Analytics: Not logging requests from admins.\')</script>';
25-
} else {
26-
echo '<script async src="https://cdn.simpleanalytics.io/hello.js"></script>';
22+
function simpleanalytics_warn_not_logging() {
23+
global $is_running;
24+
if (!$is_running) {
25+
echo '<script>console.warn(\'Simple Analytics: Not logging requests from admins\')</script>';
2726
}
2827
}
2928

30-
function inject_footer_simple_analytics() {
31-
if (current_user_can('editor') || current_user_can('administrator')) {
32-
// Nothing...
33-
} else {
29+
function simpleanalytics_inject_footer() {
30+
global $is_running;
31+
if ($is_running) {
3432
echo '<noscript><img src="https://api.simpleanalytics.io/hello.gif" alt=""></noscript>';
3533
}
3634
}
3735

38-
add_action('wp_head', 'inject_header_simple_analytics', 10);
36+
function simpleanalytics_init() {
37+
global $is_running;
38+
$is_running = !current_user_can('editor') && !current_user_can('administrator');
39+
if ($is_running) {
40+
wp_enqueue_script('simpleanalytics_script', 'https://cdn.simpleanalytics.io/hello.js', array(), null, true);
41+
add_action('wp_footer', 'simpleanalytics_inject_footer', 10);
42+
} else {
43+
add_action('wp_head', 'simpleanalytics_warn_not_logging', 10);
44+
}
45+
}
3946

40-
add_action('wp_footer', 'inject_footer_simple_analytics', 10);
47+
add_action('init', 'simpleanalytics_init');

0 commit comments

Comments
 (0)