<?php 
/*
	Plugin Name: GA Google Analytics Pro
	Plugin URI: https://plugin-planet.com/ga-google-analytics-pro/
	Description: Adds GA Tracking to your WordPress site. Pro version features opt-out box, multiple trackers, code previews and more.
	Tags: analytics, google, google analytics, tracking, statistics
	Author: Jeff Starr
	Author URI: https://plugin-planet.com/
	Donate link: https://monzillamedia.com/donate.html
	Contributors: specialk
	Requires at least: 4.6
	Tested up to: 6.7
	Stable tag: 2.7.1
	Version:    2.7.1
	Requires PHP: 5.6.20
	Text Domain: ga-google-analytics-pro
	Domain Path: /languages
	License: The GA Google Analytics Pro license is comprised of two parts:
	
	* Part 1: Its PHP code is licensed under the GPL (v2 or later), like WordPress. More info @ https://www.gnu.org/licenses/
	* Part 2: Everything else (e.g., CSS, HTML, JavaScript, images, design) is licensed according to the purchased license. More info @ https://plugin-planet.com/ga-google-analytics-pro/
	
	Without prior written consent from Monzilla Media, you must NOT directly or indirectly: license, sub-license, sell, resell, or provide for free any aspect or component of Part 2.
	
	Further license information is available in the plugin directory, /license/, and online @ https://plugin-planet.com/wp/files/ga-google-analytics-pro/license.txt
	
	Upgrades: Your purchase of GA Google Analytics Pro includes free lifetime upgrades, which include new features, bug fixes, and other improvements. 
	
	Copyright 2024 Monzilla Media. All rights reserved.
*/

if (!defined('ABSPATH')) die();

if (!class_exists('GA_Google_Analytics_Pro')) {
	
	class GA_Google_Analytics_Pro {
		
		function __construct() {
			
			$this->constants();
			$this->includes();
			
			register_activation_hook(__FILE__, 'ga_google_analytics_pro_dismiss_notice_activate');
			
			add_action('admin_init',          array($this, 'check_plugin'));
			add_action('admin_init',          array($this, 'check_version'));
			add_filter('admin_footer_text',   array($this, 'footer_text'),  10, 1);
			add_filter('plugin_action_links', array($this, 'action_links'), 10, 2);
			add_filter('plugin_row_meta',     array($this, 'plugin_links'), 10, 2);
			add_action('init',                array($this, 'load_i18n'));
			
			add_action('admin_menu',            'ga_google_analytics_pro_add_options_page');
			add_filter('admin_init',            'ga_google_analytics_pro_register_settings');
			add_action('admin_init',            'ga_google_analytics_pro_reset_options');
			add_action('admin_notices',         'ga_google_analytics_pro_admin_notices');
			add_action('admin_init',            'ga_google_analytics_pro_dismiss_notice_save');
			add_action('admin_init',            'ga_google_analytics_pro_dismiss_notice_version');
			add_action('admin_print_scripts',   'ga_google_analytics_pro_admin_print_scripts');
			add_action('admin_enqueue_scripts', 'ga_google_analytics_pro_admin_enqueue_scripts');
			
			add_action('wp_enqueue_scripts',    'ga_google_analytics_pro_wp_enqueue_scripts');
			add_filter('script_loader_tag',     'ga_google_analytics_pro_script_loader_tag', 10, 2);
			add_action('wp_footer',             'ga_google_analytics_pro_optout_footer');
			
			add_shortcode('ga_pro_optout',       'ga_google_analytics_pro_optout_output');
			add_shortcode('ga_pro_optout_link',  'ga_google_analytics_pro_optout_link');
			add_shortcode('ga_pro_optin_link',   'ga_google_analytics_pro_optin_link');
			add_shortcode('gapro_optin_plain',   'ga_google_analytics_pro_optin_plain');
			add_shortcode('gapro_optout_plain',  'ga_google_analytics_pro_optout_plain');
			add_shortcode('gapro_optout_toggle', 'ga_google_analytics_pro_optout_toggle');
			
		}
		
		function constants() {
			
			if (!defined('GAPRO_VERSION')) define('GAPRO_VERSION', '2.7.1');
			if (!defined('GAPRO_REQUIRE')) define('GAPRO_REQUIRE', '4.6');
			if (!defined('GAPRO_TESTED'))  define('GAPRO_TESTED',  '6.7');
			if (!defined('GAPRO_AUTHOR'))  define('GAPRO_AUTHOR',  'Jeff Starr');
			if (!defined('GAPRO_NAME'))    define('GAPRO_NAME',    __('GA Google Analytics Pro', 'ga-google-analytics-pro'));
			if (!defined('GAPRO_HOME'))    define('GAPRO_HOME',    'https://plugin-planet.com');
			if (!defined('GAPRO_PATH'))    define('GAPRO_PATH',    'options-general.php?page=ga-google-analytics-pro');
			if (!defined('GAPRO_URL'))     define('GAPRO_URL',     plugin_dir_url(__FILE__));
			if (!defined('GAPRO_DIR'))     define('GAPRO_DIR',     plugin_dir_path(__FILE__));
			if (!defined('GAPRO_FILE'))    define('GAPRO_FILE',    plugin_basename(__FILE__));
			if (!defined('GAPRO_SLUG'))    define('GAPRO_SLUG',    basename(dirname(__FILE__)));
			
		}
		
		function includes() {
			
			require_once GAPRO_DIR .'inc/core-functions.php';
			
			require_once GAPRO_DIR .'inc/optout-functions.php';
			
			if (is_admin()) {
				
				require_once GAPRO_DIR .'inc/contextual-help.php';
				require_once GAPRO_DIR .'inc/resources-enqueue.php';
				require_once GAPRO_DIR .'inc/settings-about.php';
				require_once GAPRO_DIR .'inc/settings-callbacks.php';
				require_once GAPRO_DIR .'inc/settings-display.php';
				require_once GAPRO_DIR .'inc/settings-register.php';
				require_once GAPRO_DIR .'inc/settings-reset.php';
				require_once GAPRO_DIR .'inc/settings-validate.php';
				require_once GAPRO_DIR .'inc/settings-callbacks.php';
				require_once GAPRO_DIR .'updates/updates.php';
				
			}
			
		}
		
		function default_options($option = false) {
			
			$configure = array(
				
				'gapro_id'          => '',
				'gapro_library'     => 2,
				'gapro_display_ads' => 0,
				'gapro_link_attr'   => 0,
				'gapro_anonymize'   => 0,
				'gapro_force_ssl'   => 0,
				'gapro_location'    => 0,
				'tracker_object'    => '',
				'custom_ga_code'    => '',
				'tracking_preview'  => '',
				
			);
			
			$customize = array(
				
				'custom1_code'     => '',
				'custom1_postids'  => '',
				'custom1_exclude'  => '',
				'custom1_admin'    => 0,
				'custom1_frontend' => 0,
				'custom1_position' => 0,
				'custom1_preview'  => '',
				
				'custom2_code'     => '',
				'custom2_postids'  => '',
				'custom2_exclude'  => '',
				'custom2_admin'    => 0,
				'custom2_frontend' => 0,
				'custom2_position' => 0,
				'custom2_preview'  => '',
				
				'custom3_code'     => '',
				'custom3_postids'  => '',
				'custom3_exclude'  => '',
				'custom3_admin'    => 0,
				'custom3_frontend' => 0,
				'custom3_position' => 0,
				'custom3_preview'  => '',
				
				'custom4_code'     => '',
				'custom4_postids'  => '',
				'custom4_exclude'  => '',
				'custom4_admin'    => 0,
				'custom4_frontend' => 0,
				'custom4_position' => 0,
				'custom4_preview'  => '',
				
				'custom5_code'     => '',
				'custom5_postids'  => '',
				'custom5_exclude'  => '',
				'custom5_admin'    => 0,
				'custom5_frontend' => 0,
				'custom5_position' => 0,
				'custom5_preview'  => '',
				
				'custom6_code'     => '',
				'custom6_postids'  => '',
				'custom6_exclude'  => '',
				'custom6_admin'    => 0,
				'custom6_frontend' => 0,
				'custom6_position' => 0,
				'custom6_preview'  => '',
				
				'custom7_code'     => '',
				'custom7_postids'  => '',
				'custom7_exclude'  => '',
				'custom7_admin'    => 0,
				'custom7_frontend' => 0,
				'custom7_position' => 0,
				'custom7_preview'  => '',
				
				'custom8_code'     => '',
				'custom8_postids'  => '',
				'custom8_exclude'  => '',
				'custom8_admin'    => 0,
				'custom8_frontend' => 0,
				'custom8_position' => 0,
				'custom8_preview'  => '',
				
			);
			
			$advanced = array(
				
				'login_page'         => 0,
				'admin_area'         => 0,
				'disable_admin'      => 0,
				'disable_logged'     => 0,
				'disable_post_ids'   => '',
				'disable_user_roles' => '',
				'disable_post_types' => '',
				'disable_archives'   => 0,
				'disable_search'     => 0,
				
			);
			
			$optout = array(
				
				'optout_enable'   => 0,
				'disable_hide'    => 0,
				'after_optin'     => 0,
				'info_text'       => __('This website uses Google Analytics to collect anonymous data. This helps to improve your experience.', 'ga-google-analytics-pro'),
				'success_optout'  => __('Thanks. We set a cookie in your browser, so Google Analytics will not track your future visits.', 'ga-google-analytics-pro'),
				'success_optin'   => __('Thanks. We will use Google Analytics to help improve your experience with the site.', 'ga-google-analytics-pro'),
				'optout_message'  => __('This website collects data via Google Analytics. [ga_pro_optin_link]. [ga_pro_optout_link].', 'ga-google-analytics-pro'),
				'optout_text'     => __('Click here to opt out', 'ga-google-analytics-pro'),
				'optin_text'      => __('Click here to opt in', 'ga-google-analytics-pro'),
				'css_location'    => 0,
				'css_color_text'  => '#dce8ee',
				'css_color_bg'    => '#5599bb',
				'css_color_link'  => '#ffffff',
				'css_color_hover' => '#dce8ee',
				'css_custom'      => '',
				'disable_styles'  => 0,
				'optin_require'   => 0,
				'disable_custom1' => 0,
				'disable_custom2' => 0,
				'disable_custom3' => 0,
				'disable_custom4' => 0,
				'disable_custom5' => 0,
				'disable_custom6' => 0,
				'disable_custom7' => 0,
				'disable_custom8' => 0,
			);
			
			return apply_filters('gapro_default_options', ${$option});
			
		}
		
		function action_links($links, $file) {
			
			if ($file === GAPRO_FILE && current_user_can('manage_options')) {
				
				$gapro_links = '<a href="'. admin_url(GAPRO_PATH) .'">'. esc_html__('Settings', 'ga-google-analytics-pro') .'</a>';
				
				array_unshift($links, $gapro_links);
				
			}
			
			return $links;
			
		}
		
		function plugin_links($links, $file) {
			
			if ($file === GAPRO_FILE) {
				
				$rate_href  = 'https://wordpress.org/support/plugin/ga-google-analytics/reviews/?rate=5#new-post';
				$rate_title = esc_attr__('Click here to rate and review on WordPress.org', 'ga-google-analytics-pro');
				$rate_text  = esc_html__('Rate this plugin', 'ga-google-analytics-pro') .'&nbsp;&raquo;';
				
				$links[]    = '<a target="_blank" rel="noopener noreferrer" href="'. $rate_href .'" title="'. $rate_title .'">'. $rate_text .'</a>';
				
			}
			
			return $links;
			
		}
		
		function footer_text($text) {
			
			$screen_id = ga_google_analytics_pro_get_current_screen_id();
			
			$ids = array('settings_page_ga-google-analytics-pro');
			
			if ($screen_id && apply_filters('gapro_admin_footer_text', in_array($screen_id, $ids))) {
				
				$text = __('Like this plugin? Give it a', 'ga-google-analytics-pro');
				
				$text .= ' <a target="_blank" rel="noopener noreferrer" href="https://wordpress.org/support/plugin/ga-google-analytics/reviews/?rate=5#new-post">';
				
				$text .= __('★★★★★ rating&nbsp;&raquo;', 'ga-google-analytics-pro') .'</a>';
				
			}
			
			return $text;
			
		}
		
		function check_plugin() {
			
			if (function_exists('ga_google_analytics_init')) {
				
				if (is_plugin_active('ga-google-analytics/ga-google-analytics.php')) {
					
					$msg  = '<strong>'. esc_html__('Warning:', 'ga-google-analytics-pro') .'</strong> ';
					$msg .= esc_html__('Free and Pro versions of GA Pro cannot be activated at the same time. ', 'ga-google-analytics-pro');
					$msg .= esc_html__('Please return to the ', 'ga-google-analytics-pro');
					$msg .= '<a href="'. admin_url('plugins.php') .'">'. esc_html__('WordPress Admin Area', 'ga-google-analytics-pro') .'</a> ';
					$msg .= esc_html__('and try again.', 'ga-google-analytics-pro');
					
					deactivate_plugins(GAPRO_FILE);
					
					wp_die($msg);
					
				}
				
			}
			
		}
		
		function check_version() {
			
			$wp_version = get_bloginfo('version');
			
			if (isset($_GET['activate']) && $_GET['activate'] == 'true') {
				
				if (version_compare($wp_version, GAPRO_REQUIRE, '<')) {
					
					if (is_plugin_active(GAPRO_FILE)) {
						
						deactivate_plugins(GAPRO_FILE);
						
						$msg  = '<strong>'. GAPRO_NAME .'</strong> '. esc_html__('requires WordPress ', 'ga-google-analytics-pro') . GAPRO_REQUIRE;
						$msg .= esc_html__(' or higher, and has been deactivated! ', 'ga-google-analytics-pro');
						$msg .= esc_html__('Please return to the', 'ga-google-analytics-pro') .' <a href="'. admin_url() .'">';
						$msg .= esc_html__('WP Admin Area', 'ga-google-analytics-pro') .'</a> '. esc_html__('to upgrade WordPress and try again.', 'ga-google-analytics-pro');
						
						wp_die($msg);
						
					}
					
				}
				
			}
			
		}
		
		function load_i18n() {
			
			$domain = 'ga-google-analytics-pro';
			
			$locale = apply_filters('gapro_locale', get_locale(), $domain);
			
			$dir    = trailingslashit(WP_LANG_DIR);
			
			$file   = $domain .'-'. $locale .'.mo';
			
			$path_1 = $dir . $file;
			
			$path_2 = $dir . $domain .'/'. $file;
			
			$path_3 = $dir .'plugins/'. $file;
			
			$path_4 = $dir .'plugins/'. $domain .'/'. $file;
			
			$paths = array($path_1, $path_2, $path_3, $path_4);
			
			foreach ($paths as $path) {
				
				if ($loaded = load_textdomain($domain, $path)) {
					
					return $loaded;
					
				} else {
					
					return load_plugin_textdomain($domain, false, dirname(GAPRO_FILE) .'/languages/');
					
				}
				
			}
			
		}
		
		function __clone() {
			
			_doing_it_wrong(__FUNCTION__, esc_html__('Cheatin&rsquo; huh?', 'ga-google-analytics-pro'), GAPRO_VERSION);
			
		}
		
		function __wakeup() {
			
			_doing_it_wrong(__FUNCTION__, esc_html__('Cheatin&rsquo; huh?', 'ga-google-analytics-pro'), GAPRO_VERSION);
			
		}
		
	}
	
	$GLOBALS['GA_Google_Analytics_Pro'] = $GA_Google_Analytics_Pro = new GA_Google_Analytics_Pro(); 
	
	ga_google_analytics_pro_init();
	
}
