-
Notifications
You must be signed in to change notification settings - Fork 0
/
timber-custom-loader.php
87 lines (72 loc) · 2.67 KB
/
timber-custom-loader.php
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
/*
Plugin Name: Timber Custom Loader
Description: This plugin adds a custom Twig loader to Timber
Version: 1.0.0
Author: Laurence Archer
Author URI: https://ozpital.com
*/
class TimberCustomLoader {
public function __construct() {
// Settings
$this->version = '1.0.0';
$this->name = 'Timber Custom Loader';
$this->slug = 'timber-custom-loader';
// Do Admin Stuff
if (is_admin()) {
add_action('admin_menu', [$this, 'admin_menu']);
add_action('admin_init', [$this, 'register_admin_settings']);
}
// Do User Stuff
if (!is_admin()) {
add_action('plugins_loaded', [$this, 'plugin_checks']);
}
}
public function admin_menu() {
add_options_page(
$this->name,
$this->name,
'manage_options',
$this->slug,
[$this, 'admin_page']
);
}
public function admin_page() {
?>
<div class="wrap">
<h1><?php echo $this->name; ?></h1>
<form method="post" action="options.php">
<?php settings_fields($this->slug . '-options'); ?>
<?php do_settings_sections($this->slug . '-options'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Twig templates directory</th>
<td><input type="text" name="twig_templates_directory" value="<?php echo esc_attr(get_option('twig_templates_directory')); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Template file name and extention</th>
<td><input type="text" name="twig_template_filename" value="<?php echo esc_attr(get_option('twig_template_filename')); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public function register_admin_settings() {
register_setting($this->slug . '-options', 'twig_templates_directory');
register_setting($this->slug . '-options', 'twig_template_filename');
}
public function plugin_checks() {
add_filter('timber/loader/loader', [$this, 'add_loader']);
}
public function add_loader($loader) {
require_once('loader/loader.php');
$paths = get_option('twig_templates_directory');
$filename = get_option('twig_template_filename');
$customLoader = new TwigLoader($paths, $filename);
$chainLoader = new \Twig_Loader_Chain([$loader, $customLoader]);
return $chainLoader;
}
}
new TimberCustomLoader();