advanced-pagecache.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Grav\Plugin;
  3. use \Grav\Common\Plugin;
  4. use \Grav\Common\Uri;
  5. use Grav\Common\User\DataUser\User;
  6. class AdvancedPageCachePlugin extends Plugin
  7. {
  8. /** @var Config $config */
  9. protected $config;
  10. protected $pagecache_key;
  11. /**
  12. * @return array
  13. */
  14. public static function getSubscribedEvents()
  15. {
  16. return [
  17. 'onPluginsInitialized' => ['onPluginsInitialized', 0]
  18. ];
  19. }
  20. /**
  21. * Return `true` if the page has no extension, or has the default page extension.
  22. * Return `false` if for example is a RSS version of the page
  23. */
  24. private function isValidExtension() {
  25. /** @var Uri $uri */
  26. $uri = $this->grav['uri'];
  27. $extension = $uri->extension();
  28. if (!$extension) {
  29. return true;
  30. }
  31. $disabled_extensions = $this->grav['config']->get('plugins.advanced-pagecache.disabled_extensions');
  32. if (in_array($extension, (array) $disabled_extensions)) {
  33. return false;
  34. }
  35. return true;
  36. }
  37. /**
  38. * Initialize configuration
  39. */
  40. public function onPluginsInitialized()
  41. {
  42. $config = $this->grav['config']->get('plugins.advanced-pagecache');
  43. /** @var Uri $uri */
  44. $uri = $this->grav['uri'];
  45. $full_route = $uri->uri();
  46. $route = str_replace($uri->baseIncludingLanguage(), '', $full_route);
  47. $params = $uri->params(null, true);
  48. $query = $uri->query(null, true);
  49. $user = $this->grav['user'] ?? new User();
  50. $lang = $this->grav['language']->getLanguageURLPrefix();
  51. // Definitely don't run in admin plugin or is not a valid extension
  52. if ($this->isAdmin() || !$this->isValidExtension()) {
  53. return;
  54. }
  55. // If this url is not whitelisted try some other tests
  56. if (!in_array($route, (array)$config['whitelist'])) {
  57. // do not run in these scenarios
  58. if ($config['disabled_with_params'] && !empty($params) ||
  59. $config['disabled_with_query'] && !empty($query) ||
  60. $config['disabled_on_login'] && $user["authenticated"] ||
  61. in_array($route, (array)$config['blacklist'])) {
  62. return;
  63. }
  64. }
  65. if ($config['per_user_caching']) {
  66. $this->pagecache_key = md5('adv-pc-' . $lang . $full_route . $user["username"]);
  67. } else {
  68. $this->pagecache_key = md5('adv-pc-' . $lang . $full_route);
  69. }
  70. // Should run and store page
  71. $this->enable([
  72. 'onOutputGenerated' => ['onOutputGenerated', 0]
  73. ]);
  74. $pagecache = $this->grav['cache']->fetch($this->pagecache_key);
  75. if ($pagecache) {
  76. echo $pagecache;
  77. exit;
  78. }
  79. }
  80. /**
  81. * Save the page to the cache
  82. */
  83. public function onOutputGenerated()
  84. {
  85. $this->grav['cache']->save($this->pagecache_key, $this->grav->output);
  86. }
  87. }