Location: PHPKode > scripts > Social Media Tracking > social-media-tracking/xlii-social-media-tracking.php
<?php
/* 
Plugin Name: 42functions Social Media Tracking
Plugin URI: http://www.42functions.nl
Version: v1.0.1
Author: 42functions
Description: Plugin used to track social media events with google analytics. 
*/

// 42functions author: Ferdy Perdaan (hide@address.com)

add_filter('wp_print_styles', 'xlii_smt_run');

function xlii_smt_run()
{
    if(count($active = get_option('xlii_smt_active')))
    {    
        // Register all javascript files
        wp_register_script('xlii_js_facebook', 'http://connect.facebook.net/en_US/all.js#xfbml=1');
        wp_register_script('xlii_js_twitter', 'http://platform.twitter.com/widgets.js');
        wp_register_script('xlii_js_google', 'http://apis.google.com/js/plusone.js');

        // Enable main css and tracking file
        $base = substr(dirname(__FILE__), strlen(ABSPATH) - 1);
                
        wp_enqueue_style('xlii_smt_sheet', ($sheet = get_option('xlii_smt_css')) ? $sheet : $base.'/xlii_social.css');
        wp_enqueue_script('ga_social_media', $base.'/ga_social_tracking.js', array(), false, true);
        
        // Enable additional files
        foreach($active as $type)
            wp_enqueue_script('xlii_js_'.$type);

        add_filter('the_content', 'xlii_smt_display');
        add_filter('the_excerpt', 'xlii_smt_display');  
    }    
}

function xlii_smt_display($content = '')
{
    foreach(get_option('xlii_smt_enabled') as $key => $enabled)
    {
        $method = 'is_'.$key;
        if($enabled && $method())
        {
            $html = array();
            foreach(get_option('xlii_smt_active') as $button)
            {
                switch($button)
                {
                    case 'twitter':
                        $html[] = '<li class = "'.$button.'"><a href="http://twitter.com/share" class="twitter-share-button">Tweet</a></li>';
                        break;
                    case 'google':
                        $html[] = '<li class = "'.$button.'"><g:plusone></g:plusone></li>';
                        break;
                    case 'facebook':
                        $html[] = '<li class = "'.$button.'"><fb:like send="true" layout="button_count" width="400" show_faces="true"></fb:like></li>';
                        break;
                }
            }
            
            if(count($html))
            {
                $html     = '<ul class = "xlii_smt">'.implode('', $html).'</ul>';
                $content .= is_feed() ? strip_tags($html, '<a><img>') . '<br/><br/>' : $html;
            
                return $content;
            }
        }
    }
    return $content;
}

// Plugin option support ----------------------------------------------------------------------------------------------------------------------------
register_activation_hook( __FILE__, 'xlii_smt_restore_options');

function xlii_smt_restore_options()
{
    update_option('xlii_smt_active', array('facebook', 'twitter', 'google'));
    update_option('xlii_smt_css', '');
    update_option('xlii_smt_enabled', array(
        'home' => false,
        'single' => true,
        'page' => true,
        'category' => false,
        'tag' => false,
        'date' => false,
        'search' => false,
        'author' => false,
        'feed' => false
    ));
}

function xlii_smt_update_options($options)
{   
    update_option('xlii_smt_active', array_keys($options['active']));
    update_option('xlii_smt_css', isset($options['css']) ? $options['css'] : '');
    update_option('xlii_smt_enabled', array(
        'home' => isset($options['enabled']['home']),
        'single' => isset($options['enabled']['single']),
        'page' => isset($options['enabled']['page']),
        'category' => isset($options['enabled']['category']),
        'tag' => isset($options['enabled']['tag']),
        'date' => isset($options['enabled']['date']),
        'search' => isset($options['enabled']['search']),
        'author' => isset($options['enabled']['author']),
        'feed' => isset($options['enabled']['feed'])
    ));
}

// Plugin admin pages   -----------------------------------------------------------------------------------------------------------------------------
add_action('admin_menu', 'xlii_smt_adminmenu');

function xlii_smt_adminmenu()
{
    add_options_page('42functions Social Media Tracking', 'Social Media Tracking', 9, __FILE__, 'xlii_smt_adminpage');
}

function xlii_smt_adminpage()
{
    if($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        if(isset($_POST['restore']))
            xlii_smt_restore_options();
        else
            xlii_smt_update_options($_POST);
    }
?>
    <form action="<?php echo attribute_escape( $_SERVER['REQUEST_URI'] ); ?>" method="post">
    <div class="wrap">
    	<?php screen_icon(); ?>
    	<h2><?php _e('Social Media Tracking', 'xlii_smt'); ?></h2>
    	<table class="form-table">
    	<tr>
    		<th>
    			<?php _e('Buttons', 'xlii_smt'); ?>:<br/>
    		</th>
    		<td>    
    			<?php 
    			    _e('Check the buttons you want to appear on your site.', 'sociable');
    			    echo '<br />';
    			    $buttons = get_option('xlii_smt_active');
    			?>
    			<ul id="xlii_smt_site_list">
        			<li><input type="checkbox" name="active[facebook]" id = "xlii_button_facebook" <?php checked(in_array('facebook', $buttons)); ?> /> <label for = "xlii_button_facebook"><?php _e('Facebook', 'xlii_smt'); ?></label></li>
            		<li><input type="checkbox" name="active[twitter]" id = "xlii_button_twitter" <?php checked(in_array('twitter', $buttons)); ?> /> <label for = "xlii_button_twitter"><?php _e('Twitter', 'xlii_smt'); ?></label></li>
                	<li><input type="checkbox" name="active[google]" id = "xlii_button_google" <?php checked(in_array('google', $buttons)); ?> /> <label for = "xlii_button_google"><?php _e('Google+', 'xlii_smt'); ?></label></li>
    			</ul>
    			</div>
    			<input type="hidden" id="site_order" name="site_order" value="<?php /*echo join('|', array_keys($sociable_known_sites))*/ ?>" />
    		</td>
    	</tr>	
    	<tr>
    		<th scope="row" valign="top">
    			<?php _e('Position:', 'xlii_smt'); ?>
    		</th>
    		<td>
    			<?php 
    			    _e('The icons appear at the end of each blog post, and posts may show on many different types of pages. Depending on your theme and audience, it may be tacky to display icons on all types of pages.', 'xlii_smt');
    			    echo '<br />';
    			    $enabled = get_option('xlii_smt_enabled');
    			?>
    			<ul>
    			    <li><input type="checkbox" name="enabled[home]" id = "xlii_smt_enabled_home" <?php checked($enabled['home']); ?> /> <label for = "xlii_smt_enabled_home"><?php _e('Front page of the blog', 'xlii_smt'); ?></label></li>
    			    <li><input type="checkbox" name="enabled[single]" id = "xlii_smt_enabled_single" <?php checked($enabled['single']); ?> /> <label for = "xlii_smt_enabled_single"><?php _e('Individual blog posts', 'xlii_smt'); ?></label></li>
    			    <li><input type="checkbox" name="enabled[page]" id = "xlii_smt_enabled_page" <?php checked($enabled['page']); ?> /> <label for = "xlii_smt_enabled_page"><?php _e('Individual WordPress "Pages"', 'xlii_smt'); ?></label></li>
    			    <li><input type="checkbox" name="enabled[category]" id = "xlii_smt_enabled_category" <?php checked($enabled['category']); ?> /> <label for = "xlii_smt_enabled_category"><?php _e('Category archives', 'xlii_smt'); ?></label></li>
    			    <li><input type="checkbox" name="enabled[tag]" id = "xlii_smt_enabled_tag" <?php checked($enabled['tag']); ?> /> <label for = "xlii_smt_enabled_tag"><?php _e('Tag listings', 'xlii_smt'); ?></label></li>
    			    <li><input type="checkbox" name="enabled[date]" id = "xlii_smt_enabled_date" <?php checked($enabled['date']); ?> /> <label for = "xlii_smt_enabled_date"><?php _e('Date-based archives', 'xlii_smt'); ?></label></li>
    			    <li><input type="checkbox" name="enabled[author]" id = "xlii_smt_enabled_author" <?php checked($enabled['author']); ?> /> <label for = "xlii_smt_enabled_author"><?php _e('Author archives', 'xlii_smt'); ?></label></li>
    			    <li><input type="checkbox" name="enabled[search]" id = "xlii_smt_enabled_search" <?php checked($enabled['search']); ?> /> <label for = "xlii_smt_enabled_search"><?php _e('Search results', 'xlii_smt'); ?></label></li>
    			    <li><input type="checkbox" name="enabled[feed]" id = "xlii_smt_enabled_feed" <?php checked($enabled['feed']); ?> /> <label for = "xlii_smt_enabled_feed"><?php _e('RSS feed items', 'xlii_smt'); ?></label></li>
    		    </ul>
    		</td>
    	</tr>
    	<tr>
    		<th scope="row" valign="top">
    			<?php _e('Alternative style cheet:', 'xlii_smt'); ?>
    		</th>
    		<td>
    			<input type="text" name="css" size = "80" value ="<?php echo attribute_escape(stripslashes(get_option('xlii_smt_css'))); ?>" />
    		</td>
    	</tr>
    	<tr>
    		<td>&nbsp;</td>
    		<td>
    			<span class="submit"><input name="save" value="<?php _e('Save Changes', 'xlii_smt'); ?>" type="submit" /></span>
    			<span class="submit"><input name="restore" value="<?php _e('Restore defaults', 'xlii_smt'); ?>" type="submit"/></span>
    		</td>
    	</tr>
    </table>
    </form>
<?php
}
Return current item: Social Media Tracking