aura.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\Plugin;
  4. use Grav\Common\Flex\Types\Pages\PageObject;
  5. use RocketTheme\Toolbox\Event\Event;
  6. use Grav\Common\Utils;
  7. use Grav\Plugin\Aura\Aura;
  8. /**
  9. * Class AuraPlugin
  10. *
  11. * @package Grav\Plugin
  12. */
  13. class AuraPlugin extends Plugin
  14. {
  15. /**
  16. * Gives the core a list of events the plugin wants to listen to
  17. *
  18. * @return array
  19. */
  20. public static function getSubscribedEvents()
  21. {
  22. return [
  23. 'onPluginsInitialized' => ['onPluginsInitialized', 0],
  24. ];
  25. }
  26. /**
  27. * Initialize the plugin
  28. */
  29. public function onPluginsInitialized()
  30. {
  31. spl_autoload_register(
  32. function ($class) {
  33. if (Utils::startsWith($class, 'Grav\Plugin\Aura\\')) {
  34. require_once __DIR__ . '/classes/' . strtolower(basename(str_replace("\\", '/', $class))) . '.php';
  35. }
  36. }
  37. );
  38. // Admin only events
  39. if ($this->isAdmin()) {
  40. $this->enable(
  41. [
  42. 'onGetPageBlueprints' => ['onGetPageBlueprints', 0],
  43. 'onAdminSave' => ['onAdminSave', 0],
  44. ]
  45. );
  46. return;
  47. }
  48. // Frontend events
  49. $this->enable(
  50. [
  51. 'onPageInitialized' => ['onPageInitialized', 0],
  52. ]
  53. );
  54. }
  55. /**
  56. * Extend page blueprints with additional configuration options.
  57. *
  58. * @param Event $event
  59. */
  60. public function onGetPageBlueprints($event)
  61. {
  62. $types = $event->types;
  63. $types->scanBlueprints('plugins://' . $this->name . '/blueprints');
  64. }
  65. public function onAdminSave(Event $event)
  66. {
  67. if (!$event['object'] instanceof PageObject) {
  68. return;
  69. }
  70. // Don't proceed if required params not set
  71. $requiredParams = [
  72. 'org-name',
  73. 'org-url',
  74. ];
  75. foreach ($requiredParams as $param) {
  76. $key = 'plugins.aura.' . $param;
  77. if (!$this->grav['config']->get($key)) {
  78. return;
  79. }
  80. }
  81. $page = $event['object'];
  82. $aura = new Aura($page);
  83. // Meta Description
  84. if ($aura->webpage->description) {
  85. // Append description to page metadata
  86. $aura->webpage->metadata['description'] = [
  87. 'name' => 'description',
  88. 'content' => $aura->webpage->description,
  89. ];
  90. }
  91. // Open Graph
  92. if ($this->grav['config']->get('plugins.aura.output-og')) {
  93. $aura->generateOpenGraphMeta();
  94. }
  95. // Twitter
  96. if ($this->grav['config']->get('plugins.aura.output-twitter')) {
  97. $aura->generateTwitterMeta();
  98. }
  99. // LinkedIn
  100. if ($this->grav['config']->get('plugins.aura.output-linkedin')) {
  101. $aura->generateLinkedInMeta();
  102. }
  103. // Generate Aura metadata
  104. $metadata = [];
  105. foreach ($aura->webpage->metadata as $tag) {
  106. if (array_key_exists('property', $tag)) {
  107. $metadata[$tag['property']] = $tag['content'];
  108. } elseif (array_key_exists('name', $tag)) {
  109. $metadata[$tag['name']] = $tag['content'];
  110. }
  111. }
  112. $original = $page->getOriginal();
  113. if ($original && !isset($original->header()->aura) && isset($page->header()->metadata) && is_array($page->header()->metadata)) {
  114. // Page has not been saved since installation of Aura and includes some custom metadata
  115. $legacyMetadata = ['metadata' => []];
  116. foreach ($page->header()->metadata as $key => $val) {
  117. if (!array_key_exists($key, $metadata)) {
  118. // A new value has not been supplied via Aura, salvage existing metadata
  119. $metadata[$key] = $val;
  120. $legacyMetadata['metadata'][$key] = $val;
  121. }
  122. }
  123. $page->header()->aura = array_merge($legacyMetadata, $page->header()->aura ?? []);
  124. }
  125. $page->header()->metadata = array_merge($metadata, $page->header()->aura['metadata'] ?? []);
  126. }
  127. /**
  128. * Insert meta tags and structured data to head of each page
  129. */
  130. public function onPageInitialized()
  131. {
  132. // Structured Data
  133. if (!$this->grav['config']->get('plugins.aura.output-sd')) {
  134. return;
  135. }
  136. // Don't proceed if required params not set
  137. $requiredParams = [
  138. 'org-name',
  139. 'org-url',
  140. ];
  141. foreach ($requiredParams as $param) {
  142. $key = 'plugins.aura.' . $param;
  143. if (!$this->grav['config']->get($key)) {
  144. return;
  145. }
  146. }
  147. $page = $this->grav['page'];
  148. $assets = $this->grav['assets'];
  149. $aura = new Aura($page);
  150. // Generate structured data block
  151. $sd = $aura->generateStructuredData();
  152. // Drop into JS pipeline
  153. $assets->addInlineJs($sd, null, null, ['type' => 'application/ld+json']);
  154. }
  155. }