Skip to content

Instantly share code, notes, and snippets.

@tomjn
Last active December 16, 2015 22:49
Show Gist options
  • Select an option

  • Save tomjn/5510127 to your computer and use it in GitHub Desktop.

Select an option

Save tomjn/5510127 to your computer and use it in GitHub Desktop.
A plugin that extends the auditor to track who in the Interconnect/IT offices has the most twitter users
<?php
/*
Plugin Name: Twitter Follower Audit Tracker
Plugin URI: http://interconnectit.com/products/the-auditor/
Description: Uses the auditor to track twitter follower counts, requires v1.63+
Author: Tom J Nowell, interconnect/it
Version: 1.1
Author URI: http://interconnectit.com
*/
add_action( 'init', 'tjn_twitter_audit_setup' );
function tjn_twitter_audit_setup() {
if ( class_exists( 'ICIT_Log_Cron_Task' ) && !class_exists( 'TJN_Log_Twitter' ) ) {
// ICIT_Log_Cron_Task can be used to log things at regular intervals, simply extend it and override the cron_task method with your own logging code
class TJN_Log_Twitter extends ICIT_Log_Cron_Task {
public function cron_task() {
// if we have data and it isnt empty
if ( is_array( $this->data ) ) {
if ( !empty( $this->data ) ) {
// our data here is an array of account names, so for each account
foreach ( $this->data as $account ) {
// ask twitter for general account information
$twitter_followers = wp_remote_get( 'http://api.twitter.com/1/statuses/user_timeline.xml?count=2&amp;screen_name='.$account );
if ( ! is_wp_error( $twitter_followers ) ) {
// break apart the XML response
$xml = new SimpleXmlElement( $twitter_followers['body'], LIBXML_NOCDATA );
// extract the follower count field
$count = $xml->status->user->followers_count;
$count = intval( $count ); // ensure it's treated as a number not a string else it'll be json encoded as a string
// the helper object stores a reference to the logging object provided to it
// Here we pass in null for the user ID and time, it'll auto-figure those out
// we'll also pass in the twitter account as the log entry subject
$this->log->add( 'twitter_count', $count, null, null, $account );
}
}
}
}
}
}
// grab the main logger container, and use it to create our logger cron task
$auditlogger = ICITAuditLogger::get_logger();
$twitter_cron = new TJN_Log_Twitter(
$auditlogger->primarylog, // log to the primary default log
'daily', // once a day
'log_twitter_follower_count', // using this unique ID to generate the cron hook
array( 'tarendai', 'sanchothefat', 'lauriechandler', 'davecoveney', 'anthony_casey' ) // and passing this data for use later on
);
}
}
// Lets put the follower counts on the overview page in a chart!
add_action( 'icit_audit_log_add_overview_widgets', 'twitter_follower_chart', 100 );
// Lets put the follower counts on the admin dashboard in a chart!
//add_action( 'wp_dashboard_setup', 'twitter_follower_chart', 100 );
// $overview_id is the ID of the overview page. Ofcourse you could use the admin_init hook
// and put the chart on your admin dashboard instead
function twitter_follower_chart( $overview_id ) {
// we need to pass in a list of lines to show on our chart. Each account needs its own data line
$lines = array();
// here's our list of twitter users again
$accounts = array( 'tarendai', 'sanchothefat', 'lauriechandler', 'davecoveney', 'anthony_casey' );
// so for each account
foreach ( $accounts as $account ) {
// add to the list of lines an array of parameters
// all the parameters for ICIT_Log_Query are supported and passed through, the rest are graph specific
$lines[] = array(
'type' => 'twitter_count', // log event type
'subject' => $account, // the subject, which we're storing our twitter ID in
'legend_label' => $account, // this appears in the legend box
'name_plural' => $account, // for future use
'name' => $account, // used to identify the line in JS behind the scenes
'colour_label' => $account, // the colour of the line will be generated based on this
'continuous' => true, // if this is false, days that dont have a follower count will be set to zero, and you'll get a spikey graph
'collect' => false // if we dont set this to false, we'll be told how many times the log entry occurred, not value of the log entry
);
}
// create our chart widget
$widget = new ICIT_Audit_Overview_Multiple_Line_Chart_Widget( 'overview_twitter', 'Twitter Followers', 'side' );
// tell it what lines it's going to display
$widget->setDataArgs( $lines );
// add it to the overview page
$widget->add_to( $overview_id, 'side' );
/*
// show on the main WordPress dashboard
$widget->tabs = false;
// add it to the overview page
$widget->add_to( 'dashboard', 'normal' );*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment