1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rarst\Meadow; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Meadow extension for Twig with WordPress specific functionality. |
7
|
|
|
*/ |
8
|
|
|
class Extension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface { |
9
|
|
|
|
10
|
|
|
public function getName() { |
11
|
|
|
return 'meadow'; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
public function getFunctions() { |
15
|
|
|
|
16
|
|
|
$options = array( |
17
|
|
|
'needs_environment' => true, |
18
|
|
|
'needs_context' => true, |
19
|
|
|
'is_safe' => array( 'all' ) |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
$functions = array(); |
23
|
|
|
|
24
|
|
|
foreach ( array( 'get_header', 'get_footer', 'get_sidebar', 'get_template_part', 'get_search_form', 'comments_template' ) as $function ) { |
25
|
|
|
$functions[] = new \Twig_SimpleFunction( $function, array( $this, $function ), $options ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return $functions; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getGlobals() { |
32
|
|
|
|
33
|
|
|
global $wp_query; |
|
|
|
|
34
|
|
|
|
35
|
|
|
return compact( 'wp_query' ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getTokenParsers( ) { |
39
|
|
|
|
40
|
|
|
return array( |
41
|
|
|
new Loop_Token_Parser(), |
42
|
|
|
new Comments_Token_Parser(), |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function get_header( \Twig_Environment $env, $context, $name = null ) { |
47
|
|
|
|
48
|
|
|
return $this->get_template( $env, $context, 'header', $name ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function get_templates( $slug, $name = null ) { |
52
|
|
|
|
53
|
|
|
$templates = array(); |
54
|
|
|
|
55
|
|
|
if ( ! empty( $name ) ) |
56
|
|
|
$templates[] = "{$slug}-{$name}.twig"; |
57
|
|
|
|
58
|
|
|
$templates[] = "{$slug}.twig"; |
59
|
|
|
|
60
|
|
|
return $templates; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function get_footer( \Twig_Environment $env, $context, $name = null ) { |
64
|
|
|
|
65
|
|
|
return $this->get_template( $env, $context, 'footer', $name ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function get_sidebar( \Twig_Environment $env, $context, $name = null ) { |
69
|
|
|
|
70
|
|
|
return $this->get_template( $env, $context, 'sidebar', $name ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
View Code Duplication |
public function get_template_part( \Twig_Environment $env, $context, $slug, $name = null ) { |
|
|
|
|
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
$return = twig_include( $env, $context, $this->get_templates( $slug, $name ) ); |
77
|
|
|
do_action( "get_template_part_{$slug}", $slug, $name ); |
78
|
|
|
} catch ( \Twig_Error_Loader $e ) { |
79
|
|
|
ob_start(); |
80
|
|
|
get_template_part( $slug, $name ); |
81
|
|
|
$return = ob_get_clean(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Skips rendering in native function in favor of Plugin->get_search_form() in filter |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function get_search_form() { |
93
|
|
|
|
94
|
|
|
return apply_filters( 'get_search_form', true ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
View Code Duplication |
protected function get_template( \Twig_Environment $env, $context, $type, $name = null ) { |
|
|
|
|
98
|
|
|
|
99
|
|
|
try { |
100
|
|
|
$return = twig_include( $env, $context, $this->get_templates( $type, $name ) ); |
101
|
|
|
do_action( 'get_' . $type, $name ); |
102
|
|
|
} catch ( \Twig_Error_Loader $e ) { |
103
|
|
|
ob_start(); |
104
|
|
|
call_user_func( 'get_' . $type, $name ); |
105
|
|
|
$return = ob_get_clean(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function comments_template( \Twig_Environment $env, $context, $file = 'comments.twig', $separate_comments = false ) { |
112
|
|
|
|
113
|
|
|
try { |
114
|
|
|
$env->loadTemplate( $file ); |
115
|
|
|
} catch ( \Twig_Error_Loader $e ) { |
116
|
|
|
ob_start(); |
117
|
|
|
comments_template( '/comments.php', $separate_comments ); |
118
|
|
|
|
119
|
|
|
return ob_get_clean(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
add_filter( 'comments_template', array( $this, 'return_blank_template' ) ); |
123
|
|
|
comments_template( '/comments.php', $separate_comments ); |
124
|
|
|
remove_filter( 'comments_template', array( $this, 'return_blank_template' ) ); |
125
|
|
|
|
126
|
|
|
return twig_include( $env, $context, $file ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function return_blank_template() { |
130
|
|
|
|
131
|
|
|
return __DIR__ . '/blank.php'; |
132
|
|
|
} |
133
|
|
|
} |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state