<?php
/**
 * Plugin Name:         MonsterInsights Pro
 * Plugin URI:          https://www.monsterinsights.com/?utm_source=wordpress&utm_medium=plugin&utm_campaign=wpmipluginpro&utm_content=v600
 * Description:         The best Google Analytics plugin for WordPress. See how visitors find and use your website, so you can keep them coming back. Get stats that matter.
 * Author:              MonsterInsights
 * Author URI:          https://www.monsterinsights.com/
 *
 * Version:             6.2.4
 * Requires at least:   3.9.0
 * Tested up to:        4.8.1
 *
 * License:             GPL v3
 * 
 * Text Domain:         ga-premium
 * Domain Path:         /pro/languages
 *
 * MonsterInsights Pro
 * Copyright (C) 2008-2017, MonsterInsights, support@monsterinsights.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category            Plugin
 * @copyright           Copyright © 2017 Chris Christoff
 * @author              Chris Christoff
 * @package             MonsterInsights
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Main plugin class.
 *
 * @since 6.0.0
 *
 * @package MonsterInsights
 * @author  Chris Christoff
 */
final class MonsterInsights {

	/**
	 * Holds the class object.
	 *
	 * @since 6.0.0
	 * @access public
	 * @var object Instance of instantiated MonsterInsights class.
	 */
	public static $instance;

	/**
	 * Plugin version, used for cache-busting of style and script file references.
	 *
	 * @since 6.0.0
	 * @access public
	 * @var string $version Plugin version.
	 */
	public $version = '6.2.4';

	/**
	 * The name of the plugin.
	 *
	 * @since 6.0.0
	 * @access public
	 * @var string $plugin_name Plugin name.
	 */
	public $plugin_name = 'MonsterInsights Pro';

	/**
	 * Unique plugin slug identifier.
	 *
	 * @since 6.0.0
	 * @access public
	 * @var string $plugin_slug Plugin slug.
	 */
	public $plugin_slug = 'pro';

	/**
	 * Plugin file.
	 *
	 * @since 6.0.0
	 * @access public
	 * @var string $file PHP File constant for main file.
	 */
	public $file = __FILE__;

	/**
	 * Holds instance of MonsterInsights Admin Notice class.
	 *
	 * @since 6.0.0
	 * @access public
	 * @var MonsterInsights_Admin_Notice $notices Instance of Admin Notice class.
	 */
	public $notices;

	/**
	 * Holds instance of MonsterInsights License class.
	 *
	 * @since 6.0.0
	 * @access public
	 * @var MonsterInsights_License $license Instance of License class.
	 */
	public $license;

	/**
	 * Holds instance of MonsterInsights GA class.
	 *
	 * @since 6.0.0
	 * @access public
	 * @var MonsterInsights_GA $ga Instance of GA class.
	 */
	protected $ga;
	
	/**
	 * Primary class constructor.
	 *
	 * @since 6.0.0
	 * @access public
	 */
	public function __construct() {
		// We don't use this
	}

	/**
	 * Returns the singleton instance of the class.
	 *
	 * @access public
	 * @since 6.0.0
	 *
	 * @return object The MonsterInsights object.
	 */
	public static function get_instance() {

		if ( ! isset( self::$instance ) && ! ( self::$instance instanceof MonsterInsights ) ) {
			self::$instance = new MonsterInsights();

			global $wp_version;
			
			// Detect non-supported WordPress version and return early
			if ( version_compare( $wp_version, '3.8', '<' ) && ( ! defined( 'MONSTERINSIGHTS_FORCE_ACTIVATION' ) || ! MONSTERINSIGHTS_FORCE_ACTIVATION ) ) {
				add_action( 'admin_notices', array( self::$instance, 'monsterinsights_wp_notice' ) );
				return;
			}
	
			// Detect Lite version and return early
			if ( class_exists( 'MonsterInsights_Lite' ) ) {
				add_action( 'admin_notices', array( self::$instance, 'monsterinsights_lite_notice' ) );
				if ( defined( 'GAWP_VERSION' ) ) {
					return;
				}
			}

			if ( defined( 'GAWP_ECOMMERCE_PATH' ) ) {
				add_action( 'admin_notices', array( self::$instance, 'monsterinsights_old_ecommerce' ) );
			}
			
			// Define constants
			self::$instance->define_globals();

			// Load in settings
			self::$instance->load_settings();

			// Load files
			self::$instance->require_files();
			
			// This does the version to version background upgrade routines and initial install
			$mi_version = get_option( 'monsterinsights_current_version', '5.5.3' );
			if ( version_compare( $mi_version, '6.0.11', '<' ) ) {
				monsterinsights_call_install_and_upgrade();
			}

			if ( is_admin() ) {
				new AM_Notification( 'mi', self::$instance->version );
				new AM_Deactivation_Survey( 'MonsterInsights Pro', basename( __DIR__ ) );
			}

			// Load the plugin textdomain.
			add_action( 'plugins_loaded', array( self::$instance, 'load_plugin_textdomain' ) );

			// Load GA for admin, lazyload for frontend
			if ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) {
				self::$instance->ga    		= new MonsterInsights_GA();
			}
			
			// Load admin only components.
			if ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) {
				self::$instance->notices    = new MonsterInsights_Notice_Admin();
				self::$instance->license    = new MonsterInsights_License();
				self::$instance->reports 	= new MonsterInsights_Reporting();
				if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
					self::$instance->require_updater();
				} else {
					add_action( 'admin_init', array( self::$instance, 'require_updater' ) );
				}
			}

			// Run hook to load MonsterInsights addons.
			do_action( 'monsterinsights_load_plugins' ); // the updater class for each addon needs to be instantiated via `monsterinsights_updater`
		}

		return self::$instance;

	}

	/**
	 * Throw error on object clone
	 *
	 * The whole idea of the singleton design pattern is that there is a single
	 * object therefore, we don't want the object to be cloned.
	 *
	 * @since 6.0.0
	 * @access public
	 *
	 * @return void
	 */
	public function __clone() {
		_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; huh?', 'ga-premium' ), '6.0.0' );
	}

	/**
	 * Disable unserializing of the class
	 *
	 * Attempting to wakeup an MonsterInsights instance will throw a doing it wrong notice.
	 * 
	 * @since 6.0.0
	 * @access public
	 *
	 * @return void
	 */
	public function __wakeup() {
		_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; huh?', 'ga-premium' ), '6.0.0' );
	}

	/**
	 * Magic get function.
	 *
	 * We use this to lazy load certain functionality. Right now used to lazyload
	 * the Google Object for frontend, so it's only loaded if user is using a plugin
	 * that requires it.
	 *
	 * @since 6.0.10
	 * @access public
	 *
	 * @return void
	 */
	public function __get( $key ) {
		if ( $key === 'ga' ) {
			if ( empty( self::$instance->ga ) ) {
				// LazyLoad GA for Frontend
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/google.php';
				self::$instance->ga = new MonsterInsights_GA();
			}
			return self::$instance->$key;
		} else {
			return self::$instance->$key;
		}
	}

	/**
	 * Define MonsterInsights constants.
	 *
	 * This function defines all of the MonsterInsights PHP constants.
	 *
	 * @since 6.0.0
	 * @access public
	 *
	 * @return void
	 */
	public function define_globals() {

		if ( ! defined( 'MONSTERINSIGHTS_VERSION' ) ) {
			define( 'MONSTERINSIGHTS_VERSION', $this->version );
		}

		if ( ! defined( 'MONSTERINSIGHTS_PRO_VERSION' ) ) {
			define( 'MONSTERINSIGHTS_PRO_VERSION', MONSTERINSIGHTS_VERSION );
		}

		if ( ! defined( 'GAWP_VERSION' ) ) {
			define( 'GAWP_VERSION', MONSTERINSIGHTS_VERSION );
		}

		if ( ! defined( 'GA_YOAST_PREMIUM_VERSION' ) ) {
			define( 'GA_YOAST_PREMIUM_VERSION', MONSTERINSIGHTS_VERSION );
		}

		if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_NAME' ) ) {
			define( 'MONSTERINSIGHTS_PLUGIN_NAME', $this->plugin_name );
		}

		if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_SLUG' ) ) {
			define( 'MONSTERINSIGHTS_PLUGIN_SLUG', $this->plugin_slug );
		}

		if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_FILE' ) ) {
			define( 'MONSTERINSIGHTS_PLUGIN_FILE', $this->file );
		}

		if ( ! defined( 'GAWP_FILE' ) ) {
			define( 'GAWP_FILE', MONSTERINSIGHTS_PLUGIN_FILE );
		}

		if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_DIR' ) ) {
			define( 'MONSTERINSIGHTS_PLUGIN_DIR', plugin_dir_path( $this->file )  );
		}

		if ( ! defined( 'GAWP_PATH' ) ) {
			define( 'GAWP_PATH', MONSTERINSIGHTS_PLUGIN_DIR  );
		}

		if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_URL' ) ) {
			define( 'MONSTERINSIGHTS_PLUGIN_URL', plugin_dir_url( $this->file )  );
		}

		if ( ! defined( 'GAWP_URL' ) ) {
			define( 'GAWP_URL', MONSTERINSIGHTS_PLUGIN_URL  );
		}
	}	

	/**
	 * Loads the plugin textdomain for translation.
	 *
	 * @access public
	 * @since 6.0.0
	 *
	 * @return void
	 */
	public function load_plugin_textdomain() {

		$mi_locale = get_locale();
		if ( function_exists( 'get_user_locale' ) ) {
			$mi_locale = get_user_locale();
		}		

		// Load Pro Translation files
			// Traditional WordPress plugin locale filter.
			$mi_pro_locale  = apply_filters( 'plugin_locale',  $mi_locale, 'ga-premium' );
			$mi_pro_mofile  = sprintf( '%1$s-%2$s.mo', 'ga-premium', $mi_pro_locale ); 
		
			// Look for wp-content/languages/google-analytics-premium/ga-premium-{lang}_{country}.mo
			$mi_pro_mofile1 = WP_LANG_DIR . '/google-analytics-premium/' . $mi_pro_mofile;

			// Look in wp-content/languages/plugins/google-analytics-premium/ga-premium-{lang}_{country}.mo
			$mi_pro_mofile2 = WP_LANG_DIR . '/plugins/google-analytics-premium/' . $mi_pro_mofile;

			// Look in wp-content/languages/plugins/ga-premium-{lang}_{country}.mo
			$mi_pro_mofile3 = WP_LANG_DIR . '/plugins/' . $mi_pro_mofile;

			// Look in wp-content/plugins/google-analytics-premium/pro/languages/ga-premium-{lang}_{country}.mo
			$mi_pro_mofile4 = dirname( plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ) ) . '/pro/languages/';
			$mi_pro_mofile4 = apply_filters( 'monsterinsights_pro_languages_directory', $mi_pro_mofile4 );

			if ( file_exists( $mi_pro_mofile1 ) ) {
				load_textdomain( 'ga-premium', $mi_pro_mofile1 );
			} elseif ( file_exists( $mi_pro_mofile2 ) ) {
				load_textdomain( 'ga-premium', $mi_pro_mofile2 );
			} elseif ( file_exists( $mi_pro_mofile3 ) ) {
				load_textdomain( 'ga-premium', $mi_pro_mofile3 );
			} else {
				load_plugin_textdomain( 'ga-premium', false, $mi_pro_mofile4 );
			}

		// Load Lite Translation files
			// Traditional WordPress plugin locale filter.
			$mi_locale  = apply_filters( 'plugin_locale',  $mi_locale, 'google-analytics-for-wordpress' );
			$mi_mofile  = sprintf( '%1$s-%2$s.mo', 'google-analytics-for-wordpress', $mi_locale ); 
		
			// Look for wp-content/languages/google-analytics-premium/google-analytics-for-wordpress-{lang}_{country}.mo
			$mi_mofile1 = WP_LANG_DIR . '/google-analytics-premium/' . $mi_mofile;

			// Look in wp-content/languages/plugins/google-analytics-premium/google-analytics-for-wordpress-{lang}_{country}.mo
			$mi_mofile2 = WP_LANG_DIR . '/plugins/google-analytics-premium/' . $mi_mofile;

			// Look in wp-content/languages/plugins/google-analytics-for-wordpress-{lang}_{country}.mo
			$mi_mofile3 = WP_LANG_DIR . '/plugins/' . $mi_mofile;

			// Look in wp-content/plugins/google-analytics-premium/languages/google-analytics-for-wordpress-{lang}_{country}.mo
			$mi_mofile4 = dirname( plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ) ) . '/languages/';
			$mi_mofile4 = apply_filters( 'monsterinsights_lite_languages_directory', $mi_mofile4 );

			if ( file_exists( $mi_mofile1 ) ) {
				load_textdomain( 'google-analytics-for-wordpress', $mi_mofile1 );
			} elseif ( file_exists( $mi_mofile2 ) ) {
				load_textdomain( 'google-analytics-for-wordpress', $mi_mofile2 );
			} elseif ( file_exists( $mi_mofile3 ) ) {
				load_textdomain( 'google-analytics-for-wordpress', $mi_mofile3 );
			} else {
				load_plugin_textdomain( 'google-analytics-for-wordpress', false, $mi_mofile4 );
			}
	}

	/**
	 * Output notice to update eCommerce
	 *
	 * @access public
	 * @since 6.0.0
	 *
	 * @return 	void
	 */
	public function monsterinsights_old_ecommerce() {
		?>
		<div class="error">
			<p><?php echo __( 'The version of MonsterInsights eCommerce addon you have is not compatible with the version of MonsterInsights installed. Please update the eCommerce addon as soon as possible', 'ga-premium' ); ?></p>
		</div>
		<?php

	}

	/**
	 * Output a nag notice if the user has an out of date WP version installed
	 *
	 * @access public
	 * @since 6.0.0
	 *
	 * @return 	void
	 */
	public function monsterinsights_wp_notice() {
		$url = admin_url( 'plugins.php' );
		// Check for MS dashboard
		if( is_network_admin() ) {
			$url = network_admin_url( 'plugins.php' );
		}
		?>
		<div class="error">
			<p><?php echo sprintf( esc_html__( 'Sorry, but your version of WordPress does not meet MonsterInsights\'s required version of %1$s3.8%2$s to run properly. The plugin not been activated. %3$sClick here to return to the Dashboard%4$s.', 'ga-premium' ), '<strong>', '</strong>', '<a href="' . $url . '">', '</a>' ); ?></p>
		</div>
		<?php

	}

	/**
	 * Output a nag notice if the user has both Lite and Pro activated
	 *
	 * @access public
	 * @since 6.0.0
	 *
	 * @return 	void
	 */
	public function monsterinsights_lite_notice() {
		$url = admin_url( 'plugins.php' );
		// Check for MS dashboard
		if( is_network_admin() ) {
			$url = network_admin_url( 'plugins.php' );
		}
		?>
		<div class="error">
			<p><?php echo sprintf( esc_html__( 'Please %1$suninstall%2$s the MonsterInsights Lite Plugin. Your premium version of MonsterInsights may not work as expected until the Lite version is uninstalled.', 'ga-premium' ), '<a href="' . $url . '">', '</a>' ); ?></p>
		</div>
		<?php

	}

	/**
	 * Loads MonsterInsights settings
	 *
	 * Adds the items to the base object, and adds the helper functions.
	 *
	 * @since 6.0.0
	 * @access public
	 * 
	 * @return void
	 */
	public function load_settings() {
		global $monsterinsights_settings;
		require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/options.php';
		require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/helpers.php';
		require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/deprecated.php';
		$monsterinsights_settings = monsterinsights_get_options();
	}

	/**
	 * Loads all files into scope.
	 *
	 * @access public
	 * @since 6.0.0
	 *
	 * @return 	void
	 */
	public function require_files() {

		if ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) {

			// Lite and Pro files
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'assets/lib/pandora/class-am-notification.php';
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'assets/lib/pandora/class-am-deactivation-survey.php';
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/ajax.php';
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/admin.php';
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/common.php';
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/notice.php';
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/capabilities.php';
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/licensing/license.php';
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/licensing/autoupdate.php';

			// Pages
				// Multisite
					require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/pages/network-settings.php';

				// Single Site
					require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/pages/dashboard.php';
					require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/pages/settings.php';
					require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/pages/tools.php';
					require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/pages/reports.php';
				
				// Both	
					require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/pages/addons.php';

			// Settings Tabs
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/settings/tab-general.php';
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/settings/tab-tracking.php';
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/settings/tab-status.php';
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/settings/tab-support.php';
			
			// Register Settings + Settings API
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/settings/settings-api.php';
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/settings/register-settings.php';	

			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/reporting.php';
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/abstract-report.php';
		}

		// Load Google Config
		require_once MONSTERINSIGHTS_PLUGIN_DIR . 'pro/includes/google.php';

		// Lazy Load for Frontend. Load for Admin.
		if ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) {
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/google.php';
		}

		if ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) {
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/googleauth.php';

			// Pro only files
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'pro/includes/admin/pro-settings.php';
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'pro/includes/admin/reports.php';
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'pro/includes/admin/tools.php';
			//require_once MONSTERINSIGHTS_PLUGIN_DIR . 'pro/includes/admin/tab-support.php';	
		
			// Late loading classes (self instantiating)
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/reports/overview.php';
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/tracking.php';
		}

		require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/frontend/frontend.php';
		require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/measurement-protocol.php';
		require_once MONSTERINSIGHTS_PLUGIN_DIR . 'pro/includes/frontend/class-frontend.php';
	}

	/**
	 * Loads all updater related files and functions into scope.
	 *
	 * @access public
	 * @since 6.0.0
	 *
	 * @return null Return early if the license key is not set or there are key errors.
	 */
	public function require_updater() {

		// Retrieve the license key. If it is not set, return early.
		$key = monsterinsights_get_license_key();
		if ( ! $key ) {
			return;
		}

		// If there are any errors with the key itself, return early.
		if ( monsterinsights_get_license_key_errors() ) {
			return;
		}

		// Load the updater class.
		require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/licensing/updater.php';

		// Go ahead and initialize the updater.
		$args = array(
			'plugin_name' => $this->plugin_name,
			'plugin_slug' => $this->plugin_slug,
			'plugin_path' => plugin_basename( __FILE__ ),
			'plugin_url'  => trailingslashit( WP_PLUGIN_URL ) . $this->plugin_slug,
			'remote_url'  => 'https://www.monsterinsights.com/',
			'version'     => $this->version,
			'key'         => $key,
		);

		$updater = new MonsterInsights_Updater( $args );


		// Fire a hook for Addons to register their updater since we know the key is present.
		do_action( 'monsterinsights_updater', $key );
	}
}

/**
 * Fired when the plugin is activated.
 *
 * @access public
 * @since 6.0.0
 *
 * @global int $wp_version      The version of WordPress for this install.
 * @global object $wpdb         The WordPress database object.
 * @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false otherwise.
 */
function monsterinsights_activation_hook( $network_wide ) {

	global $wp_version;
	
	$url = admin_url( 'plugins.php' );
	// Check for MS dashboard
	if ( is_network_admin() ) {
		$url = network_admin_url( 'plugins.php' );
	}
	
	if ( version_compare( $wp_version, '3.8', '<' ) && ! defined( 'MONSTERINSIGHTS_FORCE_ACTIVATION' ) ) {
		deactivate_plugins( plugin_basename( __FILE__ ) );
		wp_die( sprintf( esc_html__( 'Sorry, but your version of WordPress does not meet MonsterInsight\'s required version of %1$s3.8%2$s to run properly. The plugin has not been activated. %3$sClick here to return to the Dashboard%4$s.', 'ga-premium' ), '<strong>', '</strong>', '<a href="' . $url . '">', '</a>' ) );
	}

	if ( class_exists( 'MonsterInsights_Lite' ) ) {
		deactivate_plugins( plugin_basename( __FILE__ ) );
		wp_die( sprintf( esc_html__( 'Please uninstall and remove MonsterInsights Lite before activating MonsterInsights Pro. The Pro version has not been activated. %1$sClick here to return to the Dashboard%2$s.', 'ga-premium' ), '<a href="' . $url . '">', '</a>' ) );
	}
}
register_activation_hook( __FILE__, 'monsterinsights_activation_hook' );

/**
 * Fired when the plugin is deactivation.
 *
 * @access public
 * @since 6.0.0
 * 
 * @return 	void
 */
function monsterinsights_deactivation_hook( $network_wide ) {

	$instance = MonsterInsights();

	if ( is_multisite() && $network_wide ) {
		$site_list = get_sites();
		$options = array(
			'cron_failed',
			'cron_last_run',
		);

		foreach ( (array) $site_list as $site ) {
			switch_to_blog( $site->blog_id );
			monsterinsights_delete_options( $options );
			restore_current_blog();
		}
	} else {
		$options = array(
			'cron_failed',
			'cron_last_run',
		);
		monsterinsights_delete_options( $options );
	}

}
register_deactivation_hook( __FILE__, 'monsterinsights_deactivation_hook' );

/**
 * Fired when the plugin is uninstalled.
 *
 * @access public
 * @since 6.0.0
 * 
 * @return 	void
 */
function monsterinsights_uninstall_hook( $network_wide ) {
	wp_cache_flush();
	$instance = MonsterInsights();

	if ( is_multisite() && $network_wide ) {
		delete_site_option( 'monsterinsights_license' );
		delete_site_option( 'monsterinsights_license_updates' );
		$site_list = get_sites();

		$options = array(
			'analytics_profile_code',
			'analytics_profile',
			'analytics_profile_name',
			'analytics_name',
			'oauth_version',
			'cron_failed',
			'cron_last_run',
		);

		foreach ( (array) $site_list as $site ) {
			switch_to_blog( $site->blog_id );
			delete_option( 'monsterinsights_license' );
			delete_option( 'monsterinsights_license_updates' );

			monsterinsights_delete_options( $options );
			delete_option( 'monsterinsights_pro_access_token' );
			delete_option( 'monsterinsights_pro_refresh_token' );

			// Destroy the data
			$instance->reports->delete_aggregate_data();			
			restore_current_blog();
		}
	} else {
		delete_option( 'monsterinsights_license' );
		delete_option( 'monsterinsights_license_updates' );

		$options = array(
			'analytics_profile_code',
			'analytics_profile',
			'analytics_profile_name',
			'analytics_name',
			'oauth_version',
			'cron_failed',
			'cron_last_run',
		);
		monsterinsights_delete_options( $options );
		delete_option( 'monsterinsights_pro_access_token' );
		delete_option( 'monsterinsights_pro_refresh_token' );

		// Destroy the data
		if ( isset( $instance->reports ) && method_exists( $instance->reports,'delete_aggregate_data' ) ) {
			$instance->reports->delete_aggregate_data();
		}
	}

}
register_uninstall_hook( __FILE__, 'monsterinsights_uninstall_hook' );

/**
 * The main function responsible for returning the one true MonsterInsights
 * Instance to functions everywhere.
 *
 * Use this function like you would a global variable, except without needing
 * to declare the global.
 *
 * Example: <?php $monsterinsights = MonsterInsights(); ?>
 *
 * @since 6.0.0
 *
 * @uses MonsterInsights::get_instance() Retrieve MonsterInsights instance.
 * 
 * @return MonsterInsights The singleton MonsterInsights instance.
 */
function MonsterInsights_Pro() {
	return MonsterInsights::get_instance();
}

/**
 * MonsterInsights Install and Updates.
 *
 * This function is used install and upgrade MonsterInsights. This is used for upgrade routines
 * that can be done automatically, behind the scenes without the need for user interaction
 * (for example pagination or user input required), as well as the initial install.
 *
 * @since 6.0.0
 * @access public
 *
 * @global string $wp_version WordPress version (provided by WordPress core).
 * @uses MonsterInsights::load_settings() Loads MonsterInsights settings
 * @uses MonsterInsights_Install::init() Runs upgrade process
 * 
 * @return void
 */
function monsterinsights_install_and_upgrade() {
	global $wp_version;

	// If the WordPress site doesn't meet the correct WP version requirements, don't activate MonsterInsights
	if ( version_compare( $wp_version, '3.8', '<' ) ) {
		if ( is_plugin_active( plugin_basename( __FILE__ ) ) ) {
			return;
		}
	}

	// Don't run if MI Lite is installed
	if ( class_exists( 'MonsterInsights_Lite' ) ) {
		if ( is_plugin_active( plugin_basename( __FILE__ ) ) ) {
			return;
		}
	}

	// Load settings and globals (so we can use/set them during the upgrade process)
	MonsterInsights()->define_globals();
	MonsterInsights()->load_settings();

	// Load upgrade file
	require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/install.php';

	// Pro only install stuff
	require_once MONSTERINSIGHTS_PLUGIN_DIR . 'pro/includes/install.php';
	require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/pages/addons.php';
	require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/licensing/license.php';

	// Run the MonsterInsights upgrade routines
	$updates = new MonsterInsights_Install();
	$updates->init();
}

/**
 * MonsterInsights check for install and update processes.
 *
 * This function is used to call the MonsterInsights automatic upgrade class, which in turn
 * checks to see if there are any update procedures to be run, and if
 * so runs them. Also installs MonsterInsights for the first time.
 *
 * @since 6.0.0
 * @access public
 *
 * @uses MonsterInsights_Install() Runs install and upgrade process.
 * 
 * @return void
 */
function monsterinsights_call_install_and_upgrade(){
	add_action( 'wp_loaded', 'monsterinsights_install_and_upgrade' );
}

/**
 * Returns the MonsterInsights combined object that you can use for both
 * MonsterInsights Lite and Pro Users. When both plugins active, defers to the 
 * more complete Pro object. 
 *
 * Warning: Do not use this in Lite or Pro specific code (use the individual objects instead). 
 * Also do not use in the MonsterInsights Lite/Pro upgrade and install routines.
 *
 * Use this function like you would a global variable, except without needing
 * to declare the global.
 *
 * Prevents the need to do conditional global object logic when you have code that you want to work with
 * both Pro and Lite.
 *
 * Example: <?php $monsterinsights = MonsterInsights(); ?>
 *
 * @since 6.0.0
 *
 * @uses MonsterInsights::get_instance() Retrieve MonsterInsights Pro instance.
 * @uses MonsterInsights_Lite::get_instance() Retrieve MonsterInsights Lite instance.
 * 
 * @return MonsterInsights The singleton MonsterInsights instance.
 */
if ( ! function_exists( 'MonsterInsights' ) ) {
	function MonsterInsights() {
		return ( class_exists( 'MonsterInsights' ) ? MonsterInsights_Pro() : MonsterInsights_Lite() );
	}
	add_action( 'plugins_loaded', 'MonsterInsights' );
}