summaryrefslogtreecommitdiff
path: root/app/Site.php
blob: 5e4553596d56943af8d9fcaf98a95f0dfe16ceab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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;
    }
}