| 1 |
<?php |
| 2 |
|
| 3 |
namespace Phylax\Plugin; |
| 4 |
|
| 5 |
( defined('ABSPATH') && defined( __NAMESPACE__ . '\PLUGIN_DIR' ) ) or die('Run me within WordPress environment.'); |
| 6 |
|
| 7 |
class Plugin { |
| 8 |
|
| 9 |
public $options; |
| 10 |
|
| 11 |
public function __construct() { |
| 12 |
$this->options = Config::get_options(); |
| 13 |
add_action( 'init', array( $this, 'init' ) ); |
| 14 |
if ( is_admin() ) { |
| 15 |
# add 'Settings' link to the plugin list (applicable only when in admin area) |
| 16 |
add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 2 ); |
| 17 |
# see, if user provided Google tracking code... add warning if applicable |
| 18 |
if ( !isset( $this->options['GoogleID'] ) || ( $this->options['GoogleID'] == '' ) ) { |
| 19 |
add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 20 |
} |
| 21 |
} |
| 22 |
# now serve the google code if user provided any |
| 23 |
if ( $this->options['GoogleID'] != '' ) { |
| 24 |
add_action( 'wp_loaded', array( $this, 'wp_loaded' ) ); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
public function init() { |
| 29 |
load_plugin_textdomain( Config::TEXT_DOMAIN, false, PLUGIN_BASE ); |
| 30 |
} |
| 31 |
|
| 32 |
public function wp_loaded() { |
| 33 |
global $current_user; |
| 34 |
# first of all, we may assume that we've got proper Google tracking ID provided. |
| 35 |
# let's see if we should attach it to the page and where |
| 36 |
# user is in admin area and we need to hide google code in admin area |
| 37 |
if ( is_admin() && ( $this->options['HideInAdmin'] == '1' ) ) { return; } |
| 38 |
# user is logged-in (we can determine user's ID) and we need to hide if for logged users |
| 39 |
if ( ( $current_user->ID != 0 ) && ( $this->options['HideForLogged'] == '1' ) ) { return; } |
| 40 |
# ok, we may show the google code... let's find out where... |
| 41 |
if ( is_admin() ) { |
| 42 |
if ( $this->options['IntoFooter'] == '1' ) { |
| 43 |
# in footer |
| 44 |
add_action( 'admin_footer', array( $this, 'admin_footer' ), 99999 ); |
| 45 |
} else { |
| 46 |
# in <head> |
| 47 |
add_action( 'admin_head', array( $this, 'admin_head' ), 99999 ); |
| 48 |
} |
| 49 |
} else { |
| 50 |
if ( $this->options['IntoFooter'] == '1' ) { |
| 51 |
# in footer |
| 52 |
add_action( 'wp_footer', array( $this, 'wp_footer' ), 99999 ); |
| 53 |
} else { |
| 54 |
# in <head> |
| 55 |
add_action( 'wp_head', array( $this, 'wp_head' ), 99999 ); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public function throw_google_code() { |
| 61 |
echo "<!-- " . __( 'Added by Google Analytics Head plugin', Config::TEXT_DOMAIN ).': ' . Config::PLUGIN_URI . " --> |
| 62 |
<!-- Google Analytics --> |
| 63 |
<script> |
| 64 |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ |
| 65 |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), |
| 66 |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) |
| 67 |
})(window,document,'script','//www.google-analytics.com/analytics.js','ga'); |
| 68 |
|
| 69 |
ga('create', '" . $this->options['GoogleID'] . "', 'auto'); |
| 70 |
ga('send', 'pageview'); |
| 71 |
|
| 72 |
</script> |
| 73 |
<!-- End Google Analytics --> |
| 74 |
"; |
| 75 |
} |
| 76 |
|
| 77 |
public function wp_footer() { |
| 78 |
$this->throw_google_code(); |
| 79 |
} |
| 80 |
|
| 81 |
public function wp_head() { |
| 82 |
$this->throw_google_code(); |
| 83 |
} |
| 84 |
|
| 85 |
public function admin_footer() { |
| 86 |
$this->throw_google_code(); |
| 87 |
} |
| 88 |
|
| 89 |
public function admin_head() { |
| 90 |
$this->throw_google_code(); |
| 91 |
} |
| 92 |
|
| 93 |
public function admin_notices() { |
| 94 |
global $pagenow, $current_user; |
| 95 |
if ( ( $pagenow == 'index.php' ) && ( !get_user_meta( $current_user->ID, Config::IGNORE_NOTICE ) ) ) { |
| 96 |
echo '<div class="updated"><p>'; |
| 97 |
printf( __( 'Please remember to <a href="%s">setup Google Tracking ID</a> in order to use Google Analytics Head plugin.', Config::TEXT_DOMAIN ), 'options-general.php?page=' . Config::OPTION_SLUG ); |
| 98 |
echo '</p></div>'; |
| 99 |
} |
| 100 |
// <div class="update-nag"> |
| 101 |
} |
| 102 |
|
| 103 |
public function plugin_action_links( $links, $file ) { |
| 104 |
if ( $file == Config::ACTION_LINK ) { |
| 105 |
$links[]='<a href="options-general.php?page=' . Config::OPTION_SLUG . '">' . __( 'Settings', Config::TEXT_DOMAIN ) . '</a>'; |
| 106 |
} |
| 107 |
return $links; |
| 108 |
} |
| 109 |
|
| 110 |
private function strings() { |
| 111 |
_e( 'This plugin adds tracking code for <strong>Google Analytics</strong> to your WordPress site. Unlike other plugins, code is added to the <i><head></i> section, so you can authorize your site in <strong>Google Webmaster Tools</strong>. Of course you can also move your tracking code into footer.', Config::TEXT_DOMAIN ); |
| 112 |
_e( 'Lukasz Nowicki', Config::TEXT_DOMAIN ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
new Plugin(); |