Skip to content

Instantly share code, notes, and snippets.

@ethanhinson
Created November 11, 2013 23:05
Show Gist options
  • Save ethanhinson/7422245 to your computer and use it in GitHub Desktop.
Save ethanhinson/7422245 to your computer and use it in GitHub Desktop.
OOP approach to building settings screens
<?php
/**
*
* Class for our options
*
*
* @TODO: Make this into separate pages instead of sections
* @TODO: ->theme single field templates maybe? But Def a better way for blocks of HTML
* @TODO: abstract class that can be extended
* @TODO: autoloader for settings
*
*/
class GaGeneralSettings {
public $section = 'simple_ga';
function __construct() {
add_action('admin_init', array(&$this, 'admin_init'));
add_action('admin_menu', array(&$this, 'add_menu'));
}
public function admin_init() {
// Register our styles
wp_register_style( 'simple_ga_admin_styles', plugins_url('css/settings.css', 'simple-google-analytics/assets/admin/css') );
wp_register_script( 'simple_ga_admin_scripts', plugins_url('js/settings.js', 'simple-google-analytics/assets/admin/js') );
//Field for UA number
register_setting('simple_ga_general', 'simple_ga_ua');
add_settings_section(
'simple_ga_general',
'General Tracking',
'',
$this->section
);
add_settings_field(
'simple_ga_ua',
'Profile',
array(&$this, 'field_simple_ga_ua'),
$this->section,
'simple_ga_general'
);
register_setting('simple_ga_general', 'simple_ga_dbl_click');
add_settings_field(
'simple_ga_dbl_click',
'DoubleClick Tracking',
array(&$this, 'field_simple_ga_dbl_click'),
$this->section,
'simple_ga_general'
);
// Field(s) for Event tracking
register_setting('simple_ga_events', 'simple_ga_track_files');
add_settings_section(
'simple_ga_events',
'Event Tracking',
'',
$this->section
);
add_settings_field(
'simple_ga_track_files',
'Track File Downloads',
array(&$this, 'field_simple_ga_track_files'),
$this->section,
'simple_ga_events'
);
register_setting('simple_ga_events', 'simple_ga_track_external');
add_settings_field(
'simple_ga_track_external',
'Exit via Link',
array(&$this, 'field_simple_ga_track_external'),
$this->section,
'simple_ga_events'
);
// Field(s) for Cross Domain Tracking
register_setting('simple_ga_xdomain', 'simple_ga_track_xdomain');
add_settings_section(
'simple_ga_xdomain',
'Cross Domain Tracking',
'',
$this->section
);
add_settings_field(
'simple_ga_track_xdomain',
'Enable Cross Domain Tracking',
array(&$this, 'field_simple_ga_track_xdomain'),
$this->section,
'simple_ga_xdomain'
);
}
public function add_menu() {
// Add a page to manage this plugin's settings
add_options_page(
'Google Analytics',
'Tracking',
'manage_options',
$this->section,
array(&$this, 'settings_page')
);
/* Using registered $page handle to hook stylesheet loading */
add_action( 'admin_print_styles', array(&$this, 'admin_styles') );
add_action( 'admin_print_scripts', array(&$this, 'admin_scripts') );
} // END public function add_menu()
public function admin_styles() {
if(isset($_GET['page']) && $_GET['page'] == 'simple_ga') {
wp_enqueue_style('simple_ga_admin_styles');
}
}
public function admin_scripts() {
if(isset($_GET['page']) && $_GET['page'] == 'simple_ga') {
wp_enqueue_script('simple_ga_admin_scripts');
}
}
/**
*
* Fields
*
*/
// General Tracking
public function field_simple_ga_ua() {
$setting = get_option('simple_ga_ua');
print '<span>UA-</span><input type="text" name="simple_ga_ua" value="'.$setting.'" />';
}
public function field_simple_ga_dbl_click() {
$options = get_option('simple_ga_dbl_click');
$checked = ($options['toggle'] == 1 ? 'checked' : '' );
print '<input type="checkbox" '.$checked. ' name="simple_ga_dbl_click[toggle]" value="1" /> Alter my tracking script to support DoubleClick data.';
}
// Event Tracking
public function field_simple_ga_track_files() {
$options = get_option('simple_ga_track_files');
$checked = ($options['toggle'] == 1 ? 'checked' : '' );
$types = ($options['types'] == '' ? 'zip, pdf' : $options['types']);
$category = ($options['labels']['category'] == '' ? 'File' : $options['labels']['category']);
$action = ($options['labels']['action'] == '' ? 'Clicked Link' : $options['labels']['action']);
print '<input class="toggle" type="checkbox" '.$checked. ' name="simple_ga_track_files[toggle]" value="1" /> With this option checked, links pointing to files will be tracked with event tracking.';
print '<div class="simple-ga-dependant-fields">';
print '<div class="simple-ga-label"><strong>File Types to Track</strong></div>';
print '<input type="text" name="simple_ga_track_files[types]" value="'.$types.'" />';
print '<div class="simple-ga-label"><strong>Event Labels and Data</strong></div>';
print '<div class="sub-label">Category</div>';
print '<input type="text" name="simple_ga_track_files[labels][category]" value="'.$category.'" />';
print '<div class="sub-label">Action</div>';
print '<input type="text" name="simple_ga_track_files[labels][action]" value="'.$action.'" />';
print '<div class="sub-label">Label</div>';
print '<input type="text" name="simple_ga_track_files[labels][label]" value="'.$options['labels']['label'].'" />';
print '</div>';
}
public function field_simple_ga_track_external() {
$options = get_option('simple_ga_track_external');
$checked = ($options['toggle'] == 1 ? 'checked' : '' );
$category = ($options['labels']['category'] == '' ? 'Exit' : $options['labels']['category']);
$action = ($options['labels']['action'] == '' ? 'Clicked Link' : $options['labels']['action']);
print '<input class="toggle" type="checkbox" '.$checked. ' name="simple_ga_track_external[toggle]" value="1" /> With this option checked, external links will be tracked with event tracking.';
print '<div class="simple-ga-dependant-fields">';
print '<div class="simple-ga-label"><strong>Event Labels and Data</strong></div>';
print '<div class="sub-label">Category</div>';
print '<input type="text" name="simple_ga_track_external[labels][category]" value="'.$category.'" />';
print '<div class="sub-label">Action</div>';
print '<input type="text" name="simple_ga_track_external[labels][action]" value="'.$action.'" />';
print '<div class="sub-label">Label</div>';
print '<input type="text" name="simple_ga_track_external[labels][label]" value="'.$options['labels']['label'].'" />';
print '</div>';
}
// Cross Domain Tracking
public function field_simple_ga_track_xdomain() {
$options = get_option('simple_ga_track_xdomain');
$checked = ($options['toggle'] == 1 ? 'checked' : '' );
$allow_hash = ($options['allow_hash'] == 1 ? 'checked' : '' );
print '<input class="toggle" type="checkbox" '.$checked. ' name="simple_ga_track_xdomain[toggle]" value="1" /> With this option checked, you can track multiple domains as one. This setting may require advanced setup. Please refer to the documentation on Google\'s website.';
// Types of X Domain tracking
$choices = array('subdomains' => 'Multiple Subdomains',
'top_level' => 'Multiple Top Level Domains'
);
print '<div class="simple-ga-dependant-fields">';
print '<div class="simple-ga-label"><strong>What type of domain setup are you tracking?</strong></div>';
print '<select name="simple_ga_track_xdomain[type]">';
foreach($choices as $value => $label) {
$selected = ($options['type'] == $value ? 'selected' : '' );
print '<option type="select" '.$selected. ' value="'. $value .'" >'. $label .'</option>';
}
print '</select>';
print '<div class="simple-ga-label"><strong>Set setAllowHash to false</strong></div>';
print '<input class="toggle" type="checkbox" '.$allow_hash. ' name="simple_ga_track_xdomain[allow_hash]" value="1" /> This value (however, deprecated) must be set to false for tracking multiple top level domains.';
print '<div class="simple-ga-label"><strong>Settings for domain name <em>(_setDomainName)</em></strong> </div>';
print '<input type="text" name="simple_ga_track_xdomain[set_domain]" value="'.$options['set_domain'].'" /> Allowed values are "domain", "auto", and "none"';
print '</div>';
}
public function settings_page() {
// Global vars
global $simple_google_analytics, $wp_settings_sections, $wp_settings_fields;
//print '<pre>'; print_r($wp_settings_sections); print '</pre>';
// Array to pass to theme function
$data = array();
// Set our active tab - default to general
$active = (isset($_GET['tab']) ? $_GET['tab'] : 'simple_ga_general');
// We can store all our markup for the tabs navigation here
$nav_html = '';
// We'll use this to loop over our fields and sections for do_settings_sections
$sections = array();
// Loop over fields/sections for theme vars
foreach($wp_settings_fields['simple_ga'] as $section => $fields) {
// Populate nav markup for this section
$active_tab = ($section == $active ? 'nav-tab-active' : '');
$nav_html .= '<a class="nav-tab '. $active_tab .'" href="?page=simple_ga&tab='. $wp_settings_sections['simple_ga'][$section]['id'] .'">' . $wp_settings_sections['simple_ga'][$section]['title'] . '</a>';
// We only need the current section
if($active_tab != '') {
$data['section'] = $section;
}
}
// Get ready to render
$data['tabs'] = $nav_html;
// Render - we'll use the theme method in our main plugin file
print $simple_google_analytics->theme('views/settings/simple-ga.php', $data);
}
}
//Fire settings
global $GaGeneralSettings;
$GaGeneralSettings = new GaGeneralSettings();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment