mediaembed.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * MediaEmbed v1.3.0
  4. *
  5. * This plugin embeds several media sites (e.g. YouTube, Vimeo,
  6. * Soundcloud) by only providing the URL to the medium.
  7. *
  8. * Dual licensed under the MIT or GPL Version 3 licenses, see LICENSE.
  9. * http://benjamin-regler.de/license/
  10. *
  11. * @package MediaEmbed
  12. * @version 1.3.0
  13. * @link <https://github.com/sommerregen/grav-plugin-archive-plus>
  14. * @author Benjamin Regler <sommerregen@benjamin-regler.de>
  15. * @copyright 2017, Benjamin Regler
  16. * @license <http://opensource.org/licenses/MIT> MIT
  17. * @license <http://opensource.org/licenses/GPL-3.0> GPLv3
  18. */
  19. namespace Grav\Plugin;
  20. use Grav\Common\Grav;
  21. use Grav\Common\Plugin;
  22. use Grav\Common\Page\Page;
  23. use RocketTheme\Toolbox\Event\Event;
  24. use Grav\Plugin\MediaEmbed\Autoloader;
  25. use Grav\Plugin\MediaEmbed\MediaEmbed;
  26. /**
  27. * MediaEmbed
  28. *
  29. * This plugin ...
  30. */
  31. class MediaEmbedPlugin extends Plugin
  32. {
  33. /**
  34. * @var MediaEmbedPlugin
  35. */
  36. /** ---------------------------
  37. * Private/protected properties
  38. * ----------------------------
  39. */
  40. /**
  41. * Instance of MediaEmbed class
  42. *
  43. * @var object
  44. */
  45. protected $mediaembed;
  46. /** -------------
  47. * Public methods
  48. * --------------
  49. */
  50. /**
  51. * Return a list of subscribed events.
  52. *
  53. * @return array The list of events of the plugin of the form
  54. * 'name' => ['method_name', priority].
  55. */
  56. public static function getSubscribedEvents()
  57. {
  58. return [
  59. 'onPluginsInitialized' => ['onPluginsInitialized', 0],
  60. ];
  61. }
  62. /**
  63. * Initialize configuration.
  64. */
  65. public function onPluginsInitialized()
  66. {
  67. if ($this->isAdmin()) {
  68. $this->active = false;
  69. return;
  70. }
  71. if ($this->config->get('plugins.mediaembed.enabled')) {
  72. // Initialize Autoloader
  73. require_once(__DIR__ . '/classes/Autoloader.php');
  74. require_once(__DIR__ . '/vendor/Requests/library/Requests.php');
  75. $autoloader = new Autoloader();
  76. $autoloader->route([
  77. 'Requests_' => __DIR__ . '/vendor/Requests/library/Requests',
  78. ], false);
  79. $autoloader->register();
  80. // Initialize MediaEmbed class
  81. $this->mediaembed = new MediaEmbed($this->config);
  82. $this->enable([
  83. 'onPageContentRaw' => ['onPageContentRaw', 0],
  84. 'onPageContentProcessed' => ['onPageContentProcessed', 0],
  85. 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
  86. 'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
  87. ]);
  88. }
  89. }
  90. /**
  91. * Add content after page content was read into the system.
  92. *
  93. * @param Event $event An event object, when `onPageContentRaw` is
  94. * fired.
  95. */
  96. public function onPageContentRaw(Event $event)
  97. {
  98. /** @var Page $page */
  99. $page = $event['page'];
  100. $config = $this->mergeConfig($page);
  101. if ($config->get('enabled')) {
  102. // Get raw content and substitute all formulas by a unique token
  103. $raw_content = $page->getRawContent();
  104. // Save modified page content with tokens as placeholders
  105. $page->setRawContent(
  106. $this->mediaembed->prepare($raw_content, $page->id())
  107. );
  108. }
  109. }
  110. /**
  111. * Apply mediaembed filter to content, when each page has not been
  112. * cached yet.
  113. *
  114. * @param Event $event The event when 'onPageContentProcessed' was
  115. * fired.
  116. */
  117. public function onPageContentProcessed(Event $event)
  118. {
  119. /** @var Page $page */
  120. $page = $event['page'];
  121. $config = $this->mergeConfig($page);
  122. if ($config->get('enabled') && $this->compileOnce($page)) {
  123. // Get content
  124. $content = $page->getRawContent();
  125. // Apply MediaEmbed filter and save modified page content
  126. $page->setRawContent(
  127. $this->mediaembed->process($content, $config)
  128. );
  129. }
  130. }
  131. /**
  132. * Add current directory to twig lookup paths.
  133. */
  134. public function onTwigTemplatePaths()
  135. {
  136. // Register MediaEmbed Twig templates
  137. $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
  138. // Fire event for MediaEmbed plugins
  139. $this->mediaembed->fireEvent('onTwigTemplatePaths');
  140. }
  141. /**
  142. * Set needed variables to display videos.
  143. */
  144. public function onTwigSiteVariables()
  145. {
  146. // Register built-in CSS assets
  147. if ($this->config->get('plugins.mediaembed.built_in_css')) {
  148. $this->grav['assets']
  149. ->add('plugin://mediaembed/assets/css/mediaembed.css');
  150. }
  151. if ($this->config->get('plugins.mediaembed.built_in_js')) {
  152. $this->grav['assets']
  153. ->add('plugin://mediaembed/assets/js/mediaembed.js');
  154. }
  155. // Register assets from MediaEmbed Services
  156. $assets = $this->mediaembed->getAssets();
  157. foreach ($assets as $asset) {
  158. $this->grav['assets']->add($asset);
  159. }
  160. }
  161. /** -------------------------------
  162. * Private/protected helper methods
  163. * --------------------------------
  164. */
  165. /**
  166. * Checks if a page has already been compiled yet.
  167. *
  168. * @param Page $page The page to check
  169. *
  170. * @return boolean Returns TRUE if page has already been
  171. * compiled yet, FALSE otherwise
  172. */
  173. protected function compileOnce(Page $page)
  174. {
  175. static $processed = [];
  176. $id = md5($page->path());
  177. // Make sure that contents is only processed once
  178. if (!isset($processed[$id]) || ($processed[$id] < $page->modified())) {
  179. $processed[$id] = $page->modified();
  180. return true;
  181. }
  182. return false;
  183. }
  184. }