mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2026-05-05 15:05:39 +02:00
initial stab at a simple popularity tracker for admin
This commit is contained in:
22
admin.php
22
admin.php
@@ -45,7 +45,8 @@ class AdminPlugin extends Plugin
|
||||
*/
|
||||
public static function getSubscribedEvents() {
|
||||
return [
|
||||
'onPluginsInitialized' => ['onPluginsInitialized', 1000]
|
||||
'onPluginsInitialized' => ['onPluginsInitialized', 1000],
|
||||
'onShutdown' => ['onShutdown', 1000]
|
||||
];
|
||||
}
|
||||
|
||||
@@ -155,6 +156,23 @@ class AdminPlugin extends Plugin
|
||||
}
|
||||
}
|
||||
|
||||
public function onShutdown()
|
||||
{
|
||||
if ($this->config->get('plugins.admin.popularity')) {
|
||||
require_once PLUGINS_DIR . 'admin/classes/popularity.php';
|
||||
$popularity = new Popularity();
|
||||
|
||||
// if in admin, try to flush data
|
||||
if ($this->active) {
|
||||
$popularity->flushData();
|
||||
|
||||
// else track data
|
||||
} else {
|
||||
$popularity->trackHit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function initializeAdmin()
|
||||
{
|
||||
$this->route = $this->config->get('plugins.admin.route');
|
||||
@@ -168,6 +186,7 @@ class AdminPlugin extends Plugin
|
||||
|
||||
// Only activate admin if we're inside the admin path.
|
||||
if (substr($this->uri->route(), 0, strlen($base)) == $base) {
|
||||
$this->active = true;
|
||||
$this->enable([
|
||||
'onPagesInitialized' => ['onPagesInitialized', 1000],
|
||||
'onPageInitialized' => ['onPageInitialized', 1000],
|
||||
@@ -199,6 +218,7 @@ class AdminPlugin extends Plugin
|
||||
|
||||
// And store the class into DI container.
|
||||
$this->grav['admin'] = $this->admin;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
enabled: true
|
||||
route: '/admin'
|
||||
popularity: true
|
||||
theme: grav
|
||||
|
||||
64
classes/popularity.php
Normal file
64
classes/popularity.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
namespace Grav\Plugin;
|
||||
|
||||
use Grav\Common\User\User;
|
||||
use Grav\Common\User\Authentication;
|
||||
use Grav\Common\Filesystem\File;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Plugins;
|
||||
use Grav\Common\Session;
|
||||
use Grav\Common\Themes;
|
||||
use Grav\Common\Uri;
|
||||
use Grav\Common\Page\Pages;
|
||||
use Grav\Common\Page\Page;
|
||||
use Grav\Common\Data;
|
||||
use Grav\Common\GravTrait;
|
||||
|
||||
class Popularity
|
||||
{
|
||||
use GravTrait;
|
||||
|
||||
protected $data_path;
|
||||
protected $data_file;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->data_path = LOG_DIR . 'popularity';
|
||||
$this->data_file = date('W-Y') . '.json';
|
||||
}
|
||||
|
||||
public function trackHit()
|
||||
{
|
||||
$data_filepath = $this->data_path.'/'.$this->data_file;
|
||||
$url = self::$grav['uri']->url();
|
||||
|
||||
|
||||
\Tracy\Debugger::log($data_filepath);
|
||||
|
||||
// initial creation if it doesn't exist
|
||||
if (!file_exists($this->data_path)) {
|
||||
mkdir($this->data_path);
|
||||
file_put_contents($data_filepath, array());
|
||||
}
|
||||
|
||||
// Get the JSON data
|
||||
$data = (array) @json_decode(file_get_contents($data_filepath), true);
|
||||
|
||||
if (array_key_exists($url, $data)) {
|
||||
$data[$url] = intval($data[$url]) + 1;
|
||||
} else {
|
||||
$data[$url] = 1;
|
||||
}
|
||||
|
||||
// Store the JSON data again
|
||||
file_put_contents($data_filepath, json_encode($data));
|
||||
|
||||
}
|
||||
|
||||
function flushData($weeks = 52)
|
||||
{
|
||||
// flush data older than 1 year
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user