auto-author.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\Plugin;
  4. use Grav\Plugin\Admin\Admin;
  5. use RocketTheme\Toolbox\Event\Event;
  6. /**
  7. * Class AutoAuthorPlugin
  8. * @package Grav\Plugin
  9. */
  10. class AutoAuthorPlugin extends Plugin
  11. {
  12. /**
  13. * @return array
  14. *
  15. * The getSubscribedEvents() gives the core a list of events
  16. * that the plugin wants to listen to. The key of each
  17. * array section is the event that the plugin listens to
  18. * and the value (in the form of an array) contains the
  19. * callable (or function) as well as the priority. The
  20. * higher the number the higher the priority.
  21. */
  22. public static function getSubscribedEvents()
  23. {
  24. return [
  25. 'onAdminCreatePageFrontmatter' => ['onAdminCreatePageFrontmatter', 0]
  26. ];
  27. }
  28. /**
  29. * Initialize the plugin
  30. *
  31. * @param Event $event
  32. */
  33. public function onAdminCreatePageFrontmatter(Event $event)
  34. {
  35. $header = $event['header'];
  36. if (!isset($header['author'])) {
  37. if ($this->config->get('plugins.auto-author.user')) {
  38. /**
  39. * @var Admin $admin
  40. */
  41. $admin = $this->grav['admin'];
  42. $author = $admin->user->get('fullname');
  43. } else {
  44. $author = $this->grav['config']->get('site.author.name');
  45. }
  46. $header['author'] = $author;
  47. $event['header'] = $header;
  48. }
  49. }
  50. }