Location: PHPKode > scripts > WP SlimStat > wp-slimstat/admin/wp-slimstat-custom-report-demo.php.txt
<?php
/*
Plugin Name: WP SlimStat Custom Reports
Plugin URI: http://www.duechiacchiere.it/wp-slimstat/
Description: This is not a real plugin, it just demonstrates how to add your custom reports to WP SlimStat.
Version: 1.2
Author: Camu
Author URI: http://www.duechiacchiere.it/
*/

// In order to activate this plugin, WP SlimStat needs to be installed and active
if (!in_array('wp-slimstat/wp-slimstat.php', get_option('active_plugins'))) return;

class wp_slimstat_custom_reports{

	// Function: _get_time_spent_on_site
	// Description: Fetches popular pages from the DB
	// Input: none
	// Output: array of results
	// Notes: wp_slimstat_view::$filters_parsed is an array containing all the filters set by the user through the dropdown menus
	//        Please refer to readme.txt for a list of filters and to learn how to leverage this information in your queries
	protected static function _get_time_spent_on_site(){
		$sql = "SELECT MIN(t1.dt) mindt, t1.ip, t1.user, t1.resource, t1.visit_id, MAX(t1.dt)-MIN(t1.dt) time_spent
				FROM ".wp_slimstat_db::$filters['sql_from']['all']."
				WHERE t1.ip <> 0 ".wp_slimstat_db::$filters['sql_where'].' '.wp_slimstat_db::$filters['date_sql_where']."
				GROUP BY t1.ip, t1.user, t1.resource, t1.visit_id
				HAVING time_spent <= 1800 AND time_spent > 0
				ORDER BY t1.dt DESC
				LIMIT ".wp_slimstat_db::$filters['parsed']['starting'][1].', '.wp_slimstat_db::$filters['parsed']['limit_results'][1];

		return $GLOBALS['wpdb']->get_results($sql, ARRAY_A);
	}
	// end _get_top_pages

	// Function: show_top_custom_pages
	// Description: Formats the results obtained through _get_top_pages
	// Input: none
	// Output: HTML code
	// Notes: wp_slimstat_boxes contains a few methods that standardize the HTML required to display a module
	public static function show_top_custom_pages() {
		$results = self::_get_time_spent_on_site();

		// Boxes come in three sizes: wide, medium, normal (default).
		wp_slimstat_boxes::box_header('my_custom_box_id', 'Add some extra explanation here', 'wide', false, '', 'Time Spent on page');

		foreach($results as $a_result){
			$clean_url = preg_replace('/\?.*/', '', $a_result['resource']);
			$clean_ip = long2ip($a_result['ip']);
			$time_spent = date('i:s', $a_result['time_spent']);
			echo "<p title='URL: $clean_url'>$clean_ip <span>$time_spent</span></p>";
		}
		wp_slimstat_boxes::box_footer(); // closes the DIV's open by box_header
	}
	// end show_top_pages
}
// end of class declaration

// Use the hook 'wp_slimstat_custom_report' to attach your reports to the panel
// Of course you can attach as many reports as you want :-)
add_action('wp_slimstat_custom_report', array('wp_slimstat_custom_reports', 'show_top_custom_pages'));
Return current item: WP SlimStat