youtube.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * YouTube
  4. *
  5. * This plugin embeds YouTube video from markdown URLs
  6. *
  7. * Licensed under MIT, see LICENSE.
  8. */
  9. namespace Grav\Plugin;
  10. use Grav\Common\Data\Data;
  11. use Grav\Common\Plugin;
  12. use Grav\Common\Page\Page;
  13. use Grav\Common\Twig\Twig;
  14. use Grav\Plugin\Youtube\Twig\YoutubeTwigExtension;
  15. use RocketTheme\Toolbox\Event\Event;
  16. class YoutubePlugin extends Plugin
  17. {
  18. const YOUTUBE_REGEX = '(?:https?:\/{2}(?:(?:www.youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=))|(?:youtu\.be\/)))([a-zA-Z0-9_-]{11})';
  19. /**
  20. * Return a list of subscribed events.
  21. *
  22. * @return array The list of events of the plugin of the form
  23. * 'name' => ['method_name', priority].
  24. */
  25. public static function getSubscribedEvents()
  26. {
  27. return [
  28. 'onPluginsInitialized' => ['onPluginsInitialized', 0],
  29. ];
  30. }
  31. /**
  32. * Initialize configuration.
  33. */
  34. public function onPluginsInitialized()
  35. {
  36. if ($this->isAdmin()) {
  37. $this->enable([
  38. 'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
  39. ]);
  40. return;
  41. }
  42. $this->enable([
  43. 'onPageContentRaw' => ['onPageContentRaw', 0],
  44. 'onTwigExtensions' => ['onTwigExtensions', 0],
  45. 'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
  46. 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
  47. ]);
  48. }
  49. /**
  50. * Add content after page content was read into the system.
  51. *
  52. * @param Event $event An event object, when `onPageContentRaw` is fired.
  53. */
  54. public function onPageContentRaw(Event $event)
  55. {
  56. /** @var Page $page */
  57. $page = $event['page'];
  58. /** @var Twig $twig */
  59. $twig = $this->grav['twig'];
  60. /** @var Data $config */
  61. $config = $this->mergeConfig($page, TRUE);
  62. if ($config->get('enabled')) {
  63. // Get raw content and substitute all formulas by a unique token
  64. $raw = $page->getRawContent();
  65. // build an anonymous function to pass to `parseLinks()`
  66. $function = function ($matches) use ($twig, $config) {
  67. $search = $matches[0];
  68. // double check to make sure we found a valid YouTube video ID
  69. if (!isset($matches[1])) {
  70. return $search;
  71. }
  72. // build the replacement embed HTML string
  73. $replace = $twig->processTemplate('partials/youtube.html.twig', array(
  74. 'player_parameters' => $config->get('player_parameters'),
  75. 'privacy_enhanced_mode' => $config->get('privacy_enhanced_mode'),
  76. 'video_id' => $matches[1],
  77. ));
  78. // do the replacement
  79. return str_replace($search, $replace, $search);
  80. };
  81. // set the parsed content back into as raw content
  82. $page->setRawContent($this->parseLinks($raw, $function, $this::YOUTUBE_REGEX));
  83. }
  84. }
  85. /**
  86. * Add Twig Extensions.
  87. */
  88. public function onTwigExtensions()
  89. {
  90. require_once __DIR__ . '/classes/Twig/YoutubeTwigExtension.php';
  91. $this->grav['twig']->twig->addExtension(new YoutubeTwigExtension());
  92. }
  93. /**
  94. * Set needed variables to display breadcrumbs.
  95. */
  96. public function onTwigSiteVariables()
  97. {
  98. if (!$this->isAdmin() && $this->config->get('plugins.youtube.built_in_css')) {
  99. $this->grav['assets']->add('plugin://youtube/css/youtube.css');
  100. }
  101. if ($this->isAdmin() && $this->config->get('plugins.youtube.add_editor_button')) {
  102. $this->grav['assets']->add('plugin://youtube/admin/editor-button/js/button.js');
  103. }
  104. }
  105. /**
  106. * Add current directory to twig lookup paths.
  107. */
  108. public function onTwigTemplatePaths()
  109. {
  110. $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
  111. }
  112. }