mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2025-11-16 02:06:08 +01:00
Add visitors support and refactored
This commit is contained in:
@@ -163,10 +163,9 @@ class AdminPlugin extends Plugin
|
|||||||
require_once PLUGINS_DIR . 'admin/classes/popularity.php';
|
require_once PLUGINS_DIR . 'admin/classes/popularity.php';
|
||||||
$popularity = new Popularity();
|
$popularity = new Popularity();
|
||||||
|
|
||||||
// if in admin, try to flush data
|
// if in admin, flush old monthly data
|
||||||
if ($this->active) {
|
if ($this->active) {
|
||||||
$popularity->flushData();
|
$popularity->flushMonthly();
|
||||||
|
|
||||||
// else track data
|
// else track data
|
||||||
} else {
|
} else {
|
||||||
$popularity->trackHit();
|
$popularity->trackHit();
|
||||||
|
|||||||
@@ -2,5 +2,6 @@ enabled: true
|
|||||||
route: '/admin'
|
route: '/admin'
|
||||||
popularity:
|
popularity:
|
||||||
enabled: true
|
enabled: true
|
||||||
ignore: ['/test/*','/modular']
|
ignore: ['/test*','/modular']
|
||||||
|
visitors: 20
|
||||||
theme: grav
|
theme: grav
|
||||||
|
|||||||
@@ -18,23 +18,37 @@ class Popularity
|
|||||||
{
|
{
|
||||||
use GravTrait;
|
use GravTrait;
|
||||||
|
|
||||||
|
|
||||||
|
protected $config;
|
||||||
protected $data_path;
|
protected $data_path;
|
||||||
protected $data_file;
|
|
||||||
|
protected $monthly_file;
|
||||||
|
protected $totals_file;
|
||||||
|
protected $visitors_file;
|
||||||
|
|
||||||
|
protected $monthly_data;
|
||||||
|
protected $totals_data;
|
||||||
|
protected $visitors_data;
|
||||||
|
|
||||||
const MONTHLY_FILE = 'monthly.json';
|
const MONTHLY_FILE = 'monthly.json';
|
||||||
const TOTALS_FILE = 'totals.json';
|
const TOTALS_FILE = 'totals.json';
|
||||||
|
const VISITORS_FILE = 'visitors.json';
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
|
$this->config = self::$grav['config'];
|
||||||
|
|
||||||
$this->data_path = LOG_DIR . 'popularity';
|
$this->data_path = LOG_DIR . 'popularity';
|
||||||
$this->data_file = date('W-Y') . '.json';
|
$this->monthly_file = $this->data_path.'/'.self::MONTHLY_FILE;
|
||||||
|
$this->totals_file = $this->data_path.'/'.self::TOTALS_FILE;
|
||||||
|
$this->visitors_file = $this->data_path.'/'.self::VISITORS_FILE;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function trackHit()
|
public function trackHit()
|
||||||
{
|
{
|
||||||
$page = self::$grav['page'];
|
$page = self::$grav['page'];
|
||||||
$config = self::$grav['config'];
|
$relative_url = str_replace($this->config->get('system.base_url_relative'), '', $page->url());
|
||||||
$relative_url = str_replace($config->get('system.base_url_relative'), '', $page->url());
|
|
||||||
|
|
||||||
// Don't track error pages or pages that have no route
|
// Don't track error pages or pages that have no route
|
||||||
if ($page->template() == 'error' || !$page->route()) {
|
if ($page->template() == 'error' || !$page->route()) {
|
||||||
@@ -42,60 +56,118 @@ class Popularity
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Make sure no 'widcard-style' ignore matches this url
|
// Make sure no 'widcard-style' ignore matches this url
|
||||||
foreach ((array) self::$grav['config']->get('plugins.admin.popularity.ignore') as $ignore) {
|
foreach ((array) $this->config->get('plugins.admin.popularity.ignore') as $ignore) {
|
||||||
if (fnmatch($ignore, $relative_url)) {
|
if (fnmatch($ignore, $relative_url)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used more than once, so make a variable!
|
|
||||||
$monthly_file = $this->data_path.'/'.self::MONTHLY_FILE;
|
|
||||||
$totals_file = $this->data_path.'/'.self::TOTALS_FILE;
|
|
||||||
|
|
||||||
// initial creation if it doesn't exist
|
// initial creation if it doesn't exist
|
||||||
if (!file_exists($this->data_path)) {
|
if (!file_exists($this->data_path)) {
|
||||||
mkdir($this->data_path);
|
mkdir($this->data_path);
|
||||||
file_put_contents($monthly_file, array());
|
file_put_contents($this->monthly_file, array());
|
||||||
file_put_contents($totals_file, array());
|
file_put_contents($this->totals_file, array());
|
||||||
|
file_put_contents($this->visitors_file, array());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the data we want to track
|
// Update the data we want to track
|
||||||
$this->updateMonthly($monthly_file);
|
$this->updateMonthly();
|
||||||
$this->updateTotals($totals_file, $relative_url);
|
$this->updateTotals($page->route());
|
||||||
|
$this->updateVisitors(self::$grav['uri']->ip());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function flushData($weeks = 52)
|
protected function updateMonthly()
|
||||||
{
|
{
|
||||||
// flush data older than 1 year
|
|
||||||
|
|
||||||
|
|
||||||
|
if (!$this->monthly_data) {
|
||||||
|
$this->monthly_data = $this->getData($this->monthly_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function updateMonthly($path)
|
|
||||||
{
|
|
||||||
$data = (array) @json_decode(file_get_contents($path), true);
|
|
||||||
$month_year = date('m-Y');
|
$month_year = date('m-Y');
|
||||||
|
|
||||||
if (array_key_exists($month_year, $data)) {
|
// get the monthly access count
|
||||||
$data[$month_year] = intval($data[$month_year]) + 1;
|
if (array_key_exists($month_year, $this->monthly_data)) {
|
||||||
|
$this->monthly_data[$month_year] = intval($this->monthly_data[$month_year]) + 1;
|
||||||
} else {
|
} else {
|
||||||
$data[$month_year] = 1;
|
$this->monthly_data[$month_year] = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
file_put_contents($path, json_encode($data));
|
file_put_contents($this->monthly_file, json_encode($this->monthly_data));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function updateTotals($path, $url)
|
protected function updateTotals($url)
|
||||||
{
|
{
|
||||||
$data = (array) @json_decode(file_get_contents($path), true);
|
if (!$this->totals_data) {
|
||||||
|
$this->totals_data = $this->getData($this->totals_file);
|
||||||
|
}
|
||||||
|
|
||||||
if (array_key_exists($url, $data)) {
|
// get the totals for this url
|
||||||
$data[$url] = intval($data[$url]) + 1;
|
if (array_key_exists($url, $this->totals_data)) {
|
||||||
|
$this->totals_data[$url] = intval($this->totals_data[$url]) + 1;
|
||||||
} else {
|
} else {
|
||||||
$data[$url] = 1;
|
$this->totals_data[$url] = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
file_put_contents($path, json_encode($data));
|
file_put_contents($this->totals_file, json_encode($this->totals_data));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function updateVisitors($ip)
|
||||||
|
{
|
||||||
|
if (!$this->visitors_data) {
|
||||||
|
$this->visitors_data = $this->getData($this->visitors_file);
|
||||||
|
}
|
||||||
|
|
||||||
|
$count = intval($this->config->get('plugins.admin.popularity.visitors', 20));
|
||||||
|
|
||||||
|
// update with current timestamp
|
||||||
|
$this->visitors_data[$ip] = time();
|
||||||
|
|
||||||
|
$visitors = $this->visitors_data;
|
||||||
|
arsort($visitors);
|
||||||
|
|
||||||
|
$this->visitors_data = array_slice($visitors, 0, $count);
|
||||||
|
|
||||||
|
file_put_contents($this->visitors_file, json_encode($this->visitors_data));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getData($path)
|
||||||
|
{
|
||||||
|
return (array) @json_decode(file_get_contents($path), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function flushMonthly($months = 12)
|
||||||
|
{
|
||||||
|
// flush data older than 1 year
|
||||||
|
if (!$this->monthly_data) {
|
||||||
|
$this->monthly_data = $this->getData($this->monthly_file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there are more than $months worth of data remove the old
|
||||||
|
if (count($this->monthly_data) > $months) {
|
||||||
|
$new_monthly = array();
|
||||||
|
for ($x = 0; $x < intval($months); $x++) {
|
||||||
|
$date = date('m-Y', strtotime("now - $x month"));
|
||||||
|
if (isset($this->monthly_data[$date])) {
|
||||||
|
$new_monthly[$date] = $this->monthly_data[$date];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->monthly_data = $new_monthly;
|
||||||
|
file_put_contents($this->monthly_file, json_encode($this->monthly_data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function flushTotals()
|
||||||
|
{
|
||||||
|
// flush all totals
|
||||||
|
file_put_contents($this->totals_file, json_encode(array()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function flushVisitors()
|
||||||
|
{
|
||||||
|
// flush all the visitor data
|
||||||
|
file_put_contents($this->visitors_file, json_encode(array()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user