Grav\Plugin\Admin\Admin::processNotifications PHP Метод

processNotifications() публичный Метод

public processNotifications ( $notifications )
    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;
    }

Usage Example

Пример #1
0
 /**
  * Process Notifications. Store the notifications object locally.
  *
  * @return bool
  */
 protected function taskProcessNotifications()
 {
     $cache = $this->grav['cache'];
     $data = $this->post;
     $notifications = json_decode($data['notifications']);
     try {
         $notifications = $this->admin->processNotifications($notifications);
     } catch (\Exception $e) {
         $this->admin->json_response = ['status' => 'error', 'message' => $e->getMessage()];
         return false;
     }
     $show_immediately = false;
     if (!$cache->fetch('notifications_last_checked')) {
         $show_immediately = true;
     }
     $cache->save('notifications', $notifications);
     $cache->save('notifications_last_checked', time());
     $this->admin->json_response = ['status' => 'success', 'notifications' => $notifications, 'show_immediately' => $show_immediately];
     return true;
 }