cookieconsent.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\Plugin;
  4. /**
  5. * Class CookieconsentPlugin
  6. * @package Grav\Plugin
  7. */
  8. class CookieconsentPlugin extends Plugin
  9. {
  10. /**
  11. * @return array
  12. *
  13. * The getSubscribedEvents() gives the core a list of events
  14. * that the plugin wants to listen to. The key of each
  15. * array section is the event that the plugin listens to
  16. * and the value (in the form of an array) contains the
  17. * callable (or function) as well as the priority. The
  18. * higher the number the higher the priority.
  19. */
  20. public static function getSubscribedEvents()
  21. {
  22. return [
  23. 'onPluginsInitialized' => ['onPluginsInitialized', 0]
  24. ];
  25. }
  26. /**
  27. * Initialize the plugin
  28. */
  29. public function onPluginsInitialized()
  30. {
  31. // Don't proceed if we are in the admin plugin
  32. if ($this->isAdmin()) {
  33. return;
  34. }
  35. // Enable the main event we are interested in
  36. $this->enable([
  37. 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
  38. 'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
  39. ]);
  40. }
  41. /**
  42. * Add current directory to twig lookup paths.
  43. */
  44. public function onTwigTemplatePaths()
  45. {
  46. $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
  47. }
  48. /**
  49. * if enabled on this page, load the JS + CSS theme.
  50. */
  51. public function onTwigSiteVariables()
  52. {
  53. $twig = $this->grav['twig'];
  54. $config = $this->config->toArray();
  55. if ($this->config->get('plugins.cookieconsent.cdn')) {
  56. $this->grav['assets']->addCss("//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.6/cookieconsent.min.css");
  57. $this->grav['assets']->addJs("//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.6/cookieconsent.min.js");
  58. } else {
  59. $this->grav['assets']->addCss("plugin://cookieconsent/assets/cookieconsent.min.css");
  60. $this->grav['assets']->addJs("plugin://cookieconsent/assets/cookieconsent.min.js");
  61. }
  62. $this->grav['assets']->addInlineJs($twig->twig->render('partials/cookieconsent.html.twig', array('config' => $config)));
  63. }
  64. }