diff options
author | Daniel Weipert <code@drogueronin.de> | 2021-03-10 16:13:52 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2021-03-10 16:13:52 +0100 |
commit | 9b8832902cc1a51b5c6e2e0890c576372a60e5e1 (patch) | |
tree | e7f1db7c2e920b63bde306fda71c187d44a7c6c1 /app |
Diffstat (limited to 'app')
-rw-r--r-- | app/Site.php | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/app/Site.php b/app/Site.php new file mode 100644 index 0000000..5e45535 --- /dev/null +++ b/app/Site.php @@ -0,0 +1,87 @@ +<?php + +namespace Theme; + +use Dweipert\WpEnqueueAssets\UsesWordPressScripts; +use Timber\Menu; +use Timber\Timber; +use Twig\Environment; +use Twig\Extension\StringLoaderExtension; +use Twig\TwigFunction; + +class Site extends \Timber\Site +{ + use UsesWordPressScripts; + + /** + * Site constructor. + */ + public function __construct() + { + add_action('after_setup_theme', [$this, 'themeSupports']); + add_action('wp_enqueue_scripts', [$this, 'enqueueScripts']); + add_filter('timber/context', [$this, 'addToContext']); + add_filter('timber/twig', [$this, 'addToTwig']); + + Timber::$dirname = 'resources/views'; + + parent::__construct(); + } + + /** + * Add Theme supports + * after_setup_theme callback + */ + public function themeSupports() + { + add_theme_support('title-tag'); + add_theme_support('post-thumbnails'); + add_theme_support('html5'); + + // menu + add_theme_support('menus'); + register_nav_menus([ + 'main' => 'Main', + 'footer' => 'Footer', + ]); + } + + /** + * wp_enqueue_scripts callback + */ + public function enqueueScripts() + { + $this->enqueueStyle('theme', 'index.css'); + $this->enqueueScript('theme', 'index.js', ['jquery'], true); + } + + /** + * @param array $context + * + * @return mixed + */ + public function addToContext($context) + { + $context['site'] = $this; + + $context['menu'] = []; + foreach (get_registered_nav_menus() as $location => $name) { + $context['menu'][$location] = new Menu($location); + } + + return $context; + } + + /** + * @param Environment $twig + * + * @return Environment + */ + public function addToTwig(Environment $twig) + { + $twig->addExtension(new StringLoaderExtension()); + $twig->addFunction(new TwigFunction('assets_url', [$this, 'assetsUrl'])); + + return $twig; + } +} |