youtube.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. 'onShortcodeHandlers' => ['onShortcodeHandlers', 0],
  48. ]);
  49. }
  50. /**
  51. * Add content after page content was read into the system.
  52. *
  53. * @param Event $event An event object, when `onPageContentRaw` is fired.
  54. */
  55. public function onPageContentRaw(Event $event)
  56. {
  57. /** @var Page $page */
  58. $page = $event['page'];
  59. /** @var Twig $twig */
  60. $twig = $this->grav['twig'];
  61. /** @var Data $config */
  62. $config = $this->mergeConfig($page, TRUE);
  63. if ($config->get('enabled')) {
  64. // Get raw content and substitute all formulas by a unique token
  65. $raw = $page->getRawContent();
  66. // build an anonymous function to pass to `parseLinks()`
  67. $function = function ($matches) use ($twig, $config) {
  68. $search = $matches[0];
  69. // double check to make sure we found a valid YouTube video ID
  70. if (!isset($matches[1])) {
  71. return $search;
  72. }
  73. // build the replacement embed HTML string
  74. $replace = $twig->processTemplate('partials/youtube.html.twig', array(
  75. 'player_parameters' => $config->get('player_parameters'),
  76. 'privacy_enhanced_mode' => $config->get('privacy_enhanced_mode'),
  77. 'video_id' => $matches[1],
  78. ));
  79. // do the replacement
  80. return str_replace($search, $replace, $search);
  81. };
  82. // set the parsed content back into as raw content
  83. $page->setRawContent($this->parseLinks($raw, $function, $this::YOUTUBE_REGEX));
  84. }
  85. }
  86. /**
  87. * Add Twig Extensions.
  88. */
  89. public function onTwigExtensions()
  90. {
  91. require_once __DIR__ . '/classes/Twig/YoutubeTwigExtension.php';
  92. $this->grav['twig']->twig->addExtension(new YoutubeTwigExtension());
  93. }
  94. /**
  95. * Set needed variables to display breadcrumbs.
  96. */
  97. public function onTwigSiteVariables()
  98. {
  99. if (!$this->isAdmin() && $this->config->get('plugins.youtube.built_in_css')) {
  100. $this->grav['assets']->add('plugin://youtube/css/youtube.css');
  101. }
  102. if ($this->isAdmin() && $this->config->get('plugins.youtube.add_editor_button')) {
  103. $this->grav['assets']->add('plugin://youtube/admin/editor-button/js/button.js');
  104. }
  105. }
  106. /**
  107. * Add current directory to twig lookup paths.
  108. */
  109. public function onTwigTemplatePaths()
  110. {
  111. $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
  112. }
  113. /**
  114. * Initialize shortcodes
  115. */
  116. public function onShortcodeHandlers()
  117. {
  118. $this->grav['shortcode']->registerAllShortcodes(__DIR__.'/shortcodes');
  119. }
  120. }