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