'widget_google_stats', 'description' => __("Displays Stat Info From Google Analytics", 'google-analyticator') ); $control_ops = array('width' => 400, 'height' => 400); $this->WP_Widget('googlestats', __('Google Analytics Stats', 'google-analyticator'), $widget_ops, $control_ops); } function widget($args, $instance) { extract($args); $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']); $acnt = $instance['account']; $timeFrame = empty($instance['timeFrame']) ? '1' : $instance['timeFrame']; $pageBg = empty($instance['pageBg']) ? 'fff' : $instance['pageBg']; $widgetBg = empty($instance['widgetBg']) ? '999' : $instance['widgetBg']; $innerBg = empty($instance['innerBg']) ? 'fff' : $instance['innerBg']; $font = empty($instance['font']) ? '333' : $instance['font']; $line1 = empty($instance['line1']) ? 'Unique' : $instance['line1']; $line2 = empty($instance['line2']) ? 'Visitors' : $instance['line2']; # Before the widget echo $before_widget; # The title if ( $title ) echo $before_title . $title . $after_title; # Make the stats chicklet echo ''; $this->initiateBackground($pageBg, $font); $this->beginWidget($font, $widgetBg); $this->widgetInfo($this->getUniqueVisitors($acnt, $timeFrame), $line1, $line2, $innerBg, $font); $this->endWidget(); # After the widget echo $after_widget; } function update($new_instance, $old_instance) { $instance = $old_instance; $instance['title'] = strip_tags(stripslashes($new_instance['title'])); $instance['account'] = strip_tags(stripslashes($new_instance['account'])); $instance['timeFrame'] = strip_tags(stripslashes($new_instance['timeFrame'])); $instance['pageBg'] = strip_tags(stripslashes($new_instance['pageBg'])); $instance['widgetBg'] = strip_tags(stripslashes($new_instance['widgetBg'])); $instance['innerBg'] = strip_tags(stripslashes($new_instance['innerBg'])); $instance['font'] = strip_tags(stripslashes($new_instance['font'])); $instance['line1'] = strip_tags(stripslashes($new_instance['line1'])); $instance['line2'] = strip_tags(stripslashes($new_instance['line2'])); return $instance; } function form($instance) { //Defaults $instance = wp_parse_args( (array) $instance, array('title'=>'', 'account'=>'', 'timeFrame'=>'1', 'pageBg'=>'fff', 'widgetBg'=>'999', 'innerBg'=>'fff', 'font'=>'333', 'line1'=>'Unique', 'line2'=>'Visitors') ); $title = htmlspecialchars($instance['title']); $acnt = htmlspecialchars($instance['account']); $timeFrame = htmlspecialchars($instance['timeFrame']); $pageBg = htmlspecialchars($instance['pageBg']); $widgetBg = htmlspecialchars($instance['widgetBg']); $innerBg = htmlspecialchars($instance['innerBg']); $font = htmlspecialchars($instance['font']); $line1 = htmlspecialchars($instance['line1']); $line2 = htmlspecialchars($instance['line2']); $accounts = array(); # Get the current memory limit $current_mem_limit = substr(ini_get('memory_limit'), 0, -1); # Check if this limit is less than 96M, if so, increase it if ( $current_mem_limit < 96 || $current_mem_limit == '' ) { if ( function_exists('memory_get_usage') ) @ini_set('memory_limit', '96M'); } # Get the class for interacting with the Google Analytics require_once('class.analytics.stats.php'); # Create a new Gdata call $stats = new GoogleAnalyticsStats(); # Check if Google sucessfully logged in $login = $stats->checkLogin(); # Get a list of accounts $accounts = $stats->getAnalyticsAccounts(); # Output the options echo '

'; # The list of accounts echo '

'; # Time frame echo '

'; # Page background echo '

'; # Widget background echo '

'; # Inner background echo '

'; # Font color echo '

'; # Text line 1 echo '

'; # Text line 2 echo '

'; } /** * This function is used to display the background color behind the widget. This is necessary * for the Google Analytics text to have the same background color as the page. * * @param $font_color - Hexadecimal value for the font color used within the Widget (does not effect "Powered By Google Analytics Text"). This effects border color as well. * @param $page_background_color - Hexadecimal value for the page background color * @return void **/ function initiateBackground($page_background_color = 'FFF', $font_color = '000') { echo '
'; echo '
'; } /** * This function starts the widget. The font color and widget background color are customizable. * * @param $font_color - Hexadecimal value for the font color used within the Widget (does not effect "Powered By Google Analytics Text"). This effects border color as well. * @param $widget_background_color - Hexadecimal value for the widget background color. * @return void **/ function beginWidget($font_color = '000', $widget_background_color = 'FFF') { echo ''; } /** * This function encases the text that appears on the right hand side of the widget. * Both lines of text are customizable by each individual user. * * It also displays the visitor count that was pulled from the user's Google Analytics account. * * @param $visitor_count - Number of unique visits to the site pulled from the user's Google Analytics account. * @param $line_one - First line of text displayed on the right hand side of the widget. * @param $line_two - Second line of text displayed on the right hand side of the widget. * @param $inner_background_color - Hexadecimal value for the background color that surrounds the Visitor Count. * @param $font_color - Hexadecimal value for the font color used within the Widget (does not effect "Powered By Google Analytics Text"). This effects border color as well * @return void **/ function widgetInfo($visitor_count, $line_one = 'Unique', $line_two = 'Visitors', $inner_background_color = 'FFF', $font_color = '000') { echo ''; echo ''; } /** * The function is used strictly for visual appearance. It also displays the Google Analytics text. * * @return void **/ function endWidget() { // This closes off the widget. echo '
'. $visitor_count . '
' . $line_one . '
' . $line_two . '
'; // The following is used to displayed the "Powered By Google Anayltics" text. echo '
Powered By Google Analytics
'; } /** * Grabs the cached value of the unique visits for the previous day * * @param account - the account to get the unique visitors from * @param time - the amount of days to get * @return void **/ function getUniqueVisitors($account, $time = 1) { # Get the value from the database $visits = maybe_unserialize(get_option('google_stats_visits_' . $account)); # Check to make sure the timeframe is an int and greater than one $time = (int) $time; if ( $time < 1 ) $time = 1; # Check if the call has been made before if ( is_array($visits) ) { # Check if the last time called was within two hours, if so, return that if ( $visits['lastcalled'] > ( time() - 7200 ) ) return $visits['unique']; } # If here, the call has not been made or it is expired # Get the current memory limit $current_mem_limit = substr(ini_get('memory_limit'), 0, -1); # Check if this limit is less than 96M, if so, increase it if ( $current_mem_limit < 96 || $current_mem_limit == '' ) { if ( function_exists('memory_get_usage') ) @ini_set('memory_limit', '96M'); } # Get the class for interacting with the Google Analytics require_once('class.analytics.stats.php'); # Create a new Gdata call $stats = new GoogleAnalyticsStats(); # Set the account to the one requested $stats->setAccount($account); # Get the latest stats $before = date('Y-m-d', strtotime('-' . $time . ' days')); $yesterday = date('Y-m-d', strtotime('-1 day')); $uniques = number_format($stats->getMetric('ga:visitors', $before, $yesterday)); # Store the array update_option('google_stats_visits_' . $account, array('unique'=>$uniques, 'lastcalled'=>time())); # Return the visits return $uniques; } }// END class /** * Register Google Analytics Stat Widget. */ function GoogleStatsWidget_init() { register_widget('GoogleStatsWidget'); } add_action('widgets_init', 'GoogleStatsWidget_init'); ?>