mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2026-05-06 20:16:52 +02:00
[WIP] Notifications (#599)
Adds a new notifications feature to the Admin plugin. It will now lookup notifications which are set up on getgrav.org and will inform users, and also alert for new updates and provide awareness on various topics. Also fixes issue with Array field in `value_only` mode, improperly displaying the key when novalue was set, and fixes issue with drag handlers in Array not showing/hiding properly (#950) Updated FontAwesome to 4.6.3
This commit is contained in:
@@ -22,6 +22,8 @@ use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
|
||||
use RocketTheme\Toolbox\Session\Message;
|
||||
use RocketTheme\Toolbox\Session\Session;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Composer\Semver\Semver;
|
||||
use PicoFeed\Reader\Reader;
|
||||
|
||||
define('LOGIN_REDIRECT_COOKIE', 'grav-login-redirect');
|
||||
|
||||
@@ -1173,6 +1175,72 @@ class Admin
|
||||
$this->permissions = array_merge($this->permissions, $permissions);
|
||||
}
|
||||
|
||||
public function processNotifications($notifications)
|
||||
{
|
||||
// Sort by date
|
||||
usort($notifications, function($a, $b) {
|
||||
return strcmp($a->date, $b->date);
|
||||
});
|
||||
|
||||
$notifications = array_reverse($notifications);
|
||||
|
||||
// Make adminNicetimeFilter available
|
||||
require_once(__DIR__ . '/../twig/AdminTwigExtension.php');
|
||||
$adminTwigExtension = new AdminTwigExtension();
|
||||
|
||||
$filename = $this->grav['locator']->findResource('user://data/notifications/' . $this->grav['user']->username . YAML_EXT, true, true);
|
||||
$read_notifications = CompiledYamlFile::instance($filename)->content();
|
||||
|
||||
$notifications_processed = [];
|
||||
foreach ($notifications as $key => $notification) {
|
||||
$is_valid = true;
|
||||
|
||||
if (in_array($notification->id, $read_notifications)) {
|
||||
$notification->read = true;
|
||||
}
|
||||
|
||||
if ($is_valid && isset($notification->permissions) && !$this->authorize($notification->permissions)) {
|
||||
$is_valid = false;
|
||||
}
|
||||
|
||||
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;
|
||||
} else {
|
||||
$version = $packages[$dependency]['version'];
|
||||
if (!Semver::satisfies($version, $constraints)) {
|
||||
$is_valid = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$is_valid) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($is_valid) {
|
||||
$notifications_processed[] = $notification;
|
||||
}
|
||||
}
|
||||
|
||||
// Process notifications
|
||||
$notifications_processed = array_map(function($notification) use ($adminTwigExtension) {
|
||||
$notification->date = $adminTwigExtension->adminNicetimeFilter($notification->date);
|
||||
return $notification;
|
||||
}, $notifications_processed);
|
||||
|
||||
return $notifications_processed;
|
||||
}
|
||||
|
||||
public function findFormFields($type, $fields, $found_fields = [])
|
||||
{
|
||||
foreach ($fields as $key => $field) {
|
||||
@@ -1233,4 +1301,35 @@ class Admin
|
||||
return $path . $basename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get https://getgrav.org news feed
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFeed()
|
||||
{
|
||||
|
||||
$reader = new Reader;
|
||||
$resource = $reader->download('https://getgrav.org/blog.atom');
|
||||
|
||||
$parser = $reader->getParser(
|
||||
$resource->getUrl(),
|
||||
$resource->getContent(),
|
||||
$resource->getEncoding()
|
||||
);
|
||||
|
||||
$feed = $parser->execute();
|
||||
|
||||
return $feed;
|
||||
|
||||
}
|
||||
|
||||
public function cleanContent($content)
|
||||
{
|
||||
$string = strip_tags($content);
|
||||
$string = htmlspecialchars_decode($string, ENT_QUOTES);
|
||||
$string = str_replace("\n", ' ', $string);
|
||||
return trim($string);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user