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_Function( $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 | |||||||
55 | $templates[] = "{$slug}.twig"; |
||||||
56 | |||||||
57 | return $templates; |
||||||
58 | } |
||||||
59 | |||||||
60 | public function get_footer( \Twig_Environment $env, $context, $name = null ) { |
||||||
61 | |||||||
62 | return $this->get_template( $env, $context, 'footer', $name ); |
||||||
63 | } |
||||||
64 | |||||||
65 | public function get_sidebar( \Twig_Environment $env, $context, $name = null ) { |
||||||
66 | |||||||
67 | return $this->get_template( $env, $context, 'sidebar', $name ); |
||||||
68 | } |
||||||
69 | |||||||
70 | public function get_template_part( \Twig_Environment $env, $context, $slug, $name = null ) { |
||||||
71 | |||||||
72 | try { |
||||||
73 | $return = twig_include( $env, $context, $this->get_templates( $slug, $name ) ); |
||||||
74 | do_action( "get_template_part_{$slug}", $slug, $name ); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
75 | } catch ( \Twig_Error_Loader $e ) { |
||||||
76 | ob_start(); |
||||||
77 | get_template_part( $slug, $name ); |
||||||
0 ignored issues
–
show
The function
get_template_part was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
78 | $return = ob_get_clean(); |
||||||
79 | } |
||||||
80 | |||||||
81 | return $return; |
||||||
82 | } |
||||||
83 | |||||||
84 | /** |
||||||
85 | * Skips rendering in native function in favor of Plugin->get_search_form() in filter |
||||||
86 | * |
||||||
87 | * @return string |
||||||
88 | */ |
||||||
89 | public function get_search_form() { |
||||||
90 | |||||||
91 | return apply_filters( 'get_search_form', true ); |
||||||
0 ignored issues
–
show
The function
apply_filters was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
92 | } |
||||||
93 | |||||||
94 | protected function get_template( \Twig_Environment $env, $context, $type, $name = null ) { |
||||||
95 | |||||||
96 | try { |
||||||
97 | $return = twig_include( $env, $context, $this->get_templates( $type, $name ) ); |
||||||
98 | do_action( 'get_' . $type, $name ); |
||||||
0 ignored issues
–
show
The function
do_action was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
99 | } catch ( \Twig_Error_Loader $e ) { |
||||||
100 | ob_start(); |
||||||
101 | \call_user_func( 'get_' . $type, $name ); |
||||||
102 | $return = ob_get_clean(); |
||||||
103 | } |
||||||
104 | |||||||
105 | return $return; |
||||||
106 | } |
||||||
107 | |||||||
108 | public function comments_template( \Twig_Environment $env, $context, $file = 'comments.twig', $separate_comments = false ) { |
||||||
109 | |||||||
110 | try { |
||||||
111 | $env->load( $file ); |
||||||
112 | } catch ( \Twig_Error_Loader $e ) { |
||||||
113 | ob_start(); |
||||||
114 | comments_template( '/comments.php', $separate_comments ); |
||||||
0 ignored issues
–
show
The function
comments_template was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
115 | |||||||
116 | return ob_get_clean(); |
||||||
117 | } |
||||||
118 | |||||||
119 | add_filter( 'comments_template', array( $this, 'return_blank_template' ) ); |
||||||
0 ignored issues
–
show
The function
add_filter was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
120 | comments_template( '/comments.php', $separate_comments ); |
||||||
121 | remove_filter( 'comments_template', array( $this, 'return_blank_template' ) ); |
||||||
0 ignored issues
–
show
The function
remove_filter was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
122 | |||||||
123 | return twig_include( $env, $context, $file ); |
||||||
124 | } |
||||||
125 | |||||||
126 | public function return_blank_template() { |
||||||
127 | |||||||
128 | return __DIR__ . '/blank.php'; |
||||||
129 | } |
||||||
130 | } |