Google Analytics to your WordPress site. Unlike other plugins, code is added to the <head> section, so you can authorize your site in Google Webmaster Tools. Author: Lukasz Nowicki Version: 0.5.5 License: GPLv2 */ class AnalyticsHead { // Basic informations protected $PluginName='Analytics Head'; protected $PluginCode='ln-plugin-analytics-head'; protected $PluginVersion='0.5.5'; protected $PluginURL='http://lukasznowicki.info/analytics-head/'; protected $PluginFile='analytics_head.php'; private $OptionsName='wordpress_lnpo_ah'; private $Defaults=Array('GoogleID'=>'','HideForAdmins'=>true); private $Options=Array(); private $DirectCall=''; private $PluginUndercode=''; private $Footer=''; private $TD=''; public function __construct() { // Prepare die string for direct calls $this->DirectCall='

'.$this->PluginName.'

'.$this->PluginVersion.'

Hello, there!

Please, do not call me directly, because there is nothing I can do here.

I\'m just a plugin, so you should call me within Wordpress installation.

Learn more about '.$this->PluginName.' plugin.

'; // Die if plugin is called directly if (!function_exists('add_action')) die($this->DirectCall); // Prepare Plugin Underscore code-name $this->PluginUndercode=str_replace('-','_',$this->PluginCode); $this->TD=$this->PluginCode; // Footer information $this->Footer=__('This plugin is distributed under the terms of GPLv2. For more information see',$this->TD).': '.$this->PluginName.''; // Register hooks for plugin services register_activation_hook(__FILE__,Array($this,'plugin_activate')); register_deactivation_hook(__FILE__,Array($this,'plugin_deactivate')); // Add "settings" link to the plugin in the plugin list add_filter('plugin_action_links',Array($this,'action_links'),10,2); // Load options into $Options variable $this->serve_variables(); // Throw warning if user didn't provided Google ID if (((!isset($_POST['gah_submit']))&&($this->Options['GoogleID']==''))||((isset($_POST['gah_submit']))&&($_POST['google_id']==''))) add_action('admin_notices',Array($this,'warning')); // Adding admin menu add_action('admin_menu',Array($this,'menu')); // Add analytics code if it is possible and needed You should do it after functions will be loaded. add_action('after_setup_theme',array($this,'check_add_code')); } public function setup() { } public function add_code() { echo " "; } public function check_add_code() { $id=preg_match('([U][A][-][0-9]{1,16}[-][0-9]{1,4})',$this->Options['GoogleID'],$result); if ($id>0) $id=$result[0]; else $id=''; $a=$this->Options['HideForAdmins']; if ($id!='') if ((!is_admin())&&(($a==false)||(($a==true)&&(!current_user_can('manage_options'))))) add_action('wp_head',Array($this,'add_code')); } public function options() { // Check if user is saving options $this->submit_check(); // Show options page $this->options_body(); } public function menu() { add_options_page( __('Analytics Head plugin options',$this->TD), $this->PluginName, 'manage_options', $this->PluginCode, Array($this,'options') ); } public function warning() { echo '

'.__('Plugin is almost ready for operation. You need to enter in the tracking code ID:',$this->TD).' '.$this->PluginName.'

'; } public function plugin_activate() { // Delete old version's options, maybe it extists somewhere // It is extremely impossible but oh well. // 0.4.1 delete_option('wordpress_ln_p_ah'); // 0.3 delete_option('wordpress_ln_p_ah_ga_id'); delete_option('wordpress_ln_p_ah_add_for_admins'); // 0.2 and 0.1 delete_option('wordpress_ln_gah_ad'); delete_option('wordpress_ln_gah_id'); } public function plugin_deactivate() { // Do nothing now. Do not delete old settings because user // may want to turn on the plugin again. } public function action_links($links,$file) { if ($file==plugin_basename(dirname(__FILE__).'/'.$this->PluginFile)) $links[]=''.__('Settings').''; return $links; } private function serve_variables() { // Get options from database $this->Options=@unserialize(get_option($this->OptionsName)); if (!is_array($this->Options)) $this->Options=Array(); // Make sure all options are set and provide default // values if needed $this->Options=array_merge($this->Defaults,$this->Options); update_option($this->OptionsName,serialize($this->Options)); // Now we've got fresh options in wp database // and in $Options variable. } private function submit_check() { if (isset($_POST['gah_submit'])) { // Check if user is logged admin if (function_exists('current_user_can')&&!current_user_can('manage_options')) die(__('Cheatin’ uh?')); // Pre-check Google ID $id=preg_match('([U][A][-][0-9]{1,16}[-][0-9]{1,4})',$_POST['google_id'],$result); if ($id>0) $id=$result[0]; else $id=''; // Pre-check Hide For Admin option if (isset($_POST['hide_for_admin'])&&($_POST['hide_for_admin']=='on')) $adm=true; else $adm=false; $this->Options['GoogleID']=$id; $this->Options['HideForAdmins']=$adm; // Update newly provided options update_option($this->OptionsName,serialize($this->Options)); echo '

'.__('Options saved.').'

'; } } private function options_body() { global $title; // Is Hide For Admins option checked? if ($this->Options['HideForAdmins']) $chk=' checked="checked"'; else $chk=''; // Show options panel echo '

'.$title.'

'.__('This plugin has only two settings. Google Analytics code and the option of hiding the code for users logged on with administrator privileges. This will not count your visit that does not distort the results in the service.',$this->TD).'

'.__('Google Analytics ID',$this->TD).'
'.__('Google Analytics ID',$this->TD).'
'.__('Hide for administrators',$this->TD).'
'.__('Hide for administrators',$this->TD).'

'.__('',$this->TD).'

'.$this->Footer.'

'; } } add_action( 'init', 'AH_start_plugin' ); function AH_start_plugin() { load_plugin_textdomain( 'ln-plugin-analytics-head', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); $AH = new AnalyticsHead(); } // End of file, thank you for watching ;) // Lukasz Nowicki jagoo@post.pl