vimeo.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\Plugin;
  4. use Grav\Plugin\Vimeo\Twig\VimeoTwigExtension;
  5. use RocketTheme\Toolbox\Event\Event;
  6. /**
  7. * Class VimeoPlugin
  8. * @package Grav\Plugin
  9. */
  10. class VimeoPlugin extends Plugin
  11. {
  12. protected $locale;
  13. const VIMEO_REGEX = '(?:\S*)?:?\/{2}(?:\S*)vimeo.com(?:\/video)?\/(\d*)';
  14. /**
  15. * Returns a list of events the plugins wants to listen to.
  16. * @return array
  17. */
  18. public static function getSubscribedEvents()
  19. {
  20. return [
  21. 'onPluginsInitialized' => ['onPluginsInitialized', 0],
  22. 'onAssetsInitialized' => ['onAssetsInitialized', 0]
  23. ];
  24. }
  25. /**
  26. * Initialize the plugin
  27. */
  28. public function onPluginsInitialized()
  29. {
  30. $this->initializeLocale();
  31. // Don't proceed if we are in the admin plugin
  32. if ($this->isAdmin()) return;
  33. // Enable the events we are interested in
  34. $this->enable([
  35. 'onPageContentRaw' => ['onPageContentRaw', 0],
  36. 'onTwigExtensions' => ['onTwigExtensions', 0],
  37. 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
  38. ]);
  39. }
  40. private function initializeLocale() {
  41. $locales = [];
  42. $language = $this->grav['language'];
  43. // Available Languages
  44. if (isset($this->grav['user']) && $this->grav['user']->authenticated) $locales[] = $this->grav['user']->language;
  45. if ($language->enabled())$locales[] = $language->getLanguage();
  46. $locales[] = 'en';
  47. $locales = array_unique(array_filter($locales));
  48. foreach ($locales as $locale) {
  49. if (isset($this->grav['languages'][$locale]['PLUGIN_VIMEO'])) {
  50. $this->locale = $locale;
  51. break;
  52. }
  53. }
  54. }
  55. /**
  56. * Add Built-in CSS and Editor Button JS if wanted
  57. */
  58. public function onAssetsInitialized()
  59. {
  60. if (!$this->isAdmin() && $this->config->get('plugins.vimeo.plugin_css')) {
  61. $this->grav['assets']->add('plugin://vimeo/css/vimeo.css');
  62. }
  63. if ($this->isAdmin() && $this->config->get('plugins.vimeo.editor_button')) {
  64. $plugin_translations = $this->grav['languages'][$this->locale]['PLUGIN_VIMEO'];
  65. $translations = [
  66. 'EDITOR_BUTTON_TOOLTIP' => $plugin_translations['EDITOR_BUTTON_TOOLTIP'],
  67. 'EDITOR_BUTTON_PROMPT' => $plugin_translations['EDITOR_BUTTON_PROMPT']
  68. ];
  69. $code = 'this.GravVimeoPlugin = this.GravVimeoPlugin || {};';
  70. $code.= 'if (!this.GravVimeoPlugin.translations) this.GravVimeoPlugin.translations = '.json_encode($translations, JSON_UNESCAPED_SLASHES) .';';
  71. $this->grav['assets']->addInlineJs($code);
  72. $this->grav['assets']->add('plugin://vimeo/admin/editor-button/js/button.js');
  73. }
  74. }
  75. /**
  76. * After a page has been found, header processed, but content not processed.
  77. * @param Event $e Event
  78. */
  79. public function onPageContentRaw(Event $e)
  80. {
  81. $page = $e['page'];
  82. $config = $this->mergeConfig($page, true);
  83. if (!$config->get('enabled')) return;
  84. // Function
  85. $twig = $this->grav['twig'];
  86. $function = function ($matches) use ($twig, $config) {
  87. $search = $matches[0];
  88. // double check to make sure we found a valid Vimeo video ID
  89. if (!isset($matches[1])) return $search;
  90. // build the replacement embed HTML string
  91. $replace = $twig->processTemplate('partials/vimeo.html.twig', [
  92. 'video_id' => $matches[1],
  93. 'player_parameters' => $config->get('player_parameters', [])
  94. ]);
  95. // do the replacement
  96. return str_replace($search, $replace, $search);
  97. };
  98. $raw_content = $page->getRawContent();
  99. $page->setRawContent($this->parseLinks($raw_content, $function, static::VIMEO_REGEX));
  100. }
  101. /**
  102. * Add Vimeo twig extension
  103. */
  104. public function onTwigExtensions()
  105. {
  106. require_once __DIR__ . '/classes/Twig/VimeoTwigExtension.php';
  107. $this->grav['twig']->twig->addExtension(new VimeoTwigExtension());
  108. }
  109. /**
  110. * Add plugin templates to twig path
  111. */
  112. public function onTwigTemplatePaths()
  113. {
  114. $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
  115. }
  116. }