mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2025-11-04 20:36:03 +01:00
initial feed/notification refactors
This commit is contained in:
@@ -19,9 +19,9 @@ use Grav\Common\Uri;
|
||||
use Grav\Common\User\User;
|
||||
use Grav\Common\Utils;
|
||||
use Grav\Framework\Collection\ArrayCollection;
|
||||
use Grav\Plugin\Admin\Twig\AdminTwigExtension;
|
||||
use Grav\Plugin\Login\Login;
|
||||
use Grav\Plugin\Login\TwoFactorAuth\TwoFactorAuth;
|
||||
use PicoFeed\Parser\MalformedXmlException;
|
||||
use RocketTheme\Toolbox\Event\Event;
|
||||
use RocketTheme\Toolbox\File\File;
|
||||
use RocketTheme\Toolbox\File\JsonFile;
|
||||
@@ -1333,72 +1333,202 @@ class Admin
|
||||
$this->permissions = array_merge($this->permissions, $permissions);
|
||||
}
|
||||
|
||||
public function processNotifications($notifications)
|
||||
public function getNotifications($force = false)
|
||||
{
|
||||
// Sort by date
|
||||
usort($notifications, function ($a, $b) {
|
||||
return strcmp($a->date, $b->date);
|
||||
});
|
||||
$last_checked = null;
|
||||
$filename = $this->grav['locator']->findResource('user://data/notifications/' . md5($this->grav['user']->username) . YAML_EXT, true, true);
|
||||
|
||||
$notifications = array_reverse($notifications);
|
||||
$notifications_file = CompiledYamlFile::instance($filename);
|
||||
$notifications_content = (array)$notifications_file->content();
|
||||
|
||||
// Make adminNicetimeFilter available
|
||||
require_once __DIR__ . '/../classes/Twig/AdminTwigExtension.php';
|
||||
$adminTwigExtension = new AdminTwigExtension;
|
||||
$last_checked = $notifications_content['last_checked'] ?? null;
|
||||
$notifications = $notifications_content['data'] ?? array();
|
||||
$timeout = $this->grav['config']->get('system.session.timeout', 1800);
|
||||
|
||||
$filename = $this->grav['locator']->findResource('user://data/notifications/' . $this->grav['user']->username . YAML_EXT,
|
||||
true, true);
|
||||
$read_notifications = (array)CompiledYamlFile::instance($filename)->content();
|
||||
if ($force || !$last_checked || empty($notifications) || ($last_checked && (time() - $last_checked > $timeout))) {
|
||||
$body = Response::get('https://getgrav.org/notifications.json?' . time());
|
||||
$notifications = Yaml::parse($body);
|
||||
|
||||
$notifications_processed = [];
|
||||
foreach ($notifications as $key => $notification) {
|
||||
$is_valid = true;
|
||||
// Sort by date
|
||||
usort($notifications, function ($a, $b) {
|
||||
return strcmp($a['date'], $b['date']);
|
||||
});
|
||||
|
||||
if (in_array($notification->id, $read_notifications, true)) {
|
||||
$notification->read = true;
|
||||
}
|
||||
// Get top 10
|
||||
$notifications = array_slice($notifications, 0, 10);
|
||||
|
||||
if ($is_valid && isset($notification->permissions) && !$this->authorize($notification->permissions)) {
|
||||
$is_valid = false;
|
||||
}
|
||||
// Reverse order and create a new array
|
||||
$cleaned_notifications = array_reverse($notifications);
|
||||
|
||||
if ($is_valid && isset($notification->dependencies)) {
|
||||
foreach ($notification->dependencies as $dependency => $constraints) {
|
||||
if ($dependency === 'grav') {
|
||||
if (!Semver::satisfies(GRAV_VERSION, $constraints)) {
|
||||
$is_valid = false;
|
||||
}
|
||||
} else {
|
||||
$packages = array_merge($this->plugins()->toArray(), $this->themes()->toArray());
|
||||
if (!isset($packages[$dependency])) {
|
||||
$is_valid = false;
|
||||
foreach ($cleaned_notifications as $key => $notification) {
|
||||
|
||||
if (isset($notification['permissions']) && !$this->authorize($notification['permissions'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($notification['dependencies'])) {
|
||||
foreach ($notification['dependencies'] as $dependency => $constraints) {
|
||||
if ($dependency === 'grav') {
|
||||
if (!Semver::satisfies(GRAV_VERSION, $constraints)) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
$version = $packages[$dependency]['version'];
|
||||
if (!Semver::satisfies($version, $constraints)) {
|
||||
$is_valid = false;
|
||||
$packages = array_merge($this->plugins()->toArray(), $this->themes()->toArray());
|
||||
if (!isset($packages[$dependency])) {
|
||||
continue;
|
||||
} else {
|
||||
$version = $packages[$dependency]['version'];
|
||||
if (!Semver::satisfies($version, $constraints)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$is_valid) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$cleaned_notifications[] = $notification;
|
||||
|
||||
}
|
||||
|
||||
if ($is_valid) {
|
||||
$notifications_processed[] = $notification;
|
||||
// // Process notifications dates
|
||||
// $notifications = array_map(function ($notification) {
|
||||
// $notification['nicetime'] = $this->adminNiceTime($notification['date']);
|
||||
//
|
||||
// return $notification;
|
||||
// }, $cleaned_notifications);
|
||||
|
||||
|
||||
$notifications_file->content(['last_checked' => time(), 'data' => $notifications]);
|
||||
$notifications_file->save();
|
||||
}
|
||||
|
||||
|
||||
return $notifications;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get https://getgrav.org news feed
|
||||
*
|
||||
* @return mixed
|
||||
* @throws MalformedXmlException
|
||||
*/
|
||||
public function getFeed($force = false)
|
||||
{
|
||||
$last_checked = null;
|
||||
$filename = $this->grav['locator']->findResource('user://data/feed/' . md5($this->grav['user']->username) . YAML_EXT, true, true);
|
||||
|
||||
$feed_file = CompiledYamlFile::instance($filename);
|
||||
$feed_content = (array)$feed_file->content();
|
||||
|
||||
$last_checked = $feed_content['last_checked'] ?? null;
|
||||
$feed = $feed_content['data'] ?? array();
|
||||
$timeout = $this->grav['config']->get('system.session.timeout', 1800);
|
||||
|
||||
if ($force || !$last_checked || empty($feed) || ($last_checked && (time() - $last_checked > $timeout))) {
|
||||
$feed_url = 'https://getgrav.org/blog.atom';
|
||||
$body = Response::get($feed_url);
|
||||
|
||||
$reader = new Reader();
|
||||
$parser = $reader->getParser($feed_url, $body, 'utf-8');
|
||||
$data = $parser->execute()->getItems();
|
||||
|
||||
// Get top 10
|
||||
$data = array_slice($data, 0, 10);
|
||||
|
||||
$feed = array_map(function ($entry) {
|
||||
$simple_entry['title'] = $entry->getTitle();
|
||||
$simple_entry['url'] = $entry->getUrl();
|
||||
$simple_entry['date'] = $entry->getDate()->getTimestamp();
|
||||
$simple_entry['nicetime'] = $this->adminNiceTime($simple_entry['date']);
|
||||
return $simple_entry;
|
||||
}, $data);
|
||||
|
||||
$feed_file->content(['last_checked' => time(), 'data' => $feed]);
|
||||
$feed_file->save();
|
||||
}
|
||||
|
||||
return $feed;
|
||||
|
||||
}
|
||||
|
||||
public function adminNiceTime($date, $long_strings = true)
|
||||
{
|
||||
if (empty($date)) {
|
||||
return $this->translate('GRAV.NICETIME.NO_DATE_PROVIDED', null, true);
|
||||
}
|
||||
|
||||
if ($long_strings) {
|
||||
$periods = [
|
||||
'NICETIME.SECOND',
|
||||
'NICETIME.MINUTE',
|
||||
'NICETIME.HOUR',
|
||||
'NICETIME.DAY',
|
||||
'NICETIME.WEEK',
|
||||
'NICETIME.MONTH',
|
||||
'NICETIME.YEAR',
|
||||
'NICETIME.DECADE'
|
||||
];
|
||||
} else {
|
||||
$periods = [
|
||||
'NICETIME.SEC',
|
||||
'NICETIME.MIN',
|
||||
'NICETIME.HR',
|
||||
'NICETIME.DAY',
|
||||
'NICETIME.WK',
|
||||
'NICETIME.MO',
|
||||
'NICETIME.YR',
|
||||
'NICETIME.DEC'
|
||||
];
|
||||
}
|
||||
|
||||
$lengths = ['60', '60', '24', '7', '4.35', '12', '10'];
|
||||
|
||||
$now = time();
|
||||
|
||||
// check if unix timestamp
|
||||
if ((string)(int)$date === (string)$date) {
|
||||
$unix_date = $date;
|
||||
} else {
|
||||
$unix_date = strtotime($date);
|
||||
}
|
||||
|
||||
// check validity of date
|
||||
if (empty($unix_date)) {
|
||||
return $this->translate('GRAV.NICETIME.BAD_DATE', null, true);
|
||||
}
|
||||
|
||||
// is it future date or past date
|
||||
if ($now > $unix_date) {
|
||||
$difference = $now - $unix_date;
|
||||
$tense = $this->translate('GRAV.NICETIME.AGO', null, true);
|
||||
|
||||
} else {
|
||||
$difference = $unix_date - $now;
|
||||
$tense = $this->translate('GRAV.NICETIME.FROM_NOW', null, true);
|
||||
}
|
||||
|
||||
$len = count($lengths) - 1;
|
||||
for ($j = 0; $difference >= $lengths[$j] && $j < $len; $j++) {
|
||||
$difference /= $lengths[$j];
|
||||
}
|
||||
|
||||
$difference = round($difference);
|
||||
|
||||
if ($difference !== 1) {
|
||||
$periods[$j] .= '_PLURAL';
|
||||
}
|
||||
|
||||
if ($this->grav['language']->getTranslation($this->grav['user']->language,
|
||||
$periods[$j] . '_MORE_THAN_TWO')
|
||||
) {
|
||||
if ($difference > 2) {
|
||||
$periods[$j] .= '_MORE_THAN_TWO';
|
||||
}
|
||||
}
|
||||
|
||||
// Process notifications
|
||||
$notifications_processed = array_map(function ($notification) use ($adminTwigExtension) {
|
||||
$notification->date = $adminTwigExtension->adminNicetimeFilter($notification->date);
|
||||
$periods[$j] = $this->translate('GRAV.'.$periods[$j], null, true);
|
||||
|
||||
return $notification;
|
||||
}, $notifications_processed);
|
||||
|
||||
return $notifications_processed;
|
||||
return "{$difference} {$periods[$j]} {$tense}";
|
||||
}
|
||||
|
||||
public function findFormFields($type, $fields, $found_fields = [])
|
||||
@@ -1561,24 +1691,6 @@ class Admin
|
||||
return $reports;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get https://getgrav.org news feed
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFeed()
|
||||
{
|
||||
$feed_url = 'https://getgrav.org/blog.atom';
|
||||
|
||||
$body = Response::get($feed_url);
|
||||
|
||||
$reader = new Reader();
|
||||
$parser = $reader->getParser($feed_url, $body, 'utf-8');
|
||||
|
||||
return $parser->execute();
|
||||
|
||||
}
|
||||
|
||||
public function getRouteDetails()
|
||||
{
|
||||
return [$this->base, $this->location, $this->route];
|
||||
|
||||
Reference in New Issue
Block a user