|
1 | 1 | <?php |
2 | 2 | /* |
3 | | - * Plugin Name: Simple Analytics Official Plugin |
4 | | - * Version: 1.2 |
| 3 | + * Plugin Name: Simple Analytics Official |
| 4 | + * Version: 1.3 |
5 | 5 | * Plugin URI: https://docs.simpleanalytics.com/install-simple-analytics-on-wordpress |
6 | 6 | * Description: Embed Simple Analytics script in your WordPress website |
7 | 7 | * Author: Simple Analytics |
|
17 | 17 | * @since 1.0.0 |
18 | 18 | */ |
19 | 19 |
|
20 | | -if ( ! defined( 'ABSPATH' ) ) exit; |
| 20 | +if (!defined('ABSPATH')) exit; |
21 | 21 |
|
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>'; |
27 | 26 | } |
28 | 27 | } |
29 | 28 |
|
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) { |
34 | 32 | echo '<noscript><img src="https://api.simpleanalytics.io/hello.gif" alt=""></noscript>'; |
35 | 33 | } |
36 | 34 | } |
37 | 35 |
|
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 | +} |
39 | 46 |
|
40 | | -add_action('wp_footer', 'inject_footer_simple_analytics', 10); |
| 47 | +add_action('init', 'simpleanalytics_init'); |
0 commit comments