['onPluginsInitialized', 0], ]; } /** * Activate plugin if path matches to the configured one. */ public function onPluginsInitialized() { if (!$this->isAdmin()) { $this->active = false; return; } include __DIR__.'/vendor/autoload.php'; \Cloudinary::config([ "cloud_name" => $this->config->get('plugins.cloudinary.cloud_name'), "api_key" => $this->config->get('plugins.cloudinary.api_key'), "api_secret" => $this->config->get('plugins.cloudinary.api_secret'), ]); $this->enable([ 'onImageMediumSaved' => ['onImageMediumSaved', 0], ]); } public function onImageMediumSaved(Event $event) { $path = $event['image']; try { $cloudinary_image = \Cloudinary\Uploader::upload($path); if (!empty($cloudinary_image)) { file_put_contents($path, $cloudinary_image['secure_url']); } else { $this->grav['log']->critical(sprintf('%s has failed its upload to Cloudinary', $path)); } } catch (\Cloudinary\Error $e) { $this->grav['log']->critical(sprintf('Cloudinary API error : %s', $e->getMessage())); } } }