constants(); $this->includes(); add_action('admin_menu', array($this, 'add_menu')); add_filter('admin_init', array($this, 'add_settings')); add_action('admin_enqueue_scripts', array($this, 'admin_scripts')); 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_init', array($this, 'check_version')); add_action('admin_init', array($this, 'reset_options')); add_action('admin_notices', array($this, 'admin_notices')); } function constants() { if (!defined('GAP_VERSION')) define('GAP_VERSION', '20220517'); if (!defined('GAP_REQUIRE')) define('GAP_REQUIRE', '4.6'); if (!defined('GAP_AUTHOR')) define('GAP_AUTHOR', 'Jeff Starr'); if (!defined('GAP_NAME')) define('GAP_NAME', __('GA Google Analytics', 'ga-google-analytics')); if (!defined('GAP_HOME')) define('GAP_HOME', 'https://perishablepress.com/ga-google-analytics/'); if (!defined('GAP_PATH')) define('GAP_PATH', 'options-general.php?page=ga-google-analytics'); if (!defined('GAP_URL')) define('GAP_URL', plugin_dir_url(__FILE__)); if (!defined('GAP_DIR')) define('GAP_DIR', plugin_dir_path(__FILE__)); if (!defined('GAP_FILE')) define('GAP_FILE', plugin_basename(__FILE__)); if (!defined('GAP_SLUG')) define('GAP_SLUG', basename(dirname(__FILE__))); } function includes() { require_once GAP_DIR .'inc/plugin-core.php'; } function add_menu() { $title_page = esc_html__('GA Google Analytics', 'ga-google-analytics'); $title_menu = esc_html__('Google Analytics', 'ga-google-analytics'); add_options_page($title_page, $title_menu, 'manage_options', 'ga-google-analytics', array($this, 'display_settings')); } function add_settings() { register_setting('gap_plugin_options', 'gap_options', array($this, 'validate_settings')); } function admin_scripts($hook) { if ($hook === 'settings_page_ga-google-analytics') { wp_enqueue_style('ga-google-analytics', GAP_URL .'css/settings.css', array(), GAP_VERSION); wp_enqueue_script('ga-google-analytics', GAP_URL .'js/settings.js', array('jquery'), GAP_VERSION); $this->localize_scripts(); } } function localize_scripts() { $script = array( 'confirm_message' => esc_html__('Are you sure you want to restore all default options?', 'ga-google-analytics') ); wp_localize_script('ga-google-analytics', 'ga_google_analytics', $script); } function action_links($links, $file) { if ($file === GAP_FILE && current_user_can('manage_options')) { $settings = ''. esc_html__('Settings', 'ga-google-analytics') .''; array_unshift($links, $settings); } if ($file === GAP_FILE) { $pro_href = 'https://plugin-planet.com/ga-google-analytics-pro/?plugin'; $pro_title = esc_attr__('Get GA Pro!', 'ga-google-analytics'); $pro_text = esc_html__('Go Pro', 'ga-google-analytics'); $pro_style = 'font-weight:bold;'; $pro = ''. $pro_text .''; array_unshift($links, $pro); } return $links; } function plugin_links($links, $file) { if ($file === GAP_FILE) { $home_href = 'https://perishablepress.com/google-analytics-plugin/'; $home_title = esc_attr__('Plugin Homepage', 'ga-google-analytics'); $home_text = esc_html__('Homepage', 'ga-google-analytics'); $links[] = ''. $home_text .''; $rate_href = 'https://wordpress.org/support/plugin/'. GAP_SLUG .'/reviews/?rate=5#new-post'; $rate_title = esc_attr__('Click here to rate and review this plugin on WordPress.org', 'ga-google-analytics'); $rate_text = esc_html__('Rate this plugin', 'ga-google-analytics') .' »'; $links[] = ''. $rate_text .''; } return $links; } function check_version() { $wp_version = get_bloginfo('version'); if (isset($_GET['activate']) && $_GET['activate'] == 'true') { if (version_compare($wp_version, GAP_REQUIRE, '<')) { if (is_plugin_active(GAP_FILE)) { deactivate_plugins(GAP_FILE); $msg = ''. GAP_NAME .' '. esc_html__('requires WordPress ', 'ga-google-analytics') . GAP_REQUIRE; $msg .= esc_html__(' or higher, and has been deactivated! ', 'ga-google-analytics'); $msg .= esc_html__('Please return to the', 'ga-google-analytics') .' '; $msg .= esc_html__('WP Admin Area', 'ga-google-analytics') .' '. esc_html__('to upgrade WordPress and try again.', 'ga-google-analytics'); wp_die($msg); } } } } function load_i18n() { $domain = 'ga-google-analytics'; $locale = apply_filters('gap_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(plugin_basename(__FILE__)) .'/languages/'); } } } function admin_notices() { $screen = get_current_screen(); if (!property_exists($screen, 'id')) return; if ($screen->id === 'settings_page_ga-google-analytics') { if (isset($_GET['gap-reset-options'])) { if ($_GET['gap-reset-options'] === 'true') : ?>
GTM- ';
$message .= esc_html__('(for Google Tag Manager), which is not supported. Please try again with a supported tracking code.', 'ga-google-analytics');
add_settings_error('gap_id', 'invalid-tracking-code', $message, 'error');
}
if (!isset($input['gap_location'])) $input['gap_location'] = null;
if (!array_key_exists($input['gap_location'], $this->options_locations())) $input['gap_location'] = null;
if (!isset($input['gap_enable'])) $input['gap_enable'] = null;
if (!array_key_exists($input['gap_enable'], $this->options_libraries())) $input['gap_enable'] = null;
if (!isset($input['gap_display_ads'])) $input['gap_display_ads'] = null;
$input['gap_display_ads'] = ($input['gap_display_ads'] == 1 ? 1 : 0);
if (!isset($input['link_attr'])) $input['link_attr'] = null;
$input['link_attr'] = ($input['link_attr'] == 1 ? 1 : 0);
if (!isset($input['gap_anonymize'])) $input['gap_anonymize'] = null;
$input['gap_anonymize'] = ($input['gap_anonymize'] == 1 ? 1 : 0);
if (!isset($input['gap_force_ssl'])) $input['gap_force_ssl'] = null;
$input['gap_force_ssl'] = ($input['gap_force_ssl'] == 1 ? 1 : 0);
if (!isset($input['admin_area'])) $input['admin_area'] = null;
$input['admin_area'] = ($input['admin_area'] == 1 ? 1 : 0);
if (!isset($input['disable_admin'])) $input['disable_admin'] = null;
$input['disable_admin'] = ($input['disable_admin'] == 1 ? 1 : 0);
if (!isset($input['gap_custom_loc'])) $input['gap_custom_loc'] = null;
$input['gap_custom_loc'] = ($input['gap_custom_loc'] == 1 ? 1 : 0);
if (isset($input['tracker_object'])) $input['tracker_object'] = wp_strip_all_tags(trim($input['tracker_object']));
if (isset($input['gap_custom_code'])) $input['gap_custom_code'] = wp_strip_all_tags(trim($input['gap_custom_code']));
if (isset($input['gap_custom'])) $input['gap_custom'] = stripslashes($input['gap_custom']);
return $input;
}
function options_locations() {
return array(
'header' => array(
'value' => 'header',
'label' => esc_html__('Include tracking code in page head (via', 'ga-google-analytics') .' wp_head'. esc_html__(')', 'ga-google-analytics')
),
'footer' => array(
'value' => 'footer',
'label' => esc_html__('Include tracking code in page footer (via', 'ga-google-analytics') .' wp_footer'. esc_html__(')', 'ga-google-analytics')
)
);
}
function options_libraries() {
$url1 = 'https://developers.google.com/analytics/devguides/collection/analyticsjs/';
$url2 = 'https://developers.google.com/analytics/devguides/collection/gtagjs/';
$url3 = 'https://developers.google.com/analytics/devguides/collection/gajs/';
$link1 = ''. esc_html__('Universal Analytics', 'ga-google-analytics') .' ';
$link2 = ''. esc_html__('Global Site Tag', 'ga-google-analytics') .' ';
$link3 = ''. esc_html__('Legacy', 'ga-google-analytics') .' ';
return array(
1 => array(
'value' => 1,
'label' => $link1 .' / analytics.js '. esc_html__('(default)', 'ga-google-analytics') .''
),
2 => array(
'value' => 2,
'label' => $link2 .' / gtag.js '. esc_html__('(new method)', 'ga-google-analytics') .''
),
3 => array(
'value' => 3,
'label' => $link3 .' / ga.js '. esc_html__('(deprecated)', 'ga-google-analytics') .''
)
);
}
function display_settings() {
$gap_options = get_option('gap_options', $this->default_options());
require_once GAP_DIR .'inc/settings-display.php';
}
function select_menu($items, $menu) {
$options = get_option('gap_options', $this->default_options());
$universal = isset($options['gap_universal']) ? $options['gap_universal'] : 1;
$tracking = isset($options['gap_enable']) ? $options['gap_enable'] : 1;
$checked = '';
$output = '';
$class = '';
foreach ($items as $item) {
$key = isset($options[$menu]) ? $options[$menu] : '';
$value = isset($item['value']) ? $item['value'] : '';
if ($menu === 'gap_enable') {
if ($tracking == 0) $key = 1;
if (!$universal && $tracking == 1) $key = 3;
$class = ' gap-select-method';
}
$checked = ($value == $key) ? ' checked="checked"' : '';
$output .= '