aura.php 5.5 KB

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