Skip to content

Commit

Permalink
Overview in external API
Browse files Browse the repository at this point in the history
  • Loading branch information
ice committed Jun 3, 2016
1 parent 76c4920 commit aa7f180
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 10 deletions.
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -28,7 +28,10 @@
- [ ] Change the email notification activation control (according enabled/disabled)
- [ ] Add https://github.com/piwik/referrer-spam-blacklist
- [ ] Add jQuery Vector Maps v1.5.0
- [ ] Change overview loading: used the external api

## 1.2.3
*Released date: 2016-06-xx*
- [x] Change overview loading: used the external api


## 1.2.2
Expand Down
Binary file modified doc/external_api.pdf
Binary file not shown.
129 changes: 127 additions & 2 deletions includes/api/nsp_api_overview.php
@@ -1,7 +1,7 @@
<?php

#error_reporting(E_ALL);
#ini_set('display_errors', 1);
error_reporting(E_ALL);
ini_set('display_errors', 1);

/**
* API: Overview
Expand All @@ -16,6 +16,8 @@ function nsp_ApiOverview($typ, $par) {
global $wpdb;
global $nsp_option_vars;

$offsets = get_option($nsp_option_vars['stats_offsets']['name']);

$table_name = nsp_TABLENAME;

$since = nsp_ExpandVarsInsideCode('%since%');
Expand All @@ -40,9 +42,94 @@ function nsp_ApiOverview($typ, $par) {

// get result of dashboard as some date is shared with this
$resultJ=nsp_ApiDashboard("JSON");

$resultJ['days']=$gdays; // export


$overview_rows=array('visitors','visitors_feeds','pageview','feeds','spiders');

foreach ($overview_rows as $row) {

switch($row) {
case 'visitors' :
$row2='DISTINCT ip';
$row_title=__('Visitors','newstatpress');
$sql_QueryTotal="SELECT count($row2) AS $row FROM $table_name WHERE feed='' AND spider=''";
break;
case 'visitors_feeds' :
$row2='DISTINCT ip';
$row_title=__('Visitors through Feeds','newstatpress');
$sql_QueryTotal="SELECT count($row2) AS $row FROM $table_name WHERE feed<>'' AND spider='' AND agent<>''";
break;
case 'pageview' :
$row2='date';
$row_title=__('Pageviews','newstatpress');
$sql_QueryTotal="SELECT count($row2) AS $row FROM $table_name WHERE feed='' AND spider=''";
break;
case 'spiders' :
$row2='date';
$row_title=__('Spiders','newstatpress');
$sql_QueryTotal="SELECT count($row2) AS $row FROM $table_name WHERE feed='' AND spider<>''";
break;
case 'feeds' :
$row2='date';
$row_title=__('Pageviews through Feeds','newstatpress');
$sql_QueryTotal="SELECT count($row2) AS $row FROM $table_name WHERE feed<>'' AND spider=''";
break;
}

$resultJ[$row.'_total'] = $wpdb->get_row($sql_QueryTotal)->$row; // export
$resultJ[$row.'_tyear'] = $wpdb->get_row($sql_QueryTotal. " AND date LIKE '$thisyear%'")->$row; // export

switch($row) {
case 'visitors' :
$resultJ[$row.'_total']+=$offsets['alltotalvisits'];
break;
case 'visitors_feeds' :
$resultJ[$row.'_total']+=$offsets['visitorsfeeds'];
break;
case 'pageview' :
$resultJ[$row.'_total']+=$offsets['pageviews'];
break;
case 'spiders' :
$resultJ[$row.'_total']+=$offsets['spy'];
break;
case 'feeds' :
$resultJ[$row.'_total']+=$offsets['pageviewfeeds'];
break;
}

}

// make graph

$maxxday = 0;
for($gg=$gdays-1;$gg>=0;$gg--) {

$date=gmdate('Ymd', current_time('timestamp')-86400*$gg);

$qry_visitors = $wpdb->get_row("SELECT count(DISTINCT ip) AS total FROM $table_name WHERE feed='' AND spider='' AND date = '$date'");
$visitors[$gg] = $qry_visitors->total;

$qry_pageviews = $wpdb->get_row("SELECT count(date) AS total FROM $table_name WHERE feed='' AND spider='' AND date = '$date'");
$pageviews[$gg]= $qry_pageviews->total;

$qry_spiders = $wpdb->get_row("SELECT count(date) AS total FROM $table_name WHERE feed='' AND spider<>'' AND date = '$date'");
$spiders[$gg] = $qry_spiders->total;

$qry_feeds = $wpdb->get_row("SELECT count(date) AS total FROM $table_name WHERE feed<>'' AND spider='' AND date = '$date'");
$feeds[$gg] = $qry_feeds->total;

$total= $visitors[$gg] + $pageviews[$gg] + $spiders[$gg] + $feeds[$gg];
if ($total > $maxxday) $maxxday= $total;
}
if($maxxday == 0) { $maxxday = 1; }

$resultJ['visitors'] = $visitors; //export
$resultJ['pageviews'] = $pageviews; //export
$resultJ['spiders'] = $spiders; //export
$resultJ['feeds'] = $feeds; //export
$resultJ['max'] = $maxxday; // export

// output an HTML representation of the collected data

Expand Down Expand Up @@ -83,6 +170,8 @@ function nsp_ApiOverview($typ, $par) {

// build full current row
$overview_table.="<tr><td class='row_title $row'>".$resultJ[$row.'_title']."</td>";
$overview_table.="<td class='colc'>".$resultJ[$row.'_total']."</td>\n";
$overview_table.="<td class='colc'>".$resultJ[$row.'_tyear']."</td>\n";
$overview_table.="<td class='colc'>".$resultJ[$row.'_lmonth']."</td>\n";
$overview_table.="<td class='colr'>".$resultJ[$row.'_tmonth'].$result[0] ."</td>\n";
$overview_table.="<td class='colr'> $result[1] $result[2] </td>\n";
Expand All @@ -92,6 +181,42 @@ function nsp_ApiOverview($typ, $par) {
}

$overview_table.="</tr></table>";

$start_of_week = get_option('start_of_week');
$gd=(90/$gdays).'%';

$overview_graph="<table class='graph'><tr>";

for($gg=$gdays-1;$gg>=0;$gg--) {

$scale_factor=2; //2 : 200px in CSS

$date=gmdate('Ymd', current_time('timestamp')-86400*$gg);

$px_visitors = $scale_factor*(round($resultJ['visitors'][ $gg]*100/$maxxday));
$px_pageviews= $scale_factor*(round($resultJ['pageviews'][$gg]*100/$maxxday));
$px_spiders = $scale_factor*(round($resultJ['spiders'][$gg]*100/$maxxday));
$px_feeds = $scale_factor*(round($resultJ['feeds'][$gg]*100/$maxxday));

$px_white = $scale_factor*100 - $px_feeds - $px_spiders - $px_pageviews - $px_visitors;

$overview_graph.="<td width='$gd' valign='bottom'>";

$overview_graph.="<div class='overview-graph'>
<div style='border-left:1px; background:#ffffff;width:100%;height:".$px_white."px;'></div>
<div class='visitors_bar' style='height:".$px_visitors."px;' title='".$resultJ['visitors'][$gg]." ".__('Visitors','newstatpress')."'></div>
<div class='web_bar' style='height:".$px_pageviews."px;' title='".$resultJ['pageviews'][$gg]." ".__('Pageviews','newstatpress')."'></div>
<div class='spiders_bar' style='height:".$px_spiders."px;' title='".$resultJ['spiders'][$gg]." ".__('Spiders','newstatpress')."'></div>
<div class='feeds_bar' style='height:".$px_feeds."px;' title='".$resultJ['feeds'][$gg]." ".__('Feeds','newstatpress')."'></div>
<div style='background:gray;width:100%;height:1px;'></div>";
if($start_of_week == gmdate('w',current_time('timestamp')-86400*$gg)) $overview_graph.="<div class='legend-W'>";
else $overview_graph.="<div class='legend'>";
$overview_graph.=gmdate('d', current_time('timestamp')-86400*$gg) . ' ' . gmdate('M', current_time('timestamp')-86400*$gg) . "</div></div></td>\n";
}
$overview_graph.="</tr></table>";

$overview_table=$overview_table.$overview_graph;


$resultH=$overview_table;
return $resultH;
Expand Down
26 changes: 22 additions & 4 deletions includes/nsp_overview.php
Expand Up @@ -377,7 +377,25 @@ function nsp_NewStatPressMain() {
<div id="post-body" class="metabox-holder columns-1">

<div id="post-body-content">
<?php nsp_MakeOverview('main'); ?>
<?php

echo "<div class='wrap'><h2>". __('Overview','newstatpress'). "</h2>";

$api_key=get_option('newstatpress_apikey');
$_newstatpress_url=PluginUrl();
$url=$_newstatpress_url."/includes/api/external.php";

wp_register_script('wp_ajax_nsp_js_overview', plugins_url('./js/nsp_overview.js', __FILE__), array('jquery'));
wp_enqueue_script('jquery');
wp_enqueue_script('wp_ajax_nsp_js_overview');
wp_localize_script( 'wp_ajax_nsp_js_overview', 'ExtData', array(
'Url' => $url,
'Key' => md5(gmdate('m-d-y H i').$api_key)
));

echo "<div id=\"nsp_result-overview\"><img id=\"nsp_loader-overview\" src=\"$_newstatpress_url/images/ajax-loader.gif\"></div>";

?>
</div>

<div id="postbox-container-1" class="postbox-container">
Expand All @@ -402,7 +420,7 @@ function nsp_NewStatPressMain3() {

global $newstatpress_dir;

/*

echo "<div class='wrap'><h2>". __('Overview','newstatpress'). "</h2>";

$api_key=get_option('newstatpress_apikey');
Expand All @@ -418,9 +436,9 @@ function nsp_NewStatPressMain3() {
));

echo "<div id=\"nsp_result-overview\"><img id=\"nsp_loader-overview\" src=\"$_newstatpress_url/images/ajax-loader.gif\"></div>";
*/


nsp_MakeOverview('main');
/// nsp_MakeOverview('main');
$_newstatpress_url=PluginUrl();


Expand Down
4 changes: 2 additions & 2 deletions newstatpress.php
Expand Up @@ -4,7 +4,7 @@
Plugin URI: http://newstatpress.altervista.org
Text Domain: newstatpress
Description: Real time stats for your Wordpress blog
Version: 1.2.2
Version: 1.2.3
Author: Stefano Tognon and cHab (from Daniele Lippi works)
Author URI: http://newstatpress.altervista.org
************************************************************/
Expand All @@ -16,7 +16,7 @@
die(__('ERROR: This plugin requires WordPress and will not function if called directly.','newstatpress'));
}

$_NEWSTATPRESS['version']='1.2.2';
$_NEWSTATPRESS['version']='1.2.3';
$_NEWSTATPRESS['feedtype']='';

global $newstatpress_dir,
Expand Down
7 changes: 6 additions & 1 deletion readme.txt
Expand Up @@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_i
Tags: stats,statistics,widget,admin,sidebar,visits,visitors,pageview,user,agent,referrer,post,posts,spy,statistiche,ip2nation,country
Requires at least: 3.5
Tested up to: 4.4.1
Stable Tag: 1.2.2
Stable Tag: 1.2.3

NewStatPress (Statpress plugin fork) is a real-time plugin to manage the visits' statistics about your blog (without external web analytics).

Expand Down Expand Up @@ -148,6 +148,11 @@ screenshot-4.jpg

== Changelog ==

= 1.2.3 =
*Released date: 2016-06-xx*

* Change overview loading: used the external api

= 1.2.2 =
*Released date: 2016-05-28*

Expand Down

0 comments on commit aa7f180

Please sign in to comment.