1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-30 18:09:56 +02:00

Add a Notices::getVisible() method that returns all Notice objects visible to current user. This should prevent the need for filtering at output time.

This commit is contained in:
Ryan Cramer
2025-08-29 17:31:09 -04:00
parent 9b1fa290f1
commit c7c26938e3

View File

@@ -10,7 +10,7 @@
* Base class that holds a message, source class, and timestamp.
* Contains notices/messages used by the application to the user.
*
* ProcessWire 3.x, Copyright 2022 by Ryan Cramer
* ProcessWire 3.x, Copyright 2025 by Ryan Cramer
* https://processwire.com
*
* @property string|object|array $text Text or value of notice
@@ -911,6 +911,31 @@ class Notices extends WireArray {
return $notice;
}
/**
* Get all notices visible to current user
*
* @return Notices Returns a new Notices object
* @since 3.0.252
*
*/
public function getVisible() {
$config = $this->wire()->config;
$user = $this->wire()->user;
$items = new Notices();
$this->wire($items);
foreach($this as $notice) {
/** @var Notice $notice */
$flags = $notice->flags;
if(($flags & Notice::superuser) && !$user->isSuperuser()) continue;
if(($flags & Notice::login) && !$user->isLoggedin()) continue;
if(($flags & Notice::debug) && !$config->debug) continue;
if(($flags & Notice::admin) && !$config->admin) continue;
if(($flags & Notice::logOnly)) continue;
$items->add($notice);
}
return $items;
}
/**
* Export Notice object to string
*