|
| 1 | +<?php |
| 2 | +namespace Grav\Plugin; |
| 3 | + |
| 4 | +use Grav\Common\Page\Page; |
| 5 | +use Grav\Common\Plugin; |
| 6 | +use RocketTheme\Toolbox\Event\Event; |
| 7 | + |
| 8 | +/** |
| 9 | + * Class WebMonetizationPlugin |
| 10 | + * |
| 11 | + * @package Grav\Plugin |
| 12 | + * @see https://webmonetization.org/ |
| 13 | + */ |
| 14 | +class WebMonetizationPlugin extends Plugin |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @return array |
| 18 | + * |
| 19 | + * Give the core an array of events the plugin listens to. For each event: |
| 20 | + * 1. Key: Name of the event. |
| 21 | + * 2. Value: array containing: |
| 22 | + * 1. Callable; a named function; |
| 23 | + * 2. Priority; a higher number is a higher priority. |
| 24 | + */ |
| 25 | + public static function getSubscribedEvents() |
| 26 | + { |
| 27 | + return [ |
| 28 | + 'onPluginsInitialized' => ['onPluginsInitialized', 0] |
| 29 | + ]; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Initialize the plugin. |
| 34 | + */ |
| 35 | + public function onPluginsInitialized() |
| 36 | + { |
| 37 | + // Don’t proceed if we are in the admin plugin. |
| 38 | + if ($this->isAdmin()) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // Enable the main events we are interested in. |
| 43 | + $this->enable([ |
| 44 | + 'onPageInitialized' => ['onPageInitialized', 0], |
| 45 | + ]); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * When a Grav Page is initiliazed… |
| 50 | + */ |
| 51 | + public function onPageInitialized(Event $e) |
| 52 | + { |
| 53 | + // Get current page object. |
| 54 | + $page = $this->grav['page']; |
| 55 | + |
| 56 | + // Get current metadata. |
| 57 | + $meta = $page->metadata(); |
| 58 | + |
| 59 | + // Add a meta to the list of metadata if a pointer exists. |
| 60 | + $pointer = $this->config->get('plugins.web-monetization.pointer'); |
| 61 | + |
| 62 | + if ($pointer) { |
| 63 | + $meta['monetization'] = [ |
| 64 | + 'name' => 'monetization', |
| 65 | + 'content' => $pointer, |
| 66 | + ]; |
| 67 | + |
| 68 | + $page->metadata($meta); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments